]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/tradcpp.c
Warning fixes:
[thirdparty/gcc.git] / gcc / tradcpp.c
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
7
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
11 later version.
12
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.
17
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. */
21
22 #include "config.h"
23 #include "system.h"
24 #include "version.h"
25 #include "cppdefault.h"
26 #include "tradcpp.h"
27
28 typedef unsigned char U_CHAR;
29
30 /* Name under which this program was invoked. */
31
32 static const char *progname;
33
34 /* Current maximum length of directory names in the search path
35 for include files. (Altered as we get more of them.) */
36
37 size_t max_include_len;
38
39 /* Nonzero means copy comments into the output file. */
40
41 int put_out_comments = 0;
42
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. */
46
47 int print_deps = 0;
48
49 /* Nonzero means don't output line number information. */
50
51 int no_line_commands;
52
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. */
56
57 int dump_macros;
58
59 /* Nonzero means don't print warning messages. -w. */
60
61 int inhibit_warnings = 0;
62
63 /* Nonzero means warn if slash-star appears in a comment. */
64
65 int warn_comments;
66
67 /* Nonzero causes output not to be done,
68 but directives such as #define that have side effects
69 are still obeyed. */
70
71 int no_output;
72
73 /* Value of __USER_LABEL_PREFIX__. Target-dependent, also controlled
74 by -f(no-)leading-underscore. */
75 static const char *user_label_prefix;
76
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
83 struct file_buf {
84 const char *fname;
85 int lineno;
86 int length;
87 U_CHAR *buf;
88 U_CHAR *bufp;
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. */
97 U_CHAR *free_ptr;
98 } instack[INPUT_STACK_MAX];
99
100 typedef struct file_buf FILE_BUF;
101
102 /* Current nesting level of input sources.
103 `instack[indepth]' is the level currently being read. */
104 int indepth = -1;
105 #define CHECK_DEPTH(code) \
106 if (indepth >= (INPUT_STACK_MAX - 1)) \
107 { \
108 error_with_line (line_for_error (instack[indepth].lineno), \
109 "macro or #include recursion too deep"); \
110 code; \
111 }
112
113 /* Current depth in #include directives that use <...>. */
114 int system_include_depth = 0;
115
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. */
119
120 #define OUTBUF_SIZE 10 /* initial size of output buffer */
121 FILE_BUF outbuf;
122
123 /* Grow output buffer OBUF points at
124 so it can hold at least NEEDED more chars. */
125
126 #define check_expand(OBUF, NEEDED) do { \
127 if ((OBUF)->length - ((OBUF)->bufp - (OBUF)->buf) <= (NEEDED)) \
128 grow_outbuf ((OBUF), (NEEDED)); \
129 } while (0)
130
131 struct file_name_list
132 {
133 struct file_name_list *next;
134 const char *fname;
135 };
136
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 */
141
142 /* List of included files that contained #once. */
143 struct file_name_list *dont_repeat_files = 0;
144
145 /* List of other included files. */
146 struct file_name_list *all_include_files = 0;
147 \f
148 /* Structure allocated for every #define. For a simple replacement
149 such as
150 #define foo bar ,
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,
153 e.g.,
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
165 pattern list
166 { (0, 1), (1, 1), (1, 1), ..., (1, 1), NULL }
167 where (x, y) means (nchars, argno). */
168
169 typedef struct definition DEFINITION;
170 struct definition {
171 int nargs;
172 int length; /* length of expansion string */
173 U_CHAR *expansion;
174 struct reflist {
175 struct reflist *next;
176 char stringify; /* nonzero if this arg was preceded by a
177 # operator. */
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) */
183 } *pattern;
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;
189 };
190
191 /* different kinds of things that can appear in the value field
192 of a hash node. Actually, this may be useless now. */
193 union hashval {
194 const char *cpval;
195 DEFINITION *defn;
196 };
197
198
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. */
205
206 /* different flavors of hash nodes --- also used in keyword table */
207 enum node_type {
208 T_DEFINE = 1, /* `#define' */
209 T_INCLUDE, /* `#include' */
210 T_IFDEF, /* `#ifdef' */
211 T_IFNDEF, /* `#ifndef' */
212 T_IF, /* `#if' */
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. */
229 };
230
231 struct hashnode {
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 */
241 };
242
243 typedef struct hashnode HASHNODE;
244
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. */
251
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 */
256
257 /* `struct directive' defines one #-directive, including how to handle it. */
258
259 struct directive {
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. */
265 };
266
267 /* Last arg to output_line_command. */
268 enum file_change_code {same_file, enter_file, leave_file};
269
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
275 if stringified.
276 `free1' and `free2', if nonzero, point to blocks to be freed
277 when the macro argument data is no longer needed. */
278
279 struct argdata {
280 U_CHAR *raw, *expanded;
281 int raw_length, expand_length;
282 int stringified_length;
283 U_CHAR *free1, *free2;
284 char newlines;
285 char comments;
286 };
287
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
297 hash table. */
298
299 struct arglist {
300 struct arglist *next;
301 U_CHAR *name;
302 int length;
303 int argno;
304 };
305
306 /* Function prototypes. */
307
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 *));
317
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 *));
324
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);
330
331 static int line_for_error PARAMS ((int));
332
333 /* We know perfectly well which file this is, so we don't need to
334 use __FILE__. */
335 #undef abort
336 #if (GCC_VERSION >= 2007)
337 #define abort() fancy_abort(__LINE__, __FUNCTION__)
338 #else
339 #define abort() fancy_abort(__LINE__, 0);
340 #endif
341
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));
351
352 static int eval_if_expression PARAMS ((const U_CHAR *, int));
353
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 *));
358
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 *,
367 int *, 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 *));
371
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 *));
375
376 int main PARAMS ((int, char **));
377
378 /* Convenience. Write U"string" to get an unsigned string constant. */
379 #define U (const unsigned char *)
380
381 /* Here is the actual list of #-directives, most-often-used first. */
382
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},
395 };
396
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];
405
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)
408
409 int errors = 0; /* Error counter for exit code */
410
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,
413 struct arglist *));
414
415 /* Stack of conditionals currently in progress
416 (including both successful and failing conditionals). */
417
418 struct if_stack {
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 */
425 };
426 typedef struct if_stack IF_STACK_FRAME;
427 IF_STACK_FRAME *if_stack = NULL;
428
429 /* Buffer of -M output. */
430
431 char *deps_buffer;
432
433 /* Number of bytes allocated in above. */
434 int deps_allocated_size;
435
436 /* Number of bytes used. */
437 int deps_size;
438
439 /* Number of bytes since the last newline. */
440 int deps_column;
441
442 /* Nonzero means -I- has been seen,
443 so don't look for #include "foo" the source-file directory. */
444 int ignore_srcdir;
445
446 int
447 main (argc, argv)
448 int argc;
449 char **argv;
450 {
451 int st_mode;
452 long st_size;
453 const char *in_fname, *out_fname;
454 int f, i;
455 FILE_BUF *fp;
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;
460
461 /* Non-0 means don't output the preprocessed program. */
462 int inhibit_output = 0;
463
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;
468
469 #ifdef RLIMIT_STACK
470 /* Get rid of any avoidable limit on stack size. */
471 {
472 struct rlimit rlim;
473
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);
479 }
480 #endif /* RLIMIT_STACK defined */
481
482 progname = argv[0];
483
484 in_fname = NULL;
485 out_fname = NULL;
486
487 /* Initialize is_idchar to allow $. */
488 initialize_char_syntax ();
489
490 no_line_commands = 0;
491 dump_macros = 0;
492 no_output = 0;
493
494 max_include_len = cpp_GCC_INCLUDE_DIR_len + 7; /* ??? */
495
496 memset (pend_files, 0, argc * sizeof (char *));
497 memset (pend_defs, 0, argc * sizeof (char *));
498 memset (pend_undefs, 0, argc * sizeof (char *));
499
500 /* Process switches and find input file name. */
501
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)
507 out_fname = argv[i];
508 else
509 in_fname = argv[i];
510 } else {
511 switch (argv[i][1]) {
512 case 'A':
513 case 'E':
514 case '$':
515 case 'g':
516 break; /* Ignore for compatibility with ISO/extended cpp. */
517
518 case 'l':
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. */
531 break;
532
533 case 'i':
534 if (!strcmp (argv[i], "-include"))
535 {
536 if (i + 1 == argc)
537 fatal ("Filename missing after -i option");
538 else
539 pend_files[i] = argv[i+1], i++;
540 }
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 */
548
549 break;
550
551 case 'o':
552 if (out_fname != NULL)
553 fatal ("Output filename specified twice");
554 if (i + 1 == argc)
555 fatal ("Filename missing after -o option");
556 out_fname = argv[++i];
557 if (!strcmp (out_fname, "-"))
558 out_fname = "";
559 break;
560
561 case 'w':
562 inhibit_warnings = 1;
563 break;
564
565 case 'W':
566 if (!strcmp (argv[i], "-Wcomments"))
567 warn_comments = 1;
568 else if (!strcmp (argv[i], "-Wcomment"))
569 warn_comments = 1;
570 else if (!strcmp (argv[i], "-Wall")) {
571 warn_comments = 1;
572 }
573 break;
574
575 case 'f':
576 if (!strcmp (argv[i], "-fleading-underscore"))
577 user_label_prefix = "_";
578 else if (!strcmp (argv[i], "-fno-leading-underscore"))
579 user_label_prefix = "";
580 break;
581
582 case 'M':
583 if (!strcmp (argv[i], "-M"))
584 print_deps = 2;
585 else if (!strcmp (argv[i], "-MM"))
586 print_deps = 1;
587 inhibit_output = 1;
588 break;
589
590 case 'd':
591 dump_macros = 1;
592 no_output = 1;
593 break;
594
595 case 'v':
596 fprintf (stderr, "GNU traditional CPP version %s\n", version_string);
597 break;
598
599 case 'D':
600 {
601 char *p;
602
603 if (argv[i][2] != 0)
604 p = argv[i] + 2;
605 else if (i + 1 == argc)
606 fatal ("Macro name missing after -D option");
607 else
608 p = argv[++i];
609
610 pend_defs[i] = p;
611 }
612 break;
613
614 case 'U': /* JF #undef something */
615 if (argv[i][2] != 0)
616 pend_undefs[i] = argv[i] + 2;
617 else if (i + 1 == argc)
618 fatal ("Macro name missing after -U option");
619 else
620 pend_undefs[i] = argv[i+1], i++;
621 break;
622
623 case 'C':
624 put_out_comments = 1;
625 break;
626
627 case 'p':
628 if (!strcmp (argv[i], "-pedantic"))
629 fatal ("-pedantic and -traditional are mutually exclusive");
630 break;
631
632 case 't':
633 if (!strcmp (argv[i], "-trigraphs"))
634 fatal ("-trigraphs and -traditional are mutually exclusive");
635 break;
636
637 case 'P':
638 no_line_commands = 1;
639 break;
640
641 case 'I': /* Add directory to path for includes. */
642 add_include:
643 {
644 struct file_name_list *dirtmp;
645
646 if (! ignore_srcdir && !strcmp (argv[i] + 2, "-"))
647 ignore_srcdir = 1;
648 else {
649 dirtmp = (struct file_name_list *)
650 xmalloc (sizeof (struct file_name_list));
651 dirtmp->next = 0; /* New one goes on the end */
652 if (include == 0)
653 include = dirtmp;
654 else
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");
661 else
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;
667 }
668 }
669 break;
670
671 case 'n':
672 /* -nostdinc causes no default include directories.
673 You must specify all include-file directories with -I. */
674 no_standard_includes = 1;
675 break;
676
677 case '\0': /* JF handle '-' as file name meaning stdin or stdout */
678 if (in_fname == NULL) {
679 in_fname = "";
680 break;
681 } else if (out_fname == NULL) {
682 out_fname = "";
683 break;
684 } /* else fall through into error */
685
686 default:
687 fatal ("Invalid option `%s'", argv[i]);
688 }
689 }
690 }
691
692 if (user_label_prefix == 0)
693 user_label_prefix = USER_LABEL_PREFIX;
694
695 /* Initialize is_idchar. */
696 initialize_char_syntax ();
697
698 /* Install __LINE__, etc. Must follow initialize_char_syntax
699 and option processing. */
700 initialize_builtins ();
701
702 /* Do defines specified with -D and undefines specified with -U. */
703 for (i = 1; i < argc; i++)
704 if (pend_defs[i])
705 make_definition ((const U_CHAR *)pend_defs[i]);
706 else if (pend_undefs[i])
707 make_undef ((U_CHAR *)pend_undefs[i]);
708
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++) {
716 if (di->cplusplus)
717 continue;
718 dirtmp = (struct file_name_list *)
719 xmalloc (sizeof (struct file_name_list));
720 dirtmp->next = 0; /* New one goes on the end */
721 if (include == 0)
722 include = dirtmp;
723 else
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);
729 }
730
731 if (ignore_srcdir && first_bracket_include == 0)
732 first_bracket_include = old_last_include->next;
733 }
734
735 /* Initialize output buffer */
736
737 outbuf.buf = (U_CHAR *) xmalloc (OUTBUF_SIZE);
738 outbuf.bufp = outbuf.buf;
739 outbuf.length = OUTBUF_SIZE;
740
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. */
744
745 no_output++;
746 for (i = 1; i < argc; i++)
747 if (pend_files[i]) {
748 int fd = open (pend_files[i], O_RDONLY, 0666);
749 if (fd < 0) {
750 perror_with_name (pend_files[i]);
751 return FATAL_EXIT_CODE;
752 }
753 finclude (fd, pend_files[i], &outbuf);
754 }
755 no_output--;
756
757 /* Create an input stack level for the main input file
758 and copy the entire contents of the file into it. */
759
760 fp = &instack[++indepth];
761
762 /* JF check for stdin */
763 if (in_fname == NULL || *in_fname == 0) {
764 in_fname = "";
765 f = 0;
766 } else if ((f = open (in_fname, O_RDONLY, 0666)) < 0)
767 goto sys_error;
768
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. */
773
774 if (print_deps == 0
775 && (getenv ("SUNPRO_DEPENDENCIES") != 0
776 || getenv ("DEPENDENCIES_OUTPUT") != 0))
777 {
778 char *spec = getenv ("DEPENDENCIES_OUTPUT");
779 char *s;
780 char *output_file;
781
782 if (spec == 0)
783 {
784 spec = getenv ("SUNPRO_DEPENDENCIES");
785 print_deps = 2;
786 }
787 else
788 print_deps = 1;
789
790 /* Find the space before the DEPS_TARGET, if there is one. */
791 s = strchr (spec, ' ');
792 if (s)
793 {
794 deps_target = s + 1;
795 output_file = (char *) xmalloc (s - spec + 1);
796 memcpy (output_file, spec, s - spec);
797 output_file[s - spec] = 0;
798 }
799 else
800 {
801 deps_target = 0;
802 output_file = spec;
803 }
804
805 deps_stream = fopen (output_file, "a");
806 if (deps_stream == 0)
807 pfatal_with_name (output_file);
808 }
809 /* If the -M option was used, output the deps to standard output. */
810 else if (print_deps)
811 deps_stream = stdout;
812
813 /* For -M, print the expected object file name
814 as the target of this Make-rule. */
815 if (print_deps) {
816 deps_allocated_size = 200;
817 deps_buffer = (char *) xmalloc (deps_allocated_size);
818 deps_buffer[0] = 0;
819 deps_size = 0;
820 deps_column = 0;
821
822 if (deps_target) {
823 deps_output (deps_target, 0);
824 deps_output (":", 0);
825 } else if (*in_fname == 0)
826 deps_output ("-: ", 0);
827 else {
828 int len;
829 const char *p = in_fname;
830 const char *p1 = p;
831 /* Discard all directory prefixes from P. */
832 while (*p1) {
833 if (*p1 == '/')
834 p = p1 + 1;
835 p1++;
836 }
837 /* Output P, but remove known suffixes. */
838 len = strlen (p);
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] == '.'
843 && p[len - 2] == 'c'
844 && p[len - 1] == 'c')
845 deps_output (p, len - 3);
846 else
847 deps_output (p, 0);
848 /* Supply our own suffix. */
849 deps_output (".o : ", 0);
850 deps_output (in_fname, 0);
851 deps_output (" ", 0);
852 }
853 }
854
855 if (file_size_and_mode (f, &st_mode, &st_size))
856 goto sys_error;
857 fp->fname = in_fname;
858 fp->lineno = 1;
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. */
864 int size;
865 int bsize;
866 int cnt;
867 U_CHAR *bufp;
868
869 bsize = 2000;
870 size = 0;
871 fp->buf = (U_CHAR *) xmalloc (bsize + 2);
872 bufp = fp->buf;
873 for (;;) {
874 cnt = read (f, bufp, bsize - size);
875 if (cnt < 0) goto sys_error; /* error! */
876 if (cnt == 0) break; /* End of file */
877 size += cnt;
878 bufp += cnt;
879 if (bsize == size) { /* Buffer is full! */
880 bsize *= 2;
881 fp->buf = (U_CHAR *) xrealloc (fp->buf, bsize + 2);
882 bufp = fp->buf + size; /* May have moved */
883 }
884 }
885 fp->length = size;
886 } else {
887 /* Read a file whose size we can determine in advance.
888 For the sake of VMS, st_size is just an upper bound. */
889 long i;
890 fp->length = 0;
891 fp->buf = (U_CHAR *) xmalloc (st_size + 2);
892
893 while (st_size > 0) {
894 i = read (f, fp->buf + fp->length, st_size);
895 if (i <= 0) {
896 if (i == 0) break;
897 goto sys_error;
898 }
899 fp->length += i;
900 st_size -= i;
901 }
902 }
903 fp->bufp = fp->buf;
904 fp->if_stack = if_stack;
905
906 /* Make sure data ends with a newline. And put a null after it. */
907
908 if (fp->length > 0 && fp->buf[fp->length-1] != '\n')
909 fp->buf[fp->length++] = '\n';
910 fp->buf[fp->length] = '\0';
911
912 /* Now that we know the input file is valid, open the output. */
913
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);
918
919 output_line_command (fp, &outbuf, 0, same_file);
920
921 /* Scan the input, processing macros and directives. */
922
923 rescan (&outbuf, 0);
924
925 /* Now we have processed the entire input
926 Write whichever kind of output has been requested. */
927
928
929 if (dump_macros)
930 dump_all_macros ();
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");
934 }
935
936 if (print_deps) {
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");
943 }
944 }
945
946 if (ferror (stdout))
947 fatal ("I/O error on output");
948
949 if (errors)
950 exit (FATAL_EXIT_CODE);
951 exit (SUCCESS_EXIT_CODE);
952
953 sys_error:
954 pfatal_with_name (in_fname);
955 }
956
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). */
963 static void
964 newline_fix (bp)
965 U_CHAR *bp;
966 {
967 register U_CHAR *p = bp;
968 register int count = 0;
969
970 /* First count the backslash-newline pairs here. */
971
972 while (*p++ == '\\' && *p++ == '\n')
973 count++;
974
975 p = bp + count * 2;
976
977 /* Exit if what follows the backslash-newlines is not embarrassing. */
978
979 if (count == 0 || (*p != '/' && *p != '*'))
980 return;
981
982 /* Copy all potentially embarrassing characters
983 that follow the backslash-newline pairs
984 down to where the pairs originally started. */
985
986 while (*p == '*' || *p == '/')
987 *bp++ = *p++;
988
989 /* Now write the same number of pairs after the embarrassing chars. */
990 while (count-- > 0) {
991 *bp++ = '\\';
992 *bp++ = '\n';
993 }
994 }
995
996 /* Like newline_fix but for use within a directive-name.
997 Move any backslash-newlines up past any following symbol constituents. */
998 static void
999 name_newline_fix (bp)
1000 U_CHAR *bp;
1001 {
1002 register U_CHAR *p = bp;
1003 register int count = 0;
1004
1005 /* First count the backslash-newline pairs here. */
1006
1007 while (*p++ == '\\' && *p++ == '\n')
1008 count++;
1009
1010 p = bp + count * 2;
1011
1012 /* What follows the backslash-newlines is not embarrassing. */
1013
1014 if (count == 0 || !is_idchar[*p])
1015 return;
1016
1017 /* Copy all potentially embarrassing characters
1018 that follow the backslash-newline pairs
1019 down to where the pairs originally started. */
1020
1021 while (is_idchar[*p])
1022 *bp++ = *p++;
1023
1024 /* Now write the same number of pairs after the embarrassing chars. */
1025 while (count-- > 0) {
1026 *bp++ = '\\';
1027 *bp++ = '\n';
1028 }
1029 }
1030 \f
1031 /*
1032 * The main loop of the program.
1033 *
1034 * Read characters from the input stack, transferring them to the
1035 * output buffer OP.
1036 *
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.
1041 *
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.
1059 *
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.
1065 */
1066
1067 static void
1068 rescan (op, output_marks)
1069 FILE_BUF *op;
1070 int output_marks;
1071 {
1072 /* Character being scanned in main loop. */
1073 register U_CHAR c;
1074
1075 /* Length of pending accumulated identifier. */
1076 register int ident_length = 0;
1077
1078 /* Hash code of pending accumulated identifier. */
1079 register int hash = 0;
1080
1081 /* Current input level (&instack[indepth]). */
1082 FILE_BUF *ip;
1083
1084 /* Pointer for scanning input. */
1085 register U_CHAR *ibp;
1086
1087 /* Pointer to end of input. End of scan is controlled by LIMIT. */
1088 register U_CHAR *limit;
1089
1090 /* Pointer for storing output. */
1091 register U_CHAR *obp;
1092
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. */
1099 int redo_char = 0;
1100
1101 /* 1 if within an identifier inside of which a concatenation
1102 marker (Newline -) has been seen. */
1103 int concatenated = 0;
1104
1105 /* While scanning a comment or a string constant,
1106 this records the line it started on, for error messages. */
1107 int start_line;
1108
1109 /* Record position of last `real' newline. */
1110 U_CHAR *beg_of_line;
1111
1112 /* Pop the innermost input stack level, assuming it is a macro expansion. */
1113
1114 #define POPMACRO \
1115 do { ip->macro->type = T_MACRO; \
1116 if (ip->free_ptr) free (ip->free_ptr); \
1117 --indepth; } while (0)
1118
1119 /* Reload `rescan's local variables that describe the current
1120 level of the input stack. */
1121
1122 #define RECACHE \
1123 do { ip = &instack[indepth]; \
1124 ibp = ip->bufp; \
1125 limit = ip->buf + ip->length; \
1126 op->bufp = obp; \
1127 check_expand (op, limit - ibp); \
1128 beg_of_line = 0; \
1129 obp = op->bufp; } while (0)
1130
1131 if (no_output && instack[indepth].fname != 0)
1132 skip_if_group (&instack[indepth], 1);
1133
1134 obp = op->bufp;
1135 RECACHE;
1136 beg_of_line = ibp;
1137
1138 /* Our caller must always put a null after the end of
1139 the input at each input stack level. */
1140 if (*limit != 0)
1141 abort ();
1142
1143 while (1) {
1144 c = *ibp++;
1145 *obp++ = c;
1146
1147 switch (c) {
1148 case '\\':
1149 if (ibp >= limit)
1150 break;
1151 if (*ibp == '\n') {
1152 /* Always merge lines ending with backslash-newline,
1153 even in middle of identifier. */
1154 ++ibp;
1155 ++ip->lineno;
1156 --obp; /* remove backslash from obuf */
1157 break;
1158 }
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)
1163 goto specialchar;
1164 *obp++ = *ibp++;
1165 break;
1166
1167 case '#':
1168 /* If this is expanding a macro definition, don't recognize
1169 preprocessor directives. */
1170 if (ip->macro != 0)
1171 goto randomchar;
1172 if (ident_length)
1173 goto specialchar;
1174
1175 /* # keyword: a # must be the first char on the line */
1176 if (beg_of_line == 0)
1177 goto randomchar;
1178 if (beg_of_line + 1 != ibp)
1179 goto randomchar;
1180
1181 /* This # can start a directive. */
1182
1183 --obp; /* Don't copy the '#' */
1184
1185 ip->bufp = ibp;
1186 op->bufp = obp;
1187 if (! handle_directive (ip, op)) {
1188 #ifdef USE_C_ALLOCA
1189 alloca (0);
1190 #endif
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);
1198 RECACHE;
1199 beg_of_line = ibp;
1200 break;
1201 }
1202 ++obp; /* Copy the '#' after all */
1203 goto randomchar;
1204 }
1205 #ifdef USE_C_ALLOCA
1206 alloca (0);
1207 #endif
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);
1213 obp = op->bufp;
1214 RECACHE;
1215 beg_of_line = ibp;
1216 break;
1217
1218 case '\"': /* skip quoted string */
1219 case '\'':
1220 /* A single quoted string is treated like a double -- some
1221 programs (e.g., troff) are perverse this way */
1222
1223 if (ident_length)
1224 goto specialchar;
1225
1226 start_line = ip->lineno;
1227
1228 /* Skip ahead to a matching quote. */
1229
1230 while (1) {
1231 if (ibp >= limit) {
1232 if (ip->macro != 0) {
1233 /* try harder: this string crosses a macro expansion boundary */
1234 POPMACRO;
1235 RECACHE;
1236 continue;
1237 }
1238 break;
1239 }
1240 *obp++ = *ibp;
1241 switch (*ibp++) {
1242 case '\n':
1243 ++ip->lineno;
1244 ++op->lineno;
1245 /* Traditionally, end of line ends a string constant with no error.
1246 So exit the loop and record the new line. */
1247 beg_of_line = ibp;
1248 goto while2end;
1249
1250 case '\\':
1251 if (ibp >= limit)
1252 break;
1253 if (*ibp == '\n') {
1254 /* Backslash newline is replaced by nothing at all,
1255 but keep the line counts correct. */
1256 --obp;
1257 ++ibp;
1258 ++ip->lineno;
1259 } else {
1260 /* ANSI stupidly requires that in \\ the second \
1261 is *not* prevented from combining with a newline. */
1262 while (*ibp == '\\' && ibp[1] == '\n') {
1263 ibp += 2;
1264 ++ip->lineno;
1265 }
1266 *obp++ = *ibp++;
1267 }
1268 break;
1269
1270 case '\"':
1271 case '\'':
1272 if (ibp[-1] == c)
1273 goto while2end;
1274 break;
1275 }
1276 }
1277 while2end:
1278 break;
1279
1280 case '/':
1281 if (*ibp == '\\' && ibp[1] == '\n')
1282 newline_fix (ibp);
1283 /* Don't look for comments inside a macro definition. */
1284 if (ip->macro != 0)
1285 goto randomchar;
1286 /* A comment constitutes white space, so it can terminate an identifier.
1287 Process the identifier, if any. */
1288 if (ident_length)
1289 goto specialchar;
1290
1291 if (*ibp != '*')
1292 goto randomchar;
1293
1294 /* We have a comment. Skip it, optionally copying it to output. */
1295
1296 start_line = ip->lineno;
1297
1298 ++ibp; /* Skip the star. */
1299
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)
1303 obp--;
1304 else
1305 *obp++ = '*';
1306
1307 {
1308 U_CHAR *before_bp = ibp;
1309
1310 while (ibp < limit) {
1311 switch (*ibp++) {
1312 case '/':
1313 if (warn_comments && ibp < limit && *ibp == '*')
1314 warning("`/*' within comment");
1315 break;
1316 case '*':
1317 if (*ibp == '\\' && ibp[1] == '\n')
1318 newline_fix (ibp);
1319 if (ibp >= limit || *ibp == '/')
1320 goto comment_end;
1321 break;
1322 case '\n':
1323 ++ip->lineno;
1324 /* Copy the newline into the output buffer, in order to
1325 avoid the pain of a #line every time a multiline comment
1326 is seen. */
1327 if (!put_out_comments)
1328 *obp++ = '\n';
1329 ++op->lineno;
1330 }
1331 }
1332 comment_end:
1333
1334 if (ibp >= limit)
1335 error_with_line (line_for_error (start_line),
1336 "unterminated comment");
1337 else {
1338 ibp++;
1339 if (put_out_comments) {
1340 memcpy (obp, before_bp, ibp - before_bp);
1341 obp += ibp - before_bp;
1342 }
1343 }
1344 }
1345 break;
1346
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". */
1354
1355 if (ident_length == 0) {
1356 while (ibp < limit) {
1357 while (ibp < limit && ibp[0] == '\\' && ibp[1] == '\n') {
1358 ++ip->lineno;
1359 ibp += 2;
1360 }
1361 c = *ibp++;
1362 if (!ISALNUM (c) && c != '.' && c != '_') {
1363 --ibp;
1364 break;
1365 }
1366 *obp++ = 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') {
1371 ++ip->lineno;
1372 ibp += 2;
1373 }
1374 if (ibp < limit && (*ibp == '+' || *ibp == '-')) {
1375 *obp++ = *ibp++;
1376 /* Traditional C does not let the token go past the sign. */
1377 break;
1378 }
1379 }
1380 }
1381 break;
1382 }
1383 /* fall through */
1384
1385 case '_':
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':
1390 case 'y': case 'z':
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':
1395 case 'Y': case 'Z':
1396 ident_length++;
1397 /* Compute step of hash function, to avoid a proc call on every token */
1398 hash = HASHSTEP (hash, c);
1399 break;
1400
1401 case '\n':
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. */
1409
1410 if (*ibp == '-') {
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) {
1415 ident_length = 0;
1416 hash = 0;
1417 }
1418 ibp++;
1419 if (!output_marks) {
1420 obp--;
1421 } else {
1422 /* If expanding a macro arg, keep the newline -. */
1423 *obp++ = '-';
1424 }
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)
1429 goto specialchar;
1430
1431 /* If generating final output, newline space makes a space. */
1432 if (!output_marks) {
1433 obp[-1] = *ibp++;
1434 /* And Newline Newline makes a newline, so count it. */
1435 if (obp[-1] == '\n')
1436 op->lineno++;
1437 } else {
1438 /* If expanding a macro arg, keep the newline space.
1439 If the arg gets stringified, newline space makes nothing. */
1440 *obp++ = *ibp++;
1441 }
1442 } else abort (); /* Newline followed by something random? */
1443 break;
1444 }
1445
1446 /* If there is a pending identifier, handle it and come back here. */
1447 if (ident_length > 0)
1448 goto specialchar;
1449
1450 beg_of_line = ibp;
1451
1452 /* Update the line counts and output a #line if necessary. */
1453 ++ip->lineno;
1454 ++op->lineno;
1455 if (ip->lineno != op->lineno) {
1456 op->bufp = obp;
1457 output_line_command (ip, op, 1, same_file);
1458 check_expand (op, ip->length - (ip->bufp - ip->buf));
1459 obp = op->bufp;
1460 }
1461 break;
1462
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. */
1465 case 0:
1466 if (ibp <= limit)
1467 /* Our input really contains a null character. */
1468 goto randomchar;
1469
1470 /* At end of a macro-expansion level, pop it and read next level. */
1471 if (ip->macro != 0) {
1472 obp--;
1473 ibp--;
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]) {
1477 redo_char = 1;
1478 goto randomchar;
1479 }
1480 POPMACRO;
1481 RECACHE;
1482 break;
1483 }
1484
1485 /* If we don't have a pending identifier,
1486 return at end of input. */
1487 if (ident_length == 0) {
1488 obp--;
1489 ibp--;
1490 op->bufp = obp;
1491 ip->bufp = ibp;
1492 goto ending;
1493 }
1494
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. */
1498
1499 /* Fall through */
1500
1501 specialchar:
1502
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. */
1507
1508 ibp--;
1509 obp--;
1510 redo_char = 1;
1511
1512 default:
1513
1514 randomchar:
1515
1516 if (ident_length > 0) {
1517 register HASHNODE *hp;
1518
1519 /* We have just seen an identifier end. If it's a macro, expand it.
1520
1521 IDENT_LENGTH is the length of the identifier
1522 and HASH is its hash code.
1523
1524 The identifier has already been copied to the output,
1525 so if it is a macro we must remove it.
1526
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.
1531
1532 If REDO_CHAR is 1, the terminating char has already been
1533 backed over. OBP-IDENT_LENGTH points to the identifier. */
1534
1535 for (hp = hashtab[MAKE_POS (hash) % HASHSIZE]; hp != NULL;
1536 hp = hp->next) {
1537
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;
1544
1545 if (! redo_char)
1546 q--;
1547
1548 do { /* All this to avoid a strncmp () */
1549 if (*p++ != *q++)
1550 goto hashcollision;
1551 } while (--i);
1552
1553 /* We found a use of a macro name.
1554 see if the context shows it is a macro call. */
1555
1556 /* Back up over terminating character if not already done. */
1557 if (! redo_char) {
1558 ibp--;
1559 obp--;
1560 }
1561
1562 obufp_before_macroname = obp - ident_length;
1563 op_lineno_before_macroname = op->lineno;
1564
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)
1570 {
1571 while (1) {
1572 /* Scan forward over whitespace, copying it to the output. */
1573 if (ibp == limit && ip->macro != 0) {
1574 POPMACRO;
1575 RECACHE;
1576 }
1577 /* A comment: copy it unchanged or discard it. */
1578 else if (*ibp == '/' && ibp+1 != limit && ibp[1] == '*') {
1579 if (put_out_comments) {
1580 *obp++ = '/';
1581 *obp++ = '*';
1582 }
1583 ibp += 2;
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. */
1588 if (*ibp == '\n') {
1589 /* Newline in a file. Count it. */
1590 ++ip->lineno;
1591 ++op->lineno;
1592 }
1593 if (put_out_comments)
1594 *obp++ = *ibp++;
1595 else
1596 ibp++;
1597 }
1598 ibp += 2;
1599 if (put_out_comments) {
1600 *obp++ = '*';
1601 *obp++ = '/';
1602 }
1603 }
1604 else if (is_space[*ibp]) {
1605 *obp++ = *ibp++;
1606 if (ibp[-1] == '\n') {
1607 if (ip->macro == 0) {
1608 /* Newline in a file. Count it. */
1609 ++ip->lineno;
1610 ++op->lineno;
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. */
1616 obp--;
1617 if (*ibp == '-')
1618 ibp++;
1619 else {
1620 if (*ibp == '\n')
1621 ++op->lineno;
1622 *obp++ = *ibp++;
1623 }
1624 } else {
1625 /* A newline mark; copy both chars to the output. */
1626 *obp++ = *ibp++;
1627 }
1628 }
1629 }
1630 else break;
1631 }
1632 if (*ibp != '(')
1633 break;
1634 }
1635
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;
1641
1642 /* Expand the macro, reading arguments as needed,
1643 and push the expansion on the input stack. */
1644 ip->bufp = ibp;
1645 op->bufp = obp;
1646 macroexpand (hp, op);
1647
1648 /* Reexamine input stack, since macroexpand has pushed
1649 a new level on it. */
1650 obp = op->bufp;
1651 RECACHE;
1652 break;
1653 }
1654 hashcollision:
1655 ;
1656 } /* End hash-table-search loop */
1657 ident_length = hash = 0; /* Stop collecting identifier */
1658 redo_char = 0;
1659 concatenated = 0;
1660 } /* End if (ident_length > 0) */
1661 } /* End switch */
1662 } /* End per-char loop */
1663
1664 /* Come here to return -- but first give an error message
1665 if there was an unterminated successful conditional. */
1666 ending:
1667 if (if_stack != ip->if_stack) {
1668 const char *str;
1669 switch (if_stack->type) {
1670 case T_IF:
1671 str = "if";
1672 break;
1673 case T_IFDEF:
1674 str = "ifdef";
1675 break;
1676 case T_IFNDEF:
1677 str = "ifndef";
1678 break;
1679 case T_ELSE:
1680 str = "else";
1681 break;
1682 case T_ELIF:
1683 str = "elif";
1684 break;
1685 default:
1686 abort ();
1687 }
1688 error_with_line (line_for_error (if_stack->lineno),
1689 "unterminated #%s conditional", str);
1690 }
1691 if_stack = ip->if_stack;
1692 }
1693 \f
1694 /*
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.
1697 *
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.
1702 */
1703 static FILE_BUF
1704 expand_to_temp_buffer (buf, limit, output_marks)
1705 const U_CHAR *buf, *limit;
1706 int output_marks;
1707 {
1708 register FILE_BUF *ip;
1709 FILE_BUF obuf;
1710 int length = limit - buf;
1711 U_CHAR *buf1;
1712 int odepth = indepth;
1713
1714 if (length < 0)
1715 abort ();
1716
1717 /* Set up the input on the input stack. */
1718
1719 buf1 = (U_CHAR *) alloca (length + 1);
1720 {
1721 register const U_CHAR *p1 = buf;
1722 register U_CHAR *p2 = buf1;
1723
1724 while (p1 != limit)
1725 *p2++ = *p1++;
1726 }
1727 buf1[length] = 0;
1728
1729 /* Set up to receive the output. */
1730
1731 obuf.length = length * 2 + 100; /* Usually enough. Why be stingy? */
1732 obuf.bufp = obuf.buf = (U_CHAR *) xmalloc (obuf.length);
1733 obuf.fname = 0;
1734 obuf.macro = 0;
1735 obuf.free_ptr = 0;
1736
1737 CHECK_DEPTH ({return obuf;});
1738
1739 ++indepth;
1740
1741 ip = &instack[indepth];
1742 ip->fname = 0;
1743 ip->macro = 0;
1744 ip->free_ptr = 0;
1745 ip->length = length;
1746 ip->buf = ip->bufp = buf1;
1747 ip->if_stack = if_stack;
1748
1749 ip->lineno = obuf.lineno = 1;
1750
1751 /* Scan the input, create the output. */
1752
1753 rescan (&obuf, output_marks);
1754
1755 /* Pop input stack to original state. */
1756 --indepth;
1757
1758 if (indepth != odepth)
1759 abort ();
1760
1761 /* Record the output. */
1762 obuf.length = obuf.bufp - obuf.buf;
1763
1764 return obuf;
1765 }
1766 \f
1767 /*
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.
1776 *
1777 * Returns nonzero if this was a known # directive.
1778 * Otherwise, returns zero, without advancing the input pointer.
1779 */
1780
1781 static int
1782 handle_directive (ip, op)
1783 FILE_BUF *ip, *op;
1784 {
1785 register U_CHAR *bp, *cp;
1786 register struct directive *kt;
1787 register int ident_length;
1788 U_CHAR *resume_p;
1789
1790 /* Nonzero means we must copy the entire command
1791 to get rid of comments or backslash-newlines. */
1792 int copy_command = 0;
1793
1794 U_CHAR *ident, *after_ident;
1795
1796 bp = ip->bufp;
1797 /* Skip whitespace and \-newline. */
1798 while (1) {
1799 if (is_hor_space[*bp])
1800 bp++;
1801 else if (*bp == '/' && (newline_fix (bp + 1), bp[1]) == '*') {
1802 ip->bufp = bp;
1803 skip_to_end_of_comment (ip, &ip->lineno);
1804 bp = ip->bufp;
1805 } else if (*bp == '\\' && bp[1] == '\n') {
1806 bp += 2; ip->lineno++;
1807 } else break;
1808 }
1809
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. */
1813
1814 cp = bp;
1815 while (1) {
1816 if (is_idchar[*cp])
1817 cp++;
1818 else {
1819 if (*cp == '\\' && cp[1] == '\n')
1820 name_newline_fix (cp);
1821 if (is_idchar[*cp])
1822 cp++;
1823 else break;
1824 }
1825 }
1826 ident_length = cp - bp;
1827 ident = bp;
1828 after_ident = cp;
1829
1830 /* A line of just `#' becomes blank. */
1831
1832 if (ident_length == 0 && *after_ident == '\n') {
1833 ip->bufp = after_ident;
1834 return 1;
1835 }
1836
1837 /*
1838 * Decode the keyword and call the appropriate expansion
1839 * routine, after moving the input pointer up to the next line.
1840 */
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;
1847
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;
1851
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). */
1856
1857 buf = bp = after_ident;
1858 while (bp < limit) {
1859 register U_CHAR c = *bp++;
1860 switch (c) {
1861 case '\\':
1862 if (bp < limit) {
1863 if (*bp == '\n') {
1864 ip->lineno++;
1865 copy_command = 1;
1866 }
1867 bp++;
1868 }
1869 break;
1870
1871 case '\'':
1872 case '\"':
1873 bp = skip_quoted_string (bp - 1, limit, ip->lineno, &ip->lineno, &copy_command, &unterminated);
1874 if (unterminated) {
1875 /* Traditional preprocessing permits unterminated strings. */
1876 ip->bufp = bp;
1877 goto endloop1;
1878 }
1879 break;
1880
1881 /* <...> is special for #include. */
1882 case '<':
1883 if (kt->type != T_INCLUDE)
1884 break;
1885 while (*bp && *bp != '>') bp++;
1886 break;
1887
1888 case '/':
1889 if (*bp == '\\' && bp[1] == '\n')
1890 newline_fix (bp);
1891 if (*bp == '*') {
1892 U_CHAR *obp = bp - 1;
1893 ip->bufp = bp + 1;
1894 skip_to_end_of_comment (ip, &ip->lineno);
1895 bp = ip->bufp;
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') {
1899 bp = obp;
1900 goto endloop1;
1901 }
1902 /* Don't remove the comments if this is #define. */
1903 if (! keep_comments)
1904 copy_command++;
1905 }
1906 break;
1907
1908 case '\n':
1909 --bp; /* Point to the newline */
1910 ip->bufp = bp;
1911 goto endloop1;
1912 }
1913 }
1914 ip->bufp = bp;
1915
1916 endloop1:
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. */
1921
1922 if (copy_command) {
1923 register U_CHAR *xp = buf;
1924 /* Need to copy entire command into temp buffer before dispatching */
1925
1926 cp = (U_CHAR *) alloca (bp - buf + 5); /* room for cmd plus
1927 some slop */
1928 buf = cp;
1929
1930 /* Copy to the new buffer, deleting comments
1931 and backslash-newlines (and whitespace surrounding the latter). */
1932
1933 while (xp < bp) {
1934 register U_CHAR c = *xp++;
1935 *cp++ = c;
1936
1937 switch (c) {
1938 case '\n':
1939 break;
1940
1941 /* <...> is special for #include. */
1942 case '<':
1943 if (kt->type != T_INCLUDE)
1944 break;
1945 while (xp < bp && c != '>') {
1946 c = *xp++;
1947 if (c == '\\' && xp < bp && *xp == '\n')
1948 xp++, ip->lineno++;
1949 else
1950 *cp++ = c;
1951 }
1952 break;
1953
1954 case '\\':
1955 if (*xp == '\n') {
1956 xp++;
1957 cp--;
1958 if (cp != buf && is_space[cp[-1]]) {
1959 while (cp != buf && is_space[cp[-1]]) cp--;
1960 cp++;
1961 SKIP_WHITE_SPACE (xp);
1962 } else if (is_space[*xp]) {
1963 *cp++ = *xp++;
1964 SKIP_WHITE_SPACE (xp);
1965 }
1966 } else {
1967 *cp++ = *xp++;
1968 }
1969 break;
1970
1971 case '\'':
1972 case '\"':
1973 {
1974 register const U_CHAR *bp1
1975 = skip_quoted_string (xp - 1, limit, ip->lineno, 0, 0, 0);
1976 while (xp != bp1)
1977 *cp++ = *xp++;
1978 }
1979 break;
1980
1981 case '/':
1982 if (*xp == '*') {
1983 ip->bufp = xp + 1;
1984 skip_to_end_of_comment (ip, 0);
1985 if (keep_comments)
1986 while (xp != ip->bufp)
1987 *cp++ = *xp++;
1988 /* Delete the slash. */
1989 else
1990 cp--;
1991 xp = ip->bufp;
1992 }
1993 }
1994 }
1995
1996 /* Null-terminate the copy. */
1997
1998 *cp = 0;
1999 }
2000 else
2001 cp = bp;
2002
2003 ip->bufp = resume_p;
2004
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));
2012
2013 return 1;
2014 }
2015 }
2016
2017 return 0;
2018 }
2019 \f
2020 static const char *const
2021 monthnames[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
2022 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
2023
2024 /*
2025 * expand things like __FILE__. Place the expansion into the output
2026 * buffer *without* rescanning.
2027 */
2028 static void
2029 special_symbol (hp, op)
2030 HASHNODE *hp;
2031 FILE_BUF *op;
2032 {
2033 const char *buf;
2034 time_t t;
2035 int i, len;
2036 int true_indepth;
2037 FILE_BUF *ip = NULL;
2038 static struct tm *timebuf = NULL;
2039
2040 int paren = 0; /* For special `defined' keyword */
2041
2042 for (i = indepth; i >= 0; i--)
2043 if (instack[i].fname != NULL) {
2044 ip = &instack[i];
2045 break;
2046 }
2047 if (ip == NULL)
2048 fatal ("not in any file?!");
2049
2050 switch (hp->type) {
2051 case T_FILE:
2052 case T_BASE_FILE:
2053 {
2054 const char *string;
2055 if (hp->type == T_FILE)
2056 string = ip->fname;
2057 else
2058 string = instack[0].fname;
2059
2060 if (string)
2061 {
2062 char *tmp = (char *) alloca (3 + strlen (string));
2063 sprintf (tmp, "\"%s\"", string);
2064 buf = tmp;
2065 }
2066 else
2067 buf = "";
2068
2069 break;
2070 }
2071
2072 case T_INCLUDE_LEVEL:
2073 {
2074 char *tmp = (char *) alloca (8); /* Eigth bytes ought to be more than enough */
2075 true_indepth = 0;
2076 for (i = indepth; i >= 0; i--)
2077 if (instack[i].fname != NULL)
2078 true_indepth++;
2079
2080 sprintf (tmp, "%d", true_indepth - 1);
2081 buf = tmp;
2082 break;
2083 }
2084
2085 case T_VERSION:
2086 {
2087 char *tmp = (char *) alloca (3 + strlen (version_string));
2088 sprintf (tmp, "\"%s\"", version_string);
2089 buf = tmp;
2090 break;
2091 }
2092
2093 case T_CONST:
2094 buf = hp->value.cpval;
2095 break;
2096
2097 case T_SPECLINE:
2098 {
2099 char *tmp = (char *) alloca (10);
2100 sprintf (tmp, "%d", ip->lineno);
2101 buf = tmp;
2102 break;
2103 }
2104
2105 case T_DATE:
2106 case T_TIME:
2107 {
2108 char *tmp = (char *) alloca (20);
2109
2110 if (timebuf == NULL) {
2111 t = time (0);
2112 timebuf = localtime (&t);
2113 }
2114 if (hp->type == T_DATE)
2115 sprintf (tmp, "\"%s %2d %4d\"", monthnames[timebuf->tm_mon],
2116 timebuf->tm_mday, timebuf->tm_year + 1900);
2117 else
2118 sprintf (tmp, "\"%02d:%02d:%02d\"", timebuf->tm_hour, timebuf->tm_min,
2119 timebuf->tm_sec);
2120 buf = tmp;
2121 break;
2122 }
2123
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 == '(') {
2129 paren++;
2130 ip->bufp++; /* Skip over the paren */
2131 SKIP_WHITE_SPACE (ip->bufp);
2132 }
2133
2134 if (!is_idstart[*ip->bufp])
2135 goto oops;
2136 if (lookup (ip->bufp, -1, -1))
2137 buf = " 1 ";
2138 while (is_idchar[*ip->bufp])
2139 ++ip->bufp;
2140 SKIP_WHITE_SPACE (ip->bufp);
2141 if (paren) {
2142 if (*ip->bufp != ')')
2143 goto oops;
2144 ++ip->bufp;
2145 }
2146 break;
2147
2148 oops:
2149
2150 error ("`defined' must be followed by ident or (ident)");
2151 break;
2152
2153 default:
2154 error ("cccp error: invalid special hash type"); /* time for gdb */
2155 abort ();
2156 }
2157 len = strlen (buf);
2158 check_expand (op, len);
2159 memcpy (op->bufp, buf, len);
2160 op->bufp += len;
2161 }
2162
2163 \f
2164 /* Routines to handle #directives */
2165
2166 /*
2167 * Process include file by reading it in and calling rescan.
2168 * Expects to see "fname" or <fname> on the input.
2169 */
2170 static void
2171 do_include (buf, limit, op, keyword)
2172 U_CHAR *buf, *limit;
2173 FILE_BUF *op;
2174 struct directive *keyword ATTRIBUTE_UNUSED;
2175 {
2176 char *fname; /* Dynamically allocated fname buffer */
2177 U_CHAR *fbeg, *fend; /* Beginning and end of fname */
2178
2179 struct file_name_list *stackp = include; /* Chain of dirs to search */
2180 struct file_name_list dsp[1]; /* First in chain, if #include "..." */
2181 int flen;
2182
2183 int f; /* file number */
2184
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 <...> */
2189
2190 f= -1; /* JF we iz paranoid! */
2191
2192 get_filename:
2193
2194 fbeg = buf;
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--;
2199
2200 switch (*fbeg++) {
2201 case '\"':
2202 fend = fbeg;
2203 while (fend != limit && *fend != '\"')
2204 fend++;
2205 if (*fend == '\"' && fend + 1 == limit) {
2206 FILE_BUF *fp;
2207
2208 /* We have "filename". Figure out directory this source
2209 file is coming from and put it on the front of the list. */
2210
2211 /* If -I- was specified, don't search current dir, only spec'd ones. */
2212 if (ignore_srcdir) break;
2213
2214 for (fp = &instack[indepth]; fp >= instack; fp--)
2215 {
2216 size_t n;
2217 const char *ep, *nam;
2218
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;
2223 stackp = dsp;
2224 ep = strrchr (nam, '/');
2225 if (ep != NULL) {
2226 char *f;
2227 n = ep - nam;
2228 f = (char *) alloca (n + 1);
2229 strncpy (f, nam, n);
2230 f[n] = '\0';
2231 dsp[0].fname = f;
2232 if (n > max_include_len) max_include_len = n;
2233 } else {
2234 dsp[0].fname = 0; /* Current directory */
2235 }
2236 break;
2237 }
2238 }
2239 break;
2240 }
2241 goto fail;
2242
2243 case '<':
2244 fend = fbeg;
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;
2251 break;
2252 }
2253 goto fail;
2254
2255 default:
2256 fail:
2257 if (retried) {
2258 error ("#include expects \"fname\" or <fname>");
2259 return;
2260 } else {
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);
2265 free (trybuf.buf);
2266 retried++;
2267 goto get_filename;
2268 }
2269 }
2270
2271 flen = fend - fbeg;
2272 fname = (char *) alloca (max_include_len + flen + 2);
2273 /* + 2 above for slash and terminating null. */
2274
2275 /* If specified file name is absolute, just open it. */
2276
2277 if (*fbeg == '/') {
2278 strncpy (fname, (const char *)fbeg, flen);
2279 fname[flen] = 0;
2280 f = open (fname, O_RDONLY, 0666);
2281 } else {
2282 /* Search directory path, trying to open the file.
2283 Copy each filename tried into FNAME. */
2284
2285 for (; stackp; stackp = stackp->next) {
2286 if (stackp->fname) {
2287 strcpy (fname, stackp->fname);
2288 strcat (fname, "/");
2289 fname[strlen (fname) + flen] = 0;
2290 } else {
2291 fname[0] = 0;
2292 }
2293 strncat (fname, (const char *)fbeg, flen);
2294 if ((f = open (fname, O_RDONLY, 0666)) >= 0)
2295 break;
2296 }
2297 }
2298
2299 if (f < 0) {
2300 strncpy (fname, (const char *)fbeg, flen);
2301 fname[flen] = 0;
2302 error_from_errno (fname);
2303
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",
2308 fend - fbeg, fbeg);
2309 else
2310 {
2311 deps_output ((const char *)fbeg, fend - fbeg);
2312 deps_output (" ", 0);
2313 }
2314 }
2315 } else {
2316
2317 /* Check to see if this include file is a once-only include file.
2318 If so, give up. */
2319
2320 struct file_name_list* ptr;
2321
2322 for (ptr = dont_repeat_files; ptr; ptr = ptr->next) {
2323 if (!strcmp (ptr->fname, fname)) {
2324 close (f);
2325 return; /* This file was once'd. */
2326 }
2327 }
2328
2329 for (ptr = all_include_files; ptr; ptr = ptr->next) {
2330 if (!strcmp (ptr->fname, fname))
2331 break; /* This file was included before. */
2332 }
2333
2334 if (ptr == 0) {
2335 /* This is the first time for this file. */
2336 /* Add it to list of files included. */
2337
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);
2342
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);
2347 }
2348 }
2349
2350 if (system_header_p)
2351 system_include_depth++;
2352
2353 /* Actually process the file. */
2354 finclude (f, fname, op);
2355
2356 if (system_header_p)
2357 system_include_depth--;
2358
2359 close (f);
2360 }
2361 }
2362
2363 /* Process the contents of include file FNAME, already open on descriptor F,
2364 with output to OP. */
2365
2366 static void
2367 finclude (f, fname, op)
2368 int f;
2369 const char *fname;
2370 FILE_BUF *op;
2371 {
2372 int st_mode;
2373 long st_size;
2374 long i;
2375 FILE_BUF *fp; /* For input stack frame */
2376
2377 CHECK_DEPTH (return;);
2378
2379 if (file_size_and_mode (f, &st_mode, &st_size))
2380 goto nope;
2381
2382 fp = &instack[indepth + 1];
2383 memset (fp, 0, sizeof (FILE_BUF));
2384 fp->fname = fname;
2385 fp->length = 0;
2386 fp->lineno = 1;
2387 fp->if_stack = if_stack;
2388
2389 if (S_ISREG (st_mode)) {
2390 fp->buf = (U_CHAR *) xmalloc (st_size + 2);
2391 fp->bufp = fp->buf;
2392
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);
2397 if (i <= 0) {
2398 if (i == 0) break;
2399 goto nope;
2400 }
2401 fp->length += i;
2402 st_size -= i;
2403 }
2404 }
2405 else {
2406 /* Cannot count its file size before reading. */
2407
2408 U_CHAR *bufp;
2409 U_CHAR *basep;
2410 int bsize = 2000;
2411
2412 st_size = 0;
2413 basep = (U_CHAR *) xmalloc (bsize + 2);
2414 bufp = basep;
2415
2416 for (;;) {
2417 i = read (f, bufp, bsize - st_size);
2418 if (i < 0)
2419 goto nope; /* error! */
2420 if (i == 0)
2421 break; /* End of file */
2422 st_size += i;
2423 bufp += i;
2424 if (bsize == st_size) { /* Buffer is full! */
2425 bsize *= 2;
2426 basep = (U_CHAR *) xrealloc (basep, bsize + 2);
2427 bufp = basep + st_size; /* May have moved */
2428 }
2429 }
2430 fp->buf = basep;
2431 fp->bufp = fp->buf;
2432 fp->length = st_size;
2433 }
2434 close (f);
2435
2436 /* Make sure data ends with a newline. And put a null after it. */
2437
2438 if (fp->length > 0 && fp->buf[fp->length-1] != '\n')
2439 fp->buf[fp->length++] = '\n';
2440 fp->buf[fp->length] = '\0';
2441
2442 indepth++;
2443 output_line_command (fp, op, 0, enter_file);
2444 rescan (op, 0);
2445 indepth--;
2446 output_line_command (&instack[indepth], op, 0, leave_file);
2447 free (fp->buf);
2448 return;
2449
2450 nope:
2451 perror_with_name (fname);
2452 close (f);
2453 }
2454 \f
2455
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. */
2460
2461 static void
2462 do_define (buf, limit, op, keyword)
2463 U_CHAR *buf, *limit;
2464 FILE_BUF *op ATTRIBUTE_UNUSED;
2465 struct directive *keyword ATTRIBUTE_UNUSED;
2466 {
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 */
2470
2471 DEFINITION *defn;
2472 int arglengths = 0; /* Accumulate lengths of arg names
2473 plus number of args. */
2474 int hashcode;
2475
2476 bp = buf;
2477
2478 while (is_hor_space[*bp])
2479 bp++;
2480
2481 symname = bp; /* remember where it starts */
2482 while (is_idchar[*bp] && bp < limit) {
2483 bp++;
2484 }
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);
2494 } else {
2495 if (! strncmp ((const char *)symname, "defined", 7) && sym_length == 7)
2496 error ("defining `defined' as a macro");
2497 }
2498
2499 /* lossage will occur if identifiers or control keywords are broken
2500 across lines using backslash. This is not the right place to take
2501 care of that. */
2502
2503 if (*bp == '(') {
2504 struct arglist *arg_ptrs = NULL;
2505 int argno = 0;
2506
2507 bp++; /* skip '(' */
2508 SKIP_WHITE_SPACE (bp);
2509
2510 /* Loop over macro argument names. */
2511 while (*bp != ')') {
2512 struct arglist *temp;
2513
2514 temp = (struct arglist *) alloca (sizeof (struct arglist));
2515 temp->name = bp;
2516 temp->next = arg_ptrs;
2517 temp->argno = argno++;
2518 arg_ptrs = temp;
2519
2520 if (!is_idstart[*bp])
2521 warning ("parameter name starts with a digit in #define");
2522
2523 /* Find the end of the arg name. */
2524 while (is_idchar[*bp]) {
2525 bp++;
2526 }
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");
2532 return;
2533 }
2534 if (*bp == ',') {
2535 bp++;
2536 SKIP_WHITE_SPACE (bp);
2537 }
2538 if (bp >= limit) {
2539 error ("unterminated parameter list in #define");
2540 return;
2541 }
2542 }
2543
2544 ++bp; /* skip paren */
2545 while (is_hor_space[*bp]) /* and leading whitespace */
2546 ++bp;
2547 /* now everything from bp before limit is the definition. */
2548 defn = collect_expansion (bp, limit, argno, arg_ptrs);
2549
2550 /* Now set defn->argnames to the result of concatenating
2551 the argument names in reverse order
2552 with comma-space between them. */
2553 {
2554 struct arglist *temp;
2555 int i = 0;
2556 U_CHAR *tmp = (U_CHAR *) xmalloc (arglengths + 1);
2557
2558 for (temp = arg_ptrs; temp; temp = temp->next) {
2559 memcpy (&tmp[i], temp->name, temp->length);
2560 i += temp->length;
2561 if (temp->next != 0) {
2562 tmp[i++] = ',';
2563 tmp[i++] = ' ';
2564 }
2565 }
2566 tmp[i] = 0;
2567 defn->argnames = tmp;
2568
2569 }
2570 } else {
2571 /* simple expansion or empty definition; skip leading whitespace */
2572 while (is_hor_space[*bp])
2573 ++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 *) "";
2577 }
2578
2579 hashcode = hashf (symname, sym_length, HASHSIZE);
2580
2581 {
2582 HASHNODE *hp;
2583 if ((hp = lookup (symname, sym_length, hashcode)) == NULL)
2584 hp = install (symname, sym_length, T_MACRO, hashcode);
2585 else {
2586 if (hp->type != T_MACRO || compare_defs (defn, hp->value.defn))
2587 warning ("\"%.*s\" redefined", sym_length, symname);
2588
2589 /* Replace the old definition. */
2590 hp->type = T_MACRO;
2591 }
2592
2593 hp->value.defn = defn;
2594 }
2595 }
2596
2597 /*
2598 * return zero if two DEFINITIONs are isomorphic
2599 */
2600 static int
2601 compare_defs (d1, d2)
2602 DEFINITION *d1, *d2;
2603 {
2604 register struct reflist *a1, *a2;
2605 register U_CHAR *p1 = d1->expansion;
2606 register U_CHAR *p2 = d2->expansion;
2607 int first = 1;
2608
2609 if (d1->nargs != d2->nargs)
2610 return 1;
2611 if (strcmp ((const char *)d1->argnames, (const char *)d2->argnames))
2612 return 1;
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)
2622 return 1;
2623 first = 0;
2624 p1 += a1->nchars;
2625 p2 += a2->nchars;
2626 }
2627 if (a1 != a2)
2628 return 1;
2629 if (comp_def_part (first, p1, d1->length - (p1 - d1->expansion),
2630 p2, d2->length - (p2 - d2->expansion), 1))
2631 return 1;
2632 return 0;
2633 }
2634
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. */
2643 static int
2644 comp_def_part (first, beg1, len1, beg2, len2, last)
2645 int first;
2646 const U_CHAR *beg1, *beg2;
2647 int len1, len2;
2648 int last;
2649 {
2650 register const U_CHAR *end1 = beg1 + len1;
2651 register const U_CHAR *end2 = beg2 + len2;
2652 if (first) {
2653 while (beg1 != end1 && is_space[*beg1]) beg1++;
2654 while (beg2 != end2 && is_space[*beg2]) beg2++;
2655 }
2656 if (last) {
2657 while (beg1 != end1 && is_space[end1[-1]]) end1--;
2658 while (beg2 != end2 && is_space[end2[-1]]) end2--;
2659 }
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) {
2665 beg1++; beg2++;
2666 } else break;
2667 }
2668 return (beg1 != end1) || (beg2 != end2);
2669 }
2670
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.
2679
2680 Note that comments and backslash-newlines have already been deleted
2681 from the argument. */
2682
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.)
2688
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. */
2691
2692 static DEFINITION *
2693 collect_expansion (buf, end, nargs, arglist)
2694 U_CHAR *buf, *end;
2695 int nargs;
2696 struct arglist *arglist;
2697 {
2698 DEFINITION *defn;
2699 register U_CHAR *p, *limit, *lastp, *exp_p;
2700 struct reflist *endpat = NULL;
2701 /* Pointer to first nonspace after last ## seen. */
2702 U_CHAR *concat = 0;
2703 /* Pointer to first nonspace after last single-# seen. */
2704 U_CHAR *stringify = 0;
2705 int maxsize;
2706 int expected_delimiter = '\0';
2707
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. */
2712
2713 if (end < buf)
2714 abort ();
2715
2716 /* Find the beginning of the trailing whitespace. */
2717 /* Find end of leading whitespace. */
2718 limit = end;
2719 p = buf;
2720 while (p < limit && is_space[limit[-1]]) limit--;
2721 while (p < limit && is_space[*p]) p++;
2722
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)
2730 + (limit - p) + 3);
2731 defn = (DEFINITION *) xcalloc (1, maxsize);
2732
2733 defn->nargs = nargs;
2734 exp_p = defn->expansion = (U_CHAR *) defn + sizeof (DEFINITION);
2735 lastp = exp_p;
2736
2737 p = buf;
2738
2739 /* Convert leading whitespace to Newline-markers. */
2740 while (p < limit && is_space[*p]) {
2741 *exp_p++ = '\n';
2742 *exp_p++ = *p++;
2743 }
2744
2745 /* Process the main body of the definition. */
2746 while (p < limit) {
2747 int skipped_arg = 0;
2748 register U_CHAR c = *p++;
2749
2750 *exp_p++ = c;
2751
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. */
2756 switch (c) {
2757 case '\'':
2758 case '\"':
2759 if (expected_delimiter != '\0') {
2760 if (c == expected_delimiter)
2761 expected_delimiter = '\0';
2762 } else
2763 expected_delimiter = c;
2764 break;
2765
2766 case '\\':
2767 /* Backslash quotes delimiters and itself, but not macro args. */
2768 if (expected_delimiter != 0 && p < limit
2769 && (*p == expected_delimiter || *p == '\\')) {
2770 *exp_p++ = *p++;
2771 continue;
2772 }
2773 break;
2774
2775 case '/':
2776 if (expected_delimiter != '\0') /* No comments inside strings. */
2777 break;
2778 if (*p == '*') {
2779 /* If we find a comment that wasn't removed by handle_directive,
2780 this must be -traditional. So replace the comment with
2781 nothing at all. */
2782 exp_p--;
2783 p += 1;
2784 while (p < limit && !(p[-2] == '*' && p[-1] == '/'))
2785 p++;
2786 }
2787 break;
2788 }
2789
2790 if (is_idchar[c] && nargs > 0) {
2791 U_CHAR *id_beg = p - 1;
2792 int id_len;
2793
2794 --exp_p;
2795 while (p != limit && is_idchar[*p]) p++;
2796 id_len = p - id_beg;
2797
2798 if (is_idstart[c]) {
2799 register struct arglist *arg;
2800
2801 for (arg = arglist; arg != NULL; arg = arg->next) {
2802 struct reflist *tpat;
2803
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
2809 the pat list */
2810 tpat = (struct reflist *) xmalloc (sizeof (struct reflist));
2811 tpat->next = NULL;
2812 tpat->raw_before = concat == id_beg;
2813 tpat->raw_after = 0;
2814 tpat->stringify = expected_delimiter != '\0';
2815
2816 if (endpat == NULL)
2817 defn->pattern = tpat;
2818 else
2819 endpat->next = tpat;
2820 endpat = tpat;
2821
2822 tpat->argno = arg->argno;
2823 tpat->nchars = exp_p - lastp;
2824 {
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;
2829 }
2830 lastp = exp_p; /* place to start copying from next time */
2831 skipped_arg = 1;
2832 break;
2833 }
2834 }
2835 }
2836
2837 /* If this was not a macro arg, copy it into the expansion. */
2838 if (! skipped_arg) {
2839 register U_CHAR *lim1 = p;
2840 p = id_beg;
2841 while (p != lim1)
2842 *exp_p++ = *p++;
2843 if (stringify == id_beg)
2844 error ("# operator should be followed by a macro argument name");
2845 }
2846 }
2847 }
2848
2849 if (limit < end) {
2850 /* Convert trailing whitespace to Newline-markers. */
2851 while (limit < end && is_space[*limit]) {
2852 *exp_p++ = '\n';
2853 *exp_p++ = *limit++;
2854 }
2855 }
2856 *exp_p = '\0';
2857
2858 defn->length = exp_p - defn->expansion;
2859
2860 /* Crash now if we overrun the allocated size. */
2861 if (defn->length + 1 > maxsize)
2862 abort ();
2863
2864 return defn;
2865 }
2866 \f
2867 /*
2868 * interpret #line command. Remembers previously seen fnames
2869 * in its very own hash table.
2870 */
2871 #define FNAME_HASHSIZE 37
2872 static void
2873 do_line (buf, limit, op, keyword)
2874 U_CHAR *buf, *limit;
2875 FILE_BUF *op;
2876 struct directive *keyword ATTRIBUTE_UNUSED;
2877 {
2878 register U_CHAR *bp;
2879 FILE_BUF *ip = &instack[indepth];
2880 FILE_BUF tem;
2881 int new_lineno;
2882 enum file_change_code file_change = same_file;
2883
2884 /* Expand any macros. */
2885 tem = expand_to_temp_buffer (buf, limit, 0);
2886
2887 /* Point to macroexpanded line, which is null-terminated now. */
2888 bp = tem.buf;
2889 SKIP_WHITE_SPACE (bp);
2890
2891 if (!ISDIGIT (*bp)) {
2892 error ("invalid format #line command");
2893 return;
2894 }
2895
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;
2900
2901 /* skip over the line number. */
2902 while (ISDIGIT (*bp))
2903 bp++;
2904
2905 #if 0 /* #line 10"foo.c" is supposed to be allowed. */
2906 if (*bp && !is_space[*bp]) {
2907 error ("invalid format #line command");
2908 return;
2909 }
2910 #endif
2911
2912 SKIP_WHITE_SPACE (bp);
2913
2914 if (*bp == '\"') {
2915 static HASHNODE *fname_table[FNAME_HASHSIZE];
2916 HASHNODE *hp, **hash_bucket;
2917 U_CHAR *fname;
2918 int fname_length;
2919
2920 fname = ++bp;
2921
2922 while (*bp && *bp != '\"')
2923 bp++;
2924 if (*bp != '\"') {
2925 error ("invalid format #line command");
2926 return;
2927 }
2928
2929 fname_length = bp - fname;
2930
2931 bp++;
2932 SKIP_WHITE_SPACE (bp);
2933 if (*bp) {
2934 if (*bp == '1')
2935 file_change = enter_file;
2936 else if (*bp == '2')
2937 file_change = leave_file;
2938 else {
2939 error ("invalid format #line command");
2940 return;
2941 }
2942
2943 bp++;
2944 SKIP_WHITE_SPACE (bp);
2945 if (*bp) {
2946 error ("invalid format #line command");
2947 return;
2948 }
2949 }
2950
2951 hash_bucket =
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;
2957 break;
2958 }
2959 if (hp == 0) {
2960 char *q;
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;
2964 *hash_bucket = hp;
2965
2966 hp->length = fname_length;
2967 ip->fname = hp->value.cpval = q = ((char *) hp) + sizeof (HASHNODE);
2968 memcpy (q, fname, fname_length);
2969 }
2970 } else if (*bp) {
2971 error ("invalid format #line command");
2972 return;
2973 }
2974
2975 ip->lineno = new_lineno;
2976 output_line_command (ip, op, 0, file_change);
2977 check_expand (op, ip->length - (ip->bufp - ip->buf));
2978 }
2979
2980 /*
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.
2984 */
2985 static void
2986 do_undef (buf, limit, op, keyword)
2987 U_CHAR *buf;
2988 U_CHAR *limit ATTRIBUTE_UNUSED;
2989 FILE_BUF *op ATTRIBUTE_UNUSED;
2990 struct directive *keyword ATTRIBUTE_UNUSED;
2991 {
2992 HASHNODE *hp;
2993
2994 SKIP_WHITE_SPACE (buf);
2995
2996 if (! strncmp ((const char *)buf, "defined", 7) && ! is_idchar[buf[7]])
2997 warning ("undefining `defined'");
2998
2999 while ((hp = lookup (buf, -1, -1)) != NULL) {
3000 if (hp->type != T_MACRO)
3001 warning ("undefining `%s'", hp->name);
3002 delete_macro (hp);
3003 }
3004 }
3005
3006 /*
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.
3017 */
3018 static void
3019 do_if (buf, limit, op, keyword)
3020 U_CHAR *buf, *limit;
3021 FILE_BUF *op ATTRIBUTE_UNUSED;
3022 struct directive *keyword ATTRIBUTE_UNUSED;
3023 {
3024 int value;
3025 FILE_BUF *ip = &instack[indepth];
3026
3027 value = eval_if_expression (buf, limit - buf);
3028 conditional_skip (ip, value == 0, T_IF);
3029 }
3030
3031 /*
3032 * handle a #elif directive by not changing if_stack either.
3033 * see the comment above do_else.
3034 */
3035 static void
3036 do_elif (buf, limit, op, keyword)
3037 U_CHAR *buf, *limit;
3038 FILE_BUF *op;
3039 struct directive *keyword ATTRIBUTE_UNUSED;
3040 {
3041 int value;
3042 FILE_BUF *ip = &instack[indepth];
3043
3044 if (if_stack == instack[indepth].if_stack) {
3045 error ("#elif not within a conditional");
3046 return;
3047 } else {
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");
3055 }
3056 if_stack->type = T_ELIF;
3057 }
3058
3059 if (if_stack->if_succeeded)
3060 skip_if_group (ip, 0);
3061 else {
3062 value = eval_if_expression (buf, limit - buf);
3063 if (value == 0)
3064 skip_if_group (ip, 0);
3065 else {
3066 ++if_stack->if_succeeded; /* continue processing input */
3067 output_line_command (ip, op, 1, same_file);
3068 }
3069 }
3070 }
3071
3072 /*
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.
3075 */
3076 static int
3077 eval_if_expression (buf, length)
3078 const U_CHAR *buf;
3079 int length;
3080 {
3081 FILE_BUF temp_obuf;
3082 HASHNODE *save_defined;
3083 int value;
3084
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 */
3088
3089 value = parse_c_expression ((const char *)temp_obuf.buf);
3090
3091 free (temp_obuf.buf);
3092
3093 return value;
3094 }
3095
3096 /*
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.
3100 */
3101 static void
3102 do_xifdef (buf, limit, op, keyword)
3103 U_CHAR *buf, *limit;
3104 FILE_BUF *op ATTRIBUTE_UNUSED;
3105 struct directive *keyword;
3106 {
3107 int skip;
3108 FILE_BUF *ip = &instack[indepth];
3109 U_CHAR *end;
3110
3111 /* Discard leading and trailing whitespace. */
3112 SKIP_WHITE_SPACE (buf);
3113 while (limit != buf && is_hor_space[limit[-1]]) limit--;
3114
3115 /* Find the end of the identifier at the beginning. */
3116 for (end = buf; is_idchar[*end]; end++);
3117
3118 if (end == buf) {
3119 skip = (keyword->type == T_IFDEF);
3120 } else {
3121 skip = (lookup (buf, end-buf, -1) == NULL) ^ (keyword->type == T_IFNDEF);
3122 }
3123
3124 conditional_skip (ip, skip, T_IF);
3125 }
3126
3127 /*
3128 * push TYPE on stack; then, if SKIP is nonzero, skip ahead.
3129 */
3130 static void
3131 conditional_skip (ip, skip, type)
3132 FILE_BUF *ip;
3133 int skip;
3134 enum node_type type;
3135 {
3136 IF_STACK_FRAME *temp;
3137
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;
3142 if_stack = temp;
3143
3144 if_stack->type = type;
3145
3146 if (skip != 0) {
3147 skip_if_group (ip, 0);
3148 return;
3149 } else {
3150 ++if_stack->if_succeeded;
3151 output_line_command (ip, &outbuf, 1, same_file);
3152 }
3153 }
3154
3155 /*
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.
3159 */
3160 static void
3161 skip_if_group (ip, any)
3162 FILE_BUF *ip;
3163 int any;
3164 {
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;
3170
3171 while (bp < endb) {
3172 switch (*bp++) {
3173 case '/': /* possible comment */
3174 if (*bp == '\\' && bp[1] == '\n')
3175 newline_fix (bp);
3176 if (*bp == '*') {
3177 ip->bufp = ++bp;
3178 bp = skip_to_end_of_comment (ip, &ip->lineno);
3179 }
3180 break;
3181 case '\"':
3182 case '\'':
3183 bp = skip_quoted_string (bp - 1, endb, ip->lineno, &ip->lineno, 0, 0);
3184 break;
3185 case '\\':
3186 /* Char after backslash loses its special meaning. */
3187 if (bp < endb) {
3188 if (*bp == '\n')
3189 ++ip->lineno; /* But do update the line-count. */
3190 bp++;
3191 }
3192 break;
3193 case '\n':
3194 ++ip->lineno;
3195 beg_of_line = bp;
3196 break;
3197 case '#':
3198 ip->bufp = bp - 1;
3199
3200 /* # keyword: a # must be first nonblank char on the line */
3201 if (beg_of_line == 0)
3202 break;
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. */
3206 bp = beg_of_line;
3207 while (1) {
3208 if (is_hor_space[*bp])
3209 bp++;
3210 else if (*bp == '\\' && bp[1] == '\n')
3211 bp += 2;
3212 else if (*bp == '/' && bp[1] == '*') {
3213 bp += 2;
3214 while (!(*bp == '*' && bp[1] == '/')) {
3215 if (*bp == '\n')
3216 ip->lineno++;
3217 bp++;
3218 }
3219 bp += 2;
3220 }
3221 else break;
3222 }
3223 if (bp != ip->bufp) {
3224 bp = ip->bufp + 1; /* Reset bp to after the #. */
3225 break;
3226 }
3227
3228 bp = ip->bufp + 1; /* Point after '#'. */
3229
3230 /* Skip whitespace and \-newline. */
3231 while (1) {
3232 if (is_hor_space[*bp])
3233 bp++;
3234 else if (*bp == '\\' && bp[1] == '\n')
3235 bp += 2;
3236 else if (*bp == '/' && bp[1] == '*') {
3237 bp += 2;
3238 while (!(*bp == '*' && bp[1] == '/'))
3239 bp++;
3240 bp += 2;
3241 }
3242 else break;
3243 }
3244
3245 cp = bp;
3246
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. */
3250
3251 while (1) {
3252 if (is_idchar[*bp])
3253 bp++;
3254 else {
3255 if (*bp == '\\' && bp[1] == '\n')
3256 name_newline_fix (bp);
3257 if (is_idchar[*bp])
3258 bp++;
3259 else break;
3260 }
3261 }
3262
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]]) {
3267
3268 /* If we are asked to return on next directive,
3269 do so now. */
3270 if (any)
3271 return;
3272
3273 switch (kt->type) {
3274 case T_IF:
3275 case T_IFDEF:
3276 case T_IFNDEF:
3277 temp = (IF_STACK_FRAME *) xcalloc (1, sizeof (IF_STACK_FRAME));
3278 temp->next = if_stack;
3279 if_stack = temp;
3280 temp->lineno = ip->lineno;
3281 temp->fname = ip->fname;
3282 temp->type = kt->type;
3283 break;
3284 case T_ELSE:
3285 case T_ENDIF:
3286 case T_ELIF:
3287 if (if_stack == instack[indepth].if_stack) {
3288 error ("#%s not within a conditional", kt->name);
3289 break;
3290 }
3291 else if (if_stack == save_if_stack)
3292 return; /* found what we came for */
3293
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;
3298 break;
3299 }
3300
3301 temp = if_stack;
3302 if_stack = if_stack->next;
3303 free (temp);
3304 break;
3305
3306 default:
3307 /* Anything else is ignored. */
3308 break;
3309 }
3310 break;
3311 }
3312 }
3313 }
3314 }
3315 ip->bufp = bp;
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. */
3319 }
3320
3321 /*
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.
3326 */
3327 static void
3328 do_else (buf, limit, op, keyword)
3329 U_CHAR *buf ATTRIBUTE_UNUSED;
3330 U_CHAR *limit ATTRIBUTE_UNUSED;
3331 FILE_BUF *op;
3332 struct directive *keyword ATTRIBUTE_UNUSED;
3333 {
3334 FILE_BUF *ip = &instack[indepth];
3335
3336 if (if_stack == instack[indepth].if_stack) {
3337 error ("#else not within a conditional");
3338 return;
3339 } else {
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");
3346 }
3347 if_stack->type = T_ELSE;
3348 }
3349
3350 if (if_stack->if_succeeded)
3351 skip_if_group (ip, 0);
3352 else {
3353 ++if_stack->if_succeeded; /* continue processing input */
3354 output_line_command (ip, op, 1, same_file);
3355 }
3356 }
3357
3358 /*
3359 * unstack after #endif command
3360 */
3361 static void
3362 do_endif (buf, limit, op, keyword)
3363 U_CHAR *buf ATTRIBUTE_UNUSED;
3364 U_CHAR *limit ATTRIBUTE_UNUSED;
3365 FILE_BUF *op;
3366 struct directive *keyword ATTRIBUTE_UNUSED;
3367 {
3368 if (if_stack == instack[indepth].if_stack)
3369 error ("unbalanced #endif");
3370 else {
3371 IF_STACK_FRAME *temp = if_stack;
3372 if_stack = if_stack->next;
3373 free (temp);
3374 output_line_command (&instack[indepth], op, 1, same_file);
3375 }
3376 }
3377
3378 /*
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.
3384 */
3385 static U_CHAR *
3386 skip_to_end_of_comment (ip, line_counter)
3387 register FILE_BUF *ip;
3388 int *line_counter; /* place to remember newlines, or NULL */
3389 {
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;
3394
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 */
3398 if (output) {
3399 *op->bufp++ = '/';
3400 *op->bufp++ = '*';
3401 }
3402 while (bp < limit) {
3403 if (output)
3404 *op->bufp++ = *bp;
3405 switch (*bp++) {
3406 case '/':
3407 if (warn_comments && bp < limit && *bp == '*')
3408 warning("`/*' within comment");
3409 break;
3410 case '\n':
3411 if (line_counter != NULL)
3412 ++*line_counter;
3413 if (output)
3414 ++op->lineno;
3415 break;
3416 case '*':
3417 if (*bp == '\\' && bp[1] == '\n')
3418 newline_fix (bp);
3419 if (*bp == '/') {
3420 if (output)
3421 *op->bufp++ = '/';
3422 ip->bufp = ++bp;
3423 return bp;
3424 }
3425 break;
3426 }
3427 }
3428 ip->bufp = bp;
3429 return bp;
3430 }
3431
3432 /*
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).
3437 *
3438 * The input stack state is not changed.
3439 *
3440 * If COUNT_NEWLINES is nonzero, it points to an int to increment
3441 * for each newline passed.
3442 *
3443 * If BACKSLASH_NEWLINES_P is nonzero, store 1 thru it
3444 * if we pass a backslash-newline.
3445 *
3446 * If EOFP is nonzero, set *EOFP to 1 if the string is unterminated.
3447 */
3448 static U_CHAR *
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;
3452 int start_line;
3453 int *count_newlines;
3454 int *backslash_newlines_p;
3455 int *eofp;
3456 {
3457 register U_CHAR c, match;
3458
3459 match = *bp++;
3460 while (1) {
3461 if (bp >= limit) {
3462 error_with_line (line_for_error (start_line),
3463 "unterminated string or character constant");
3464 if (eofp)
3465 *eofp = 1;
3466 break;
3467 }
3468 c = *bp++;
3469 if (c == '\\') {
3470 while (*bp == '\\' && bp[1] == '\n') {
3471 if (backslash_newlines_p)
3472 *backslash_newlines_p = 1;
3473 if (count_newlines)
3474 ++*count_newlines;
3475 bp += 2;
3476 }
3477 if (*bp == '\n' && count_newlines) {
3478 if (backslash_newlines_p)
3479 *backslash_newlines_p = 1;
3480 ++*count_newlines;
3481 }
3482 bp++;
3483 } else if (c == '\n') {
3484 /* Unterminated strings and character constants are 'legal'. */
3485 bp--; /* Don't consume the newline. */
3486 if (eofp)
3487 *eofp = 1;
3488 break;
3489 } else if (c == match)
3490 break;
3491 }
3492 return (U_CHAR *) bp;
3493 }
3494 \f
3495 /*
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.
3501 */
3502
3503 static void
3504 output_line_command (ip, op, conditional, file_change)
3505 FILE_BUF *ip, *op;
3506 int conditional;
3507 enum file_change_code file_change;
3508 {
3509 int len;
3510 char line_cmd_buf[500];
3511
3512 if (no_line_commands
3513 || ip->fname == NULL
3514 || no_output) {
3515 op->lineno = ip->lineno;
3516 return;
3517 }
3518
3519 if (conditional) {
3520 if (ip->lineno == op->lineno)
3521 return;
3522
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) {
3528 *op->bufp++ = '\n';
3529 op->lineno++;
3530 }
3531 return;
3532 }
3533 }
3534
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')
3542 *op->bufp++ = '\n';
3543 memcpy (op->bufp, line_cmd_buf, len);
3544 op->bufp += len;
3545 op->lineno = ip->lineno;
3546 }
3547 \f
3548
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.
3553
3554 If macro wants arguments, caller has already verified that
3555 an argument list follows; arguments come from the input stack. */
3556
3557 static void
3558 macroexpand (hp, op)
3559 HASHNODE *hp;
3560 FILE_BUF *op;
3561 {
3562 int nargs;
3563 DEFINITION *defn = hp->value.defn;
3564 register U_CHAR *xbuf;
3565 int xbuf_len;
3566 int start_line = instack[indepth].lineno;
3567
3568 CHECK_DEPTH (return;);
3569
3570 /* it might not actually be a macro. */
3571 if (hp->type != T_MACRO) {
3572 special_symbol (hp, op);
3573 return;
3574 }
3575
3576 nargs = defn->nargs;
3577
3578 if (nargs >= 0) {
3579 register int i;
3580 struct argdata *args;
3581 const char *parse_error = 0;
3582
3583 args = (struct argdata *) alloca ((nargs + 1) * sizeof (struct argdata));
3584
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;
3590 }
3591
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. */
3595 i = 0;
3596 do {
3597 /* Discard the open-parenthesis or comma before the next arg. */
3598 ++instack[indepth].bufp;
3599 parse_error
3600 = macarg ((i < nargs || (nargs == 0 && i == 0)) ? &args[i] : 0);
3601 if (parse_error)
3602 {
3603 error_with_line (line_for_error (start_line), parse_error);
3604 break;
3605 }
3606 i++;
3607 } while (*instack[indepth].bufp != ')');
3608
3609 /* If we got one arg but it was just whitespace, call that 0 args. */
3610 if (i == 1) {
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++;
3614 if (bp == lim)
3615 i = 0;
3616 }
3617
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)
3623 ;
3624 else if (i == 0)
3625 error ("no args to macro `%s'", hp->name);
3626 else if (i == 1)
3627 error ("only 1 arg to macro `%s'", hp->name);
3628 else
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);
3632
3633 /* Swallow the closeparen. */
3634 ++instack[indepth].bufp;
3635
3636 /* If macro wants zero args, we parsed the arglist for checking only.
3637 Read directly from the macro definition. */
3638 if (nargs == 0) {
3639 xbuf = defn->expansion;
3640 xbuf_len = defn->length;
3641 } else {
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 */
3646
3647 register struct reflist *ap;
3648
3649 /* Macro really takes args. Compute the expansion of this call. */
3650
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) {
3654 if (ap->stringify)
3655 xbuf_len += args[ap->argno].stringified_length;
3656 else
3657 xbuf_len += args[ap->argno].raw_length;
3658 }
3659
3660 xbuf = (U_CHAR *) xmalloc (xbuf_len + 1);
3661
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];
3670
3671 for (i = 0; i < ap->nchars; i++)
3672 xbuf[totlen++] = exp[offset++];
3673
3674 if (ap->stringify != 0) {
3675 int arglen = arg->raw_length;
3676 int escaped = 0;
3677 int in_string = 0;
3678 int c;
3679 i = 0;
3680 while (i < arglen
3681 && (c = arg->raw[i], is_space[c]))
3682 i++;
3683 while (i < arglen
3684 && (c = arg->raw[arglen - 1], is_space[c]))
3685 arglen--;
3686 for (; i < arglen; i++) {
3687 c = arg->raw[i];
3688
3689 /* Special markers Newline Space
3690 generate nothing for a stringified argument. */
3691 if (c == '\n' && arg->raw[i+1] != '\n') {
3692 i++;
3693 continue;
3694 }
3695
3696 /* Internal sequences of whitespace are replaced by one space
3697 except within an string or char token. */
3698 if (! in_string
3699 && (c == '\n' ? arg->raw[i+1] == '\n' : is_space[c])) {
3700 while (1) {
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]])
3704 i += 2;
3705 else if (c != '\n' && is_space[c])
3706 i++;
3707 else break;
3708 c = arg->raw[i];
3709 }
3710 i--;
3711 c = ' ';
3712 }
3713
3714 if (escaped)
3715 escaped = 0;
3716 else {
3717 if (c == '\\')
3718 escaped = 1;
3719 if (in_string) {
3720 if (c == in_string)
3721 in_string = 0;
3722 } else if (c == '\"' || c == '\'')
3723 in_string = c;
3724 }
3725
3726 /* Escape these chars */
3727 if (c == '\"' || (in_string && c == '\\'))
3728 xbuf[totlen++] = '\\';
3729 if (ISPRINT (c))
3730 xbuf[totlen++] = c;
3731 else {
3732 sprintf ((char *) &xbuf[totlen], "\\%03o", (unsigned int) c);
3733 totlen += 4;
3734 }
3735 }
3736 } else {
3737 const U_CHAR *p1 = arg->raw;
3738 const U_CHAR *l1 = p1 + arg->raw_length;
3739
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] == '-')
3748 p1 += 2;
3749 }
3750 if (ap->raw_after) {
3751 /* Arg is concatenated after: delete trailing whitespace,
3752 whitespace markers, and no-reexpansion markers. */
3753 while (p1 != l1) {
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) {
3761 l1 -= 2;
3762 }
3763 else break;
3764 }
3765 else break;
3766 }
3767 }
3768 memmove (xbuf + totlen, p1, l1 - p1);
3769 totlen += l1 - p1;
3770 }
3771
3772 if (totlen > xbuf_len)
3773 abort ();
3774 }
3775
3776 /* if there is anything left of the definition
3777 after handling the arg list, copy that in too. */
3778
3779 for (i = offset; i < defn->length; i++)
3780 xbuf[totlen++] = exp[i];
3781
3782 xbuf[totlen] = 0;
3783 xbuf_len = totlen;
3784
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);
3790 }
3791 }
3792 } else {
3793 xbuf = defn->expansion;
3794 xbuf_len = defn->length;
3795 }
3796
3797 /* Now put the expansion on the input stack
3798 so our caller will commence reading from it. */
3799 {
3800 register FILE_BUF *ip2;
3801
3802 ip2 = &instack[++indepth];
3803
3804 ip2->fname = 0;
3805 ip2->lineno = 0;
3806 ip2->buf = xbuf;
3807 ip2->length = xbuf_len;
3808 ip2->bufp = xbuf;
3809 ip2->free_ptr = (nargs > 0) ? xbuf : 0;
3810 ip2->macro = hp;
3811 ip2->if_stack = if_stack;
3812 }
3813 }
3814 \f
3815 /*
3816 * Parse a macro argument and store the info on it into *ARGPTR.
3817 * Return nonzero to indicate a syntax error.
3818 */
3819
3820 static const char *
3821 macarg (argptr)
3822 register struct argdata *argptr;
3823 {
3824 FILE_BUF *ip = &instack[indepth];
3825 int paren = 0;
3826 int newlines = 0;
3827 int comments = 0;
3828
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);
3833
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) {
3838 if (argptr != 0) {
3839 argptr->raw = ip->bufp;
3840 argptr->raw_length = bp - ip->bufp;
3841 }
3842 ip->bufp = bp;
3843 } else {
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;
3852
3853 memcpy (buffer, ip->bufp, bufsize);
3854 ip->bufp = bp;
3855 ip->lineno += newlines;
3856
3857 while (bp == ip->buf + ip->length) {
3858 if (instack[indepth].macro == 0) {
3859 free (buffer);
3860 return "unterminated macro call";
3861 }
3862 ip->macro->type = T_MACRO;
3863 if (ip->free_ptr)
3864 free (ip->free_ptr);
3865 ip = &instack[--indepth];
3866 newlines = 0;
3867 comments = 0;
3868 bp = macarg1 (ip->bufp, ip->buf + ip->length, &paren,
3869 &newlines, &comments);
3870 final_start = bufsize;
3871 bufsize += bp - ip->bufp;
3872 extra += newlines;
3873 buffer = (U_CHAR *) xrealloc (buffer, bufsize + extra + 1);
3874 memcpy (buffer + bufsize - (bp - ip->bufp), ip->bufp, bp - ip->bufp);
3875 ip->bufp = bp;
3876 ip->lineno += newlines;
3877 }
3878
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. */
3884 if (argptr != 0) {
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)
3891 argptr->raw_length
3892 = final_start +
3893 discard_comments (argptr->raw + final_start,
3894 argptr->raw_length - final_start,
3895 newlines);
3896 argptr->raw[argptr->raw_length] = 0;
3897 if (argptr->raw_length > bufsize + extra)
3898 abort ();
3899 }
3900 }
3901
3902 /* If we are not discarding this argument,
3903 macroexpand it and compute its length as stringified.
3904 All this info goes into *ARGPTR. */
3905
3906 if (argptr != 0) {
3907 FILE_BUF obuf;
3908 register const U_CHAR *buf, *lim;
3909 register int totlen;
3910
3911 obuf = expand_to_temp_buffer (argptr->raw,
3912 argptr->raw + argptr->raw_length,
3913 1);
3914
3915 argptr->expanded = obuf.buf;
3916 argptr->expand_length = obuf.length;
3917 argptr->free2 = obuf.buf;
3918
3919 buf = argptr->raw;
3920 lim = buf + argptr->raw_length;
3921
3922 totlen = 0;
3923 while (buf != lim) {
3924 register U_CHAR c = *buf++;
3925 totlen++;
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 */
3930 totlen++;
3931 else if (!ISPRINT (c))
3932 totlen += 3;
3933 }
3934 argptr->stringified_length = totlen;
3935 }
3936 return 0;
3937 }
3938
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.
3947
3948 Increment *NEWLINES each time a newline is passed.
3949 Set *COMMENTS to 1 if a comment is seen. */
3950
3951 static U_CHAR *
3952 macarg1 (start, limit, depthptr, newlines, comments)
3953 U_CHAR *start;
3954 register const U_CHAR *limit;
3955 int *depthptr, *newlines, *comments;
3956 {
3957 register U_CHAR *bp = start;
3958
3959 while (bp < limit) {
3960 switch (*bp) {
3961 case '(':
3962 (*depthptr)++;
3963 break;
3964 case ')':
3965 if (--(*depthptr) < 0)
3966 return bp;
3967 break;
3968 case '\\':
3969 /* Traditionally, backslash makes following char not special. */
3970 if (bp + 1 < limit)
3971 {
3972 bp++;
3973 /* But count source lines anyway. */
3974 if (*bp == '\n')
3975 ++*newlines;
3976 }
3977 break;
3978 case '\n':
3979 ++*newlines;
3980 break;
3981 case '/':
3982 if (bp[1] == '\\' && bp[2] == '\n')
3983 newline_fix (bp + 1);
3984 if (bp[1] != '*' || bp + 1 >= limit)
3985 break;
3986 *comments = 1;
3987 bp += 2;
3988 while (bp + 1 < limit) {
3989 if (bp[0] == '*'
3990 && bp[1] == '\\' && bp[2] == '\n')
3991 newline_fix (bp + 1);
3992 if (bp[0] == '*' && bp[1] == '/')
3993 break;
3994 if (*bp == '\n') ++*newlines;
3995 bp++;
3996 }
3997 bp += 1;
3998 break;
3999 case '\'':
4000 case '\"':
4001 {
4002 int quotec;
4003 for (quotec = *bp++; bp + 1 < limit && *bp != quotec; bp++) {
4004 if (*bp == '\\') {
4005 bp++;
4006 if (*bp == '\n')
4007 ++*newlines;
4008 while (*bp == '\\' && bp[1] == '\n') {
4009 bp += 2;
4010 }
4011 } else if (*bp == '\n') {
4012 ++*newlines;
4013 if (quotec == '\'')
4014 break;
4015 }
4016 }
4017 }
4018 break;
4019 case ',':
4020 if ((*depthptr) == 0)
4021 return bp;
4022 break;
4023 }
4024 bp++;
4025 }
4026
4027 return bp;
4028 }
4029
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.
4034
4035 NEWLINES is the number of newlines that must be duplicated.
4036 We assume that that much extra space is available past the end
4037 of the string. */
4038
4039 static int
4040 discard_comments (start, length, newlines)
4041 U_CHAR *start;
4042 int length;
4043 int newlines;
4044 {
4045 register U_CHAR *ibp;
4046 register U_CHAR *obp;
4047 register const U_CHAR *limit;
4048 register int c;
4049
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
4053 while copying down.
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. */
4057 if (newlines > 0) {
4058 ibp = start + length;
4059 obp = ibp + newlines;
4060 limit = start;
4061 while (limit != ibp)
4062 *--obp = *--ibp;
4063 }
4064
4065 ibp = start + newlines;
4066 limit = start + length + newlines;
4067 obp = start;
4068
4069 while (ibp < limit) {
4070 *obp++ = c = *ibp++;
4071 switch (c) {
4072 case '\n':
4073 /* Duplicate the newline. */
4074 *obp++ = '\n';
4075 break;
4076
4077 case '\\':
4078 if (*ibp == '\n') {
4079 obp--;
4080 ibp++;
4081 }
4082 break;
4083
4084 case '/':
4085 if (*ibp == '\\' && ibp[1] == '\n')
4086 newline_fix (ibp);
4087 /* Delete any comment. */
4088 if (ibp[0] != '*' || ibp + 1 >= limit)
4089 break;
4090 obp--;
4091 ibp++;
4092 while (ibp + 1 < limit) {
4093 if (ibp[0] == '*'
4094 && ibp[1] == '\\' && ibp[2] == '\n')
4095 newline_fix (ibp + 1);
4096 if (ibp[0] == '*' && ibp[1] == '/')
4097 break;
4098 ibp++;
4099 }
4100 ibp += 2;
4101 break;
4102
4103 case '\'':
4104 case '\"':
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. */
4108 {
4109 int quotec = c;
4110 while (ibp < limit) {
4111 *obp++ = c = *ibp++;
4112 if (c == quotec)
4113 break;
4114 if (c == '\n' && quotec == '\'')
4115 break;
4116 if (c == '\\' && ibp < limit) {
4117 while (*ibp == '\\' && ibp[1] == '\n')
4118 ibp += 2;
4119 *obp++ = *ibp++;
4120 }
4121 }
4122 }
4123 break;
4124 }
4125 }
4126
4127 return obp - start;
4128 }
4129 \f
4130
4131 /* Core error handling routine. */
4132 static void
4133 v_message (mtype, line, msgid, ap)
4134 enum msgtype mtype;
4135 int line;
4136 const char *msgid;
4137 va_list ap;
4138 {
4139 const char *fname = 0;
4140 int i;
4141
4142 if (mtype == WARNING && inhibit_warnings)
4143 return;
4144
4145 for (i = indepth; i >= 0; i--)
4146 if (instack[i].fname != NULL) {
4147 if (line == 0)
4148 line = instack[i].lineno;
4149 fname = instack[i].fname;
4150 break;
4151 }
4152
4153 if (fname)
4154 fprintf (stderr, "%s:%d: ", fname, line);
4155 else
4156 fprintf (stderr, "%s: ", progname);
4157
4158 if (mtype == WARNING)
4159 fputs ("warning: ", stderr);
4160
4161 vfprintf (stderr, msgid, ap);
4162 putc ('\n', stderr);
4163
4164 if (mtype == ERROR)
4165 errors++;
4166 }
4167
4168 /*
4169 * error - print error message and increment count of errors.
4170 */
4171 void
4172 error VPARAMS ((const char *msgid, ...))
4173 {
4174 #ifndef ANSI_PROTOTYPES
4175 const char *msgid;
4176 #endif
4177 va_list ap;
4178
4179 VA_START(ap, msgid);
4180
4181 #ifndef ANSI_PROTOTYPES
4182 msgid = va_arg (ap, const char *);
4183 #endif
4184
4185 v_message (ERROR, 0, msgid, ap);
4186 }
4187
4188 void
4189 error_with_line VPARAMS ((int line, const char *msgid, ...))
4190 {
4191 #ifndef ANSI_PROTOTYPES
4192 int line;
4193 const char *msgid;
4194 #endif
4195 va_list ap;
4196
4197 VA_START(ap, msgid);
4198
4199 #ifndef ANSI_PROTOTYPES
4200 line = va_arg (ap, int);
4201 msgid = va_arg (ap, const char *);
4202 #endif
4203
4204 v_message (ERROR, line, msgid, ap);
4205 }
4206
4207 /* Error including a message from `errno'. */
4208 void
4209 error_from_errno (name)
4210 const char *name;
4211 {
4212 error ("%s: %s", name, strerror (errno));
4213 }
4214
4215 /* Print error message but don't count it. */
4216 void
4217 warning VPARAMS ((const char *msgid, ...))
4218 {
4219 #ifndef ANSI_PROTOTYPES
4220 const char *msgid;
4221 #endif
4222 va_list ap;
4223
4224 VA_START(ap, msgid);
4225
4226 #ifndef ANSI_PROTOTYPES
4227 msgid = va_arg (ap, const char *);
4228 #endif
4229
4230 v_message (WARNING, 0, msgid, ap);
4231 }
4232
4233 void
4234 fatal VPARAMS ((const char *msgid, ...))
4235 {
4236 #ifndef ANSI_PROTOTYPES
4237 const char *msgid;
4238 #endif
4239 va_list ap;
4240
4241 VA_START(ap, msgid);
4242
4243 #ifndef ANSI_PROTOTYPES
4244 msgid = va_arg (ap, const char *);
4245 #endif
4246
4247 v_message (FATAL, 0, msgid, ap);
4248 exit (FATAL_EXIT_CODE);
4249 }
4250
4251 /* More 'friendly' abort that prints the location at which we died. */
4252 void
4253 fancy_abort (line, func)
4254 int line;
4255 const char *func;
4256 {
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);
4260 }
4261
4262 void
4263 perror_with_name (name)
4264 const char *name;
4265 {
4266 fprintf (stderr, "%s: %s: %s\n", progname, name, strerror (errno));
4267 errors++;
4268 }
4269
4270 void
4271 pfatal_with_name (name)
4272 const char *name;
4273 {
4274 perror_with_name (name);
4275 exit (FATAL_EXIT_CODE);
4276 }
4277
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. */
4285 static int
4286 line_for_error (line)
4287 int line;
4288 {
4289 int i;
4290 int line1 = line;
4291
4292 for (i = indepth; i >= 0; ) {
4293 if (instack[i].fname != 0)
4294 return line1;
4295 i--;
4296 if (i < 0)
4297 return 0;
4298 line1 = instack[i].lineno;
4299 }
4300 return 0;
4301 }
4302
4303 /*
4304 * If OBUF doesn't have NEEDED bytes after OPTR, make it bigger.
4305 *
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,
4309 * should work ok.
4310 */
4311
4312 static void
4313 grow_outbuf (obuf, needed)
4314 register FILE_BUF *obuf;
4315 register int needed;
4316 {
4317 register U_CHAR *p;
4318 int minsize;
4319
4320 if (obuf->length - (obuf->bufp - obuf->buf) > needed)
4321 return;
4322
4323 /* Make it at least twice as big as it is now. */
4324 obuf->length *= 2;
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;
4329
4330 p = (U_CHAR *) xrealloc (obuf->buf, obuf->length);
4331 obuf->bufp = p + (obuf->bufp - obuf->buf);
4332 obuf->buf = p;
4333 }
4334 \f
4335 /* Symbol table for macro names and special symbols */
4336
4337 /*
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.
4344 *
4345 * If LEN is >= 0, it is the length of the name.
4346 * Otherwise, compute the length by scanning the entire name.
4347 *
4348 * If HASH is >= 0, it is the precomputed hash code.
4349 * Otherwise, compute the hash code.
4350 *
4351 * caller must set the value, if any is desired.
4352 */
4353 static HASHNODE *
4354 install (name, len, type, hash)
4355 const U_CHAR *name;
4356 int len;
4357 enum node_type type;
4358 int hash;
4359 /* watch out here if sizeof (U_CHAR *) != sizeof (int) */
4360 {
4361 register HASHNODE *hp;
4362 register int bucket;
4363 register const U_CHAR *p;
4364 U_CHAR *q;
4365
4366 if (len < 0) {
4367 p = name;
4368 while (is_idchar[*p])
4369 p++;
4370 len = p - name;
4371 }
4372
4373 if (hash < 0)
4374 hash = hashf (name, len, HASHSIZE);
4375
4376 hp = (HASHNODE *) xmalloc (sizeof (HASHNODE) + len + 1);
4377 bucket = hash;
4378 hp->bucket_hdr = &hashtab[bucket];
4379 hp->next = hashtab[bucket];
4380 hashtab[bucket] = hp;
4381 hp->prev = NULL;
4382 if (hp->next != NULL)
4383 hp->next->prev = hp;
4384 hp->type = type;
4385 hp->length = len;
4386 hp->name = q = ((U_CHAR *) hp) + sizeof (HASHNODE);
4387 memcpy (q, name, len);
4388 q[len] = 0;
4389 return hp;
4390 }
4391
4392 /*
4393 * find the most recent hash node for name name (ending with first
4394 * non-identifier char) installed by install
4395 *
4396 * If LEN is >= 0, it is the length of the name.
4397 * Otherwise, compute the length by scanning the entire name.
4398 *
4399 * If HASH is >= 0, it is the precomputed hash code.
4400 * Otherwise, compute the hash code.
4401 */
4402 HASHNODE *
4403 lookup (name, len, hash)
4404 const U_CHAR *name;
4405 int len;
4406 int hash;
4407 {
4408 register const U_CHAR *bp;
4409 register HASHNODE *bucket;
4410
4411 if (len < 0) {
4412 for (bp = name; is_idchar[*bp]; bp++) ;
4413 len = bp - name;
4414 }
4415
4416 if (hash < 0)
4417 hash = hashf (name, len, HASHSIZE);
4418
4419 bucket = hashtab[hash];
4420 while (bucket) {
4421 if (bucket->length == len
4422 && strncmp ((const char *)bucket->name, (const char *)name, len) == 0)
4423 return bucket;
4424 bucket = bucket->next;
4425 }
4426 return NULL;
4427 }
4428
4429 /*
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.
4433 */
4434
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. */
4442 static void
4443 delete_macro (hp)
4444 HASHNODE *hp;
4445 {
4446
4447 if (hp->prev != NULL)
4448 hp->prev->next = hp->next;
4449 if (hp->next != NULL)
4450 hp->next->prev = hp->prev;
4451
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;
4456
4457 free (hp);
4458 }
4459
4460 /*
4461 * return hash function on name. must be compatible with the one
4462 * computed a step at a time, elsewhere
4463 */
4464 static int
4465 hashf (name, len, hashsize)
4466 register const U_CHAR *name;
4467 register int len;
4468 int hashsize;
4469 {
4470 register int r = 0;
4471
4472 while (len--)
4473 r = HASHSTEP (r, *name++);
4474
4475 return MAKE_POS (r) % hashsize;
4476 }
4477 \f
4478 /* Dump all macro definitions as #defines to stdout. */
4479
4480 static void
4481 dump_all_macros ()
4482 {
4483 int bucket;
4484
4485 for (bucket = 0; bucket < HASHSIZE; bucket++) {
4486 register HASHNODE *hp;
4487
4488 for (hp = hashtab[bucket]; hp; hp= hp->next) {
4489 if (hp->type == T_MACRO) {
4490 register DEFINITION *defn = hp->value.defn;
4491 struct reflist *ap;
4492 int offset;
4493 int concat;
4494
4495
4496 /* Print the definition of the macro HP. */
4497
4498 printf ("#define %s", hp->name);
4499 if (defn->nargs >= 0) {
4500 int i;
4501
4502 printf ("(");
4503 for (i = 0; i < defn->nargs; i++) {
4504 dump_arg_n (defn, i);
4505 if (i + 1 < defn->nargs)
4506 printf (", ");
4507 }
4508 printf (")");
4509 }
4510
4511 printf (" ");
4512
4513 offset = 0;
4514 concat = 0;
4515 for (ap = defn->pattern; ap != NULL; ap = ap->next) {
4516 dump_defn_1 (defn->expansion, offset, ap->nchars);
4517 if (ap->nchars != 0)
4518 concat = 0;
4519 offset += ap->nchars;
4520 if (ap->stringify)
4521 printf (" #");
4522 if (ap->raw_before && !concat)
4523 printf (" ## ");
4524 concat = 0;
4525 dump_arg_n (defn, ap->argno);
4526 if (ap->raw_after) {
4527 printf (" ## ");
4528 concat = 1;
4529 }
4530 }
4531 dump_defn_1 (defn->expansion, offset, defn->length - offset);
4532 printf ("\n");
4533 }
4534 }
4535 }
4536 }
4537
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. */
4543 static void
4544 dump_defn_1 (base, start, length)
4545 const U_CHAR *base;
4546 int start;
4547 int length;
4548 {
4549 const U_CHAR *p = base + start;
4550 const U_CHAR *limit = base + start + length;
4551
4552 while (p < limit) {
4553 if (*p != '\n')
4554 putchar (*p);
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);
4558 p = p1 - 1;
4559 }
4560 p++;
4561 }
4562 }
4563
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. */
4567 static void
4568 dump_arg_n (defn, argnum)
4569 DEFINITION *defn;
4570 int argnum;
4571 {
4572 register const U_CHAR *p = defn->argnames;
4573 while (argnum + 1 < defn->nargs) {
4574 p = (const U_CHAR *) strchr ((const char *)p, ' ') + 1;
4575 argnum++;
4576 }
4577
4578 while (*p && *p != ',') {
4579 putchar (*p);
4580 p++;
4581 }
4582 }
4583 \f
4584 /* Initialize syntactic classifications of characters. */
4585 static void
4586 initialize_char_syntax ()
4587 {
4588 register int i;
4589
4590 /*
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
4594 * refer to them.
4595 */
4596 for (i = 'a'; i <= 'z'; i++) {
4597 is_idchar[i - 'a' + 'A'] = 1;
4598 is_idchar[i] = 1;
4599 is_idstart[i - 'a' + 'A'] = 1;
4600 is_idstart[i] = 1;
4601 }
4602 for (i = '0'; i <= '9'; i++)
4603 is_idchar[i] = 1;
4604 is_idchar['_'] = 1;
4605 is_idstart['_'] = 1;
4606
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;
4613
4614 is_space[' '] = 1;
4615 is_space['\t'] = 1;
4616 is_space['\v'] = 1;
4617 is_space['\f'] = 1;
4618 is_space['\n'] = 1;
4619 is_space['\r'] = 1;
4620 }
4621
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;
4628 static void
4629 initialize_builtins ()
4630 {
4631 HASHNODE *hp;
4632
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);
4640
4641 #ifndef NO_BUILTIN_SIZE_TYPE
4642 install_value ("__SIZE_TYPE__", SIZE_TYPE);
4643 #endif
4644 #ifndef NO_BUILTIN_PTRDIFF_TYPE
4645 install_value ("__PTRDIFF_TYPE__", PTRDIFF_TYPE);
4646 #endif
4647 #ifndef NO_BUILTIN_WCHAR_TYPE
4648 install_value ("__WCHAR_TYPE__", WCHAR_TYPE);
4649 #endif
4650 #ifndef NO_BUILTIN_WINT_TYPE
4651 install_value ("__WINT_TYPE__", WINT_TYPE);
4652 #endif
4653 install_value ("__REGISTER_PREFIX__", REGISTER_PREFIX);
4654 install_value ("__USER_LABEL_PREFIX__", user_label_prefix);
4655 }
4656 #undef DSC
4657 #undef install_spec
4658 #undef install_value
4659 \f
4660 /*
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.
4665 */
4666 static void
4667 make_definition (str)
4668 const U_CHAR *str;
4669 {
4670 FILE_BUF *ip;
4671 struct directive *kt;
4672 U_CHAR *buf;
4673 const U_CHAR *p;
4674 size_t len = strlen ((const char *)str);
4675
4676 p = (const U_CHAR *) strchr ((const char *)str, '=');
4677 if (p == NULL) {
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);
4682 len += 2;
4683 } else {
4684 buf = (U_CHAR *) alloca (len + 1);
4685 memcpy (buf, str, len + 1);
4686 buf[p - str] = ' ';
4687 }
4688
4689 ip = &instack[++indepth];
4690 ip->fname = "*Initialization*";
4691
4692 ip->buf = ip->bufp = buf;
4693 ip->length = len;
4694 ip->lineno = 1;
4695 ip->macro = 0;
4696 ip->free_ptr = 0;
4697 ip->if_stack = if_stack;
4698
4699 for (kt = directive_table; kt->type != T_DEFINE; kt++)
4700 ;
4701
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);
4705 --indepth;
4706 }
4707
4708 /* JF, this does the work for the -U option */
4709 static void
4710 make_undef (str)
4711 U_CHAR *str;
4712 {
4713 FILE_BUF *ip;
4714 struct directive *kt;
4715
4716 ip = &instack[++indepth];
4717 ip->fname = "*undef*";
4718
4719 ip->buf = ip->bufp = str;
4720 ip->length = strlen ((const char *)str);
4721 ip->lineno = 1;
4722 ip->macro = 0;
4723 ip->free_ptr = 0;
4724 ip->if_stack = if_stack;
4725
4726 for (kt = directive_table; kt->type != T_UNDEF; kt++)
4727 ;
4728
4729 do_undef (str, str + ip->length, NULL, kt);
4730 --indepth;
4731 }
4732 \f
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. */
4737 static void
4738 deps_output (string, size)
4739 const char *string;
4740 int size;
4741 {
4742 #ifndef MAX_OUTPUT_COLUMNS
4743 #define MAX_OUTPUT_COLUMNS 75
4744 #endif
4745 if (size != 0 && deps_column != 0
4746 && size + deps_column > MAX_OUTPUT_COLUMNS) {
4747 deps_output ("\\\n ", 0);
4748 deps_column = 0;
4749 }
4750
4751 if (size == 0)
4752 size = strlen (string);
4753
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);
4758 }
4759 memcpy (&deps_buffer[deps_size], string, size);
4760 deps_size += size;
4761 deps_column += size;
4762 deps_buffer[deps_size] = 0;
4763 }
4764
4765 /* Get the file-mode and data size of the file open on FD
4766 and store them in *MODE_POINTER and *SIZE_POINTER. */
4767
4768 static int
4769 file_size_and_mode (fd, mode_pointer, size_pointer)
4770 int fd;
4771 int *mode_pointer;
4772 long *size_pointer;
4773 {
4774 struct stat sbuf;
4775
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;
4779 return 0;
4780 }