Fix `make bootstrap' failures where libraries are compiled with wrong compiler.
[gcc.git] / gcc / cpplib.c
1 /* CPP Library.
2 Copyright (C) 1986, 87, 89, 92-97, 1998 Free Software Foundation, Inc.
3 Contributed by Per Bothner, 1994-95.
4 Based on CCCP program by Paul Rubin, June 1986
5 Adapted to ANSI C, Richard Stallman, Jan 1987
6
7 This program is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 2, or (at your option) any
10 later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
20
21 #include "config.h"
22 #include "system.h"
23
24 #ifndef STDC_VALUE
25 #define STDC_VALUE 1
26 #endif
27
28 #include <signal.h>
29
30 #ifdef HAVE_SYS_TIMES_H
31 #include <sys/times.h>
32 #endif
33
34 #ifdef HAVE_SYS_RESOURCE_H
35 # include <sys/resource.h>
36 #endif
37
38 #include "gansidecl.h"
39 #include "cpplib.h"
40 #include "cpphash.h"
41
42 #ifndef GET_ENV_PATH_LIST
43 #define GET_ENV_PATH_LIST(VAR,NAME) do { (VAR) = getenv (NAME); } while (0)
44 #endif
45
46 extern char *update_path PARAMS ((char *, char *));
47
48 #undef MIN
49 #undef MAX
50 #define MIN(X,Y) ((X) < (Y) ? (X) : (Y))
51 #define MAX(X,Y) ((X) > (Y) ? (X) : (Y))
52
53 /* Find the largest host integer type and set its size and type.
54 Watch out: on some crazy hosts `long' is shorter than `int'. */
55
56 #ifndef HOST_WIDE_INT
57 # if HAVE_INTTYPES_H
58 # include <inttypes.h>
59 # define HOST_WIDE_INT intmax_t
60 # else
61 # if (HOST_BITS_PER_LONG <= HOST_BITS_PER_INT \
62 && HOST_BITS_PER_LONGLONG <= HOST_BITS_PER_INT)
63 # define HOST_WIDE_INT int
64 # else
65 # if (HOST_BITS_PER_LONGLONG <= HOST_BITS_PER_LONG \
66 || ! (defined LONG_LONG_MAX || defined LLONG_MAX))
67 # define HOST_WIDE_INT long
68 # else
69 # define HOST_WIDE_INT long long
70 # endif
71 # endif
72 # endif
73 #endif
74
75 #ifndef S_ISREG
76 #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
77 #endif
78
79 #ifndef S_ISDIR
80 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
81 #endif
82
83 /* By default, colon separates directories in a path. */
84 #ifndef PATH_SEPARATOR
85 #define PATH_SEPARATOR ':'
86 #endif
87
88 #ifndef STANDARD_INCLUDE_DIR
89 #define STANDARD_INCLUDE_DIR "/usr/include"
90 #endif
91 #ifndef INCLUDE_LEN_FUDGE
92 #define INCLUDE_LEN_FUDGE 0
93 #endif
94
95 /* Symbols to predefine. */
96
97 #ifdef CPP_PREDEFINES
98 static char *predefs = CPP_PREDEFINES;
99 #else
100 static char *predefs = "";
101 #endif
102 \f
103 /* We let tm.h override the types used here, to handle trivial differences
104 such as the choice of unsigned int or long unsigned int for size_t.
105 When machines start needing nontrivial differences in the size type,
106 it would be best to do something here to figure out automatically
107 from other information what type to use. */
108
109 /* The string value for __SIZE_TYPE__. */
110
111 #ifndef SIZE_TYPE
112 #define SIZE_TYPE "long unsigned int"
113 #endif
114
115 /* The string value for __PTRDIFF_TYPE__. */
116
117 #ifndef PTRDIFF_TYPE
118 #define PTRDIFF_TYPE "long int"
119 #endif
120
121 /* The string value for __WCHAR_TYPE__. */
122
123 #ifndef WCHAR_TYPE
124 #define WCHAR_TYPE "int"
125 #endif
126 #define CPP_WCHAR_TYPE(PFILE) \
127 (CPP_OPTIONS (PFILE)->cplusplus ? "__wchar_t" : WCHAR_TYPE)
128
129 /* The string value for __USER_LABEL_PREFIX__ */
130
131 #ifndef USER_LABEL_PREFIX
132 #define USER_LABEL_PREFIX ""
133 #endif
134
135 /* The string value for __REGISTER_PREFIX__ */
136
137 #ifndef REGISTER_PREFIX
138 #define REGISTER_PREFIX ""
139 #endif
140 \f
141 /* In the definition of a #assert name, this structure forms
142 a list of the individual values asserted.
143 Each value is itself a list of "tokens".
144 These are strings that are compared by name. */
145
146 struct tokenlist_list {
147 struct tokenlist_list *next;
148 struct arglist *tokens;
149 };
150
151 struct assertion_hashnode {
152 struct assertion_hashnode *next; /* double links for easy deletion */
153 struct assertion_hashnode *prev;
154 /* also, a back pointer to this node's hash
155 chain is kept, in case the node is the head
156 of the chain and gets deleted. */
157 struct assertion_hashnode **bucket_hdr;
158 int length; /* length of token, for quick comparison */
159 U_CHAR *name; /* the actual name */
160 /* List of token-sequences. */
161 struct tokenlist_list *value;
162 };
163 \f
164 #define SKIP_WHITE_SPACE(p) do { while (is_hor_space[*p]) p++; } while (0)
165 #define SKIP_ALL_WHITE_SPACE(p) do { while (is_space[*p]) p++; } while (0)
166
167 #define PEEKN(N) (CPP_BUFFER (pfile)->rlimit - CPP_BUFFER (pfile)->cur >= (N) ? CPP_BUFFER (pfile)->cur[N] : EOF)
168 #define FORWARD(N) CPP_FORWARD (CPP_BUFFER (pfile), (N))
169 #define GETC() CPP_BUF_GET (CPP_BUFFER (pfile))
170 #define PEEKC() CPP_BUF_PEEK (CPP_BUFFER (pfile))
171 /* CPP_IS_MACRO_BUFFER is true if the buffer contains macro expansion.
172 (Note that it is false while we're expanding marco *arguments*.) */
173 #define CPP_IS_MACRO_BUFFER(PBUF) ((PBUF)->cleanup == macro_cleanup)
174
175 /* Move all backslash-newline pairs out of embarrassing places.
176 Exchange all such pairs following BP
177 with any potentially-embarrassing characters that follow them.
178 Potentially-embarrassing characters are / and *
179 (because a backslash-newline inside a comment delimiter
180 would cause it not to be recognized). */
181
182 #define NEWLINE_FIX \
183 do {while (PEEKC() == '\\' && PEEKN(1) == '\n') FORWARD(2); } while(0)
184
185 /* Same, but assume we've already read the potential '\\' into C. */
186 #define NEWLINE_FIX1(C) do { \
187 while ((C) == '\\' && PEEKC() == '\n') { FORWARD(1); (C) = GETC(); }\
188 } while(0)
189
190 struct cpp_pending {
191 struct cpp_pending *next;
192 char *cmd;
193 char *arg;
194 };
195
196 /* Forward declarations. */
197
198 char *xmalloc ();
199 extern void cpp_hash_cleanup PARAMS ((cpp_reader *));
200
201 static char *my_strerror PROTO ((int));
202 static void add_import PROTO ((cpp_reader *, int, char *));
203 static void append_include_chain PROTO ((cpp_reader *,
204 struct file_name_list *,
205 struct file_name_list *));
206 static void make_assertion PROTO ((cpp_reader *, char *, U_CHAR *));
207 static void path_include PROTO ((cpp_reader *, char *));
208 static void initialize_builtins PROTO ((cpp_reader *));
209 static void initialize_char_syntax PROTO ((struct cpp_options *));
210 #if 0
211 static void trigraph_pcp ();
212 #endif
213 static int finclude PROTO ((cpp_reader *, int, char *,
214 int, struct file_name_list *));
215 static void validate_else PROTO ((cpp_reader *, char *));
216 static int comp_def_part PROTO ((int, U_CHAR *, int, U_CHAR *,
217 int, int));
218 #ifdef abort
219 extern void fancy_abort ();
220 #endif
221 static int lookup_import PROTO ((cpp_reader *, char *,
222 struct file_name_list *));
223 static int redundant_include_p PROTO ((cpp_reader *, char *));
224 static int is_system_include PROTO ((cpp_reader *, char *));
225 static struct file_name_map *read_name_map PROTO ((cpp_reader *, char *));
226 static char *read_filename_string PROTO ((int, FILE *));
227 static int open_include_file PROTO ((cpp_reader *, char *,
228 struct file_name_list *));
229 static int check_macro_name PROTO ((cpp_reader *, U_CHAR *, char *));
230 static int compare_defs PROTO ((cpp_reader *,
231 DEFINITION *, DEFINITION *));
232 static int compare_token_lists PROTO ((struct arglist *,
233 struct arglist *));
234 static HOST_WIDE_INT eval_if_expression PROTO ((cpp_reader *, U_CHAR *, int));
235 static int change_newlines PROTO ((U_CHAR *, int));
236 static struct arglist *read_token_list PROTO ((cpp_reader *, int *));
237 static void free_token_list PROTO ((struct arglist *));
238 static int safe_read PROTO ((int, char *, int));
239 static void push_macro_expansion PARAMS ((cpp_reader *,
240 U_CHAR *, int, HASHNODE *));
241 static struct cpp_pending *nreverse_pending PARAMS ((struct cpp_pending *));
242 extern char *xrealloc ();
243 static char *xcalloc PROTO ((unsigned, unsigned));
244 static char *savestring PROTO ((char *));
245
246 static void conditional_skip PROTO ((cpp_reader *, int,
247 enum node_type, U_CHAR *));
248 static void skip_if_group PROTO ((cpp_reader *, int));
249 static int parse_name PARAMS ((cpp_reader *, int));
250 static void print_help PROTO ((void));
251
252 /* Last arg to output_line_command. */
253 enum file_change_code {same_file, enter_file, leave_file};
254
255 /* External declarations. */
256
257 extern HOST_WIDE_INT cpp_parse_expr PARAMS ((cpp_reader *));
258
259 extern char *version_string;
260 extern struct tm *localtime ();
261 \f
262 struct file_name_list
263 {
264 struct file_name_list *next;
265 char *fname;
266 /* If the following is nonzero, it is a macro name.
267 Don't include the file again if that macro is defined. */
268 U_CHAR *control_macro;
269 /* If the following is nonzero, it is a C-language system include
270 directory. */
271 int c_system_include_path;
272 /* Mapping of file names for this directory. */
273 struct file_name_map *name_map;
274 /* Non-zero if name_map is valid. */
275 int got_name_map;
276 };
277
278 /* If a buffer's dir field is SELF_DIR_DUMMY, it means the file was found
279 via the same directory as the file that #included it. */
280 #define SELF_DIR_DUMMY ((struct file_name_list *) (~0))
281
282 /* #include "file" looks in source file dir, then stack. */
283 /* #include <file> just looks in the stack. */
284 /* -I directories are added to the end, then the defaults are added. */
285 /* The */
286 static struct default_include {
287 char *fname; /* The name of the directory. */
288 char *component; /* The component containing the directory */
289 int cplusplus; /* Only look here if we're compiling C++. */
290 int cxx_aware; /* Includes in this directory don't need to
291 be wrapped in extern "C" when compiling
292 C++. */
293 } include_defaults_array[]
294 #ifdef INCLUDE_DEFAULTS
295 = INCLUDE_DEFAULTS;
296 #else
297 = {
298 /* Pick up GNU C++ specific include files. */
299 { GPLUSPLUS_INCLUDE_DIR, "G++", 1, 1 },
300 #ifdef CROSS_COMPILE
301 /* This is the dir for fixincludes. Put it just before
302 the files that we fix. */
303 { GCC_INCLUDE_DIR, "GCC", 0, 0 },
304 /* For cross-compilation, this dir name is generated
305 automatically in Makefile.in. */
306 { CROSS_INCLUDE_DIR, "GCC",0, 0 },
307 #ifdef TOOL_INCLUDE_DIR
308 /* This is another place that the target system's headers might be. */
309 { TOOL_INCLUDE_DIR, "BINUTILS", 0, 1 },
310 #endif
311 #else /* not CROSS_COMPILE */
312 #ifdef LOCAL_INCLUDE_DIR
313 /* This should be /usr/local/include and should come before
314 the fixincludes-fixed header files. */
315 { LOCAL_INCLUDE_DIR, 0, 0, 1 },
316 #endif
317 #ifdef TOOL_INCLUDE_DIR
318 /* This is here ahead of GCC_INCLUDE_DIR because assert.h goes here.
319 Likewise, behind LOCAL_INCLUDE_DIR, where glibc puts its assert.h. */
320 { TOOL_INCLUDE_DIR, "BINUTILS", 0, 1 },
321 #endif
322 /* This is the dir for fixincludes. Put it just before
323 the files that we fix. */
324 { GCC_INCLUDE_DIR, "GCC", 0, 0 },
325 /* Some systems have an extra dir of include files. */
326 #ifdef SYSTEM_INCLUDE_DIR
327 { SYSTEM_INCLUDE_DIR, 0, 0, 0 },
328 #endif
329 #ifndef STANDARD_INCLUDE_COMPONENT
330 #define STANDARD_INCLUDE_COMPONENT 0
331 #endif
332 { STANDARD_INCLUDE_DIR, STANDARD_INCLUDE_COMPONENT, 0, 0 },
333 #endif /* not CROSS_COMPILE */
334 { 0, 0, 0, 0 }
335 };
336 #endif /* no INCLUDE_DEFAULTS */
337
338 /* `struct directive' defines one #-directive, including how to handle it. */
339
340 struct directive {
341 int length; /* Length of name */
342 int (*func) /* Function to handle directive */
343 PARAMS ((cpp_reader *, struct directive *, U_CHAR *, U_CHAR *));
344 char *name; /* Name of directive */
345 enum node_type type; /* Code which describes which directive. */
346 char command_reads_line; /* One if rest of line is read by func. */
347 };
348
349 /* These functions are declared to return int instead of void since they
350 are going to be placed in a table and some old compilers have trouble with
351 pointers to functions returning void. */
352
353 static int do_define PARAMS ((cpp_reader *, struct directive *, U_CHAR *, U_CHAR *));
354 static int do_line PARAMS ((cpp_reader *, struct directive *, U_CHAR *, U_CHAR *));
355 static int do_include PARAMS ((cpp_reader *, struct directive *, U_CHAR *, U_CHAR *));
356 static int do_undef PARAMS ((cpp_reader *, struct directive *, U_CHAR *, U_CHAR *));
357 static int do_error PARAMS ((cpp_reader *, struct directive *, U_CHAR *, U_CHAR *));
358 static int do_pragma PARAMS ((cpp_reader *, struct directive *, U_CHAR *, U_CHAR *));
359 static int do_ident PARAMS ((cpp_reader *, struct directive *, U_CHAR *, U_CHAR *));
360 static int do_if PARAMS ((cpp_reader *, struct directive *, U_CHAR *, U_CHAR *));
361 static int do_xifdef PARAMS ((cpp_reader *, struct directive *, U_CHAR *, U_CHAR *));
362 static int do_else PARAMS ((cpp_reader *, struct directive *, U_CHAR *, U_CHAR *));
363 static int do_elif PARAMS ((cpp_reader *, struct directive *, U_CHAR *, U_CHAR *));
364 static int do_endif PARAMS ((cpp_reader *, struct directive *, U_CHAR *, U_CHAR *));
365 #ifdef SCCS_DIRECTIVE
366 static int do_sccs PARAMS ((cpp_reader *, struct directive *, U_CHAR *, U_CHAR *));
367 #endif
368 static int do_once PARAMS ((cpp_reader *, struct directive *, U_CHAR *, U_CHAR *));
369 static int do_assert PARAMS ((cpp_reader *, struct directive *, U_CHAR *, U_CHAR *));
370 static int do_unassert PARAMS ((cpp_reader *, struct directive *, U_CHAR *, U_CHAR *));
371 static int do_warning PARAMS ((cpp_reader *, struct directive *, U_CHAR *, U_CHAR *));
372
373 #define IS_INCLUDE_DIRECTIVE_TYPE(t) \
374 ((int) T_INCLUDE <= (int) (t) && (int) (t) <= (int) T_IMPORT)
375
376 /* Here is the actual list of #-directives, most-often-used first.
377 The initialize_builtins function assumes #define is the very first. */
378
379 static struct directive directive_table[] = {
380 { 6, do_define, "define", T_DEFINE},
381 { 5, do_xifdef, "ifdef", T_IFDEF, 1},
382 { 6, do_xifdef, "ifndef", T_IFNDEF, 1},
383 { 7, do_include, "include", T_INCLUDE, 1},
384 { 12, do_include, "include_next", T_INCLUDE_NEXT, 1},
385 { 6, do_include, "import", T_IMPORT, 1},
386 { 5, do_endif, "endif", T_ENDIF, 1},
387 { 4, do_else, "else", T_ELSE, 1},
388 { 2, do_if, "if", T_IF, 1},
389 { 4, do_elif, "elif", T_ELIF, 1},
390 { 5, do_undef, "undef", T_UNDEF},
391 { 5, do_error, "error", T_ERROR},
392 { 7, do_warning, "warning", T_WARNING},
393 { 6, do_pragma, "pragma", T_PRAGMA},
394 { 4, do_line, "line", T_LINE, 1},
395 { 5, do_ident, "ident", T_IDENT, 1},
396 #ifdef SCCS_DIRECTIVE
397 { 4, do_sccs, "sccs", T_SCCS},
398 #endif
399 { 6, do_assert, "assert", T_ASSERT, 1},
400 { 8, do_unassert, "unassert", T_UNASSERT, 1},
401 { -1, 0, "", T_UNUSED},
402 };
403 \f
404 /* table to tell if char can be part of a C identifier. */
405 U_CHAR is_idchar[256];
406 /* table to tell if char can be first char of a c identifier. */
407 U_CHAR is_idstart[256];
408 /* table to tell if c is horizontal space. */
409 U_CHAR is_hor_space[256];
410 /* table to tell if c is horizontal or vertical space. */
411 static U_CHAR is_space[256];
412
413 /* Initialize syntactic classifications of characters. */
414
415 static void
416 initialize_char_syntax (opts)
417 struct cpp_options *opts;
418 {
419 register int i;
420
421 /*
422 * Set up is_idchar and is_idstart tables. These should be
423 * faster than saying (is_alpha (c) || c == '_'), etc.
424 * Set up these things before calling any routines tthat
425 * refer to them.
426 */
427 for (i = 'a'; i <= 'z'; i++) {
428 is_idchar[i - 'a' + 'A'] = 1;
429 is_idchar[i] = 1;
430 is_idstart[i - 'a' + 'A'] = 1;
431 is_idstart[i] = 1;
432 }
433 for (i = '0'; i <= '9'; i++)
434 is_idchar[i] = 1;
435 is_idchar['_'] = 1;
436 is_idstart['_'] = 1;
437 is_idchar['$'] = opts->dollars_in_ident;
438 is_idstart['$'] = opts->dollars_in_ident;
439
440 /* horizontal space table */
441 is_hor_space[' '] = 1;
442 is_hor_space['\t'] = 1;
443 is_hor_space['\v'] = 1;
444 is_hor_space['\f'] = 1;
445 is_hor_space['\r'] = 1;
446
447 is_space[' '] = 1;
448 is_space['\t'] = 1;
449 is_space['\v'] = 1;
450 is_space['\f'] = 1;
451 is_space['\n'] = 1;
452 is_space['\r'] = 1;
453 }
454
455
456 /* Place into PFILE a quoted string representing the string SRC.
457 Caller must reserve enough space in pfile->token_buffer. */
458
459 static void
460 quote_string (pfile, src)
461 cpp_reader *pfile;
462 char *src;
463 {
464 U_CHAR c;
465
466 CPP_PUTC_Q (pfile, '\"');
467 for (;;)
468 switch ((c = *src++))
469 {
470 default:
471 if (ISPRINT (c))
472 CPP_PUTC_Q (pfile, c);
473 else
474 {
475 sprintf ((char *)CPP_PWRITTEN (pfile), "\\%03o", c);
476 CPP_ADJUST_WRITTEN (pfile, 4);
477 }
478 break;
479
480 case '\"':
481 case '\\':
482 CPP_PUTC_Q (pfile, '\\');
483 CPP_PUTC_Q (pfile, c);
484 break;
485
486 case '\0':
487 CPP_PUTC_Q (pfile, '\"');
488 CPP_NUL_TERMINATE_Q (pfile);
489 return;
490 }
491 }
492
493 /* Re-allocates PFILE->token_buffer so it will hold at least N more chars. */
494
495 void
496 cpp_grow_buffer (pfile, n)
497 cpp_reader *pfile;
498 long n;
499 {
500 long old_written = CPP_WRITTEN (pfile);
501 pfile->token_buffer_size = n + 2 * pfile->token_buffer_size;
502 pfile->token_buffer = (U_CHAR *)
503 xrealloc(pfile->token_buffer, pfile->token_buffer_size);
504 CPP_SET_WRITTEN (pfile, old_written);
505 }
506
507 \f
508 /*
509 * process a given definition string, for initialization
510 * If STR is just an identifier, define it with value 1.
511 * If STR has anything after the identifier, then it should
512 * be identifier=definition.
513 */
514
515 void
516 cpp_define (pfile, str)
517 cpp_reader *pfile;
518 U_CHAR *str;
519 {
520 U_CHAR *buf, *p;
521
522 buf = str;
523 p = str;
524 if (!is_idstart[*p])
525 {
526 cpp_error (pfile, "malformed option `-D %s'", str);
527 return;
528 }
529 while (is_idchar[*++p])
530 ;
531 if (*p == '(') {
532 while (is_idchar[*++p] || *p == ',' || is_hor_space[*p])
533 ;
534 if (*p++ != ')')
535 p = (U_CHAR *) str; /* Error */
536 }
537 if (*p == 0)
538 {
539 buf = (U_CHAR *) alloca (p - buf + 4);
540 strcpy ((char *)buf, str);
541 strcat ((char *)buf, " 1");
542 }
543 else if (*p != '=')
544 {
545 cpp_error (pfile, "malformed option `-D %s'", str);
546 return;
547 }
548 else
549 {
550 U_CHAR *q;
551 /* Copy the entire option so we can modify it. */
552 buf = (U_CHAR *) alloca (2 * strlen (str) + 1);
553 strncpy (buf, str, p - str);
554 /* Change the = to a space. */
555 buf[p - str] = ' ';
556 /* Scan for any backslash-newline and remove it. */
557 p++;
558 q = &buf[p - str];
559 while (*p)
560 {
561 if (*p == '\\' && p[1] == '\n')
562 p += 2;
563 else
564 *q++ = *p++;
565 }
566 *q = 0;
567 }
568
569 do_define (pfile, NULL, buf, buf + strlen (buf));
570 }
571 \f
572 /* Process the string STR as if it appeared as the body of a #assert.
573 OPTION is the option name for which STR was the argument. */
574
575 static void
576 make_assertion (pfile, option, str)
577 cpp_reader *pfile;
578 char *option;
579 U_CHAR *str;
580 {
581 U_CHAR *buf, *p, *q;
582
583 /* Copy the entire option so we can modify it. */
584 buf = (U_CHAR *) alloca (strlen (str) + 1);
585 strcpy ((char *) buf, str);
586 /* Scan for any backslash-newline and remove it. */
587 p = q = buf;
588 while (*p) {
589 #if 0
590 if (*p == '\\' && p[1] == '\n')
591 p += 2;
592 else
593 #endif
594 *q++ = *p++;
595 }
596 *q = 0;
597
598 p = buf;
599 if (!is_idstart[*p]) {
600 cpp_error (pfile, "malformed option `%s %s'", option, str);
601 return;
602 }
603 while (is_idchar[*++p])
604 ;
605 while (*p == ' ' || *p == '\t') p++;
606 if (! (*p == 0 || *p == '(')) {
607 cpp_error (pfile, "malformed option `%s %s'", option, str);
608 return;
609 }
610
611 if (cpp_push_buffer (pfile, buf, strlen (buf)) != NULL)
612 {
613 do_assert (pfile, NULL, NULL, NULL);
614 cpp_pop_buffer (pfile);
615 }
616 }
617 \f
618 /* Append a chain of `struct file_name_list's
619 to the end of the main include chain.
620 FIRST is the beginning of the chain to append, and LAST is the end. */
621
622 static void
623 append_include_chain (pfile, first, last)
624 cpp_reader *pfile;
625 struct file_name_list *first, *last;
626 {
627 struct cpp_options *opts = CPP_OPTIONS (pfile);
628 struct file_name_list *dir;
629
630 if (!first || !last)
631 return;
632
633 if (opts->include == 0)
634 opts->include = first;
635 else
636 opts->last_include->next = first;
637
638 if (opts->first_bracket_include == 0)
639 opts->first_bracket_include = first;
640
641 for (dir = first; ; dir = dir->next) {
642 int len = strlen (dir->fname) + INCLUDE_LEN_FUDGE;
643 if (len > pfile->max_include_len)
644 pfile->max_include_len = len;
645 if (dir == last)
646 break;
647 }
648
649 last->next = NULL;
650 opts->last_include = last;
651 }
652 \f
653 /* Add output to `deps_buffer' for the -M switch.
654 STRING points to the text to be output.
655 SPACER is ':' for targets, ' ' for dependencies, zero for text
656 to be inserted literally. */
657
658 static void
659 deps_output (pfile, string, spacer)
660 cpp_reader *pfile;
661 char *string;
662 int spacer;
663 {
664 int size = strlen (string);
665
666 if (size == 0)
667 return;
668
669 #ifndef MAX_OUTPUT_COLUMNS
670 #define MAX_OUTPUT_COLUMNS 72
671 #endif
672 if (spacer
673 && pfile->deps_column > 0
674 && (pfile->deps_column + size) > MAX_OUTPUT_COLUMNS)
675 {
676 deps_output (pfile, " \\\n ", 0);
677 pfile->deps_column = 0;
678 }
679
680 if (pfile->deps_size + size + 8 > pfile->deps_allocated_size)
681 {
682 pfile->deps_allocated_size = (pfile->deps_size + size + 50) * 2;
683 pfile->deps_buffer = (char *) xrealloc (pfile->deps_buffer,
684 pfile->deps_allocated_size);
685 }
686 if (spacer == ' ' && pfile->deps_column > 0)
687 pfile->deps_buffer[pfile->deps_size++] = ' ';
688 bcopy (string, &pfile->deps_buffer[pfile->deps_size], size);
689 pfile->deps_size += size;
690 pfile->deps_column += size;
691 if (spacer == ':')
692 pfile->deps_buffer[pfile->deps_size++] = ':';
693 pfile->deps_buffer[pfile->deps_size] = 0;
694 }
695 \f
696 /* Given a colon-separated list of file names PATH,
697 add all the names to the search path for include files. */
698
699 static void
700 path_include (pfile, path)
701 cpp_reader *pfile;
702 char *path;
703 {
704 char *p;
705
706 p = path;
707
708 if (*p)
709 while (1) {
710 char *q = p;
711 char *name;
712 struct file_name_list *dirtmp;
713
714 /* Find the end of this name. */
715 while (*q != 0 && *q != PATH_SEPARATOR) q++;
716 if (p == q) {
717 /* An empty name in the path stands for the current directory. */
718 name = (char *) xmalloc (2);
719 name[0] = '.';
720 name[1] = 0;
721 } else {
722 /* Otherwise use the directory that is named. */
723 name = (char *) xmalloc (q - p + 1);
724 bcopy (p, name, q - p);
725 name[q - p] = 0;
726 }
727
728 dirtmp = (struct file_name_list *)
729 xmalloc (sizeof (struct file_name_list));
730 dirtmp->next = 0; /* New one goes on the end */
731 dirtmp->control_macro = 0;
732 dirtmp->c_system_include_path = 0;
733 dirtmp->fname = name;
734 dirtmp->got_name_map = 0;
735 append_include_chain (pfile, dirtmp, dirtmp);
736
737 /* Advance past this name. */
738 p = q;
739 if (*p == 0)
740 break;
741 /* Skip the colon. */
742 p++;
743 }
744 }
745 \f
746 void
747 cpp_options_init (opts)
748 cpp_options *opts;
749 {
750 bzero ((char *) opts, sizeof *opts);
751 opts->in_fname = NULL;
752 opts->out_fname = NULL;
753
754 /* Initialize is_idchar to allow $. */
755 opts->dollars_in_ident = 1;
756 initialize_char_syntax (opts);
757
758 opts->no_line_commands = 0;
759 opts->no_trigraphs = 1;
760 opts->put_out_comments = 0;
761 opts->print_include_names = 0;
762 opts->dump_macros = dump_none;
763 opts->no_output = 0;
764 opts->remap = 0;
765 opts->cplusplus = 0;
766 opts->cplusplus_comments = 1;
767
768 opts->verbose = 0;
769 opts->objc = 0;
770 opts->lang_asm = 0;
771 opts->for_lint = 0;
772 opts->chill = 0;
773 opts->pedantic_errors = 0;
774 opts->inhibit_warnings = 0;
775 opts->warn_comments = 0;
776 opts->warn_import = 1;
777 opts->warnings_are_errors = 0;
778 }
779
780 enum cpp_token
781 null_underflow (pfile)
782 cpp_reader *pfile ATTRIBUTE_UNUSED;
783 {
784 return CPP_EOF;
785 }
786
787 int
788 null_cleanup (pbuf, pfile)
789 cpp_buffer *pbuf ATTRIBUTE_UNUSED;
790 cpp_reader *pfile ATTRIBUTE_UNUSED;
791 {
792 return 0;
793 }
794
795 int
796 macro_cleanup (pbuf, pfile)
797 cpp_buffer *pbuf;
798 cpp_reader *pfile ATTRIBUTE_UNUSED;
799 {
800 HASHNODE *macro = (HASHNODE *) pbuf->data;
801 if (macro->type == T_DISABLED)
802 macro->type = T_MACRO;
803 if (macro->type != T_MACRO || pbuf->buf != macro->value.defn->expansion)
804 free (pbuf->buf);
805 return 0;
806 }
807
808 int
809 file_cleanup (pbuf, pfile)
810 cpp_buffer *pbuf;
811 cpp_reader *pfile ATTRIBUTE_UNUSED;
812 {
813 if (pbuf->buf)
814 {
815 free (pbuf->buf);
816 pbuf->buf = 0;
817 }
818 return 0;
819 }
820
821 /* Assuming we have read '/'.
822 If this is the start of a comment (followed by '*' or '/'),
823 skip to the end of the comment, and return ' '.
824 Return EOF if we reached the end of file before the end of the comment.
825 If not the start of a comment, return '/'. */
826
827 static int
828 skip_comment (pfile, linep)
829 cpp_reader *pfile;
830 long *linep;
831 {
832 int c = 0;
833 while (PEEKC() == '\\' && PEEKN(1) == '\n')
834 {
835 if (linep)
836 (*linep)++;
837 FORWARD(2);
838 }
839 if (PEEKC() == '*')
840 {
841 FORWARD(1);
842 for (;;)
843 {
844 int prev_c = c;
845 c = GETC ();
846 if (c == EOF)
847 return EOF;
848 while (c == '\\' && PEEKC() == '\n')
849 {
850 if (linep)
851 (*linep)++;
852 FORWARD(1), c = GETC();
853 }
854 if (prev_c == '*' && c == '/')
855 return ' ';
856 if (c == '\n' && linep)
857 (*linep)++;
858 }
859 }
860 else if (PEEKC() == '/' && CPP_OPTIONS (pfile)->cplusplus_comments)
861 {
862 FORWARD(1);
863 for (;;)
864 {
865 c = GETC ();
866 if (c == EOF)
867 return ' '; /* Allow // to be terminated by EOF. */
868 while (c == '\\' && PEEKC() == '\n')
869 {
870 FORWARD(1);
871 c = GETC();
872 if (linep)
873 (*linep)++;
874 }
875 if (c == '\n')
876 {
877 /* Don't consider final '\n' to be part of comment. */
878 FORWARD(-1);
879 return ' ';
880 }
881 }
882 }
883 else
884 return '/';
885 }
886
887 /* Skip whitespace \-newline and comments. Does not macro-expand. */
888
889 void
890 cpp_skip_hspace (pfile)
891 cpp_reader *pfile;
892 {
893 while (1)
894 {
895 int c = PEEKC();
896 if (c == EOF)
897 return; /* FIXME */
898 if (is_hor_space[c])
899 {
900 if ((c == '\f' || c == '\v') && CPP_PEDANTIC (pfile))
901 cpp_pedwarn (pfile, "%s in preprocessing directive",
902 c == '\f' ? "formfeed" : "vertical tab");
903 FORWARD(1);
904 }
905 else if (c == '/')
906 {
907 FORWARD (1);
908 c = skip_comment (pfile, NULL);
909 if (c == '/')
910 FORWARD(-1);
911 if (c == EOF || c == '/')
912 return;
913 }
914 else if (c == '\\' && PEEKN(1) == '\n') {
915 FORWARD(2);
916 }
917 else if (c == '@' && CPP_BUFFER (pfile)->has_escapes
918 && is_hor_space[PEEKN(1)])
919 FORWARD(2);
920 else return;
921 }
922 }
923
924 /* Read the rest of the current line.
925 The line is appended to PFILE's output buffer. */
926
927 static void
928 copy_rest_of_line (pfile)
929 cpp_reader *pfile;
930 {
931 struct cpp_options *opts = CPP_OPTIONS (pfile);
932 for (;;)
933 {
934 int c = GETC();
935 int nextc;
936 switch (c)
937 {
938 case EOF:
939 goto end_directive;
940 case '\\':
941 if (PEEKC() == '\n')
942 {
943 FORWARD (1);
944 continue;
945 }
946 case '\'':
947 case '\"':
948 goto scan_directive_token;
949 break;
950 case '/':
951 nextc = PEEKC();
952 if (nextc == '*' || (opts->cplusplus_comments && nextc == '/'))
953 goto scan_directive_token;
954 break;
955 case '\f':
956 case '\v':
957 if (CPP_PEDANTIC (pfile))
958 cpp_pedwarn (pfile, "%s in preprocessing directive",
959 c == '\f' ? "formfeed" : "vertical tab");
960 break;
961
962 case '\n':
963 FORWARD(-1);
964 goto end_directive;
965 scan_directive_token:
966 FORWARD(-1);
967 cpp_get_token (pfile);
968 continue;
969 }
970 CPP_PUTC (pfile, c);
971 }
972 end_directive: ;
973 CPP_NUL_TERMINATE (pfile);
974 }
975
976 void
977 skip_rest_of_line (pfile)
978 cpp_reader *pfile;
979 {
980 long old = CPP_WRITTEN (pfile);
981 copy_rest_of_line (pfile);
982 CPP_SET_WRITTEN (pfile, old);
983 }
984
985 /* Handle a possible # directive.
986 '#' has already been read. */
987
988 int
989 handle_directive (pfile)
990 cpp_reader *pfile;
991 { int c;
992 register struct directive *kt;
993 int ident_length;
994 long after_ident;
995 U_CHAR *ident, *line_end;
996 long old_written = CPP_WRITTEN (pfile);
997
998 cpp_skip_hspace (pfile);
999
1000 c = PEEKC ();
1001 if (c >= '0' && c <= '9')
1002 {
1003 /* Handle # followed by a line number. */
1004 if (CPP_PEDANTIC (pfile))
1005 cpp_pedwarn (pfile, "`#' followed by integer");
1006 do_line (pfile, NULL, NULL, NULL);
1007 goto done_a_directive;
1008 }
1009
1010 /* Now find the directive name. */
1011 CPP_PUTC (pfile, '#');
1012 parse_name (pfile, GETC());
1013 ident = pfile->token_buffer + old_written + 1;
1014 ident_length = CPP_PWRITTEN (pfile) - ident;
1015 if (ident_length == 0 && PEEKC() == '\n')
1016 {
1017 /* A line of just `#' becomes blank. */
1018 goto done_a_directive;
1019 }
1020
1021 #if 0
1022 if (ident_length == 0 || !is_idstart[*ident]) {
1023 U_CHAR *p = ident;
1024 while (is_idchar[*p]) {
1025 if (*p < '0' || *p > '9')
1026 break;
1027 p++;
1028 }
1029 /* Avoid error for `###' and similar cases unless -pedantic. */
1030 if (p == ident) {
1031 while (*p == '#' || is_hor_space[*p]) p++;
1032 if (*p == '\n') {
1033 if (pedantic && !lang_asm)
1034 cpp_warning (pfile, "invalid preprocessor directive");
1035 return 0;
1036 }
1037 }
1038
1039 if (!lang_asm)
1040 cpp_error (pfile, "invalid preprocessor directive name");
1041
1042 return 0;
1043 }
1044 #endif
1045 /*
1046 * Decode the keyword and call the appropriate expansion
1047 * routine, after moving the input pointer up to the next line.
1048 */
1049 for (kt = directive_table; ; kt++) {
1050 if (kt->length <= 0)
1051 goto not_a_directive;
1052 if (kt->length == ident_length && !strncmp (kt->name, ident, ident_length))
1053 break;
1054 }
1055
1056 if (kt->command_reads_line)
1057 after_ident = 0;
1058 else
1059 {
1060 /* Nonzero means do not delete comments within the directive.
1061 #define needs this when -traditional. */
1062 int comments = CPP_TRADITIONAL (pfile) && kt->type == T_DEFINE;
1063 int save_put_out_comments = CPP_OPTIONS (pfile)->put_out_comments;
1064 CPP_OPTIONS (pfile)->put_out_comments = comments;
1065 after_ident = CPP_WRITTEN (pfile);
1066 copy_rest_of_line (pfile);
1067 CPP_OPTIONS (pfile)->put_out_comments = save_put_out_comments;
1068 }
1069
1070 /* We may want to pass through #define, #pragma, and #include.
1071 Other directives may create output, but we don't want the directive
1072 itself out, so we pop it now. For example conditionals may emit
1073 #failed ... #endfailed stuff. But note that popping the buffer
1074 means the parameters to kt->func may point after pfile->limit
1075 so these parameters are invalid as soon as something gets appended
1076 to the token_buffer. */
1077
1078 line_end = CPP_PWRITTEN (pfile);
1079 if (! (kt->type == T_DEFINE
1080 || kt->type == T_PRAGMA
1081 || (IS_INCLUDE_DIRECTIVE_TYPE (kt->type)
1082 && CPP_OPTIONS (pfile)->dump_includes)))
1083 CPP_SET_WRITTEN (pfile, old_written);
1084
1085 (*kt->func) (pfile, kt, pfile->token_buffer + after_ident, line_end);
1086
1087 if (kt->type == T_DEFINE)
1088 {
1089 if (CPP_OPTIONS (pfile)->dump_macros == dump_names)
1090 {
1091 /* Skip "#define". */
1092 U_CHAR *p = pfile->token_buffer + old_written + 7;
1093
1094 SKIP_WHITE_SPACE (p);
1095 while (is_idchar[*p]) p++;
1096 pfile->limit = p;
1097 CPP_PUTC (pfile, '\n');
1098 }
1099 else if (CPP_OPTIONS (pfile)->dump_macros != dump_definitions)
1100 CPP_SET_WRITTEN (pfile, old_written);
1101 }
1102
1103 done_a_directive:
1104 return 1;
1105
1106 not_a_directive:
1107 return 0;
1108 }
1109
1110 /* Pass a directive through to the output file.
1111 BUF points to the contents of the directive, as a contiguous string.
1112 LIMIT points to the first character past the end of the directive.
1113 KEYWORD is the keyword-table entry for the directive. */
1114
1115 static void
1116 pass_thru_directive (buf, limit, pfile, keyword)
1117 U_CHAR *buf, *limit;
1118 cpp_reader *pfile;
1119 struct directive *keyword;
1120 {
1121 register unsigned keyword_length = keyword->length;
1122
1123 CPP_RESERVE (pfile, 1 + keyword_length + (limit - buf));
1124 CPP_PUTC_Q (pfile, '#');
1125 CPP_PUTS_Q (pfile, keyword->name, keyword_length);
1126 if (limit != buf && buf[0] != ' ')
1127 CPP_PUTC_Q (pfile, ' ');
1128 CPP_PUTS_Q (pfile, buf, limit - buf);
1129 #if 0
1130 CPP_PUTS_Q (pfile, '\n');
1131 /* Count the line we have just made in the output,
1132 to get in sync properly. */
1133 pfile->lineno++;
1134 #endif
1135 }
1136 \f
1137 /* The arglist structure is built by do_define to tell
1138 collect_definition where the argument names begin. That
1139 is, for a define like "#define f(x,y,z) foo+x-bar*y", the arglist
1140 would contain pointers to the strings x, y, and z.
1141 Collect_definition would then build a DEFINITION node,
1142 with reflist nodes pointing to the places x, y, and z had
1143 appeared. So the arglist is just convenience data passed
1144 between these two routines. It is not kept around after
1145 the current #define has been processed and entered into the
1146 hash table. */
1147
1148 struct arglist {
1149 struct arglist *next;
1150 U_CHAR *name;
1151 int length;
1152 int argno;
1153 char rest_args;
1154 };
1155
1156 /* Read a replacement list for a macro with parameters.
1157 Build the DEFINITION structure.
1158 Reads characters of text starting at BUF until END.
1159 ARGLIST specifies the formal parameters to look for
1160 in the text of the definition; NARGS is the number of args
1161 in that list, or -1 for a macro name that wants no argument list.
1162 MACRONAME is the macro name itself (so we can avoid recursive expansion)
1163 and NAMELEN is its length in characters.
1164
1165 Note that comments, backslash-newlines, and leading white space
1166 have already been deleted from the argument. */
1167
1168 static DEFINITION *
1169 collect_expansion (pfile, buf, limit, nargs, arglist)
1170 cpp_reader *pfile;
1171 U_CHAR *buf, *limit;
1172 int nargs;
1173 struct arglist *arglist;
1174 {
1175 DEFINITION *defn;
1176 register U_CHAR *p, *lastp, *exp_p;
1177 struct reflist *endpat = NULL;
1178 /* Pointer to first nonspace after last ## seen. */
1179 U_CHAR *concat = 0;
1180 /* Pointer to first nonspace after last single-# seen. */
1181 U_CHAR *stringify = 0;
1182 int maxsize;
1183 int expected_delimiter = '\0';
1184
1185 /* Scan thru the replacement list, ignoring comments and quoted
1186 strings, picking up on the macro calls. It does a linear search
1187 thru the arg list on every potential symbol. Profiling might say
1188 that something smarter should happen. */
1189
1190 if (limit < buf)
1191 abort ();
1192
1193 /* Find the beginning of the trailing whitespace. */
1194 p = buf;
1195 while (p < limit && is_space[limit[-1]]) limit--;
1196
1197 /* Allocate space for the text in the macro definition.
1198 Leading and trailing whitespace chars need 2 bytes each.
1199 Each other input char may or may not need 1 byte,
1200 so this is an upper bound. The extra 5 are for invented
1201 leading and trailing newline-marker and final null. */
1202 maxsize = (sizeof (DEFINITION)
1203 + (limit - p) + 5);
1204 /* Occurrences of '@' get doubled, so allocate extra space for them. */
1205 while (p < limit)
1206 if (*p++ == '@')
1207 maxsize++;
1208 defn = (DEFINITION *) xcalloc (1, maxsize);
1209
1210 defn->nargs = nargs;
1211 exp_p = defn->expansion = (U_CHAR *) defn + sizeof (DEFINITION);
1212 lastp = exp_p;
1213
1214 p = buf;
1215
1216 /* Add one initial space escape-marker to prevent accidental
1217 token-pasting (often removed by macroexpand). */
1218 *exp_p++ = '@';
1219 *exp_p++ = ' ';
1220
1221 if (limit - p >= 2 && p[0] == '#' && p[1] == '#') {
1222 cpp_error (pfile, "`##' at start of macro definition");
1223 p += 2;
1224 }
1225
1226 /* Process the main body of the definition. */
1227 while (p < limit) {
1228 int skipped_arg = 0;
1229 register U_CHAR c = *p++;
1230
1231 *exp_p++ = c;
1232
1233 if (!CPP_TRADITIONAL (pfile)) {
1234 switch (c) {
1235 case '\'':
1236 case '\"':
1237 if (expected_delimiter != '\0') {
1238 if (c == expected_delimiter)
1239 expected_delimiter = '\0';
1240 } else
1241 expected_delimiter = c;
1242 break;
1243
1244 case '\\':
1245 if (p < limit && expected_delimiter) {
1246 /* In a string, backslash goes through
1247 and makes next char ordinary. */
1248 *exp_p++ = *p++;
1249 }
1250 break;
1251
1252 case '@':
1253 /* An '@' in a string or character constant stands for itself,
1254 and does not need to be escaped. */
1255 if (!expected_delimiter)
1256 *exp_p++ = c;
1257 break;
1258
1259 case '#':
1260 /* # is ordinary inside a string. */
1261 if (expected_delimiter)
1262 break;
1263 if (p < limit && *p == '#') {
1264 /* ##: concatenate preceding and following tokens. */
1265 /* Take out the first #, discard preceding whitespace. */
1266 exp_p--;
1267 while (exp_p > lastp && is_hor_space[exp_p[-1]])
1268 --exp_p;
1269 /* Skip the second #. */
1270 p++;
1271 /* Discard following whitespace. */
1272 SKIP_WHITE_SPACE (p);
1273 concat = p;
1274 if (p == limit)
1275 cpp_error (pfile, "`##' at end of macro definition");
1276 } else if (nargs >= 0) {
1277 /* Single #: stringify following argument ref.
1278 Don't leave the # in the expansion. */
1279 exp_p--;
1280 SKIP_WHITE_SPACE (p);
1281 if (p == limit || ! is_idstart[*p]
1282 || (*p == 'L' && p + 1 < limit && (p[1] == '\'' || p[1] == '"')))
1283 cpp_error (pfile,
1284 "`#' operator is not followed by a macro argument name");
1285 else
1286 stringify = p;
1287 }
1288 break;
1289 }
1290 } else {
1291 /* In -traditional mode, recognize arguments inside strings and
1292 character constants, and ignore special properties of #.
1293 Arguments inside strings are considered "stringified", but no
1294 extra quote marks are supplied. */
1295 switch (c) {
1296 case '\'':
1297 case '\"':
1298 if (expected_delimiter != '\0') {
1299 if (c == expected_delimiter)
1300 expected_delimiter = '\0';
1301 } else
1302 expected_delimiter = c;
1303 break;
1304
1305 case '\\':
1306 /* Backslash quotes delimiters and itself, but not macro args. */
1307 if (expected_delimiter != 0 && p < limit
1308 && (*p == expected_delimiter || *p == '\\')) {
1309 *exp_p++ = *p++;
1310 continue;
1311 }
1312 break;
1313
1314 case '/':
1315 if (expected_delimiter != '\0') /* No comments inside strings. */
1316 break;
1317 if (*p == '*') {
1318 /* If we find a comment that wasn't removed by handle_directive,
1319 this must be -traditional. So replace the comment with
1320 nothing at all. */
1321 exp_p--;
1322 p += 1;
1323 while (p < limit && !(p[-2] == '*' && p[-1] == '/'))
1324 p++;
1325 #if 0
1326 /* Mark this as a concatenation-point, as if it had been ##. */
1327 concat = p;
1328 #endif
1329 }
1330 break;
1331 }
1332 }
1333
1334 /* Handle the start of a symbol. */
1335 if (is_idchar[c] && nargs > 0) {
1336 U_CHAR *id_beg = p - 1;
1337 int id_len;
1338
1339 --exp_p;
1340 while (p != limit && is_idchar[*p]) p++;
1341 id_len = p - id_beg;
1342
1343 if (is_idstart[c]
1344 && ! (id_len == 1 && c == 'L' && (*p == '\'' || *p == '"'))) {
1345 register struct arglist *arg;
1346
1347 for (arg = arglist; arg != NULL; arg = arg->next) {
1348 struct reflist *tpat;
1349
1350 if (arg->name[0] == c
1351 && arg->length == id_len
1352 && strncmp (arg->name, id_beg, id_len) == 0) {
1353 if (expected_delimiter && CPP_OPTIONS (pfile)->warn_stringify) {
1354 if (CPP_TRADITIONAL (pfile)) {
1355 cpp_warning (pfile, "macro argument `%.*s' is stringified.",
1356 id_len, arg->name);
1357 } else {
1358 cpp_warning (pfile,
1359 "macro arg `%.*s' would be stringified with -traditional.",
1360 id_len, arg->name);
1361 }
1362 }
1363 /* If ANSI, don't actually substitute inside a string. */
1364 if (!CPP_TRADITIONAL (pfile) && expected_delimiter)
1365 break;
1366 /* make a pat node for this arg and append it to the end of
1367 the pat list */
1368 tpat = (struct reflist *) xmalloc (sizeof (struct reflist));
1369 tpat->next = NULL;
1370 tpat->raw_before = concat == id_beg;
1371 tpat->raw_after = 0;
1372 tpat->rest_args = arg->rest_args;
1373 tpat->stringify = (CPP_TRADITIONAL (pfile)
1374 ? expected_delimiter != '\0'
1375 : stringify == id_beg);
1376
1377 if (endpat == NULL)
1378 defn->pattern = tpat;
1379 else
1380 endpat->next = tpat;
1381 endpat = tpat;
1382
1383 tpat->argno = arg->argno;
1384 tpat->nchars = exp_p - lastp;
1385 {
1386 register U_CHAR *p1 = p;
1387 SKIP_WHITE_SPACE (p1);
1388 if (p1 + 2 <= limit && p1[0] == '#' && p1[1] == '#')
1389 tpat->raw_after = 1;
1390 }
1391 lastp = exp_p; /* place to start copying from next time */
1392 skipped_arg = 1;
1393 break;
1394 }
1395 }
1396 }
1397
1398 /* If this was not a macro arg, copy it into the expansion. */
1399 if (! skipped_arg) {
1400 register U_CHAR *lim1 = p;
1401 p = id_beg;
1402 while (p != lim1)
1403 *exp_p++ = *p++;
1404 if (stringify == id_beg)
1405 cpp_error (pfile,
1406 "`#' operator should be followed by a macro argument name");
1407 }
1408 }
1409 }
1410
1411 if (!CPP_TRADITIONAL (pfile) && expected_delimiter == 0)
1412 {
1413 /* If ANSI, put in a "@ " marker to prevent token pasting.
1414 But not if "inside a string" (which in ANSI mode
1415 happens only for -D option). */
1416 *exp_p++ = '@';
1417 *exp_p++ = ' ';
1418 }
1419
1420 *exp_p = '\0';
1421
1422 defn->length = exp_p - defn->expansion;
1423
1424 /* Crash now if we overrun the allocated size. */
1425 if (defn->length + 1 > maxsize)
1426 abort ();
1427
1428 #if 0
1429 /* This isn't worth the time it takes. */
1430 /* give back excess storage */
1431 defn->expansion = (U_CHAR *) xrealloc (defn->expansion, defn->length + 1);
1432 #endif
1433
1434 return defn;
1435 }
1436
1437 /*
1438 * special extension string that can be added to the last macro argument to
1439 * allow it to absorb the "rest" of the arguments when expanded. Ex:
1440 * #define wow(a, b...) process (b, a, b)
1441 * { wow (1, 2, 3); } -> { process (2, 3, 1, 2, 3); }
1442 * { wow (one, two); } -> { process (two, one, two); }
1443 * if this "rest_arg" is used with the concat token '##' and if it is not
1444 * supplied then the token attached to with ## will not be outputted. Ex:
1445 * #define wow (a, b...) process (b ## , a, ## b)
1446 * { wow (1, 2); } -> { process (2, 1, 2); }
1447 * { wow (one); } -> { process (one); {
1448 */
1449 static char rest_extension[] = "...";
1450 #define REST_EXTENSION_LENGTH (sizeof (rest_extension) - 1)
1451
1452 /* Create a DEFINITION node from a #define directive. Arguments are
1453 as for do_define. */
1454
1455 static MACRODEF
1456 create_definition (buf, limit, pfile, predefinition)
1457 U_CHAR *buf, *limit;
1458 cpp_reader *pfile;
1459 int predefinition;
1460 {
1461 U_CHAR *bp; /* temp ptr into input buffer */
1462 U_CHAR *symname; /* remember where symbol name starts */
1463 int sym_length; /* and how long it is */
1464 int rest_args = 0;
1465 long line, col;
1466 char *file = CPP_BUFFER (pfile) ? CPP_BUFFER (pfile)->nominal_fname : "";
1467 DEFINITION *defn;
1468 int arglengths = 0; /* Accumulate lengths of arg names
1469 plus number of args. */
1470 MACRODEF mdef;
1471 cpp_buf_line_and_col (CPP_BUFFER (pfile), &line, &col);
1472
1473 bp = buf;
1474
1475 while (is_hor_space[*bp])
1476 bp++;
1477
1478 symname = bp; /* remember where it starts */
1479
1480 sym_length = check_macro_name (pfile, bp, "macro");
1481 bp += sym_length;
1482
1483 /* Lossage will occur if identifiers or control keywords are broken
1484 across lines using backslash. This is not the right place to take
1485 care of that. */
1486
1487 if (*bp == '(') {
1488 struct arglist *arg_ptrs = NULL;
1489 int argno = 0;
1490
1491 bp++; /* skip '(' */
1492 SKIP_WHITE_SPACE (bp);
1493
1494 /* Loop over macro argument names. */
1495 while (*bp != ')') {
1496 struct arglist *temp;
1497
1498 temp = (struct arglist *) alloca (sizeof (struct arglist));
1499 temp->name = bp;
1500 temp->next = arg_ptrs;
1501 temp->argno = argno++;
1502 temp->rest_args = 0;
1503 arg_ptrs = temp;
1504
1505 if (rest_args)
1506 cpp_pedwarn (pfile, "another parameter follows `%s'", rest_extension);
1507
1508 if (!is_idstart[*bp])
1509 cpp_pedwarn (pfile, "invalid character in macro parameter name");
1510
1511 /* Find the end of the arg name. */
1512 while (is_idchar[*bp]) {
1513 bp++;
1514 /* do we have a "special" rest-args extension here? */
1515 if (limit - bp > REST_EXTENSION_LENGTH
1516 && strncmp (rest_extension, bp, REST_EXTENSION_LENGTH) == 0) {
1517 rest_args = 1;
1518 temp->rest_args = 1;
1519 break;
1520 }
1521 }
1522 temp->length = bp - temp->name;
1523 if (rest_args == 1)
1524 bp += REST_EXTENSION_LENGTH;
1525 arglengths += temp->length + 2;
1526 SKIP_WHITE_SPACE (bp);
1527 if (temp->length == 0 || (*bp != ',' && *bp != ')')) {
1528 cpp_error (pfile, "badly punctuated parameter list in `#define'");
1529 goto nope;
1530 }
1531 if (*bp == ',') {
1532 bp++;
1533 SKIP_WHITE_SPACE (bp);
1534 }
1535 if (bp >= limit) {
1536 cpp_error (pfile, "unterminated parameter list in `#define'");
1537 goto nope;
1538 }
1539 {
1540 struct arglist *otemp;
1541
1542 for (otemp = temp->next; otemp != NULL; otemp = otemp->next)
1543 if (temp->length == otemp->length
1544 && strncmp (temp->name, otemp->name, temp->length) == 0) {
1545 U_CHAR *name;
1546
1547 name = (U_CHAR *) alloca (temp->length + 1);
1548 (void) strncpy (name, temp->name, temp->length);
1549 name[temp->length] = '\0';
1550 cpp_error (pfile,
1551 "duplicate argument name `%s' in `#define'", name);
1552 goto nope;
1553 }
1554 }
1555 }
1556
1557 ++bp; /* skip paren */
1558 SKIP_WHITE_SPACE (bp);
1559 /* now everything from bp before limit is the definition. */
1560 defn = collect_expansion (pfile, bp, limit, argno, arg_ptrs);
1561 defn->rest_args = rest_args;
1562
1563 /* Now set defn->args.argnames to the result of concatenating
1564 the argument names in reverse order
1565 with comma-space between them. */
1566 defn->args.argnames = (U_CHAR *) xmalloc (arglengths + 1);
1567 {
1568 struct arglist *temp;
1569 int i = 0;
1570 for (temp = arg_ptrs; temp; temp = temp->next) {
1571 bcopy (temp->name, &defn->args.argnames[i], temp->length);
1572 i += temp->length;
1573 if (temp->next != 0) {
1574 defn->args.argnames[i++] = ',';
1575 defn->args.argnames[i++] = ' ';
1576 }
1577 }
1578 defn->args.argnames[i] = 0;
1579 }
1580 } else {
1581 /* Simple expansion or empty definition. */
1582
1583 if (bp < limit)
1584 {
1585 if (is_hor_space[*bp]) {
1586 bp++;
1587 SKIP_WHITE_SPACE (bp);
1588 } else {
1589 switch (*bp) {
1590 case '!': case '"': case '#': case '%': case '&': case '\'':
1591 case ')': case '*': case '+': case ',': case '-': case '.':
1592 case '/': case ':': case ';': case '<': case '=': case '>':
1593 case '?': case '[': case '\\': case ']': case '^': case '{':
1594 case '|': case '}': case '~':
1595 cpp_warning (pfile, "missing white space after `#define %.*s'",
1596 sym_length, symname);
1597 break;
1598
1599 default:
1600 cpp_pedwarn (pfile, "missing white space after `#define %.*s'",
1601 sym_length, symname);
1602 break;
1603 }
1604 }
1605 }
1606 /* now everything from bp before limit is the definition. */
1607 defn = collect_expansion (pfile, bp, limit, -1, NULL_PTR);
1608 defn->args.argnames = (U_CHAR *) "";
1609 }
1610
1611 defn->line = line;
1612 defn->file = file;
1613
1614 /* OP is null if this is a predefinition */
1615 defn->predefined = predefinition;
1616 mdef.defn = defn;
1617 mdef.symnam = symname;
1618 mdef.symlen = sym_length;
1619
1620 return mdef;
1621
1622 nope:
1623 mdef.defn = 0;
1624 return mdef;
1625 }
1626
1627 /* Check a purported macro name SYMNAME, and yield its length.
1628 USAGE is the kind of name this is intended for. */
1629
1630 static int
1631 check_macro_name (pfile, symname, usage)
1632 cpp_reader *pfile;
1633 U_CHAR *symname;
1634 char *usage;
1635 {
1636 U_CHAR *p;
1637 int sym_length;
1638
1639 for (p = symname; is_idchar[*p]; p++)
1640 ;
1641 sym_length = p - symname;
1642 if (sym_length == 0
1643 || (sym_length == 1 && *symname == 'L' && (*p == '\'' || *p == '"')))
1644 cpp_error (pfile, "invalid %s name", usage);
1645 else if (!is_idstart[*symname]) {
1646 U_CHAR *msg; /* what pain... */
1647 msg = (U_CHAR *) alloca (sym_length + 1);
1648 bcopy (symname, msg, sym_length);
1649 msg[sym_length] = 0;
1650 cpp_error (pfile, "invalid %s name `%s'", usage, msg);
1651 } else {
1652 if (! strncmp (symname, "defined", 7) && sym_length == 7)
1653 cpp_error (pfile, "invalid %s name `defined'", usage);
1654 }
1655 return sym_length;
1656 }
1657
1658 /* Return zero if two DEFINITIONs are isomorphic. */
1659
1660 static int
1661 compare_defs (pfile, d1, d2)
1662 cpp_reader *pfile;
1663 DEFINITION *d1, *d2;
1664 {
1665 register struct reflist *a1, *a2;
1666 register U_CHAR *p1 = d1->expansion;
1667 register U_CHAR *p2 = d2->expansion;
1668 int first = 1;
1669
1670 if (d1->nargs != d2->nargs)
1671 return 1;
1672 if (CPP_PEDANTIC (pfile)
1673 && strcmp ((char *)d1->args.argnames, (char *)d2->args.argnames))
1674 return 1;
1675 for (a1 = d1->pattern, a2 = d2->pattern; a1 && a2;
1676 a1 = a1->next, a2 = a2->next) {
1677 if (!((a1->nchars == a2->nchars && ! strncmp (p1, p2, a1->nchars))
1678 || ! comp_def_part (first, p1, a1->nchars, p2, a2->nchars, 0))
1679 || a1->argno != a2->argno
1680 || a1->stringify != a2->stringify
1681 || a1->raw_before != a2->raw_before
1682 || a1->raw_after != a2->raw_after)
1683 return 1;
1684 first = 0;
1685 p1 += a1->nchars;
1686 p2 += a2->nchars;
1687 }
1688 if (a1 != a2)
1689 return 1;
1690 if (comp_def_part (first, p1, d1->length - (p1 - d1->expansion),
1691 p2, d2->length - (p2 - d2->expansion), 1))
1692 return 1;
1693 return 0;
1694 }
1695
1696 /* Return 1 if two parts of two macro definitions are effectively different.
1697 One of the parts starts at BEG1 and has LEN1 chars;
1698 the other has LEN2 chars at BEG2.
1699 Any sequence of whitespace matches any other sequence of whitespace.
1700 FIRST means these parts are the first of a macro definition;
1701 so ignore leading whitespace entirely.
1702 LAST means these parts are the last of a macro definition;
1703 so ignore trailing whitespace entirely. */
1704
1705 static int
1706 comp_def_part (first, beg1, len1, beg2, len2, last)
1707 int first;
1708 U_CHAR *beg1, *beg2;
1709 int len1, len2;
1710 int last;
1711 {
1712 register U_CHAR *end1 = beg1 + len1;
1713 register U_CHAR *end2 = beg2 + len2;
1714 if (first) {
1715 while (beg1 != end1 && is_space[*beg1]) beg1++;
1716 while (beg2 != end2 && is_space[*beg2]) beg2++;
1717 }
1718 if (last) {
1719 while (beg1 != end1 && is_space[end1[-1]]) end1--;
1720 while (beg2 != end2 && is_space[end2[-1]]) end2--;
1721 }
1722 while (beg1 != end1 && beg2 != end2) {
1723 if (is_space[*beg1] && is_space[*beg2]) {
1724 while (beg1 != end1 && is_space[*beg1]) beg1++;
1725 while (beg2 != end2 && is_space[*beg2]) beg2++;
1726 } else if (*beg1 == *beg2) {
1727 beg1++; beg2++;
1728 } else break;
1729 }
1730 return (beg1 != end1) || (beg2 != end2);
1731 }
1732
1733 /* Process a #define command.
1734 BUF points to the contents of the #define command, as a contiguous string.
1735 LIMIT points to the first character past the end of the definition.
1736 KEYWORD is the keyword-table entry for #define,
1737 or NULL for a "predefined" macro. */
1738
1739 static int
1740 do_define (pfile, keyword, buf, limit)
1741 cpp_reader *pfile;
1742 struct directive *keyword;
1743 U_CHAR *buf, *limit;
1744 {
1745 int hashcode;
1746 MACRODEF mdef;
1747 HASHNODE *hp;
1748
1749 #if 0
1750 /* If this is a precompiler run (with -pcp) pass thru #define commands. */
1751 if (pcp_outfile && keyword)
1752 pass_thru_directive (buf, limit, pfile, keyword);
1753 #endif
1754
1755 mdef = create_definition (buf, limit, pfile, keyword == NULL);
1756 if (mdef.defn == 0)
1757 goto nope;
1758
1759 hashcode = hashf (mdef.symnam, mdef.symlen, HASHSIZE);
1760
1761 if ((hp = cpp_lookup (pfile, mdef.symnam, mdef.symlen, hashcode)) != NULL)
1762 {
1763 int ok = 0;
1764 /* Redefining a precompiled key is ok. */
1765 if (hp->type == T_PCSTRING)
1766 ok = 1;
1767 /* Redefining a macro is ok if the definitions are the same. */
1768 else if (hp->type == T_MACRO)
1769 ok = ! compare_defs (pfile, mdef.defn, hp->value.defn);
1770 /* Redefining a constant is ok with -D. */
1771 else if (hp->type == T_CONST)
1772 ok = ! CPP_OPTIONS (pfile)->done_initializing;
1773 /* Print the warning if it's not ok. */
1774 if (!ok)
1775 {
1776 U_CHAR *msg; /* what pain... */
1777
1778 /* If we are passing through #define and #undef directives, do
1779 that for this re-definition now. */
1780 if (CPP_OPTIONS (pfile)->debug_output && keyword)
1781 pass_thru_directive (buf, limit, pfile, keyword);
1782
1783 msg = (U_CHAR *) alloca (mdef.symlen + 22);
1784 *msg = '`';
1785 bcopy (mdef.symnam, msg + 1, mdef.symlen);
1786 strcpy ((char *) (msg + mdef.symlen + 1), "' redefined");
1787 cpp_pedwarn (pfile, msg);
1788 if (hp->type == T_MACRO)
1789 cpp_pedwarn_with_file_and_line (pfile, hp->value.defn->file, hp->value.defn->line,
1790 "this is the location of the previous definition");
1791 }
1792 /* Replace the old definition. */
1793 hp->type = T_MACRO;
1794 hp->value.defn = mdef.defn;
1795 }
1796 else
1797 {
1798 /* If we are passing through #define and #undef directives, do
1799 that for this new definition now. */
1800 if (CPP_OPTIONS (pfile)->debug_output && keyword)
1801 pass_thru_directive (buf, limit, pfile, keyword);
1802 install (mdef.symnam, mdef.symlen, T_MACRO, 0,
1803 (char *) mdef.defn, hashcode);
1804 }
1805
1806 return 0;
1807
1808 nope:
1809
1810 return 1;
1811 }
1812
1813 /* This structure represents one parsed argument in a macro call.
1814 `raw' points to the argument text as written (`raw_length' is its length).
1815 `expanded' points to the argument's macro-expansion
1816 (its length is `expand_length').
1817 `stringified_length' is the length the argument would have
1818 if stringified.
1819 `use_count' is the number of times this macro arg is substituted
1820 into the macro. If the actual use count exceeds 10,
1821 the value stored is 10. */
1822
1823 /* raw and expanded are relative to ARG_BASE */
1824 #define ARG_BASE ((pfile)->token_buffer)
1825
1826 struct argdata {
1827 /* Strings relative to pfile->token_buffer */
1828 long raw, expanded, stringified;
1829 int raw_length, expand_length;
1830 int stringified_length;
1831 char newlines;
1832 char use_count;
1833 };
1834
1835 /* Allocate a new cpp_buffer for PFILE, and push it on the input buffer stack.
1836 If BUFFER != NULL, then use the LENGTH characters in BUFFER
1837 as the new input buffer.
1838 Return the new buffer, or NULL on failure. */
1839
1840 cpp_buffer *
1841 cpp_push_buffer (pfile, buffer, length)
1842 cpp_reader *pfile;
1843 U_CHAR *buffer;
1844 long length;
1845 {
1846 register cpp_buffer *buf = CPP_BUFFER (pfile);
1847 if (buf == pfile->buffer_stack)
1848 {
1849 cpp_fatal (pfile, "%s: macro or `#include' recursion too deep",
1850 buf->fname);
1851 return NULL;
1852 }
1853 buf--;
1854 bzero ((char *) buf, sizeof (cpp_buffer));
1855 CPP_BUFFER (pfile) = buf;
1856 buf->if_stack = pfile->if_stack;
1857 buf->cleanup = null_cleanup;
1858 buf->underflow = null_underflow;
1859 buf->buf = buf->cur = buffer;
1860 buf->alimit = buf->rlimit = buffer + length;
1861
1862 return buf;
1863 }
1864
1865 cpp_buffer *
1866 cpp_pop_buffer (pfile)
1867 cpp_reader *pfile;
1868 {
1869 cpp_buffer *buf = CPP_BUFFER (pfile);
1870 (*buf->cleanup) (buf, pfile);
1871 return ++CPP_BUFFER (pfile);
1872 }
1873
1874 /* Scan until CPP_BUFFER (PFILE) is exhausted into PFILE->token_buffer.
1875 Pop the buffer when done. */
1876
1877 void
1878 cpp_scan_buffer (pfile)
1879 cpp_reader *pfile;
1880 {
1881 cpp_buffer *buffer = CPP_BUFFER (pfile);
1882 for (;;)
1883 {
1884 enum cpp_token token = cpp_get_token (pfile);
1885 if (token == CPP_EOF) /* Should not happen ... */
1886 break;
1887 if (token == CPP_POP && CPP_BUFFER (pfile) == buffer)
1888 {
1889 cpp_pop_buffer (pfile);
1890 break;
1891 }
1892 }
1893 }
1894
1895 /*
1896 * Rescan a string (which may have escape marks) into pfile's buffer.
1897 * Place the result in pfile->token_buffer.
1898 *
1899 * The input is copied before it is scanned, so it is safe to pass
1900 * it something from the token_buffer that will get overwritten
1901 * (because it follows CPP_WRITTEN). This is used by do_include.
1902 */
1903
1904 static void
1905 cpp_expand_to_buffer (pfile, buf, length)
1906 cpp_reader *pfile;
1907 U_CHAR *buf;
1908 int length;
1909 {
1910 register cpp_buffer *ip;
1911 #if 0
1912 cpp_buffer obuf;
1913 #endif
1914 U_CHAR *limit = buf + length;
1915 U_CHAR *buf1;
1916 #if 0
1917 int odepth = indepth;
1918 #endif
1919
1920 if (length < 0)
1921 abort ();
1922
1923 /* Set up the input on the input stack. */
1924
1925 buf1 = (U_CHAR *) alloca (length + 1);
1926 {
1927 register U_CHAR *p1 = buf;
1928 register U_CHAR *p2 = buf1;
1929
1930 while (p1 != limit)
1931 *p2++ = *p1++;
1932 }
1933 buf1[length] = 0;
1934
1935 ip = cpp_push_buffer (pfile, buf1, length);
1936 if (ip == NULL)
1937 return;
1938 ip->has_escapes = 1;
1939 #if 0
1940 ip->lineno = obuf.lineno = 1;
1941 #endif
1942
1943 /* Scan the input, create the output. */
1944 cpp_scan_buffer (pfile);
1945
1946 #if 0
1947 if (indepth != odepth)
1948 abort ();
1949 #endif
1950
1951 CPP_NUL_TERMINATE (pfile);
1952 }
1953
1954 \f
1955 static void
1956 adjust_position (buf, limit, linep, colp)
1957 U_CHAR *buf;
1958 U_CHAR *limit;
1959 long *linep;
1960 long *colp;
1961 {
1962 while (buf < limit)
1963 {
1964 U_CHAR ch = *buf++;
1965 if (ch == '\n')
1966 (*linep)++, (*colp) = 1;
1967 else
1968 (*colp)++;
1969 }
1970 }
1971
1972 /* Move line_base forward, updating lineno and colno. */
1973
1974 static void
1975 update_position (pbuf)
1976 register cpp_buffer *pbuf;
1977 {
1978 unsigned char *old_pos = pbuf->buf + pbuf->line_base;
1979 unsigned char *new_pos = pbuf->cur;
1980 register struct parse_marker *mark;
1981 for (mark = pbuf->marks; mark != NULL; mark = mark->next)
1982 {
1983 if (pbuf->buf + mark->position < new_pos)
1984 new_pos = pbuf->buf + mark->position;
1985 }
1986 pbuf->line_base += new_pos - old_pos;
1987 adjust_position (old_pos, new_pos, &pbuf->lineno, &pbuf->colno);
1988 }
1989
1990 void
1991 cpp_buf_line_and_col (pbuf, linep, colp)
1992 register cpp_buffer *pbuf;
1993 long *linep, *colp;
1994 {
1995 long dummy;
1996 if (colp == NULL)
1997 colp = &dummy;
1998 if (pbuf)
1999 {
2000 *linep = pbuf->lineno;
2001 *colp = pbuf->colno;
2002 adjust_position (pbuf->buf + pbuf->line_base, pbuf->cur, linep, colp);
2003 }
2004 else
2005 {
2006 *linep = 0;
2007 *colp = 0;
2008 }
2009 }
2010
2011 /* Return the cpp_buffer that corresponds to a file (not a macro). */
2012
2013 cpp_buffer *
2014 cpp_file_buffer (pfile)
2015 cpp_reader *pfile;
2016 {
2017 cpp_buffer *ip = CPP_BUFFER (pfile);
2018
2019 for ( ; ip != CPP_NULL_BUFFER (pfile); ip = CPP_PREV_BUFFER (ip))
2020 if (ip->fname != NULL)
2021 return ip;
2022 return NULL;
2023 }
2024
2025 static long
2026 count_newlines (buf, limit)
2027 register U_CHAR *buf;
2028 register U_CHAR *limit;
2029 {
2030 register long count = 0;
2031 while (buf < limit)
2032 {
2033 U_CHAR ch = *buf++;
2034 if (ch == '\n')
2035 count++;
2036 }
2037 return count;
2038 }
2039
2040 /*
2041 * write out a #line command, for instance, after an #include file.
2042 * If CONDITIONAL is nonzero, we can omit the #line if it would
2043 * appear to be a no-op, and we can output a few newlines instead
2044 * if we want to increase the line number by a small amount.
2045 * FILE_CHANGE says whether we are entering a file, leaving, or neither.
2046 */
2047
2048 static void
2049 output_line_command (pfile, conditional, file_change)
2050 cpp_reader *pfile;
2051 int conditional;
2052 enum file_change_code file_change;
2053 {
2054 long line, col;
2055 cpp_buffer *ip = CPP_BUFFER (pfile);
2056
2057 if (ip->fname == NULL)
2058 return;
2059
2060 update_position (ip);
2061
2062 if (CPP_OPTIONS (pfile)->no_line_commands
2063 || CPP_OPTIONS (pfile)->no_output)
2064 return;
2065
2066 line = CPP_BUFFER (pfile)->lineno;
2067 col = CPP_BUFFER (pfile)->colno;
2068 adjust_position (CPP_LINE_BASE (ip), ip->cur, &line, &col);
2069
2070 if (CPP_OPTIONS (pfile)->no_line_commands)
2071 return;
2072
2073 if (conditional) {
2074 if (line == pfile->lineno)
2075 return;
2076
2077 /* If the inherited line number is a little too small,
2078 output some newlines instead of a #line command. */
2079 if (line > pfile->lineno && line < pfile->lineno + 8) {
2080 CPP_RESERVE (pfile, 20);
2081 while (line > pfile->lineno) {
2082 CPP_PUTC_Q (pfile, '\n');
2083 pfile->lineno++;
2084 }
2085 return;
2086 }
2087 }
2088
2089 #if 0
2090 /* Don't output a line number of 0 if we can help it. */
2091 if (ip->lineno == 0 && ip->bufp - ip->buf < ip->length
2092 && *ip->bufp == '\n') {
2093 ip->lineno++;
2094 ip->bufp++;
2095 }
2096 #endif
2097
2098 CPP_RESERVE (pfile, 4 * strlen (ip->nominal_fname) + 50);
2099 {
2100 #ifdef OUTPUT_LINE_COMMANDS
2101 static char sharp_line[] = "#line ";
2102 #else
2103 static char sharp_line[] = "# ";
2104 #endif
2105 CPP_PUTS_Q (pfile, sharp_line, sizeof(sharp_line)-1);
2106 }
2107
2108 sprintf ((char *) CPP_PWRITTEN (pfile), "%ld ", line);
2109 CPP_ADJUST_WRITTEN (pfile, strlen (CPP_PWRITTEN (pfile)));
2110
2111 quote_string (pfile, ip->nominal_fname);
2112 if (file_change != same_file) {
2113 CPP_PUTC_Q (pfile, ' ');
2114 CPP_PUTC_Q (pfile, file_change == enter_file ? '1' : '2');
2115 }
2116 /* Tell cc1 if following text comes from a system header file. */
2117 if (ip->system_header_p) {
2118 CPP_PUTC_Q (pfile, ' ');
2119 CPP_PUTC_Q (pfile, '3');
2120 }
2121 #ifndef NO_IMPLICIT_EXTERN_C
2122 /* Tell cc1plus if following text should be treated as C. */
2123 if (ip->system_header_p == 2 && CPP_OPTIONS (pfile)->cplusplus) {
2124 CPP_PUTC_Q (pfile, ' ');
2125 CPP_PUTC_Q (pfile, '4');
2126 }
2127 #endif
2128 CPP_PUTC_Q (pfile, '\n');
2129 pfile->lineno = line;
2130 }
2131 \f
2132 /*
2133 * Parse a macro argument and append the info on PFILE's token_buffer.
2134 * REST_ARGS means to absorb the rest of the args.
2135 * Return nonzero to indicate a syntax error.
2136 */
2137
2138 static enum cpp_token
2139 macarg (pfile, rest_args)
2140 cpp_reader *pfile;
2141 int rest_args;
2142 {
2143 int paren = 0;
2144 enum cpp_token token;
2145 char save_put_out_comments = CPP_OPTIONS (pfile)->put_out_comments;
2146 CPP_OPTIONS (pfile)->put_out_comments = 0;
2147
2148 /* Try to parse as much of the argument as exists at this
2149 input stack level. */
2150 pfile->no_macro_expand++;
2151 for (;;)
2152 {
2153 token = cpp_get_token (pfile);
2154 switch (token)
2155 {
2156 case CPP_EOF:
2157 goto done;
2158 case CPP_POP:
2159 /* If we've hit end of file, it's an error (reported by caller).
2160 Ditto if it's the end of cpp_expand_to_buffer text.
2161 If we've hit end of macro, just continue. */
2162 if (! CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
2163 goto done;
2164 break;
2165 case CPP_LPAREN:
2166 paren++;
2167 break;
2168 case CPP_RPAREN:
2169 if (--paren < 0)
2170 goto found;
2171 break;
2172 case CPP_COMMA:
2173 /* if we've returned to lowest level and
2174 we aren't absorbing all args */
2175 if (paren == 0 && rest_args == 0)
2176 goto found;
2177 break;
2178 found:
2179 /* Remove ',' or ')' from argument buffer. */
2180 CPP_ADJUST_WRITTEN (pfile, -1);
2181 goto done;
2182 default: ;
2183 }
2184 }
2185
2186 done:
2187 CPP_OPTIONS (pfile)->put_out_comments = save_put_out_comments;
2188 pfile->no_macro_expand--;
2189
2190 return token;
2191 }
2192 \f
2193 /* Turn newlines to spaces in the string of length LENGTH at START,
2194 except inside of string constants.
2195 The string is copied into itself with its beginning staying fixed. */
2196
2197 static int
2198 change_newlines (start, length)
2199 U_CHAR *start;
2200 int length;
2201 {
2202 register U_CHAR *ibp;
2203 register U_CHAR *obp;
2204 register U_CHAR *limit;
2205 register int c;
2206
2207 ibp = start;
2208 limit = start + length;
2209 obp = start;
2210
2211 while (ibp < limit) {
2212 *obp++ = c = *ibp++;
2213 switch (c) {
2214
2215 case '\'':
2216 case '\"':
2217 /* Notice and skip strings, so that we don't delete newlines in them. */
2218 {
2219 int quotec = c;
2220 while (ibp < limit) {
2221 *obp++ = c = *ibp++;
2222 if (c == quotec)
2223 break;
2224 if (c == '\n' && quotec == '\'')
2225 break;
2226 }
2227 }
2228 break;
2229 }
2230 }
2231
2232 return obp - start;
2233 }
2234
2235 \f
2236 static struct tm *
2237 timestamp (pfile)
2238 cpp_reader *pfile;
2239 {
2240 if (!pfile->timebuf) {
2241 time_t t = time ((time_t *) 0);
2242 pfile->timebuf = localtime (&t);
2243 }
2244 return pfile->timebuf;
2245 }
2246
2247 static char *monthnames[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
2248 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
2249 };
2250
2251 /*
2252 * expand things like __FILE__. Place the expansion into the output
2253 * buffer *without* rescanning.
2254 */
2255
2256 static void
2257 special_symbol (hp, pfile)
2258 HASHNODE *hp;
2259 cpp_reader *pfile;
2260 {
2261 char *buf;
2262 int len;
2263 int true_indepth;
2264 cpp_buffer *ip = NULL;
2265 struct tm *timebuf;
2266
2267 int paren = 0; /* For special `defined' keyword */
2268
2269 #if 0
2270 if (pcp_outfile && pcp_inside_if
2271 && hp->type != T_SPEC_DEFINED && hp->type != T_CONST)
2272 cpp_error (pfile,
2273 "Predefined macro `%s' used inside `#if' during precompilation",
2274 hp->name);
2275 #endif
2276
2277 for (ip = CPP_BUFFER (pfile); ; ip = CPP_PREV_BUFFER (ip))
2278 {
2279 if (ip == CPP_NULL_BUFFER (pfile))
2280 {
2281 cpp_error (pfile, "cccp error: not in any file?!");
2282 return; /* the show must go on */
2283 }
2284 if (ip->fname != NULL)
2285 break;
2286 }
2287
2288 switch (hp->type)
2289 {
2290 case T_FILE:
2291 case T_BASE_FILE:
2292 {
2293 char *string;
2294 if (hp->type == T_BASE_FILE)
2295 {
2296 while (CPP_PREV_BUFFER (ip) != CPP_NULL_BUFFER (pfile))
2297 ip = CPP_PREV_BUFFER (ip);
2298 }
2299 string = ip->nominal_fname;
2300
2301 if (!string)
2302 string = "";
2303 CPP_RESERVE (pfile, 3 + 4 * strlen (string));
2304 quote_string (pfile, string);
2305 return;
2306 }
2307
2308 case T_INCLUDE_LEVEL:
2309 true_indepth = 0;
2310 ip = CPP_BUFFER (pfile);
2311 for (; ip != CPP_NULL_BUFFER (pfile); ip = CPP_PREV_BUFFER (ip))
2312 if (ip->fname != NULL)
2313 true_indepth++;
2314
2315 buf = (char *) alloca (8); /* Eight bytes ought to be more than enough */
2316 sprintf (buf, "%d", true_indepth - 1);
2317 break;
2318
2319 case T_VERSION:
2320 buf = (char *) alloca (3 + strlen (version_string));
2321 sprintf (buf, "\"%s\"", version_string);
2322 break;
2323
2324 #ifndef NO_BUILTIN_SIZE_TYPE
2325 case T_SIZE_TYPE:
2326 buf = SIZE_TYPE;
2327 break;
2328 #endif
2329
2330 #ifndef NO_BUILTIN_PTRDIFF_TYPE
2331 case T_PTRDIFF_TYPE:
2332 buf = PTRDIFF_TYPE;
2333 break;
2334 #endif
2335
2336 case T_WCHAR_TYPE:
2337 buf = CPP_WCHAR_TYPE (pfile);
2338 break;
2339
2340 case T_USER_LABEL_PREFIX_TYPE:
2341 buf = USER_LABEL_PREFIX;
2342 break;
2343
2344 case T_REGISTER_PREFIX_TYPE:
2345 buf = REGISTER_PREFIX;
2346 break;
2347
2348 case T_CONST:
2349 buf = (char *) alloca (4 * sizeof (int));
2350 sprintf (buf, "%d", hp->value.ival);
2351 #ifdef STDC_0_IN_SYSTEM_HEADERS
2352 if (ip->system_header_p
2353 && hp->length == 8 && bcmp (hp->name, "__STDC__", 8) == 0
2354 && ! cpp_lookup (pfile, (U_CHAR *) "__STRICT_ANSI__", -1, -1))
2355 strcpy (buf, "0");
2356 #endif
2357 #if 0
2358 if (pcp_inside_if && pcp_outfile)
2359 /* Output a precondition for this macro use */
2360 fprintf (pcp_outfile, "#define %s %d\n", hp->name, hp->value.ival);
2361 #endif
2362 break;
2363
2364 case T_SPECLINE:
2365 {
2366 long line = ip->lineno;
2367 long col = ip->colno;
2368 adjust_position (CPP_LINE_BASE (ip), ip->cur, &line, &col);
2369
2370 buf = (char *) alloca (10);
2371 sprintf (buf, "%ld", line);
2372 }
2373 break;
2374
2375 case T_DATE:
2376 case T_TIME:
2377 buf = (char *) alloca (20);
2378 timebuf = timestamp (pfile);
2379 if (hp->type == T_DATE)
2380 sprintf (buf, "\"%s %2d %4d\"", monthnames[timebuf->tm_mon],
2381 timebuf->tm_mday, timebuf->tm_year + 1900);
2382 else
2383 sprintf (buf, "\"%02d:%02d:%02d\"", timebuf->tm_hour, timebuf->tm_min,
2384 timebuf->tm_sec);
2385 break;
2386
2387 case T_SPEC_DEFINED:
2388 buf = " 0 "; /* Assume symbol is not defined */
2389 ip = CPP_BUFFER (pfile);
2390 SKIP_WHITE_SPACE (ip->cur);
2391 if (*ip->cur == '(')
2392 {
2393 paren++;
2394 ip->cur++; /* Skip over the paren */
2395 SKIP_WHITE_SPACE (ip->cur);
2396 }
2397
2398 if (!is_idstart[*ip->cur])
2399 goto oops;
2400 if (ip->cur[0] == 'L' && (ip->cur[1] == '\'' || ip->cur[1] == '"'))
2401 goto oops;
2402 if ((hp = cpp_lookup (pfile, ip->cur, -1, -1)))
2403 {
2404 #if 0
2405 if (pcp_outfile && pcp_inside_if
2406 && (hp->type == T_CONST
2407 || (hp->type == T_MACRO && hp->value.defn->predefined)))
2408 /* Output a precondition for this macro use. */
2409 fprintf (pcp_outfile, "#define %s\n", hp->name);
2410 #endif
2411 buf = " 1 ";
2412 }
2413 #if 0
2414 else
2415 if (pcp_outfile && pcp_inside_if)
2416 {
2417 /* Output a precondition for this macro use */
2418 U_CHAR *cp = ip->bufp;
2419 fprintf (pcp_outfile, "#undef ");
2420 while (is_idchar[*cp]) /* Ick! */
2421 fputc (*cp++, pcp_outfile);
2422 putc ('\n', pcp_outfile);
2423 }
2424 #endif
2425 while (is_idchar[*ip->cur])
2426 ++ip->cur;
2427 SKIP_WHITE_SPACE (ip->cur);
2428 if (paren)
2429 {
2430 if (*ip->cur != ')')
2431 goto oops;
2432 ++ip->cur;
2433 }
2434 break;
2435
2436 oops:
2437
2438 cpp_error (pfile, "`defined' without an identifier");
2439 break;
2440
2441 default:
2442 cpp_error (pfile, "cccp error: invalid special hash type"); /* time for gdb */
2443 abort ();
2444 }
2445 len = strlen (buf);
2446 CPP_RESERVE (pfile, len + 1);
2447 CPP_PUTS_Q (pfile, buf, len);
2448 CPP_NUL_TERMINATE_Q (pfile);
2449
2450 return;
2451 }
2452
2453 /* Write out a #define command for the special named MACRO_NAME
2454 to PFILE's token_buffer. */
2455
2456 static void
2457 dump_special_to_buffer (pfile, macro_name)
2458 cpp_reader *pfile;
2459 char *macro_name;
2460 {
2461 static char define_directive[] = "#define ";
2462 int macro_name_length = strlen (macro_name);
2463 output_line_command (pfile, 0, same_file);
2464 CPP_RESERVE (pfile, sizeof(define_directive) + macro_name_length);
2465 CPP_PUTS_Q (pfile, define_directive, sizeof(define_directive)-1);
2466 CPP_PUTS_Q (pfile, macro_name, macro_name_length);
2467 CPP_PUTC_Q (pfile, ' ');
2468 cpp_expand_to_buffer (pfile, macro_name, macro_name_length);
2469 CPP_PUTC (pfile, '\n');
2470 }
2471
2472 /* Initialize the built-in macros. */
2473
2474 static void
2475 initialize_builtins (pfile)
2476 cpp_reader *pfile;
2477 {
2478 install ((U_CHAR *)"__LINE__", -1, T_SPECLINE, 0, 0, -1);
2479 install ((U_CHAR *)"__DATE__", -1, T_DATE, 0, 0, -1);
2480 install ((U_CHAR *)"__FILE__", -1, T_FILE, 0, 0, -1);
2481 install ((U_CHAR *)"__BASE_FILE__", -1, T_BASE_FILE, 0, 0, -1);
2482 install ((U_CHAR *)"__INCLUDE_LEVEL__", -1, T_INCLUDE_LEVEL, 0, 0, -1);
2483 install ((U_CHAR *)"__VERSION__", -1, T_VERSION, 0, 0, -1);
2484 #ifndef NO_BUILTIN_SIZE_TYPE
2485 install ((U_CHAR *)"__SIZE_TYPE__", -1, T_SIZE_TYPE, 0, 0, -1);
2486 #endif
2487 #ifndef NO_BUILTIN_PTRDIFF_TYPE
2488 install ((U_CHAR *)"__PTRDIFF_TYPE__ ", -1, T_PTRDIFF_TYPE, 0, 0, -1);
2489 #endif
2490 install ((U_CHAR *)"__WCHAR_TYPE__", -1, T_WCHAR_TYPE, 0, 0, -1);
2491 install ((U_CHAR *)"__USER_LABEL_PREFIX__", -1, T_USER_LABEL_PREFIX_TYPE, 0, 0, -1);
2492 install ((U_CHAR *)"__REGISTER_PREFIX__", -1, T_REGISTER_PREFIX_TYPE, 0, 0, -1);
2493 install ((U_CHAR *)"__TIME__", -1, T_TIME, 0, 0, -1);
2494 if (!CPP_TRADITIONAL (pfile))
2495 install ((U_CHAR *)"__STDC__", -1, T_CONST, STDC_VALUE, 0, -1);
2496 if (CPP_OPTIONS (pfile)->objc)
2497 install ((U_CHAR *)"__OBJC__", -1, T_CONST, 1, 0, -1);
2498 /* This is supplied using a -D by the compiler driver
2499 so that it is present only when truly compiling with GNU C. */
2500 /* install ("__GNUC__", -1, T_CONST, 2, 0, -1); */
2501
2502 if (CPP_OPTIONS (pfile)->debug_output)
2503 {
2504 dump_special_to_buffer (pfile, "__BASE_FILE__");
2505 dump_special_to_buffer (pfile, "__VERSION__");
2506 #ifndef NO_BUILTIN_SIZE_TYPE
2507 dump_special_to_buffer (pfile, "__SIZE_TYPE__");
2508 #endif
2509 #ifndef NO_BUILTIN_PTRDIFF_TYPE
2510 dump_special_to_buffer (pfile, "__PTRDIFF_TYPE__");
2511 #endif
2512 dump_special_to_buffer (pfile, "__WCHAR_TYPE__");
2513 dump_special_to_buffer (pfile, "__DATE__");
2514 dump_special_to_buffer (pfile, "__TIME__");
2515 if (!CPP_TRADITIONAL (pfile))
2516 dump_special_to_buffer (pfile, "__STDC__");
2517 if (CPP_OPTIONS (pfile)->objc)
2518 dump_special_to_buffer (pfile, "__OBJC__");
2519 }
2520 }
2521 \f
2522 /* Return 1 iff a token ending in C1 followed directly by a token C2
2523 could cause mis-tokenization. */
2524
2525 static int
2526 unsafe_chars (c1, c2)
2527 int c1, c2;
2528 {
2529 switch (c1)
2530 {
2531 case '+': case '-':
2532 if (c2 == c1 || c2 == '=')
2533 return 1;
2534 goto letter;
2535 case '.':
2536 case '0': case '1': case '2': case '3': case '4':
2537 case '5': case '6': case '7': case '8': case '9':
2538 case 'e': case 'E': case 'p': case 'P':
2539 if (c2 == '-' || c2 == '+')
2540 return 1; /* could extend a pre-processing number */
2541 goto letter;
2542 case 'L':
2543 if (c2 == '\'' || c2 == '\"')
2544 return 1; /* Could turn into L"xxx" or L'xxx'. */
2545 goto letter;
2546 letter:
2547 case '_':
2548 case 'a': case 'b': case 'c': case 'd': case 'f':
2549 case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
2550 case 'm': case 'n': case 'o': case 'q': case 'r':
2551 case 's': case 't': case 'u': case 'v': case 'w': case 'x':
2552 case 'y': case 'z':
2553 case 'A': case 'B': case 'C': case 'D': case 'F':
2554 case 'G': case 'H': case 'I': case 'J': case 'K':
2555 case 'M': case 'N': case 'O': case 'Q': case 'R':
2556 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
2557 case 'Y': case 'Z':
2558 /* We're in the middle of either a name or a pre-processing number. */
2559 return (is_idchar[c2] || c2 == '.');
2560 case '<': case '>': case '!': case '%': case '#': case ':':
2561 case '^': case '&': case '|': case '*': case '/': case '=':
2562 return (c2 == c1 || c2 == '=');
2563 }
2564 return 0;
2565 }
2566
2567 /* Expand a macro call.
2568 HP points to the symbol that is the macro being called.
2569 Put the result of expansion onto the input stack
2570 so that subsequent input by our caller will use it.
2571
2572 If macro wants arguments, caller has already verified that
2573 an argument list follows; arguments come from the input stack. */
2574
2575 static void
2576 macroexpand (pfile, hp)
2577 cpp_reader *pfile;
2578 HASHNODE *hp;
2579 {
2580 int nargs;
2581 DEFINITION *defn = hp->value.defn;
2582 register U_CHAR *xbuf;
2583 long start_line, start_column;
2584 int xbuf_len;
2585 struct argdata *args;
2586 long old_written = CPP_WRITTEN (pfile);
2587 #if 0
2588 int start_line = instack[indepth].lineno;
2589 #endif
2590 int rest_args, rest_zero;
2591 register int i;
2592
2593 #if 0
2594 CHECK_DEPTH (return;);
2595 #endif
2596
2597 #if 0
2598 /* This macro is being used inside a #if, which means it must be */
2599 /* recorded as a precondition. */
2600 if (pcp_inside_if && pcp_outfile && defn->predefined)
2601 dump_single_macro (hp, pcp_outfile);
2602 #endif
2603
2604 pfile->output_escapes++;
2605 cpp_buf_line_and_col (cpp_file_buffer (pfile), &start_line, &start_column);
2606
2607 nargs = defn->nargs;
2608
2609 if (nargs >= 0)
2610 {
2611 enum cpp_token token;
2612
2613 args = (struct argdata *) alloca ((nargs + 1) * sizeof (struct argdata));
2614
2615 for (i = 0; i < nargs; i++)
2616 {
2617 args[i].raw = args[i].expanded = 0;
2618 args[i].raw_length = 0;
2619 args[i].expand_length = args[i].stringified_length = -1;
2620 args[i].use_count = 0;
2621 }
2622
2623 /* Parse all the macro args that are supplied. I counts them.
2624 The first NARGS args are stored in ARGS.
2625 The rest are discarded. If rest_args is set then we assume
2626 macarg absorbed the rest of the args. */
2627 i = 0;
2628 rest_args = 0;
2629 rest_args = 0;
2630 FORWARD(1); /* Discard the open-parenthesis before the first arg. */
2631 do
2632 {
2633 if (rest_args)
2634 continue;
2635 if (i < nargs || (nargs == 0 && i == 0))
2636 {
2637 /* if we are working on last arg which absorbs rest of args... */
2638 if (i == nargs - 1 && defn->rest_args)
2639 rest_args = 1;
2640 args[i].raw = CPP_WRITTEN (pfile);
2641 token = macarg (pfile, rest_args);
2642 args[i].raw_length = CPP_WRITTEN (pfile) - args[i].raw;
2643 args[i].newlines = 0; /* FIXME */
2644 }
2645 else
2646 token = macarg (pfile, 0);
2647 if (token == CPP_EOF || token == CPP_POP)
2648 {
2649 cpp_error_with_line (pfile, start_line, start_column,
2650 "unterminated macro call");
2651 return;
2652 }
2653 i++;
2654 } while (token == CPP_COMMA);
2655
2656 /* If we got one arg but it was just whitespace, call that 0 args. */
2657 if (i == 1)
2658 {
2659 register U_CHAR *bp = ARG_BASE + args[0].raw;
2660 register U_CHAR *lim = bp + args[0].raw_length;
2661 /* cpp.texi says for foo ( ) we provide one argument.
2662 However, if foo wants just 0 arguments, treat this as 0. */
2663 if (nargs == 0)
2664 while (bp != lim && is_space[*bp]) bp++;
2665 if (bp == lim)
2666 i = 0;
2667 }
2668
2669 /* Don't output an error message if we have already output one for
2670 a parse error above. */
2671 rest_zero = 0;
2672 if (nargs == 0 && i > 0)
2673 {
2674 cpp_error (pfile, "arguments given to macro `%s'", hp->name);
2675 }
2676 else if (i < nargs)
2677 {
2678 /* traditional C allows foo() if foo wants one argument. */
2679 if (nargs == 1 && i == 0 && CPP_TRADITIONAL (pfile))
2680 ;
2681 /* the rest args token is allowed to absorb 0 tokens */
2682 else if (i == nargs - 1 && defn->rest_args)
2683 rest_zero = 1;
2684 else if (i == 0)
2685 cpp_error (pfile, "macro `%s' used without args", hp->name);
2686 else if (i == 1)
2687 cpp_error (pfile, "macro `%s' used with just one arg", hp->name);
2688 else
2689 cpp_error (pfile, "macro `%s' used with only %d args",
2690 hp->name, i);
2691 }
2692 else if (i > nargs)
2693 {
2694 cpp_error (pfile,
2695 "macro `%s' used with too many (%d) args", hp->name, i);
2696 }
2697 }
2698
2699 /* If macro wants zero args, we parsed the arglist for checking only.
2700 Read directly from the macro definition. */
2701 if (nargs <= 0)
2702 {
2703 xbuf = defn->expansion;
2704 xbuf_len = defn->length;
2705 }
2706 else
2707 {
2708 register U_CHAR *exp = defn->expansion;
2709 register int offset; /* offset in expansion,
2710 copied a piece at a time */
2711 register int totlen; /* total amount of exp buffer filled so far */
2712
2713 register struct reflist *ap, *last_ap;
2714
2715 /* Macro really takes args. Compute the expansion of this call. */
2716
2717 /* Compute length in characters of the macro's expansion.
2718 Also count number of times each arg is used. */
2719 xbuf_len = defn->length;
2720 for (ap = defn->pattern; ap != NULL; ap = ap->next)
2721 {
2722 if (ap->stringify)
2723 {
2724 register struct argdata *arg = &args[ap->argno];
2725 /* Stringify if it hasn't already been */
2726 if (arg->stringified_length < 0)
2727 {
2728 int arglen = arg->raw_length;
2729 int escaped = 0;
2730 int in_string = 0;
2731 int c;
2732 /* Initially need_space is -1. Otherwise, 1 means the
2733 previous character was a space, but we suppressed it;
2734 0 means the previous character was a non-space. */
2735 int need_space = -1;
2736 i = 0;
2737 arg->stringified = CPP_WRITTEN (pfile);
2738 if (!CPP_TRADITIONAL (pfile))
2739 CPP_PUTC (pfile, '\"'); /* insert beginning quote */
2740 for (; i < arglen; i++)
2741 {
2742 c = (ARG_BASE + arg->raw)[i];
2743
2744 if (! in_string)
2745 {
2746 /* Internal sequences of whitespace are replaced by
2747 one space except within an string or char token.*/
2748 if (is_space[c])
2749 {
2750 if (CPP_WRITTEN (pfile) > arg->stringified
2751 && (CPP_PWRITTEN (pfile))[-1] == '@')
2752 {
2753 /* "@ " escape markers are removed */
2754 CPP_ADJUST_WRITTEN (pfile, -1);
2755 continue;
2756 }
2757 if (need_space == 0)
2758 need_space = 1;
2759 continue;
2760 }
2761 else if (need_space > 0)
2762 CPP_PUTC (pfile, ' ');
2763 need_space = 0;
2764 }
2765
2766 if (escaped)
2767 escaped = 0;
2768 else
2769 {
2770 if (c == '\\')
2771 escaped = 1;
2772 if (in_string)
2773 {
2774 if (c == in_string)
2775 in_string = 0;
2776 }
2777 else if (c == '\"' || c == '\'')
2778 in_string = c;
2779 }
2780
2781 /* Escape these chars */
2782 if (c == '\"' || (in_string && c == '\\'))
2783 CPP_PUTC (pfile, '\\');
2784 if (ISPRINT (c))
2785 CPP_PUTC (pfile, c);
2786 else
2787 {
2788 CPP_RESERVE (pfile, 4);
2789 sprintf ((char *)CPP_PWRITTEN (pfile), "\\%03o",
2790 (unsigned int) c);
2791 CPP_ADJUST_WRITTEN (pfile, 4);
2792 }
2793 }
2794 if (!CPP_TRADITIONAL (pfile))
2795 CPP_PUTC (pfile, '\"'); /* insert ending quote */
2796 arg->stringified_length
2797 = CPP_WRITTEN (pfile) - arg->stringified;
2798 }
2799 xbuf_len += args[ap->argno].stringified_length;
2800 }
2801 else if (ap->raw_before || ap->raw_after || CPP_TRADITIONAL (pfile))
2802 /* Add 4 for two newline-space markers to prevent
2803 token concatenation. */
2804 xbuf_len += args[ap->argno].raw_length + 4;
2805 else
2806 {
2807 /* We have an ordinary (expanded) occurrence of the arg.
2808 So compute its expansion, if we have not already. */
2809 if (args[ap->argno].expand_length < 0)
2810 {
2811 args[ap->argno].expanded = CPP_WRITTEN (pfile);
2812 cpp_expand_to_buffer (pfile,
2813 ARG_BASE + args[ap->argno].raw,
2814 args[ap->argno].raw_length);
2815
2816 args[ap->argno].expand_length
2817 = CPP_WRITTEN (pfile) - args[ap->argno].expanded;
2818 }
2819
2820 /* Add 4 for two newline-space markers to prevent
2821 token concatenation. */
2822 xbuf_len += args[ap->argno].expand_length + 4;
2823 }
2824 if (args[ap->argno].use_count < 10)
2825 args[ap->argno].use_count++;
2826 }
2827
2828 xbuf = (U_CHAR *) xmalloc (xbuf_len + 1);
2829
2830 /* Generate in XBUF the complete expansion
2831 with arguments substituted in.
2832 TOTLEN is the total size generated so far.
2833 OFFSET is the index in the definition
2834 of where we are copying from. */
2835 offset = totlen = 0;
2836 for (last_ap = NULL, ap = defn->pattern; ap != NULL;
2837 last_ap = ap, ap = ap->next)
2838 {
2839 register struct argdata *arg = &args[ap->argno];
2840 int count_before = totlen;
2841
2842 /* Add chars to XBUF. */
2843 for (i = 0; i < ap->nchars; i++, offset++)
2844 xbuf[totlen++] = exp[offset];
2845
2846 /* If followed by an empty rest arg with concatenation,
2847 delete the last run of nonwhite chars. */
2848 if (rest_zero && totlen > count_before
2849 && ((ap->rest_args && ap->raw_before)
2850 || (last_ap != NULL && last_ap->rest_args
2851 && last_ap->raw_after)))
2852 {
2853 /* Delete final whitespace. */
2854 while (totlen > count_before && is_space[xbuf[totlen - 1]])
2855 totlen--;
2856
2857 /* Delete the nonwhites before them. */
2858 while (totlen > count_before && ! is_space[xbuf[totlen - 1]])
2859 totlen--;
2860 }
2861
2862 if (ap->stringify != 0)
2863 {
2864 bcopy (ARG_BASE + arg->stringified,
2865 xbuf + totlen, arg->stringified_length);
2866 totlen += arg->stringified_length;
2867 }
2868 else if (ap->raw_before || ap->raw_after || CPP_TRADITIONAL (pfile))
2869 {
2870 U_CHAR *p1 = ARG_BASE + arg->raw;
2871 U_CHAR *l1 = p1 + arg->raw_length;
2872 if (ap->raw_before)
2873 {
2874 while (p1 != l1 && is_space[*p1]) p1++;
2875 while (p1 != l1 && is_idchar[*p1])
2876 xbuf[totlen++] = *p1++;
2877 /* Delete any no-reexpansion marker that follows
2878 an identifier at the beginning of the argument
2879 if the argument is concatenated with what precedes it. */
2880 if (p1[0] == '@' && p1[1] == '-')
2881 p1 += 2;
2882 }
2883 if (ap->raw_after)
2884 {
2885 /* Arg is concatenated after: delete trailing whitespace,
2886 whitespace markers, and no-reexpansion markers. */
2887 while (p1 != l1)
2888 {
2889 if (is_space[l1[-1]]) l1--;
2890 else if (l1[-1] == '-')
2891 {
2892 U_CHAR *p2 = l1 - 1;
2893 /* If a `-' is preceded by an odd number of newlines then it
2894 and the last newline are a no-reexpansion marker. */
2895 while (p2 != p1 && p2[-1] == '\n') p2--;
2896 if ((l1 - 1 - p2) & 1) {
2897 l1 -= 2;
2898 }
2899 else break;
2900 }
2901 else break;
2902 }
2903 }
2904
2905 bcopy (p1, xbuf + totlen, l1 - p1);
2906 totlen += l1 - p1;
2907 }
2908 else
2909 {
2910 U_CHAR *expanded = ARG_BASE + arg->expanded;
2911 if (!ap->raw_before && totlen > 0 && arg->expand_length
2912 && !CPP_TRADITIONAL(pfile)
2913 && unsafe_chars (xbuf[totlen-1], expanded[0]))
2914 {
2915 xbuf[totlen++] = '@';
2916 xbuf[totlen++] = ' ';
2917 }
2918
2919 bcopy (expanded, xbuf + totlen, arg->expand_length);
2920 totlen += arg->expand_length;
2921
2922 if (!ap->raw_after && totlen > 0 && offset < defn->length
2923 && !CPP_TRADITIONAL(pfile)
2924 && unsafe_chars (xbuf[totlen-1], exp[offset]))
2925 {
2926 xbuf[totlen++] = '@';
2927 xbuf[totlen++] = ' ';
2928 }
2929
2930 /* If a macro argument with newlines is used multiple times,
2931 then only expand the newlines once. This avoids creating
2932 output lines which don't correspond to any input line,
2933 which confuses gdb and gcov. */
2934 if (arg->use_count > 1 && arg->newlines > 0)
2935 {
2936 /* Don't bother doing change_newlines for subsequent
2937 uses of arg. */
2938 arg->use_count = 1;
2939 arg->expand_length
2940 = change_newlines (expanded, arg->expand_length);
2941 }
2942 }
2943
2944 if (totlen > xbuf_len)
2945 abort ();
2946 }
2947
2948 /* if there is anything left of the definition
2949 after handling the arg list, copy that in too. */
2950
2951 for (i = offset; i < defn->length; i++)
2952 {
2953 /* if we've reached the end of the macro */
2954 if (exp[i] == ')')
2955 rest_zero = 0;
2956 if (! (rest_zero && last_ap != NULL && last_ap->rest_args
2957 && last_ap->raw_after))
2958 xbuf[totlen++] = exp[i];
2959 }
2960
2961 xbuf[totlen] = 0;
2962 xbuf_len = totlen;
2963
2964 }
2965
2966 pfile->output_escapes--;
2967
2968 /* Now put the expansion on the input stack
2969 so our caller will commence reading from it. */
2970 push_macro_expansion (pfile, xbuf, xbuf_len, hp);
2971 CPP_BUFFER (pfile)->has_escapes = 1;
2972
2973 /* Pop the space we've used in the token_buffer for argument expansion. */
2974 CPP_SET_WRITTEN (pfile, old_written);
2975
2976 /* Recursive macro use sometimes works traditionally.
2977 #define foo(x,y) bar (x (y,0), y)
2978 foo (foo, baz) */
2979
2980 if (!CPP_TRADITIONAL (pfile))
2981 hp->type = T_DISABLED;
2982 }
2983
2984 static void
2985 push_macro_expansion (pfile, xbuf, xbuf_len, hp)
2986 cpp_reader *pfile;
2987 register U_CHAR *xbuf;
2988 int xbuf_len;
2989 HASHNODE *hp;
2990 {
2991 register cpp_buffer *mbuf = cpp_push_buffer (pfile, xbuf, xbuf_len);
2992 if (mbuf == NULL)
2993 return;
2994 mbuf->cleanup = macro_cleanup;
2995 mbuf->data = hp;
2996
2997 /* The first chars of the expansion should be a "@ " added by
2998 collect_expansion. This is to prevent accidental token-pasting
2999 between the text preceding the macro invocation, and the macro
3000 expansion text.
3001
3002 We would like to avoid adding unneeded spaces (for the sake of
3003 tools that use cpp, such as imake). In some common cases we can
3004 tell that it is safe to omit the space.
3005
3006 The character before the macro invocation cannot have been an
3007 idchar (or else it would have been pasted with the idchars of
3008 the macro name). Therefore, if the first non-space character
3009 of the expansion is an idchar, we do not need the extra space
3010 to prevent token pasting.
3011
3012 Also, we don't need the extra space if the first char is '(',
3013 or some other (less common) characters. */
3014
3015 if (xbuf[0] == '@' && xbuf[1] == ' '
3016 && (is_idchar[xbuf[2]] || xbuf[2] == '(' || xbuf[2] == '\''
3017 || xbuf[2] == '\"'))
3018 mbuf->cur += 2;
3019 }
3020 \f
3021 /* Like cpp_get_token, except that it does not read past end-of-line.
3022 Also, horizontal space is skipped, and macros are popped. */
3023
3024 static enum cpp_token
3025 get_directive_token (pfile)
3026 cpp_reader *pfile;
3027 {
3028 for (;;)
3029 {
3030 long old_written = CPP_WRITTEN (pfile);
3031 enum cpp_token token;
3032 cpp_skip_hspace (pfile);
3033 if (PEEKC () == '\n')
3034 return CPP_VSPACE;
3035 token = cpp_get_token (pfile);
3036 switch (token)
3037 {
3038 case CPP_POP:
3039 if (! CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
3040 return token;
3041 /* ... else fall though ... */
3042 case CPP_HSPACE: case CPP_COMMENT:
3043 CPP_SET_WRITTEN (pfile, old_written);
3044 break;
3045 default:
3046 return token;
3047 }
3048 }
3049 }
3050 \f
3051 /* Handle #include and #import.
3052 This function expects to see "fname" or <fname> on the input.
3053
3054 The input is normally in part of the output_buffer following
3055 CPP_WRITTEN, and will get overwritten by output_line_command.
3056 I.e. in input file specification has been popped by handle_directive.
3057 This is safe. */
3058
3059 static int
3060 do_include (pfile, keyword, unused1, unused2)
3061 cpp_reader *pfile;
3062 struct directive *keyword;
3063 U_CHAR *unused1 ATTRIBUTE_UNUSED, *unused2 ATTRIBUTE_UNUSED;
3064 {
3065 int importing = (keyword->type == T_IMPORT);
3066 int skip_dirs = (keyword->type == T_INCLUDE_NEXT);
3067 char *fname; /* Dynamically allocated fname buffer */
3068 char *pcftry;
3069 U_CHAR *fbeg, *fend; /* Beginning and end of fname */
3070 enum cpp_token token;
3071
3072 /* Chain of dirs to search */
3073 struct file_name_list *search_start = CPP_OPTIONS (pfile)->include;
3074 struct file_name_list dsp[1]; /* First in chain, if #include "..." */
3075 struct file_name_list *searchptr = 0;
3076 long old_written = CPP_WRITTEN (pfile);
3077
3078 int flen;
3079
3080 int f; /* file number */
3081
3082 int angle_brackets = 0; /* 0 for "...", 1 for <...> */
3083 char *pcfbuf;
3084 #if 0
3085 int pcf = -1;
3086 char *pcfbuflimit;
3087 #endif
3088 int pcfnum;
3089 f= -1; /* JF we iz paranoid! */
3090
3091 if (CPP_PEDANTIC (pfile) && !CPP_BUFFER (pfile)->system_header_p)
3092 {
3093 if (importing)
3094 cpp_pedwarn (pfile, "ANSI C does not allow `#import'");
3095 if (skip_dirs)
3096 cpp_pedwarn (pfile, "ANSI C does not allow `#include_next'");
3097 }
3098
3099 if (importing && CPP_OPTIONS (pfile)->warn_import
3100 && !CPP_OPTIONS (pfile)->inhibit_warnings
3101 && !CPP_BUFFER (pfile)->system_header_p && !pfile->import_warning)
3102 {
3103 pfile->import_warning = 1;
3104 cpp_warning (pfile, "using `#import' is not recommended");
3105 fprintf (stderr, "The fact that a certain header file need not be processed more than once\n");
3106 fprintf (stderr, "should be indicated in the header file, not where it is used.\n");
3107 fprintf (stderr, "The best way to do this is with a conditional of this form:\n\n");
3108 fprintf (stderr, " #ifndef _FOO_H_INCLUDED\n");
3109 fprintf (stderr, " #define _FOO_H_INCLUDED\n");
3110 fprintf (stderr, " ... <real contents of file> ...\n");
3111 fprintf (stderr, " #endif /* Not _FOO_H_INCLUDED */\n\n");
3112 fprintf (stderr, "Then users can use `#include' any number of times.\n");
3113 fprintf (stderr, "GNU C automatically avoids processing the file more than once\n");
3114 fprintf (stderr, "when it is equipped with such a conditional.\n");
3115 }
3116
3117 pfile->parsing_include_directive++;
3118 token = get_directive_token (pfile);
3119 pfile->parsing_include_directive--;
3120
3121 if (token == CPP_STRING)
3122 {
3123 /* FIXME - check no trailing garbage */
3124 fbeg = pfile->token_buffer + old_written + 1;
3125 fend = CPP_PWRITTEN (pfile) - 1;
3126 if (fbeg[-1] == '<')
3127 {
3128 angle_brackets = 1;
3129 /* If -I-, start with the first -I dir after the -I-. */
3130 if (CPP_OPTIONS (pfile)->first_bracket_include)
3131 search_start = CPP_OPTIONS (pfile)->first_bracket_include;
3132 }
3133 /* If -I- was specified, don't search current dir, only spec'd ones. */
3134 else if (! CPP_OPTIONS (pfile)->ignore_srcdir)
3135 {
3136 cpp_buffer *fp = CPP_BUFFER (pfile);
3137 /* We have "filename". Figure out directory this source
3138 file is coming from and put it on the front of the list. */
3139
3140 for ( ; fp != CPP_NULL_BUFFER (pfile); fp = CPP_PREV_BUFFER (fp))
3141 {
3142 int n;
3143 char *ep,*nam;
3144
3145 if ((nam = fp->nominal_fname) != NULL)
3146 {
3147 /* Found a named file. Figure out dir of the file,
3148 and put it in front of the search list. */
3149 dsp[0].next = search_start;
3150 search_start = dsp;
3151 #ifndef VMS
3152 ep = rindex (nam, '/');
3153 #else /* VMS */
3154 ep = rindex (nam, ']');
3155 if (ep == NULL) ep = rindex (nam, '>');
3156 if (ep == NULL) ep = rindex (nam, ':');
3157 if (ep != NULL) ep++;
3158 #endif /* VMS */
3159 if (ep != NULL)
3160 {
3161 n = ep - nam;
3162 dsp[0].fname = (char *) alloca (n + 1);
3163 strncpy (dsp[0].fname, nam, n);
3164 dsp[0].fname[n] = '\0';
3165 if (n + INCLUDE_LEN_FUDGE > pfile->max_include_len)
3166 pfile->max_include_len = n + INCLUDE_LEN_FUDGE;
3167 }
3168 else
3169 {
3170 dsp[0].fname = 0; /* Current directory */
3171 }
3172 dsp[0].got_name_map = 0;
3173 break;
3174 }
3175 }
3176 }
3177 }
3178 #ifdef VMS
3179 else if (token == CPP_NAME)
3180 {
3181 /*
3182 * Support '#include xyz' like VAX-C to allow for easy use of all the
3183 * decwindow include files. It defaults to '#include <xyz.h>' (so the
3184 * code from case '<' is repeated here) and generates a warning.
3185 */
3186 cpp_warning (pfile,
3187 "VAX-C-style include specification found, use '#include <filename.h>' !");
3188 angle_brackets = 1;
3189 /* If -I-, start with the first -I dir after the -I-. */
3190 if (CPP_OPTIONS (pfile)->first_bracket_include)
3191 search_start = CPP_OPTIONS (pfile)->first_bracket_include;
3192 fbeg = pfile->token_buffer + old_written;
3193 fend = CPP_PWRITTEN (pfile);
3194 }
3195 #endif
3196 else
3197 {
3198 cpp_error (pfile,
3199 "`#%s' expects \"FILENAME\" or <FILENAME>", keyword->name);
3200 CPP_SET_WRITTEN (pfile, old_written);
3201 skip_rest_of_line (pfile);
3202 return 0;
3203 }
3204
3205 *fend = 0;
3206
3207 token = get_directive_token (pfile);
3208 if (token != CPP_VSPACE)
3209 {
3210 cpp_error (pfile, "junk at end of `#include'");
3211 while (token != CPP_VSPACE && token != CPP_EOF && token != CPP_POP)
3212 token = get_directive_token (pfile);
3213 }
3214
3215 /* For #include_next, skip in the search path
3216 past the dir in which the containing file was found. */
3217 if (skip_dirs)
3218 {
3219 cpp_buffer *fp = CPP_BUFFER (pfile);
3220 for (; fp != CPP_NULL_BUFFER (pfile); fp = CPP_PREV_BUFFER (fp))
3221 if (fp->fname != NULL)
3222 {
3223 /* fp->dir is null if the containing file was specified with
3224 an absolute file name. In that case, don't skip anything. */
3225 if (fp->dir == SELF_DIR_DUMMY)
3226 search_start = CPP_OPTIONS (pfile)->include;
3227 else if (fp->dir)
3228 search_start = fp->dir->next;
3229 break;
3230 }
3231 }
3232
3233 CPP_SET_WRITTEN (pfile, old_written);
3234
3235 flen = fend - fbeg;
3236
3237 if (flen == 0)
3238 {
3239 cpp_error (pfile, "empty file name in `#%s'", keyword->name);
3240 return 0;
3241 }
3242
3243 /* Allocate this permanently, because it gets stored in the definitions
3244 of macros. */
3245 fname = (char *) xmalloc (pfile->max_include_len + flen + 4);
3246 /* + 2 above for slash and terminating null. */
3247 /* + 2 added for '.h' on VMS (to support '#include filename') */
3248
3249 /* If specified file name is absolute, just open it. */
3250
3251 if (*fbeg == '/') {
3252 strncpy (fname, fbeg, flen);
3253 fname[flen] = 0;
3254 if (redundant_include_p (pfile, fname))
3255 return 0;
3256 if (importing)
3257 f = lookup_import (pfile, fname, NULL_PTR);
3258 else
3259 f = open_include_file (pfile, fname, NULL_PTR);
3260 if (f == -2)
3261 return 0; /* Already included this file */
3262 } else {
3263 /* Search directory path, trying to open the file.
3264 Copy each filename tried into FNAME. */
3265
3266 for (searchptr = search_start; searchptr; searchptr = searchptr->next) {
3267 if (searchptr->fname) {
3268 /* The empty string in a search path is ignored.
3269 This makes it possible to turn off entirely
3270 a standard piece of the list. */
3271 if (searchptr->fname[0] == 0)
3272 continue;
3273 strcpy (fname, searchptr->fname);
3274 strcat (fname, "/");
3275 fname[strlen (fname) + flen] = 0;
3276 } else {
3277 fname[0] = 0;
3278 }
3279 strncat (fname, fbeg, flen);
3280 #ifdef VMS
3281 /* Change this 1/2 Unix 1/2 VMS file specification into a
3282 full VMS file specification */
3283 if (searchptr->fname && (searchptr->fname[0] != 0)) {
3284 /* Fix up the filename */
3285 hack_vms_include_specification (fname);
3286 } else {
3287 /* This is a normal VMS filespec, so use it unchanged. */
3288 strncpy (fname, fbeg, flen);
3289 fname[flen] = 0;
3290 /* if it's '#include filename', add the missing .h */
3291 if (index(fname,'.')==NULL) {
3292 strcat (fname, ".h");
3293 }
3294 }
3295 #endif /* VMS */
3296 /* ??? There are currently 3 separate mechanisms for avoiding processing
3297 of redundant include files: #import, #pragma once, and
3298 redundant_include_p. It would be nice if they were unified. */
3299 if (redundant_include_p (pfile, fname))
3300 return 0;
3301 if (importing)
3302 f = lookup_import (pfile, fname, searchptr);
3303 else
3304 f = open_include_file (pfile, fname, searchptr);
3305 if (f == -2)
3306 return 0; /* Already included this file */
3307 #ifdef EACCES
3308 else if (f == -1 && errno == EACCES)
3309 cpp_warning (pfile, "Header file %s exists, but is not readable",
3310 fname);
3311 #endif
3312 if (f >= 0)
3313 break;
3314 }
3315 }
3316
3317 if (f < 0)
3318 {
3319 /* A file that was not found. */
3320 strncpy (fname, fbeg, flen);
3321 fname[flen] = 0;
3322 /* If generating dependencies and -MG was specified, we assume missing
3323 files are leaf files, living in the same directory as the source file
3324 or other similar place; these missing files may be generated from
3325 other files and may not exist yet (eg: y.tab.h). */
3326
3327 if (CPP_OPTIONS(pfile)->print_deps_missing_files
3328 && CPP_PRINT_DEPS (pfile)
3329 > (angle_brackets || (pfile->system_include_depth > 0)))
3330 {
3331 /* If it was requested as a system header file,
3332 then assume it belongs in the first place to look for such. */
3333 if (angle_brackets)
3334 {
3335 for (searchptr = search_start; searchptr;
3336 searchptr = searchptr->next)
3337 {
3338 if (searchptr->fname)
3339 {
3340 char *p;
3341
3342 if (searchptr->fname[0] == 0)
3343 continue;
3344 p = (char *) alloca (strlen (searchptr->fname)
3345 + strlen (fname) + 2);
3346 strcpy (p, searchptr->fname);
3347 strcat (p, "/");
3348 strcat (p, fname);
3349 deps_output (pfile, p, ' ');
3350 break;
3351 }
3352 }
3353 }
3354 else
3355 {
3356 /* Otherwise, omit the directory, as if the file existed
3357 in the directory with the source. */
3358 deps_output (pfile, fname, ' ');
3359 }
3360 }
3361 /* If -M was specified, and this header file won't be added to the
3362 dependency list, then don't count this as an error, because we can
3363 still produce correct output. Otherwise, we can't produce correct
3364 output, because there may be dependencies we need inside the missing
3365 file, and we don't know what directory this missing file exists in.*/
3366 else if (CPP_PRINT_DEPS (pfile)
3367 && (CPP_PRINT_DEPS (pfile)
3368 <= (angle_brackets || (pfile->system_include_depth > 0))))
3369 cpp_warning (pfile, "No include path in which to find %s", fname);
3370 else if (search_start)
3371 cpp_error_from_errno (pfile, fname);
3372 else
3373 cpp_error (pfile, "No include path in which to find %s", fname);
3374 }
3375 else {
3376 /* Check to see if this include file is a once-only include file.
3377 If so, give up. */
3378
3379 struct file_name_list *ptr;
3380
3381 for (ptr = pfile->dont_repeat_files; ptr; ptr = ptr->next) {
3382 if (!strcmp (ptr->fname, fname)) {
3383 close (f);
3384 return 0; /* This file was once'd. */
3385 }
3386 }
3387
3388 for (ptr = pfile->all_include_files; ptr; ptr = ptr->next) {
3389 if (!strcmp (ptr->fname, fname))
3390 break; /* This file was included before. */
3391 }
3392
3393 if (ptr == 0) {
3394 /* This is the first time for this file. */
3395 /* Add it to list of files included. */
3396
3397 ptr = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
3398 ptr->control_macro = 0;
3399 ptr->c_system_include_path = 0;
3400 ptr->next = pfile->all_include_files;
3401 pfile->all_include_files = ptr;
3402 ptr->fname = savestring (fname);
3403 ptr->got_name_map = 0;
3404
3405 /* For -M, add this file to the dependencies. */
3406 if (CPP_PRINT_DEPS (pfile)
3407 > (angle_brackets || (pfile->system_include_depth > 0)))
3408 deps_output (pfile, fname, ' ');
3409 }
3410
3411 /* Handle -H option. */
3412 if (CPP_OPTIONS(pfile)->print_include_names)
3413 {
3414 cpp_buffer *buf = CPP_BUFFER (pfile);
3415 while ((buf = CPP_PREV_BUFFER (buf)) != CPP_NULL_BUFFER (pfile))
3416 putc ('.', stderr);
3417 fprintf (stderr, "%s\n", fname);
3418 }
3419
3420 if (angle_brackets)
3421 pfile->system_include_depth++;
3422
3423 /* Actually process the file. */
3424
3425 /* Record file on "seen" list for #import. */
3426 add_import (pfile, f, fname);
3427
3428 pcftry = (char *) alloca (strlen (fname) + 30);
3429 pcfbuf = 0;
3430 pcfnum = 0;
3431
3432 #if 0
3433 if (!no_precomp)
3434 {
3435 struct stat stat_f;
3436
3437 fstat (f, &stat_f);
3438
3439 do {
3440 sprintf (pcftry, "%s%d", fname, pcfnum++);
3441
3442 pcf = open (pcftry, O_RDONLY, 0666);
3443 if (pcf != -1)
3444 {
3445 struct stat s;
3446
3447 fstat (pcf, &s);
3448 if (bcmp ((char *) &stat_f.st_ino, (char *) &s.st_ino,
3449 sizeof (s.st_ino))
3450 || stat_f.st_dev != s.st_dev)
3451 {
3452 pcfbuf = check_precompiled (pcf, fname, &pcfbuflimit);
3453 /* Don't need it any more. */
3454 close (pcf);
3455 }
3456 else
3457 {
3458 /* Don't need it at all. */
3459 close (pcf);
3460 break;
3461 }
3462 }
3463 } while (pcf != -1 && !pcfbuf);
3464 }
3465 #endif
3466
3467 /* Actually process the file */
3468 if (cpp_push_buffer (pfile, NULL, 0) == NULL)
3469 return 0;
3470 if (finclude (pfile, f, fname, is_system_include (pfile, fname),
3471 searchptr != dsp ? searchptr : SELF_DIR_DUMMY))
3472 {
3473 output_line_command (pfile, 0, enter_file);
3474 pfile->only_seen_white = 2;
3475 }
3476
3477 if (angle_brackets)
3478 pfile->system_include_depth--;
3479 }
3480 return 0;
3481 }
3482
3483 /* Return nonzero if there is no need to include file NAME
3484 because it has already been included and it contains a conditional
3485 to make a repeated include do nothing. */
3486
3487 static int
3488 redundant_include_p (pfile, name)
3489 cpp_reader *pfile;
3490 char *name;
3491 {
3492 struct file_name_list *l = pfile->all_include_files;
3493 for (; l; l = l->next)
3494 if (! strcmp (name, l->fname)
3495 && l->control_macro
3496 && cpp_lookup (pfile, l->control_macro, -1, -1))
3497 return 1;
3498 return 0;
3499 }
3500
3501 /* Return nonzero if the given FILENAME is an absolute pathname which
3502 designates a file within one of the known "system" include file
3503 directories. We assume here that if the given FILENAME looks like
3504 it is the name of a file which resides either directly in a "system"
3505 include file directory, or within any subdirectory thereof, then the
3506 given file must be a "system" include file. This function tells us
3507 if we should suppress pedantic errors/warnings for the given FILENAME.
3508
3509 The value is 2 if the file is a C-language system header file
3510 for which C++ should (on most systems) assume `extern "C"'. */
3511
3512 static int
3513 is_system_include (pfile, filename)
3514 cpp_reader *pfile;
3515 register char *filename;
3516 {
3517 struct file_name_list *searchptr;
3518
3519 for (searchptr = CPP_OPTIONS (pfile)->first_system_include; searchptr;
3520 searchptr = searchptr->next)
3521 if (searchptr->fname) {
3522 register char *sys_dir = searchptr->fname;
3523 register unsigned length = strlen (sys_dir);
3524
3525 if (! strncmp (sys_dir, filename, length) && filename[length] == '/')
3526 {
3527 if (searchptr->c_system_include_path)
3528 return 2;
3529 else
3530 return 1;
3531 }
3532 }
3533 return 0;
3534 }
3535
3536 \f
3537 /*
3538 * Install a name in the assertion hash table.
3539 *
3540 * If LEN is >= 0, it is the length of the name.
3541 * Otherwise, compute the length by scanning the entire name.
3542 *
3543 * If HASH is >= 0, it is the precomputed hash code.
3544 * Otherwise, compute the hash code.
3545 */
3546
3547 static ASSERTION_HASHNODE *
3548 assertion_install (pfile, name, len, hash)
3549 cpp_reader *pfile;
3550 U_CHAR *name;
3551 int len;
3552 int hash;
3553 {
3554 register ASSERTION_HASHNODE *hp;
3555 register int i, bucket;
3556 register U_CHAR *p, *q;
3557
3558 i = sizeof (ASSERTION_HASHNODE) + len + 1;
3559 hp = (ASSERTION_HASHNODE *) xmalloc (i);
3560 bucket = hash;
3561 hp->bucket_hdr = &pfile->assertion_hashtab[bucket];
3562 hp->next = pfile->assertion_hashtab[bucket];
3563 pfile->assertion_hashtab[bucket] = hp;
3564 hp->prev = NULL;
3565 if (hp->next != NULL)
3566 hp->next->prev = hp;
3567 hp->length = len;
3568 hp->value = 0;
3569 hp->name = ((U_CHAR *) hp) + sizeof (ASSERTION_HASHNODE);
3570 p = hp->name;
3571 q = name;
3572 for (i = 0; i < len; i++)
3573 *p++ = *q++;
3574 hp->name[len] = 0;
3575 return hp;
3576 }
3577 /*
3578 * find the most recent hash node for name "name" (ending with first
3579 * non-identifier char) installed by install
3580 *
3581 * If LEN is >= 0, it is the length of the name.
3582 * Otherwise, compute the length by scanning the entire name.
3583 *
3584 * If HASH is >= 0, it is the precomputed hash code.
3585 * Otherwise, compute the hash code.
3586 */
3587
3588 static ASSERTION_HASHNODE *
3589 assertion_lookup (pfile, name, len, hash)
3590 cpp_reader *pfile;
3591 U_CHAR *name;
3592 int len;
3593 int hash;
3594 {
3595 register ASSERTION_HASHNODE *bucket;
3596
3597 bucket = pfile->assertion_hashtab[hash];
3598 while (bucket) {
3599 if (bucket->length == len && strncmp (bucket->name, name, len) == 0)
3600 return bucket;
3601 bucket = bucket->next;
3602 }
3603 return NULL;
3604 }
3605
3606 static void
3607 delete_assertion (hp)
3608 ASSERTION_HASHNODE *hp;
3609 {
3610 struct tokenlist_list *tail;
3611 if (hp->prev != NULL)
3612 hp->prev->next = hp->next;
3613 if (hp->next != NULL)
3614 hp->next->prev = hp->prev;
3615
3616 for (tail = hp->value; tail; )
3617 {
3618 struct tokenlist_list *next = tail->next;
3619 free_token_list (tail->tokens);
3620 free (tail);
3621 tail = next;
3622 }
3623
3624 /* Make sure that the bucket chain header that
3625 the deleted guy was on points to the right thing afterwards. */
3626 if (hp == *hp->bucket_hdr)
3627 *hp->bucket_hdr = hp->next;
3628
3629 free (hp);
3630 }
3631 \f
3632 /* Convert a character string literal into a nul-terminated string.
3633 The input string is [IN ... LIMIT).
3634 The result is placed in RESULT. RESULT can be the same as IN.
3635 The value returned in the end of the string written to RESULT,
3636 or NULL on error. */
3637
3638 static U_CHAR *
3639 convert_string (pfile, result, in, limit, handle_escapes)
3640 cpp_reader *pfile;
3641 register U_CHAR *result, *in, *limit;
3642 int handle_escapes;
3643 {
3644 U_CHAR c;
3645 c = *in++;
3646 if (c != '\"')
3647 return NULL;
3648 while (in < limit)
3649 {
3650 U_CHAR c = *in++;
3651 switch (c)
3652 {
3653 case '\0':
3654 return NULL;
3655 case '\"':
3656 limit = in;
3657 break;
3658 case '\\':
3659 if (handle_escapes)
3660 {
3661 char *bpc = (char *) in;
3662 int i = (U_CHAR) cpp_parse_escape (pfile, &bpc);
3663 in = (U_CHAR *) bpc;
3664 if (i >= 0)
3665 *result++ = (U_CHAR)c;
3666 break;
3667 }
3668 /* else fall through */
3669 default:
3670 *result++ = c;
3671 }
3672 }
3673 *result = 0;
3674 return result;
3675 }
3676
3677 /*
3678 * interpret #line command. Remembers previously seen fnames
3679 * in its very own hash table.
3680 */
3681 #define FNAME_HASHSIZE 37
3682
3683 static int
3684 do_line (pfile, keyword, unused1, unused2)
3685 cpp_reader *pfile;
3686 struct directive *keyword ATTRIBUTE_UNUSED;
3687 U_CHAR *unused1 ATTRIBUTE_UNUSED, *unused2 ATTRIBUTE_UNUSED;
3688 {
3689 cpp_buffer *ip = CPP_BUFFER (pfile);
3690 int new_lineno;
3691 long old_written = CPP_WRITTEN (pfile);
3692 enum file_change_code file_change = same_file;
3693 enum cpp_token token;
3694
3695 token = get_directive_token (pfile);
3696
3697 if (token != CPP_NUMBER
3698 || !ISDIGIT(pfile->token_buffer[old_written]))
3699 {
3700 cpp_error (pfile, "invalid format `#line' command");
3701 goto bad_line_directive;
3702 }
3703
3704 /* The Newline at the end of this line remains to be processed.
3705 To put the next line at the specified line number,
3706 we must store a line number now that is one less. */
3707 new_lineno = atoi ((char *)(pfile->token_buffer + old_written)) - 1;
3708 CPP_SET_WRITTEN (pfile, old_written);
3709
3710 /* NEW_LINENO is one less than the actual line number here. */
3711 if (CPP_PEDANTIC (pfile) && new_lineno < 0)
3712 cpp_pedwarn (pfile, "line number out of range in `#line' command");
3713
3714 #if 0 /* #line 10"foo.c" is supposed to be allowed. */
3715 if (PEEKC() && !is_space[PEEKC()]) {
3716 cpp_error (pfile, "invalid format `#line' command");
3717 goto bad_line_directive;
3718 }
3719 #endif
3720
3721 token = get_directive_token (pfile);
3722
3723 if (token == CPP_STRING) {
3724 U_CHAR *fname = pfile->token_buffer + old_written;
3725 U_CHAR *end_name;
3726 static HASHNODE *fname_table[FNAME_HASHSIZE];
3727 HASHNODE *hp, **hash_bucket;
3728 U_CHAR *p;
3729 long num_start;
3730 int fname_length;
3731
3732 /* Turn the file name, which is a character string literal,
3733 into a null-terminated string. Do this in place. */
3734 end_name = convert_string (pfile, fname, fname, CPP_PWRITTEN (pfile), 1);
3735 if (end_name == NULL)
3736 {
3737 cpp_error (pfile, "invalid format `#line' command");
3738 goto bad_line_directive;
3739 }
3740
3741 fname_length = end_name - fname;
3742
3743 num_start = CPP_WRITTEN (pfile);
3744 token = get_directive_token (pfile);
3745 if (token != CPP_VSPACE && token != CPP_EOF && token != CPP_POP) {
3746 p = pfile->token_buffer + num_start;
3747 if (CPP_PEDANTIC (pfile))
3748 cpp_pedwarn (pfile, "garbage at end of `#line' command");
3749
3750 if (token != CPP_NUMBER || *p < '0' || *p > '4' || p[1] != '\0')
3751 {
3752 cpp_error (pfile, "invalid format `#line' command");
3753 goto bad_line_directive;
3754 }
3755 if (*p == '1')
3756 file_change = enter_file;
3757 else if (*p == '2')
3758 file_change = leave_file;
3759 else if (*p == '3')
3760 ip->system_header_p = 1;
3761 else /* if (*p == '4') */
3762 ip->system_header_p = 2;
3763
3764 CPP_SET_WRITTEN (pfile, num_start);
3765 token = get_directive_token (pfile);
3766 p = pfile->token_buffer + num_start;
3767 if (token == CPP_NUMBER && p[1] == '\0' && (*p == '3' || *p== '4')) {
3768 ip->system_header_p = *p == '3' ? 1 : 2;
3769 token = get_directive_token (pfile);
3770 }
3771 if (token != CPP_VSPACE) {
3772 cpp_error (pfile, "invalid format `#line' command");
3773 goto bad_line_directive;
3774 }
3775 }
3776
3777 hash_bucket = &fname_table[hashf (fname, fname_length, FNAME_HASHSIZE)];
3778 for (hp = *hash_bucket; hp != NULL; hp = hp->next)
3779 if (hp->length == fname_length
3780 && strncmp (hp->value.cpval, fname, fname_length) == 0) {
3781 ip->nominal_fname = hp->value.cpval;
3782 break;
3783 }
3784 if (hp == 0) {
3785 /* Didn't find it; cons up a new one. */
3786 hp = (HASHNODE *) xcalloc (1, sizeof (HASHNODE) + fname_length + 1);
3787 hp->next = *hash_bucket;
3788 *hash_bucket = hp;
3789
3790 hp->length = fname_length;
3791 ip->nominal_fname = hp->value.cpval = ((char *) hp) + sizeof (HASHNODE);
3792 bcopy (fname, hp->value.cpval, fname_length);
3793 }
3794 }
3795 else if (token != CPP_VSPACE && token != CPP_EOF) {
3796 cpp_error (pfile, "invalid format `#line' command");
3797 goto bad_line_directive;
3798 }
3799
3800 ip->lineno = new_lineno;
3801 bad_line_directive:
3802 skip_rest_of_line (pfile);
3803 CPP_SET_WRITTEN (pfile, old_written);
3804 output_line_command (pfile, 0, file_change);
3805 return 0;
3806 }
3807
3808 /*
3809 * remove the definition of a symbol from the symbol table.
3810 * according to un*x /lib/cpp, it is not an error to undef
3811 * something that has no definitions, so it isn't one here either.
3812 */
3813
3814 static int
3815 do_undef (pfile, keyword, buf, limit)
3816 cpp_reader *pfile;
3817 struct directive *keyword;
3818 U_CHAR *buf, *limit;
3819 {
3820 int sym_length;
3821 HASHNODE *hp;
3822 U_CHAR *orig_buf = buf;
3823
3824 #if 0
3825 /* If this is a precompiler run (with -pcp) pass thru #undef commands. */
3826 if (pcp_outfile && keyword)
3827 pass_thru_directive (buf, limit, pfile, keyword);
3828 #endif
3829
3830 SKIP_WHITE_SPACE (buf);
3831 sym_length = check_macro_name (pfile, buf, "macro");
3832
3833 while ((hp = cpp_lookup (pfile, buf, sym_length, -1)) != NULL)
3834 {
3835 /* If we are generating additional info for debugging (with -g) we
3836 need to pass through all effective #undef commands. */
3837 if (CPP_OPTIONS (pfile)->debug_output && keyword)
3838 pass_thru_directive (orig_buf, limit, pfile, keyword);
3839 if (hp->type != T_MACRO)
3840 cpp_warning (pfile, "undefining `%s'", hp->name);
3841 delete_macro (hp);
3842 }
3843
3844 if (CPP_PEDANTIC (pfile)) {
3845 buf += sym_length;
3846 SKIP_WHITE_SPACE (buf);
3847 if (buf != limit)
3848 cpp_pedwarn (pfile, "garbage after `#undef' directive");
3849 }
3850 return 0;
3851 }
3852 \f
3853 /*
3854 * Report an error detected by the program we are processing.
3855 * Use the text of the line in the error message.
3856 * (We use error because it prints the filename & line#.)
3857 */
3858
3859 static int
3860 do_error (pfile, keyword, buf, limit)
3861 cpp_reader *pfile;
3862 struct directive *keyword ATTRIBUTE_UNUSED;
3863 U_CHAR *buf, *limit;
3864 {
3865 int length = limit - buf;
3866 U_CHAR *copy = (U_CHAR *) alloca (length + 1);
3867 bcopy (buf, copy, length);
3868 copy[length] = 0;
3869 SKIP_WHITE_SPACE (copy);
3870 cpp_error (pfile, "#error %s", copy);
3871 return 0;
3872 }
3873
3874 /*
3875 * Report a warning detected by the program we are processing.
3876 * Use the text of the line in the warning message, then continue.
3877 * (We use error because it prints the filename & line#.)
3878 */
3879
3880 static int
3881 do_warning (pfile, keyword, buf, limit)
3882 cpp_reader *pfile;
3883 struct directive *keyword ATTRIBUTE_UNUSED;
3884 U_CHAR *buf, *limit;
3885 {
3886 int length = limit - buf;
3887 U_CHAR *copy = (U_CHAR *) alloca (length + 1);
3888 bcopy (buf, copy, length);
3889 copy[length] = 0;
3890 SKIP_WHITE_SPACE (copy);
3891
3892 if (CPP_PEDANTIC (pfile) && !CPP_BUFFER (pfile)->system_header_p)
3893 cpp_pedwarn (pfile, "ANSI C does not allow `#warning'");
3894
3895 /* Use `pedwarn' not `warning', because #warning isn't in the C Standard;
3896 if -pedantic-errors is given, #warning should cause an error. */
3897 cpp_pedwarn (pfile, "#warning %s", copy);
3898 return 0;
3899 }
3900
3901 /* Remember the name of the current file being read from so that we can
3902 avoid ever including it again. */
3903
3904 static int
3905 do_once (pfile, keyword, unused1, unused2)
3906 cpp_reader *pfile;
3907 struct directive *keyword ATTRIBUTE_UNUSED;
3908 U_CHAR *unused1 ATTRIBUTE_UNUSED, *unused2 ATTRIBUTE_UNUSED;
3909 {
3910 cpp_buffer *ip = NULL;
3911 struct file_name_list *new;
3912
3913 for (ip = CPP_BUFFER (pfile); ; ip = CPP_PREV_BUFFER (ip))
3914 {
3915 if (ip == CPP_NULL_BUFFER (pfile))
3916 return 0;
3917 if (ip->fname != NULL)
3918 break;
3919 }
3920
3921
3922 new = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
3923 new->next = pfile->dont_repeat_files;
3924 pfile->dont_repeat_files = new;
3925 new->fname = savestring (ip->fname);
3926 new->control_macro = 0;
3927 new->got_name_map = 0;
3928 new->c_system_include_path = 0;
3929
3930 return 0;
3931 }
3932
3933 /* Report program identification. */
3934
3935 static int
3936 do_ident (pfile, keyword, buf, limit)
3937 cpp_reader *pfile;
3938 struct directive *keyword ATTRIBUTE_UNUSED;
3939 U_CHAR *buf ATTRIBUTE_UNUSED, *limit ATTRIBUTE_UNUSED;
3940 {
3941 /* long old_written = CPP_WRITTEN (pfile);*/
3942
3943 /* Allow #ident in system headers, since that's not user's fault. */
3944 if (CPP_PEDANTIC (pfile) && !CPP_BUFFER (pfile)->system_header_p)
3945 cpp_pedwarn (pfile, "ANSI C does not allow `#ident'");
3946
3947 /* Leave rest of line to be read by later calls to cpp_get_token. */
3948
3949 return 0;
3950 }
3951
3952 /* #pragma and its argument line have already been copied to the output file.
3953 Just check for some recognized pragmas that need validation here. */
3954
3955 static int
3956 do_pragma (pfile, keyword, buf, limit)
3957 cpp_reader *pfile;
3958 struct directive *keyword ATTRIBUTE_UNUSED;
3959 U_CHAR *buf, *limit ATTRIBUTE_UNUSED;
3960 {
3961 while (*buf == ' ' || *buf == '\t')
3962 buf++;
3963 if (!strncmp (buf, "once", 4)) {
3964 /* Allow #pragma once in system headers, since that's not the user's
3965 fault. */
3966 if (!CPP_BUFFER (pfile)->system_header_p)
3967 cpp_warning (pfile, "`#pragma once' is obsolete");
3968 do_once (pfile, NULL, NULL, NULL);
3969 }
3970
3971 if (!strncmp (buf, "implementation", 14)) {
3972 /* Be quiet about `#pragma implementation' for a file only if it hasn't
3973 been included yet. */
3974 struct file_name_list *ptr;
3975 U_CHAR *p = buf + 14, *fname, *inc_fname;
3976 int fname_len;
3977 SKIP_WHITE_SPACE (p);
3978 if (*p == '\n' || *p != '\"')
3979 return 0;
3980
3981 fname = p + 1;
3982 p = (U_CHAR *) index (fname, '\"');
3983 fname_len = p != NULL ? p - fname : strlen (fname);
3984
3985 for (ptr = pfile->all_include_files; ptr; ptr = ptr->next) {
3986 inc_fname = (U_CHAR *) rindex (ptr->fname, '/');
3987 inc_fname = inc_fname ? inc_fname + 1 : (U_CHAR *) ptr->fname;
3988 if (inc_fname && !strncmp (inc_fname, fname, fname_len))
3989 cpp_warning (pfile,
3990 "`#pragma implementation' for `%s' appears after file is included",
3991 fname);
3992 }
3993 }
3994
3995 return 0;
3996 }
3997
3998 #if 0
3999 /* This was a fun hack, but #pragma seems to start to be useful.
4000 By failing to recognize it, we pass it through unchanged to cc1. */
4001
4002 /*
4003 * the behavior of the #pragma directive is implementation defined.
4004 * this implementation defines it as follows.
4005 */
4006
4007 static int
4008 do_pragma ()
4009 {
4010 close (0);
4011 if (open ("/dev/tty", O_RDONLY, 0666) != 0)
4012 goto nope;
4013 close (1);
4014 if (open ("/dev/tty", O_WRONLY, 0666) != 1)
4015 goto nope;
4016 execl ("/usr/games/hack", "#pragma", 0);
4017 execl ("/usr/games/rogue", "#pragma", 0);
4018 execl ("/usr/new/emacs", "-f", "hanoi", "9", "-kill", 0);
4019 execl ("/usr/local/emacs", "-f", "hanoi", "9", "-kill", 0);
4020 nope:
4021 fatal ("You are in a maze of twisty compiler features, all different");
4022 }
4023 #endif
4024
4025 #ifdef SCCS_DIRECTIVE
4026 /* Just ignore #sccs, on systems where we define it at all. */
4027
4028 static int
4029 do_sccs (pfile, keyword, buf, limit)
4030 cpp_reader *pfile;
4031 struct directive *keyword ATTRIBUTE_UNUSED;
4032 U_CHAR *buf ATTRIBUTE_UNUSED, *limit ATTRIBUTE_UNUSED;
4033 {
4034 if (CPP_PEDANTIC (pfile))
4035 cpp_pedwarn (pfile, "ANSI C does not allow `#sccs'");
4036 return 0;
4037 }
4038 #endif
4039 \f
4040 /*
4041 * handle #if command by
4042 * 1) inserting special `defined' keyword into the hash table
4043 * that gets turned into 0 or 1 by special_symbol (thus,
4044 * if the luser has a symbol called `defined' already, it won't
4045 * work inside the #if command)
4046 * 2) rescan the input into a temporary output buffer
4047 * 3) pass the output buffer to the yacc parser and collect a value
4048 * 4) clean up the mess left from steps 1 and 2.
4049 * 5) call conditional_skip to skip til the next #endif (etc.),
4050 * or not, depending on the value from step 3.
4051 */
4052
4053 static int
4054 do_if (pfile, keyword, buf, limit)
4055 cpp_reader *pfile;
4056 struct directive *keyword ATTRIBUTE_UNUSED;
4057 U_CHAR *buf, *limit;
4058 {
4059 HOST_WIDE_INT value = eval_if_expression (pfile, buf, limit - buf);
4060 conditional_skip (pfile, value == 0, T_IF, NULL_PTR);
4061 return 0;
4062 }
4063
4064 /*
4065 * handle a #elif directive by not changing if_stack either.
4066 * see the comment above do_else.
4067 */
4068
4069 static int
4070 do_elif (pfile, keyword, buf, limit)
4071 cpp_reader *pfile;
4072 struct directive *keyword ATTRIBUTE_UNUSED;
4073 U_CHAR *buf, *limit;
4074 {
4075 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack) {
4076 cpp_error (pfile, "`#elif' not within a conditional");
4077 return 0;
4078 } else {
4079 if (pfile->if_stack->type != T_IF && pfile->if_stack->type != T_ELIF) {
4080 cpp_error (pfile, "`#elif' after `#else'");
4081 #if 0
4082 fprintf (stderr, " (matches line %d", pfile->if_stack->lineno);
4083 #endif
4084 if (pfile->if_stack->fname != NULL && CPP_BUFFER (pfile)->fname != NULL
4085 && strcmp (pfile->if_stack->fname,
4086 CPP_BUFFER (pfile)->nominal_fname) != 0)
4087 fprintf (stderr, ", file %s", pfile->if_stack->fname);
4088 fprintf (stderr, ")\n");
4089 }
4090 pfile->if_stack->type = T_ELIF;
4091 }
4092
4093 if (pfile->if_stack->if_succeeded)
4094 skip_if_group (pfile, 0);
4095 else {
4096 HOST_WIDE_INT value = eval_if_expression (pfile, buf, limit - buf);
4097 if (value == 0)
4098 skip_if_group (pfile, 0);
4099 else {
4100 ++pfile->if_stack->if_succeeded; /* continue processing input */
4101 output_line_command (pfile, 1, same_file);
4102 }
4103 }
4104 return 0;
4105 }
4106
4107 /*
4108 * evaluate a #if expression in BUF, of length LENGTH,
4109 * then parse the result as a C expression and return the value as an int.
4110 */
4111
4112 static HOST_WIDE_INT
4113 eval_if_expression (pfile, buf, length)
4114 cpp_reader *pfile;
4115 U_CHAR *buf ATTRIBUTE_UNUSED;
4116 int length ATTRIBUTE_UNUSED;
4117 {
4118 HASHNODE *save_defined;
4119 HOST_WIDE_INT value;
4120 long old_written = CPP_WRITTEN (pfile);
4121
4122 save_defined = install ((U_CHAR *)"defined", -1, T_SPEC_DEFINED, 0, 0, -1);
4123 pfile->pcp_inside_if = 1;
4124
4125 value = cpp_parse_expr (pfile);
4126 pfile->pcp_inside_if = 0;
4127 delete_macro (save_defined); /* clean up special symbol */
4128
4129 CPP_SET_WRITTEN (pfile, old_written); /* Pop */
4130
4131 return value;
4132 }
4133
4134 /*
4135 * routine to handle ifdef/ifndef. Try to look up the symbol,
4136 * then do or don't skip to the #endif/#else/#elif depending
4137 * on what directive is actually being processed.
4138 */
4139
4140 static int
4141 do_xifdef (pfile, keyword, unused1, unused2)
4142 cpp_reader *pfile;
4143 struct directive *keyword;
4144 U_CHAR *unused1 ATTRIBUTE_UNUSED, *unused2 ATTRIBUTE_UNUSED;
4145 {
4146 int skip;
4147 cpp_buffer *ip = CPP_BUFFER (pfile);
4148 U_CHAR *ident;
4149 int ident_length;
4150 enum cpp_token token;
4151 int start_of_file = 0;
4152 U_CHAR *control_macro = 0;
4153 int old_written = CPP_WRITTEN (pfile);
4154
4155 /* Detect a #ifndef at start of file (not counting comments). */
4156 if (ip->fname != 0 && keyword->type == T_IFNDEF)
4157 start_of_file = pfile->only_seen_white == 2;
4158
4159 pfile->no_macro_expand++;
4160 token = get_directive_token (pfile);
4161 pfile->no_macro_expand--;
4162
4163 ident = pfile->token_buffer + old_written;
4164 ident_length = CPP_WRITTEN (pfile) - old_written;
4165 CPP_SET_WRITTEN (pfile, old_written); /* Pop */
4166
4167 if (token == CPP_VSPACE || token == CPP_POP || token == CPP_EOF)
4168 {
4169 skip = (keyword->type == T_IFDEF);
4170 if (! CPP_TRADITIONAL (pfile))
4171 cpp_pedwarn (pfile, "`#%s' with no argument", keyword->name);
4172 }
4173 else if (token == CPP_NAME)
4174 {
4175 HASHNODE *hp = cpp_lookup (pfile, ident, ident_length, -1);
4176 skip = (hp == NULL) ^ (keyword->type == T_IFNDEF);
4177 if (start_of_file && !skip)
4178 {
4179 control_macro = (U_CHAR *) xmalloc (ident_length + 1);
4180 bcopy (ident, control_macro, ident_length + 1);
4181 }
4182 }
4183 else
4184 {
4185 skip = (keyword->type == T_IFDEF);
4186 if (! CPP_TRADITIONAL (pfile))
4187 cpp_error (pfile, "`#%s' with invalid argument", keyword->name);
4188 }
4189
4190 if (!CPP_TRADITIONAL (pfile))
4191 { int c;
4192 cpp_skip_hspace (pfile);
4193 c = PEEKC ();
4194 if (c != EOF && c != '\n')
4195 cpp_pedwarn (pfile, "garbage at end of `#%s' argument", keyword->name);
4196 }
4197 skip_rest_of_line (pfile);
4198
4199 #if 0
4200 if (pcp_outfile) {
4201 /* Output a precondition for this macro. */
4202 if (hp && hp->value.defn->predefined)
4203 fprintf (pcp_outfile, "#define %s\n", hp->name);
4204 else {
4205 U_CHAR *cp = buf;
4206 fprintf (pcp_outfile, "#undef ");
4207 while (is_idchar[*cp]) /* Ick! */
4208 fputc (*cp++, pcp_outfile);
4209 putc ('\n', pcp_outfile);
4210 }
4211 #endif
4212
4213 conditional_skip (pfile, skip, T_IF, control_macro);
4214 return 0;
4215 }
4216
4217 /* Push TYPE on stack; then, if SKIP is nonzero, skip ahead.
4218 If this is a #ifndef starting at the beginning of a file,
4219 CONTROL_MACRO is the macro name tested by the #ifndef.
4220 Otherwise, CONTROL_MACRO is 0. */
4221
4222 static void
4223 conditional_skip (pfile, skip, type, control_macro)
4224 cpp_reader *pfile;
4225 int skip;
4226 enum node_type type;
4227 U_CHAR *control_macro;
4228 {
4229 IF_STACK_FRAME *temp;
4230
4231 temp = (IF_STACK_FRAME *) xcalloc (1, sizeof (IF_STACK_FRAME));
4232 temp->fname = CPP_BUFFER (pfile)->nominal_fname;
4233 #if 0
4234 temp->lineno = CPP_BUFFER (pfile)->lineno;
4235 #endif
4236 temp->next = pfile->if_stack;
4237 temp->control_macro = control_macro;
4238 pfile->if_stack = temp;
4239
4240 pfile->if_stack->type = type;
4241
4242 if (skip != 0) {
4243 skip_if_group (pfile, 0);
4244 return;
4245 } else {
4246 ++pfile->if_stack->if_succeeded;
4247 output_line_command (pfile, 1, same_file);
4248 }
4249 }
4250
4251 /*
4252 * skip to #endif, #else, or #elif. adjust line numbers, etc.
4253 * leaves input ptr at the sharp sign found.
4254 * If ANY is nonzero, return at next directive of any sort.
4255 */
4256
4257 static void
4258 skip_if_group (pfile, any)
4259 cpp_reader *pfile;
4260 int any;
4261 {
4262 int c;
4263 struct directive *kt;
4264 IF_STACK_FRAME *save_if_stack = pfile->if_stack; /* don't pop past here */
4265 #if 0
4266 U_CHAR *beg_of_line = bp;
4267 #endif
4268 register int ident_length;
4269 U_CHAR *ident;
4270 struct parse_marker line_start_mark;
4271
4272 parse_set_mark (&line_start_mark, pfile);
4273
4274 if (CPP_OPTIONS (pfile)->output_conditionals) {
4275 static char failed[] = "#failed\n";
4276 CPP_PUTS (pfile, failed, sizeof(failed)-1);
4277 pfile->lineno++;
4278 output_line_command (pfile, 1, same_file);
4279 }
4280
4281 beg_of_line:
4282 if (CPP_OPTIONS (pfile)->output_conditionals)
4283 {
4284 cpp_buffer *pbuf = CPP_BUFFER (pfile);
4285 U_CHAR *start_line = pbuf->buf + line_start_mark.position;
4286 CPP_PUTS (pfile, start_line, pbuf->cur - start_line);
4287 }
4288 parse_move_mark (&line_start_mark, pfile);
4289 if (!CPP_TRADITIONAL (pfile))
4290 cpp_skip_hspace (pfile);
4291 c = GETC();
4292 if (c == '#')
4293 {
4294 int old_written = CPP_WRITTEN (pfile);
4295 cpp_skip_hspace (pfile);
4296
4297 parse_name (pfile, GETC());
4298 ident_length = CPP_WRITTEN (pfile) - old_written;
4299 ident = pfile->token_buffer + old_written;
4300 pfile->limit = ident;
4301 #if 0
4302 if (ident_length == 0)
4303 goto not_a_directive;
4304
4305 /* Handle # followed by a line number. */
4306
4307 /* Avoid error for `###' and similar cases unless -pedantic. */
4308 #endif
4309
4310 for (kt = directive_table; kt->length >= 0; kt++)
4311 {
4312 IF_STACK_FRAME *temp;
4313 if (ident_length == kt->length
4314 && strncmp (ident, kt->name, kt->length) == 0)
4315 {
4316 /* If we are asked to return on next directive, do so now. */
4317 if (any)
4318 goto done;
4319
4320 switch (kt->type)
4321 {
4322 case T_IF:
4323 case T_IFDEF:
4324 case T_IFNDEF:
4325 temp
4326 = (IF_STACK_FRAME *) xcalloc (1, sizeof (IF_STACK_FRAME));
4327 temp->next = pfile->if_stack;
4328 pfile->if_stack = temp;
4329 #if 0
4330 temp->lineno = CPP_BUFFER(pfile)->lineno;
4331 #endif
4332 temp->fname = CPP_BUFFER(pfile)->nominal_fname;
4333 temp->type = kt->type;
4334 break;
4335 case T_ELSE:
4336 case T_ENDIF:
4337 if (CPP_PEDANTIC (pfile) && pfile->if_stack != save_if_stack)
4338 validate_else (pfile,
4339 kt->type == T_ELSE ? "#else" : "#endif");
4340 case T_ELIF:
4341 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack)
4342 {
4343 cpp_error (pfile,
4344 "`#%s' not within a conditional", kt->name);
4345 break;
4346 }
4347 else if (pfile->if_stack == save_if_stack)
4348 goto done; /* found what we came for */
4349
4350 if (kt->type != T_ENDIF)
4351 {
4352 if (pfile->if_stack->type == T_ELSE)
4353 cpp_error (pfile, "`#else' or `#elif' after `#else'");
4354 pfile->if_stack->type = kt->type;
4355 break;
4356 }
4357
4358 temp = pfile->if_stack;
4359 pfile->if_stack = temp->next;
4360 free (temp);
4361 break;
4362 default: ;
4363 }
4364 break;
4365 }
4366 /* Don't let erroneous code go by. */
4367 if (kt->length < 0 && !CPP_OPTIONS (pfile)->lang_asm
4368 && CPP_PEDANTIC (pfile))
4369 cpp_pedwarn (pfile, "invalid preprocessor directive name");
4370 }
4371 c = GETC ();
4372 }
4373 /* We're in the middle of a line. Skip the rest of it. */
4374 for (;;) {
4375 switch (c)
4376 {
4377 long old;
4378 case EOF:
4379 goto done;
4380 case '/': /* possible comment */
4381 c = skip_comment (pfile, NULL);
4382 if (c == EOF)
4383 goto done;
4384 break;
4385 case '\"':
4386 case '\'':
4387 FORWARD(-1);
4388 old = CPP_WRITTEN (pfile);
4389 cpp_get_token (pfile);
4390 CPP_SET_WRITTEN (pfile, old);
4391 break;
4392 case '\\':
4393 /* Char after backslash loses its special meaning. */
4394 if (PEEKC() == '\n')
4395 FORWARD (1);
4396 break;
4397 case '\n':
4398 goto beg_of_line;
4399 break;
4400 }
4401 c = GETC ();
4402 }
4403 done:
4404 if (CPP_OPTIONS (pfile)->output_conditionals) {
4405 static char end_failed[] = "#endfailed\n";
4406 CPP_PUTS (pfile, end_failed, sizeof(end_failed)-1);
4407 pfile->lineno++;
4408 }
4409 pfile->only_seen_white = 1;
4410 parse_goto_mark (&line_start_mark, pfile);
4411 parse_clear_mark (&line_start_mark);
4412 }
4413
4414 /*
4415 * handle a #else directive. Do this by just continuing processing
4416 * without changing if_stack ; this is so that the error message
4417 * for missing #endif's etc. will point to the original #if. It
4418 * is possible that something different would be better.
4419 */
4420
4421 static int
4422 do_else (pfile, keyword, buf, limit)
4423 cpp_reader *pfile;
4424 struct directive *keyword ATTRIBUTE_UNUSED;
4425 U_CHAR *buf ATTRIBUTE_UNUSED, *limit ATTRIBUTE_UNUSED;
4426 {
4427 cpp_buffer *ip = CPP_BUFFER (pfile);
4428
4429 if (CPP_PEDANTIC (pfile))
4430 validate_else (pfile, "#else");
4431 skip_rest_of_line (pfile);
4432
4433 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack) {
4434 cpp_error (pfile, "`#else' not within a conditional");
4435 return 0;
4436 } else {
4437 /* #ifndef can't have its special treatment for containing the whole file
4438 if it has a #else clause. */
4439 pfile->if_stack->control_macro = 0;
4440
4441 if (pfile->if_stack->type != T_IF && pfile->if_stack->type != T_ELIF) {
4442 cpp_error (pfile, "`#else' after `#else'");
4443 fprintf (stderr, " (matches line %d", pfile->if_stack->lineno);
4444 if (strcmp (pfile->if_stack->fname, ip->nominal_fname) != 0)
4445 fprintf (stderr, ", file %s", pfile->if_stack->fname);
4446 fprintf (stderr, ")\n");
4447 }
4448 pfile->if_stack->type = T_ELSE;
4449 }
4450
4451 if (pfile->if_stack->if_succeeded)
4452 skip_if_group (pfile, 0);
4453 else {
4454 ++pfile->if_stack->if_succeeded; /* continue processing input */
4455 output_line_command (pfile, 1, same_file);
4456 }
4457 return 0;
4458 }
4459
4460 /*
4461 * unstack after #endif command
4462 */
4463
4464 static int
4465 do_endif (pfile, keyword, buf, limit)
4466 cpp_reader *pfile;
4467 struct directive *keyword ATTRIBUTE_UNUSED;
4468 U_CHAR *buf ATTRIBUTE_UNUSED, *limit ATTRIBUTE_UNUSED;
4469 {
4470 if (CPP_PEDANTIC (pfile))
4471 validate_else (pfile, "#endif");
4472 skip_rest_of_line (pfile);
4473
4474 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack)
4475 cpp_error (pfile, "unbalanced `#endif'");
4476 else
4477 {
4478 IF_STACK_FRAME *temp = pfile->if_stack;
4479 pfile->if_stack = temp->next;
4480 if (temp->control_macro != 0)
4481 {
4482 /* This #endif matched a #ifndef at the start of the file.
4483 See if it is at the end of the file. */
4484 struct parse_marker start_mark;
4485 int c;
4486
4487 parse_set_mark (&start_mark, pfile);
4488
4489 for (;;)
4490 {
4491 cpp_skip_hspace (pfile);
4492 c = GETC ();
4493 if (c != '\n')
4494 break;
4495 }
4496 parse_goto_mark (&start_mark, pfile);
4497 parse_clear_mark (&start_mark);
4498
4499 if (c == EOF)
4500 {
4501 /* If we get here, this #endif ends a #ifndef
4502 that contains all of the file (aside from whitespace).
4503 Arrange not to include the file again
4504 if the macro that was tested is defined.
4505
4506 Do not do this for the top-level file in a -include or any
4507 file in a -imacros. */
4508 #if 0
4509 FIXME!
4510 if (indepth != 0
4511 && ! (indepth == 1 && pfile->no_record_file)
4512 && ! (pfile->no_record_file && no_output))
4513 #endif
4514 {
4515 struct file_name_list *ifile = pfile->all_include_files;
4516
4517 for ( ; ifile != NULL; ifile = ifile->next)
4518 {
4519 if (!strcmp (ifile->fname, CPP_BUFFER (pfile)->fname))
4520 {
4521 ifile->control_macro = temp->control_macro;
4522 break;
4523 }
4524 }
4525 }
4526 }
4527 }
4528 free (temp);
4529 output_line_command (pfile, 1, same_file);
4530 }
4531 return 0;
4532 }
4533
4534 /* When an #else or #endif is found while skipping failed conditional,
4535 if -pedantic was specified, this is called to warn about text after
4536 the command name. P points to the first char after the command name. */
4537
4538 static void
4539 validate_else (pfile, directive)
4540 cpp_reader *pfile;
4541 char *directive;
4542 {
4543 int c;
4544 cpp_skip_hspace (pfile);
4545 c = PEEKC ();
4546 if (c != EOF && c != '\n')
4547 cpp_pedwarn (pfile,
4548 "text following `%s' violates ANSI standard", directive);
4549 }
4550
4551 /* Get the next token, and add it to the text in pfile->token_buffer.
4552 Return the kind of token we got. */
4553
4554 enum cpp_token
4555 cpp_get_token (pfile)
4556 cpp_reader *pfile;
4557 {
4558 register int c, c2, c3;
4559 long old_written;
4560 long start_line, start_column;
4561 enum cpp_token token;
4562 struct cpp_options *opts = CPP_OPTIONS (pfile);
4563 CPP_BUFFER (pfile)->prev = CPP_BUFFER (pfile)->cur;
4564 get_next:
4565 c = GETC();
4566 if (c == EOF)
4567 {
4568 handle_eof:
4569 if (CPP_BUFFER (pfile)->seen_eof)
4570 {
4571 if (cpp_pop_buffer (pfile) != CPP_NULL_BUFFER (pfile))
4572 goto get_next;
4573 else
4574 return CPP_EOF;
4575 }
4576 else
4577 {
4578 cpp_buffer *next_buf
4579 = CPP_PREV_BUFFER (CPP_BUFFER (pfile));
4580 CPP_BUFFER (pfile)->seen_eof = 1;
4581 if (CPP_BUFFER (pfile)->nominal_fname
4582 && next_buf != CPP_NULL_BUFFER (pfile))
4583 {
4584 /* We're about to return from an #include file.
4585 Emit #line information now (as part of the CPP_POP) result.
4586 But the #line refers to the file we will pop to. */
4587 cpp_buffer *cur_buffer = CPP_BUFFER (pfile);
4588 CPP_BUFFER (pfile) = next_buf;
4589 pfile->input_stack_listing_current = 0;
4590 output_line_command (pfile, 0, leave_file);
4591 CPP_BUFFER (pfile) = cur_buffer;
4592 }
4593 return CPP_POP;
4594 }
4595 }
4596 else
4597 {
4598 switch (c)
4599 {
4600 long newlines;
4601 struct parse_marker start_mark;
4602 case '/':
4603 if (PEEKC () == '=')
4604 goto op2;
4605 if (opts->put_out_comments)
4606 parse_set_mark (&start_mark, pfile);
4607 newlines = 0;
4608 cpp_buf_line_and_col (cpp_file_buffer (pfile),
4609 &start_line, &start_column);
4610 c = skip_comment (pfile, &newlines);
4611 if (opts->put_out_comments && (c == '/' || c == EOF))
4612 parse_clear_mark (&start_mark);
4613 if (c == '/')
4614 goto randomchar;
4615 if (c == EOF)
4616 {
4617 cpp_error_with_line (pfile, start_line, start_column,
4618 "unterminated comment");
4619 goto handle_eof;
4620 }
4621 c = '/'; /* Initial letter of comment. */
4622 return_comment:
4623 /* Comments are equivalent to spaces.
4624 For -traditional, a comment is equivalent to nothing. */
4625 if (opts->put_out_comments)
4626 {
4627 cpp_buffer *pbuf = CPP_BUFFER (pfile);
4628 U_CHAR *start = pbuf->buf + start_mark.position;
4629 int len = pbuf->cur - start;
4630 CPP_RESERVE(pfile, 1 + len);
4631 CPP_PUTC_Q (pfile, c);
4632 CPP_PUTS_Q (pfile, start, len);
4633 pfile->lineno += newlines;
4634 parse_clear_mark (&start_mark);
4635 return CPP_COMMENT;
4636 }
4637 else if (CPP_TRADITIONAL (pfile))
4638 {
4639 return CPP_COMMENT;
4640 }
4641 else
4642 {
4643 #if 0
4644 /* This may not work if cpp_get_token is called recursively,
4645 since many places look for horizontal space. */
4646 if (newlines)
4647 {
4648 /* Copy the newlines into the output buffer, in order to
4649 avoid the pain of a #line every time a multiline comment
4650 is seen. */
4651 CPP_RESERVE(pfile, newlines);
4652 while (--newlines >= 0)
4653 {
4654 CPP_PUTC_Q (pfile, '\n');
4655 pfile->lineno++;
4656 }
4657 return CPP_VSPACE;
4658 }
4659 #endif
4660 CPP_RESERVE(pfile, 1);
4661 CPP_PUTC_Q (pfile, ' ');
4662 return CPP_HSPACE;
4663 }
4664 #if 0
4665 if (opts->for_lint) {
4666 U_CHAR *argbp;
4667 int cmdlen, arglen;
4668 char *lintcmd = get_lintcmd (ibp, limit, &argbp, &arglen, &cmdlen);
4669
4670 if (lintcmd != NULL) {
4671 /* I believe it is always safe to emit this newline: */
4672 obp[-1] = '\n';
4673 bcopy ("#pragma lint ", (char *) obp, 13);
4674 obp += 13;
4675 bcopy (lintcmd, (char *) obp, cmdlen);
4676 obp += cmdlen;
4677
4678 if (arglen != 0) {
4679 *(obp++) = ' ';
4680 bcopy (argbp, (char *) obp, arglen);
4681 obp += arglen;
4682 }
4683
4684 /* OK, now bring us back to the state we were in before we entered
4685 this branch. We need #line because the newline for the pragma
4686 could mess things up. */
4687 output_line_command (pfile, 0, same_file);
4688 *(obp++) = ' '; /* just in case, if comments are copied thru */
4689 *(obp++) = '/';
4690 }
4691 }
4692 #endif
4693
4694 case '#':
4695 #if 0
4696 /* If this is expanding a macro definition, don't recognize
4697 preprocessor directives. */
4698 if (ip->macro != 0)
4699 goto randomchar;
4700 /* If this is expand_into_temp_buffer, recognize them
4701 only after an actual newline at this level,
4702 not at the beginning of the input level. */
4703 if (ip->fname == 0 && beg_of_line == ip->buf)
4704 goto randomchar;
4705 if (ident_length)
4706 goto specialchar;
4707 #endif
4708
4709 if (!pfile->only_seen_white)
4710 goto randomchar;
4711 if (handle_directive (pfile))
4712 return CPP_DIRECTIVE;
4713 pfile->only_seen_white = 0;
4714 return CPP_OTHER;
4715
4716 case '\"':
4717 case '\'':
4718 /* A single quoted string is treated like a double -- some
4719 programs (e.g., troff) are perverse this way */
4720 cpp_buf_line_and_col (cpp_file_buffer (pfile),
4721 &start_line, &start_column);
4722 old_written = CPP_WRITTEN (pfile);
4723 string:
4724 CPP_PUTC (pfile, c);
4725 while (1)
4726 {
4727 int cc = GETC();
4728 if (cc == EOF)
4729 {
4730 if (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
4731 {
4732 /* try harder: this string crosses a macro expansion
4733 boundary. This can happen naturally if -traditional.
4734 Otherwise, only -D can make a macro with an unmatched
4735 quote. */
4736 cpp_buffer *next_buf
4737 = CPP_PREV_BUFFER (CPP_BUFFER (pfile));
4738 (*CPP_BUFFER (pfile)->cleanup)
4739 (CPP_BUFFER (pfile), pfile);
4740 CPP_BUFFER (pfile) = next_buf;
4741 continue;
4742 }
4743 if (!CPP_TRADITIONAL (pfile))
4744 {
4745 cpp_error_with_line (pfile, start_line, start_column,
4746 "unterminated string or character constant");
4747 if (pfile->multiline_string_line != start_line
4748 && pfile->multiline_string_line != 0)
4749 cpp_error_with_line (pfile,
4750 pfile->multiline_string_line, -1,
4751 "possible real start of unterminated constant");
4752 pfile->multiline_string_line = 0;
4753 }
4754 break;
4755 }
4756 CPP_PUTC (pfile, cc);
4757 switch (cc)
4758 {
4759 case '\n':
4760 /* Traditionally, end of line ends a string constant with
4761 no error. So exit the loop and record the new line. */
4762 if (CPP_TRADITIONAL (pfile))
4763 goto while2end;
4764 if (c == '\'')
4765 {
4766 cpp_error_with_line (pfile, start_line, start_column,
4767 "unterminated character constant");
4768 goto while2end;
4769 }
4770 if (CPP_PEDANTIC (pfile)
4771 && pfile->multiline_string_line == 0)
4772 {
4773 cpp_pedwarn_with_line (pfile, start_line, start_column,
4774 "string constant runs past end of line");
4775 }
4776 if (pfile->multiline_string_line == 0)
4777 pfile->multiline_string_line = start_line;
4778 break;
4779
4780 case '\\':
4781 cc = GETC();
4782 if (cc == '\n')
4783 {
4784 /* Backslash newline is replaced by nothing at all. */
4785 CPP_ADJUST_WRITTEN (pfile, -1);
4786 pfile->lineno++;
4787 }
4788 else
4789 {
4790 /* ANSI stupidly requires that in \\ the second \
4791 is *not* prevented from combining with a newline. */
4792 NEWLINE_FIX1(cc);
4793 if (cc != EOF)
4794 CPP_PUTC (pfile, cc);
4795 }
4796 break;
4797
4798 case '\"':
4799 case '\'':
4800 if (cc == c)
4801 goto while2end;
4802 break;
4803 }
4804 }
4805 while2end:
4806 pfile->lineno += count_newlines (pfile->token_buffer + old_written,
4807 CPP_PWRITTEN (pfile));
4808 pfile->only_seen_white = 0;
4809 return c == '\'' ? CPP_CHAR : CPP_STRING;
4810
4811 case '$':
4812 if (!opts->dollars_in_ident)
4813 goto randomchar;
4814 goto letter;
4815
4816 case ':':
4817 if (opts->cplusplus && PEEKC () == ':')
4818 goto op2;
4819 goto randomchar;
4820
4821 case '&':
4822 case '+':
4823 case '|':
4824 NEWLINE_FIX;
4825 c2 = PEEKC ();
4826 if (c2 == c || c2 == '=')
4827 goto op2;
4828 goto randomchar;
4829
4830 case '*':
4831 case '!':
4832 case '%':
4833 case '=':
4834 case '^':
4835 NEWLINE_FIX;
4836 if (PEEKC () == '=')
4837 goto op2;
4838 goto randomchar;
4839
4840 case '-':
4841 NEWLINE_FIX;
4842 c2 = PEEKC ();
4843 if (c2 == '-' && opts->chill)
4844 {
4845 /* Chill style comment */
4846 if (opts->put_out_comments)
4847 parse_set_mark (&start_mark, pfile);
4848 FORWARD(1); /* Skip second '-'. */
4849 for (;;)
4850 {
4851 c = GETC ();
4852 if (c == EOF)
4853 break;
4854 if (c == '\n')
4855 {
4856 /* Don't consider final '\n' to be part of comment. */
4857 FORWARD(-1);
4858 break;
4859 }
4860 }
4861 c = '-';
4862 goto return_comment;
4863 }
4864 if (c2 == '-' || c2 == '=' || c2 == '>')
4865 goto op2;
4866 goto randomchar;
4867
4868 case '<':
4869 if (pfile->parsing_include_directive)
4870 {
4871 for (;;)
4872 {
4873 CPP_PUTC (pfile, c);
4874 if (c == '>')
4875 break;
4876 c = GETC ();
4877 NEWLINE_FIX1 (c);
4878 if (c == '\n' || c == EOF)
4879 {
4880 cpp_error (pfile,
4881 "missing '>' in `#include <FILENAME>'");
4882 break;
4883 }
4884 }
4885 return CPP_STRING;
4886 }
4887 /* else fall through */
4888 case '>':
4889 NEWLINE_FIX;
4890 c2 = PEEKC ();
4891 if (c2 == '=')
4892 goto op2;
4893 if (c2 != c)
4894 goto randomchar;
4895 FORWARD(1);
4896 CPP_RESERVE (pfile, 4);
4897 CPP_PUTC (pfile, c);
4898 CPP_PUTC (pfile, c2);
4899 NEWLINE_FIX;
4900 c3 = PEEKC ();
4901 if (c3 == '=')
4902 CPP_PUTC_Q (pfile, GETC ());
4903 CPP_NUL_TERMINATE_Q (pfile);
4904 pfile->only_seen_white = 0;
4905 return CPP_OTHER;
4906
4907 case '@':
4908 if (CPP_BUFFER (pfile)->has_escapes)
4909 {
4910 c = GETC ();
4911 if (c == '-')
4912 {
4913 if (pfile->output_escapes)
4914 CPP_PUTS (pfile, "@-", 2);
4915 parse_name (pfile, GETC ());
4916 return CPP_NAME;
4917 }
4918 else if (is_space [c])
4919 {
4920 CPP_RESERVE (pfile, 2);
4921 if (pfile->output_escapes)
4922 CPP_PUTC_Q (pfile, '@');
4923 CPP_PUTC_Q (pfile, c);
4924 return CPP_HSPACE;
4925 }
4926 }
4927 if (pfile->output_escapes)
4928 {
4929 CPP_PUTS (pfile, "@@", 2);
4930 return CPP_OTHER;
4931 }
4932 goto randomchar;
4933
4934 case '.':
4935 NEWLINE_FIX;
4936 c2 = PEEKC ();
4937 if (ISDIGIT(c2))
4938 {
4939 CPP_RESERVE(pfile, 2);
4940 CPP_PUTC_Q (pfile, '.');
4941 c = GETC ();
4942 goto number;
4943 }
4944 /* FIXME - misses the case "..\\\n." */
4945 if (c2 == '.' && PEEKN(1) == '.')
4946 {
4947 CPP_RESERVE(pfile, 4);
4948 CPP_PUTC_Q (pfile, '.');
4949 CPP_PUTC_Q (pfile, '.');
4950 CPP_PUTC_Q (pfile, '.');
4951 FORWARD (2);
4952 CPP_NUL_TERMINATE_Q (pfile);
4953 pfile->only_seen_white = 0;
4954 return CPP_3DOTS;
4955 }
4956 goto randomchar;
4957
4958 op2:
4959 token = CPP_OTHER;
4960 pfile->only_seen_white = 0;
4961 op2any:
4962 CPP_RESERVE(pfile, 3);
4963 CPP_PUTC_Q (pfile, c);
4964 CPP_PUTC_Q (pfile, GETC ());
4965 CPP_NUL_TERMINATE_Q (pfile);
4966 return token;
4967
4968 case 'L':
4969 NEWLINE_FIX;
4970 c2 = PEEKC ();
4971 if ((c2 == '\'' || c2 == '\"') && !CPP_TRADITIONAL (pfile))
4972 {
4973 CPP_PUTC (pfile, c);
4974 c = GETC ();
4975 goto string;
4976 }
4977 goto letter;
4978
4979 case '0': case '1': case '2': case '3': case '4':
4980 case '5': case '6': case '7': case '8': case '9':
4981 number:
4982 c2 = '.';
4983 for (;;)
4984 {
4985 CPP_RESERVE (pfile, 2);
4986 CPP_PUTC_Q (pfile, c);
4987 NEWLINE_FIX;
4988 c = PEEKC ();
4989 if (c == EOF)
4990 break;
4991 if (!is_idchar[c] && c != '.'
4992 && ((c2 != 'e' && c2 != 'E'
4993 && ((c2 != 'p' && c2 != 'P') || CPP_C89 (pfile)))
4994 || (c != '+' && c != '-')))
4995 break;
4996 FORWARD(1);
4997 c2= c;
4998 }
4999 CPP_NUL_TERMINATE_Q (pfile);
5000 pfile->only_seen_white = 0;
5001 return CPP_NUMBER;
5002 case 'b': case 'c': case 'd': case 'h': case 'o':
5003 case 'B': case 'C': case 'D': case 'H': case 'O':
5004 if (opts->chill && PEEKC () == '\'')
5005 {
5006 pfile->only_seen_white = 0;
5007 CPP_RESERVE (pfile, 2);
5008 CPP_PUTC_Q (pfile, c);
5009 CPP_PUTC_Q (pfile, '\'');
5010 FORWARD(1);
5011 for (;;)
5012 {
5013 c = GETC();
5014 if (c == EOF)
5015 goto chill_number_eof;
5016 if (!is_idchar[c])
5017 {
5018 if (c == '\\' && PEEKC() == '\n')
5019 {
5020 FORWARD(2);
5021 continue;
5022 }
5023 break;
5024 }
5025 CPP_PUTC (pfile, c);
5026 }
5027 if (c == '\'')
5028 {
5029 CPP_RESERVE (pfile, 2);
5030 CPP_PUTC_Q (pfile, c);
5031 CPP_NUL_TERMINATE_Q (pfile);
5032 return CPP_STRING;
5033 }
5034 else
5035 {
5036 FORWARD(-1);
5037 chill_number_eof:
5038 CPP_NUL_TERMINATE (pfile);
5039 return CPP_NUMBER;
5040 }
5041 }
5042 else
5043 goto letter;
5044 case '_':
5045 case 'a': case 'e': case 'f': case 'g': case 'i': case 'j':
5046 case 'k': case 'l': case 'm': case 'n': case 'p': case 'q':
5047 case 'r': case 's': case 't': case 'u': case 'v': case 'w':
5048 case 'x': case 'y': case 'z':
5049 case 'A': case 'E': case 'F': case 'G': case 'I': case 'J':
5050 case 'K': case 'M': case 'N': case 'P': case 'Q': case 'R':
5051 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
5052 case 'Y': case 'Z':
5053 letter:
5054 {
5055 HASHNODE *hp;
5056 unsigned char *ident;
5057 int before_name_written = CPP_WRITTEN (pfile);
5058 int ident_len;
5059 parse_name (pfile, c);
5060 pfile->only_seen_white = 0;
5061 if (pfile->no_macro_expand)
5062 return CPP_NAME;
5063 ident = pfile->token_buffer + before_name_written;
5064 ident_len = CPP_PWRITTEN (pfile) - ident;
5065 hp = cpp_lookup (pfile, ident, ident_len, -1);
5066 if (!hp)
5067 return CPP_NAME;
5068 if (hp->type == T_DISABLED)
5069 {
5070 if (pfile->output_escapes)
5071 { /* Return "@-IDENT", followed by '\0'. */
5072 int i;
5073 CPP_RESERVE (pfile, 3);
5074 ident = pfile->token_buffer + before_name_written;
5075 CPP_ADJUST_WRITTEN (pfile, 2);
5076 for (i = ident_len; i >= 0; i--) ident[i+2] = ident[i];
5077 ident[0] = '@';
5078 ident[1] = '-';
5079 }
5080 return CPP_NAME;
5081 }
5082
5083 /* If macro wants an arglist, verify that a '(' follows.
5084 first skip all whitespace, copying it to the output
5085 after the macro name. Then, if there is no '(',
5086 decide this is not a macro call and leave things that way. */
5087 if (hp->type == T_MACRO && hp->value.defn->nargs >= 0)
5088 {
5089 struct parse_marker macro_mark;
5090 int is_macro_call;
5091 while (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
5092 {
5093 cpp_buffer *next_buf;
5094 cpp_skip_hspace (pfile);
5095 if (PEEKC () != EOF)
5096 break;
5097 next_buf = CPP_PREV_BUFFER (CPP_BUFFER (pfile));
5098 (*CPP_BUFFER (pfile)->cleanup) (CPP_BUFFER (pfile), pfile);
5099 CPP_BUFFER (pfile) = next_buf;
5100 }
5101 parse_set_mark (&macro_mark, pfile);
5102 for (;;)
5103 {
5104 cpp_skip_hspace (pfile);
5105 c = PEEKC ();
5106 is_macro_call = c == '(';
5107 if (c != '\n')
5108 break;
5109 FORWARD (1);
5110 }
5111 if (!is_macro_call)
5112 parse_goto_mark (&macro_mark, pfile);
5113 parse_clear_mark (&macro_mark);
5114 if (!is_macro_call)
5115 return CPP_NAME;
5116 }
5117 /* This is now known to be a macro call. */
5118
5119 /* it might not actually be a macro. */
5120 if (hp->type != T_MACRO) {
5121 int xbuf_len; U_CHAR *xbuf;
5122 CPP_SET_WRITTEN (pfile, before_name_written);
5123 special_symbol (hp, pfile);
5124 xbuf_len = CPP_WRITTEN (pfile) - before_name_written;
5125 xbuf = (U_CHAR *) xmalloc (xbuf_len + 1);
5126 CPP_SET_WRITTEN (pfile, before_name_written);
5127 bcopy (CPP_PWRITTEN (pfile), xbuf, xbuf_len + 1);
5128 push_macro_expansion (pfile, xbuf, xbuf_len, hp);
5129 }
5130 else
5131 {
5132 /* Expand the macro, reading arguments as needed,
5133 and push the expansion on the input stack. */
5134 macroexpand (pfile, hp);
5135 CPP_SET_WRITTEN (pfile, before_name_written);
5136 }
5137
5138 /* An extra "@ " is added to the end of a macro expansion
5139 to prevent accidental token pasting. We prefer to avoid
5140 unneeded extra spaces (for the sake of cpp-using tools like
5141 imake). Here we remove the space if it is safe to do so. */
5142 if (pfile->buffer->rlimit - pfile->buffer->cur >= 3
5143 && pfile->buffer->rlimit[-2] == '@'
5144 && pfile->buffer->rlimit[-1] == ' ')
5145 {
5146 int c1 = pfile->buffer->rlimit[-3];
5147 int c2 = CPP_BUF_PEEK (CPP_PREV_BUFFER (CPP_BUFFER (pfile)));
5148 if (c2 == EOF || ! unsafe_chars (c1, c2))
5149 pfile->buffer->rlimit -= 2;
5150 }
5151 }
5152 goto get_next;
5153
5154 case ' ': case '\t': case '\v': case '\r':
5155 for (;;)
5156 {
5157 CPP_PUTC (pfile, c);
5158 c = PEEKC ();
5159 if (c == EOF || !is_hor_space[c])
5160 break;
5161 FORWARD(1);
5162 }
5163 return CPP_HSPACE;
5164
5165 case '\\':
5166 c2 = PEEKC ();
5167 if (c2 != '\n')
5168 goto randomchar;
5169 token = CPP_HSPACE;
5170 goto op2any;
5171
5172 case '\n':
5173 CPP_PUTC (pfile, c);
5174 if (pfile->only_seen_white == 0)
5175 pfile->only_seen_white = 1;
5176 pfile->lineno++;
5177 output_line_command (pfile, 1, same_file);
5178 return CPP_VSPACE;
5179
5180 case '(': token = CPP_LPAREN; goto char1;
5181 case ')': token = CPP_RPAREN; goto char1;
5182 case '{': token = CPP_LBRACE; goto char1;
5183 case '}': token = CPP_RBRACE; goto char1;
5184 case ',': token = CPP_COMMA; goto char1;
5185 case ';': token = CPP_SEMICOLON; goto char1;
5186
5187 randomchar:
5188 default:
5189 token = CPP_OTHER;
5190 char1:
5191 pfile->only_seen_white = 0;
5192 CPP_PUTC (pfile, c);
5193 return token;
5194 }
5195 }
5196 }
5197
5198 /* Like cpp_get_token, but skip spaces and comments. */
5199
5200 enum cpp_token
5201 cpp_get_non_space_token (pfile)
5202 cpp_reader *pfile;
5203 {
5204 int old_written = CPP_WRITTEN (pfile);
5205 for (;;)
5206 {
5207 enum cpp_token token = cpp_get_token (pfile);
5208 if (token != CPP_COMMENT && token != CPP_POP
5209 && token != CPP_HSPACE && token != CPP_VSPACE)
5210 return token;
5211 CPP_SET_WRITTEN (pfile, old_written);
5212 }
5213 }
5214
5215 /* Parse an identifier starting with C. */
5216
5217 static int
5218 parse_name (pfile, c)
5219 cpp_reader *pfile; int c;
5220 {
5221 for (;;)
5222 {
5223 if (! is_idchar[c])
5224 {
5225 if (c == '\\' && PEEKC() == '\n')
5226 {
5227 FORWARD(2);
5228 continue;
5229 }
5230 FORWARD (-1);
5231 break;
5232 }
5233
5234 if (c == '$' && CPP_PEDANTIC (pfile))
5235 cpp_pedwarn (pfile, "`$' in identifier");
5236
5237 CPP_RESERVE(pfile, 2); /* One more for final NUL. */
5238 CPP_PUTC_Q (pfile, c);
5239 c = GETC();
5240 if (c == EOF)
5241 break;
5242 }
5243 CPP_NUL_TERMINATE_Q (pfile);
5244 return 1;
5245 }
5246
5247 \f
5248 /* Maintain and search list of included files, for #import. */
5249
5250 /* Hash a file name for import_hash_table. */
5251
5252 static int
5253 import_hash (f)
5254 char *f;
5255 {
5256 int val = 0;
5257
5258 while (*f) val += *f++;
5259 return (val%IMPORT_HASH_SIZE);
5260 }
5261
5262 /* Search for file FILENAME in import_hash_table.
5263 Return -2 if found, either a matching name or a matching inode.
5264 Otherwise, open the file and return a file descriptor if successful
5265 or -1 if unsuccessful. */
5266
5267 static int
5268 lookup_import (pfile, filename, searchptr)
5269 cpp_reader *pfile;
5270 char *filename;
5271 struct file_name_list *searchptr;
5272 {
5273 struct import_file *i;
5274 int h;
5275 int hashval;
5276 struct stat sb;
5277 int fd;
5278
5279 hashval = import_hash (filename);
5280
5281 /* Attempt to find file in list of already included files */
5282 i = pfile->import_hash_table[hashval];
5283
5284 while (i) {
5285 if (!strcmp (filename, i->name))
5286 return -2; /* return found */
5287 i = i->next;
5288 }
5289 /* Open it and try a match on inode/dev */
5290 fd = open_include_file (pfile, filename, searchptr);
5291 if (fd < 0)
5292 return fd;
5293 fstat (fd, &sb);
5294 for (h = 0; h < IMPORT_HASH_SIZE; h++) {
5295 i = pfile->import_hash_table[h];
5296 while (i) {
5297 /* Compare the inode and the device.
5298 Supposedly on some systems the inode is not a scalar. */
5299 if (!bcmp ((char *) &i->inode, (char *) &sb.st_ino, sizeof (sb.st_ino))
5300 && i->dev == sb.st_dev) {
5301 close (fd);
5302 return -2; /* return found */
5303 }
5304 i = i->next;
5305 }
5306 }
5307 return fd; /* Not found, return open file */
5308 }
5309
5310 /* Add the file FNAME, open on descriptor FD, to import_hash_table. */
5311
5312 static void
5313 add_import (pfile, fd, fname)
5314 cpp_reader *pfile;
5315 int fd;
5316 char *fname;
5317 {
5318 struct import_file *i;
5319 int hashval;
5320 struct stat sb;
5321
5322 hashval = import_hash (fname);
5323 fstat (fd, &sb);
5324 i = (struct import_file *)xmalloc (sizeof (struct import_file));
5325 i->name = (char *)xmalloc (strlen (fname)+1);
5326 strcpy (i->name, fname);
5327 bcopy ((char *) &sb.st_ino, (char *) &i->inode, sizeof (sb.st_ino));
5328 i->dev = sb.st_dev;
5329 i->next = pfile->import_hash_table[hashval];
5330 pfile->import_hash_table[hashval] = i;
5331 }
5332 \f
5333 /* The file_name_map structure holds a mapping of file names for a
5334 particular directory. This mapping is read from the file named
5335 FILE_NAME_MAP_FILE in that directory. Such a file can be used to
5336 map filenames on a file system with severe filename restrictions,
5337 such as DOS. The format of the file name map file is just a series
5338 of lines with two tokens on each line. The first token is the name
5339 to map, and the second token is the actual name to use. */
5340
5341 struct file_name_map
5342 {
5343 struct file_name_map *map_next;
5344 char *map_from;
5345 char *map_to;
5346 };
5347
5348 #define FILE_NAME_MAP_FILE "header.gcc"
5349
5350 /* Read a space delimited string of unlimited length from a stdio
5351 file. */
5352
5353 static char *
5354 read_filename_string (ch, f)
5355 int ch;
5356 FILE *f;
5357 {
5358 char *alloc, *set;
5359 int len;
5360
5361 len = 20;
5362 set = alloc = xmalloc (len + 1);
5363 if (! is_space[ch])
5364 {
5365 *set++ = ch;
5366 while ((ch = getc (f)) != EOF && ! is_space[ch])
5367 {
5368 if (set - alloc == len)
5369 {
5370 len *= 2;
5371 alloc = xrealloc (alloc, len + 1);
5372 set = alloc + len / 2;
5373 }
5374 *set++ = ch;
5375 }
5376 }
5377 *set = '\0';
5378 ungetc (ch, f);
5379 return alloc;
5380 }
5381
5382 /* This structure holds a linked list of file name maps, one per directory. */
5383
5384 struct file_name_map_list
5385 {
5386 struct file_name_map_list *map_list_next;
5387 char *map_list_name;
5388 struct file_name_map *map_list_map;
5389 };
5390
5391 /* Read the file name map file for DIRNAME. */
5392
5393 static struct file_name_map *
5394 read_name_map (pfile, dirname)
5395 cpp_reader *pfile;
5396 char *dirname;
5397 {
5398 register struct file_name_map_list *map_list_ptr;
5399 char *name;
5400 FILE *f;
5401
5402 for (map_list_ptr = CPP_OPTIONS (pfile)->map_list; map_list_ptr;
5403 map_list_ptr = map_list_ptr->map_list_next)
5404 if (! strcmp (map_list_ptr->map_list_name, dirname))
5405 return map_list_ptr->map_list_map;
5406
5407 map_list_ptr = ((struct file_name_map_list *)
5408 xmalloc (sizeof (struct file_name_map_list)));
5409 map_list_ptr->map_list_name = savestring (dirname);
5410 map_list_ptr->map_list_map = NULL;
5411
5412 name = (char *) alloca (strlen (dirname) + strlen (FILE_NAME_MAP_FILE) + 2);
5413 strcpy (name, dirname);
5414 if (*dirname)
5415 strcat (name, "/");
5416 strcat (name, FILE_NAME_MAP_FILE);
5417 f = fopen (name, "r");
5418 if (!f)
5419 map_list_ptr->map_list_map = NULL;
5420 else
5421 {
5422 int ch;
5423 int dirlen = strlen (dirname);
5424
5425 while ((ch = getc (f)) != EOF)
5426 {
5427 char *from, *to;
5428 struct file_name_map *ptr;
5429
5430 if (is_space[ch])
5431 continue;
5432 from = read_filename_string (ch, f);
5433 while ((ch = getc (f)) != EOF && is_hor_space[ch])
5434 ;
5435 to = read_filename_string (ch, f);
5436
5437 ptr = ((struct file_name_map *)
5438 xmalloc (sizeof (struct file_name_map)));
5439 ptr->map_from = from;
5440
5441 /* Make the real filename absolute. */
5442 if (*to == '/')
5443 ptr->map_to = to;
5444 else
5445 {
5446 ptr->map_to = xmalloc (dirlen + strlen (to) + 2);
5447 strcpy (ptr->map_to, dirname);
5448 ptr->map_to[dirlen] = '/';
5449 strcpy (ptr->map_to + dirlen + 1, to);
5450 free (to);
5451 }
5452
5453 ptr->map_next = map_list_ptr->map_list_map;
5454 map_list_ptr->map_list_map = ptr;
5455
5456 while ((ch = getc (f)) != '\n')
5457 if (ch == EOF)
5458 break;
5459 }
5460 fclose (f);
5461 }
5462
5463 map_list_ptr->map_list_next = CPP_OPTIONS (pfile)->map_list;
5464 CPP_OPTIONS (pfile)->map_list = map_list_ptr;
5465
5466 return map_list_ptr->map_list_map;
5467 }
5468
5469 /* Try to open include file FILENAME. SEARCHPTR is the directory
5470 being tried from the include file search path. This function maps
5471 filenames on file systems based on information read by
5472 read_name_map. */
5473
5474 static int
5475 open_include_file (pfile, filename, searchptr)
5476 cpp_reader *pfile;
5477 char *filename;
5478 struct file_name_list *searchptr;
5479 {
5480 if (CPP_OPTIONS (pfile)->remap)
5481 {
5482 register struct file_name_map *map;
5483 register char *from;
5484 char *p, *dir;
5485
5486 if (searchptr && ! searchptr->got_name_map)
5487 {
5488 searchptr->name_map = read_name_map (pfile,
5489 searchptr->fname
5490 ? searchptr->fname : ".");
5491 searchptr->got_name_map = 1;
5492 }
5493
5494 /* First check the mapping for the directory we are using. */
5495 if (searchptr && searchptr->name_map)
5496 {
5497 from = filename;
5498 if (searchptr->fname)
5499 from += strlen (searchptr->fname) + 1;
5500 for (map = searchptr->name_map; map; map = map->map_next)
5501 {
5502 if (! strcmp (map->map_from, from))
5503 {
5504 /* Found a match. */
5505 return open (map->map_to, O_RDONLY, 0666);
5506 }
5507 }
5508 }
5509
5510 /* Try to find a mapping file for the particular directory we are
5511 looking in. Thus #include <sys/types.h> will look up sys/types.h
5512 in /usr/include/header.gcc and look up types.h in
5513 /usr/include/sys/header.gcc. */
5514 p = rindex (filename, '/');
5515 if (! p)
5516 p = filename;
5517 if (searchptr
5518 && searchptr->fname
5519 && strlen (searchptr->fname) == p - filename
5520 && ! strncmp (searchptr->fname, filename, p - filename))
5521 {
5522 /* FILENAME is in SEARCHPTR, which we've already checked. */
5523 return open (filename, O_RDONLY, 0666);
5524 }
5525
5526 if (p == filename)
5527 {
5528 dir = ".";
5529 from = filename;
5530 }
5531 else
5532 {
5533 dir = (char *) alloca (p - filename + 1);
5534 bcopy (filename, dir, p - filename);
5535 dir[p - filename] = '\0';
5536 from = p + 1;
5537 }
5538 for (map = read_name_map (pfile, dir); map; map = map->map_next)
5539 if (! strcmp (map->map_from, from))
5540 return open (map->map_to, O_RDONLY, 0666);
5541 }
5542
5543 return open (filename, O_RDONLY, 0666);
5544 }
5545
5546 /* Process the contents of include file FNAME, already open on descriptor F,
5547 with output to OP.
5548 SYSTEM_HEADER_P is 1 if this file resides in any one of the known
5549 "system" include directories (as decided by the `is_system_include'
5550 function above).
5551 DIRPTR is the link in the dir path through which this file was found,
5552 or 0 if the file name was absolute or via the current directory.
5553 Return 1 on success, 0 on failure.
5554
5555 The caller is responsible for the cpp_push_buffer. */
5556
5557 static int
5558 finclude (pfile, f, fname, system_header_p, dirptr)
5559 cpp_reader *pfile;
5560 int f;
5561 char *fname;
5562 int system_header_p;
5563 struct file_name_list *dirptr;
5564 {
5565 struct stat st;
5566 size_t st_size;
5567 long i;
5568 int length;
5569 cpp_buffer *fp; /* For input stack frame */
5570 #if 0
5571 int missing_newline = 0;
5572 #endif
5573
5574 if (fstat (f, &st) < 0)
5575 {
5576 cpp_perror_with_name (pfile, fname);
5577 close (f);
5578 cpp_pop_buffer (pfile);
5579 return 0;
5580 }
5581
5582 fp = CPP_BUFFER (pfile);
5583 fp->nominal_fname = fp->fname = fname;
5584 #if 0
5585 fp->length = 0;
5586 #endif
5587 fp->dir = dirptr;
5588 fp->system_header_p = system_header_p;
5589 fp->lineno = 1;
5590 fp->colno = 1;
5591 fp->cleanup = file_cleanup;
5592
5593 if (S_ISREG (st.st_mode)) {
5594 st_size = (size_t) st.st_size;
5595 if (st_size != st.st_size || st_size + 2 < st_size) {
5596 cpp_error (pfile, "file `%s' too large", fname);
5597 close (f);
5598 return 0;
5599 }
5600 fp->buf = (U_CHAR *) xmalloc (st_size + 2);
5601 fp->alimit = fp->buf + st_size + 2;
5602 fp->cur = fp->buf;
5603
5604 /* Read the file contents, knowing that st_size is an upper bound
5605 on the number of bytes we can read. */
5606 length = safe_read (f, fp->buf, st_size);
5607 fp->rlimit = fp->buf + length;
5608 if (length < 0) goto nope;
5609 }
5610 else if (S_ISDIR (st.st_mode)) {
5611 cpp_error (pfile, "directory `%s' specified in #include", fname);
5612 close (f);
5613 return 0;
5614 } else {
5615 /* Cannot count its file size before reading.
5616 First read the entire file into heap and
5617 copy them into buffer on stack. */
5618
5619 int bsize = 2000;
5620
5621 st_size = 0;
5622 fp->buf = (U_CHAR *) xmalloc (bsize + 2);
5623
5624 for (;;) {
5625 i = safe_read (f, fp->buf + st_size, bsize - st_size);
5626 if (i < 0)
5627 goto nope; /* error! */
5628 st_size += i;
5629 if (st_size != bsize)
5630 break; /* End of file */
5631 bsize *= 2;
5632 fp->buf = (U_CHAR *) xrealloc (fp->buf, bsize + 2);
5633 }
5634 fp->cur = fp->buf;
5635 length = st_size;
5636 }
5637
5638 if ((length > 0 && fp->buf[length - 1] != '\n')
5639 /* Backslash-newline at end is not good enough. */
5640 || (length > 1 && fp->buf[length - 2] == '\\')) {
5641 fp->buf[length++] = '\n';
5642 #if 0
5643 missing_newline = 1;
5644 #endif
5645 }
5646 fp->buf[length] = '\0';
5647 fp->rlimit = fp->buf + length;
5648
5649 /* Close descriptor now, so nesting does not use lots of descriptors. */
5650 close (f);
5651
5652 /* Must do this before calling trigraph_pcp, so that the correct file name
5653 will be printed in warning messages. */
5654
5655 pfile->input_stack_listing_current = 0;
5656
5657 #if 0
5658 if (!no_trigraphs)
5659 trigraph_pcp (fp);
5660 #endif
5661
5662 #if 0
5663 rescan (op, 0);
5664
5665 if (missing_newline)
5666 fp->lineno--;
5667
5668 if (CPP_PEDANTIC (pfile) && missing_newline)
5669 pedwarn ("file does not end in newline");
5670
5671 indepth--;
5672 input_file_stack_tick++;
5673 free (fp->buf);
5674 #endif
5675 return 1;
5676
5677 nope:
5678
5679 cpp_perror_with_name (pfile, fname);
5680 close (f);
5681 free (fp->buf);
5682 return 1;
5683 }
5684
5685 /* This is called after options have been processed.
5686 * Check options for consistency, and setup for processing input
5687 * from the file named FNAME. (Use standard input if FNAME==NULL.)
5688 * Return 1 on success, 0 on failure.
5689 */
5690
5691 int
5692 cpp_start_read (pfile, fname)
5693 cpp_reader *pfile;
5694 char *fname;
5695 {
5696 struct cpp_options *opts = CPP_OPTIONS (pfile);
5697 struct cpp_pending *pend;
5698 char *p;
5699 int f;
5700 cpp_buffer *fp;
5701
5702 /* The code looks at the defaults through this pointer, rather than through
5703 the constant structure above. This pointer gets changed if an environment
5704 variable specifies other defaults. */
5705 struct default_include *include_defaults = include_defaults_array;
5706
5707 /* Add dirs from CPATH after dirs from -I. */
5708 /* There seems to be confusion about what CPATH should do,
5709 so for the moment it is not documented. */
5710 /* Some people say that CPATH should replace the standard include dirs,
5711 but that seems pointless: it comes before them, so it overrides them
5712 anyway. */
5713 GET_ENV_PATH_LIST (p, "CPATH");
5714 if (p != 0 && ! opts->no_standard_includes)
5715 path_include (pfile, p);
5716
5717 /* Now that dollars_in_ident is known, initialize is_idchar. */
5718 initialize_char_syntax (opts);
5719
5720 /* Do partial setup of input buffer for the sake of generating
5721 early #line directives (when -g is in effect). */
5722 fp = cpp_push_buffer (pfile, NULL, 0);
5723 if (!fp)
5724 return 0;
5725 if (opts->in_fname == NULL)
5726 opts->in_fname = "";
5727 fp->nominal_fname = fp->fname = opts->in_fname;
5728 fp->lineno = 0;
5729
5730 /* Install __LINE__, etc. Must follow initialize_char_syntax
5731 and option processing. */
5732 initialize_builtins (pfile);
5733
5734 /* Do standard #defines and assertions
5735 that identify system and machine type. */
5736
5737 if (!opts->inhibit_predefs) {
5738 char *p = (char *) alloca (strlen (predefs) + 1);
5739 strcpy (p, predefs);
5740 while (*p) {
5741 char *q;
5742 while (*p == ' ' || *p == '\t')
5743 p++;
5744 /* Handle -D options. */
5745 if (p[0] == '-' && p[1] == 'D') {
5746 q = &p[2];
5747 while (*p && *p != ' ' && *p != '\t')
5748 p++;
5749 if (*p != 0)
5750 *p++= 0;
5751 if (opts->debug_output)
5752 output_line_command (pfile, 0, same_file);
5753 cpp_define (pfile, q);
5754 while (*p == ' ' || *p == '\t')
5755 p++;
5756 } else if (p[0] == '-' && p[1] == 'A') {
5757 /* Handle -A options (assertions). */
5758 char *assertion;
5759 char *past_name;
5760 char *value;
5761 char *past_value;
5762 char *termination;
5763 int save_char;
5764
5765 assertion = &p[2];
5766 past_name = assertion;
5767 /* Locate end of name. */
5768 while (*past_name && *past_name != ' '
5769 && *past_name != '\t' && *past_name != '(')
5770 past_name++;
5771 /* Locate `(' at start of value. */
5772 value = past_name;
5773 while (*value && (*value == ' ' || *value == '\t'))
5774 value++;
5775 if (*value++ != '(')
5776 abort ();
5777 while (*value && (*value == ' ' || *value == '\t'))
5778 value++;
5779 past_value = value;
5780 /* Locate end of value. */
5781 while (*past_value && *past_value != ' '
5782 && *past_value != '\t' && *past_value != ')')
5783 past_value++;
5784 termination = past_value;
5785 while (*termination && (*termination == ' ' || *termination == '\t'))
5786 termination++;
5787 if (*termination++ != ')')
5788 abort ();
5789 if (*termination && *termination != ' ' && *termination != '\t')
5790 abort ();
5791 /* Temporarily null-terminate the value. */
5792 save_char = *termination;
5793 *termination = '\0';
5794 /* Install the assertion. */
5795 make_assertion (pfile, "-A", assertion);
5796 *termination = (char) save_char;
5797 p = termination;
5798 while (*p == ' ' || *p == '\t')
5799 p++;
5800 } else {
5801 abort ();
5802 }
5803 }
5804 }
5805
5806 /* Now handle the command line options. */
5807
5808 /* Do -U's, -D's and -A's in the order they were seen. */
5809 /* First reverse the list. */
5810 opts->pending = nreverse_pending (opts->pending);
5811
5812 for (pend = opts->pending; pend; pend = pend->next)
5813 {
5814 if (pend->cmd != NULL && pend->cmd[0] == '-')
5815 {
5816 switch (pend->cmd[1])
5817 {
5818 case 'U':
5819 if (opts->debug_output)
5820 output_line_command (pfile, 0, same_file);
5821 do_undef (pfile, NULL, pend->arg, pend->arg + strlen (pend->arg));
5822 break;
5823 case 'D':
5824 if (opts->debug_output)
5825 output_line_command (pfile, 0, same_file);
5826 cpp_define (pfile, pend->arg);
5827 break;
5828 case 'A':
5829 make_assertion (pfile, "-A", pend->arg);
5830 break;
5831 }
5832 }
5833 }
5834
5835 opts->done_initializing = 1;
5836
5837 { /* Read the appropriate environment variable and if it exists
5838 replace include_defaults with the listed path. */
5839 char *epath = 0;
5840 switch ((opts->objc << 1) + opts->cplusplus)
5841 {
5842 case 0:
5843 GET_ENV_PATH_LIST (epath, "C_INCLUDE_PATH");
5844 break;
5845 case 1:
5846 GET_ENV_PATH_LIST (epath, "CPLUS_INCLUDE_PATH");
5847 break;
5848 case 2:
5849 GET_ENV_PATH_LIST (epath, "OBJC_INCLUDE_PATH");
5850 break;
5851 case 3:
5852 GET_ENV_PATH_LIST (epath, "OBJCPLUS_INCLUDE_PATH");
5853 break;
5854 }
5855 /* If the environment var for this language is set,
5856 add to the default list of include directories. */
5857 if (epath) {
5858 char *nstore = (char *) alloca (strlen (epath) + 2);
5859 int num_dirs;
5860 char *startp, *endp;
5861
5862 for (num_dirs = 1, startp = epath; *startp; startp++)
5863 if (*startp == PATH_SEPARATOR)
5864 num_dirs++;
5865 include_defaults
5866 = (struct default_include *) xmalloc ((num_dirs
5867 * sizeof (struct default_include))
5868 + sizeof (include_defaults_array));
5869 startp = endp = epath;
5870 num_dirs = 0;
5871 while (1) {
5872 /* Handle cases like c:/usr/lib:d:/gcc/lib */
5873 if ((*endp == PATH_SEPARATOR)
5874 || *endp == 0) {
5875 strncpy (nstore, startp, endp-startp);
5876 if (endp == startp)
5877 strcpy (nstore, ".");
5878 else
5879 nstore[endp-startp] = '\0';
5880
5881 include_defaults[num_dirs].fname = savestring (nstore);
5882 include_defaults[num_dirs].component = 0;
5883 include_defaults[num_dirs].cplusplus = opts->cplusplus;
5884 include_defaults[num_dirs].cxx_aware = 1;
5885 num_dirs++;
5886 if (*endp == '\0')
5887 break;
5888 endp = startp = endp + 1;
5889 } else
5890 endp++;
5891 }
5892 /* Put the usual defaults back in at the end. */
5893 bcopy ((char *) include_defaults_array,
5894 (char *) &include_defaults[num_dirs],
5895 sizeof (include_defaults_array));
5896 }
5897 }
5898
5899 append_include_chain (pfile, opts->before_system, opts->last_before_system);
5900 opts->first_system_include = opts->before_system;
5901
5902 /* Unless -fnostdinc,
5903 tack on the standard include file dirs to the specified list */
5904 if (!opts->no_standard_includes) {
5905 struct default_include *p = include_defaults;
5906 char *specd_prefix = opts->include_prefix;
5907 char *default_prefix = savestring (GCC_INCLUDE_DIR);
5908 int default_len = 0;
5909 /* Remove the `include' from /usr/local/lib/gcc.../include. */
5910 if (!strcmp (default_prefix + strlen (default_prefix) - 8, "/include")) {
5911 default_len = strlen (default_prefix) - 7;
5912 default_prefix[default_len] = 0;
5913 }
5914 /* Search "translated" versions of GNU directories.
5915 These have /usr/local/lib/gcc... replaced by specd_prefix. */
5916 if (specd_prefix != 0 && default_len != 0)
5917 for (p = include_defaults; p->fname; p++) {
5918 /* Some standard dirs are only for C++. */
5919 if (!p->cplusplus
5920 || (opts->cplusplus && !opts->no_standard_cplusplus_includes)) {
5921 /* Does this dir start with the prefix? */
5922 if (!strncmp (p->fname, default_prefix, default_len)) {
5923 /* Yes; change prefix and add to search list. */
5924 struct file_name_list *new
5925 = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
5926 int this_len = strlen (specd_prefix) + strlen (p->fname) - default_len;
5927 char *str = (char *) xmalloc (this_len + 1);
5928 strcpy (str, specd_prefix);
5929 strcat (str, p->fname + default_len);
5930 new->fname = str;
5931 new->control_macro = 0;
5932 new->c_system_include_path = !p->cxx_aware;
5933 new->got_name_map = 0;
5934 append_include_chain (pfile, new, new);
5935 if (opts->first_system_include == 0)
5936 opts->first_system_include = new;
5937 }
5938 }
5939 }
5940 /* Search ordinary names for GNU include directories. */
5941 for (p = include_defaults; p->fname; p++) {
5942 /* Some standard dirs are only for C++. */
5943 if (!p->cplusplus
5944 || (opts->cplusplus && !opts->no_standard_cplusplus_includes)) {
5945 struct file_name_list *new
5946 = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
5947 new->control_macro = 0;
5948 new->c_system_include_path = !p->cxx_aware;
5949 new->fname = update_path (p->fname, p->component);
5950 new->got_name_map = 0;
5951 append_include_chain (pfile, new, new);
5952 if (opts->first_system_include == 0)
5953 opts->first_system_include = new;
5954 }
5955 }
5956 }
5957
5958 /* Tack the after_include chain at the end of the include chain. */
5959 append_include_chain (pfile, opts->after_include, opts->last_after_include);
5960 if (opts->first_system_include == 0)
5961 opts->first_system_include = opts->after_include;
5962
5963 /* With -v, print the list of dirs to search. */
5964 if (opts->verbose) {
5965 struct file_name_list *p;
5966 fprintf (stderr, "#include \"...\" search starts here:\n");
5967 for (p = opts->include; p; p = p->next) {
5968 if (p == opts->first_bracket_include)
5969 fprintf (stderr, "#include <...> search starts here:\n");
5970 fprintf (stderr, " %s\n", p->fname);
5971 }
5972 fprintf (stderr, "End of search list.\n");
5973 }
5974
5975 /* Scan the -imacros files before the main input.
5976 Much like #including them, but with no_output set
5977 so that only their macro definitions matter. */
5978
5979 opts->no_output++; pfile->no_record_file++;
5980 for (pend = opts->pending; pend; pend = pend->next)
5981 {
5982 if (pend->cmd != NULL && strcmp (pend->cmd, "-imacros") == 0)
5983 {
5984 int fd = open (pend->arg, O_RDONLY, 0666);
5985 if (fd < 0)
5986 {
5987 cpp_perror_with_name (pfile, pend->arg);
5988 return 0;
5989 }
5990 if (!cpp_push_buffer (pfile, NULL, 0))
5991 return 0;
5992 finclude (pfile, fd, pend->arg, 0, NULL_PTR);
5993 cpp_scan_buffer (pfile);
5994 }
5995 }
5996 opts->no_output--; pfile->no_record_file--;
5997
5998 /* Copy the entire contents of the main input file into
5999 the stacked input buffer previously allocated for it. */
6000 if (fname == NULL || *fname == 0) {
6001 fname = "";
6002 f = 0;
6003 } else if ((f = open (fname, O_RDONLY, 0666)) < 0)
6004 cpp_pfatal_with_name (pfile, fname);
6005
6006 /* -MG doesn't select the form of output and must be specified with one of
6007 -M or -MM. -MG doesn't make sense with -MD or -MMD since they don't
6008 inhibit compilation. */
6009 if (opts->print_deps_missing_files
6010 && (opts->print_deps == 0 || !opts->no_output))
6011 {
6012 cpp_fatal (pfile, "-MG must be specified with one of -M or -MM");
6013 return 0;
6014 }
6015
6016 /* Either of two environment variables can specify output of deps.
6017 Its value is either "OUTPUT_FILE" or "OUTPUT_FILE DEPS_TARGET",
6018 where OUTPUT_FILE is the file to write deps info to
6019 and DEPS_TARGET is the target to mention in the deps. */
6020
6021 if (opts->print_deps == 0
6022 && (getenv ("SUNPRO_DEPENDENCIES") != 0
6023 || getenv ("DEPENDENCIES_OUTPUT") != 0)) {
6024 char *spec = getenv ("DEPENDENCIES_OUTPUT");
6025 char *s;
6026 char *output_file;
6027
6028 if (spec == 0)
6029 {
6030 spec = getenv ("SUNPRO_DEPENDENCIES");
6031 opts->print_deps = 2;
6032 }
6033 else
6034 opts->print_deps = 1;
6035
6036 s = spec;
6037 /* Find the space before the DEPS_TARGET, if there is one. */
6038 /* This should use index. (mrs) */
6039 while (*s != 0 && *s != ' ') s++;
6040 if (*s != 0)
6041 {
6042 opts->deps_target = s + 1;
6043 output_file = (char *) xmalloc (s - spec + 1);
6044 bcopy (spec, output_file, s - spec);
6045 output_file[s - spec] = 0;
6046 }
6047 else
6048 {
6049 opts->deps_target = 0;
6050 output_file = spec;
6051 }
6052
6053 opts->deps_file = output_file;
6054 opts->print_deps_append = 1;
6055 }
6056
6057 /* For -M, print the expected object file name
6058 as the target of this Make-rule. */
6059 if (opts->print_deps)
6060 {
6061 pfile->deps_allocated_size = 200;
6062 pfile->deps_buffer = (char *) xmalloc (pfile->deps_allocated_size);
6063 pfile->deps_buffer[0] = 0;
6064 pfile->deps_size = 0;
6065 pfile->deps_column = 0;
6066
6067 if (opts->deps_target)
6068 deps_output (pfile, opts->deps_target, ':');
6069 else if (*opts->in_fname == 0)
6070 deps_output (pfile, "-", ':');
6071 else
6072 {
6073 char *p, *q, *r;
6074 int len, x;
6075 static char *known_suffixes[] = { ".c", ".C", ".s", ".S", ".m",
6076 ".cc", ".cxx", ".cpp", ".cp",
6077 ".c++", 0
6078 };
6079
6080 /* Discard all directory prefixes from filename. */
6081 if ((q = rindex (opts->in_fname, '/')) != NULL
6082 #ifdef DIR_SEPARATOR
6083 && (q = rindex (opts->in_fname, DIR_SEPARATOR)) != NULL
6084 #endif
6085 )
6086 ++q;
6087 else
6088 q = opts->in_fname;
6089
6090 /* Copy remainder to mungable area. */
6091 p = (char *) alloca (strlen(q) + 8);
6092 strcpy (p, q);
6093
6094 /* Output P, but remove known suffixes. */
6095 len = strlen (p);
6096 q = p + len;
6097 /* Point to the filename suffix. */
6098 r = rindex (p, '.');
6099 /* Compare against the known suffixes. */
6100 x = 0;
6101 while (known_suffixes[x] != 0)
6102 {
6103 if (strncmp (known_suffixes[x], r, q - r) == 0)
6104 {
6105 /* Make q point to the bit we're going to overwrite
6106 with an object suffix. */
6107 q = r;
6108 break;
6109 }
6110 x++;
6111 }
6112
6113 /* Supply our own suffix. */
6114 #ifndef VMS
6115 strcpy (q, ".o");
6116 #else
6117 strcpy (q, ".obj");
6118 #endif
6119
6120 deps_output (pfile, p, ':');
6121 deps_output (pfile, opts->in_fname, ' ');
6122 }
6123 }
6124
6125 #if 0
6126 /* Make sure data ends with a newline. And put a null after it. */
6127
6128 if ((fp->length > 0 && fp->buf[fp->length - 1] != '\n')
6129 /* Backslash-newline at end is not good enough. */
6130 || (fp->length > 1 && fp->buf[fp->length - 2] == '\\')) {
6131 fp->buf[fp->length++] = '\n';
6132 missing_newline = 1;
6133 }
6134 fp->buf[fp->length] = '\0';
6135
6136 /* Unless inhibited, convert trigraphs in the input. */
6137
6138 if (!no_trigraphs)
6139 trigraph_pcp (fp);
6140 #endif
6141
6142 /* Scan the -include files before the main input.
6143 We push these in reverse order, so that the first one is handled first. */
6144
6145 pfile->no_record_file++;
6146 opts->pending = nreverse_pending (opts->pending);
6147 for (pend = opts->pending; pend; pend = pend->next)
6148 {
6149 if (pend->cmd != NULL && strcmp (pend->cmd, "-include") == 0)
6150 {
6151 int fd = open (pend->arg, O_RDONLY, 0666);
6152 if (fd < 0)
6153 {
6154 cpp_perror_with_name (pfile, pend->arg);
6155 return 0;
6156 }
6157 if (!cpp_push_buffer (pfile, NULL, 0))
6158 return 0;
6159 finclude (pfile, fd, pend->arg, 0, NULL_PTR);
6160 }
6161 }
6162 pfile->no_record_file--;
6163
6164 /* Free the pending list. */
6165 for (pend = opts->pending; pend; )
6166 {
6167 struct cpp_pending *next = pend->next;
6168 free (pend);
6169 pend = next;
6170 }
6171 opts->pending = NULL;
6172
6173 #if 0
6174 /* Scan the input, processing macros and directives. */
6175
6176 rescan (&outbuf, 0);
6177
6178 if (missing_newline)
6179 fp->lineno--;
6180
6181 if (CPP_PEDANTIC (pfile) && missing_newline)
6182 pedwarn ("file does not end in newline");
6183
6184 #endif
6185 if (finclude (pfile, f, fname, 0, NULL_PTR))
6186 output_line_command (pfile, 0, same_file);
6187 return 1;
6188 }
6189
6190 void
6191 cpp_reader_init (pfile)
6192 cpp_reader *pfile;
6193 {
6194 bzero ((char *) pfile, sizeof (cpp_reader));
6195 pfile->get_token = cpp_get_token;
6196
6197 pfile->token_buffer_size = 200;
6198 pfile->token_buffer = (U_CHAR *) xmalloc (pfile->token_buffer_size);
6199 CPP_SET_WRITTEN (pfile, 0);
6200
6201 pfile->system_include_depth = 0;
6202 pfile->dont_repeat_files = 0;
6203 pfile->all_include_files = 0;
6204 pfile->max_include_len = 0;
6205 pfile->timebuf = NULL;
6206 pfile->only_seen_white = 1;
6207 pfile->buffer = CPP_NULL_BUFFER(pfile);
6208 }
6209
6210 static struct cpp_pending *
6211 nreverse_pending (list)
6212 struct cpp_pending *list;
6213
6214 {
6215 register struct cpp_pending *prev = 0, *next, *pend;
6216 for (pend = list; pend; pend = next)
6217 {
6218 next = pend->next;
6219 pend->next = prev;
6220 prev = pend;
6221 }
6222 return prev;
6223 }
6224
6225 static void
6226 push_pending (pfile, cmd, arg)
6227 cpp_reader *pfile;
6228 char *cmd;
6229 char *arg;
6230 {
6231 struct cpp_pending *pend
6232 = (struct cpp_pending *) xmalloc (sizeof (struct cpp_pending));
6233 pend->cmd = cmd;
6234 pend->arg = arg;
6235 pend->next = CPP_OPTIONS (pfile)->pending;
6236 CPP_OPTIONS (pfile)->pending = pend;
6237 }
6238
6239 \f
6240 static void
6241 print_help ()
6242 {
6243 printf ("Usage: %s [switches] input output\n", progname);
6244 printf ("Switches:\n");
6245 printf (" -include <file> Include the contents of <file> before other files\n");
6246 printf (" -imacros <file> Accept definition of marcos in <file>\n");
6247 printf (" -iprefix <path> Specify <path> as a prefix for next two options\n");
6248 printf (" -iwithprefix <dir> Add <dir> to the end of the system include paths\n");
6249 printf (" -iwithprefixbefore <dir> Add <dir> to the end of the main include paths\n");
6250 printf (" -isystem <dir> Add <dir> to the start of the system include paths\n");
6251 printf (" -idirafter <dir> Add <dir> to the end of the system include paths\n");
6252 printf (" -I <dir> Add <dir> to the end of the main include paths\n");
6253 printf (" -nostdinc Do not search the system include directories\n");
6254 printf (" -nostdinc++ Do not search the system include directories for C++\n");
6255 printf (" -o <file> Put output into <file>\n");
6256 printf (" -pedantic Issue all warnings demanded by strict ANSI C\n");
6257 printf (" -traditional Follow K&R pre-processor behaviour\n");
6258 printf (" -trigraphs Support ANSI C trigraphs\n");
6259 printf (" -lang-c Assume that the input sources are in C\n");
6260 printf (" -lang-c89 Assume that the input sources are in C89\n");
6261 printf (" -lang-c++ Assume that the input sources are in C++\n");
6262 printf (" -lang-objc Assume that the input sources are in ObjectiveC\n");
6263 printf (" -lang-objc++ Assume that the input sources are in ObjectiveC++\n");
6264 printf (" -lang-asm Assume that the input sources are in assembler\n");
6265 printf (" -lang-chill Assume that the input sources are in Chill\n");
6266 printf (" -+ Allow parsing of C++ style features\n");
6267 printf (" -w Inhibit warning messages\n");
6268 printf (" -Wtrigraphs Warn if trigraphs are encountered\n");
6269 printf (" -Wno-trigraphs Do not warn about trigraphs\n");
6270 printf (" -Wcomment{s} Warn if one comment starts inside another\n");
6271 printf (" -Wno-comment{s} Do not warn about comments\n");
6272 printf (" -Wtraditional Warn if a macro argument is/would be turned into\n");
6273 printf (" a string if -tradtional is specified\n");
6274 printf (" -Wno-traditional Do not warn about stringification\n");
6275 printf (" -Wundef Warn if an undefined macro is used by #if\n");
6276 printf (" -Wno-undef Do not warn about testing udefined macros\n");
6277 printf (" -Wimport Warn about the use of the #import directive\n");
6278 printf (" -Wno-import Do not warn about the use of #import\n");
6279 printf (" -Werror Treat all warnings as errors\n");
6280 printf (" -Wno-error Do not treat warnings as errors\n");
6281 printf (" -Wall Enable all preprocessor warnings\n");
6282 printf (" -M Generate make dependencies\n");
6283 printf (" -MM As -M, but ignore system header files\n");
6284 printf (" -MD As -M, but put output in a .d file\n");
6285 printf (" -MMD As -MD, but ignore system header files\n");
6286 printf (" -MG Treat missing header file as generated files\n");
6287 printf (" -g Include #define and #undef directives in the output\n");
6288 printf (" -D<macro> Define a <macro> with string '1' as its value\n");
6289 printf (" -D<macro>=<val> Define a <macro> with <val> as its value\n");
6290 printf (" -A<question> (<answer>) Assert the <answer> to <question>\n");
6291 printf (" -U<macro> Undefine <macro> \n");
6292 printf (" -u or -undef Do not predefine any macros\n");
6293 printf (" -v Display the version number\n");
6294 printf (" -H Print the name of header files as they are used\n");
6295 printf (" -C Do not discard comments\n");
6296 printf (" -dM Display a list of macro definitions active at end\n");
6297 printf (" -dD Preserve macro definitions in output\n");
6298 printf (" -dN As -dD except that only the names are preserved\n");
6299 printf (" -dI Include #include directives in the output\n");
6300 printf (" -ifoutput Describe skipped code blocks in output \n");
6301 printf (" -P Do not generate #line directives\n");
6302 printf (" -$ Do not include '$' in identifiers\n");
6303 printf (" -remap Remap file names when including files.\n");
6304 printf (" -h or --help Display this information\n");
6305 }
6306 \f
6307
6308 /* Handle one command-line option in (argc, argv).
6309 Can be called multiple times, to handle multiple sets of options.
6310 Returns number of strings consumed. */
6311 int
6312 cpp_handle_option (pfile, argc, argv)
6313 cpp_reader *pfile;
6314 int argc;
6315 char **argv;
6316 {
6317 struct cpp_options *opts = CPP_OPTIONS (pfile);
6318 int i = 0;
6319 if (argv[i][0] != '-') {
6320 if (opts->out_fname != NULL)
6321 {
6322 print_help ();
6323 cpp_fatal (pfile, "Too many arguments");
6324 }
6325 else if (opts->in_fname != NULL)
6326 opts->out_fname = argv[i];
6327 else
6328 opts->in_fname = argv[i];
6329 } else {
6330 switch (argv[i][1]) {
6331
6332 missing_filename:
6333 cpp_fatal (pfile, "Filename missing after `%s' option", argv[i]);
6334 return argc;
6335 missing_dirname:
6336 cpp_fatal (pfile, "Directory name missing after `%s' option", argv[i]);
6337 return argc;
6338
6339 case 'i':
6340 if (!strcmp (argv[i], "-include")
6341 || !strcmp (argv[i], "-imacros")) {
6342 if (i + 1 == argc)
6343 goto missing_filename;
6344 else
6345 push_pending (pfile, argv[i], argv[i+1]), i++;
6346 }
6347 if (!strcmp (argv[i], "-iprefix")) {
6348 if (i + 1 == argc)
6349 goto missing_filename;
6350 else
6351 opts->include_prefix = argv[++i];
6352 }
6353 if (!strcmp (argv[i], "-ifoutput")) {
6354 opts->output_conditionals = 1;
6355 }
6356 if (!strcmp (argv[i], "-isystem")) {
6357 struct file_name_list *dirtmp;
6358
6359 if (i + 1 == argc)
6360 goto missing_filename;
6361
6362 dirtmp = (struct file_name_list *)
6363 xmalloc (sizeof (struct file_name_list));
6364 dirtmp->next = 0;
6365 dirtmp->control_macro = 0;
6366 dirtmp->c_system_include_path = 1;
6367 dirtmp->fname = (char *) xmalloc (strlen (argv[i+1]) + 1);
6368 strcpy (dirtmp->fname, argv[++i]);
6369 dirtmp->got_name_map = 0;
6370
6371 if (opts->before_system == 0)
6372 opts->before_system = dirtmp;
6373 else
6374 opts->last_before_system->next = dirtmp;
6375 opts->last_before_system = dirtmp; /* Tail follows the last one */
6376 }
6377 /* Add directory to end of path for includes,
6378 with the default prefix at the front of its name. */
6379 if (!strcmp (argv[i], "-iwithprefix")) {
6380 struct file_name_list *dirtmp;
6381 char *prefix;
6382
6383 if (opts->include_prefix != 0)
6384 prefix = opts->include_prefix;
6385 else {
6386 prefix = savestring (GCC_INCLUDE_DIR);
6387 /* Remove the `include' from /usr/local/lib/gcc.../include. */
6388 if (!strcmp (prefix + strlen (prefix) - 8, "/include"))
6389 prefix[strlen (prefix) - 7] = 0;
6390 }
6391
6392 dirtmp = (struct file_name_list *)
6393 xmalloc (sizeof (struct file_name_list));
6394 dirtmp->next = 0; /* New one goes on the end */
6395 dirtmp->control_macro = 0;
6396 dirtmp->c_system_include_path = 0;
6397 if (i + 1 == argc)
6398 goto missing_dirname;
6399
6400 dirtmp->fname = (char *) xmalloc (strlen (argv[i+1])
6401 + strlen (prefix) + 1);
6402 strcpy (dirtmp->fname, prefix);
6403 strcat (dirtmp->fname, argv[++i]);
6404 dirtmp->got_name_map = 0;
6405
6406 if (opts->after_include == 0)
6407 opts->after_include = dirtmp;
6408 else
6409 opts->last_after_include->next = dirtmp;
6410 opts->last_after_include = dirtmp; /* Tail follows the last one */
6411 }
6412 /* Add directory to main path for includes,
6413 with the default prefix at the front of its name. */
6414 if (!strcmp (argv[i], "-iwithprefixbefore")) {
6415 struct file_name_list *dirtmp;
6416 char *prefix;
6417
6418 if (opts->include_prefix != 0)
6419 prefix = opts->include_prefix;
6420 else {
6421 prefix = savestring (GCC_INCLUDE_DIR);
6422 /* Remove the `include' from /usr/local/lib/gcc.../include. */
6423 if (!strcmp (prefix + strlen (prefix) - 8, "/include"))
6424 prefix[strlen (prefix) - 7] = 0;
6425 }
6426
6427 dirtmp = (struct file_name_list *)
6428 xmalloc (sizeof (struct file_name_list));
6429 dirtmp->next = 0; /* New one goes on the end */
6430 dirtmp->control_macro = 0;
6431 dirtmp->c_system_include_path = 0;
6432 if (i + 1 == argc)
6433 goto missing_dirname;
6434
6435 dirtmp->fname = (char *) xmalloc (strlen (argv[i+1])
6436 + strlen (prefix) + 1);
6437 strcpy (dirtmp->fname, prefix);
6438 strcat (dirtmp->fname, argv[++i]);
6439 dirtmp->got_name_map = 0;
6440
6441 append_include_chain (pfile, dirtmp, dirtmp);
6442 }
6443 /* Add directory to end of path for includes. */
6444 if (!strcmp (argv[i], "-idirafter")) {
6445 struct file_name_list *dirtmp;
6446
6447 dirtmp = (struct file_name_list *)
6448 xmalloc (sizeof (struct file_name_list));
6449 dirtmp->next = 0; /* New one goes on the end */
6450 dirtmp->control_macro = 0;
6451 dirtmp->c_system_include_path = 0;
6452 if (i + 1 == argc)
6453 goto missing_dirname;
6454 else
6455 dirtmp->fname = argv[++i];
6456 dirtmp->got_name_map = 0;
6457
6458 if (opts->after_include == 0)
6459 opts->after_include = dirtmp;
6460 else
6461 opts->last_after_include->next = dirtmp;
6462 opts->last_after_include = dirtmp; /* Tail follows the last one */
6463 }
6464 break;
6465
6466 case 'o':
6467 if (opts->out_fname != NULL)
6468 {
6469 cpp_fatal (pfile, "Output filename specified twice");
6470 return argc;
6471 }
6472 if (i + 1 == argc)
6473 goto missing_filename;
6474 opts->out_fname = argv[++i];
6475 if (!strcmp (opts->out_fname, "-"))
6476 opts->out_fname = "";
6477 break;
6478
6479 case 'p':
6480 if (!strcmp (argv[i], "-pedantic"))
6481 CPP_PEDANTIC (pfile) = 1;
6482 else if (!strcmp (argv[i], "-pedantic-errors")) {
6483 CPP_PEDANTIC (pfile) = 1;
6484 opts->pedantic_errors = 1;
6485 }
6486 #if 0
6487 else if (!strcmp (argv[i], "-pcp")) {
6488 char *pcp_fname = argv[++i];
6489 pcp_outfile = ((pcp_fname[0] != '-' || pcp_fname[1] != '\0')
6490 ? fopen (pcp_fname, "w")
6491 : fdopen (dup (fileno (stdout)), "w"));
6492 if (pcp_outfile == 0)
6493 cpp_pfatal_with_name (pfile, pcp_fname);
6494 no_precomp = 1;
6495 }
6496 #endif
6497 break;
6498
6499 case 't':
6500 if (!strcmp (argv[i], "-traditional")) {
6501 opts->traditional = 1;
6502 opts->cplusplus_comments = 0;
6503 } else if (!strcmp (argv[i], "-trigraphs")) {
6504 if (!opts->chill)
6505 opts->no_trigraphs = 0;
6506 }
6507 break;
6508
6509 case 'l':
6510 if (! strcmp (argv[i], "-lang-c"))
6511 opts->cplusplus = 0, opts->cplusplus_comments = 1, opts->c89 = 0,
6512 opts->objc = 0;
6513 if (! strcmp (argv[i], "-lang-c89"))
6514 opts->cplusplus = 0, opts->cplusplus_comments = 0, opts->c89 = 1,
6515 opts->objc = 0;
6516 if (! strcmp (argv[i], "-lang-c++"))
6517 opts->cplusplus = 1, opts->cplusplus_comments = 1, opts->c89 = 0,
6518 opts->objc = 0;
6519 if (! strcmp (argv[i], "-lang-objc"))
6520 opts->cplusplus = 0, opts->cplusplus_comments = 1, opts->c89 = 0,
6521 opts->objc = 1;
6522 if (! strcmp (argv[i], "-lang-objc++"))
6523 opts->cplusplus = 1, opts->cplusplus_comments = 1, opts->c89 = 0,
6524 opts->objc = 1;
6525 if (! strcmp (argv[i], "-lang-asm"))
6526 opts->lang_asm = 1;
6527 if (! strcmp (argv[i], "-lint"))
6528 opts->for_lint = 1;
6529 if (! strcmp (argv[i], "-lang-chill"))
6530 opts->objc = 0, opts->cplusplus = 0, opts->chill = 1,
6531 opts->traditional = 1, opts->no_trigraphs = 1;
6532 break;
6533
6534 case '+':
6535 opts->cplusplus = 1, opts->cplusplus_comments = 1;
6536 break;
6537
6538 case 'w':
6539 opts->inhibit_warnings = 1;
6540 break;
6541
6542 case 'W':
6543 if (!strcmp (argv[i], "-Wtrigraphs"))
6544 opts->warn_trigraphs = 1;
6545 else if (!strcmp (argv[i], "-Wno-trigraphs"))
6546 opts->warn_trigraphs = 0;
6547 else if (!strcmp (argv[i], "-Wcomment"))
6548 opts->warn_comments = 1;
6549 else if (!strcmp (argv[i], "-Wno-comment"))
6550 opts->warn_comments = 0;
6551 else if (!strcmp (argv[i], "-Wcomments"))
6552 opts->warn_comments = 1;
6553 else if (!strcmp (argv[i], "-Wno-comments"))
6554 opts->warn_comments = 0;
6555 else if (!strcmp (argv[i], "-Wtraditional"))
6556 opts->warn_stringify = 1;
6557 else if (!strcmp (argv[i], "-Wno-traditional"))
6558 opts->warn_stringify = 0;
6559 else if (!strcmp (argv[i], "-Wundef"))
6560 opts->warn_undef = 1;
6561 else if (!strcmp (argv[i], "-Wno-undef"))
6562 opts->warn_undef = 0;
6563 else if (!strcmp (argv[i], "-Wimport"))
6564 opts->warn_import = 1;
6565 else if (!strcmp (argv[i], "-Wno-import"))
6566 opts->warn_import = 0;
6567 else if (!strcmp (argv[i], "-Werror"))
6568 opts->warnings_are_errors = 1;
6569 else if (!strcmp (argv[i], "-Wno-error"))
6570 opts->warnings_are_errors = 0;
6571 else if (!strcmp (argv[i], "-Wall"))
6572 {
6573 opts->warn_trigraphs = 1;
6574 opts->warn_comments = 1;
6575 }
6576 break;
6577
6578 case 'M':
6579 /* The style of the choices here is a bit mixed.
6580 The chosen scheme is a hybrid of keeping all options in one string
6581 and specifying each option in a separate argument:
6582 -M|-MM|-MD file|-MMD file [-MG]. An alternative is:
6583 -M|-MM|-MD file|-MMD file|-MG|-MMG; or more concisely:
6584 -M[M][G][D file]. This is awkward to handle in specs, and is not
6585 as extensible. */
6586 /* ??? -MG must be specified in addition to one of -M or -MM.
6587 This can be relaxed in the future without breaking anything.
6588 The converse isn't true. */
6589
6590 /* -MG isn't valid with -MD or -MMD. This is checked for later. */
6591 if (!strcmp (argv[i], "-MG"))
6592 {
6593 opts->print_deps_missing_files = 1;
6594 break;
6595 }
6596 if (!strcmp (argv[i], "-M"))
6597 opts->print_deps = 2;
6598 else if (!strcmp (argv[i], "-MM"))
6599 opts->print_deps = 1;
6600 else if (!strcmp (argv[i], "-MD"))
6601 opts->print_deps = 2;
6602 else if (!strcmp (argv[i], "-MMD"))
6603 opts->print_deps = 1;
6604 /* For -MD and -MMD options, write deps on file named by next arg. */
6605 if (!strcmp (argv[i], "-MD") || !strcmp (argv[i], "-MMD"))
6606 {
6607 if (i+1 == argc)
6608 goto missing_filename;
6609 opts->deps_file = argv[++i];
6610 }
6611 else
6612 {
6613 /* For -M and -MM, write deps on standard output
6614 and suppress the usual output. */
6615 opts->no_output = 1;
6616 }
6617 break;
6618
6619 case 'd':
6620 {
6621 char *p = argv[i] + 2;
6622 char c;
6623 while ((c = *p++) != 0) {
6624 /* Arg to -d specifies what parts of macros to dump */
6625 switch (c) {
6626 case 'M':
6627 opts->dump_macros = dump_only;
6628 opts->no_output = 1;
6629 break;
6630 case 'N':
6631 opts->dump_macros = dump_names;
6632 break;
6633 case 'D':
6634 opts->dump_macros = dump_definitions;
6635 break;
6636 case 'I':
6637 opts->dump_includes = 1;
6638 break;
6639 }
6640 }
6641 }
6642 break;
6643
6644 case 'g':
6645 if (argv[i][2] == '3')
6646 opts->debug_output = 1;
6647 break;
6648
6649 case '-':
6650 if (strcmp (argv[i], "--help") != 0)
6651 return i;
6652 print_help ();
6653 break;
6654
6655 case 'v':
6656 fprintf (stderr, "GNU CPP version %s", version_string);
6657 #ifdef TARGET_VERSION
6658 TARGET_VERSION;
6659 #endif
6660 fprintf (stderr, "\n");
6661 opts->verbose = 1;
6662 break;
6663
6664 case 'H':
6665 opts->print_include_names = 1;
6666 break;
6667
6668 case 'D':
6669 if (argv[i][2] != 0)
6670 push_pending (pfile, "-D", argv[i] + 2);
6671 else if (i + 1 == argc)
6672 {
6673 cpp_fatal (pfile, "Macro name missing after -D option");
6674 return argc;
6675 }
6676 else
6677 i++, push_pending (pfile, "-D", argv[i]);
6678 break;
6679
6680 case 'A':
6681 {
6682 char *p;
6683
6684 if (argv[i][2] != 0)
6685 p = argv[i] + 2;
6686 else if (i + 1 == argc)
6687 {
6688 cpp_fatal (pfile, "Assertion missing after -A option");
6689 return argc;
6690 }
6691 else
6692 p = argv[++i];
6693
6694 if (!strcmp (p, "-")) {
6695 struct cpp_pending **ptr;
6696 /* -A- eliminates all predefined macros and assertions.
6697 Let's include also any that were specified earlier
6698 on the command line. That way we can get rid of any
6699 that were passed automatically in from GCC. */
6700 opts->inhibit_predefs = 1;
6701 for (ptr = &opts->pending; *ptr != NULL; )
6702 {
6703 struct cpp_pending *pend = *ptr;
6704 if (pend->cmd && pend->cmd[0] == '-'
6705 && (pend->cmd[1] == 'D' || pend->cmd[1] == 'A'))
6706 {
6707 *ptr = pend->next;
6708 free (pend);
6709 }
6710 else
6711 ptr = &pend->next;
6712 }
6713 } else {
6714 push_pending (pfile, "-A", p);
6715 }
6716 }
6717 break;
6718
6719 case 'U': /* JF #undef something */
6720 if (argv[i][2] != 0)
6721 push_pending (pfile, "-U", argv[i] + 2);
6722 else if (i + 1 == argc)
6723 {
6724 cpp_fatal (pfile, "Macro name missing after -U option");
6725 return argc;
6726 }
6727 else
6728 push_pending (pfile, "-U", argv[i+1]), i++;
6729 break;
6730
6731 case 'C':
6732 opts->put_out_comments = 1;
6733 break;
6734
6735 case 'E': /* -E comes from cc -E; ignore it. */
6736 break;
6737
6738 case 'P':
6739 opts->no_line_commands = 1;
6740 break;
6741
6742 case '$': /* Don't include $ in identifiers. */
6743 opts->dollars_in_ident = 0;
6744 break;
6745
6746 case 'I': /* Add directory to path for includes. */
6747 {
6748 struct file_name_list *dirtmp;
6749
6750 if (! CPP_OPTIONS(pfile)->ignore_srcdir
6751 && !strcmp (argv[i] + 2, "-")) {
6752 CPP_OPTIONS (pfile)->ignore_srcdir = 1;
6753 /* Don't use any preceding -I directories for #include <...>. */
6754 CPP_OPTIONS (pfile)->first_bracket_include = 0;
6755 }
6756 else {
6757 dirtmp = (struct file_name_list *)
6758 xmalloc (sizeof (struct file_name_list));
6759 dirtmp->next = 0; /* New one goes on the end */
6760 dirtmp->control_macro = 0;
6761 dirtmp->c_system_include_path = 0;
6762 if (argv[i][2] != 0)
6763 dirtmp->fname = argv[i] + 2;
6764 else if (i + 1 == argc)
6765 goto missing_dirname;
6766 else
6767 dirtmp->fname = argv[++i];
6768 dirtmp->got_name_map = 0;
6769 append_include_chain (pfile, dirtmp, dirtmp);
6770 }
6771 }
6772 break;
6773
6774 case 'n':
6775 if (!strcmp (argv[i], "-nostdinc"))
6776 /* -nostdinc causes no default include directories.
6777 You must specify all include-file directories with -I. */
6778 opts->no_standard_includes = 1;
6779 else if (!strcmp (argv[i], "-nostdinc++"))
6780 /* -nostdinc++ causes no default C++-specific include directories. */
6781 opts->no_standard_cplusplus_includes = 1;
6782 #if 0
6783 else if (!strcmp (argv[i], "-noprecomp"))
6784 no_precomp = 1;
6785 #endif
6786 break;
6787
6788 case 'r':
6789 if (!strcmp (argv[i], "-remap"))
6790 opts->remap = 1;
6791 break;
6792
6793 case 'u':
6794 /* Sun compiler passes undocumented switch "-undef".
6795 Let's assume it means to inhibit the predefined symbols. */
6796 opts->inhibit_predefs = 1;
6797 break;
6798
6799 case '\0': /* JF handle '-' as file name meaning stdin or stdout */
6800 if (opts->in_fname == NULL) {
6801 opts->in_fname = "";
6802 break;
6803 } else if (opts->out_fname == NULL) {
6804 opts->out_fname = "";
6805 break;
6806 } /* else fall through into error */
6807
6808 default:
6809 return i;
6810 }
6811 }
6812
6813 return i + 1;
6814 }
6815
6816 /* Handle command-line options in (argc, argv).
6817 Can be called multiple times, to handle multiple sets of options.
6818 Returns if an unrecognized option is seen.
6819 Returns number of strings consumed. */
6820
6821 int
6822 cpp_handle_options (pfile, argc, argv)
6823 cpp_reader *pfile;
6824 int argc;
6825 char **argv;
6826 {
6827 int i;
6828 int strings_processed;
6829 for (i = 0; i < argc; i += strings_processed)
6830 {
6831 strings_processed = cpp_handle_option (pfile, argc - i, argv + i);
6832 if (strings_processed == 0)
6833 break;
6834 }
6835 return i;
6836 }
6837 \f
6838 void
6839 cpp_finish (pfile)
6840 cpp_reader *pfile;
6841 {
6842 struct cpp_options *opts = CPP_OPTIONS (pfile);
6843
6844 if (opts->print_deps)
6845 {
6846 /* Stream on which to print the dependency information. */
6847 FILE *deps_stream;
6848
6849 /* Don't actually write the deps file if compilation has failed. */
6850 if (pfile->errors == 0)
6851 {
6852 char *deps_mode = opts->print_deps_append ? "a" : "w";
6853 if (opts->deps_file == 0)
6854 deps_stream = stdout;
6855 else if ((deps_stream = fopen (opts->deps_file, deps_mode)) == 0)
6856 cpp_pfatal_with_name (pfile, opts->deps_file);
6857 fputs (pfile->deps_buffer, deps_stream);
6858 putc ('\n', deps_stream);
6859 if (opts->deps_file)
6860 {
6861 if (ferror (deps_stream) || fclose (deps_stream) != 0)
6862 cpp_fatal (pfile, "I/O error on output");
6863 }
6864 }
6865 }
6866 }
6867
6868 /* Free resources used by PFILE.
6869 This is the cpp_reader 'finalizer' or 'destructor' (in C++ terminology). */
6870
6871 void
6872 cpp_cleanup (pfile)
6873 cpp_reader *pfile;
6874 {
6875 int i;
6876 while ( CPP_BUFFER (pfile) != CPP_NULL_BUFFER (pfile))
6877 cpp_pop_buffer (pfile);
6878
6879 if (pfile->token_buffer)
6880 {
6881 free (pfile->token_buffer);
6882 pfile->token_buffer = NULL;
6883 }
6884
6885 if (pfile->deps_buffer)
6886 {
6887 free (pfile->deps_buffer);
6888 pfile->deps_buffer = NULL;
6889 pfile->deps_allocated_size = 0;
6890 }
6891
6892 while (pfile->if_stack)
6893 {
6894 IF_STACK_FRAME *temp = pfile->if_stack;
6895 pfile->if_stack = temp->next;
6896 free (temp);
6897 }
6898
6899 while (pfile->dont_repeat_files)
6900 {
6901 struct file_name_list *temp = pfile->dont_repeat_files;
6902 pfile->dont_repeat_files = temp->next;
6903 free (temp->fname);
6904 free (temp);
6905 }
6906
6907 while (pfile->all_include_files)
6908 {
6909 struct file_name_list *temp = pfile->all_include_files;
6910 pfile->all_include_files = temp->next;
6911 free (temp->fname);
6912 free (temp);
6913 }
6914
6915 for (i = IMPORT_HASH_SIZE; --i >= 0; )
6916 {
6917 register struct import_file *imp = pfile->import_hash_table[i];
6918 while (imp)
6919 {
6920 struct import_file *next = imp->next;
6921 free (imp->name);
6922 free (imp);
6923 imp = next;
6924 }
6925 pfile->import_hash_table[i] = 0;
6926 }
6927
6928 for (i = ASSERTION_HASHSIZE; --i >= 0; )
6929 {
6930 while (pfile->assertion_hashtab[i])
6931 delete_assertion (pfile->assertion_hashtab[i]);
6932 }
6933
6934 cpp_hash_cleanup (pfile);
6935 }
6936 \f
6937 static int
6938 do_assert (pfile, keyword, buf, limit)
6939 cpp_reader *pfile;
6940 struct directive *keyword ATTRIBUTE_UNUSED;
6941 U_CHAR *buf ATTRIBUTE_UNUSED, *limit ATTRIBUTE_UNUSED;
6942 {
6943 long symstart; /* remember where symbol name starts */
6944 int c;
6945 int sym_length; /* and how long it is */
6946 struct arglist *tokens = NULL;
6947
6948 if (CPP_PEDANTIC (pfile) && CPP_OPTIONS (pfile)->done_initializing
6949 && !CPP_BUFFER (pfile)->system_header_p)
6950 cpp_pedwarn (pfile, "ANSI C does not allow `#assert'");
6951
6952 cpp_skip_hspace (pfile);
6953 symstart = CPP_WRITTEN (pfile); /* remember where it starts */
6954 parse_name (pfile, GETC());
6955 sym_length = check_macro_name (pfile, pfile->token_buffer + symstart,
6956 "assertion");
6957
6958 cpp_skip_hspace (pfile);
6959 if (PEEKC() != '(') {
6960 cpp_error (pfile, "missing token-sequence in `#assert'");
6961 goto error;
6962 }
6963
6964 {
6965 int error_flag = 0;
6966 tokens = read_token_list (pfile, &error_flag);
6967 if (error_flag)
6968 goto error;
6969 if (tokens == 0) {
6970 cpp_error (pfile, "empty token-sequence in `#assert'");
6971 goto error;
6972 }
6973 cpp_skip_hspace (pfile);
6974 c = PEEKC ();
6975 if (c != EOF && c != '\n')
6976 cpp_pedwarn (pfile, "junk at end of `#assert'");
6977 skip_rest_of_line (pfile);
6978 }
6979
6980 /* If this name isn't already an assertion name, make it one.
6981 Error if it was already in use in some other way. */
6982
6983 {
6984 ASSERTION_HASHNODE *hp;
6985 U_CHAR *symname = pfile->token_buffer + symstart;
6986 int hashcode = hashf (symname, sym_length, ASSERTION_HASHSIZE);
6987 struct tokenlist_list *value
6988 = (struct tokenlist_list *) xmalloc (sizeof (struct tokenlist_list));
6989
6990 hp = assertion_lookup (pfile, symname, sym_length, hashcode);
6991 if (hp == NULL) {
6992 if (sym_length == 7 && ! strncmp (symname, "defined", sym_length))
6993 cpp_error (pfile, "`defined' redefined as assertion");
6994 hp = assertion_install (pfile, symname, sym_length, hashcode);
6995 }
6996
6997 /* Add the spec'd token-sequence to the list of such. */
6998 value->tokens = tokens;
6999 value->next = hp->value;
7000 hp->value = value;
7001 }
7002 CPP_SET_WRITTEN (pfile, symstart); /* Pop */
7003 return 0;
7004 error:
7005 CPP_SET_WRITTEN (pfile, symstart); /* Pop */
7006 skip_rest_of_line (pfile);
7007 return 1;
7008 }
7009 \f
7010 static int
7011 do_unassert (pfile, keyword, buf, limit)
7012 cpp_reader *pfile;
7013 struct directive *keyword ATTRIBUTE_UNUSED;
7014 U_CHAR *buf ATTRIBUTE_UNUSED, *limit ATTRIBUTE_UNUSED;
7015 {
7016 long symstart; /* remember where symbol name starts */
7017 int sym_length; /* and how long it is */
7018 int c;
7019
7020 struct arglist *tokens = NULL;
7021 int tokens_specified = 0;
7022
7023 if (CPP_PEDANTIC (pfile) && CPP_OPTIONS (pfile)->done_initializing
7024 && !CPP_BUFFER (pfile)->system_header_p)
7025 cpp_pedwarn (pfile, "ANSI C does not allow `#unassert'");
7026
7027 cpp_skip_hspace (pfile);
7028
7029 symstart = CPP_WRITTEN (pfile); /* remember where it starts */
7030 parse_name (pfile, GETC());
7031 sym_length = check_macro_name (pfile, pfile->token_buffer + symstart,
7032 "assertion");
7033
7034 cpp_skip_hspace (pfile);
7035 if (PEEKC() == '(') {
7036 int error_flag = 0;
7037
7038 tokens = read_token_list (pfile, &error_flag);
7039 if (error_flag)
7040 goto error;
7041 if (tokens == 0) {
7042 cpp_error (pfile, "empty token list in `#unassert'");
7043 goto error;
7044 }
7045
7046 tokens_specified = 1;
7047 }
7048
7049 cpp_skip_hspace (pfile);
7050 c = PEEKC ();
7051 if (c != EOF && c != '\n')
7052 cpp_error (pfile, "junk at end of `#unassert'");
7053 skip_rest_of_line (pfile);
7054
7055 {
7056 ASSERTION_HASHNODE *hp;
7057 U_CHAR *symname = pfile->token_buffer + symstart;
7058 int hashcode = hashf (symname, sym_length, ASSERTION_HASHSIZE);
7059 struct tokenlist_list *tail, *prev;
7060
7061 hp = assertion_lookup (pfile, symname, sym_length, hashcode);
7062 if (hp == NULL)
7063 return 1;
7064
7065 /* If no token list was specified, then eliminate this assertion
7066 entirely. */
7067 if (! tokens_specified)
7068 delete_assertion (hp);
7069 else {
7070 /* If a list of tokens was given, then delete any matching list. */
7071
7072 tail = hp->value;
7073 prev = 0;
7074 while (tail) {
7075 struct tokenlist_list *next = tail->next;
7076 if (compare_token_lists (tail->tokens, tokens)) {
7077 if (prev)
7078 prev->next = next;
7079 else
7080 hp->value = tail->next;
7081 free_token_list (tail->tokens);
7082 free (tail);
7083 } else {
7084 prev = tail;
7085 }
7086 tail = next;
7087 }
7088 }
7089 }
7090
7091 CPP_SET_WRITTEN (pfile, symstart); /* Pop */
7092 return 0;
7093 error:
7094 CPP_SET_WRITTEN (pfile, symstart); /* Pop */
7095 skip_rest_of_line (pfile);
7096 return 1;
7097 }
7098 \f
7099 /* Test whether there is an assertion named NAME
7100 and optionally whether it has an asserted token list TOKENS.
7101 NAME is not null terminated; its length is SYM_LENGTH.
7102 If TOKENS_SPECIFIED is 0, then don't check for any token list. */
7103
7104 int
7105 check_assertion (pfile, name, sym_length, tokens_specified, tokens)
7106 cpp_reader *pfile;
7107 U_CHAR *name;
7108 int sym_length;
7109 int tokens_specified;
7110 struct arglist *tokens;
7111 {
7112 ASSERTION_HASHNODE *hp;
7113 int hashcode = hashf (name, sym_length, ASSERTION_HASHSIZE);
7114
7115 if (CPP_PEDANTIC (pfile) && !CPP_BUFFER (pfile)->system_header_p)
7116 cpp_pedwarn (pfile, "ANSI C does not allow testing assertions");
7117
7118 hp = assertion_lookup (pfile, name, sym_length, hashcode);
7119 if (hp == NULL)
7120 /* It is not an assertion; just return false. */
7121 return 0;
7122
7123 /* If no token list was specified, then value is 1. */
7124 if (! tokens_specified)
7125 return 1;
7126
7127 {
7128 struct tokenlist_list *tail;
7129
7130 tail = hp->value;
7131
7132 /* If a list of tokens was given,
7133 then succeed if the assertion records a matching list. */
7134
7135 while (tail) {
7136 if (compare_token_lists (tail->tokens, tokens))
7137 return 1;
7138 tail = tail->next;
7139 }
7140
7141 /* Fail if the assertion has no matching list. */
7142 return 0;
7143 }
7144 }
7145
7146 /* Compare two lists of tokens for equality including order of tokens. */
7147
7148 static int
7149 compare_token_lists (l1, l2)
7150 struct arglist *l1, *l2;
7151 {
7152 while (l1 && l2) {
7153 if (l1->length != l2->length)
7154 return 0;
7155 if (strncmp (l1->name, l2->name, l1->length))
7156 return 0;
7157 l1 = l1->next;
7158 l2 = l2->next;
7159 }
7160
7161 /* Succeed if both lists end at the same time. */
7162 return l1 == l2;
7163 }
7164 \f
7165 struct arglist *
7166 reverse_token_list (tokens)
7167 struct arglist *tokens;
7168 {
7169 register struct arglist *prev = 0, *this, *next;
7170 for (this = tokens; this; this = next)
7171 {
7172 next = this->next;
7173 this->next = prev;
7174 prev = this;
7175 }
7176 return prev;
7177 }
7178
7179 /* Read a space-separated list of tokens ending in a close parenthesis.
7180 Return a list of strings, in the order they were written.
7181 (In case of error, return 0 and store -1 in *ERROR_FLAG.) */
7182
7183 static struct arglist *
7184 read_token_list (pfile, error_flag)
7185 cpp_reader *pfile;
7186 int *error_flag;
7187 {
7188 struct arglist *token_ptrs = 0;
7189 int depth = 1;
7190 int length;
7191
7192 *error_flag = 0;
7193 FORWARD (1); /* Skip '(' */
7194
7195 /* Loop over the assertion value tokens. */
7196 while (depth > 0)
7197 {
7198 struct arglist *temp;
7199 long name_written = CPP_WRITTEN (pfile);
7200 int c;
7201
7202 cpp_skip_hspace (pfile);
7203
7204 c = GETC ();
7205
7206 /* Find the end of the token. */
7207 if (c == '(')
7208 {
7209 CPP_PUTC (pfile, c);
7210 depth++;
7211 }
7212 else if (c == ')')
7213 {
7214 depth--;
7215 if (depth == 0)
7216 break;
7217 CPP_PUTC (pfile, c);
7218 }
7219 else if (c == '"' || c == '\'')
7220 {
7221 FORWARD(-1);
7222 cpp_get_token (pfile);
7223 }
7224 else if (c == '\n')
7225 break;
7226 else
7227 {
7228 while (c != EOF && ! is_space[c] && c != '(' && c != ')'
7229 && c != '"' && c != '\'')
7230 {
7231 CPP_PUTC (pfile, c);
7232 c = GETC();
7233 }
7234 if (c != EOF) FORWARD(-1);
7235 }
7236
7237 length = CPP_WRITTEN (pfile) - name_written;
7238 temp = (struct arglist *)
7239 xmalloc (sizeof (struct arglist) + length + 1);
7240 temp->name = (U_CHAR *) (temp + 1);
7241 bcopy ((char *) (pfile->token_buffer + name_written),
7242 (char *) temp->name, length);
7243 temp->name[length] = 0;
7244 temp->next = token_ptrs;
7245 token_ptrs = temp;
7246 temp->length = length;
7247
7248 CPP_ADJUST_WRITTEN (pfile, -length); /* pop */
7249
7250 if (c == EOF || c == '\n')
7251 { /* FIXME */
7252 cpp_error (pfile,
7253 "unterminated token sequence following `#' operator");
7254 return 0;
7255 }
7256 }
7257
7258 /* We accumulated the names in reverse order.
7259 Now reverse them to get the proper order. */
7260 return reverse_token_list (token_ptrs);
7261 }
7262
7263 static void
7264 free_token_list (tokens)
7265 struct arglist *tokens;
7266 {
7267 while (tokens) {
7268 struct arglist *next = tokens->next;
7269 free (tokens->name);
7270 free (tokens);
7271 tokens = next;
7272 }
7273 }
7274 \f
7275 /* Read LEN bytes at PTR from descriptor DESC, for file FILENAME,
7276 retrying if necessary. If MAX_READ_LEN is defined, read at most
7277 that bytes at a time. Return a negative value if an error occurs,
7278 otherwise return the actual number of bytes read,
7279 which must be LEN unless end-of-file was reached. */
7280
7281 static int
7282 safe_read (desc, ptr, len)
7283 int desc;
7284 char *ptr;
7285 int len;
7286 {
7287 int left, rcount, nchars;
7288
7289 left = len;
7290 while (left > 0) {
7291 rcount = left;
7292 #ifdef MAX_READ_LEN
7293 if (rcount > MAX_READ_LEN)
7294 rcount = MAX_READ_LEN;
7295 #endif
7296 nchars = read (desc, ptr, rcount);
7297 if (nchars < 0)
7298 {
7299 #ifdef EINTR
7300 if (errno == EINTR)
7301 continue;
7302 #endif
7303 return nchars;
7304 }
7305 if (nchars == 0)
7306 break;
7307 ptr += nchars;
7308 left -= nchars;
7309 }
7310 return len - left;
7311 }
7312
7313 static char *
7314 xcalloc (number, size)
7315 unsigned number, size;
7316 {
7317 register unsigned total = number * size;
7318 register char *ptr = (char *) xmalloc (total);
7319 bzero (ptr, total);
7320 return ptr;
7321 }
7322
7323 static char *
7324 savestring (input)
7325 char *input;
7326 {
7327 unsigned size = strlen (input);
7328 char *output = xmalloc (size + 1);
7329 strcpy (output, input);
7330 return output;
7331 }
7332 \f
7333 /* Initialize PMARK to remember the current position of PFILE. */
7334
7335 void
7336 parse_set_mark (pmark, pfile)
7337 struct parse_marker *pmark;
7338 cpp_reader *pfile;
7339 {
7340 cpp_buffer *pbuf = CPP_BUFFER (pfile);
7341 pmark->next = pbuf->marks;
7342 pbuf->marks = pmark;
7343 pmark->buf = pbuf;
7344 pmark->position = pbuf->cur - pbuf->buf;
7345 }
7346
7347 /* Cleanup PMARK - we no longer need it. */
7348
7349 void
7350 parse_clear_mark (pmark)
7351 struct parse_marker *pmark;
7352 {
7353 struct parse_marker **pp = &pmark->buf->marks;
7354 for (; ; pp = &(*pp)->next) {
7355 if (*pp == NULL) abort ();
7356 if (*pp == pmark) break;
7357 }
7358 *pp = pmark->next;
7359 }
7360
7361 /* Backup the current position of PFILE to that saved in PMARK. */
7362
7363 void
7364 parse_goto_mark (pmark, pfile)
7365 struct parse_marker *pmark;
7366 cpp_reader *pfile;
7367 {
7368 cpp_buffer *pbuf = CPP_BUFFER (pfile);
7369 if (pbuf != pmark->buf)
7370 cpp_fatal (pfile, "internal error %s", "parse_goto_mark");
7371 pbuf->cur = pbuf->buf + pmark->position;
7372 }
7373
7374 /* Reset PMARK to point to the current position of PFILE. (Same
7375 as parse_clear_mark (PMARK), parse_set_mark (PMARK, PFILE) but faster. */
7376
7377 void
7378 parse_move_mark (pmark, pfile)
7379 struct parse_marker *pmark;
7380 cpp_reader *pfile;
7381 {
7382 cpp_buffer *pbuf = CPP_BUFFER (pfile);
7383 if (pbuf != pmark->buf)
7384 cpp_fatal (pfile, "internal error %s", "parse_move_mark");
7385 pmark->position = pbuf->cur - pbuf->buf;
7386 }
7387
7388 int
7389 cpp_read_check_assertion (pfile)
7390 cpp_reader *pfile;
7391 {
7392 int name_start = CPP_WRITTEN (pfile);
7393 int name_length, name_written;
7394 int result;
7395 FORWARD (1); /* Skip '#' */
7396 cpp_skip_hspace (pfile);
7397 parse_name (pfile, GETC ());
7398 name_written = CPP_WRITTEN (pfile);
7399 name_length = name_written - name_start;
7400 cpp_skip_hspace (pfile);
7401 if (CPP_BUF_PEEK (CPP_BUFFER (pfile)) == '(')
7402 {
7403 int error_flag;
7404 struct arglist *token_ptrs = read_token_list (pfile, &error_flag);
7405 result = check_assertion (pfile,
7406 pfile->token_buffer + name_start, name_length,
7407 1, token_ptrs);
7408 }
7409 else
7410 result = check_assertion (pfile,
7411 pfile->token_buffer + name_start, name_length,
7412 0, NULL_PTR);
7413 CPP_ADJUST_WRITTEN (pfile, - name_length); /* pop */
7414 return result;
7415 }
7416 \f
7417 void
7418 cpp_print_file_and_line (pfile)
7419 cpp_reader *pfile;
7420 {
7421 cpp_buffer *ip = cpp_file_buffer (pfile);
7422
7423 if (ip != NULL)
7424 {
7425 long line, col;
7426 cpp_buf_line_and_col (ip, &line, &col);
7427 cpp_file_line_for_message (pfile, ip->nominal_fname,
7428 line, pfile->show_column ? col : -1);
7429 }
7430 }
7431
7432 static void
7433 v_cpp_error (pfile, msg, ap)
7434 cpp_reader *pfile;
7435 const char *msg;
7436 va_list ap;
7437 {
7438 cpp_print_containing_files (pfile);
7439 cpp_print_file_and_line (pfile);
7440 v_cpp_message (pfile, 1, msg, ap);
7441 }
7442
7443 void
7444 cpp_error VPROTO ((cpp_reader * pfile, const char *msg, ...))
7445 {
7446 #ifndef __STDC__
7447 cpp_reader *pfile;
7448 const char *msg;
7449 #endif
7450 va_list ap;
7451
7452 VA_START(ap, msg);
7453
7454 #ifndef __STDC__
7455 pfile = va_arg (ap, cpp_reader *);
7456 msg = va_arg (ap, const char *);
7457 #endif
7458
7459 v_cpp_error (pfile, msg, ap);
7460 va_end(ap);
7461 }
7462
7463 /* Print error message but don't count it. */
7464
7465 static void
7466 v_cpp_warning (pfile, msg, ap)
7467 cpp_reader *pfile;
7468 const char *msg;
7469 va_list ap;
7470 {
7471 if (CPP_OPTIONS (pfile)->inhibit_warnings)
7472 return;
7473
7474 if (CPP_OPTIONS (pfile)->warnings_are_errors)
7475 pfile->errors++;
7476
7477 cpp_print_containing_files (pfile);
7478 cpp_print_file_and_line (pfile);
7479 v_cpp_message (pfile, 0, msg, ap);
7480 }
7481
7482 void
7483 cpp_warning VPROTO ((cpp_reader * pfile, const char *msg, ...))
7484 {
7485 #ifndef __STDC__
7486 cpp_reader *pfile;
7487 const char *msg;
7488 #endif
7489 va_list ap;
7490
7491 VA_START (ap, msg);
7492
7493 #ifndef __STDC__
7494 pfile = va_arg (ap, cpp_reader *);
7495 msg = va_arg (ap, const char *);
7496 #endif
7497
7498 v_cpp_warning (pfile, msg, ap);
7499 va_end(ap);
7500 }
7501
7502 /* Print an error message and maybe count it. */
7503
7504 void
7505 cpp_pedwarn VPROTO ((cpp_reader * pfile, const char *msg, ...))
7506 {
7507 #ifndef __STDC__
7508 cpp_reader *pfile;
7509 const char *msg;
7510 #endif
7511 va_list ap;
7512
7513 VA_START (ap, msg);
7514
7515 #ifndef __STDC__
7516 pfile = va_arg (ap, cpp_reader *);
7517 msg = va_arg (ap, const char *);
7518 #endif
7519
7520 if (CPP_OPTIONS (pfile)->pedantic_errors)
7521 v_cpp_error (pfile, msg, ap);
7522 else
7523 v_cpp_warning (pfile, msg, ap);
7524 va_end(ap);
7525 }
7526
7527 static void
7528 v_cpp_error_with_line (pfile, line, column, msg, ap)
7529 cpp_reader * pfile;
7530 int line;
7531 int column;
7532 const char * msg;
7533 va_list ap;
7534 {
7535 cpp_buffer *ip = cpp_file_buffer (pfile);
7536
7537 cpp_print_containing_files (pfile);
7538
7539 if (ip != NULL)
7540 cpp_file_line_for_message (pfile, ip->nominal_fname, line, column);
7541
7542 v_cpp_message (pfile, 1, msg, ap);
7543 }
7544
7545 void
7546 cpp_error_with_line VPROTO ((cpp_reader * pfile, int line, int column, const char *msg, ...))
7547 {
7548 #ifndef __STDC__
7549 cpp_reader *pfile;
7550 int line;
7551 int column;
7552 const char *msg;
7553 #endif
7554 va_list ap;
7555
7556 VA_START (ap, msg);
7557
7558 #ifndef __STDC__
7559 pfile = va_arg (ap, cpp_reader *);
7560 line = va_arg (ap, int);
7561 column = va_arg (ap, int);
7562 msg = va_arg (ap, const char *);
7563 #endif
7564
7565 v_cpp_error_with_line(pfile, line, column, msg, ap);
7566 va_end(ap);
7567 }
7568
7569 static void
7570 v_cpp_warning_with_line (pfile, line, column, msg, ap)
7571 cpp_reader * pfile;
7572 int line;
7573 int column;
7574 const char *msg;
7575 va_list ap;
7576 {
7577 cpp_buffer *ip;
7578
7579 if (CPP_OPTIONS (pfile)->inhibit_warnings)
7580 return;
7581
7582 if (CPP_OPTIONS (pfile)->warnings_are_errors)
7583 pfile->errors++;
7584
7585 cpp_print_containing_files (pfile);
7586
7587 ip = cpp_file_buffer (pfile);
7588
7589 if (ip != NULL)
7590 cpp_file_line_for_message (pfile, ip->nominal_fname, line, column);
7591
7592 v_cpp_message (pfile, 0, msg, ap);
7593 }
7594
7595 #if 0
7596 static void
7597 cpp_warning_with_line VPROTO ((cpp_reader * pfile, int line, int column, const char *msg, ...))
7598 {
7599 #ifndef __STDC__
7600 cpp_reader *pfile;
7601 int line;
7602 int column;
7603 const char *msg;
7604 #endif
7605 va_list ap;
7606
7607 VA_START (ap, msg);
7608
7609 #ifndef __STDC__
7610 pfile = va_arg (ap, cpp_reader *);
7611 line = va_arg (ap, int);
7612 column = va_arg (ap, int);
7613 msg = va_arg (ap, const char *);
7614 #endif
7615
7616 v_cpp_warning_with_line (pfile, line, column, msg, ap);
7617 va_end(ap);
7618 }
7619 #endif
7620
7621 void
7622 cpp_pedwarn_with_line VPROTO ((cpp_reader * pfile, int line, int column, const char *msg, ...))
7623 {
7624 #ifndef __STDC__
7625 cpp_reader *pfile;
7626 int line;
7627 int column;
7628 const char *msg;
7629 #endif
7630 va_list ap;
7631
7632 VA_START (ap, msg);
7633
7634 #ifndef __STDC__
7635 pfile = va_arg (ap, cpp_reader *);
7636 line = va_arg (ap, int);
7637 column = va_arg (ap, int);
7638 msg = va_arg (ap, const char *);
7639 #endif
7640
7641 if (CPP_OPTIONS (pfile)->pedantic_errors)
7642 v_cpp_error_with_line (pfile, column, line, msg, ap);
7643 else
7644 v_cpp_warning_with_line (pfile, line, column, msg, ap);
7645 va_end(ap);
7646 }
7647
7648 /* Report a warning (or an error if pedantic_errors)
7649 giving specified file name and line number, not current. */
7650
7651 void
7652 cpp_pedwarn_with_file_and_line VPROTO ((cpp_reader *pfile, char *file, int line, const char *msg, ...))
7653 {
7654 #ifndef __STDC__
7655 cpp_reader *pfile;
7656 char *file;
7657 int line;
7658 const char *msg;
7659 #endif
7660 va_list ap;
7661
7662 VA_START (ap, msg);
7663
7664 #ifndef __STDC__
7665 pfile = va_arg (ap, cpp_reader *);
7666 file = va_arg (ap, char *);
7667 line = va_arg (ap, int);
7668 msg = va_arg (ap, const char *);
7669 #endif
7670
7671 if (!CPP_OPTIONS (pfile)->pedantic_errors
7672 && CPP_OPTIONS (pfile)->inhibit_warnings)
7673 return;
7674 if (file != NULL)
7675 cpp_file_line_for_message (pfile, file, line, -1);
7676 v_cpp_message (pfile, CPP_OPTIONS (pfile)->pedantic_errors, msg, ap);
7677 va_end(ap);
7678 }
7679
7680 /* my_strerror - return the descriptive text associated with an
7681 `errno' code. */
7682
7683 static char *
7684 my_strerror (errnum)
7685 int errnum;
7686 {
7687 char *result;
7688
7689 #ifndef VMS
7690 #ifndef HAVE_STRERROR
7691 result = (char *) ((errnum < sys_nerr) ? sys_errlist[errnum] : 0);
7692 #else
7693 result = strerror (errnum);
7694 #endif
7695 #else /* VMS */
7696 /* VAXCRTL's strerror() takes an optional second argument, which only
7697 matters when the first argument is EVMSERR. However, it's simplest
7698 just to pass it unconditionally. `vaxc$errno' is declared in
7699 <errno.h>, and maintained by the library in parallel with `errno'.
7700 We assume that caller's `errnum' either matches the last setting of
7701 `errno' by the library or else does not have the value `EVMSERR'. */
7702
7703 result = strerror (errnum, vaxc$errno);
7704 #endif
7705
7706 if (!result)
7707 result = "undocumented I/O error";
7708
7709 return result;
7710 }
7711
7712 /* Error including a message from `errno'. */
7713
7714 void
7715 cpp_error_from_errno (pfile, name)
7716 cpp_reader *pfile;
7717 const char *name;
7718 {
7719 cpp_message_from_errno (pfile, 1, name);
7720 }
7721
7722 void
7723 cpp_message_from_errno (pfile, is_error, name)
7724 cpp_reader *pfile;
7725 int is_error;
7726 const char *name;
7727 {
7728 int e = errno;
7729 cpp_buffer *ip = cpp_file_buffer (pfile);
7730
7731 cpp_print_containing_files (pfile);
7732
7733 if (ip != NULL)
7734 cpp_file_line_for_message (pfile, ip->nominal_fname, ip->lineno, -1);
7735
7736 cpp_message (pfile, is_error, "%s: %s", name, my_strerror (e));
7737 }
7738
7739 void
7740 cpp_perror_with_name (pfile, name)
7741 cpp_reader *pfile;
7742 const char *name;
7743 {
7744 cpp_message (pfile, 1, "%s: %s: %s", progname, name, my_strerror (errno));
7745 }
7746
7747 /* TODO:
7748 * No pre-compiled header file support.
7749 *
7750 * Possibly different enum token codes for each C/C++ token.
7751 *
7752 * Should clean up remaining directives to that do_XXX functions
7753 * only take two arguments and all have command_reads_line.
7754 *
7755 * Find and cleanup remaining uses of static variables,
7756 *
7757 * Support for trigraphs.
7758 *
7759 * Support -dM flag (dump_all_macros).
7760 *
7761 * Support for_lint flag.
7762 */