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
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! */
29 #include "cppdefault.h"
31 typedef unsigned char U_CHAR
;
33 /* Name under which this program was invoked. */
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. */
52 /* Nonzero means don't output line number information. */
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. */
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. */
70 /* Nonzero causes output not to be done,
71 but directives such as #define that have side effects
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
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. */
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. */
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"); \
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 */
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)); \
134 struct file_name_list
136 struct file_name_list
*next
;
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
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,
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
169 { (0, 1), (1, 1), (1, 1), ..., (1, 1), NULL }
170 where (x, y) means (nchars, argno). */
172 typedef struct definition DEFINITION
;
175 int length
; /* length of expansion string */
178 struct reflist
*next
;
179 char stringify
; /* nonzero if this arg was preceded by a
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) */
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. */
194 /* different kinds of things that can appear in the value field
195 of a hash node. Actually, this may be useless now. */
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 */
211 T_DEFINE
= 1, /* `#define' */
212 T_INCLUDE
, /* `#include' */
213 T_IFDEF
, /* `#ifdef' */
214 T_IFNDEF
, /* `#ifndef' */
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. */
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. */
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
279 `free1' and `free2', if nonzero, point to blocks to be freed
280 when the macro argument data is no longer needed. */
283 U_CHAR
*raw
, *expanded
;
284 int raw_length
, expand_length
;
285 int stringified_length
;
286 U_CHAR
*free1
, *free2
;
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
303 struct arglist
*next
;
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
348 #if (GCC_VERSION >= 2007)
349 #define abort() fancy_abort(__LINE__, __FUNCTION__)
351 #define abort() fancy_abort(__LINE__, 0);
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,
427 /* Stack of conditionals currently in progress
428 (including both successful and failing conditionals). */
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. */
445 /* Number of bytes allocated in above. */
446 int deps_allocated_size
;
448 /* Number of bytes used. */
451 /* Number of bytes since the last newline. */
454 /* Nonzero means -I- has been seen,
455 so don't look for #include "foo" the source-file directory. */
465 const char *in_fname
, *out_fname
;
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;
482 /* Get rid of any avoidable limit on stack size. */
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 */
499 /* Initialize is_idchar to allow $. */
500 initialize_char_syntax ();
502 no_line_commands
= 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
)
523 switch (argv
[i
][1]) {
528 break; /* Ignore for compatibility with ISO/extended cpp. */
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. */
546 if (!strcmp (argv
[i
], "-include"))
549 fatal ("Filename missing after -i option");
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 */
564 if (out_fname
!= NULL
)
565 fatal ("Output filename specified twice");
567 fatal ("Filename missing after -o option");
568 out_fname
= argv
[++i
];
569 if (!strcmp (out_fname
, "-"))
574 inhibit_warnings
= 1;
578 if (!strcmp (argv
[i
], "-Wcomments"))
580 else if (!strcmp (argv
[i
], "-Wcomment"))
582 else if (!strcmp (argv
[i
], "-Wall")) {
588 if (!strcmp (argv
[i
], "-fleading-underscore"))
589 user_label_prefix
= "_";
590 else if (!strcmp (argv
[i
], "-fno-leading-underscore"))
591 user_label_prefix
= "";
595 if (!strcmp (argv
[i
], "-M"))
597 else if (!strcmp (argv
[i
], "-MM"))
608 fprintf (stderr
, "GNU traditional CPP version %s\n", version_string
);
617 else if (i
+ 1 == argc
)
618 fatal ("Macro name missing after -D option");
626 case 'U': /* JF #undef something */
628 pend_undefs
[i
] = argv
[i
] + 2;
629 else if (i
+ 1 == argc
)
630 fatal ("Macro name missing after -U option");
632 pend_undefs
[i
] = argv
[i
+1], i
++;
636 put_out_comments
= 1;
640 if (!strcmp (argv
[i
], "-pedantic"))
641 fatal ("-pedantic and -traditional are mutually exclusive");
645 if (!strcmp (argv
[i
], "-trigraphs"))
646 fatal ("-trigraphs and -traditional are mutually exclusive");
650 no_line_commands
= 1;
653 case 'I': /* Add directory to path for includes. */
656 struct file_name_list
*dirtmp
;
658 if (! ignore_srcdir
&& !strcmp (argv
[i
] + 2, "-"))
661 dirtmp
= (struct file_name_list
*)
662 xmalloc (sizeof (struct file_name_list
));
663 dirtmp
->next
= 0; /* New one goes on the end */
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");
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
;
684 /* -nostdinc causes no default include directories.
685 You must specify all include-file directories with -I. */
686 no_standard_includes
= 1;
689 case '\0': /* JF handle '-' as file name meaning stdin or stdout */
690 if (in_fname
== NULL
) {
693 } else if (out_fname
== NULL
) {
696 } /* else fall through into error */
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 and undefines specified with -U. */
715 for (i
= 1; i
< argc
; i
++)
717 make_definition ((U_CHAR
*)pend_defs
[i
]);
718 else if (pend_undefs
[i
])
719 make_undef ((U_CHAR
*)pend_undefs
[i
]);
721 /* Unless -fnostdinc,
722 tack on the standard include file dirs to the specified list */
723 if (!no_standard_includes
) {
724 const struct default_include
*di
;
725 struct file_name_list
*old_last_include
= last_include
;
726 struct file_name_list
*dirtmp
;
727 for (di
= cpp_include_defaults
; di
->fname
; di
++) {
730 dirtmp
= (struct file_name_list
*)
731 xmalloc (sizeof (struct file_name_list
));
732 dirtmp
->next
= 0; /* New one goes on the end */
736 last_include
->next
= dirtmp
;
737 last_include
= dirtmp
; /* Tail follows the last one */
738 dirtmp
->fname
= di
->fname
;
739 if (strlen (dirtmp
->fname
) > max_include_len
)
740 max_include_len
= strlen (dirtmp
->fname
);
743 if (ignore_srcdir
&& first_bracket_include
== 0)
744 first_bracket_include
= old_last_include
->next
;
747 /* Initialize output buffer */
749 outbuf
.buf
= (U_CHAR
*) xmalloc (OUTBUF_SIZE
);
750 outbuf
.bufp
= outbuf
.buf
;
751 outbuf
.length
= OUTBUF_SIZE
;
753 /* Scan the -i files before the main input.
754 Much like #including them, but with no_output set
755 so that only their macro definitions matter. */
758 for (i
= 1; i
< argc
; i
++)
760 int fd
= open (pend_files
[i
], O_RDONLY
, 0666);
762 perror_with_name (pend_files
[i
]);
763 return FATAL_EXIT_CODE
;
765 finclude (fd
, pend_files
[i
], &outbuf
);
769 /* Create an input stack level for the main input file
770 and copy the entire contents of the file into it. */
772 fp
= &instack
[++indepth
];
774 /* JF check for stdin */
775 if (in_fname
== NULL
|| *in_fname
== 0) {
778 } else if ((f
= open (in_fname
, O_RDONLY
, 0666)) < 0)
781 /* Either of two environment variables can specify output of deps.
782 Its value is either "OUTPUT_FILE" or "OUTPUT_FILE DEPS_TARGET",
783 where OUTPUT_FILE is the file to write deps info to
784 and DEPS_TARGET is the target to mention in the deps. */
787 && (getenv ("SUNPRO_DEPENDENCIES") != 0
788 || getenv ("DEPENDENCIES_OUTPUT") != 0))
790 char *spec
= getenv ("DEPENDENCIES_OUTPUT");
796 spec
= getenv ("SUNPRO_DEPENDENCIES");
802 /* Find the space before the DEPS_TARGET, if there is one. */
803 s
= strchr (spec
, ' ');
807 output_file
= (char *) xmalloc (s
- spec
+ 1);
808 memcpy (output_file
, spec
, s
- spec
);
809 output_file
[s
- spec
] = 0;
817 deps_stream
= fopen (output_file
, "a");
818 if (deps_stream
== 0)
819 pfatal_with_name (output_file
);
821 /* If the -M option was used, output the deps to standard output. */
823 deps_stream
= stdout
;
825 /* For -M, print the expected object file name
826 as the target of this Make-rule. */
828 deps_allocated_size
= 200;
829 deps_buffer
= (char *) xmalloc (deps_allocated_size
);
835 deps_output (deps_target
, 0);
836 deps_output (":", 0);
837 } else if (*in_fname
== 0)
838 deps_output ("-: ", 0);
841 const char *p
= in_fname
;
843 /* Discard all directory prefixes from P. */
849 /* Output P, but remove known suffixes. */
851 if (p
[len
- 2] == '.'
852 && (p
[len
- 1] == 'c' || p
[len
- 1] == 'C' || p
[len
- 1] == 'S'))
853 deps_output (p
, len
- 2);
854 else if (p
[len
- 3] == '.'
856 && p
[len
- 1] == 'c')
857 deps_output (p
, len
- 3);
860 /* Supply our own suffix. */
861 deps_output (".o : ", 0);
862 deps_output (in_fname
, 0);
863 deps_output (" ", 0);
867 if (file_size_and_mode (f
, &st_mode
, &st_size
))
869 fp
->fname
= in_fname
;
871 /* JF all this is mine about reading pipes and ttys */
872 if (!S_ISREG (st_mode
)) {
873 /* Read input from a file that is not a normal disk file.
874 We cannot preallocate a buffer with the correct size,
875 so we must read in the file a piece at the time and make it bigger. */
883 fp
->buf
= (U_CHAR
*) xmalloc (bsize
+ 2);
886 cnt
= read (f
, bufp
, bsize
- size
);
887 if (cnt
< 0) goto sys_error
; /* error! */
888 if (cnt
== 0) break; /* End of file */
891 if (bsize
== size
) { /* Buffer is full! */
893 fp
->buf
= (U_CHAR
*) xrealloc (fp
->buf
, bsize
+ 2);
894 bufp
= fp
->buf
+ size
; /* May have moved */
899 /* Read a file whose size we can determine in advance.
900 For the sake of VMS, st_size is just an upper bound. */
903 fp
->buf
= (U_CHAR
*) xmalloc (st_size
+ 2);
905 while (st_size
> 0) {
906 i
= read (f
, fp
->buf
+ fp
->length
, st_size
);
916 fp
->if_stack
= if_stack
;
918 /* Make sure data ends with a newline. And put a null after it. */
920 if (fp
->length
> 0 && fp
->buf
[fp
->length
-1] != '\n')
921 fp
->buf
[fp
->length
++] = '\n';
922 fp
->buf
[fp
->length
] = '\0';
924 /* Now that we know the input file is valid, open the output. */
926 if (!out_fname
|| !strcmp (out_fname
, ""))
927 out_fname
= "stdout";
928 else if (! freopen (out_fname
, "w", stdout
))
929 pfatal_with_name (out_fname
);
931 output_line_command (fp
, &outbuf
, 0, same_file
);
933 /* Scan the input, processing macros and directives. */
937 /* Now we have processed the entire input
938 Write whichever kind of output has been requested. */
943 else if (! inhibit_output
&& deps_stream
!= stdout
) {
944 if (write (fileno (stdout
), outbuf
.buf
, outbuf
.bufp
- outbuf
.buf
) < 0)
945 fatal ("I/O error on output");
949 fputs (deps_buffer
, deps_stream
);
950 putc ('\n', deps_stream
);
951 if (deps_stream
!= stdout
) {
952 fclose (deps_stream
);
953 if (ferror (deps_stream
))
954 fatal ("I/O error on output");
959 fatal ("I/O error on output");
962 exit (FATAL_EXIT_CODE
);
963 exit (SUCCESS_EXIT_CODE
);
966 pfatal_with_name (in_fname
);
969 /* Move all backslash-newline pairs out of embarrassing places.
970 Exchange all such pairs following BP
971 with any potentially-embarrasing characters that follow them.
972 Potentially-embarrassing characters are / and *
973 (because a backslash-newline inside a comment delimiter
974 would cause it not to be recognized). */
979 register U_CHAR
*p
= bp
;
980 register int count
= 0;
982 /* First count the backslash-newline pairs here. */
984 while (*p
++ == '\\' && *p
++ == '\n')
989 /* Exit if what follows the backslash-newlines is not embarrassing. */
991 if (count
== 0 || (*p
!= '/' && *p
!= '*'))
994 /* Copy all potentially embarrassing characters
995 that follow the backslash-newline pairs
996 down to where the pairs originally started. */
998 while (*p
== '*' || *p
== '/')
1001 /* Now write the same number of pairs after the embarrassing chars. */
1002 while (count
-- > 0) {
1008 /* Like newline_fix but for use within a directive-name.
1009 Move any backslash-newlines up past any following symbol constituents. */
1011 name_newline_fix (bp
)
1014 register U_CHAR
*p
= bp
;
1015 register int count
= 0;
1017 /* First count the backslash-newline pairs here. */
1019 while (*p
++ == '\\' && *p
++ == '\n')
1024 /* What follows the backslash-newlines is not embarrassing. */
1026 if (count
== 0 || !is_idchar
[*p
])
1029 /* Copy all potentially embarrassing characters
1030 that follow the backslash-newline pairs
1031 down to where the pairs originally started. */
1033 while (is_idchar
[*p
])
1036 /* Now write the same number of pairs after the embarrassing chars. */
1037 while (count
-- > 0) {
1044 * The main loop of the program.
1046 * Read characters from the input stack, transferring them to the
1049 * Macros are expanded and push levels on the input stack.
1050 * At the end of such a level it is popped off and we keep reading.
1051 * At the end of any other kind of level, we return.
1052 * #-directives are handled, except within macros.
1054 * If OUTPUT_MARKS is nonzero, keep Newline markers found in the input
1055 * and insert them when appropriate. This is set while scanning macro
1056 * arguments before substitution. It is zero when scanning for final output.
1057 * There are three types of Newline markers:
1058 * * Newline - follows a macro name that was not expanded
1059 * because it appeared inside an expansion of the same macro.
1060 * This marker prevents future expansion of that identifier.
1061 * When the input is rescanned into the final output, these are deleted.
1062 * These are also deleted by ## concatenation.
1063 * * Newline Space (or Newline and any other whitespace character)
1064 * stands for a place that tokens must be separated or whitespace
1065 * is otherwise desirable, but where the ANSI standard specifies there
1066 * is no whitespace. This marker turns into a Space (or whichever other
1067 * whitespace char appears in the marker) in the final output,
1068 * but it turns into nothing in an argument that is stringified with #.
1069 * Such stringified arguments are the only place where the ANSI standard
1070 * specifies with precision that whitespace may not appear.
1072 * During this function, IP->bufp is kept cached in IBP for speed of access.
1073 * Likewise, OP->bufp is kept in OBP. Before calling a subroutine
1074 * IBP, IP and OBP must be copied back to memory. IP and IBP are
1075 * copied back with the RECACHE macro. OBP must be copied back from OP->bufp
1076 * explicitly, and before RECACHE, since RECACHE uses OBP.
1080 rescan (op
, output_marks
)
1084 /* Character being scanned in main loop. */
1087 /* Length of pending accumulated identifier. */
1088 register int ident_length
= 0;
1090 /* Hash code of pending accumulated identifier. */
1091 register int hash
= 0;
1093 /* Current input level (&instack[indepth]). */
1096 /* Pointer for scanning input. */
1097 register U_CHAR
*ibp
;
1099 /* Pointer to end of input. End of scan is controlled by LIMIT. */
1100 register U_CHAR
*limit
;
1102 /* Pointer for storing output. */
1103 register U_CHAR
*obp
;
1105 /* REDO_CHAR is nonzero if we are processing an identifier
1106 after backing up over the terminating character.
1107 Sometimes we process an identifier without backing up over
1108 the terminating character, if the terminating character
1109 is not special. Backing up is done so that the terminating character
1110 will be dispatched on again once the identifier is dealt with. */
1113 /* 1 if within an identifier inside of which a concatenation
1114 marker (Newline -) has been seen. */
1115 int concatenated
= 0;
1117 /* While scanning a comment or a string constant,
1118 this records the line it started on, for error messages. */
1121 /* Record position of last `real' newline. */
1122 U_CHAR
*beg_of_line
;
1124 /* Pop the innermost input stack level, assuming it is a macro expansion. */
1127 do { ip->macro->type = T_MACRO; \
1128 if (ip->free_ptr) free (ip->free_ptr); \
1129 --indepth; } while (0)
1131 /* Reload `rescan's local variables that describe the current
1132 level of the input stack. */
1135 do { ip = &instack[indepth]; \
1137 limit = ip->buf + ip->length; \
1139 check_expand (op, limit - ibp); \
1141 obp = op->bufp; } while (0)
1143 if (no_output
&& instack
[indepth
].fname
!= 0)
1144 skip_if_group (&instack
[indepth
], 1);
1150 /* Our caller must always put a null after the end of
1151 the input at each input stack level. */
1164 /* Always merge lines ending with backslash-newline,
1165 even in middle of identifier. */
1168 --obp
; /* remove backslash from obuf */
1171 /* Otherwise, backslash suppresses specialness of following char,
1172 so copy it here to prevent the switch from seeing it.
1173 But first get any pending identifier processed. */
1174 if (ident_length
> 0)
1180 /* If this is expanding a macro definition, don't recognize
1181 preprocessor directives. */
1187 /* # keyword: a # must be the first char on the line */
1188 if (beg_of_line
== 0)
1190 if (beg_of_line
+ 1 != ibp
)
1193 /* This # can start a directive. */
1195 --obp
; /* Don't copy the '#' */
1199 if (! handle_directive (ip
, op
)) {
1203 /* Not a known directive: treat it as ordinary text.
1204 IP, OP, IBP, etc. have not been changed. */
1205 if (no_output
&& instack
[indepth
].fname
) {
1206 /* If not generating expanded output,
1207 what we do with ordinary text is skip it.
1208 Discard everything until next # directive. */
1209 skip_if_group (&instack
[indepth
], 1);
1214 ++obp
; /* Copy the '#' after all */
1220 /* A # directive has been successfully processed. */
1221 /* If not generating expanded output, ignore everything until
1222 next # directive. */
1223 if (no_output
&& instack
[indepth
].fname
)
1224 skip_if_group (&instack
[indepth
], 1);
1230 case '\"': /* skip quoted string */
1232 /* A single quoted string is treated like a double -- some
1233 programs (e.g., troff) are perverse this way */
1238 start_line
= ip
->lineno
;
1240 /* Skip ahead to a matching quote. */
1244 if (ip
->macro
!= 0) {
1245 /* try harder: this string crosses a macro expansion boundary */
1257 /* Traditionally, end of line ends a string constant with no error.
1258 So exit the loop and record the new line. */
1266 /* Backslash newline is replaced by nothing at all,
1267 but keep the line counts correct. */
1272 /* ANSI stupidly requires that in \\ the second \
1273 is *not* prevented from combining with a newline. */
1274 while (*ibp
== '\\' && ibp
[1] == '\n') {
1293 if (*ibp
== '\\' && ibp
[1] == '\n')
1295 /* Don't look for comments inside a macro definition. */
1298 /* A comment constitutes white space, so it can terminate an identifier.
1299 Process the identifier, if any. */
1306 /* We have a comment. Skip it, optionally copying it to output. */
1308 start_line
= ip
->lineno
;
1310 ++ibp
; /* Skip the star. */
1312 /* In K+R C, a comment is equivalent to nothing. Note that we
1313 already output the slash; we might not want it. */
1314 if (! put_out_comments
)
1320 U_CHAR
*before_bp
= ibp
;
1322 while (ibp
< limit
) {
1325 if (warn_comments
&& ibp
< limit
&& *ibp
== '*')
1326 warning("`/*' within comment");
1329 if (*ibp
== '\\' && ibp
[1] == '\n')
1331 if (ibp
>= limit
|| *ibp
== '/')
1336 /* Copy the newline into the output buffer, in order to
1337 avoid the pain of a #line every time a multiline comment
1339 if (!put_out_comments
)
1347 error_with_line (line_for_error (start_line
),
1348 "unterminated comment");
1351 if (put_out_comments
) {
1352 memcpy (obp
, before_bp
, ibp
- before_bp
);
1353 obp
+= ibp
- before_bp
;
1359 case '0': case '1': case '2': case '3': case '4':
1360 case '5': case '6': case '7': case '8': case '9':
1361 /* If digit is not part of identifier, it starts a number,
1362 which means that following letters are not an identifier.
1363 "0x5" does not refer to an identifier "x5".
1364 So copy all alphanumerics that follow without accumulating
1365 as an identifier. Periods also, for sake of "3.e7". */
1367 if (ident_length
== 0) {
1368 while (ibp
< limit
) {
1369 while (ibp
< limit
&& ibp
[0] == '\\' && ibp
[1] == '\n') {
1374 if (!isalnum (c
) && c
!= '.' && c
!= '_') {
1379 /* A sign can be part of a preprocessing number
1380 if it follows an e. */
1381 if (c
== 'e' || c
== 'E') {
1382 while (ibp
< limit
&& ibp
[0] == '\\' && ibp
[1] == '\n') {
1386 if (ibp
< limit
&& (*ibp
== '+' || *ibp
== '-')) {
1388 /* Traditional C does not let the token go past the sign. */
1398 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
1399 case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
1400 case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
1401 case 's': case 't': case 'u': case 'v': case 'w': case 'x':
1403 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
1404 case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
1405 case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
1406 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
1409 /* Compute step of hash function, to avoid a proc call on every token */
1410 hash
= HASHSTEP (hash
, c
);
1414 /* If reprocessing a macro expansion, newline is a special marker. */
1415 if (ip
->macro
!= 0) {
1416 /* Newline White is a "funny space" to separate tokens that are
1417 supposed to be separate but without space between.
1418 Here White means any horizontal whitespace character.
1419 Newline - marks a recursive macro use that is not
1420 supposed to be expandable. */
1423 /* Newline - inhibits expansion of preceding token.
1424 If expanding a macro arg, we keep the newline -.
1425 In final output, it is deleted. */
1426 if (! concatenated
) {
1431 if (!output_marks
) {
1434 /* If expanding a macro arg, keep the newline -. */
1437 } else if (is_space
[*ibp
]) {
1438 /* Newline Space does not prevent expansion of preceding token
1439 so expand the preceding token and then come back. */
1440 if (ident_length
> 0)
1443 /* If generating final output, newline space makes a space. */
1444 if (!output_marks
) {
1446 /* And Newline Newline makes a newline, so count it. */
1447 if (obp
[-1] == '\n')
1450 /* If expanding a macro arg, keep the newline space.
1451 If the arg gets stringified, newline space makes nothing. */
1454 } else abort (); /* Newline followed by something random? */
1458 /* If there is a pending identifier, handle it and come back here. */
1459 if (ident_length
> 0)
1464 /* Update the line counts and output a #line if necessary. */
1467 if (ip
->lineno
!= op
->lineno
) {
1469 output_line_command (ip
, op
, 1, same_file
);
1470 check_expand (op
, ip
->length
- (ip
->bufp
- ip
->buf
));
1475 /* Come here either after (1) a null character that is part of the input
1476 or (2) at the end of the input, because there is a null there. */
1479 /* Our input really contains a null character. */
1482 /* At end of a macro-expansion level, pop it and read next level. */
1483 if (ip
->macro
!= 0) {
1486 /* If we have an identifier that ends here, process it now, so
1487 we get the right error for recursion. */
1488 if (ident_length
&& ! is_idchar
[*instack
[indepth
- 1].bufp
]) {
1497 /* If we don't have a pending identifier,
1498 return at end of input. */
1499 if (ident_length
== 0) {
1507 /* If we do have a pending identifier, just consider this null
1508 a special character and arrange to dispatch on it again.
1509 The second time, IDENT_LENGTH will be zero so we will return. */
1515 /* Handle the case of a character such as /, ', " or null
1516 seen following an identifier. Back over it so that
1517 after the identifier is processed the special char
1518 will be dispatched on again. */
1528 if (ident_length
> 0) {
1529 register HASHNODE
*hp
;
1531 /* We have just seen an identifier end. If it's a macro, expand it.
1533 IDENT_LENGTH is the length of the identifier
1534 and HASH is its hash code.
1536 The identifier has already been copied to the output,
1537 so if it is a macro we must remove it.
1539 If REDO_CHAR is 0, the char that terminated the identifier
1540 has been skipped in the output and the input.
1541 OBP-IDENT_LENGTH-1 points to the identifier.
1542 If the identifier is a macro, we must back over the terminator.
1544 If REDO_CHAR is 1, the terminating char has already been
1545 backed over. OBP-IDENT_LENGTH points to the identifier. */
1547 for (hp
= hashtab
[MAKE_POS (hash
) % HASHSIZE
]; hp
!= NULL
;
1550 if (hp
->length
== ident_length
) {
1551 U_CHAR
*obufp_before_macroname
;
1552 int op_lineno_before_macroname
;
1553 register int i
= ident_length
;
1554 register U_CHAR
*p
= hp
->name
;
1555 register U_CHAR
*q
= obp
- i
;
1560 do { /* All this to avoid a strncmp () */
1565 /* We found a use of a macro name.
1566 see if the context shows it is a macro call. */
1568 /* Back up over terminating character if not already done. */
1574 obufp_before_macroname
= obp
- ident_length
;
1575 op_lineno_before_macroname
= op
->lineno
;
1577 /* If macro wants an arglist, verify that a '(' follows.
1578 first skip all whitespace, copying it to the output
1579 after the macro name. Then, if there is no '(',
1580 decide this is not a macro call and leave things that way. */
1581 if (hp
->type
== T_MACRO
&& hp
->value
.defn
->nargs
>= 0)
1584 /* Scan forward over whitespace, copying it to the output. */
1585 if (ibp
== limit
&& ip
->macro
!= 0) {
1589 /* A comment: copy it unchanged or discard it. */
1590 else if (*ibp
== '/' && ibp
+1 != limit
&& ibp
[1] == '*') {
1591 if (put_out_comments
) {
1596 while (ibp
+ 1 != limit
1597 && !(ibp
[0] == '*' && ibp
[1] == '/')) {
1598 /* We need not worry about newline-marks,
1599 since they are never found in comments. */
1601 /* Newline in a file. Count it. */
1605 if (put_out_comments
)
1611 if (put_out_comments
) {
1616 else if (is_space
[*ibp
]) {
1618 if (ibp
[-1] == '\n') {
1619 if (ip
->macro
== 0) {
1620 /* Newline in a file. Count it. */
1623 } else if (!output_marks
) {
1624 /* A newline mark, and we don't want marks
1625 in the output. If it is newline-hyphen,
1626 discard it entirely. Otherwise, it is
1627 newline-whitechar, so keep the whitechar. */
1637 /* A newline mark; copy both chars to the output. */
1648 /* This is now known to be a macro call.
1649 Discard the macro name from the output,
1650 along with any following whitespace just copied. */
1651 obp
= obufp_before_macroname
;
1652 op
->lineno
= op_lineno_before_macroname
;
1654 /* Expand the macro, reading arguments as needed,
1655 and push the expansion on the input stack. */
1658 macroexpand (hp
, op
);
1660 /* Reexamine input stack, since macroexpand has pushed
1661 a new level on it. */
1668 } /* End hash-table-search loop */
1669 ident_length
= hash
= 0; /* Stop collecting identifier */
1672 } /* End if (ident_length > 0) */
1674 } /* End per-char loop */
1676 /* Come here to return -- but first give an error message
1677 if there was an unterminated successful conditional. */
1679 if (if_stack
!= ip
->if_stack
) {
1681 switch (if_stack
->type
) {
1700 error_with_line (line_for_error (if_stack
->lineno
),
1701 "unterminated #%s conditional", str
);
1703 if_stack
= ip
->if_stack
;
1707 * Rescan a string into a temporary buffer and return the result
1708 * as a FILE_BUF. Note this function returns a struct, not a pointer.
1710 * OUTPUT_MARKS nonzero means keep Newline markers found in the input
1711 * and insert such markers when appropriate. See `rescan' for details.
1712 * OUTPUT_MARKS is 1 for macroexpanding a macro argument separately
1713 * before substitution; it is 0 for other uses.
1716 expand_to_temp_buffer (buf
, limit
, output_marks
)
1717 U_CHAR
*buf
, *limit
;
1720 register FILE_BUF
*ip
;
1722 int length
= limit
- buf
;
1724 int odepth
= indepth
;
1729 /* Set up the input on the input stack. */
1731 buf1
= (U_CHAR
*) alloca (length
+ 1);
1733 register U_CHAR
*p1
= buf
;
1734 register U_CHAR
*p2
= buf1
;
1741 /* Set up to receive the output. */
1743 obuf
.length
= length
* 2 + 100; /* Usually enough. Why be stingy? */
1744 obuf
.bufp
= obuf
.buf
= (U_CHAR
*) xmalloc (obuf
.length
);
1749 CHECK_DEPTH ({return obuf
;});
1753 ip
= &instack
[indepth
];
1757 ip
->length
= length
;
1758 ip
->buf
= ip
->bufp
= buf1
;
1759 ip
->if_stack
= if_stack
;
1761 ip
->lineno
= obuf
.lineno
= 1;
1763 /* Scan the input, create the output. */
1765 rescan (&obuf
, output_marks
);
1767 /* Pop input stack to original state. */
1770 if (indepth
!= odepth
)
1773 /* Record the output. */
1774 obuf
.length
= obuf
.bufp
- obuf
.buf
;
1780 * Process a # directive. Expects IP->bufp to point to the '#', as in
1781 * `#define foo bar'. Passes to the command handler
1782 * (do_define, do_include, etc.): the addresses of the 1st and
1783 * last chars of the command (starting immediately after the #
1784 * keyword), plus op and the keyword table pointer. If the command
1785 * contains comments it is copied into a temporary buffer sans comments
1786 * and the temporary buffer is passed to the command handler instead.
1787 * Likewise for backslash-newlines.
1789 * Returns nonzero if this was a known # directive.
1790 * Otherwise, returns zero, without advancing the input pointer.
1794 handle_directive (ip
, op
)
1797 register U_CHAR
*bp
, *cp
;
1798 register struct directive
*kt
;
1799 register int ident_length
;
1802 /* Nonzero means we must copy the entire command
1803 to get rid of comments or backslash-newlines. */
1804 int copy_command
= 0;
1806 U_CHAR
*ident
, *after_ident
;
1809 /* Skip whitespace and \-newline. */
1811 if (is_hor_space
[*bp
])
1813 else if (*bp
== '/' && (newline_fix (bp
+ 1), bp
[1]) == '*') {
1815 skip_to_end_of_comment (ip
, &ip
->lineno
);
1817 } else if (*bp
== '\\' && bp
[1] == '\n') {
1818 bp
+= 2; ip
->lineno
++;
1822 /* Now find end of directive name.
1823 If we encounter a backslash-newline, exchange it with any following
1824 symbol-constituents so that we end up with a contiguous name. */
1831 if (*cp
== '\\' && cp
[1] == '\n')
1832 name_newline_fix (cp
);
1838 ident_length
= cp
- bp
;
1842 /* A line of just `#' becomes blank. */
1844 if (ident_length
== 0 && *after_ident
== '\n') {
1845 ip
->bufp
= after_ident
;
1850 * Decode the keyword and call the appropriate expansion
1851 * routine, after moving the input pointer up to the next line.
1853 for (kt
= directive_table
; kt
->length
> 0; kt
++) {
1854 if (kt
->length
== ident_length
1855 && !strncmp (kt
->name
, (char *)ident
, ident_length
)) {
1856 register U_CHAR
*buf
;
1857 register U_CHAR
*limit
= ip
->buf
+ ip
->length
;
1858 int unterminated
= 0;
1860 /* Nonzero means do not delete comments within the directive.
1861 #define needs this to detect traditional token paste. */
1862 int keep_comments
= kt
->type
== T_DEFINE
;
1864 /* Find the end of this command (first newline not backslashed
1865 and not in a string or comment).
1866 Set COPY_COMMAND if the command must be copied
1867 (it contains a backslash-newline or a comment). */
1869 buf
= bp
= after_ident
;
1870 while (bp
< limit
) {
1871 register U_CHAR c
= *bp
++;
1885 bp
= skip_quoted_string (bp
- 1, limit
, ip
->lineno
, &ip
->lineno
, ©_command
, &unterminated
);
1887 /* Traditional preprocessing permits unterminated strings. */
1893 /* <...> is special for #include. */
1895 if (kt
->type
!= T_INCLUDE
)
1897 while (*bp
&& *bp
!= '>') bp
++;
1901 if (*bp
== '\\' && bp
[1] == '\n')
1904 U_CHAR
*obp
= bp
- 1;
1906 skip_to_end_of_comment (ip
, &ip
->lineno
);
1908 /* No need to copy the command because of a comment at the end;
1909 just don't include the comment in the directive. */
1910 if (bp
== limit
|| *bp
== '\n') {
1914 /* Don't remove the comments if this is #define. */
1915 if (! keep_comments
)
1921 --bp
; /* Point to the newline */
1929 resume_p
= ip
->bufp
;
1930 /* BP is the end of the directive.
1931 RESUME_P is the next interesting data after the directive.
1932 A comment may come between. */
1935 register U_CHAR
*xp
= buf
;
1936 /* Need to copy entire command into temp buffer before dispatching */
1938 cp
= (U_CHAR
*) alloca (bp
- buf
+ 5); /* room for cmd plus
1942 /* Copy to the new buffer, deleting comments
1943 and backslash-newlines (and whitespace surrounding the latter). */
1946 register U_CHAR c
= *xp
++;
1953 /* <...> is special for #include. */
1955 if (kt
->type
!= T_INCLUDE
)
1957 while (xp
< bp
&& c
!= '>') {
1959 if (c
== '\\' && xp
< bp
&& *xp
== '\n')
1970 if (cp
!= buf
&& is_space
[cp
[-1]]) {
1971 while (cp
!= buf
&& is_space
[cp
[-1]]) cp
--;
1973 SKIP_WHITE_SPACE (xp
);
1974 } else if (is_space
[*xp
]) {
1976 SKIP_WHITE_SPACE (xp
);
1986 register U_CHAR
*bp1
1987 = skip_quoted_string (xp
- 1, limit
, ip
->lineno
, 0, 0, 0);
1996 skip_to_end_of_comment (ip
, 0);
1998 while (xp
!= ip
->bufp
)
2000 /* Delete the slash. */
2008 /* Null-terminate the copy. */
2015 ip
->bufp
= resume_p
;
2017 /* Call the appropriate command handler. buf now points to
2018 either the appropriate place in the input buffer, or to
2019 the temp buffer if it was necessary to make one. cp
2020 points to the first char after the contents of the (possibly
2021 copied) command, in either case. */
2022 (*kt
->func
) (buf
, cp
, op
, kt
);
2023 check_expand (op
, ip
->length
- (ip
->bufp
- ip
->buf
));
2032 static const char *const
2033 monthnames
[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
2034 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
2037 * expand things like __FILE__. Place the expansion into the output
2038 * buffer *without* rescanning.
2041 special_symbol (hp
, op
)
2049 FILE_BUF
*ip
= NULL
;
2050 static struct tm
*timebuf
= NULL
;
2052 int paren
= 0; /* For special `defined' keyword */
2054 for (i
= indepth
; i
>= 0; i
--)
2055 if (instack
[i
].fname
!= NULL
) {
2060 fatal ("not in any file?!");
2067 if (hp
->type
== T_FILE
)
2070 string
= instack
[0].fname
;
2074 buf
= (char *) alloca (3 + strlen (string
));
2075 sprintf (buf
, "\"%s\"", string
);
2078 strcpy (buf
, "\"\"");
2083 case T_INCLUDE_LEVEL
:
2085 for (i
= indepth
; i
>= 0; i
--)
2086 if (instack
[i
].fname
!= NULL
)
2089 buf
= (char *) alloca (8); /* Eigth bytes ought to be more than enough */
2090 sprintf (buf
, "%d", true_indepth
- 1);
2094 buf
= (char *) alloca (3 + strlen (version_string
));
2095 sprintf (buf
, "\"%s\"", version_string
);
2099 buf
= (char *) hp
->value
.cpval
;
2103 buf
= (char *) alloca (10);
2104 sprintf (buf
, "%d", ip
->lineno
);
2109 if (timebuf
== NULL
) {
2111 timebuf
= localtime (&t
);
2113 buf
= (char *) alloca (20);
2114 if (hp
->type
== T_DATE
)
2115 sprintf (buf
, "\"%s %2d %4d\"", monthnames
[timebuf
->tm_mon
],
2116 timebuf
->tm_mday
, timebuf
->tm_year
+ 1900);
2118 sprintf (buf
, "\"%02d:%02d:%02d\"", timebuf
->tm_hour
, timebuf
->tm_min
,
2122 case T_SPEC_DEFINED
:
2123 buf
= (char *) " 0 "; /* Assume symbol is not defined */
2124 ip
= &instack
[indepth
];
2125 SKIP_WHITE_SPACE (ip
->bufp
);
2126 if (*ip
->bufp
== '(') {
2128 ip
->bufp
++; /* Skip over the paren */
2129 SKIP_WHITE_SPACE (ip
->bufp
);
2132 if (!is_idstart
[*ip
->bufp
])
2134 if (lookup (ip
->bufp
, -1, -1))
2135 buf
= (char *) " 1 ";
2136 while (is_idchar
[*ip
->bufp
])
2138 SKIP_WHITE_SPACE (ip
->bufp
);
2140 if (*ip
->bufp
!= ')')
2148 error ("`defined' must be followed by ident or (ident)");
2152 error ("cccp error: invalid special hash type"); /* time for gdb */
2156 check_expand (op
, len
);
2157 memcpy (op
->bufp
, buf
, len
);
2162 /* Routines to handle #directives */
2165 * Process include file by reading it in and calling rescan.
2166 * Expects to see "fname" or <fname> on the input.
2169 do_include (buf
, limit
, op
, keyword
)
2170 U_CHAR
*buf
, *limit
;
2172 struct directive
*keyword ATTRIBUTE_UNUSED
;
2174 char *fname
; /* Dynamically allocated fname buffer */
2175 U_CHAR
*fbeg
, *fend
; /* Beginning and end of fname */
2177 struct file_name_list
*stackp
= include
; /* Chain of dirs to search */
2178 struct file_name_list dsp
[1]; /* First in chain, if #include "..." */
2181 int f
; /* file number */
2183 int retried
= 0; /* Have already tried macro
2184 expanding the include line*/
2185 FILE_BUF trybuf
; /* It got expanded into here */
2186 int system_header_p
= 0; /* 0 for "...", 1 for <...> */
2188 f
= -1; /* JF we iz paranoid! */
2193 SKIP_WHITE_SPACE (fbeg
);
2194 /* Discard trailing whitespace so we can easily see
2195 if we have parsed all the significant chars we were given. */
2196 while (limit
!= fbeg
&& is_hor_space
[limit
[-1]]) limit
--;
2201 while (fend
!= limit
&& *fend
!= '\"')
2203 if (*fend
== '\"' && fend
+ 1 == limit
) {
2206 /* We have "filename". Figure out directory this source
2207 file is coming from and put it on the front of the list. */
2209 /* If -I- was specified, don't search current dir, only spec'd ones. */
2210 if (ignore_srcdir
) break;
2212 for (fp
= &instack
[indepth
]; fp
>= instack
; fp
--)
2215 const char *ep
, *nam
;
2217 if ((nam
= fp
->fname
) != NULL
) {
2218 /* Found a named file. Figure out dir of the file,
2219 and put it in front of the search list. */
2220 dsp
[0].next
= stackp
;
2222 ep
= strrchr (nam
, '/');
2226 f
= (char *) alloca (n
+ 1);
2227 strncpy (f
, nam
, n
);
2230 if (n
> max_include_len
) max_include_len
= n
;
2232 dsp
[0].fname
= 0; /* Current directory */
2243 while (fend
!= limit
&& *fend
!= '>') fend
++;
2244 if (*fend
== '>' && fend
+ 1 == limit
) {
2245 system_header_p
= 1;
2246 /* If -I-, start with the first -I dir after the -I-. */
2247 if (first_bracket_include
)
2248 stackp
= first_bracket_include
;
2256 error ("#include expects \"fname\" or <fname>");
2259 trybuf
= expand_to_temp_buffer (buf
, limit
, 0);
2260 buf
= (U_CHAR
*) alloca (trybuf
.bufp
- trybuf
.buf
+ 1);
2261 memcpy (buf
, trybuf
.buf
, trybuf
.bufp
- trybuf
.buf
);
2262 limit
= buf
+ (trybuf
.bufp
- trybuf
.buf
);
2270 fname
= (char *) alloca (max_include_len
+ flen
+ 2);
2271 /* + 2 above for slash and terminating null. */
2273 /* If specified file name is absolute, just open it. */
2276 strncpy (fname
, (char *)fbeg
, flen
);
2278 f
= open (fname
, O_RDONLY
, 0666);
2280 /* Search directory path, trying to open the file.
2281 Copy each filename tried into FNAME. */
2283 for (; stackp
; stackp
= stackp
->next
) {
2284 if (stackp
->fname
) {
2285 strcpy (fname
, stackp
->fname
);
2286 strcat (fname
, "/");
2287 fname
[strlen (fname
) + flen
] = 0;
2291 strncat (fname
, (char *)fbeg
, flen
);
2292 if ((f
= open (fname
, O_RDONLY
, 0666)) >= 0)
2298 strncpy (fname
, (char *)fbeg
, flen
);
2300 error_from_errno (fname
);
2302 /* For -M, add this file to the dependencies. */
2303 if (print_deps
> (system_header_p
|| (system_include_depth
> 0))) {
2304 if (system_header_p
)
2305 warning ("nonexistent file <%.*s> omitted from dependency output",
2309 deps_output ((const char *)fbeg
, fend
- fbeg
);
2310 deps_output (" ", 0);
2315 /* Check to see if this include file is a once-only include file.
2318 struct file_name_list
* ptr
;
2320 for (ptr
= dont_repeat_files
; ptr
; ptr
= ptr
->next
) {
2321 if (!strcmp (ptr
->fname
, fname
)) {
2323 return; /* This file was once'd. */
2327 for (ptr
= all_include_files
; ptr
; ptr
= ptr
->next
) {
2328 if (!strcmp (ptr
->fname
, fname
))
2329 break; /* This file was included before. */
2333 /* This is the first time for this file. */
2334 /* Add it to list of files included. */
2336 ptr
= (struct file_name_list
*) xmalloc (sizeof (struct file_name_list
));
2337 ptr
->next
= all_include_files
;
2338 all_include_files
= ptr
;
2339 ptr
->fname
= xstrdup (fname
);
2341 /* For -M, add this file to the dependencies. */
2342 if (print_deps
> (system_header_p
|| (system_include_depth
> 0))) {
2343 deps_output (fname
, strlen (fname
));
2344 deps_output (" ", 0);
2348 if (system_header_p
)
2349 system_include_depth
++;
2351 /* Actually process the file. */
2352 finclude (f
, fname
, op
);
2354 if (system_header_p
)
2355 system_include_depth
--;
2361 /* Process the contents of include file FNAME, already open on descriptor F,
2362 with output to OP. */
2365 finclude (f
, fname
, op
)
2373 FILE_BUF
*fp
; /* For input stack frame */
2375 CHECK_DEPTH (return;);
2377 if (file_size_and_mode (f
, &st_mode
, &st_size
))
2380 fp
= &instack
[indepth
+ 1];
2381 memset (fp
, 0, sizeof (FILE_BUF
));
2385 fp
->if_stack
= if_stack
;
2387 if (S_ISREG (st_mode
)) {
2388 fp
->buf
= (U_CHAR
*) xmalloc (st_size
+ 2);
2391 /* Read the file contents, knowing that st_size is an upper bound
2392 on the number of bytes we can read. */
2393 while (st_size
> 0) {
2394 i
= read (f
, fp
->buf
+ fp
->length
, st_size
);
2404 /* Cannot count its file size before reading. */
2411 basep
= (U_CHAR
*) xmalloc (bsize
+ 2);
2415 i
= read (f
, bufp
, bsize
- st_size
);
2417 goto nope
; /* error! */
2419 break; /* End of file */
2422 if (bsize
== st_size
) { /* Buffer is full! */
2424 basep
= (U_CHAR
*) xrealloc (basep
, bsize
+ 2);
2425 bufp
= basep
+ st_size
; /* May have moved */
2430 fp
->length
= st_size
;
2434 /* Make sure data ends with a newline. And put a null after it. */
2436 if (fp
->length
> 0 && fp
->buf
[fp
->length
-1] != '\n')
2437 fp
->buf
[fp
->length
++] = '\n';
2438 fp
->buf
[fp
->length
] = '\0';
2441 output_line_command (fp
, op
, 0, enter_file
);
2444 output_line_command (&instack
[indepth
], op
, 0, leave_file
);
2449 perror_with_name (fname
);
2454 /* Process a #define command.
2455 BUF points to the contents of the #define command, as a continguous string.
2456 LIMIT points to the first character past the end of the definition.
2457 KEYWORD is the keyword-table entry for #define. */
2460 do_define (buf
, limit
, op
, keyword
)
2461 U_CHAR
*buf
, *limit
;
2462 FILE_BUF
*op ATTRIBUTE_UNUSED
;
2463 struct directive
*keyword ATTRIBUTE_UNUSED
;
2465 U_CHAR
*bp
; /* temp ptr into input buffer */
2466 U_CHAR
*symname
; /* remember where symbol name starts */
2467 int sym_length
; /* and how long it is */
2470 int arglengths
= 0; /* Accumulate lengths of arg names
2471 plus number of args. */
2476 while (is_hor_space
[*bp
])
2479 symname
= bp
; /* remember where it starts */
2480 while (is_idchar
[*bp
] && bp
< limit
) {
2483 sym_length
= bp
- symname
;
2484 if (sym_length
== 0)
2485 error ("invalid macro name");
2486 else if (!is_idstart
[*symname
]) {
2487 U_CHAR
*msg
; /* what pain... */
2488 msg
= (U_CHAR
*) alloca (sym_length
+ 1);
2489 memcpy (msg
, symname
, sym_length
);
2490 msg
[sym_length
] = 0;
2491 error ("invalid macro name `%s'", msg
);
2493 if (! strncmp ((char *)symname
, "defined", 7) && sym_length
== 7)
2494 error ("defining `defined' as a macro");
2497 /* lossage will occur if identifiers or control keywords are broken
2498 across lines using backslash. This is not the right place to take
2502 struct arglist
*arg_ptrs
= NULL
;
2505 bp
++; /* skip '(' */
2506 SKIP_WHITE_SPACE (bp
);
2508 /* Loop over macro argument names. */
2509 while (*bp
!= ')') {
2510 struct arglist
*temp
;
2512 temp
= (struct arglist
*) alloca (sizeof (struct arglist
));
2514 temp
->next
= arg_ptrs
;
2515 temp
->argno
= argno
++;
2518 if (!is_idstart
[*bp
])
2519 warning ("parameter name starts with a digit in #define");
2521 /* Find the end of the arg name. */
2522 while (is_idchar
[*bp
]) {
2525 temp
->length
= bp
- temp
->name
;
2526 arglengths
+= temp
->length
+ 2;
2527 SKIP_WHITE_SPACE (bp
);
2528 if (temp
->length
== 0 || (*bp
!= ',' && *bp
!= ')')) {
2529 error ("badly punctuated parameter list in #define");
2534 SKIP_WHITE_SPACE (bp
);
2537 error ("unterminated parameter list in #define");
2542 ++bp
; /* skip paren */
2543 while (is_hor_space
[*bp
]) /* and leading whitespace */
2545 /* now everything from bp before limit is the definition. */
2546 defn
= collect_expansion (bp
, limit
, argno
, arg_ptrs
);
2548 /* Now set defn->argnames to the result of concatenating
2549 the argument names in reverse order
2550 with comma-space between them. */
2551 defn
->argnames
= (U_CHAR
*) xmalloc (arglengths
+ 1);
2553 struct arglist
*temp
;
2555 for (temp
= arg_ptrs
; temp
; temp
= temp
->next
) {
2556 memcpy (&defn
->argnames
[i
], temp
->name
, temp
->length
);
2558 if (temp
->next
!= 0) {
2559 defn
->argnames
[i
++] = ',';
2560 defn
->argnames
[i
++] = ' ';
2563 defn
->argnames
[i
] = 0;
2566 /* simple expansion or empty definition; skip leading whitespace */
2567 while (is_hor_space
[*bp
])
2569 /* now everything from bp before limit is the definition. */
2570 defn
= collect_expansion (bp
, limit
, -1, 0);
2571 defn
->argnames
= (U_CHAR
*) "";
2574 hashcode
= hashf (symname
, sym_length
, HASHSIZE
);
2578 if ((hp
= lookup (symname
, sym_length
, hashcode
)) == NULL
)
2579 hp
= install (symname
, sym_length
, T_MACRO
, hashcode
);
2581 if (hp
->type
!= T_MACRO
|| compare_defs (defn
, hp
->value
.defn
))
2582 warning ("\"%.*s\" redefined", sym_length
, symname
);
2584 /* Replace the old definition. */
2588 hp
->value
.defn
= defn
;
2593 * return zero if two DEFINITIONs are isomorphic
2596 compare_defs (d1
, d2
)
2597 DEFINITION
*d1
, *d2
;
2599 register struct reflist
*a1
, *a2
;
2600 register U_CHAR
*p1
= d1
->expansion
;
2601 register U_CHAR
*p2
= d2
->expansion
;
2604 if (d1
->nargs
!= d2
->nargs
)
2606 if (strcmp ((char *)d1
->argnames
, (char *)d2
->argnames
))
2608 for (a1
= d1
->pattern
, a2
= d2
->pattern
; a1
&& a2
;
2609 a1
= a1
->next
, a2
= a2
->next
) {
2610 if (!((a1
->nchars
== a2
->nchars
2611 && ! strncmp ((char *)p1
, (char *)p2
, a1
->nchars
))
2612 || ! comp_def_part (first
, p1
, a1
->nchars
, p2
, a2
->nchars
, 0))
2613 || a1
->argno
!= a2
->argno
2614 || a1
->stringify
!= a2
->stringify
2615 || a1
->raw_before
!= a2
->raw_before
2616 || a1
->raw_after
!= a2
->raw_after
)
2624 if (comp_def_part (first
, p1
, d1
->length
- (p1
- d1
->expansion
),
2625 p2
, d2
->length
- (p2
- d2
->expansion
), 1))
2630 /* Return 1 if two parts of two macro definitions are effectively different.
2631 One of the parts starts at BEG1 and has LEN1 chars;
2632 the other has LEN2 chars at BEG2.
2633 Any sequence of whitespace matches any other sequence of whitespace.
2634 FIRST means these parts are the first of a macro definition;
2635 so ignore leading whitespace entirely.
2636 LAST means these parts are the last of a macro definition;
2637 so ignore trailing whitespace entirely. */
2639 comp_def_part (first
, beg1
, len1
, beg2
, len2
, last
)
2641 U_CHAR
*beg1
, *beg2
;
2645 register U_CHAR
*end1
= beg1
+ len1
;
2646 register U_CHAR
*end2
= beg2
+ len2
;
2648 while (beg1
!= end1
&& is_space
[*beg1
]) beg1
++;
2649 while (beg2
!= end2
&& is_space
[*beg2
]) beg2
++;
2652 while (beg1
!= end1
&& is_space
[end1
[-1]]) end1
--;
2653 while (beg2
!= end2
&& is_space
[end2
[-1]]) end2
--;
2655 while (beg1
!= end1
&& beg2
!= end2
) {
2656 if (is_space
[*beg1
] && is_space
[*beg2
]) {
2657 while (beg1
!= end1
&& is_space
[*beg1
]) beg1
++;
2658 while (beg2
!= end2
&& is_space
[*beg2
]) beg2
++;
2659 } else if (*beg1
== *beg2
) {
2663 return (beg1
!= end1
) || (beg2
!= end2
);
2666 /* Read a replacement list for a macro with parameters.
2667 Build the DEFINITION structure.
2668 Reads characters of text starting at BUF until LIMIT.
2669 ARGLIST specifies the formal parameters to look for
2670 in the text of the definition; NARGS is the number of args
2671 in that list, or -1 for a macro name that wants no argument list.
2672 MACRONAME is the macro name itself (so we can avoid recursive expansion)
2673 and NAMELEN is its length in characters.
2675 Note that comments and backslash-newlines have already been deleted
2676 from the argument. */
2678 /* Leading and trailing Space, Tab, etc. are converted to markers
2679 Newline Space, Newline Tab, etc.
2680 Newline Space makes a space in the final output
2681 but is discarded if stringified. (Newline Tab is similar but
2682 makes a Tab instead.)
2684 If there is no trailing whitespace, a Newline Space is added at the end
2685 to prevent concatenation that would be contrary to the standard. */
2688 collect_expansion (buf
, end
, nargs
, arglist
)
2691 struct arglist
*arglist
;
2694 register U_CHAR
*p
, *limit
, *lastp
, *exp_p
;
2695 struct reflist
*endpat
= NULL
;
2696 /* Pointer to first nonspace after last ## seen. */
2698 /* Pointer to first nonspace after last single-# seen. */
2699 U_CHAR
*stringify
= 0;
2701 int expected_delimiter
= '\0';
2703 /* Scan thru the replacement list, ignoring comments and quoted
2704 strings, picking up on the macro calls. It does a linear search
2705 thru the arg list on every potential symbol. Profiling might say
2706 that something smarter should happen. */
2711 /* Find the beginning of the trailing whitespace. */
2712 /* Find end of leading whitespace. */
2715 while (p
< limit
&& is_space
[limit
[-1]]) limit
--;
2716 while (p
< limit
&& is_space
[*p
]) p
++;
2718 /* Allocate space for the text in the macro definition.
2719 Leading and trailing whitespace chars need 2 bytes each.
2720 Each other input char may or may not need 1 byte,
2721 so this is an upper bound.
2722 The extra 2 are for invented trailing newline-marker and final null. */
2723 maxsize
= (sizeof (DEFINITION
)
2724 + 2 * (end
- limit
) + 2 * (p
- buf
)
2726 defn
= (DEFINITION
*) xcalloc (1, maxsize
);
2728 defn
->nargs
= nargs
;
2729 exp_p
= defn
->expansion
= (U_CHAR
*) defn
+ sizeof (DEFINITION
);
2734 /* Convert leading whitespace to Newline-markers. */
2735 while (p
< limit
&& is_space
[*p
]) {
2740 /* Process the main body of the definition. */
2742 int skipped_arg
= 0;
2743 register U_CHAR c
= *p
++;
2747 /* In -traditional mode, recognize arguments inside strings and
2748 and character constants, and ignore special properties of #.
2749 Arguments inside strings are considered "stringified", but no
2750 extra quote marks are supplied. */
2754 if (expected_delimiter
!= '\0') {
2755 if (c
== expected_delimiter
)
2756 expected_delimiter
= '\0';
2758 expected_delimiter
= c
;
2762 /* Backslash quotes delimiters and itself, but not macro args. */
2763 if (expected_delimiter
!= 0 && p
< limit
2764 && (*p
== expected_delimiter
|| *p
== '\\')) {
2771 if (expected_delimiter
!= '\0') /* No comments inside strings. */
2774 /* If we find a comment that wasn't removed by handle_directive,
2775 this must be -traditional. So replace the comment with
2779 while (p
< limit
&& !(p
[-2] == '*' && p
[-1] == '/'))
2785 if (is_idchar
[c
] && nargs
> 0) {
2786 U_CHAR
*id_beg
= p
- 1;
2790 while (p
!= limit
&& is_idchar
[*p
]) p
++;
2791 id_len
= p
- id_beg
;
2793 if (is_idstart
[c
]) {
2794 register struct arglist
*arg
;
2796 for (arg
= arglist
; arg
!= NULL
; arg
= arg
->next
) {
2797 struct reflist
*tpat
;
2799 if (arg
->name
[0] == c
2800 && arg
->length
== id_len
2801 && strncmp ((char *)arg
->name
, (char *)id_beg
, id_len
) == 0) {
2802 /* make a pat node for this arg and append it to the end of
2804 tpat
= (struct reflist
*) xmalloc (sizeof (struct reflist
));
2806 tpat
->raw_before
= concat
== id_beg
;
2807 tpat
->raw_after
= 0;
2808 tpat
->stringify
= expected_delimiter
!= '\0';
2811 defn
->pattern
= tpat
;
2813 endpat
->next
= tpat
;
2816 tpat
->argno
= arg
->argno
;
2817 tpat
->nchars
= exp_p
- lastp
;
2819 register U_CHAR
*p1
= p
;
2820 SKIP_WHITE_SPACE (p1
);
2821 if (p1
+ 2 <= limit
&& p1
[0] == '#' && p1
[1] == '#')
2822 tpat
->raw_after
= 1;
2824 lastp
= exp_p
; /* place to start copying from next time */
2831 /* If this was not a macro arg, copy it into the expansion. */
2832 if (! skipped_arg
) {
2833 register U_CHAR
*lim1
= p
;
2837 if (stringify
== id_beg
)
2838 error ("# operator should be followed by a macro argument name");
2844 /* Convert trailing whitespace to Newline-markers. */
2845 while (limit
< end
&& is_space
[*limit
]) {
2847 *exp_p
++ = *limit
++;
2852 defn
->length
= exp_p
- defn
->expansion
;
2854 /* Crash now if we overrun the allocated size. */
2855 if (defn
->length
+ 1 > maxsize
)
2862 * interpret #line command. Remembers previously seen fnames
2863 * in its very own hash table.
2865 #define FNAME_HASHSIZE 37
2867 do_line (buf
, limit
, op
, keyword
)
2868 U_CHAR
*buf
, *limit
;
2870 struct directive
*keyword ATTRIBUTE_UNUSED
;
2872 register U_CHAR
*bp
;
2873 FILE_BUF
*ip
= &instack
[indepth
];
2876 enum file_change_code file_change
= same_file
;
2878 /* Expand any macros. */
2879 tem
= expand_to_temp_buffer (buf
, limit
, 0);
2881 /* Point to macroexpanded line, which is null-terminated now. */
2883 SKIP_WHITE_SPACE (bp
);
2885 if (!isdigit (*bp
)) {
2886 error ("invalid format #line command");
2890 /* The Newline at the end of this line remains to be processed.
2891 To put the next line at the specified line number,
2892 we must store a line number now that is one less. */
2893 new_lineno
= atoi ((char *)bp
) - 1;
2895 /* skip over the line number. */
2896 while (isdigit (*bp
))
2899 #if 0 /* #line 10"foo.c" is supposed to be allowed. */
2900 if (*bp
&& !is_space
[*bp
]) {
2901 error ("invalid format #line command");
2906 SKIP_WHITE_SPACE (bp
);
2909 static HASHNODE
*fname_table
[FNAME_HASHSIZE
];
2910 HASHNODE
*hp
, **hash_bucket
;
2916 while (*bp
&& *bp
!= '\"')
2919 error ("invalid format #line command");
2923 fname_length
= bp
- fname
;
2926 SKIP_WHITE_SPACE (bp
);
2929 file_change
= enter_file
;
2930 else if (*bp
== '2')
2931 file_change
= leave_file
;
2933 error ("invalid format #line command");
2938 SKIP_WHITE_SPACE (bp
);
2940 error ("invalid format #line command");
2946 &fname_table
[hashf (fname
, fname_length
, FNAME_HASHSIZE
)];
2947 for (hp
= *hash_bucket
; hp
!= NULL
; hp
= hp
->next
)
2948 if (hp
->length
== fname_length
&&
2949 strncmp (hp
->value
.cpval
, (char *)fname
, fname_length
) == 0) {
2950 ip
->fname
= hp
->value
.cpval
;
2955 /* Didn't find it; cons up a new one. */
2956 hp
= (HASHNODE
*) xcalloc (1, sizeof (HASHNODE
) + fname_length
+ 1);
2957 hp
->next
= *hash_bucket
;
2960 hp
->length
= fname_length
;
2961 ip
->fname
= hp
->value
.cpval
= q
= ((char *) hp
) + sizeof (HASHNODE
);
2962 memcpy (q
, fname
, fname_length
);
2965 error ("invalid format #line command");
2969 ip
->lineno
= new_lineno
;
2970 output_line_command (ip
, op
, 0, file_change
);
2971 check_expand (op
, ip
->length
- (ip
->bufp
- ip
->buf
));
2975 * remove all definitions of symbol from symbol table.
2976 * according to un*x /lib/cpp, it is not an error to undef
2977 * something that has no definitions, so it isn't one here either.
2980 do_undef (buf
, limit
, op
, keyword
)
2982 U_CHAR
*limit ATTRIBUTE_UNUSED
;
2983 FILE_BUF
*op ATTRIBUTE_UNUSED
;
2984 struct directive
*keyword ATTRIBUTE_UNUSED
;
2988 SKIP_WHITE_SPACE (buf
);
2990 if (! strncmp ((char *)buf
, "defined", 7) && ! is_idchar
[buf
[7]])
2991 warning ("undefining `defined'");
2993 while ((hp
= lookup (buf
, -1, -1)) != NULL
) {
2994 if (hp
->type
!= T_MACRO
)
2995 warning ("undefining `%s'", hp
->name
);
3001 * handle #if command by
3002 * 1) inserting special `defined' keyword into the hash table
3003 * that gets turned into 0 or 1 by special_symbol (thus,
3004 * if the luser has a symbol called `defined' already, it won't
3005 * work inside the #if command)
3006 * 2) rescan the input into a temporary output buffer
3007 * 3) pass the output buffer to the yacc parser and collect a value
3008 * 4) clean up the mess left from steps 1 and 2.
3009 * 5) call conditional_skip to skip til the next #endif (etc.),
3010 * or not, depending on the value from step 3.
3013 do_if (buf
, limit
, op
, keyword
)
3014 U_CHAR
*buf
, *limit
;
3015 FILE_BUF
*op ATTRIBUTE_UNUSED
;
3016 struct directive
*keyword ATTRIBUTE_UNUSED
;
3019 FILE_BUF
*ip
= &instack
[indepth
];
3021 value
= eval_if_expression (buf
, limit
- buf
);
3022 conditional_skip (ip
, value
== 0, T_IF
);
3026 * handle a #elif directive by not changing if_stack either.
3027 * see the comment above do_else.
3030 do_elif (buf
, limit
, op
, keyword
)
3031 U_CHAR
*buf
, *limit
;
3033 struct directive
*keyword ATTRIBUTE_UNUSED
;
3036 FILE_BUF
*ip
= &instack
[indepth
];
3038 if (if_stack
== instack
[indepth
].if_stack
) {
3039 error ("#elif not within a conditional");
3042 if (if_stack
->type
!= T_IF
&& if_stack
->type
!= T_ELIF
) {
3043 error ("#elif after #else");
3044 fprintf (stderr
, " (matches line %d", if_stack
->lineno
);
3045 if (if_stack
->fname
!= NULL
&& ip
->fname
!= NULL
&&
3046 strcmp (if_stack
->fname
, ip
->fname
) != 0)
3047 fprintf (stderr
, ", file %s", if_stack
->fname
);
3048 fprintf (stderr
, ")\n");
3050 if_stack
->type
= T_ELIF
;
3053 if (if_stack
->if_succeeded
)
3054 skip_if_group (ip
, 0);
3056 value
= eval_if_expression (buf
, limit
- buf
);
3058 skip_if_group (ip
, 0);
3060 ++if_stack
->if_succeeded
; /* continue processing input */
3061 output_line_command (ip
, op
, 1, same_file
);
3067 * evaluate a #if expression in BUF, of length LENGTH,
3068 * then parse the result as a C expression and return the value as an int.
3071 eval_if_expression (buf
, length
)
3076 HASHNODE
*save_defined
;
3079 save_defined
= install (U
"defined", -1, T_SPEC_DEFINED
, -1);
3080 temp_obuf
= expand_to_temp_buffer (buf
, buf
+ length
, 0);
3081 delete_macro (save_defined
); /* clean up special symbol */
3083 value
= parse_c_expression ((char *)temp_obuf
.buf
);
3085 free (temp_obuf
.buf
);
3091 * routine to handle ifdef/ifndef. Try to look up the symbol,
3092 * then do or don't skip to the #endif/#else/#elif depending
3093 * on what directive is actually being processed.
3096 do_xifdef (buf
, limit
, op
, keyword
)
3097 U_CHAR
*buf
, *limit
;
3098 FILE_BUF
*op ATTRIBUTE_UNUSED
;
3099 struct directive
*keyword
;
3102 FILE_BUF
*ip
= &instack
[indepth
];
3105 /* Discard leading and trailing whitespace. */
3106 SKIP_WHITE_SPACE (buf
);
3107 while (limit
!= buf
&& is_hor_space
[limit
[-1]]) limit
--;
3109 /* Find the end of the identifier at the beginning. */
3110 for (end
= buf
; is_idchar
[*end
]; end
++);
3113 skip
= (keyword
->type
== T_IFDEF
);
3115 skip
= (lookup (buf
, end
-buf
, -1) == NULL
) ^ (keyword
->type
== T_IFNDEF
);
3118 conditional_skip (ip
, skip
, T_IF
);
3122 * push TYPE on stack; then, if SKIP is nonzero, skip ahead.
3125 conditional_skip (ip
, skip
, type
)
3128 enum node_type type
;
3130 IF_STACK_FRAME
*temp
;
3132 temp
= (IF_STACK_FRAME
*) xcalloc (1, sizeof (IF_STACK_FRAME
));
3133 temp
->fname
= ip
->fname
;
3134 temp
->lineno
= ip
->lineno
;
3135 temp
->next
= if_stack
;
3138 if_stack
->type
= type
;
3141 skip_if_group (ip
, 0);
3144 ++if_stack
->if_succeeded
;
3145 output_line_command (ip
, &outbuf
, 1, same_file
);
3150 * skip to #endif, #else, or #elif. adjust line numbers, etc.
3151 * leaves input ptr at the sharp sign found.
3152 * If ANY is nonzero, return at next directive of any sort.
3155 skip_if_group (ip
, any
)
3159 register U_CHAR
*bp
= ip
->bufp
, *cp
;
3160 register U_CHAR
*endb
= ip
->buf
+ ip
->length
;
3161 struct directive
*kt
;
3162 IF_STACK_FRAME
*save_if_stack
= if_stack
; /* don't pop past here */
3163 U_CHAR
*beg_of_line
= bp
;
3167 case '/': /* possible comment */
3168 if (*bp
== '\\' && bp
[1] == '\n')
3172 bp
= skip_to_end_of_comment (ip
, &ip
->lineno
);
3177 bp
= skip_quoted_string (bp
- 1, endb
, ip
->lineno
, &ip
->lineno
, 0, 0);
3180 /* Char after backslash loses its special meaning. */
3183 ++ip
->lineno
; /* But do update the line-count. */
3194 /* # keyword: a # must be first nonblank char on the line */
3195 if (beg_of_line
== 0)
3197 /* Scan from start of line, skipping whitespace, comments
3198 and backslash-newlines, and see if we reach this #.
3199 If not, this # is not special. */
3202 if (is_hor_space
[*bp
])
3204 else if (*bp
== '\\' && bp
[1] == '\n')
3206 else if (*bp
== '/' && bp
[1] == '*') {
3208 while (!(*bp
== '*' && bp
[1] == '/')) {
3217 if (bp
!= ip
->bufp
) {
3218 bp
= ip
->bufp
+ 1; /* Reset bp to after the #. */
3222 bp
= ip
->bufp
+ 1; /* Point after '#'. */
3224 /* Skip whitespace and \-newline. */
3226 if (is_hor_space
[*bp
])
3228 else if (*bp
== '\\' && bp
[1] == '\n')
3230 else if (*bp
== '/' && bp
[1] == '*') {
3232 while (!(*bp
== '*' && bp
[1] == '/'))
3241 /* Now find end of directive name.
3242 If we encounter a backslash-newline, exchange it with any following
3243 symbol-constituents so that we end up with a contiguous name. */
3249 if (*bp
== '\\' && bp
[1] == '\n')
3250 name_newline_fix (bp
);
3257 for (kt
= directive_table
; kt
->length
>= 0; kt
++) {
3258 IF_STACK_FRAME
*temp
;
3259 if (strncmp ((char *)cp
, kt
->name
, kt
->length
) == 0
3260 && !is_idchar
[cp
[kt
->length
]]) {
3262 /* If we are asked to return on next directive,
3271 temp
= (IF_STACK_FRAME
*) xcalloc (1, sizeof (IF_STACK_FRAME
));
3272 temp
->next
= if_stack
;
3274 temp
->lineno
= ip
->lineno
;
3275 temp
->fname
= ip
->fname
;
3276 temp
->type
= kt
->type
;
3281 if (if_stack
== instack
[indepth
].if_stack
) {
3282 error ("#%s not within a conditional", kt
->name
);
3285 else if (if_stack
== save_if_stack
)
3286 return; /* found what we came for */
3288 if (kt
->type
!= T_ENDIF
) {
3289 if (if_stack
->type
== T_ELSE
)
3290 error ("#else or #elif after #else");
3291 if_stack
->type
= kt
->type
;
3296 if_stack
= if_stack
->next
;
3301 /* Anything else is ignored. */
3310 /* after this returns, rescan will exit because ip->bufp
3311 now points to the end of the buffer.
3312 rescan is responsible for the error message also. */
3316 * handle a #else directive. Do this by just continuing processing
3317 * without changing if_stack ; this is so that the error message
3318 * for missing #endif's etc. will point to the original #if. It
3319 * is possible that something different would be better.
3322 do_else (buf
, limit
, op
, keyword
)
3323 U_CHAR
*buf ATTRIBUTE_UNUSED
;
3324 U_CHAR
*limit ATTRIBUTE_UNUSED
;
3326 struct directive
*keyword ATTRIBUTE_UNUSED
;
3328 FILE_BUF
*ip
= &instack
[indepth
];
3330 if (if_stack
== instack
[indepth
].if_stack
) {
3331 error ("#else not within a conditional");
3334 if (if_stack
->type
!= T_IF
&& if_stack
->type
!= T_ELIF
) {
3335 error ("#else after #else");
3336 fprintf (stderr
, " (matches line %d", if_stack
->lineno
);
3337 if (strcmp (if_stack
->fname
, ip
->fname
) != 0)
3338 fprintf (stderr
, ", file %s", if_stack
->fname
);
3339 fprintf (stderr
, ")\n");
3341 if_stack
->type
= T_ELSE
;
3344 if (if_stack
->if_succeeded
)
3345 skip_if_group (ip
, 0);
3347 ++if_stack
->if_succeeded
; /* continue processing input */
3348 output_line_command (ip
, op
, 1, same_file
);
3353 * unstack after #endif command
3356 do_endif (buf
, limit
, op
, keyword
)
3357 U_CHAR
*buf ATTRIBUTE_UNUSED
;
3358 U_CHAR
*limit ATTRIBUTE_UNUSED
;
3360 struct directive
*keyword ATTRIBUTE_UNUSED
;
3362 if (if_stack
== instack
[indepth
].if_stack
)
3363 error ("unbalanced #endif");
3365 IF_STACK_FRAME
*temp
= if_stack
;
3366 if_stack
= if_stack
->next
;
3368 output_line_command (&instack
[indepth
], op
, 1, same_file
);
3373 * Skip a comment, assuming the input ptr immediately follows the
3374 * initial slash-star. Bump line counter as necessary.
3375 * (The canonical line counter is &ip->lineno).
3376 * Don't use this routine (or the next one) if bumping the line
3377 * counter is not sufficient to deal with newlines in the string.
3380 skip_to_end_of_comment (ip
, line_counter
)
3381 register FILE_BUF
*ip
;
3382 int *line_counter
; /* place to remember newlines, or NULL */
3384 register U_CHAR
*limit
= ip
->buf
+ ip
->length
;
3385 register U_CHAR
*bp
= ip
->bufp
;
3386 FILE_BUF
*op
= &outbuf
; /* JF */
3387 int output
= put_out_comments
&& !line_counter
;
3389 /* JF this line_counter stuff is a crock to make sure the
3390 comment is only put out once, no matter how many times
3391 the comment is skipped. It almost works */
3396 while (bp
< limit
) {
3401 if (warn_comments
&& bp
< limit
&& *bp
== '*')
3402 warning("`/*' within comment");
3405 if (line_counter
!= NULL
)
3411 if (*bp
== '\\' && bp
[1] == '\n')
3427 * Skip over a quoted string. BP points to the opening quote.
3428 * Returns a pointer after the closing quote. Don't go past LIMIT.
3429 * START_LINE is the line number of the starting point (but it need
3430 * not be valid if the starting point is inside a macro expansion).
3432 * The input stack state is not changed.
3434 * If COUNT_NEWLINES is nonzero, it points to an int to increment
3435 * for each newline passed.
3437 * If BACKSLASH_NEWLINES_P is nonzero, store 1 thru it
3438 * if we pass a backslash-newline.
3440 * If EOFP is nonzero, set *EOFP to 1 if the string is unterminated.
3443 skip_quoted_string (bp
, limit
, start_line
, count_newlines
, backslash_newlines_p
, eofp
)
3444 register U_CHAR
*bp
;
3445 register U_CHAR
*limit
;
3447 int *count_newlines
;
3448 int *backslash_newlines_p
;
3451 register U_CHAR c
, match
;
3456 error_with_line (line_for_error (start_line
),
3457 "unterminated string or character constant");
3464 while (*bp
== '\\' && bp
[1] == '\n') {
3465 if (backslash_newlines_p
)
3466 *backslash_newlines_p
= 1;
3471 if (*bp
== '\n' && count_newlines
) {
3472 if (backslash_newlines_p
)
3473 *backslash_newlines_p
= 1;
3477 } else if (c
== '\n') {
3478 /* Unterminated strings and character constants are 'legal'. */
3479 bp
--; /* Don't consume the newline. */
3483 } else if (c
== match
)
3490 * write out a #line command, for instance, after an #include file.
3491 * If CONDITIONAL is nonzero, we can omit the #line if it would
3492 * appear to be a no-op, and we can output a few newlines instead
3493 * if we want to increase the line number by a small amount.
3494 * FILE_CHANGE says whether we are entering a file, leaving, or neither.
3498 output_line_command (ip
, op
, conditional
, file_change
)
3501 enum file_change_code file_change
;
3504 char line_cmd_buf
[500];
3506 if (no_line_commands
3507 || ip
->fname
== NULL
3509 op
->lineno
= ip
->lineno
;
3514 if (ip
->lineno
== op
->lineno
)
3517 /* If the inherited line number is a little too small,
3518 output some newlines instead of a #line command. */
3519 if (ip
->lineno
> op
->lineno
&& ip
->lineno
< op
->lineno
+ 8) {
3520 check_expand (op
, 10);
3521 while (ip
->lineno
> op
->lineno
) {
3529 sprintf (line_cmd_buf
, "# %d \"%s\"", ip
->lineno
, ip
->fname
);
3530 if (file_change
!= same_file
)
3531 strcat (line_cmd_buf
, file_change
== enter_file
? " 1" : " 2");
3532 len
= strlen (line_cmd_buf
);
3533 line_cmd_buf
[len
++] = '\n';
3534 check_expand (op
, len
+ 1);
3535 if (op
->bufp
> op
->buf
&& op
->bufp
[-1] != '\n')
3537 memcpy (op
->bufp
, line_cmd_buf
, len
);
3539 op
->lineno
= ip
->lineno
;
3543 /* Expand a macro call.
3544 HP points to the symbol that is the macro being called.
3545 Put the result of expansion onto the input stack
3546 so that subsequent input by our caller will use it.
3548 If macro wants arguments, caller has already verified that
3549 an argument list follows; arguments come from the input stack. */
3552 macroexpand (hp
, op
)
3557 DEFINITION
*defn
= hp
->value
.defn
;
3558 register U_CHAR
*xbuf
;
3560 int start_line
= instack
[indepth
].lineno
;
3562 CHECK_DEPTH (return;);
3564 /* it might not actually be a macro. */
3565 if (hp
->type
!= T_MACRO
) {
3566 special_symbol (hp
, op
);
3570 nargs
= defn
->nargs
;
3574 struct argdata
*args
;
3575 const char *parse_error
= 0;
3577 args
= (struct argdata
*) alloca ((nargs
+ 1) * sizeof (struct argdata
));
3579 for (i
= 0; i
< nargs
; i
++) {
3580 args
[i
].raw
= args
[i
].expanded
= (U_CHAR
*) "";
3581 args
[i
].raw_length
= args
[i
].expand_length
3582 = args
[i
].stringified_length
= 0;
3583 args
[i
].free1
= args
[i
].free2
= 0;
3586 /* Parse all the macro args that are supplied. I counts them.
3587 The first NARGS args are stored in ARGS.
3588 The rest are discarded. */
3591 /* Discard the open-parenthesis or comma before the next arg. */
3592 ++instack
[indepth
].bufp
;
3594 = macarg ((i
< nargs
|| (nargs
== 0 && i
== 0)) ? &args
[i
] : 0);
3597 error_with_line (line_for_error (start_line
), parse_error
);
3601 } while (*instack
[indepth
].bufp
!= ')');
3603 /* If we got one arg but it was just whitespace, call that 0 args. */
3605 register U_CHAR
*bp
= args
[0].raw
;
3606 register U_CHAR
*lim
= bp
+ args
[0].raw_length
;
3607 while (bp
!= lim
&& is_space
[*bp
]) bp
++;
3612 if (nargs
== 0 && i
> 0)
3613 error ("arguments given to macro `%s'", hp
->name
);
3614 else if (i
< nargs
) {
3615 /* traditional C allows foo() if foo wants one argument. */
3616 if (nargs
== 1 && i
== 0)
3619 error ("no args to macro `%s'", hp
->name
);
3621 error ("only 1 arg to macro `%s'", hp
->name
);
3623 error ("only %d args to macro `%s'", i
, hp
->name
);
3624 } else if (i
> nargs
)
3625 error ("too many (%d) args to macro `%s'", i
, hp
->name
);
3627 /* Swallow the closeparen. */
3628 ++instack
[indepth
].bufp
;
3630 /* If macro wants zero args, we parsed the arglist for checking only.
3631 Read directly from the macro definition. */
3633 xbuf
= defn
->expansion
;
3634 xbuf_len
= defn
->length
;
3636 register U_CHAR
*exp
= defn
->expansion
;
3637 register int offset
; /* offset in expansion,
3638 copied a piece at a time */
3639 register int totlen
; /* total amount of exp buffer filled so far */
3641 register struct reflist
*ap
;
3643 /* Macro really takes args. Compute the expansion of this call. */
3645 /* Compute length in characters of the macro's expansion. */
3646 xbuf_len
= defn
->length
;
3647 for (ap
= defn
->pattern
; ap
!= NULL
; ap
= ap
->next
) {
3649 xbuf_len
+= args
[ap
->argno
].stringified_length
;
3651 xbuf_len
+= args
[ap
->argno
].raw_length
;
3654 xbuf
= (U_CHAR
*) xmalloc (xbuf_len
+ 1);
3656 /* Generate in XBUF the complete expansion
3657 with arguments substituted in.
3658 TOTLEN is the total size generated so far.
3659 OFFSET is the index in the definition
3660 of where we are copying from. */
3661 offset
= totlen
= 0;
3662 for (ap
= defn
->pattern
; ap
!= NULL
; ap
= ap
->next
) {
3663 register struct argdata
*arg
= &args
[ap
->argno
];
3665 for (i
= 0; i
< ap
->nchars
; i
++)
3666 xbuf
[totlen
++] = exp
[offset
++];
3668 if (ap
->stringify
!= 0) {
3669 int arglen
= arg
->raw_length
;
3675 && (c
= arg
->raw
[i
], is_space
[c
]))
3678 && (c
= arg
->raw
[arglen
- 1], is_space
[c
]))
3680 for (; i
< arglen
; i
++) {
3683 /* Special markers Newline Space
3684 generate nothing for a stringified argument. */
3685 if (c
== '\n' && arg
->raw
[i
+1] != '\n') {
3690 /* Internal sequences of whitespace are replaced by one space
3691 except within an string or char token. */
3693 && (c
== '\n' ? arg
->raw
[i
+1] == '\n' : is_space
[c
])) {
3695 /* Note that Newline Space does occur within whitespace
3696 sequences; consider it part of the sequence. */
3697 if (c
== '\n' && is_space
[arg
->raw
[i
+1]])
3699 else if (c
!= '\n' && is_space
[c
])
3716 } else if (c
== '\"' || c
== '\'')
3720 /* Escape these chars */
3721 if (c
== '\"' || (in_string
&& c
== '\\'))
3722 xbuf
[totlen
++] = '\\';
3726 sprintf ((char *) &xbuf
[totlen
], "\\%03o", (unsigned int) c
);
3731 U_CHAR
*p1
= arg
->raw
;
3732 U_CHAR
*l1
= p1
+ arg
->raw_length
;
3734 if (ap
->raw_before
) {
3735 while (p1
!= l1
&& is_space
[*p1
]) p1
++;
3736 while (p1
!= l1
&& is_idchar
[*p1
])
3737 xbuf
[totlen
++] = *p1
++;
3738 /* Delete any no-reexpansion marker that follows
3739 an identifier at the beginning of the argument
3740 if the argument is concatenated with what precedes it. */
3741 if (p1
[0] == '\n' && p1
[1] == '-')
3744 if (ap
->raw_after
) {
3745 /* Arg is concatenated after: delete trailing whitespace,
3746 whitespace markers, and no-reexpansion markers. */
3748 if (is_space
[l1
[-1]]) l1
--;
3749 else if (l1
[-1] == '-') {
3750 U_CHAR
*p2
= l1
- 1;
3751 /* If a `-' is preceded by an odd number of newlines then it
3752 and the last newline are a no-reexpansion marker. */
3753 while (p2
!= p1
&& p2
[-1] == '\n') p2
--;
3754 if ((l1
- 1 - p2
) & 1) {
3762 memmove (xbuf
+ totlen
, p1
, l1
- p1
);
3766 if (totlen
> xbuf_len
)
3770 /* if there is anything left of the definition
3771 after handling the arg list, copy that in too. */
3773 for (i
= offset
; i
< defn
->length
; i
++)
3774 xbuf
[totlen
++] = exp
[i
];
3779 for (i
= 0; i
< nargs
; i
++) {
3780 if (args
[i
].free1
!= 0)
3781 free (args
[i
].free1
);
3782 if (args
[i
].free2
!= 0)
3783 free (args
[i
].free2
);
3787 xbuf
= defn
->expansion
;
3788 xbuf_len
= defn
->length
;
3791 /* Now put the expansion on the input stack
3792 so our caller will commence reading from it. */
3794 register FILE_BUF
*ip2
;
3796 ip2
= &instack
[++indepth
];
3801 ip2
->length
= xbuf_len
;
3803 ip2
->free_ptr
= (nargs
> 0) ? xbuf
: 0;
3805 ip2
->if_stack
= if_stack
;
3810 * Parse a macro argument and store the info on it into *ARGPTR.
3811 * Return nonzero to indicate a syntax error.
3816 register struct argdata
*argptr
;
3818 FILE_BUF
*ip
= &instack
[indepth
];
3823 /* Try to parse as much of the argument as exists at this
3824 input stack level. */
3825 U_CHAR
*bp
= macarg1 (ip
->bufp
, ip
->buf
+ ip
->length
,
3826 &paren
, &newlines
, &comments
);
3828 /* If we find the end of the argument at this level,
3829 set up *ARGPTR to point at it in the input stack. */
3830 if (!(ip
->fname
!= 0 && (newlines
!= 0 || comments
!= 0))
3831 && bp
!= ip
->buf
+ ip
->length
) {
3833 argptr
->raw
= ip
->bufp
;
3834 argptr
->raw_length
= bp
- ip
->bufp
;
3838 /* This input stack level ends before the macro argument does.
3839 We must pop levels and keep parsing.
3840 Therefore, we must allocate a temporary buffer and copy
3841 the macro argument into it. */
3842 int bufsize
= bp
- ip
->bufp
;
3843 int extra
= newlines
;
3844 U_CHAR
*buffer
= (U_CHAR
*) xmalloc (bufsize
+ extra
+ 1);
3845 int final_start
= 0;
3847 memcpy (buffer
, ip
->bufp
, bufsize
);
3849 ip
->lineno
+= newlines
;
3851 while (bp
== ip
->buf
+ ip
->length
) {
3852 if (instack
[indepth
].macro
== 0) {
3854 return "unterminated macro call";
3856 ip
->macro
->type
= T_MACRO
;
3858 free (ip
->free_ptr
);
3859 ip
= &instack
[--indepth
];
3862 bp
= macarg1 (ip
->bufp
, ip
->buf
+ ip
->length
, &paren
,
3863 &newlines
, &comments
);
3864 final_start
= bufsize
;
3865 bufsize
+= bp
- ip
->bufp
;
3867 buffer
= (U_CHAR
*) xrealloc (buffer
, bufsize
+ extra
+ 1);
3868 memcpy (buffer
+ bufsize
- (bp
- ip
->bufp
), ip
->bufp
, bp
- ip
->bufp
);
3870 ip
->lineno
+= newlines
;
3873 /* Now, if arg is actually wanted, record its raw form,
3874 discarding comments and duplicating newlines in whatever
3875 part of it did not come from a macro expansion.
3876 EXTRA space has been preallocated for duplicating the newlines.
3877 FINAL_START is the index of the start of that part. */
3879 argptr
->raw
= buffer
;
3880 argptr
->raw_length
= bufsize
;
3881 argptr
->free1
= buffer
;
3882 argptr
->newlines
= newlines
;
3883 argptr
->comments
= comments
;
3884 if ((newlines
|| comments
) && ip
->fname
!= 0)
3887 discard_comments (argptr
->raw
+ final_start
,
3888 argptr
->raw_length
- final_start
,
3890 argptr
->raw
[argptr
->raw_length
] = 0;
3891 if (argptr
->raw_length
> bufsize
+ extra
)
3896 /* If we are not discarding this argument,
3897 macroexpand it and compute its length as stringified.
3898 All this info goes into *ARGPTR. */
3902 register U_CHAR
*buf
, *lim
;
3903 register int totlen
;
3905 obuf
= expand_to_temp_buffer (argptr
->raw
,
3906 argptr
->raw
+ argptr
->raw_length
,
3909 argptr
->expanded
= obuf
.buf
;
3910 argptr
->expand_length
= obuf
.length
;
3911 argptr
->free2
= obuf
.buf
;
3914 lim
= buf
+ argptr
->raw_length
;
3917 while (buf
!= lim
) {
3918 register U_CHAR c
= *buf
++;
3920 /* Internal sequences of whitespace are replaced by one space
3921 in most cases, but not always. So count all the whitespace
3922 in case we need to keep it all. */
3923 if (c
== '\"' || c
== '\\') /* escape these chars */
3925 else if (!isprint (c
))
3928 argptr
->stringified_length
= totlen
;
3933 /* Scan text from START (inclusive) up to LIMIT (exclusive),
3934 counting parens in *DEPTHPTR,
3935 and return if reach LIMIT
3936 or before a `)' that would make *DEPTHPTR negative
3937 or before a comma when *DEPTHPTR is zero.
3938 Single and double quotes are matched and termination
3939 is inhibited within them. Comments also inhibit it.
3940 Value returned is pointer to stopping place.
3942 Increment *NEWLINES each time a newline is passed.
3943 Set *COMMENTS to 1 if a comment is seen. */
3946 macarg1 (start
, limit
, depthptr
, newlines
, comments
)
3948 register U_CHAR
*limit
;
3949 int *depthptr
, *newlines
, *comments
;
3951 register U_CHAR
*bp
= start
;
3953 while (bp
< limit
) {
3959 if (--(*depthptr
) < 0)
3963 /* Traditionally, backslash makes following char not special. */
3967 /* But count source lines anyway. */
3976 if (bp
[1] == '\\' && bp
[2] == '\n')
3977 newline_fix (bp
+ 1);
3978 if (bp
[1] != '*' || bp
+ 1 >= limit
)
3982 while (bp
+ 1 < limit
) {
3984 && bp
[1] == '\\' && bp
[2] == '\n')
3985 newline_fix (bp
+ 1);
3986 if (bp
[0] == '*' && bp
[1] == '/')
3988 if (*bp
== '\n') ++*newlines
;
3997 for (quotec
= *bp
++; bp
+ 1 < limit
&& *bp
!= quotec
; bp
++) {
4002 while (*bp
== '\\' && bp
[1] == '\n') {
4005 } else if (*bp
== '\n') {
4014 if ((*depthptr
) == 0)
4024 /* Discard comments and duplicate newlines
4025 in the string of length LENGTH at START,
4026 except inside of string constants.
4027 The string is copied into itself with its beginning staying fixed.
4029 NEWLINES is the number of newlines that must be duplicated.
4030 We assume that that much extra space is available past the end
4034 discard_comments (start
, length
, newlines
)
4039 register U_CHAR
*ibp
;
4040 register U_CHAR
*obp
;
4041 register U_CHAR
*limit
;
4044 /* If we have newlines to duplicate, copy everything
4045 that many characters up. Then, in the second part,
4046 we will have room to insert the newlines
4048 NEWLINES may actually be too large, because it counts
4049 newlines in string constants, and we don't duplicate those.
4050 But that does no harm. */
4052 ibp
= start
+ length
;
4053 obp
= ibp
+ newlines
;
4055 while (limit
!= ibp
)
4059 ibp
= start
+ newlines
;
4060 limit
= start
+ length
+ newlines
;
4063 while (ibp
< limit
) {
4064 *obp
++ = c
= *ibp
++;
4067 /* Duplicate the newline. */
4079 if (*ibp
== '\\' && ibp
[1] == '\n')
4081 /* Delete any comment. */
4082 if (ibp
[0] != '*' || ibp
+ 1 >= limit
)
4086 while (ibp
+ 1 < limit
) {
4088 && ibp
[1] == '\\' && ibp
[2] == '\n')
4089 newline_fix (ibp
+ 1);
4090 if (ibp
[0] == '*' && ibp
[1] == '/')
4099 /* Notice and skip strings, so that we don't
4100 think that comments start inside them,
4101 and so we don't duplicate newlines in them. */
4104 while (ibp
< limit
) {
4105 *obp
++ = c
= *ibp
++;
4108 if (c
== '\n' && quotec
== '\'')
4110 if (c
== '\\' && ibp
< limit
) {
4111 while (*ibp
== '\\' && ibp
[1] == '\n')
4125 /* Core error handling routine. */
4127 v_message (mtype
, line
, msgid
, ap
)
4133 const char *fname
= 0;
4136 if (mtype
== WARNING
&& inhibit_warnings
)
4139 for (i
= indepth
; i
>= 0; i
--)
4140 if (instack
[i
].fname
!= NULL
) {
4142 line
= instack
[i
].lineno
;
4143 fname
= instack
[i
].fname
;
4148 fprintf (stderr
, "%s:%d: ", fname
, line
);
4150 fprintf (stderr
, "%s: ", progname
);
4152 if (mtype
== WARNING
)
4153 fputs ("warning: ", stderr
);
4155 vfprintf (stderr
, msgid
, ap
);
4156 putc ('\n', stderr
);
4163 * error - print error message and increment count of errors.
4166 error
VPARAMS ((const char *msgid
, ...))
4168 #ifndef ANSI_PROTOTYPES
4173 VA_START(ap
, msgid
);
4175 #ifndef ANSI_PROTOTYPES
4176 msgid
= va_arg (ap
, const char *);
4179 v_message (ERROR
, 0, msgid
, ap
);
4183 error_with_line
VPARAMS ((int line
, const char *msgid
, ...))
4185 #ifndef ANSI_PROTOTYPES
4191 VA_START(ap
, msgid
);
4193 #ifndef ANSI_PROTOTYPES
4194 line
= va_arg (ap
, int);
4195 msgid
= va_arg (ap
, const char *);
4198 v_message (ERROR
, line
, msgid
, ap
);
4201 /* Error including a message from `errno'. */
4203 error_from_errno (name
)
4206 error ("%s: %s", name
, strerror (errno
));
4209 /* Print error message but don't count it. */
4211 warning
VPARAMS ((const char *msgid
, ...))
4213 #ifndef ANSI_PROTOTYPES
4218 VA_START(ap
, msgid
);
4220 #ifndef ANSI_PROTOTYPES
4221 msgid
= va_arg (ap
, const char *);
4224 v_message (WARNING
, 0, msgid
, ap
);
4228 fatal
VPARAMS ((const char *msgid
, ...))
4230 #ifndef ANSI_PROTOTYPES
4235 VA_START(ap
, msgid
);
4237 #ifndef ANSI_PROTOTYPES
4238 msgid
= va_arg (ap
, const char *);
4241 v_message (FATAL
, 0, msgid
, ap
);
4242 exit (FATAL_EXIT_CODE
);
4245 /* More 'friendly' abort that prints the location at which we died. */
4247 fancy_abort (line
, func
)
4251 fatal ("Internal error in %s, at tradcpp.c:%d\n\
4252 Please submit a full bug report.\n\
4253 See %s for instructions.", func
, line
, GCCBUGURL
);
4257 perror_with_name (name
)
4260 fprintf (stderr
, "%s: %s: %s\n", progname
, name
, strerror (errno
));
4265 pfatal_with_name (name
)
4268 perror_with_name (name
);
4269 exit (FATAL_EXIT_CODE
);
4272 /* Return the line at which an error occurred.
4273 The error is not necessarily associated with the current spot
4274 in the input stack, so LINE says where. LINE will have been
4275 copied from ip->lineno for the current input level.
4276 If the current level is for a file, we return LINE.
4277 But if the current level is not for a file, LINE is meaningless.
4278 In that case, we return the lineno of the innermost file. */
4280 line_for_error (line
)
4286 for (i
= indepth
; i
>= 0; ) {
4287 if (instack
[i
].fname
!= 0)
4292 line1
= instack
[i
].lineno
;
4298 * If OBUF doesn't have NEEDED bytes after OPTR, make it bigger.
4300 * As things stand, nothing is ever placed in the output buffer to be
4301 * removed again except when it's KNOWN to be part of an identifier,
4302 * so flushing and moving down everything left, instead of expanding,
4307 grow_outbuf (obuf
, needed
)
4308 register FILE_BUF
*obuf
;
4309 register int needed
;
4314 if (obuf
->length
- (obuf
->bufp
- obuf
->buf
) > needed
)
4317 /* Make it at least twice as big as it is now. */
4319 /* Make it have at least 150% of the free space we will need. */
4320 minsize
= (3 * needed
) / 2 + (obuf
->bufp
- obuf
->buf
);
4321 if (minsize
> obuf
->length
)
4322 obuf
->length
= minsize
;
4324 p
= (U_CHAR
*) xrealloc (obuf
->buf
, obuf
->length
);
4325 obuf
->bufp
= p
+ (obuf
->bufp
- obuf
->buf
);
4329 /* Symbol table for macro names and special symbols */
4332 * install a name in the main hash table, even if it is already there.
4333 * name stops with first non alphanumeric, except leading '#'.
4334 * caller must check against redefinition if that is desired.
4335 * delete_macro () removes things installed by install () in fifo order.
4336 * this is important because of the `defined' special symbol used
4337 * in #if, and also if pushdef/popdef directives are ever implemented.
4339 * If LEN is >= 0, it is the length of the name.
4340 * Otherwise, compute the length by scanning the entire name.
4342 * If HASH is >= 0, it is the precomputed hash code.
4343 * Otherwise, compute the hash code.
4345 * caller must set the value, if any is desired.
4348 install (name
, len
, type
, hash
)
4351 enum node_type type
;
4353 /* watch out here if sizeof (U_CHAR *) != sizeof (int) */
4355 register HASHNODE
*hp
;
4356 register int bucket
;
4357 register const U_CHAR
*p
;
4362 while (is_idchar
[*p
])
4368 hash
= hashf (name
, len
, HASHSIZE
);
4370 hp
= (HASHNODE
*) xmalloc (sizeof (HASHNODE
) + len
+ 1);
4372 hp
->bucket_hdr
= &hashtab
[bucket
];
4373 hp
->next
= hashtab
[bucket
];
4374 hashtab
[bucket
] = hp
;
4376 if (hp
->next
!= NULL
)
4377 hp
->next
->prev
= hp
;
4380 hp
->name
= q
= ((U_CHAR
*) hp
) + sizeof (HASHNODE
);
4381 memcpy (q
, name
, len
);
4387 * find the most recent hash node for name name (ending with first
4388 * non-identifier char) installed by install
4390 * If LEN is >= 0, it is the length of the name.
4391 * Otherwise, compute the length by scanning the entire name.
4393 * If HASH is >= 0, it is the precomputed hash code.
4394 * Otherwise, compute the hash code.
4397 lookup (name
, len
, hash
)
4402 register const U_CHAR
*bp
;
4403 register HASHNODE
*bucket
;
4406 for (bp
= name
; is_idchar
[*bp
]; bp
++) ;
4411 hash
= hashf (name
, len
, HASHSIZE
);
4413 bucket
= hashtab
[hash
];
4415 if (bucket
->length
== len
4416 && strncmp ((char *)bucket
->name
, (char *)name
, len
) == 0)
4418 bucket
= bucket
->next
;
4424 * Delete a hash node. Some weirdness to free junk from macros.
4425 * More such weirdness will have to be added if you define more hash
4426 * types that need it.
4429 /* Note that the DEFINITION of a macro is removed from the hash table
4430 but its storage is not freed. This would be a storage leak
4431 except that it is not reasonable to keep undefining and redefining
4432 large numbers of macros many times.
4433 In any case, this is necessary, because a macro can be #undef'd
4434 in the middle of reading the arguments to a call to it.
4435 If #undef freed the DEFINITION, that would crash. */
4441 if (hp
->prev
!= NULL
)
4442 hp
->prev
->next
= hp
->next
;
4443 if (hp
->next
!= NULL
)
4444 hp
->next
->prev
= hp
->prev
;
4446 /* make sure that the bucket chain header that
4447 the deleted guy was on points to the right thing afterwards. */
4448 if (hp
== *hp
->bucket_hdr
)
4449 *hp
->bucket_hdr
= hp
->next
;
4455 * return hash function on name. must be compatible with the one
4456 * computed a step at a time, elsewhere
4459 hashf (name
, len
, hashsize
)
4460 register const U_CHAR
*name
;
4467 r
= HASHSTEP (r
, *name
++);
4469 return MAKE_POS (r
) % hashsize
;
4472 /* Dump all macro definitions as #defines to stdout. */
4479 for (bucket
= 0; bucket
< HASHSIZE
; bucket
++) {
4480 register HASHNODE
*hp
;
4482 for (hp
= hashtab
[bucket
]; hp
; hp
= hp
->next
) {
4483 if (hp
->type
== T_MACRO
) {
4484 register DEFINITION
*defn
= hp
->value
.defn
;
4490 /* Print the definition of the macro HP. */
4492 printf ("#define %s", hp
->name
);
4493 if (defn
->nargs
>= 0) {
4497 for (i
= 0; i
< defn
->nargs
; i
++) {
4498 dump_arg_n (defn
, i
);
4499 if (i
+ 1 < defn
->nargs
)
4509 for (ap
= defn
->pattern
; ap
!= NULL
; ap
= ap
->next
) {
4510 dump_defn_1 (defn
->expansion
, offset
, ap
->nchars
);
4511 if (ap
->nchars
!= 0)
4513 offset
+= ap
->nchars
;
4516 if (ap
->raw_before
&& !concat
)
4519 dump_arg_n (defn
, ap
->argno
);
4520 if (ap
->raw_after
) {
4525 dump_defn_1 (defn
->expansion
, offset
, defn
->length
- offset
);
4532 /* Output to stdout a substring of a macro definition.
4533 BASE is the beginning of the definition.
4534 Output characters START thru LENGTH.
4535 Discard newlines outside of strings, thus
4536 converting funny-space markers to ordinary spaces. */
4538 dump_defn_1 (base
, start
, length
)
4543 U_CHAR
*p
= base
+ start
;
4544 U_CHAR
*limit
= base
+ start
+ length
;
4549 else if (*p
== '\"' || *p
=='\'') {
4550 U_CHAR
*p1
= skip_quoted_string (p
, limit
, 0, 0, 0, 0);
4551 fwrite (p
, p1
- p
, 1, stdout
);
4558 /* Print the name of argument number ARGNUM of macro definition DEFN.
4559 Recall that DEFN->argnames contains all the arg names
4560 concatenated in reverse order with comma-space in between. */
4562 dump_arg_n (defn
, argnum
)
4566 register U_CHAR
*p
= defn
->argnames
;
4567 while (argnum
+ 1 < defn
->nargs
) {
4568 p
= (U_CHAR
*) strchr ((char *)p
, ' ') + 1;
4572 while (*p
&& *p
!= ',') {
4578 /* Initialize syntactic classifications of characters. */
4580 initialize_char_syntax ()
4585 * Set up is_idchar and is_idstart tables. These should be
4586 * faster than saying (is_alpha (c) || c == '_'), etc.
4587 * Must do set up these things before calling any routines tthat
4590 for (i
= 'a'; i
<= 'z'; i
++) {
4591 is_idchar
[i
- 'a' + 'A'] = 1;
4593 is_idstart
[i
- 'a' + 'A'] = 1;
4596 for (i
= '0'; i
<= '9'; i
++)
4599 is_idstart
['_'] = 1;
4601 /* horizontal space table */
4602 is_hor_space
[' '] = 1;
4603 is_hor_space
['\t'] = 1;
4604 is_hor_space
['\v'] = 1;
4605 is_hor_space
['\f'] = 1;
4606 is_hor_space
['\r'] = 1;
4616 /* Initialize the built-in macros. */
4617 #define DSC(x) U x, sizeof x - 1
4618 #define install_spec(name, type) \
4619 install(DSC(name), type, -1);
4620 #define install_value(name, val) \
4621 hp = install(DSC(name), T_CONST, -1); hp->value.cpval = val;
4623 initialize_builtins ()
4627 install_spec ("__BASE_FILE__", T_BASE_FILE
);
4628 install_spec ("__DATE__", T_DATE
);
4629 install_spec ("__FILE__", T_FILE
);
4630 install_spec ("__TIME__", T_TIME
);
4631 install_spec ("__VERSION__", T_VERSION
);
4632 install_spec ("__INCLUDE_LEVEL__", T_INCLUDE_LEVEL
);
4633 install_spec ("__LINE__", T_SPECLINE
);
4635 #ifndef NO_BUILTIN_SIZE_TYPE
4636 install_value ("__SIZE_TYPE__", SIZE_TYPE
);
4638 #ifndef NO_BUILTIN_PTRDIFF_TYPE
4639 install_value ("__PTRDIFF_TYPE__", PTRDIFF_TYPE
);
4641 #ifndef NO_BUILTIN_WCHAR_TYPE
4642 install_value ("__WCHAR_TYPE__", WCHAR_TYPE
);
4644 #ifndef NO_BUILTIN_WINT_TYPE
4645 install_value ("__WINT_TYPE__", WINT_TYPE
);
4647 install_value ("__REGISTER_PREFIX__", REGISTER_PREFIX
);
4648 install_value ("__USER_LABEL_PREFIX__", user_label_prefix
);
4652 #undef install_value
4655 * process a given definition string, for initialization
4656 * If STR is just an identifier, define it with value 1.
4657 * If STR has anything after the identifier, then it should
4658 * be identifier-space-definition.
4661 make_definition (str
)
4665 struct directive
*kt
;
4667 size_t len
= strlen ((char *)str
);
4669 p
= (U_CHAR
*) strchr ((char *)str
, '=');
4671 /* Change -DFOO into #define FOO 1 */
4672 buf
= (U_CHAR
*) alloca (len
+ 3);
4673 memcpy (buf
, str
, len
);
4674 memcpy (buf
+ len
, " 1", 3);
4677 buf
= (U_CHAR
*) alloca (len
+ 1);
4678 memcpy (buf
, str
, len
+ 1);
4682 ip
= &instack
[++indepth
];
4683 ip
->fname
= "*Initialization*";
4685 ip
->buf
= ip
->bufp
= buf
;
4690 ip
->if_stack
= if_stack
;
4692 for (kt
= directive_table
; kt
->type
!= T_DEFINE
; kt
++)
4695 /* pass NULL as output ptr to do_define since we KNOW it never
4696 does any output.... */
4697 do_define (buf
, buf
+ ip
->length
, NULL
, kt
);
4701 /* JF, this does the work for the -U option */
4707 struct directive
*kt
;
4709 ip
= &instack
[++indepth
];
4710 ip
->fname
= "*undef*";
4712 ip
->buf
= ip
->bufp
= str
;
4713 ip
->length
= strlen ((char *)str
);
4717 ip
->if_stack
= if_stack
;
4719 for (kt
= directive_table
; kt
->type
!= T_UNDEF
; kt
++)
4722 do_undef (str
, str
+ ip
->length
, NULL
, kt
);
4726 /* Add output to `deps_buffer' for the -M switch.
4727 STRING points to the text to be output.
4728 SIZE is the number of bytes, or 0 meaning output until a null.
4729 If SIZE is nonzero, we break the line first, if it is long enough. */
4731 deps_output (string
, size
)
4735 #ifndef MAX_OUTPUT_COLUMNS
4736 #define MAX_OUTPUT_COLUMNS 75
4738 if (size
!= 0 && deps_column
!= 0
4739 && size
+ deps_column
> MAX_OUTPUT_COLUMNS
) {
4740 deps_output ("\\\n ", 0);
4745 size
= strlen (string
);
4747 if (deps_size
+ size
+ 1 > deps_allocated_size
) {
4748 deps_allocated_size
= deps_size
+ size
+ 50;
4749 deps_allocated_size
*= 2;
4750 deps_buffer
= (char *) xrealloc (deps_buffer
, deps_allocated_size
);
4752 memcpy (&deps_buffer
[deps_size
], string
, size
);
4754 deps_column
+= size
;
4755 deps_buffer
[deps_size
] = 0;
4758 /* Get the file-mode and data size of the file open on FD
4759 and store them in *MODE_POINTER and *SIZE_POINTER. */
4762 file_size_and_mode (fd
, mode_pointer
, size_pointer
)
4769 if (fstat (fd
, &sbuf
) < 0) return -1;
4770 if (mode_pointer
) *mode_pointer
= sbuf
.st_mode
;
4771 if (size_pointer
) *size_pointer
= sbuf
.st_size
;