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 2, 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, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
25 #include "cppdefault.h"
28 typedef unsigned char U_CHAR
;
30 /* Name under which this program was invoked. */
32 static const char *progname
;
34 /* Current maximum length of directory names in the search path
35 for include files. (Altered as we get more of them.) */
37 size_t max_include_len
;
39 /* Nonzero means copy comments into the output file. */
41 int put_out_comments
= 0;
43 /* Nonzero means print the names of included files rather than
44 the preprocessed output. 1 means just the #include "...",
45 2 means #include <...> as well. */
49 /* Nonzero means don't output line number information. */
53 /* Nonzero means inhibit output of the preprocessed text
54 and instead output the definitions of all user-defined macros
55 in a form suitable for use as input to cccp. */
59 /* Nonzero means don't print warning messages. -w. */
61 int inhibit_warnings
= 0;
63 /* Nonzero means warn if slash-star appears in a comment. */
67 /* Nonzero causes output not to be done,
68 but directives such as #define that have side effects
73 /* Value of __USER_LABEL_PREFIX__. Target-dependent, also controlled
74 by -f(no-)leading-underscore. */
75 static const char *user_label_prefix
;
77 /* I/O buffer structure.
78 The `fname' field is nonzero for source files and #include files
79 and for the dummy text used for -D and -U.
80 It is zero for rescanning results of macro expansion
81 and for expanding macro arguments. */
82 #define INPUT_STACK_MAX 200
89 /* Macro that this level is the expansion of.
90 Included so that we can reenable the macro
91 at the end of this level. */
92 struct hashnode
*macro
;
93 /* Value of if_stack at start of this file.
94 Used to prohibit unmatched #endif (etc) in an include file. */
95 struct if_stack
*if_stack
;
96 /* Object to be freed at end of input at this level. */
98 } instack
[INPUT_STACK_MAX
];
100 typedef struct file_buf FILE_BUF
;
102 /* Current nesting level of input sources.
103 `instack[indepth]' is the level currently being read. */
105 #define CHECK_DEPTH(code) \
106 if (indepth >= (INPUT_STACK_MAX - 1)) \
108 error_with_line (line_for_error (instack[indepth].lineno), \
109 "macro or #include recursion too deep"); \
113 /* Current depth in #include directives that use <...>. */
114 int system_include_depth
= 0;
116 /* The output buffer. Its LENGTH field is the amount of room allocated
117 for the buffer, not the number of chars actually present. To get
118 that, subtract outbuf.buf from outbuf.bufp. */
120 #define OUTBUF_SIZE 10 /* initial size of output buffer */
123 /* Grow output buffer OBUF points at
124 so it can hold at least NEEDED more chars. */
126 #define check_expand(OBUF, NEEDED) do { \
127 if ((OBUF)->length - ((OBUF)->bufp - (OBUF)->buf) <= (NEEDED)) \
128 grow_outbuf ((OBUF), (NEEDED)); \
131 struct file_name_list
133 struct file_name_list
*next
;
137 struct file_name_list
*include
= 0; /* First dir to search */
138 /* First dir to search for <file> */
139 struct file_name_list
*first_bracket_include
= 0;
140 struct file_name_list
*last_include
= 0; /* Last in chain */
142 /* List of included files that contained #once. */
143 struct file_name_list
*dont_repeat_files
= 0;
145 /* List of other included files. */
146 struct file_name_list
*all_include_files
= 0;
148 /* Structure allocated for every #define. For a simple replacement
151 nargs = -1, the `pattern' list is null, and the expansion is just
152 the replacement text. Nargs = 0 means a functionlike macro with no args,
154 #define getchar() getc (stdin) .
155 When there are args, the expansion is the replacement text with the
156 args squashed out, and the reflist is a list describing how to
157 build the output from the input: e.g., "3 chars, then the 1st arg,
158 then 9 chars, then the 3rd arg, then 0 chars, then the 2nd arg".
159 The chars here come from the expansion. Whatever is left of the
160 expansion after the last arg-occurrence is copied after that arg.
161 Note that the reflist can be arbitrarily long---
162 its length depends on the number of times the arguments appear in
163 the replacement text, not how many args there are. Example:
164 #define f(x) x+x+x+x+x+x+x would have replacement text "++++++" and
166 { (0, 1), (1, 1), (1, 1), ..., (1, 1), NULL }
167 where (x, y) means (nchars, argno). */
169 typedef struct definition DEFINITION
;
172 int length
; /* length of expansion string */
175 struct reflist
*next
;
176 char stringify
; /* nonzero if this arg was preceded by a
178 char raw_before
; /* Nonzero if a ## operator before arg. */
179 char raw_after
; /* Nonzero if a ## operator after arg. */
180 int nchars
; /* Number of literal chars to copy before
181 this arg occurrence. */
182 int argno
; /* Number of arg to substitute (origin-0) */
184 /* Names of macro args, concatenated in reverse order
185 with comma-space between them.
186 The only use of this is that we warn on redefinition
187 if this differs between the old and new definitions. */
188 const U_CHAR
*argnames
;
191 /* different kinds of things that can appear in the value field
192 of a hash node. Actually, this may be useless now. */
199 /* The structure of a node in the hash table. The hash table
200 has entries for all tokens defined by #define commands (type T_MACRO),
201 plus some special tokens like __LINE__ (these each have their own
202 type, and the appropriate code is run when that type of node is seen.
203 It does not contain control words like "#define", which are recognized
204 by a separate piece of code. */
206 /* different flavors of hash nodes --- also used in keyword table */
208 T_DEFINE
= 1, /* `#define' */
209 T_INCLUDE
, /* `#include' */
210 T_IFDEF
, /* `#ifdef' */
211 T_IFNDEF
, /* `#ifndef' */
213 T_ELSE
, /* `#else' */
214 T_ELIF
, /* `#elif' */
215 T_UNDEF
, /* `#undef' */
216 T_LINE
, /* `#line' */
217 T_ENDIF
, /* `#endif' */
218 T_SPECLINE
, /* special symbol `__LINE__' */
219 T_DATE
, /* `__DATE__' */
220 T_FILE
, /* `__FILE__' */
221 T_BASE_FILE
, /* `__BASE_FILE__' */
222 T_INCLUDE_LEVEL
, /* `__INCLUDE_LEVEL__' */
223 T_VERSION
, /* `__VERSION__' */
224 T_TIME
, /* `__TIME__' */
225 T_CONST
, /* Constant value, used by `__STDC__' */
226 T_MACRO
, /* macro defined by `#define' */
227 T_SPEC_DEFINED
, /* special `defined' macro for use in #if statements */
228 T_UNUSED
/* Used for something not defined. */
232 struct hashnode
*next
; /* double links for easy deletion */
233 struct hashnode
*prev
;
234 struct hashnode
**bucket_hdr
; /* also, a back pointer to this node's hash
235 chain is kept, in case the node is the head
236 of the chain and gets deleted. */
237 enum node_type type
; /* type of special token */
238 int length
; /* length of token, for quick comparison */
239 U_CHAR
*name
; /* the actual name */
240 union hashval value
; /* pointer to expansion, or whatever */
243 typedef struct hashnode HASHNODE
;
245 /* Some definitions for the hash table. The hash function MUST be
246 computed as shown in hashf () below. That is because the rescan
247 loop computes the hash value `on the fly' for most tokens,
248 in order to avoid the overhead of a lot of procedure calls to
249 the hashf () function. Hashf () only exists for the sake of
250 politeness, for use when speed isn't so important. */
252 #define HASHSIZE 1403
253 HASHNODE
*hashtab
[HASHSIZE
];
254 #define HASHSTEP(old, c) ((old << 2) + c)
255 #define MAKE_POS(v) (v & 0x7fffffff) /* make number positive */
257 /* `struct directive' defines one #-directive, including how to handle it. */
260 int length
; /* Length of name */
261 void (*func
) PARAMS ((U_CHAR
*, U_CHAR
*, FILE_BUF
*, struct directive
*));
262 /* Function to handle directive */
263 const char *name
; /* Name of directive */
264 enum node_type type
; /* Code which describes which directive. */
267 /* Last arg to output_line_command. */
268 enum file_change_code
{same_file
, enter_file
, leave_file
};
270 /* This structure represents one parsed argument in a macro call.
271 `raw' points to the argument text as written (`raw_length' is its length).
272 `expanded' points to the argument's macro-expansion
273 (its length is `expand_length').
274 `stringified_length' is the length the argument would have
276 `free1' and `free2', if nonzero, point to blocks to be freed
277 when the macro argument data is no longer needed. */
280 U_CHAR
*raw
, *expanded
;
281 int raw_length
, expand_length
;
282 int stringified_length
;
283 U_CHAR
*free1
, *free2
;
288 /* The arglist structure is built by do_define to tell
289 collect_definition where the argument names begin. That
290 is, for a define like "#define f(x,y,z) foo+x-bar*y", the arglist
291 would contain pointers to the strings x, y, and z.
292 Collect_definition would then build a DEFINITION node,
293 with reflist nodes pointing to the places x, y, and z had
294 appeared. So the arglist is just convenience data passed
295 between these two routines. It is not kept around after
296 the current #define has been processed and entered into the
300 struct arglist
*next
;
306 /* Function prototypes. */
308 static void do_define
PARAMS ((U_CHAR
*, U_CHAR
*, FILE_BUF
*, struct directive
*));
309 static void do_line
PARAMS ((U_CHAR
*, U_CHAR
*, FILE_BUF
*, struct directive
*));
310 static void do_include
PARAMS ((U_CHAR
*, U_CHAR
*, FILE_BUF
*, struct directive
*));
311 static void do_undef
PARAMS ((U_CHAR
*, U_CHAR
*, FILE_BUF
*, struct directive
*));
312 static void do_if
PARAMS ((U_CHAR
*, U_CHAR
*, FILE_BUF
*, struct directive
*));
313 static void do_xifdef
PARAMS ((U_CHAR
*, U_CHAR
*, FILE_BUF
*, struct directive
*));
314 static void do_else
PARAMS ((U_CHAR
*, U_CHAR
*, FILE_BUF
*, struct directive
*));
315 static void do_elif
PARAMS ((U_CHAR
*, U_CHAR
*, FILE_BUF
*, struct directive
*));
316 static void do_endif
PARAMS ((U_CHAR
*, U_CHAR
*, FILE_BUF
*, struct directive
*));
318 static struct hashnode
*install
PARAMS ((const U_CHAR
*, int, enum node_type
, int));
319 static int hashf
PARAMS ((const U_CHAR
*, int, int));
320 static int compare_defs
PARAMS ((DEFINITION
*, DEFINITION
*));
321 static int comp_def_part
PARAMS ((int, const U_CHAR
*, int,
322 const U_CHAR
*, int, int));
323 static void delete_macro
PARAMS ((HASHNODE
*));
325 /* First arg to v_message. */
326 enum msgtype
{ WARNING
= 0, ERROR
, FATAL
};
327 static void v_message
PARAMS ((enum msgtype mtype
, int line
,
328 const char *msgid
, va_list ap
))
329 ATTRIBUTE_PRINTF (3, 0);
331 static int line_for_error
PARAMS ((int));
333 /* We know perfectly well which file this is, so we don't need to
336 #if (GCC_VERSION >= 2007)
337 #define abort() fancy_abort(__LINE__, __FUNCTION__)
339 #define abort() fancy_abort(__LINE__, 0);
342 static void macroexpand
PARAMS ((HASHNODE
*, FILE_BUF
*));
343 static void special_symbol
PARAMS ((HASHNODE
*, FILE_BUF
*));
344 static void dump_all_macros
PARAMS ((void));
345 static void dump_defn_1
PARAMS ((const U_CHAR
*, int, int));
346 static void dump_arg_n
PARAMS ((DEFINITION
*, int));
347 static void conditional_skip
PARAMS ((FILE_BUF
*, int, enum node_type
));
348 static void skip_if_group
PARAMS ((FILE_BUF
*, int));
349 static void output_line_command
PARAMS ((FILE_BUF
*, FILE_BUF
*,
350 int, enum file_change_code
));
352 static int eval_if_expression
PARAMS ((const U_CHAR
*, int));
354 static void initialize_char_syntax
PARAMS ((void));
355 static void initialize_builtins
PARAMS ((void));
356 static void make_definition
PARAMS ((const U_CHAR
*));
357 static void make_undef
PARAMS ((U_CHAR
*));
359 static void grow_outbuf
PARAMS ((FILE_BUF
*, int));
360 static int handle_directive
PARAMS ((FILE_BUF
*, FILE_BUF
*));
361 static void finclude
PARAMS ((int, const char *, FILE_BUF
*));
362 static void deps_output
PARAMS ((const char *, int));
363 static void rescan
PARAMS ((FILE_BUF
*, int));
364 static void newline_fix
PARAMS ((U_CHAR
*));
365 static void name_newline_fix
PARAMS ((U_CHAR
*));
366 static U_CHAR
*macarg1
PARAMS ((U_CHAR
*, const U_CHAR
*, int *,
368 static const char *macarg
PARAMS ((struct argdata
*));
369 static int discard_comments
PARAMS ((U_CHAR
*, int, int));
370 static int file_size_and_mode
PARAMS ((int, int *, long *));
372 static U_CHAR
*skip_to_end_of_comment
PARAMS ((FILE_BUF
*, int *));
373 static U_CHAR
*skip_quoted_string
PARAMS ((const U_CHAR
*, const U_CHAR
*,
374 int, int *, int *, int *));
376 int main
PARAMS ((int, char **));
378 /* Convenience. Write U"string" to get an unsigned string constant. */
379 #define U (const unsigned char *)
381 /* Here is the actual list of #-directives, most-often-used first. */
383 struct directive directive_table
[] = {
384 { 6, do_define
, "define", T_DEFINE
},
385 { 7, do_include
, "include", T_INCLUDE
},
386 { 5, do_endif
, "endif", T_ENDIF
},
387 { 5, do_xifdef
, "ifdef", T_IFDEF
},
388 { 2, do_if
, "if", T_IF
, },
389 { 4, do_else
, "else", T_ELSE
},
390 { 6, do_xifdef
, "ifndef", T_IFNDEF
},
391 { 5, do_undef
, "undef", T_UNDEF
},
392 { 4, do_line
, "line", T_LINE
},
393 { 4, do_elif
, "elif", T_ELIF
},
394 { -1, 0, "", T_UNUSED
},
397 /* table to tell if char can be part of a C identifier. */
398 U_CHAR is_idchar
[256];
399 /* table to tell if char can be first char of a c identifier. */
400 U_CHAR is_idstart
[256];
401 /* table to tell if c is horizontal space. */
402 U_CHAR is_hor_space
[256];
403 /* table to tell if c is horizontal or vertical space. */
404 U_CHAR is_space
[256];
406 #define SKIP_WHITE_SPACE(p) do { while (is_hor_space[*p]) p++; } while (0)
407 #define SKIP_ALL_WHITE_SPACE(p) do { while (is_space[*p]) p++; } while (0)
409 int errors
= 0; /* Error counter for exit code */
411 static FILE_BUF expand_to_temp_buffer
PARAMS ((const U_CHAR
*, const U_CHAR
*, int));
412 static DEFINITION
*collect_expansion
PARAMS ((U_CHAR
*, U_CHAR
*, int,
415 /* Stack of conditionals currently in progress
416 (including both successful and failing conditionals). */
419 struct if_stack
*next
; /* for chaining to the next stack frame */
420 const char *fname
; /* copied from input when frame is made */
421 int lineno
; /* similarly */
422 int if_succeeded
; /* true if a leg of this if-group
423 has been passed through rescan */
424 enum node_type type
; /* type of last directive seen in this group */
426 typedef struct if_stack IF_STACK_FRAME
;
427 IF_STACK_FRAME
*if_stack
= NULL
;
429 /* Buffer of -M output. */
433 /* Number of bytes allocated in above. */
434 int deps_allocated_size
;
436 /* Number of bytes used. */
439 /* Number of bytes since the last newline. */
442 /* Nonzero means -I- has been seen,
443 so don't look for #include "foo" the source-file directory. */
453 const char *in_fname
, *out_fname
;
456 const char **pend_files
= (const char **) xmalloc (argc
* sizeof (char *));
457 const char **pend_defs
= (const char **) xmalloc (argc
* sizeof (char *));
458 const char **pend_undefs
= (const char **) xmalloc (argc
* sizeof (char *));
459 int no_standard_includes
= 0;
461 /* Non-0 means don't output the preprocessed program. */
462 int inhibit_output
= 0;
464 /* Stream on which to print the dependency information. */
465 FILE *deps_stream
= 0;
466 /* Target-name to write with the dependency information. */
467 char *deps_target
= 0;
470 /* Get rid of any avoidable limit on stack size. */
474 /* Set the stack limit huge so that alloca (particularly stringtab
475 * in dbxread.c) does not fail. */
476 getrlimit (RLIMIT_STACK
, &rlim
);
477 rlim
.rlim_cur
= rlim
.rlim_max
;
478 setrlimit (RLIMIT_STACK
, &rlim
);
480 #endif /* RLIMIT_STACK defined */
487 /* Initialize is_idchar to allow $. */
488 initialize_char_syntax ();
490 no_line_commands
= 0;
494 max_include_len
= cpp_GCC_INCLUDE_DIR_len
+ 7; /* ??? */
496 memset (pend_files
, 0, argc
* sizeof (char *));
497 memset (pend_defs
, 0, argc
* sizeof (char *));
498 memset (pend_undefs
, 0, argc
* sizeof (char *));
500 /* Process switches and find input file name. */
502 for (i
= 1; i
< argc
; i
++) {
503 if (argv
[i
][0] != '-') {
504 if (out_fname
!= NULL
)
505 fatal ("Usage: %s [switches] input output", argv
[0]);
506 else if (in_fname
!= NULL
)
511 switch (argv
[i
][1]) {
516 break; /* Ignore for compatibility with ISO/extended cpp. */
519 if (!strcmp (argv
[i
], "-lang-c++")
520 || !strcmp (argv
[i
], "-lang-objc++"))
521 fatal ("-traditional is not supported in C++");
522 else if (!strcmp (argv
[i
], "-lang-c89"))
523 fatal ("-traditional and -ansi are mutually exclusive");
524 else if (!strcmp (argv
[i
], "-lang-objc"))
525 pend_defs
[i
] = "__OBJC__";
526 else if (!strcmp (argv
[i
], "-lang-asm"))
527 pend_defs
[i
] = "__ASSEMBLER__";
528 else if (!strcmp (argv
[i
], "-lang-fortran"))
529 pend_defs
[i
] = "_LANGUAGE_FORTRAN";
530 /* All other possibilities ignored. */
534 if (!strcmp (argv
[i
], "-include"))
537 fatal ("Filename missing after -i option");
539 pend_files
[i
] = argv
[i
+1], i
++;
541 else if (!strcmp (argv
[i
], "-iprefix"))
542 i
++; /* Ignore for compatibility */
543 else if (!strcmp (argv
[i
], "-isystem")
544 || !strcmp (argv
[i
], "-iwithprefix")
545 || !strcmp (argv
[i
], "-iwithprefixbefore")
546 || !strcmp (argv
[i
], "-idirafter"))
547 goto add_include
; /* best we can do */
552 if (out_fname
!= NULL
)
553 fatal ("Output filename specified twice");
555 fatal ("Filename missing after -o option");
556 out_fname
= argv
[++i
];
557 if (!strcmp (out_fname
, "-"))
562 inhibit_warnings
= 1;
566 if (!strcmp (argv
[i
], "-Wcomments"))
568 else if (!strcmp (argv
[i
], "-Wcomment"))
570 else if (!strcmp (argv
[i
], "-Wall")) {
576 if (!strcmp (argv
[i
], "-fleading-underscore"))
577 user_label_prefix
= "_";
578 else if (!strcmp (argv
[i
], "-fno-leading-underscore"))
579 user_label_prefix
= "";
583 if (!strcmp (argv
[i
], "-M"))
585 else if (!strcmp (argv
[i
], "-MM"))
596 fprintf (stderr
, "GNU traditional CPP version %s\n", version_string
);
605 else if (i
+ 1 == argc
)
606 fatal ("Macro name missing after -D option");
614 case 'U': /* JF #undef something */
616 pend_undefs
[i
] = argv
[i
] + 2;
617 else if (i
+ 1 == argc
)
618 fatal ("Macro name missing after -U option");
620 pend_undefs
[i
] = argv
[i
+1], i
++;
624 put_out_comments
= 1;
628 if (!strcmp (argv
[i
], "-pedantic"))
629 fatal ("-pedantic and -traditional are mutually exclusive");
633 if (!strcmp (argv
[i
], "-trigraphs"))
634 fatal ("-trigraphs and -traditional are mutually exclusive");
638 no_line_commands
= 1;
641 case 'I': /* Add directory to path for includes. */
644 struct file_name_list
*dirtmp
;
646 if (! ignore_srcdir
&& !strcmp (argv
[i
] + 2, "-"))
649 dirtmp
= (struct file_name_list
*)
650 xmalloc (sizeof (struct file_name_list
));
651 dirtmp
->next
= 0; /* New one goes on the end */
655 last_include
->next
= dirtmp
;
656 last_include
= dirtmp
; /* Tail follows the last one */
657 if (argv
[i
][1] == 'I' && argv
[i
][2] != 0)
658 dirtmp
->fname
= argv
[i
] + 2;
659 else if (i
+ 1 == argc
)
660 fatal ("Directory name missing after -I option");
662 dirtmp
->fname
= argv
[++i
];
663 if (strlen (dirtmp
->fname
) > max_include_len
)
664 max_include_len
= strlen (dirtmp
->fname
);
665 if (ignore_srcdir
&& first_bracket_include
== 0)
666 first_bracket_include
= dirtmp
;
672 /* -nostdinc causes no default include directories.
673 You must specify all include-file directories with -I. */
674 no_standard_includes
= 1;
677 case '\0': /* JF handle '-' as file name meaning stdin or stdout */
678 if (in_fname
== NULL
) {
681 } else if (out_fname
== NULL
) {
684 } /* else fall through into error */
687 fatal ("Invalid option `%s'", argv
[i
]);
692 if (user_label_prefix
== 0)
693 user_label_prefix
= USER_LABEL_PREFIX
;
695 /* Initialize is_idchar. */
696 initialize_char_syntax ();
698 /* Install __LINE__, etc. Must follow initialize_char_syntax
699 and option processing. */
700 initialize_builtins ();
702 /* Do defines specified with -D and undefines specified with -U. */
703 for (i
= 1; i
< argc
; i
++)
705 make_definition ((const U_CHAR
*)pend_defs
[i
]);
706 else if (pend_undefs
[i
])
707 make_undef ((U_CHAR
*)pend_undefs
[i
]);
709 /* Unless -fnostdinc,
710 tack on the standard include file dirs to the specified list */
711 if (!no_standard_includes
) {
712 const struct default_include
*di
;
713 struct file_name_list
*old_last_include
= last_include
;
714 struct file_name_list
*dirtmp
;
715 for (di
= cpp_include_defaults
; di
->fname
; di
++) {
718 dirtmp
= (struct file_name_list
*)
719 xmalloc (sizeof (struct file_name_list
));
720 dirtmp
->next
= 0; /* New one goes on the end */
724 last_include
->next
= dirtmp
;
725 last_include
= dirtmp
; /* Tail follows the last one */
726 dirtmp
->fname
= di
->fname
;
727 if (strlen (dirtmp
->fname
) > max_include_len
)
728 max_include_len
= strlen (dirtmp
->fname
);
731 if (ignore_srcdir
&& first_bracket_include
== 0)
732 first_bracket_include
= old_last_include
->next
;
735 /* Initialize output buffer */
737 outbuf
.buf
= (U_CHAR
*) xmalloc (OUTBUF_SIZE
);
738 outbuf
.bufp
= outbuf
.buf
;
739 outbuf
.length
= OUTBUF_SIZE
;
741 /* Scan the -i files before the main input.
742 Much like #including them, but with no_output set
743 so that only their macro definitions matter. */
746 for (i
= 1; i
< argc
; i
++)
748 int fd
= open (pend_files
[i
], O_RDONLY
, 0666);
750 perror_with_name (pend_files
[i
]);
751 return FATAL_EXIT_CODE
;
753 finclude (fd
, pend_files
[i
], &outbuf
);
757 /* Create an input stack level for the main input file
758 and copy the entire contents of the file into it. */
760 fp
= &instack
[++indepth
];
762 /* JF check for stdin */
763 if (in_fname
== NULL
|| *in_fname
== 0) {
766 } else if ((f
= open (in_fname
, O_RDONLY
, 0666)) < 0)
769 /* Either of two environment variables can specify output of deps.
770 Its value is either "OUTPUT_FILE" or "OUTPUT_FILE DEPS_TARGET",
771 where OUTPUT_FILE is the file to write deps info to
772 and DEPS_TARGET is the target to mention in the deps. */
775 && (getenv ("SUNPRO_DEPENDENCIES") != 0
776 || getenv ("DEPENDENCIES_OUTPUT") != 0))
778 char *spec
= getenv ("DEPENDENCIES_OUTPUT");
784 spec
= getenv ("SUNPRO_DEPENDENCIES");
790 /* Find the space before the DEPS_TARGET, if there is one. */
791 s
= strchr (spec
, ' ');
795 output_file
= (char *) xmalloc (s
- spec
+ 1);
796 memcpy (output_file
, spec
, s
- spec
);
797 output_file
[s
- spec
] = 0;
805 deps_stream
= fopen (output_file
, "a");
806 if (deps_stream
== 0)
807 pfatal_with_name (output_file
);
809 /* If the -M option was used, output the deps to standard output. */
811 deps_stream
= stdout
;
813 /* For -M, print the expected object file name
814 as the target of this Make-rule. */
816 deps_allocated_size
= 200;
817 deps_buffer
= (char *) xmalloc (deps_allocated_size
);
823 deps_output (deps_target
, 0);
824 deps_output (":", 0);
825 } else if (*in_fname
== 0)
826 deps_output ("-: ", 0);
829 const char *p
= in_fname
;
831 /* Discard all directory prefixes from P. */
837 /* Output P, but remove known suffixes. */
839 if (p
[len
- 2] == '.'
840 && (p
[len
- 1] == 'c' || p
[len
- 1] == 'C' || p
[len
- 1] == 'S'))
841 deps_output (p
, len
- 2);
842 else if (p
[len
- 3] == '.'
844 && p
[len
- 1] == 'c')
845 deps_output (p
, len
- 3);
848 /* Supply our own suffix. */
849 deps_output (".o : ", 0);
850 deps_output (in_fname
, 0);
851 deps_output (" ", 0);
855 if (file_size_and_mode (f
, &st_mode
, &st_size
))
857 fp
->fname
= in_fname
;
859 /* JF all this is mine about reading pipes and ttys */
860 if (!S_ISREG (st_mode
)) {
861 /* Read input from a file that is not a normal disk file.
862 We cannot preallocate a buffer with the correct size,
863 so we must read in the file a piece at the time and make it bigger. */
871 fp
->buf
= (U_CHAR
*) xmalloc (bsize
+ 2);
874 cnt
= read (f
, bufp
, bsize
- size
);
875 if (cnt
< 0) goto sys_error
; /* error! */
876 if (cnt
== 0) break; /* End of file */
879 if (bsize
== size
) { /* Buffer is full! */
881 fp
->buf
= (U_CHAR
*) xrealloc (fp
->buf
, bsize
+ 2);
882 bufp
= fp
->buf
+ size
; /* May have moved */
887 /* Read a file whose size we can determine in advance.
888 For the sake of VMS, st_size is just an upper bound. */
891 fp
->buf
= (U_CHAR
*) xmalloc (st_size
+ 2);
893 while (st_size
> 0) {
894 i
= read (f
, fp
->buf
+ fp
->length
, st_size
);
904 fp
->if_stack
= if_stack
;
906 /* Make sure data ends with a newline. And put a null after it. */
908 if (fp
->length
> 0 && fp
->buf
[fp
->length
-1] != '\n')
909 fp
->buf
[fp
->length
++] = '\n';
910 fp
->buf
[fp
->length
] = '\0';
912 /* Now that we know the input file is valid, open the output. */
914 if (!out_fname
|| !strcmp (out_fname
, ""))
915 out_fname
= "stdout";
916 else if (! freopen (out_fname
, "w", stdout
))
917 pfatal_with_name (out_fname
);
919 output_line_command (fp
, &outbuf
, 0, same_file
);
921 /* Scan the input, processing macros and directives. */
925 /* Now we have processed the entire input
926 Write whichever kind of output has been requested. */
931 else if (! inhibit_output
&& deps_stream
!= stdout
) {
932 if (write (fileno (stdout
), outbuf
.buf
, outbuf
.bufp
- outbuf
.buf
) < 0)
933 fatal ("I/O error on output");
937 fputs (deps_buffer
, deps_stream
);
938 putc ('\n', deps_stream
);
939 if (deps_stream
!= stdout
) {
940 fclose (deps_stream
);
941 if (ferror (deps_stream
))
942 fatal ("I/O error on output");
947 fatal ("I/O error on output");
950 exit (FATAL_EXIT_CODE
);
951 exit (SUCCESS_EXIT_CODE
);
954 pfatal_with_name (in_fname
);
957 /* Move all backslash-newline pairs out of embarrassing places.
958 Exchange all such pairs following BP
959 with any potentially-embarrasing characters that follow them.
960 Potentially-embarrassing characters are / and *
961 (because a backslash-newline inside a comment delimiter
962 would cause it not to be recognized). */
967 register U_CHAR
*p
= bp
;
968 register int count
= 0;
970 /* First count the backslash-newline pairs here. */
972 while (*p
++ == '\\' && *p
++ == '\n')
977 /* Exit if what follows the backslash-newlines is not embarrassing. */
979 if (count
== 0 || (*p
!= '/' && *p
!= '*'))
982 /* Copy all potentially embarrassing characters
983 that follow the backslash-newline pairs
984 down to where the pairs originally started. */
986 while (*p
== '*' || *p
== '/')
989 /* Now write the same number of pairs after the embarrassing chars. */
990 while (count
-- > 0) {
996 /* Like newline_fix but for use within a directive-name.
997 Move any backslash-newlines up past any following symbol constituents. */
999 name_newline_fix (bp
)
1002 register U_CHAR
*p
= bp
;
1003 register int count
= 0;
1005 /* First count the backslash-newline pairs here. */
1007 while (*p
++ == '\\' && *p
++ == '\n')
1012 /* What follows the backslash-newlines is not embarrassing. */
1014 if (count
== 0 || !is_idchar
[*p
])
1017 /* Copy all potentially embarrassing characters
1018 that follow the backslash-newline pairs
1019 down to where the pairs originally started. */
1021 while (is_idchar
[*p
])
1024 /* Now write the same number of pairs after the embarrassing chars. */
1025 while (count
-- > 0) {
1032 * The main loop of the program.
1034 * Read characters from the input stack, transferring them to the
1037 * Macros are expanded and push levels on the input stack.
1038 * At the end of such a level it is popped off and we keep reading.
1039 * At the end of any other kind of level, we return.
1040 * #-directives are handled, except within macros.
1042 * If OUTPUT_MARKS is nonzero, keep Newline markers found in the input
1043 * and insert them when appropriate. This is set while scanning macro
1044 * arguments before substitution. It is zero when scanning for final output.
1045 * There are three types of Newline markers:
1046 * * Newline - follows a macro name that was not expanded
1047 * because it appeared inside an expansion of the same macro.
1048 * This marker prevents future expansion of that identifier.
1049 * When the input is rescanned into the final output, these are deleted.
1050 * These are also deleted by ## concatenation.
1051 * * Newline Space (or Newline and any other whitespace character)
1052 * stands for a place that tokens must be separated or whitespace
1053 * is otherwise desirable, but where the ANSI standard specifies there
1054 * is no whitespace. This marker turns into a Space (or whichever other
1055 * whitespace char appears in the marker) in the final output,
1056 * but it turns into nothing in an argument that is stringified with #.
1057 * Such stringified arguments are the only place where the ANSI standard
1058 * specifies with precision that whitespace may not appear.
1060 * During this function, IP->bufp is kept cached in IBP for speed of access.
1061 * Likewise, OP->bufp is kept in OBP. Before calling a subroutine
1062 * IBP, IP and OBP must be copied back to memory. IP and IBP are
1063 * copied back with the RECACHE macro. OBP must be copied back from OP->bufp
1064 * explicitly, and before RECACHE, since RECACHE uses OBP.
1068 rescan (op
, output_marks
)
1072 /* Character being scanned in main loop. */
1075 /* Length of pending accumulated identifier. */
1076 register int ident_length
= 0;
1078 /* Hash code of pending accumulated identifier. */
1079 register int hash
= 0;
1081 /* Current input level (&instack[indepth]). */
1084 /* Pointer for scanning input. */
1085 register U_CHAR
*ibp
;
1087 /* Pointer to end of input. End of scan is controlled by LIMIT. */
1088 register U_CHAR
*limit
;
1090 /* Pointer for storing output. */
1091 register U_CHAR
*obp
;
1093 /* REDO_CHAR is nonzero if we are processing an identifier
1094 after backing up over the terminating character.
1095 Sometimes we process an identifier without backing up over
1096 the terminating character, if the terminating character
1097 is not special. Backing up is done so that the terminating character
1098 will be dispatched on again once the identifier is dealt with. */
1101 /* 1 if within an identifier inside of which a concatenation
1102 marker (Newline -) has been seen. */
1103 int concatenated
= 0;
1105 /* While scanning a comment or a string constant,
1106 this records the line it started on, for error messages. */
1109 /* Record position of last `real' newline. */
1110 U_CHAR
*beg_of_line
;
1112 /* Pop the innermost input stack level, assuming it is a macro expansion. */
1115 do { ip->macro->type = T_MACRO; \
1116 if (ip->free_ptr) free (ip->free_ptr); \
1117 --indepth; } while (0)
1119 /* Reload `rescan's local variables that describe the current
1120 level of the input stack. */
1123 do { ip = &instack[indepth]; \
1125 limit = ip->buf + ip->length; \
1127 check_expand (op, limit - ibp); \
1129 obp = op->bufp; } while (0)
1131 if (no_output
&& instack
[indepth
].fname
!= 0)
1132 skip_if_group (&instack
[indepth
], 1);
1138 /* Our caller must always put a null after the end of
1139 the input at each input stack level. */
1152 /* Always merge lines ending with backslash-newline,
1153 even in middle of identifier. */
1156 --obp
; /* remove backslash from obuf */
1159 /* Otherwise, backslash suppresses specialness of following char,
1160 so copy it here to prevent the switch from seeing it.
1161 But first get any pending identifier processed. */
1162 if (ident_length
> 0)
1168 /* If this is expanding a macro definition, don't recognize
1169 preprocessor directives. */
1175 /* # keyword: a # must be the first char on the line */
1176 if (beg_of_line
== 0)
1178 if (beg_of_line
+ 1 != ibp
)
1181 /* This # can start a directive. */
1183 --obp
; /* Don't copy the '#' */
1187 if (! handle_directive (ip
, op
)) {
1191 /* Not a known directive: treat it as ordinary text.
1192 IP, OP, IBP, etc. have not been changed. */
1193 if (no_output
&& instack
[indepth
].fname
) {
1194 /* If not generating expanded output,
1195 what we do with ordinary text is skip it.
1196 Discard everything until next # directive. */
1197 skip_if_group (&instack
[indepth
], 1);
1202 ++obp
; /* Copy the '#' after all */
1208 /* A # directive has been successfully processed. */
1209 /* If not generating expanded output, ignore everything until
1210 next # directive. */
1211 if (no_output
&& instack
[indepth
].fname
)
1212 skip_if_group (&instack
[indepth
], 1);
1218 case '\"': /* skip quoted string */
1220 /* A single quoted string is treated like a double -- some
1221 programs (e.g., troff) are perverse this way */
1226 start_line
= ip
->lineno
;
1228 /* Skip ahead to a matching quote. */
1232 if (ip
->macro
!= 0) {
1233 /* try harder: this string crosses a macro expansion boundary */
1245 /* Traditionally, end of line ends a string constant with no error.
1246 So exit the loop and record the new line. */
1254 /* Backslash newline is replaced by nothing at all,
1255 but keep the line counts correct. */
1260 /* ANSI stupidly requires that in \\ the second \
1261 is *not* prevented from combining with a newline. */
1262 while (*ibp
== '\\' && ibp
[1] == '\n') {
1281 if (*ibp
== '\\' && ibp
[1] == '\n')
1283 /* Don't look for comments inside a macro definition. */
1286 /* A comment constitutes white space, so it can terminate an identifier.
1287 Process the identifier, if any. */
1294 /* We have a comment. Skip it, optionally copying it to output. */
1296 start_line
= ip
->lineno
;
1298 ++ibp
; /* Skip the star. */
1300 /* In K+R C, a comment is equivalent to nothing. Note that we
1301 already output the slash; we might not want it. */
1302 if (! put_out_comments
)
1308 U_CHAR
*before_bp
= ibp
;
1310 while (ibp
< limit
) {
1313 if (warn_comments
&& ibp
< limit
&& *ibp
== '*')
1314 warning("`/*' within comment");
1317 if (*ibp
== '\\' && ibp
[1] == '\n')
1319 if (ibp
>= limit
|| *ibp
== '/')
1324 /* Copy the newline into the output buffer, in order to
1325 avoid the pain of a #line every time a multiline comment
1327 if (!put_out_comments
)
1335 error_with_line (line_for_error (start_line
),
1336 "unterminated comment");
1339 if (put_out_comments
) {
1340 memcpy (obp
, before_bp
, ibp
- before_bp
);
1341 obp
+= ibp
- before_bp
;
1347 case '0': case '1': case '2': case '3': case '4':
1348 case '5': case '6': case '7': case '8': case '9':
1349 /* If digit is not part of identifier, it starts a number,
1350 which means that following letters are not an identifier.
1351 "0x5" does not refer to an identifier "x5".
1352 So copy all alphanumerics that follow without accumulating
1353 as an identifier. Periods also, for sake of "3.e7". */
1355 if (ident_length
== 0) {
1356 while (ibp
< limit
) {
1357 while (ibp
< limit
&& ibp
[0] == '\\' && ibp
[1] == '\n') {
1362 if (!ISALNUM (c
) && c
!= '.' && c
!= '_') {
1367 /* A sign can be part of a preprocessing number
1368 if it follows an e. */
1369 if (c
== 'e' || c
== 'E') {
1370 while (ibp
< limit
&& ibp
[0] == '\\' && ibp
[1] == '\n') {
1374 if (ibp
< limit
&& (*ibp
== '+' || *ibp
== '-')) {
1376 /* Traditional C does not let the token go past the sign. */
1386 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
1387 case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
1388 case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
1389 case 's': case 't': case 'u': case 'v': case 'w': case 'x':
1391 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
1392 case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
1393 case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
1394 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
1397 /* Compute step of hash function, to avoid a proc call on every token */
1398 hash
= HASHSTEP (hash
, c
);
1402 /* If reprocessing a macro expansion, newline is a special marker. */
1403 if (ip
->macro
!= 0) {
1404 /* Newline White is a "funny space" to separate tokens that are
1405 supposed to be separate but without space between.
1406 Here White means any horizontal whitespace character.
1407 Newline - marks a recursive macro use that is not
1408 supposed to be expandable. */
1411 /* Newline - inhibits expansion of preceding token.
1412 If expanding a macro arg, we keep the newline -.
1413 In final output, it is deleted. */
1414 if (! concatenated
) {
1419 if (!output_marks
) {
1422 /* If expanding a macro arg, keep the newline -. */
1425 } else if (is_space
[*ibp
]) {
1426 /* Newline Space does not prevent expansion of preceding token
1427 so expand the preceding token and then come back. */
1428 if (ident_length
> 0)
1431 /* If generating final output, newline space makes a space. */
1432 if (!output_marks
) {
1434 /* And Newline Newline makes a newline, so count it. */
1435 if (obp
[-1] == '\n')
1438 /* If expanding a macro arg, keep the newline space.
1439 If the arg gets stringified, newline space makes nothing. */
1442 } else abort (); /* Newline followed by something random? */
1446 /* If there is a pending identifier, handle it and come back here. */
1447 if (ident_length
> 0)
1452 /* Update the line counts and output a #line if necessary. */
1455 if (ip
->lineno
!= op
->lineno
) {
1457 output_line_command (ip
, op
, 1, same_file
);
1458 check_expand (op
, ip
->length
- (ip
->bufp
- ip
->buf
));
1463 /* Come here either after (1) a null character that is part of the input
1464 or (2) at the end of the input, because there is a null there. */
1467 /* Our input really contains a null character. */
1470 /* At end of a macro-expansion level, pop it and read next level. */
1471 if (ip
->macro
!= 0) {
1474 /* If we have an identifier that ends here, process it now, so
1475 we get the right error for recursion. */
1476 if (ident_length
&& ! is_idchar
[*instack
[indepth
- 1].bufp
]) {
1485 /* If we don't have a pending identifier,
1486 return at end of input. */
1487 if (ident_length
== 0) {
1495 /* If we do have a pending identifier, just consider this null
1496 a special character and arrange to dispatch on it again.
1497 The second time, IDENT_LENGTH will be zero so we will return. */
1503 /* Handle the case of a character such as /, ', " or null
1504 seen following an identifier. Back over it so that
1505 after the identifier is processed the special char
1506 will be dispatched on again. */
1516 if (ident_length
> 0) {
1517 register HASHNODE
*hp
;
1519 /* We have just seen an identifier end. If it's a macro, expand it.
1521 IDENT_LENGTH is the length of the identifier
1522 and HASH is its hash code.
1524 The identifier has already been copied to the output,
1525 so if it is a macro we must remove it.
1527 If REDO_CHAR is 0, the char that terminated the identifier
1528 has been skipped in the output and the input.
1529 OBP-IDENT_LENGTH-1 points to the identifier.
1530 If the identifier is a macro, we must back over the terminator.
1532 If REDO_CHAR is 1, the terminating char has already been
1533 backed over. OBP-IDENT_LENGTH points to the identifier. */
1535 for (hp
= hashtab
[MAKE_POS (hash
) % HASHSIZE
]; hp
!= NULL
;
1538 if (hp
->length
== ident_length
) {
1539 U_CHAR
*obufp_before_macroname
;
1540 int op_lineno_before_macroname
;
1541 register int i
= ident_length
;
1542 register U_CHAR
*p
= hp
->name
;
1543 register U_CHAR
*q
= obp
- i
;
1548 do { /* All this to avoid a strncmp () */
1553 /* We found a use of a macro name.
1554 see if the context shows it is a macro call. */
1556 /* Back up over terminating character if not already done. */
1562 obufp_before_macroname
= obp
- ident_length
;
1563 op_lineno_before_macroname
= op
->lineno
;
1565 /* If macro wants an arglist, verify that a '(' follows.
1566 first skip all whitespace, copying it to the output
1567 after the macro name. Then, if there is no '(',
1568 decide this is not a macro call and leave things that way. */
1569 if (hp
->type
== T_MACRO
&& hp
->value
.defn
->nargs
>= 0)
1572 /* Scan forward over whitespace, copying it to the output. */
1573 if (ibp
== limit
&& ip
->macro
!= 0) {
1577 /* A comment: copy it unchanged or discard it. */
1578 else if (*ibp
== '/' && ibp
+1 != limit
&& ibp
[1] == '*') {
1579 if (put_out_comments
) {
1584 while (ibp
+ 1 != limit
1585 && !(ibp
[0] == '*' && ibp
[1] == '/')) {
1586 /* We need not worry about newline-marks,
1587 since they are never found in comments. */
1589 /* Newline in a file. Count it. */
1593 if (put_out_comments
)
1599 if (put_out_comments
) {
1604 else if (is_space
[*ibp
]) {
1606 if (ibp
[-1] == '\n') {
1607 if (ip
->macro
== 0) {
1608 /* Newline in a file. Count it. */
1611 } else if (!output_marks
) {
1612 /* A newline mark, and we don't want marks
1613 in the output. If it is newline-hyphen,
1614 discard it entirely. Otherwise, it is
1615 newline-whitechar, so keep the whitechar. */
1625 /* A newline mark; copy both chars to the output. */
1636 /* This is now known to be a macro call.
1637 Discard the macro name from the output,
1638 along with any following whitespace just copied. */
1639 obp
= obufp_before_macroname
;
1640 op
->lineno
= op_lineno_before_macroname
;
1642 /* Expand the macro, reading arguments as needed,
1643 and push the expansion on the input stack. */
1646 macroexpand (hp
, op
);
1648 /* Reexamine input stack, since macroexpand has pushed
1649 a new level on it. */
1656 } /* End hash-table-search loop */
1657 ident_length
= hash
= 0; /* Stop collecting identifier */
1660 } /* End if (ident_length > 0) */
1662 } /* End per-char loop */
1664 /* Come here to return -- but first give an error message
1665 if there was an unterminated successful conditional. */
1667 if (if_stack
!= ip
->if_stack
) {
1669 switch (if_stack
->type
) {
1688 error_with_line (line_for_error (if_stack
->lineno
),
1689 "unterminated #%s conditional", str
);
1691 if_stack
= ip
->if_stack
;
1695 * Rescan a string into a temporary buffer and return the result
1696 * as a FILE_BUF. Note this function returns a struct, not a pointer.
1698 * OUTPUT_MARKS nonzero means keep Newline markers found in the input
1699 * and insert such markers when appropriate. See `rescan' for details.
1700 * OUTPUT_MARKS is 1 for macroexpanding a macro argument separately
1701 * before substitution; it is 0 for other uses.
1704 expand_to_temp_buffer (buf
, limit
, output_marks
)
1705 const U_CHAR
*buf
, *limit
;
1708 register FILE_BUF
*ip
;
1710 int length
= limit
- buf
;
1712 int odepth
= indepth
;
1717 /* Set up the input on the input stack. */
1719 buf1
= (U_CHAR
*) alloca (length
+ 1);
1721 register const U_CHAR
*p1
= buf
;
1722 register U_CHAR
*p2
= buf1
;
1729 /* Set up to receive the output. */
1731 obuf
.length
= length
* 2 + 100; /* Usually enough. Why be stingy? */
1732 obuf
.bufp
= obuf
.buf
= (U_CHAR
*) xmalloc (obuf
.length
);
1737 CHECK_DEPTH ({return obuf
;});
1741 ip
= &instack
[indepth
];
1745 ip
->length
= length
;
1746 ip
->buf
= ip
->bufp
= buf1
;
1747 ip
->if_stack
= if_stack
;
1749 ip
->lineno
= obuf
.lineno
= 1;
1751 /* Scan the input, create the output. */
1753 rescan (&obuf
, output_marks
);
1755 /* Pop input stack to original state. */
1758 if (indepth
!= odepth
)
1761 /* Record the output. */
1762 obuf
.length
= obuf
.bufp
- obuf
.buf
;
1768 * Process a # directive. Expects IP->bufp to point to the '#', as in
1769 * `#define foo bar'. Passes to the command handler
1770 * (do_define, do_include, etc.): the addresses of the 1st and
1771 * last chars of the command (starting immediately after the #
1772 * keyword), plus op and the keyword table pointer. If the command
1773 * contains comments it is copied into a temporary buffer sans comments
1774 * and the temporary buffer is passed to the command handler instead.
1775 * Likewise for backslash-newlines.
1777 * Returns nonzero if this was a known # directive.
1778 * Otherwise, returns zero, without advancing the input pointer.
1782 handle_directive (ip
, op
)
1785 register U_CHAR
*bp
, *cp
;
1786 register struct directive
*kt
;
1787 register int ident_length
;
1790 /* Nonzero means we must copy the entire command
1791 to get rid of comments or backslash-newlines. */
1792 int copy_command
= 0;
1794 U_CHAR
*ident
, *after_ident
;
1797 /* Skip whitespace and \-newline. */
1799 if (is_hor_space
[*bp
])
1801 else if (*bp
== '/' && (newline_fix (bp
+ 1), bp
[1]) == '*') {
1803 skip_to_end_of_comment (ip
, &ip
->lineno
);
1805 } else if (*bp
== '\\' && bp
[1] == '\n') {
1806 bp
+= 2; ip
->lineno
++;
1810 /* Now find end of directive name.
1811 If we encounter a backslash-newline, exchange it with any following
1812 symbol-constituents so that we end up with a contiguous name. */
1819 if (*cp
== '\\' && cp
[1] == '\n')
1820 name_newline_fix (cp
);
1826 ident_length
= cp
- bp
;
1830 /* A line of just `#' becomes blank. */
1832 if (ident_length
== 0 && *after_ident
== '\n') {
1833 ip
->bufp
= after_ident
;
1838 * Decode the keyword and call the appropriate expansion
1839 * routine, after moving the input pointer up to the next line.
1841 for (kt
= directive_table
; kt
->length
> 0; kt
++) {
1842 if (kt
->length
== ident_length
1843 && !strncmp (kt
->name
, (const char *)ident
, ident_length
)) {
1844 register U_CHAR
*buf
;
1845 register U_CHAR
*limit
= ip
->buf
+ ip
->length
;
1846 int unterminated
= 0;
1848 /* Nonzero means do not delete comments within the directive.
1849 #define needs this to detect traditional token paste. */
1850 int keep_comments
= kt
->type
== T_DEFINE
;
1852 /* Find the end of this command (first newline not backslashed
1853 and not in a string or comment).
1854 Set COPY_COMMAND if the command must be copied
1855 (it contains a backslash-newline or a comment). */
1857 buf
= bp
= after_ident
;
1858 while (bp
< limit
) {
1859 register U_CHAR c
= *bp
++;
1873 bp
= skip_quoted_string (bp
- 1, limit
, ip
->lineno
, &ip
->lineno
, ©_command
, &unterminated
);
1875 /* Traditional preprocessing permits unterminated strings. */
1881 /* <...> is special for #include. */
1883 if (kt
->type
!= T_INCLUDE
)
1885 while (*bp
&& *bp
!= '>') bp
++;
1889 if (*bp
== '\\' && bp
[1] == '\n')
1892 U_CHAR
*obp
= bp
- 1;
1894 skip_to_end_of_comment (ip
, &ip
->lineno
);
1896 /* No need to copy the command because of a comment at the end;
1897 just don't include the comment in the directive. */
1898 if (bp
== limit
|| *bp
== '\n') {
1902 /* Don't remove the comments if this is #define. */
1903 if (! keep_comments
)
1909 --bp
; /* Point to the newline */
1917 resume_p
= ip
->bufp
;
1918 /* BP is the end of the directive.
1919 RESUME_P is the next interesting data after the directive.
1920 A comment may come between. */
1923 register U_CHAR
*xp
= buf
;
1924 /* Need to copy entire command into temp buffer before dispatching */
1926 cp
= (U_CHAR
*) alloca (bp
- buf
+ 5); /* room for cmd plus
1930 /* Copy to the new buffer, deleting comments
1931 and backslash-newlines (and whitespace surrounding the latter). */
1934 register U_CHAR c
= *xp
++;
1941 /* <...> is special for #include. */
1943 if (kt
->type
!= T_INCLUDE
)
1945 while (xp
< bp
&& c
!= '>') {
1947 if (c
== '\\' && xp
< bp
&& *xp
== '\n')
1958 if (cp
!= buf
&& is_space
[cp
[-1]]) {
1959 while (cp
!= buf
&& is_space
[cp
[-1]]) cp
--;
1961 SKIP_WHITE_SPACE (xp
);
1962 } else if (is_space
[*xp
]) {
1964 SKIP_WHITE_SPACE (xp
);
1974 register const U_CHAR
*bp1
1975 = skip_quoted_string (xp
- 1, limit
, ip
->lineno
, 0, 0, 0);
1984 skip_to_end_of_comment (ip
, 0);
1986 while (xp
!= ip
->bufp
)
1988 /* Delete the slash. */
1996 /* Null-terminate the copy. */
2003 ip
->bufp
= resume_p
;
2005 /* Call the appropriate command handler. buf now points to
2006 either the appropriate place in the input buffer, or to
2007 the temp buffer if it was necessary to make one. cp
2008 points to the first char after the contents of the (possibly
2009 copied) command, in either case. */
2010 (*kt
->func
) (buf
, cp
, op
, kt
);
2011 check_expand (op
, ip
->length
- (ip
->bufp
- ip
->buf
));
2020 static const char *const
2021 monthnames
[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
2022 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
2025 * expand things like __FILE__. Place the expansion into the output
2026 * buffer *without* rescanning.
2029 special_symbol (hp
, op
)
2037 FILE_BUF
*ip
= NULL
;
2038 static struct tm
*timebuf
= NULL
;
2040 int paren
= 0; /* For special `defined' keyword */
2042 for (i
= indepth
; i
>= 0; i
--)
2043 if (instack
[i
].fname
!= NULL
) {
2048 fatal ("not in any file?!");
2055 if (hp
->type
== T_FILE
)
2058 string
= instack
[0].fname
;
2062 char *tmp
= (char *) alloca (3 + strlen (string
));
2063 sprintf (tmp
, "\"%s\"", string
);
2072 case T_INCLUDE_LEVEL
:
2074 char *tmp
= (char *) alloca (8); /* Eigth bytes ought to be more than enough */
2076 for (i
= indepth
; i
>= 0; i
--)
2077 if (instack
[i
].fname
!= NULL
)
2080 sprintf (tmp
, "%d", true_indepth
- 1);
2087 char *tmp
= (char *) alloca (3 + strlen (version_string
));
2088 sprintf (tmp
, "\"%s\"", version_string
);
2094 buf
= hp
->value
.cpval
;
2099 char *tmp
= (char *) alloca (10);
2100 sprintf (tmp
, "%d", ip
->lineno
);
2108 char *tmp
= (char *) alloca (20);
2110 if (timebuf
== NULL
) {
2112 timebuf
= localtime (&t
);
2114 if (hp
->type
== T_DATE
)
2115 sprintf (tmp
, "\"%s %2d %4d\"", monthnames
[timebuf
->tm_mon
],
2116 timebuf
->tm_mday
, timebuf
->tm_year
+ 1900);
2118 sprintf (tmp
, "\"%02d:%02d:%02d\"", timebuf
->tm_hour
, timebuf
->tm_min
,
2124 case T_SPEC_DEFINED
:
2125 buf
= " 0 "; /* Assume symbol is not defined */
2126 ip
= &instack
[indepth
];
2127 SKIP_WHITE_SPACE (ip
->bufp
);
2128 if (*ip
->bufp
== '(') {
2130 ip
->bufp
++; /* Skip over the paren */
2131 SKIP_WHITE_SPACE (ip
->bufp
);
2134 if (!is_idstart
[*ip
->bufp
])
2136 if (lookup (ip
->bufp
, -1, -1))
2138 while (is_idchar
[*ip
->bufp
])
2140 SKIP_WHITE_SPACE (ip
->bufp
);
2142 if (*ip
->bufp
!= ')')
2150 error ("`defined' must be followed by ident or (ident)");
2154 error ("cccp error: invalid special hash type"); /* time for gdb */
2158 check_expand (op
, len
);
2159 memcpy (op
->bufp
, buf
, len
);
2164 /* Routines to handle #directives */
2167 * Process include file by reading it in and calling rescan.
2168 * Expects to see "fname" or <fname> on the input.
2171 do_include (buf
, limit
, op
, keyword
)
2172 U_CHAR
*buf
, *limit
;
2174 struct directive
*keyword ATTRIBUTE_UNUSED
;
2176 char *fname
; /* Dynamically allocated fname buffer */
2177 U_CHAR
*fbeg
, *fend
; /* Beginning and end of fname */
2179 struct file_name_list
*stackp
= include
; /* Chain of dirs to search */
2180 struct file_name_list dsp
[1]; /* First in chain, if #include "..." */
2183 int f
; /* file number */
2185 int retried
= 0; /* Have already tried macro
2186 expanding the include line*/
2187 FILE_BUF trybuf
; /* It got expanded into here */
2188 int system_header_p
= 0; /* 0 for "...", 1 for <...> */
2190 f
= -1; /* JF we iz paranoid! */
2195 SKIP_WHITE_SPACE (fbeg
);
2196 /* Discard trailing whitespace so we can easily see
2197 if we have parsed all the significant chars we were given. */
2198 while (limit
!= fbeg
&& is_hor_space
[limit
[-1]]) limit
--;
2203 while (fend
!= limit
&& *fend
!= '\"')
2205 if (*fend
== '\"' && fend
+ 1 == limit
) {
2208 /* We have "filename". Figure out directory this source
2209 file is coming from and put it on the front of the list. */
2211 /* If -I- was specified, don't search current dir, only spec'd ones. */
2212 if (ignore_srcdir
) break;
2214 for (fp
= &instack
[indepth
]; fp
>= instack
; fp
--)
2217 const char *ep
, *nam
;
2219 if ((nam
= fp
->fname
) != NULL
) {
2220 /* Found a named file. Figure out dir of the file,
2221 and put it in front of the search list. */
2222 dsp
[0].next
= stackp
;
2224 ep
= strrchr (nam
, '/');
2228 f
= (char *) alloca (n
+ 1);
2229 strncpy (f
, nam
, n
);
2232 if (n
> max_include_len
) max_include_len
= n
;
2234 dsp
[0].fname
= 0; /* Current directory */
2245 while (fend
!= limit
&& *fend
!= '>') fend
++;
2246 if (*fend
== '>' && fend
+ 1 == limit
) {
2247 system_header_p
= 1;
2248 /* If -I-, start with the first -I dir after the -I-. */
2249 if (first_bracket_include
)
2250 stackp
= first_bracket_include
;
2258 error ("#include expects \"fname\" or <fname>");
2261 trybuf
= expand_to_temp_buffer (buf
, limit
, 0);
2262 buf
= (U_CHAR
*) alloca (trybuf
.bufp
- trybuf
.buf
+ 1);
2263 memcpy (buf
, trybuf
.buf
, trybuf
.bufp
- trybuf
.buf
);
2264 limit
= buf
+ (trybuf
.bufp
- trybuf
.buf
);
2272 fname
= (char *) alloca (max_include_len
+ flen
+ 2);
2273 /* + 2 above for slash and terminating null. */
2275 /* If specified file name is absolute, just open it. */
2278 strncpy (fname
, (const char *)fbeg
, flen
);
2280 f
= open (fname
, O_RDONLY
, 0666);
2282 /* Search directory path, trying to open the file.
2283 Copy each filename tried into FNAME. */
2285 for (; stackp
; stackp
= stackp
->next
) {
2286 if (stackp
->fname
) {
2287 strcpy (fname
, stackp
->fname
);
2288 strcat (fname
, "/");
2289 fname
[strlen (fname
) + flen
] = 0;
2293 strncat (fname
, (const char *)fbeg
, flen
);
2294 if ((f
= open (fname
, O_RDONLY
, 0666)) >= 0)
2300 strncpy (fname
, (const char *)fbeg
, flen
);
2302 error_from_errno (fname
);
2304 /* For -M, add this file to the dependencies. */
2305 if (print_deps
> (system_header_p
|| (system_include_depth
> 0))) {
2306 if (system_header_p
)
2307 warning ("nonexistent file <%.*s> omitted from dependency output",
2311 deps_output ((const char *)fbeg
, fend
- fbeg
);
2312 deps_output (" ", 0);
2317 /* Check to see if this include file is a once-only include file.
2320 struct file_name_list
* ptr
;
2322 for (ptr
= dont_repeat_files
; ptr
; ptr
= ptr
->next
) {
2323 if (!strcmp (ptr
->fname
, fname
)) {
2325 return; /* This file was once'd. */
2329 for (ptr
= all_include_files
; ptr
; ptr
= ptr
->next
) {
2330 if (!strcmp (ptr
->fname
, fname
))
2331 break; /* This file was included before. */
2335 /* This is the first time for this file. */
2336 /* Add it to list of files included. */
2338 ptr
= (struct file_name_list
*) xmalloc (sizeof (struct file_name_list
));
2339 ptr
->next
= all_include_files
;
2340 all_include_files
= ptr
;
2341 ptr
->fname
= xstrdup (fname
);
2343 /* For -M, add this file to the dependencies. */
2344 if (print_deps
> (system_header_p
|| (system_include_depth
> 0))) {
2345 deps_output (fname
, strlen (fname
));
2346 deps_output (" ", 0);
2350 if (system_header_p
)
2351 system_include_depth
++;
2353 /* Actually process the file. */
2354 finclude (f
, fname
, op
);
2356 if (system_header_p
)
2357 system_include_depth
--;
2363 /* Process the contents of include file FNAME, already open on descriptor F,
2364 with output to OP. */
2367 finclude (f
, fname
, op
)
2375 FILE_BUF
*fp
; /* For input stack frame */
2377 CHECK_DEPTH (return;);
2379 if (file_size_and_mode (f
, &st_mode
, &st_size
))
2382 fp
= &instack
[indepth
+ 1];
2383 memset (fp
, 0, sizeof (FILE_BUF
));
2387 fp
->if_stack
= if_stack
;
2389 if (S_ISREG (st_mode
)) {
2390 fp
->buf
= (U_CHAR
*) xmalloc (st_size
+ 2);
2393 /* Read the file contents, knowing that st_size is an upper bound
2394 on the number of bytes we can read. */
2395 while (st_size
> 0) {
2396 i
= read (f
, fp
->buf
+ fp
->length
, st_size
);
2406 /* Cannot count its file size before reading. */
2413 basep
= (U_CHAR
*) xmalloc (bsize
+ 2);
2417 i
= read (f
, bufp
, bsize
- st_size
);
2419 goto nope
; /* error! */
2421 break; /* End of file */
2424 if (bsize
== st_size
) { /* Buffer is full! */
2426 basep
= (U_CHAR
*) xrealloc (basep
, bsize
+ 2);
2427 bufp
= basep
+ st_size
; /* May have moved */
2432 fp
->length
= st_size
;
2436 /* Make sure data ends with a newline. And put a null after it. */
2438 if (fp
->length
> 0 && fp
->buf
[fp
->length
-1] != '\n')
2439 fp
->buf
[fp
->length
++] = '\n';
2440 fp
->buf
[fp
->length
] = '\0';
2443 output_line_command (fp
, op
, 0, enter_file
);
2446 output_line_command (&instack
[indepth
], op
, 0, leave_file
);
2451 perror_with_name (fname
);
2456 /* Process a #define command.
2457 BUF points to the contents of the #define command, as a continguous string.
2458 LIMIT points to the first character past the end of the definition.
2459 KEYWORD is the keyword-table entry for #define. */
2462 do_define (buf
, limit
, op
, keyword
)
2463 U_CHAR
*buf
, *limit
;
2464 FILE_BUF
*op ATTRIBUTE_UNUSED
;
2465 struct directive
*keyword ATTRIBUTE_UNUSED
;
2467 U_CHAR
*bp
; /* temp ptr into input buffer */
2468 U_CHAR
*symname
; /* remember where symbol name starts */
2469 int sym_length
; /* and how long it is */
2472 int arglengths
= 0; /* Accumulate lengths of arg names
2473 plus number of args. */
2478 while (is_hor_space
[*bp
])
2481 symname
= bp
; /* remember where it starts */
2482 while (is_idchar
[*bp
] && bp
< limit
) {
2485 sym_length
= bp
- symname
;
2486 if (sym_length
== 0)
2487 error ("invalid macro name");
2488 else if (!is_idstart
[*symname
]) {
2489 U_CHAR
*msg
; /* what pain... */
2490 msg
= (U_CHAR
*) alloca (sym_length
+ 1);
2491 memcpy (msg
, symname
, sym_length
);
2492 msg
[sym_length
] = 0;
2493 error ("invalid macro name `%s'", msg
);
2495 if (! strncmp ((const char *)symname
, "defined", 7) && sym_length
== 7)
2496 error ("defining `defined' as a macro");
2499 /* lossage will occur if identifiers or control keywords are broken
2500 across lines using backslash. This is not the right place to take
2504 struct arglist
*arg_ptrs
= NULL
;
2507 bp
++; /* skip '(' */
2508 SKIP_WHITE_SPACE (bp
);
2510 /* Loop over macro argument names. */
2511 while (*bp
!= ')') {
2512 struct arglist
*temp
;
2514 temp
= (struct arglist
*) alloca (sizeof (struct arglist
));
2516 temp
->next
= arg_ptrs
;
2517 temp
->argno
= argno
++;
2520 if (!is_idstart
[*bp
])
2521 warning ("parameter name starts with a digit in #define");
2523 /* Find the end of the arg name. */
2524 while (is_idchar
[*bp
]) {
2527 temp
->length
= bp
- temp
->name
;
2528 arglengths
+= temp
->length
+ 2;
2529 SKIP_WHITE_SPACE (bp
);
2530 if (temp
->length
== 0 || (*bp
!= ',' && *bp
!= ')')) {
2531 error ("badly punctuated parameter list in #define");
2536 SKIP_WHITE_SPACE (bp
);
2539 error ("unterminated parameter list in #define");
2544 ++bp
; /* skip paren */
2545 while (is_hor_space
[*bp
]) /* and leading whitespace */
2547 /* now everything from bp before limit is the definition. */
2548 defn
= collect_expansion (bp
, limit
, argno
, arg_ptrs
);
2550 /* Now set defn->argnames to the result of concatenating
2551 the argument names in reverse order
2552 with comma-space between them. */
2554 struct arglist
*temp
;
2556 U_CHAR
*tmp
= (U_CHAR
*) xmalloc (arglengths
+ 1);
2558 for (temp
= arg_ptrs
; temp
; temp
= temp
->next
) {
2559 memcpy (&tmp
[i
], temp
->name
, temp
->length
);
2561 if (temp
->next
!= 0) {
2567 defn
->argnames
= tmp
;
2571 /* simple expansion or empty definition; skip leading whitespace */
2572 while (is_hor_space
[*bp
])
2574 /* now everything from bp before limit is the definition. */
2575 defn
= collect_expansion (bp
, limit
, -1, 0);
2576 defn
->argnames
= (const U_CHAR
*) "";
2579 hashcode
= hashf (symname
, sym_length
, HASHSIZE
);
2583 if ((hp
= lookup (symname
, sym_length
, hashcode
)) == NULL
)
2584 hp
= install (symname
, sym_length
, T_MACRO
, hashcode
);
2586 if (hp
->type
!= T_MACRO
|| compare_defs (defn
, hp
->value
.defn
))
2587 warning ("\"%.*s\" redefined", sym_length
, symname
);
2589 /* Replace the old definition. */
2593 hp
->value
.defn
= defn
;
2598 * return zero if two DEFINITIONs are isomorphic
2601 compare_defs (d1
, d2
)
2602 DEFINITION
*d1
, *d2
;
2604 register struct reflist
*a1
, *a2
;
2605 register U_CHAR
*p1
= d1
->expansion
;
2606 register U_CHAR
*p2
= d2
->expansion
;
2609 if (d1
->nargs
!= d2
->nargs
)
2611 if (strcmp ((const char *)d1
->argnames
, (const char *)d2
->argnames
))
2613 for (a1
= d1
->pattern
, a2
= d2
->pattern
; a1
&& a2
;
2614 a1
= a1
->next
, a2
= a2
->next
) {
2615 if (!((a1
->nchars
== a2
->nchars
2616 && ! strncmp ((const char *)p1
, (const char *)p2
, a1
->nchars
))
2617 || ! comp_def_part (first
, p1
, a1
->nchars
, p2
, a2
->nchars
, 0))
2618 || a1
->argno
!= a2
->argno
2619 || a1
->stringify
!= a2
->stringify
2620 || a1
->raw_before
!= a2
->raw_before
2621 || a1
->raw_after
!= a2
->raw_after
)
2629 if (comp_def_part (first
, p1
, d1
->length
- (p1
- d1
->expansion
),
2630 p2
, d2
->length
- (p2
- d2
->expansion
), 1))
2635 /* Return 1 if two parts of two macro definitions are effectively different.
2636 One of the parts starts at BEG1 and has LEN1 chars;
2637 the other has LEN2 chars at BEG2.
2638 Any sequence of whitespace matches any other sequence of whitespace.
2639 FIRST means these parts are the first of a macro definition;
2640 so ignore leading whitespace entirely.
2641 LAST means these parts are the last of a macro definition;
2642 so ignore trailing whitespace entirely. */
2644 comp_def_part (first
, beg1
, len1
, beg2
, len2
, last
)
2646 const U_CHAR
*beg1
, *beg2
;
2650 register const U_CHAR
*end1
= beg1
+ len1
;
2651 register const U_CHAR
*end2
= beg2
+ len2
;
2653 while (beg1
!= end1
&& is_space
[*beg1
]) beg1
++;
2654 while (beg2
!= end2
&& is_space
[*beg2
]) beg2
++;
2657 while (beg1
!= end1
&& is_space
[end1
[-1]]) end1
--;
2658 while (beg2
!= end2
&& is_space
[end2
[-1]]) end2
--;
2660 while (beg1
!= end1
&& beg2
!= end2
) {
2661 if (is_space
[*beg1
] && is_space
[*beg2
]) {
2662 while (beg1
!= end1
&& is_space
[*beg1
]) beg1
++;
2663 while (beg2
!= end2
&& is_space
[*beg2
]) beg2
++;
2664 } else if (*beg1
== *beg2
) {
2668 return (beg1
!= end1
) || (beg2
!= end2
);
2671 /* Read a replacement list for a macro with parameters.
2672 Build the DEFINITION structure.
2673 Reads characters of text starting at BUF until LIMIT.
2674 ARGLIST specifies the formal parameters to look for
2675 in the text of the definition; NARGS is the number of args
2676 in that list, or -1 for a macro name that wants no argument list.
2677 MACRONAME is the macro name itself (so we can avoid recursive expansion)
2678 and NAMELEN is its length in characters.
2680 Note that comments and backslash-newlines have already been deleted
2681 from the argument. */
2683 /* Leading and trailing Space, Tab, etc. are converted to markers
2684 Newline Space, Newline Tab, etc.
2685 Newline Space makes a space in the final output
2686 but is discarded if stringified. (Newline Tab is similar but
2687 makes a Tab instead.)
2689 If there is no trailing whitespace, a Newline Space is added at the end
2690 to prevent concatenation that would be contrary to the standard. */
2693 collect_expansion (buf
, end
, nargs
, arglist
)
2696 struct arglist
*arglist
;
2699 register U_CHAR
*p
, *limit
, *lastp
, *exp_p
;
2700 struct reflist
*endpat
= NULL
;
2701 /* Pointer to first nonspace after last ## seen. */
2703 /* Pointer to first nonspace after last single-# seen. */
2704 U_CHAR
*stringify
= 0;
2706 int expected_delimiter
= '\0';
2708 /* Scan thru the replacement list, ignoring comments and quoted
2709 strings, picking up on the macro calls. It does a linear search
2710 thru the arg list on every potential symbol. Profiling might say
2711 that something smarter should happen. */
2716 /* Find the beginning of the trailing whitespace. */
2717 /* Find end of leading whitespace. */
2720 while (p
< limit
&& is_space
[limit
[-1]]) limit
--;
2721 while (p
< limit
&& is_space
[*p
]) p
++;
2723 /* Allocate space for the text in the macro definition.
2724 Leading and trailing whitespace chars need 2 bytes each.
2725 Each other input char may or may not need 1 byte,
2726 so this is an upper bound.
2727 The extra 2 are for invented trailing newline-marker and final null. */
2728 maxsize
= (sizeof (DEFINITION
)
2729 + 2 * (end
- limit
) + 2 * (p
- buf
)
2731 defn
= (DEFINITION
*) xcalloc (1, maxsize
);
2733 defn
->nargs
= nargs
;
2734 exp_p
= defn
->expansion
= (U_CHAR
*) defn
+ sizeof (DEFINITION
);
2739 /* Convert leading whitespace to Newline-markers. */
2740 while (p
< limit
&& is_space
[*p
]) {
2745 /* Process the main body of the definition. */
2747 int skipped_arg
= 0;
2748 register U_CHAR c
= *p
++;
2752 /* In -traditional mode, recognize arguments inside strings and
2753 and character constants, and ignore special properties of #.
2754 Arguments inside strings are considered "stringified", but no
2755 extra quote marks are supplied. */
2759 if (expected_delimiter
!= '\0') {
2760 if (c
== expected_delimiter
)
2761 expected_delimiter
= '\0';
2763 expected_delimiter
= c
;
2767 /* Backslash quotes delimiters and itself, but not macro args. */
2768 if (expected_delimiter
!= 0 && p
< limit
2769 && (*p
== expected_delimiter
|| *p
== '\\')) {
2776 if (expected_delimiter
!= '\0') /* No comments inside strings. */
2779 /* If we find a comment that wasn't removed by handle_directive,
2780 this must be -traditional. So replace the comment with
2784 while (p
< limit
&& !(p
[-2] == '*' && p
[-1] == '/'))
2790 if (is_idchar
[c
] && nargs
> 0) {
2791 U_CHAR
*id_beg
= p
- 1;
2795 while (p
!= limit
&& is_idchar
[*p
]) p
++;
2796 id_len
= p
- id_beg
;
2798 if (is_idstart
[c
]) {
2799 register struct arglist
*arg
;
2801 for (arg
= arglist
; arg
!= NULL
; arg
= arg
->next
) {
2802 struct reflist
*tpat
;
2804 if (arg
->name
[0] == c
2805 && arg
->length
== id_len
2806 && strncmp ((const char *)arg
->name
,
2807 (const char *)id_beg
, id_len
) == 0) {
2808 /* make a pat node for this arg and append it to the end of
2810 tpat
= (struct reflist
*) xmalloc (sizeof (struct reflist
));
2812 tpat
->raw_before
= concat
== id_beg
;
2813 tpat
->raw_after
= 0;
2814 tpat
->stringify
= expected_delimiter
!= '\0';
2817 defn
->pattern
= tpat
;
2819 endpat
->next
= tpat
;
2822 tpat
->argno
= arg
->argno
;
2823 tpat
->nchars
= exp_p
- lastp
;
2825 register U_CHAR
*p1
= p
;
2826 SKIP_WHITE_SPACE (p1
);
2827 if (p1
+ 2 <= limit
&& p1
[0] == '#' && p1
[1] == '#')
2828 tpat
->raw_after
= 1;
2830 lastp
= exp_p
; /* place to start copying from next time */
2837 /* If this was not a macro arg, copy it into the expansion. */
2838 if (! skipped_arg
) {
2839 register U_CHAR
*lim1
= p
;
2843 if (stringify
== id_beg
)
2844 error ("# operator should be followed by a macro argument name");
2850 /* Convert trailing whitespace to Newline-markers. */
2851 while (limit
< end
&& is_space
[*limit
]) {
2853 *exp_p
++ = *limit
++;
2858 defn
->length
= exp_p
- defn
->expansion
;
2860 /* Crash now if we overrun the allocated size. */
2861 if (defn
->length
+ 1 > maxsize
)
2868 * interpret #line command. Remembers previously seen fnames
2869 * in its very own hash table.
2871 #define FNAME_HASHSIZE 37
2873 do_line (buf
, limit
, op
, keyword
)
2874 U_CHAR
*buf
, *limit
;
2876 struct directive
*keyword ATTRIBUTE_UNUSED
;
2878 register U_CHAR
*bp
;
2879 FILE_BUF
*ip
= &instack
[indepth
];
2882 enum file_change_code file_change
= same_file
;
2884 /* Expand any macros. */
2885 tem
= expand_to_temp_buffer (buf
, limit
, 0);
2887 /* Point to macroexpanded line, which is null-terminated now. */
2889 SKIP_WHITE_SPACE (bp
);
2891 if (!ISDIGIT (*bp
)) {
2892 error ("invalid format #line command");
2896 /* The Newline at the end of this line remains to be processed.
2897 To put the next line at the specified line number,
2898 we must store a line number now that is one less. */
2899 new_lineno
= atoi ((const char *)bp
) - 1;
2901 /* skip over the line number. */
2902 while (ISDIGIT (*bp
))
2905 #if 0 /* #line 10"foo.c" is supposed to be allowed. */
2906 if (*bp
&& !is_space
[*bp
]) {
2907 error ("invalid format #line command");
2912 SKIP_WHITE_SPACE (bp
);
2915 static HASHNODE
*fname_table
[FNAME_HASHSIZE
];
2916 HASHNODE
*hp
, **hash_bucket
;
2922 while (*bp
&& *bp
!= '\"')
2925 error ("invalid format #line command");
2929 fname_length
= bp
- fname
;
2932 SKIP_WHITE_SPACE (bp
);
2935 file_change
= enter_file
;
2936 else if (*bp
== '2')
2937 file_change
= leave_file
;
2939 error ("invalid format #line command");
2944 SKIP_WHITE_SPACE (bp
);
2946 error ("invalid format #line command");
2952 &fname_table
[hashf (fname
, fname_length
, FNAME_HASHSIZE
)];
2953 for (hp
= *hash_bucket
; hp
!= NULL
; hp
= hp
->next
)
2954 if (hp
->length
== fname_length
&&
2955 strncmp (hp
->value
.cpval
, (const char *)fname
, fname_length
) == 0) {
2956 ip
->fname
= hp
->value
.cpval
;
2961 /* Didn't find it; cons up a new one. */
2962 hp
= (HASHNODE
*) xcalloc (1, sizeof (HASHNODE
) + fname_length
+ 1);
2963 hp
->next
= *hash_bucket
;
2966 hp
->length
= fname_length
;
2967 ip
->fname
= hp
->value
.cpval
= q
= ((char *) hp
) + sizeof (HASHNODE
);
2968 memcpy (q
, fname
, fname_length
);
2971 error ("invalid format #line command");
2975 ip
->lineno
= new_lineno
;
2976 output_line_command (ip
, op
, 0, file_change
);
2977 check_expand (op
, ip
->length
- (ip
->bufp
- ip
->buf
));
2981 * remove all definitions of symbol from symbol table.
2982 * according to un*x /lib/cpp, it is not an error to undef
2983 * something that has no definitions, so it isn't one here either.
2986 do_undef (buf
, limit
, op
, keyword
)
2988 U_CHAR
*limit ATTRIBUTE_UNUSED
;
2989 FILE_BUF
*op ATTRIBUTE_UNUSED
;
2990 struct directive
*keyword ATTRIBUTE_UNUSED
;
2994 SKIP_WHITE_SPACE (buf
);
2996 if (! strncmp ((const char *)buf
, "defined", 7) && ! is_idchar
[buf
[7]])
2997 warning ("undefining `defined'");
2999 while ((hp
= lookup (buf
, -1, -1)) != NULL
) {
3000 if (hp
->type
!= T_MACRO
)
3001 warning ("undefining `%s'", hp
->name
);
3007 * handle #if command by
3008 * 1) inserting special `defined' keyword into the hash table
3009 * that gets turned into 0 or 1 by special_symbol (thus,
3010 * if the luser has a symbol called `defined' already, it won't
3011 * work inside the #if command)
3012 * 2) rescan the input into a temporary output buffer
3013 * 3) pass the output buffer to the yacc parser and collect a value
3014 * 4) clean up the mess left from steps 1 and 2.
3015 * 5) call conditional_skip to skip til the next #endif (etc.),
3016 * or not, depending on the value from step 3.
3019 do_if (buf
, limit
, op
, keyword
)
3020 U_CHAR
*buf
, *limit
;
3021 FILE_BUF
*op ATTRIBUTE_UNUSED
;
3022 struct directive
*keyword ATTRIBUTE_UNUSED
;
3025 FILE_BUF
*ip
= &instack
[indepth
];
3027 value
= eval_if_expression (buf
, limit
- buf
);
3028 conditional_skip (ip
, value
== 0, T_IF
);
3032 * handle a #elif directive by not changing if_stack either.
3033 * see the comment above do_else.
3036 do_elif (buf
, limit
, op
, keyword
)
3037 U_CHAR
*buf
, *limit
;
3039 struct directive
*keyword ATTRIBUTE_UNUSED
;
3042 FILE_BUF
*ip
= &instack
[indepth
];
3044 if (if_stack
== instack
[indepth
].if_stack
) {
3045 error ("#elif not within a conditional");
3048 if (if_stack
->type
!= T_IF
&& if_stack
->type
!= T_ELIF
) {
3049 error ("#elif after #else");
3050 fprintf (stderr
, " (matches line %d", if_stack
->lineno
);
3051 if (if_stack
->fname
!= NULL
&& ip
->fname
!= NULL
&&
3052 strcmp (if_stack
->fname
, ip
->fname
) != 0)
3053 fprintf (stderr
, ", file %s", if_stack
->fname
);
3054 fprintf (stderr
, ")\n");
3056 if_stack
->type
= T_ELIF
;
3059 if (if_stack
->if_succeeded
)
3060 skip_if_group (ip
, 0);
3062 value
= eval_if_expression (buf
, limit
- buf
);
3064 skip_if_group (ip
, 0);
3066 ++if_stack
->if_succeeded
; /* continue processing input */
3067 output_line_command (ip
, op
, 1, same_file
);
3073 * evaluate a #if expression in BUF, of length LENGTH,
3074 * then parse the result as a C expression and return the value as an int.
3077 eval_if_expression (buf
, length
)
3082 HASHNODE
*save_defined
;
3085 save_defined
= install (U
"defined", -1, T_SPEC_DEFINED
, -1);
3086 temp_obuf
= expand_to_temp_buffer (buf
, buf
+ length
, 0);
3087 delete_macro (save_defined
); /* clean up special symbol */
3089 value
= parse_c_expression ((const char *)temp_obuf
.buf
);
3091 free (temp_obuf
.buf
);
3097 * routine to handle ifdef/ifndef. Try to look up the symbol,
3098 * then do or don't skip to the #endif/#else/#elif depending
3099 * on what directive is actually being processed.
3102 do_xifdef (buf
, limit
, op
, keyword
)
3103 U_CHAR
*buf
, *limit
;
3104 FILE_BUF
*op ATTRIBUTE_UNUSED
;
3105 struct directive
*keyword
;
3108 FILE_BUF
*ip
= &instack
[indepth
];
3111 /* Discard leading and trailing whitespace. */
3112 SKIP_WHITE_SPACE (buf
);
3113 while (limit
!= buf
&& is_hor_space
[limit
[-1]]) limit
--;
3115 /* Find the end of the identifier at the beginning. */
3116 for (end
= buf
; is_idchar
[*end
]; end
++);
3119 skip
= (keyword
->type
== T_IFDEF
);
3121 skip
= (lookup (buf
, end
-buf
, -1) == NULL
) ^ (keyword
->type
== T_IFNDEF
);
3124 conditional_skip (ip
, skip
, T_IF
);
3128 * push TYPE on stack; then, if SKIP is nonzero, skip ahead.
3131 conditional_skip (ip
, skip
, type
)
3134 enum node_type type
;
3136 IF_STACK_FRAME
*temp
;
3138 temp
= (IF_STACK_FRAME
*) xcalloc (1, sizeof (IF_STACK_FRAME
));
3139 temp
->fname
= ip
->fname
;
3140 temp
->lineno
= ip
->lineno
;
3141 temp
->next
= if_stack
;
3144 if_stack
->type
= type
;
3147 skip_if_group (ip
, 0);
3150 ++if_stack
->if_succeeded
;
3151 output_line_command (ip
, &outbuf
, 1, same_file
);
3156 * skip to #endif, #else, or #elif. adjust line numbers, etc.
3157 * leaves input ptr at the sharp sign found.
3158 * If ANY is nonzero, return at next directive of any sort.
3161 skip_if_group (ip
, any
)
3165 register U_CHAR
*bp
= ip
->bufp
, *cp
;
3166 register U_CHAR
*endb
= ip
->buf
+ ip
->length
;
3167 struct directive
*kt
;
3168 IF_STACK_FRAME
*save_if_stack
= if_stack
; /* don't pop past here */
3169 U_CHAR
*beg_of_line
= bp
;
3173 case '/': /* possible comment */
3174 if (*bp
== '\\' && bp
[1] == '\n')
3178 bp
= skip_to_end_of_comment (ip
, &ip
->lineno
);
3183 bp
= skip_quoted_string (bp
- 1, endb
, ip
->lineno
, &ip
->lineno
, 0, 0);
3186 /* Char after backslash loses its special meaning. */
3189 ++ip
->lineno
; /* But do update the line-count. */
3200 /* # keyword: a # must be first nonblank char on the line */
3201 if (beg_of_line
== 0)
3203 /* Scan from start of line, skipping whitespace, comments
3204 and backslash-newlines, and see if we reach this #.
3205 If not, this # is not special. */
3208 if (is_hor_space
[*bp
])
3210 else if (*bp
== '\\' && bp
[1] == '\n')
3212 else if (*bp
== '/' && bp
[1] == '*') {
3214 while (!(*bp
== '*' && bp
[1] == '/')) {
3223 if (bp
!= ip
->bufp
) {
3224 bp
= ip
->bufp
+ 1; /* Reset bp to after the #. */
3228 bp
= ip
->bufp
+ 1; /* Point after '#'. */
3230 /* Skip whitespace and \-newline. */
3232 if (is_hor_space
[*bp
])
3234 else if (*bp
== '\\' && bp
[1] == '\n')
3236 else if (*bp
== '/' && bp
[1] == '*') {
3238 while (!(*bp
== '*' && bp
[1] == '/'))
3247 /* Now find end of directive name.
3248 If we encounter a backslash-newline, exchange it with any following
3249 symbol-constituents so that we end up with a contiguous name. */
3255 if (*bp
== '\\' && bp
[1] == '\n')
3256 name_newline_fix (bp
);
3263 for (kt
= directive_table
; kt
->length
>= 0; kt
++) {
3264 IF_STACK_FRAME
*temp
;
3265 if (strncmp ((const char *)cp
, kt
->name
, kt
->length
) == 0
3266 && !is_idchar
[cp
[kt
->length
]]) {
3268 /* If we are asked to return on next directive,
3277 temp
= (IF_STACK_FRAME
*) xcalloc (1, sizeof (IF_STACK_FRAME
));
3278 temp
->next
= if_stack
;
3280 temp
->lineno
= ip
->lineno
;
3281 temp
->fname
= ip
->fname
;
3282 temp
->type
= kt
->type
;
3287 if (if_stack
== instack
[indepth
].if_stack
) {
3288 error ("#%s not within a conditional", kt
->name
);
3291 else if (if_stack
== save_if_stack
)
3292 return; /* found what we came for */
3294 if (kt
->type
!= T_ENDIF
) {
3295 if (if_stack
->type
== T_ELSE
)
3296 error ("#else or #elif after #else");
3297 if_stack
->type
= kt
->type
;
3302 if_stack
= if_stack
->next
;
3307 /* Anything else is ignored. */
3316 /* after this returns, rescan will exit because ip->bufp
3317 now points to the end of the buffer.
3318 rescan is responsible for the error message also. */
3322 * handle a #else directive. Do this by just continuing processing
3323 * without changing if_stack ; this is so that the error message
3324 * for missing #endif's etc. will point to the original #if. It
3325 * is possible that something different would be better.
3328 do_else (buf
, limit
, op
, keyword
)
3329 U_CHAR
*buf ATTRIBUTE_UNUSED
;
3330 U_CHAR
*limit ATTRIBUTE_UNUSED
;
3332 struct directive
*keyword ATTRIBUTE_UNUSED
;
3334 FILE_BUF
*ip
= &instack
[indepth
];
3336 if (if_stack
== instack
[indepth
].if_stack
) {
3337 error ("#else not within a conditional");
3340 if (if_stack
->type
!= T_IF
&& if_stack
->type
!= T_ELIF
) {
3341 error ("#else after #else");
3342 fprintf (stderr
, " (matches line %d", if_stack
->lineno
);
3343 if (strcmp (if_stack
->fname
, ip
->fname
) != 0)
3344 fprintf (stderr
, ", file %s", if_stack
->fname
);
3345 fprintf (stderr
, ")\n");
3347 if_stack
->type
= T_ELSE
;
3350 if (if_stack
->if_succeeded
)
3351 skip_if_group (ip
, 0);
3353 ++if_stack
->if_succeeded
; /* continue processing input */
3354 output_line_command (ip
, op
, 1, same_file
);
3359 * unstack after #endif command
3362 do_endif (buf
, limit
, op
, keyword
)
3363 U_CHAR
*buf ATTRIBUTE_UNUSED
;
3364 U_CHAR
*limit ATTRIBUTE_UNUSED
;
3366 struct directive
*keyword ATTRIBUTE_UNUSED
;
3368 if (if_stack
== instack
[indepth
].if_stack
)
3369 error ("unbalanced #endif");
3371 IF_STACK_FRAME
*temp
= if_stack
;
3372 if_stack
= if_stack
->next
;
3374 output_line_command (&instack
[indepth
], op
, 1, same_file
);
3379 * Skip a comment, assuming the input ptr immediately follows the
3380 * initial slash-star. Bump line counter as necessary.
3381 * (The canonical line counter is &ip->lineno).
3382 * Don't use this routine (or the next one) if bumping the line
3383 * counter is not sufficient to deal with newlines in the string.
3386 skip_to_end_of_comment (ip
, line_counter
)
3387 register FILE_BUF
*ip
;
3388 int *line_counter
; /* place to remember newlines, or NULL */
3390 register U_CHAR
*limit
= ip
->buf
+ ip
->length
;
3391 register U_CHAR
*bp
= ip
->bufp
;
3392 FILE_BUF
*op
= &outbuf
; /* JF */
3393 int output
= put_out_comments
&& !line_counter
;
3395 /* JF this line_counter stuff is a crock to make sure the
3396 comment is only put out once, no matter how many times
3397 the comment is skipped. It almost works */
3402 while (bp
< limit
) {
3407 if (warn_comments
&& bp
< limit
&& *bp
== '*')
3408 warning("`/*' within comment");
3411 if (line_counter
!= NULL
)
3417 if (*bp
== '\\' && bp
[1] == '\n')
3433 * Skip over a quoted string. BP points to the opening quote.
3434 * Returns a pointer after the closing quote. Don't go past LIMIT.
3435 * START_LINE is the line number of the starting point (but it need
3436 * not be valid if the starting point is inside a macro expansion).
3438 * The input stack state is not changed.
3440 * If COUNT_NEWLINES is nonzero, it points to an int to increment
3441 * for each newline passed.
3443 * If BACKSLASH_NEWLINES_P is nonzero, store 1 thru it
3444 * if we pass a backslash-newline.
3446 * If EOFP is nonzero, set *EOFP to 1 if the string is unterminated.
3449 skip_quoted_string (bp
, limit
, start_line
, count_newlines
, backslash_newlines_p
, eofp
)
3450 register const U_CHAR
*bp
;
3451 register const U_CHAR
*limit
;
3453 int *count_newlines
;
3454 int *backslash_newlines_p
;
3457 register U_CHAR c
, match
;
3462 error_with_line (line_for_error (start_line
),
3463 "unterminated string or character constant");
3470 while (*bp
== '\\' && bp
[1] == '\n') {
3471 if (backslash_newlines_p
)
3472 *backslash_newlines_p
= 1;
3477 if (*bp
== '\n' && count_newlines
) {
3478 if (backslash_newlines_p
)
3479 *backslash_newlines_p
= 1;
3483 } else if (c
== '\n') {
3484 /* Unterminated strings and character constants are 'legal'. */
3485 bp
--; /* Don't consume the newline. */
3489 } else if (c
== match
)
3492 return (U_CHAR
*) bp
;
3496 * write out a #line command, for instance, after an #include file.
3497 * If CONDITIONAL is nonzero, we can omit the #line if it would
3498 * appear to be a no-op, and we can output a few newlines instead
3499 * if we want to increase the line number by a small amount.
3500 * FILE_CHANGE says whether we are entering a file, leaving, or neither.
3504 output_line_command (ip
, op
, conditional
, file_change
)
3507 enum file_change_code file_change
;
3510 char line_cmd_buf
[500];
3512 if (no_line_commands
3513 || ip
->fname
== NULL
3515 op
->lineno
= ip
->lineno
;
3520 if (ip
->lineno
== op
->lineno
)
3523 /* If the inherited line number is a little too small,
3524 output some newlines instead of a #line command. */
3525 if (ip
->lineno
> op
->lineno
&& ip
->lineno
< op
->lineno
+ 8) {
3526 check_expand (op
, 10);
3527 while (ip
->lineno
> op
->lineno
) {
3535 sprintf (line_cmd_buf
, "# %d \"%s\"", ip
->lineno
, ip
->fname
);
3536 if (file_change
!= same_file
)
3537 strcat (line_cmd_buf
, file_change
== enter_file
? " 1" : " 2");
3538 len
= strlen (line_cmd_buf
);
3539 line_cmd_buf
[len
++] = '\n';
3540 check_expand (op
, len
+ 1);
3541 if (op
->bufp
> op
->buf
&& op
->bufp
[-1] != '\n')
3543 memcpy (op
->bufp
, line_cmd_buf
, len
);
3545 op
->lineno
= ip
->lineno
;
3549 /* Expand a macro call.
3550 HP points to the symbol that is the macro being called.
3551 Put the result of expansion onto the input stack
3552 so that subsequent input by our caller will use it.
3554 If macro wants arguments, caller has already verified that
3555 an argument list follows; arguments come from the input stack. */
3558 macroexpand (hp
, op
)
3563 DEFINITION
*defn
= hp
->value
.defn
;
3564 register U_CHAR
*xbuf
;
3566 int start_line
= instack
[indepth
].lineno
;
3568 CHECK_DEPTH (return;);
3570 /* it might not actually be a macro. */
3571 if (hp
->type
!= T_MACRO
) {
3572 special_symbol (hp
, op
);
3576 nargs
= defn
->nargs
;
3580 struct argdata
*args
;
3581 const char *parse_error
= 0;
3583 args
= (struct argdata
*) alloca ((nargs
+ 1) * sizeof (struct argdata
));
3585 for (i
= 0; i
< nargs
; i
++) {
3586 args
[i
].raw
= args
[i
].expanded
= (U_CHAR
*) "";
3587 args
[i
].raw_length
= args
[i
].expand_length
3588 = args
[i
].stringified_length
= 0;
3589 args
[i
].free1
= args
[i
].free2
= 0;
3592 /* Parse all the macro args that are supplied. I counts them.
3593 The first NARGS args are stored in ARGS.
3594 The rest are discarded. */
3597 /* Discard the open-parenthesis or comma before the next arg. */
3598 ++instack
[indepth
].bufp
;
3600 = macarg ((i
< nargs
|| (nargs
== 0 && i
== 0)) ? &args
[i
] : 0);
3603 error_with_line (line_for_error (start_line
), parse_error
);
3607 } while (*instack
[indepth
].bufp
!= ')');
3609 /* If we got one arg but it was just whitespace, call that 0 args. */
3611 register const U_CHAR
*bp
= args
[0].raw
;
3612 register const U_CHAR
*lim
= bp
+ args
[0].raw_length
;
3613 while (bp
!= lim
&& is_space
[*bp
]) bp
++;
3618 if (nargs
== 0 && i
> 0)
3619 error ("arguments given to macro `%s'", hp
->name
);
3620 else if (i
< nargs
) {
3621 /* traditional C allows foo() if foo wants one argument. */
3622 if (nargs
== 1 && i
== 0)
3625 error ("no args to macro `%s'", hp
->name
);
3627 error ("only 1 arg to macro `%s'", hp
->name
);
3629 error ("only %d args to macro `%s'", i
, hp
->name
);
3630 } else if (i
> nargs
)
3631 error ("too many (%d) args to macro `%s'", i
, hp
->name
);
3633 /* Swallow the closeparen. */
3634 ++instack
[indepth
].bufp
;
3636 /* If macro wants zero args, we parsed the arglist for checking only.
3637 Read directly from the macro definition. */
3639 xbuf
= defn
->expansion
;
3640 xbuf_len
= defn
->length
;
3642 register U_CHAR
*exp
= defn
->expansion
;
3643 register int offset
; /* offset in expansion,
3644 copied a piece at a time */
3645 register int totlen
; /* total amount of exp buffer filled so far */
3647 register struct reflist
*ap
;
3649 /* Macro really takes args. Compute the expansion of this call. */
3651 /* Compute length in characters of the macro's expansion. */
3652 xbuf_len
= defn
->length
;
3653 for (ap
= defn
->pattern
; ap
!= NULL
; ap
= ap
->next
) {
3655 xbuf_len
+= args
[ap
->argno
].stringified_length
;
3657 xbuf_len
+= args
[ap
->argno
].raw_length
;
3660 xbuf
= (U_CHAR
*) xmalloc (xbuf_len
+ 1);
3662 /* Generate in XBUF the complete expansion
3663 with arguments substituted in.
3664 TOTLEN is the total size generated so far.
3665 OFFSET is the index in the definition
3666 of where we are copying from. */
3667 offset
= totlen
= 0;
3668 for (ap
= defn
->pattern
; ap
!= NULL
; ap
= ap
->next
) {
3669 register struct argdata
*arg
= &args
[ap
->argno
];
3671 for (i
= 0; i
< ap
->nchars
; i
++)
3672 xbuf
[totlen
++] = exp
[offset
++];
3674 if (ap
->stringify
!= 0) {
3675 int arglen
= arg
->raw_length
;
3681 && (c
= arg
->raw
[i
], is_space
[c
]))
3684 && (c
= arg
->raw
[arglen
- 1], is_space
[c
]))
3686 for (; i
< arglen
; i
++) {
3689 /* Special markers Newline Space
3690 generate nothing for a stringified argument. */
3691 if (c
== '\n' && arg
->raw
[i
+1] != '\n') {
3696 /* Internal sequences of whitespace are replaced by one space
3697 except within an string or char token. */
3699 && (c
== '\n' ? arg
->raw
[i
+1] == '\n' : is_space
[c
])) {
3701 /* Note that Newline Space does occur within whitespace
3702 sequences; consider it part of the sequence. */
3703 if (c
== '\n' && is_space
[arg
->raw
[i
+1]])
3705 else if (c
!= '\n' && is_space
[c
])
3722 } else if (c
== '\"' || c
== '\'')
3726 /* Escape these chars */
3727 if (c
== '\"' || (in_string
&& c
== '\\'))
3728 xbuf
[totlen
++] = '\\';
3732 sprintf ((char *) &xbuf
[totlen
], "\\%03o", (unsigned int) c
);
3737 const U_CHAR
*p1
= arg
->raw
;
3738 const U_CHAR
*l1
= p1
+ arg
->raw_length
;
3740 if (ap
->raw_before
) {
3741 while (p1
!= l1
&& is_space
[*p1
]) p1
++;
3742 while (p1
!= l1
&& is_idchar
[*p1
])
3743 xbuf
[totlen
++] = *p1
++;
3744 /* Delete any no-reexpansion marker that follows
3745 an identifier at the beginning of the argument
3746 if the argument is concatenated with what precedes it. */
3747 if (p1
[0] == '\n' && p1
[1] == '-')
3750 if (ap
->raw_after
) {
3751 /* Arg is concatenated after: delete trailing whitespace,
3752 whitespace markers, and no-reexpansion markers. */
3754 if (is_space
[l1
[-1]]) l1
--;
3755 else if (l1
[-1] == '-') {
3756 const U_CHAR
*p2
= l1
- 1;
3757 /* If a `-' is preceded by an odd number of newlines then it
3758 and the last newline are a no-reexpansion marker. */
3759 while (p2
!= p1
&& p2
[-1] == '\n') p2
--;
3760 if ((l1
- 1 - p2
) & 1) {
3768 memmove (xbuf
+ totlen
, p1
, l1
- p1
);
3772 if (totlen
> xbuf_len
)
3776 /* if there is anything left of the definition
3777 after handling the arg list, copy that in too. */
3779 for (i
= offset
; i
< defn
->length
; i
++)
3780 xbuf
[totlen
++] = exp
[i
];
3785 for (i
= 0; i
< nargs
; i
++) {
3786 if (args
[i
].free1
!= 0)
3787 free (args
[i
].free1
);
3788 if (args
[i
].free2
!= 0)
3789 free (args
[i
].free2
);
3793 xbuf
= defn
->expansion
;
3794 xbuf_len
= defn
->length
;
3797 /* Now put the expansion on the input stack
3798 so our caller will commence reading from it. */
3800 register FILE_BUF
*ip2
;
3802 ip2
= &instack
[++indepth
];
3807 ip2
->length
= xbuf_len
;
3809 ip2
->free_ptr
= (nargs
> 0) ? xbuf
: 0;
3811 ip2
->if_stack
= if_stack
;
3816 * Parse a macro argument and store the info on it into *ARGPTR.
3817 * Return nonzero to indicate a syntax error.
3822 register struct argdata
*argptr
;
3824 FILE_BUF
*ip
= &instack
[indepth
];
3829 /* Try to parse as much of the argument as exists at this
3830 input stack level. */
3831 U_CHAR
*bp
= macarg1 (ip
->bufp
, ip
->buf
+ ip
->length
,
3832 &paren
, &newlines
, &comments
);
3834 /* If we find the end of the argument at this level,
3835 set up *ARGPTR to point at it in the input stack. */
3836 if (!(ip
->fname
!= 0 && (newlines
!= 0 || comments
!= 0))
3837 && bp
!= ip
->buf
+ ip
->length
) {
3839 argptr
->raw
= ip
->bufp
;
3840 argptr
->raw_length
= bp
- ip
->bufp
;
3844 /* This input stack level ends before the macro argument does.
3845 We must pop levels and keep parsing.
3846 Therefore, we must allocate a temporary buffer and copy
3847 the macro argument into it. */
3848 int bufsize
= bp
- ip
->bufp
;
3849 int extra
= newlines
;
3850 U_CHAR
*buffer
= (U_CHAR
*) xmalloc (bufsize
+ extra
+ 1);
3851 int final_start
= 0;
3853 memcpy (buffer
, ip
->bufp
, bufsize
);
3855 ip
->lineno
+= newlines
;
3857 while (bp
== ip
->buf
+ ip
->length
) {
3858 if (instack
[indepth
].macro
== 0) {
3860 return "unterminated macro call";
3862 ip
->macro
->type
= T_MACRO
;
3864 free (ip
->free_ptr
);
3865 ip
= &instack
[--indepth
];
3868 bp
= macarg1 (ip
->bufp
, ip
->buf
+ ip
->length
, &paren
,
3869 &newlines
, &comments
);
3870 final_start
= bufsize
;
3871 bufsize
+= bp
- ip
->bufp
;
3873 buffer
= (U_CHAR
*) xrealloc (buffer
, bufsize
+ extra
+ 1);
3874 memcpy (buffer
+ bufsize
- (bp
- ip
->bufp
), ip
->bufp
, bp
- ip
->bufp
);
3876 ip
->lineno
+= newlines
;
3879 /* Now, if arg is actually wanted, record its raw form,
3880 discarding comments and duplicating newlines in whatever
3881 part of it did not come from a macro expansion.
3882 EXTRA space has been preallocated for duplicating the newlines.
3883 FINAL_START is the index of the start of that part. */
3885 argptr
->raw
= buffer
;
3886 argptr
->raw_length
= bufsize
;
3887 argptr
->free1
= buffer
;
3888 argptr
->newlines
= newlines
;
3889 argptr
->comments
= comments
;
3890 if ((newlines
|| comments
) && ip
->fname
!= 0)
3893 discard_comments (argptr
->raw
+ final_start
,
3894 argptr
->raw_length
- final_start
,
3896 argptr
->raw
[argptr
->raw_length
] = 0;
3897 if (argptr
->raw_length
> bufsize
+ extra
)
3902 /* If we are not discarding this argument,
3903 macroexpand it and compute its length as stringified.
3904 All this info goes into *ARGPTR. */
3908 register const U_CHAR
*buf
, *lim
;
3909 register int totlen
;
3911 obuf
= expand_to_temp_buffer (argptr
->raw
,
3912 argptr
->raw
+ argptr
->raw_length
,
3915 argptr
->expanded
= obuf
.buf
;
3916 argptr
->expand_length
= obuf
.length
;
3917 argptr
->free2
= obuf
.buf
;
3920 lim
= buf
+ argptr
->raw_length
;
3923 while (buf
!= lim
) {
3924 register U_CHAR c
= *buf
++;
3926 /* Internal sequences of whitespace are replaced by one space
3927 in most cases, but not always. So count all the whitespace
3928 in case we need to keep it all. */
3929 if (c
== '\"' || c
== '\\') /* escape these chars */
3931 else if (!ISPRINT (c
))
3934 argptr
->stringified_length
= totlen
;
3939 /* Scan text from START (inclusive) up to LIMIT (exclusive),
3940 counting parens in *DEPTHPTR,
3941 and return if reach LIMIT
3942 or before a `)' that would make *DEPTHPTR negative
3943 or before a comma when *DEPTHPTR is zero.
3944 Single and double quotes are matched and termination
3945 is inhibited within them. Comments also inhibit it.
3946 Value returned is pointer to stopping place.
3948 Increment *NEWLINES each time a newline is passed.
3949 Set *COMMENTS to 1 if a comment is seen. */
3952 macarg1 (start
, limit
, depthptr
, newlines
, comments
)
3954 register const U_CHAR
*limit
;
3955 int *depthptr
, *newlines
, *comments
;
3957 register U_CHAR
*bp
= start
;
3959 while (bp
< limit
) {
3965 if (--(*depthptr
) < 0)
3969 /* Traditionally, backslash makes following char not special. */
3973 /* But count source lines anyway. */
3982 if (bp
[1] == '\\' && bp
[2] == '\n')
3983 newline_fix (bp
+ 1);
3984 if (bp
[1] != '*' || bp
+ 1 >= limit
)
3988 while (bp
+ 1 < limit
) {
3990 && bp
[1] == '\\' && bp
[2] == '\n')
3991 newline_fix (bp
+ 1);
3992 if (bp
[0] == '*' && bp
[1] == '/')
3994 if (*bp
== '\n') ++*newlines
;
4003 for (quotec
= *bp
++; bp
+ 1 < limit
&& *bp
!= quotec
; bp
++) {
4008 while (*bp
== '\\' && bp
[1] == '\n') {
4011 } else if (*bp
== '\n') {
4020 if ((*depthptr
) == 0)
4030 /* Discard comments and duplicate newlines
4031 in the string of length LENGTH at START,
4032 except inside of string constants.
4033 The string is copied into itself with its beginning staying fixed.
4035 NEWLINES is the number of newlines that must be duplicated.
4036 We assume that that much extra space is available past the end
4040 discard_comments (start
, length
, newlines
)
4045 register U_CHAR
*ibp
;
4046 register U_CHAR
*obp
;
4047 register const U_CHAR
*limit
;
4050 /* If we have newlines to duplicate, copy everything
4051 that many characters up. Then, in the second part,
4052 we will have room to insert the newlines
4054 NEWLINES may actually be too large, because it counts
4055 newlines in string constants, and we don't duplicate those.
4056 But that does no harm. */
4058 ibp
= start
+ length
;
4059 obp
= ibp
+ newlines
;
4061 while (limit
!= ibp
)
4065 ibp
= start
+ newlines
;
4066 limit
= start
+ length
+ newlines
;
4069 while (ibp
< limit
) {
4070 *obp
++ = c
= *ibp
++;
4073 /* Duplicate the newline. */
4085 if (*ibp
== '\\' && ibp
[1] == '\n')
4087 /* Delete any comment. */
4088 if (ibp
[0] != '*' || ibp
+ 1 >= limit
)
4092 while (ibp
+ 1 < limit
) {
4094 && ibp
[1] == '\\' && ibp
[2] == '\n')
4095 newline_fix (ibp
+ 1);
4096 if (ibp
[0] == '*' && ibp
[1] == '/')
4105 /* Notice and skip strings, so that we don't
4106 think that comments start inside them,
4107 and so we don't duplicate newlines in them. */
4110 while (ibp
< limit
) {
4111 *obp
++ = c
= *ibp
++;
4114 if (c
== '\n' && quotec
== '\'')
4116 if (c
== '\\' && ibp
< limit
) {
4117 while (*ibp
== '\\' && ibp
[1] == '\n')
4131 /* Core error handling routine. */
4133 v_message (mtype
, line
, msgid
, ap
)
4139 const char *fname
= 0;
4142 if (mtype
== WARNING
&& inhibit_warnings
)
4145 for (i
= indepth
; i
>= 0; i
--)
4146 if (instack
[i
].fname
!= NULL
) {
4148 line
= instack
[i
].lineno
;
4149 fname
= instack
[i
].fname
;
4154 fprintf (stderr
, "%s:%d: ", fname
, line
);
4156 fprintf (stderr
, "%s: ", progname
);
4158 if (mtype
== WARNING
)
4159 fputs ("warning: ", stderr
);
4161 vfprintf (stderr
, msgid
, ap
);
4162 putc ('\n', stderr
);
4169 * error - print error message and increment count of errors.
4172 error
VPARAMS ((const char *msgid
, ...))
4174 #ifndef ANSI_PROTOTYPES
4179 VA_START(ap
, msgid
);
4181 #ifndef ANSI_PROTOTYPES
4182 msgid
= va_arg (ap
, const char *);
4185 v_message (ERROR
, 0, msgid
, ap
);
4189 error_with_line
VPARAMS ((int line
, const char *msgid
, ...))
4191 #ifndef ANSI_PROTOTYPES
4197 VA_START(ap
, msgid
);
4199 #ifndef ANSI_PROTOTYPES
4200 line
= va_arg (ap
, int);
4201 msgid
= va_arg (ap
, const char *);
4204 v_message (ERROR
, line
, msgid
, ap
);
4207 /* Error including a message from `errno'. */
4209 error_from_errno (name
)
4212 error ("%s: %s", name
, strerror (errno
));
4215 /* Print error message but don't count it. */
4217 warning
VPARAMS ((const char *msgid
, ...))
4219 #ifndef ANSI_PROTOTYPES
4224 VA_START(ap
, msgid
);
4226 #ifndef ANSI_PROTOTYPES
4227 msgid
= va_arg (ap
, const char *);
4230 v_message (WARNING
, 0, msgid
, ap
);
4234 fatal
VPARAMS ((const char *msgid
, ...))
4236 #ifndef ANSI_PROTOTYPES
4241 VA_START(ap
, msgid
);
4243 #ifndef ANSI_PROTOTYPES
4244 msgid
= va_arg (ap
, const char *);
4247 v_message (FATAL
, 0, msgid
, ap
);
4248 exit (FATAL_EXIT_CODE
);
4251 /* More 'friendly' abort that prints the location at which we died. */
4253 fancy_abort (line
, func
)
4257 fatal ("Internal error in %s, at tradcpp.c:%d\n\
4258 Please submit a full bug report.\n\
4259 See %s for instructions.", func
, line
, GCCBUGURL
);
4263 perror_with_name (name
)
4266 fprintf (stderr
, "%s: %s: %s\n", progname
, name
, strerror (errno
));
4271 pfatal_with_name (name
)
4274 perror_with_name (name
);
4275 exit (FATAL_EXIT_CODE
);
4278 /* Return the line at which an error occurred.
4279 The error is not necessarily associated with the current spot
4280 in the input stack, so LINE says where. LINE will have been
4281 copied from ip->lineno for the current input level.
4282 If the current level is for a file, we return LINE.
4283 But if the current level is not for a file, LINE is meaningless.
4284 In that case, we return the lineno of the innermost file. */
4286 line_for_error (line
)
4292 for (i
= indepth
; i
>= 0; ) {
4293 if (instack
[i
].fname
!= 0)
4298 line1
= instack
[i
].lineno
;
4304 * If OBUF doesn't have NEEDED bytes after OPTR, make it bigger.
4306 * As things stand, nothing is ever placed in the output buffer to be
4307 * removed again except when it's KNOWN to be part of an identifier,
4308 * so flushing and moving down everything left, instead of expanding,
4313 grow_outbuf (obuf
, needed
)
4314 register FILE_BUF
*obuf
;
4315 register int needed
;
4320 if (obuf
->length
- (obuf
->bufp
- obuf
->buf
) > needed
)
4323 /* Make it at least twice as big as it is now. */
4325 /* Make it have at least 150% of the free space we will need. */
4326 minsize
= (3 * needed
) / 2 + (obuf
->bufp
- obuf
->buf
);
4327 if (minsize
> obuf
->length
)
4328 obuf
->length
= minsize
;
4330 p
= (U_CHAR
*) xrealloc (obuf
->buf
, obuf
->length
);
4331 obuf
->bufp
= p
+ (obuf
->bufp
- obuf
->buf
);
4335 /* Symbol table for macro names and special symbols */
4338 * install a name in the main hash table, even if it is already there.
4339 * name stops with first non alphanumeric, except leading '#'.
4340 * caller must check against redefinition if that is desired.
4341 * delete_macro () removes things installed by install () in fifo order.
4342 * this is important because of the `defined' special symbol used
4343 * in #if, and also if pushdef/popdef directives are ever implemented.
4345 * If LEN is >= 0, it is the length of the name.
4346 * Otherwise, compute the length by scanning the entire name.
4348 * If HASH is >= 0, it is the precomputed hash code.
4349 * Otherwise, compute the hash code.
4351 * caller must set the value, if any is desired.
4354 install (name
, len
, type
, hash
)
4357 enum node_type type
;
4359 /* watch out here if sizeof (U_CHAR *) != sizeof (int) */
4361 register HASHNODE
*hp
;
4362 register int bucket
;
4363 register const U_CHAR
*p
;
4368 while (is_idchar
[*p
])
4374 hash
= hashf (name
, len
, HASHSIZE
);
4376 hp
= (HASHNODE
*) xmalloc (sizeof (HASHNODE
) + len
+ 1);
4378 hp
->bucket_hdr
= &hashtab
[bucket
];
4379 hp
->next
= hashtab
[bucket
];
4380 hashtab
[bucket
] = hp
;
4382 if (hp
->next
!= NULL
)
4383 hp
->next
->prev
= hp
;
4386 hp
->name
= q
= ((U_CHAR
*) hp
) + sizeof (HASHNODE
);
4387 memcpy (q
, name
, len
);
4393 * find the most recent hash node for name name (ending with first
4394 * non-identifier char) installed by install
4396 * If LEN is >= 0, it is the length of the name.
4397 * Otherwise, compute the length by scanning the entire name.
4399 * If HASH is >= 0, it is the precomputed hash code.
4400 * Otherwise, compute the hash code.
4403 lookup (name
, len
, hash
)
4408 register const U_CHAR
*bp
;
4409 register HASHNODE
*bucket
;
4412 for (bp
= name
; is_idchar
[*bp
]; bp
++) ;
4417 hash
= hashf (name
, len
, HASHSIZE
);
4419 bucket
= hashtab
[hash
];
4421 if (bucket
->length
== len
4422 && strncmp ((const char *)bucket
->name
, (const char *)name
, len
) == 0)
4424 bucket
= bucket
->next
;
4430 * Delete a hash node. Some weirdness to free junk from macros.
4431 * More such weirdness will have to be added if you define more hash
4432 * types that need it.
4435 /* Note that the DEFINITION of a macro is removed from the hash table
4436 but its storage is not freed. This would be a storage leak
4437 except that it is not reasonable to keep undefining and redefining
4438 large numbers of macros many times.
4439 In any case, this is necessary, because a macro can be #undef'd
4440 in the middle of reading the arguments to a call to it.
4441 If #undef freed the DEFINITION, that would crash. */
4447 if (hp
->prev
!= NULL
)
4448 hp
->prev
->next
= hp
->next
;
4449 if (hp
->next
!= NULL
)
4450 hp
->next
->prev
= hp
->prev
;
4452 /* make sure that the bucket chain header that
4453 the deleted guy was on points to the right thing afterwards. */
4454 if (hp
== *hp
->bucket_hdr
)
4455 *hp
->bucket_hdr
= hp
->next
;
4461 * return hash function on name. must be compatible with the one
4462 * computed a step at a time, elsewhere
4465 hashf (name
, len
, hashsize
)
4466 register const U_CHAR
*name
;
4473 r
= HASHSTEP (r
, *name
++);
4475 return MAKE_POS (r
) % hashsize
;
4478 /* Dump all macro definitions as #defines to stdout. */
4485 for (bucket
= 0; bucket
< HASHSIZE
; bucket
++) {
4486 register HASHNODE
*hp
;
4488 for (hp
= hashtab
[bucket
]; hp
; hp
= hp
->next
) {
4489 if (hp
->type
== T_MACRO
) {
4490 register DEFINITION
*defn
= hp
->value
.defn
;
4496 /* Print the definition of the macro HP. */
4498 printf ("#define %s", hp
->name
);
4499 if (defn
->nargs
>= 0) {
4503 for (i
= 0; i
< defn
->nargs
; i
++) {
4504 dump_arg_n (defn
, i
);
4505 if (i
+ 1 < defn
->nargs
)
4515 for (ap
= defn
->pattern
; ap
!= NULL
; ap
= ap
->next
) {
4516 dump_defn_1 (defn
->expansion
, offset
, ap
->nchars
);
4517 if (ap
->nchars
!= 0)
4519 offset
+= ap
->nchars
;
4522 if (ap
->raw_before
&& !concat
)
4525 dump_arg_n (defn
, ap
->argno
);
4526 if (ap
->raw_after
) {
4531 dump_defn_1 (defn
->expansion
, offset
, defn
->length
- offset
);
4538 /* Output to stdout a substring of a macro definition.
4539 BASE is the beginning of the definition.
4540 Output characters START thru LENGTH.
4541 Discard newlines outside of strings, thus
4542 converting funny-space markers to ordinary spaces. */
4544 dump_defn_1 (base
, start
, length
)
4549 const U_CHAR
*p
= base
+ start
;
4550 const U_CHAR
*limit
= base
+ start
+ length
;
4555 else if (*p
== '\"' || *p
=='\'') {
4556 const U_CHAR
*p1
= skip_quoted_string (p
, limit
, 0, 0, 0, 0);
4557 fwrite (p
, p1
- p
, 1, stdout
);
4564 /* Print the name of argument number ARGNUM of macro definition DEFN.
4565 Recall that DEFN->argnames contains all the arg names
4566 concatenated in reverse order with comma-space in between. */
4568 dump_arg_n (defn
, argnum
)
4572 register const U_CHAR
*p
= defn
->argnames
;
4573 while (argnum
+ 1 < defn
->nargs
) {
4574 p
= (const U_CHAR
*) strchr ((const char *)p
, ' ') + 1;
4578 while (*p
&& *p
!= ',') {
4584 /* Initialize syntactic classifications of characters. */
4586 initialize_char_syntax ()
4591 * Set up is_idchar and is_idstart tables. These should be
4592 * faster than saying (is_alpha (c) || c == '_'), etc.
4593 * Must do set up these things before calling any routines tthat
4596 for (i
= 'a'; i
<= 'z'; i
++) {
4597 is_idchar
[i
- 'a' + 'A'] = 1;
4599 is_idstart
[i
- 'a' + 'A'] = 1;
4602 for (i
= '0'; i
<= '9'; i
++)
4605 is_idstart
['_'] = 1;
4607 /* horizontal space table */
4608 is_hor_space
[' '] = 1;
4609 is_hor_space
['\t'] = 1;
4610 is_hor_space
['\v'] = 1;
4611 is_hor_space
['\f'] = 1;
4612 is_hor_space
['\r'] = 1;
4622 /* Initialize the built-in macros. */
4623 #define DSC(x) U x, sizeof x - 1
4624 #define install_spec(name, type) \
4625 install(DSC(name), type, -1);
4626 #define install_value(name, val) \
4627 hp = install(DSC(name), T_CONST, -1); hp->value.cpval = val;
4629 initialize_builtins ()
4633 install_spec ("__BASE_FILE__", T_BASE_FILE
);
4634 install_spec ("__DATE__", T_DATE
);
4635 install_spec ("__FILE__", T_FILE
);
4636 install_spec ("__TIME__", T_TIME
);
4637 install_spec ("__VERSION__", T_VERSION
);
4638 install_spec ("__INCLUDE_LEVEL__", T_INCLUDE_LEVEL
);
4639 install_spec ("__LINE__", T_SPECLINE
);
4641 #ifndef NO_BUILTIN_SIZE_TYPE
4642 install_value ("__SIZE_TYPE__", SIZE_TYPE
);
4644 #ifndef NO_BUILTIN_PTRDIFF_TYPE
4645 install_value ("__PTRDIFF_TYPE__", PTRDIFF_TYPE
);
4647 #ifndef NO_BUILTIN_WCHAR_TYPE
4648 install_value ("__WCHAR_TYPE__", WCHAR_TYPE
);
4650 #ifndef NO_BUILTIN_WINT_TYPE
4651 install_value ("__WINT_TYPE__", WINT_TYPE
);
4653 install_value ("__REGISTER_PREFIX__", REGISTER_PREFIX
);
4654 install_value ("__USER_LABEL_PREFIX__", user_label_prefix
);
4658 #undef install_value
4661 * process a given definition string, for initialization
4662 * If STR is just an identifier, define it with value 1.
4663 * If STR has anything after the identifier, then it should
4664 * be identifier-space-definition.
4667 make_definition (str
)
4671 struct directive
*kt
;
4674 size_t len
= strlen ((const char *)str
);
4676 p
= (const U_CHAR
*) strchr ((const char *)str
, '=');
4678 /* Change -DFOO into #define FOO 1 */
4679 buf
= (U_CHAR
*) alloca (len
+ 3);
4680 memcpy (buf
, str
, len
);
4681 memcpy (buf
+ len
, " 1", 3);
4684 buf
= (U_CHAR
*) alloca (len
+ 1);
4685 memcpy (buf
, str
, len
+ 1);
4689 ip
= &instack
[++indepth
];
4690 ip
->fname
= "*Initialization*";
4692 ip
->buf
= ip
->bufp
= buf
;
4697 ip
->if_stack
= if_stack
;
4699 for (kt
= directive_table
; kt
->type
!= T_DEFINE
; kt
++)
4702 /* pass NULL as output ptr to do_define since we KNOW it never
4703 does any output.... */
4704 do_define (buf
, buf
+ ip
->length
, NULL
, kt
);
4708 /* JF, this does the work for the -U option */
4714 struct directive
*kt
;
4716 ip
= &instack
[++indepth
];
4717 ip
->fname
= "*undef*";
4719 ip
->buf
= ip
->bufp
= str
;
4720 ip
->length
= strlen ((const char *)str
);
4724 ip
->if_stack
= if_stack
;
4726 for (kt
= directive_table
; kt
->type
!= T_UNDEF
; kt
++)
4729 do_undef (str
, str
+ ip
->length
, NULL
, kt
);
4733 /* Add output to `deps_buffer' for the -M switch.
4734 STRING points to the text to be output.
4735 SIZE is the number of bytes, or 0 meaning output until a null.
4736 If SIZE is nonzero, we break the line first, if it is long enough. */
4738 deps_output (string
, size
)
4742 #ifndef MAX_OUTPUT_COLUMNS
4743 #define MAX_OUTPUT_COLUMNS 75
4745 if (size
!= 0 && deps_column
!= 0
4746 && size
+ deps_column
> MAX_OUTPUT_COLUMNS
) {
4747 deps_output ("\\\n ", 0);
4752 size
= strlen (string
);
4754 if (deps_size
+ size
+ 1 > deps_allocated_size
) {
4755 deps_allocated_size
= deps_size
+ size
+ 50;
4756 deps_allocated_size
*= 2;
4757 deps_buffer
= (char *) xrealloc (deps_buffer
, deps_allocated_size
);
4759 memcpy (&deps_buffer
[deps_size
], string
, size
);
4761 deps_column
+= size
;
4762 deps_buffer
[deps_size
] = 0;
4765 /* Get the file-mode and data size of the file open on FD
4766 and store them in *MODE_POINTER and *SIZE_POINTER. */
4769 file_size_and_mode (fd
, mode_pointer
, size_pointer
)
4776 if (fstat (fd
, &sbuf
) < 0) return -1;
4777 if (mode_pointer
) *mode_pointer
= sbuf
.st_mode
;
4778 if (size_pointer
) *size_pointer
= sbuf
.st_size
;