(*zeroextract[qs]i_compare0_scratch): Use const_int_operand
[official-gcc.git] / gcc / cpplib.c
blob0fdd991f48e9e5f6cb32337fe8d9afb4c6bd5e72
1 /* CPP Library.
2 Copyright (C) 1986, 87, 89, 92, 93, 94, 1995 Free Software Foundation, Inc.
3 Written by Per Bothner, 1994-95.
4 Based on CCCP program by by Paul Rubin, June 1986
5 Adapted to ANSI C, Richard Stallman, Jan 1987
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.
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.
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.
21 In other words, you are welcome to use, share and improve this program.
22 You are forbidden to forbid anyone else to use, share and improve
23 what you give them. Help stamp out software-hoarding! */
25 #ifdef EMACS
26 #define NO_SHORTNAMES
27 #include "../src/config.h"
28 #ifdef open
29 #undef open
30 #undef read
31 #undef write
32 #endif /* open */
33 #endif /* EMACS */
35 /* The macro EMACS is defined when cpp is distributed as part of Emacs,
36 for the sake of machines with limited C compilers. */
37 #ifndef EMACS
38 #include "config.h"
39 #endif /* not EMACS */
41 #ifndef STANDARD_INCLUDE_DIR
42 #define STANDARD_INCLUDE_DIR "/usr/include"
43 #endif
45 #ifndef LOCAL_INCLUDE_DIR
46 #define LOCAL_INCLUDE_DIR "/usr/local/include"
47 #endif
49 #if 0 /* We can't get ptrdiff_t, so I arranged not to need PTR_INT_TYPE. */
50 #ifdef __STDC__
51 #define PTR_INT_TYPE ptrdiff_t
52 #else
53 #define PTR_INT_TYPE long
54 #endif
55 #endif /* 0 */
57 #include "cpplib.h"
58 #include "cpphash.h"
60 #ifndef STDC_VALUE
61 #define STDC_VALUE 1
62 #endif
64 /* By default, colon separates directories in a path. */
65 #ifndef PATH_SEPARATOR
66 #define PATH_SEPARATOR ':'
67 #endif
69 #include <ctype.h>
70 #include <stdio.h>
71 #include <signal.h>
72 #ifdef __STDC__
73 #include <stdlib.h>
74 #endif
76 #ifndef VMS
77 #ifndef USG
78 #include <sys/time.h> /* for __DATE__ and __TIME__ */
79 #include <sys/resource.h>
80 #else
81 #include <sys/times.h>
82 #include <time.h>
83 #include <fcntl.h>
84 #endif /* USG */
85 #endif /* not VMS */
87 /* This defines "errno" properly for VMS, and gives us EACCES. */
88 #include <errno.h>
90 extern char *index ();
91 extern char *rindex ();
93 #ifndef O_RDONLY
94 #define O_RDONLY 0
95 #endif
97 #undef MIN
98 #undef MAX
99 #define MIN(X,Y) ((X) < (Y) ? (X) : (Y))
100 #define MAX(X,Y) ((X) > (Y) ? (X) : (Y))
102 /* Find the largest host integer type and set its size and type. */
104 #ifndef HOST_BITS_PER_WIDE_INT
106 #if HOST_BITS_PER_LONG > HOST_BITS_PER_INT
107 #define HOST_BITS_PER_WIDE_INT HOST_BITS_PER_LONG
108 #define HOST_WIDE_INT long
109 #else
110 #define HOST_BITS_PER_WIDE_INT HOST_BITS_PER_INT
111 #define HOST_WIDE_INT int
112 #endif
114 #endif
116 #ifndef S_ISREG
117 #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
118 #endif
120 #ifndef S_ISDIR
121 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
122 #endif
124 /* Define a generic NULL if one hasn't already been defined. */
126 #ifndef NULL
127 #define NULL 0
128 #endif
130 #ifndef GENERIC_PTR
131 #if defined (USE_PROTOTYPES) ? USE_PROTOTYPES : defined (__STDC__)
132 #define GENERIC_PTR void *
133 #else
134 #define GENERIC_PTR char *
135 #endif
136 #endif
138 #ifndef NULL_PTR
139 #define NULL_PTR ((GENERIC_PTR)0)
140 #endif
142 #ifndef INCLUDE_LEN_FUDGE
143 #define INCLUDE_LEN_FUDGE 0
144 #endif
146 /* Symbols to predefine. */
148 #ifdef CPP_PREDEFINES
149 static char *predefs = CPP_PREDEFINES;
150 #else
151 static char *predefs = "";
152 #endif
154 /* We let tm.h override the types used here, to handle trivial differences
155 such as the choice of unsigned int or long unsigned int for size_t.
156 When machines start needing nontrivial differences in the size type,
157 it would be best to do something here to figure out automatically
158 from other information what type to use. */
160 /* The string value for __SIZE_TYPE__. */
162 #ifndef SIZE_TYPE
163 #define SIZE_TYPE "long unsigned int"
164 #endif
166 /* The string value for __PTRDIFF_TYPE__. */
168 #ifndef PTRDIFF_TYPE
169 #define PTRDIFF_TYPE "long int"
170 #endif
172 /* The string value for __WCHAR_TYPE__. */
174 #ifndef WCHAR_TYPE
175 #define WCHAR_TYPE "int"
176 #endif
177 #define CPP_WCHAR_TYPE(PFILE) \
178 (CPP_OPTIONS (PFILE)->cplusplus ? "__wchar_t" : WCHAR_TYPE)
180 /* The string value for __USER_LABEL_PREFIX__ */
182 #ifndef USER_LABEL_PREFIX
183 #define USER_LABEL_PREFIX ""
184 #endif
186 /* The string value for __REGISTER_PREFIX__ */
188 #ifndef REGISTER_PREFIX
189 #define REGISTER_PREFIX ""
190 #endif
192 /* In the definition of a #assert name, this structure forms
193 a list of the individual values asserted.
194 Each value is itself a list of "tokens".
195 These are strings that are compared by name. */
197 struct tokenlist_list {
198 struct tokenlist_list *next;
199 struct arglist *tokens;
202 struct assertion_hashnode {
203 struct assertion_hashnode *next; /* double links for easy deletion */
204 struct assertion_hashnode *prev;
205 /* also, a back pointer to this node's hash
206 chain is kept, in case the node is the head
207 of the chain and gets deleted. */
208 struct assertion_hashnode **bucket_hdr;
209 int length; /* length of token, for quick comparison */
210 U_CHAR *name; /* the actual name */
211 /* List of token-sequences. */
212 struct tokenlist_list *value;
215 #define SKIP_WHITE_SPACE(p) do { while (is_hor_space[*p]) p++; } while (0)
216 #define SKIP_ALL_WHITE_SPACE(p) do { while (is_space[*p]) p++; } while (0)
218 #define PEEKN(N) (CPP_BUFFER (pfile)->rlimit - CPP_BUFFER (pfile)->cur >= (N) ? CPP_BUFFER (pfile)->cur[N] : EOF)
219 #define FORWARD(N) CPP_FORWARD (CPP_BUFFER (pfile), (N))
220 #define GETC() CPP_BUF_GET (CPP_BUFFER (pfile))
221 #define PEEKC() CPP_BUF_PEEK (CPP_BUFFER (pfile))
222 /* CPP_IS_MACRO_BUFFER is true if the buffer contains macro expansion.
223 (Note that it is false while we're expanding marco *arguments*.) */
224 #define CPP_IS_MACRO_BUFFER(PBUF) ((PBUF)->cleanup == macro_cleanup)
226 /* Move all backslash-newline pairs out of embarrassing places.
227 Exchange all such pairs following BP
228 with any potentially-embarrassing characters that follow them.
229 Potentially-embarrassing characters are / and *
230 (because a backslash-newline inside a comment delimiter
231 would cause it not to be recognized). */
233 #define NEWLINE_FIX \
234 do {while (PEEKC() == '\\' && PEEKN(1) == '\n') FORWARD(2); } while(0)
236 /* Same, but assume we've already read the potential '\\' into C. */
237 #define NEWLINE_FIX1(C) do { \
238 while ((C) == '\\' && PEEKC() == '\n') { FORWARD(1); (C) = GETC(); }\
239 } while(0)
241 struct cpp_pending {
242 struct cpp_pending *next;
243 char *cmd;
244 char *arg;
247 /* Forward declarations. */
249 extern char *xmalloc ();
251 static void add_import ();
252 static void append_include_chain ();
253 static void make_undef ();
254 static void make_assertion ();
255 static void path_include ();
256 static void initialize_builtins ();
257 static void initialize_char_syntax ();
258 static void dump_arg_n ();
259 static void dump_defn_1 ();
260 extern void delete_macro ();
261 static void trigraph_pcp ();
262 static int finclude ();
263 static void validate_else ();
264 static int comp_def_part ();
265 extern void fancy_abort ();
266 static void pipe_closed ();
267 static void print_containing_files ();
268 static int lookup_import ();
269 static int redundant_include_p ();
270 static is_system_include ();
271 static struct file_name_map *read_name_map ();
272 static char *read_filename_string ();
273 static int open_include_file ();
274 static int check_preconditions ();
275 static void pcfinclude ();
276 static void pcstring_used ();
277 static int check_macro_name ();
278 static int compare_defs ();
279 static int compare_token_lists ();
280 static HOST_WIDE_INT eval_if_expression ();
281 static int change_newlines ();
282 extern int hashf ();
283 static int file_size_and_mode ();
284 static struct arglist *read_token_list ();
285 static void free_token_list ();
286 static int safe_read ();
287 static void push_macro_expansion PARAMS ((cpp_reader *,
288 U_CHAR*, int, HASHNODE*));
289 static struct cpp_pending *nreverse_pending PARAMS ((struct cpp_pending*));
290 extern char *xrealloc ();
291 extern char *xcalloc ();
292 static char *savestring ();
294 static void conditional_skip ();
295 static void skip_if_group ();
297 /* Last arg to output_line_command. */
298 enum file_change_code {same_file, enter_file, leave_file};
300 /* External declarations. */
302 extern HOST_WIDE_INT cpp_parse_expr PARAMS ((cpp_reader*));
304 extern char *getenv ();
305 extern FILE *fdopen ();
306 extern char *version_string;
307 extern struct tm *localtime ();
309 /* These functions are declared to return int instead of void since they
310 are going to be placed in a table and some old compilers have trouble with
311 pointers to functions returning void. */
313 static int do_define ();
314 static int do_line ();
315 static int do_include ();
316 static int do_undef ();
317 static int do_error ();
318 static int do_pragma ();
319 static int do_ident ();
320 static int do_if ();
321 static int do_xifdef ();
322 static int do_else ();
323 static int do_elif ();
324 static int do_endif ();
325 static int do_sccs ();
326 static int do_once ();
327 static int do_assert ();
328 static int do_unassert ();
329 static int do_warning ();
331 struct file_name_list
333 struct file_name_list *next;
334 char *fname;
335 /* If the following is nonzero, it is a macro name.
336 Don't include the file again if that macro is defined. */
337 U_CHAR *control_macro;
338 /* If the following is nonzero, it is a C-language system include
339 directory. */
340 int c_system_include_path;
341 /* Mapping of file names for this directory. */
342 struct file_name_map *name_map;
343 /* Non-zero if name_map is valid. */
344 int got_name_map;
347 /* If a buffer's dir field is SELF_DIR_DUMMY, it means the file was found
348 via the same directory as the file that #included it. */
349 #define SELF_DIR_DUMMY ((struct file_name_list*)(~0))
351 /* #include "file" looks in source file dir, then stack. */
352 /* #include <file> just looks in the stack. */
353 /* -I directories are added to the end, then the defaults are added. */
354 /* The */
355 static struct default_include {
356 char *fname; /* The name of the directory. */
357 int cplusplus; /* Only look here if we're compiling C++. */
358 int cxx_aware; /* Includes in this directory don't need to
359 be wrapped in extern "C" when compiling
360 C++. */
361 } include_defaults_array[]
362 #ifdef INCLUDE_DEFAULTS
363 = INCLUDE_DEFAULTS;
364 #else
366 /* Pick up GNU C++ specific include files. */
367 { GPLUSPLUS_INCLUDE_DIR, 1, 1 },
368 #ifdef CROSS_COMPILE
369 /* This is the dir for fixincludes. Put it just before
370 the files that we fix. */
371 { GCC_INCLUDE_DIR, 0, 0 },
372 /* For cross-compilation, this dir name is generated
373 automatically in Makefile.in. */
374 { CROSS_INCLUDE_DIR, 0, 0 },
375 /* This is another place that the target system's headers might be. */
376 { TOOL_INCLUDE_DIR, 0, 1 },
377 #else /* not CROSS_COMPILE */
378 /* This should be /usr/local/include and should come before
379 the fixincludes-fixed header files. */
380 { LOCAL_INCLUDE_DIR, 0, 1 },
381 /* This is here ahead of GCC_INCLUDE_DIR because assert.h goes here.
382 Likewise, behind LOCAL_INCLUDE_DIR, where glibc puts its assert.h. */
383 { TOOL_INCLUDE_DIR, 0, 1 },
384 /* This is the dir for fixincludes. Put it just before
385 the files that we fix. */
386 { GCC_INCLUDE_DIR, 0, 0 },
387 /* Some systems have an extra dir of include files. */
388 #ifdef SYSTEM_INCLUDE_DIR
389 { SYSTEM_INCLUDE_DIR, 0, 0 },
390 #endif
391 { STANDARD_INCLUDE_DIR, 0, 0 },
392 #endif /* not CROSS_COMPILE */
393 { 0, 0, 0 }
395 #endif /* no INCLUDE_DEFAULTS */
397 /* `struct directive' defines one #-directive, including how to handle it. */
399 struct directive {
400 int length; /* Length of name */
401 int (*func)(); /* Function to handle directive */
402 char *name; /* Name of directive */
403 enum node_type type; /* Code which describes which directive. */
404 char command_reads_line; /* One if rest of line is read by func. */
405 char traditional_comments; /* Nonzero: keep comments if -traditional. */
406 char pass_thru; /* Copy preprocessed directive to output file.*/
409 /* Here is the actual list of #-directives, most-often-used first.
410 The initialize_builtins function assumes #define is the very first. */
412 static struct directive directive_table[] = {
413 { 6, do_define, "define", T_DEFINE, 0, 1},
414 { 5, do_xifdef, "ifdef", T_IFDEF, 1},
415 { 6, do_xifdef, "ifndef", T_IFNDEF, 1},
416 { 7, do_include, "include", T_INCLUDE, 1},
417 { 12, do_include, "include_next", T_INCLUDE_NEXT, 1},
418 { 6, do_include, "import", T_IMPORT, 1},
419 { 5, do_endif, "endif", T_ENDIF, 1},
420 { 4, do_else, "else", T_ELSE, 1},
421 { 2, do_if, "if", T_IF, 1},
422 { 4, do_elif, "elif", T_ELIF, 1},
423 { 5, do_undef, "undef", T_UNDEF},
424 { 5, do_error, "error", T_ERROR},
425 { 7, do_warning, "warning", T_WARNING},
426 { 6, do_pragma, "pragma", T_PRAGMA, 0, 0, 1},
427 { 4, do_line, "line", T_LINE, 1},
428 { 5, do_ident, "ident", T_IDENT, 1, 0, 1},
429 #ifdef SCCS_DIRECTIVE
430 { 4, do_sccs, "sccs", T_SCCS},
431 #endif
432 { 6, do_assert, "assert", T_ASSERT, 1},
433 { 8, do_unassert, "unassert", T_UNASSERT, 1},
434 { -1, 0, "", T_UNUSED},
437 /* table to tell if char can be part of a C identifier. */
438 U_CHAR is_idchar[256];
439 /* table to tell if char can be first char of a c identifier. */
440 U_CHAR is_idstart[256];
441 /* table to tell if c is horizontal space. */
442 U_CHAR is_hor_space[256];
443 /* table to tell if c is horizontal or vertical space. */
444 static U_CHAR is_space[256];
446 /* Initialize syntactic classifications of characters. */
448 static void
449 initialize_char_syntax (opts)
450 struct cpp_options *opts;
452 register int i;
455 * Set up is_idchar and is_idstart tables. These should be
456 * faster than saying (is_alpha (c) || c == '_'), etc.
457 * Set up these things before calling any routines tthat
458 * refer to them.
460 for (i = 'a'; i <= 'z'; i++) {
461 is_idchar[i - 'a' + 'A'] = 1;
462 is_idchar[i] = 1;
463 is_idstart[i - 'a' + 'A'] = 1;
464 is_idstart[i] = 1;
466 for (i = '0'; i <= '9'; i++)
467 is_idchar[i] = 1;
468 is_idchar['_'] = 1;
469 is_idstart['_'] = 1;
470 is_idchar['$'] = opts->dollars_in_ident;
471 is_idstart['$'] = opts->dollars_in_ident;
473 /* horizontal space table */
474 is_hor_space[' '] = 1;
475 is_hor_space['\t'] = 1;
476 is_hor_space['\v'] = 1;
477 is_hor_space['\f'] = 1;
478 is_hor_space['\r'] = 1;
480 is_space[' '] = 1;
481 is_space['\t'] = 1;
482 is_space['\v'] = 1;
483 is_space['\f'] = 1;
484 is_space['\n'] = 1;
485 is_space['\r'] = 1;
489 /* Place into PFILE a quoted string representing the string SRC.
490 Caller must reserve enough space in pfile->token_buffer. */
491 static void
492 quote_string (pfile, src)
493 cpp_reader *pfile;
494 char *src;
496 U_CHAR c;
498 CPP_PUTC_Q (pfile, '\"');
499 for (;;)
500 switch ((c = *src++))
502 default:
503 if (isprint (c))
504 CPP_PUTC_Q (pfile, c);
505 else
507 sprintf (CPP_PWRITTEN (pfile), "\\%03o", c);
508 CPP_ADJUST_WRITTEN (pfile, 4);
510 break;
512 case '\"':
513 case '\\':
514 CPP_PUTC_Q (pfile, '\\');
515 CPP_PUTC_Q (pfile, c);
516 break;
518 case '\0':
519 CPP_PUTC_Q (pfile, '\"');
520 CPP_NUL_TERMINATE_Q (pfile);
521 return;
525 /* Make sure PFILE->token_buffer will hold at least N more chars. */
527 void
528 cpp_grow_buffer (pfile, n)
529 cpp_reader *pfile;
530 long n;
532 long old_written = CPP_WRITTEN (pfile);
533 pfile->token_buffer_size = n + 2 * pfile->token_buffer_size;
534 pfile->token_buffer = (U_CHAR*)
535 xrealloc(pfile->token_buffer, pfile->token_buffer_size);
536 CPP_SET_WRITTEN (pfile, old_written);
541 * process a given definition string, for initialization
542 * If STR is just an identifier, define it with value 1.
543 * If STR has anything after the identifier, then it should
544 * be identifier=definition.
547 void
548 cpp_define (pfile, str)
549 cpp_reader *pfile;
550 U_CHAR *str;
552 U_CHAR *buf, *p;
554 buf = str;
555 p = str;
556 if (!is_idstart[*p])
558 cpp_error (pfile, "malformed option `-D %s'", str);
559 return;
561 while (is_idchar[*++p])
563 if (*p == 0)
565 buf = (U_CHAR *) alloca (p - buf + 4);
566 strcpy ((char *)buf, str);
567 strcat ((char *)buf, " 1");
569 else if (*p != '=')
571 cpp_error (pfile, "malformed option `-D %s'", str);
572 return;
574 else
576 U_CHAR *q;
577 /* Copy the entire option so we can modify it. */
578 buf = (U_CHAR *) alloca (2 * strlen (str) + 1);
579 strncpy (buf, str, p - str);
580 /* Change the = to a space. */
581 buf[p - str] = ' ';
582 /* Scan for any backslash-newline and remove it. */
583 p++;
584 q = &buf[p - str];
585 while (*p)
587 if (*p == '\\' && p[1] == '\n')
588 p += 2;
589 else
590 *q++ = *p++;
592 *q = 0;
595 do_define (pfile, NULL, buf, buf + strlen (buf));
598 /* Process the string STR as if it appeared as the body of a #assert.
599 OPTION is the option name for which STR was the argument. */
601 static void
602 make_assertion (pfile, option, str)
603 cpp_reader *pfile;
604 char *option;
605 U_CHAR *str;
607 cpp_buffer *ip;
608 struct directive *kt;
609 U_CHAR *buf, *p, *q;
611 /* Copy the entire option so we can modify it. */
612 buf = (U_CHAR *) alloca (strlen (str) + 1);
613 strcpy ((char *) buf, str);
614 /* Scan for any backslash-newline and remove it. */
615 p = q = buf;
616 while (*p) {
617 #if 0
618 if (*p == '\\' && p[1] == '\n')
619 p += 2;
620 else
621 #endif
622 *q++ = *p++;
624 *q = 0;
626 p = buf;
627 if (!is_idstart[*p]) {
628 cpp_error (pfile, "malformed option `%s %s'", option, str);
629 return;
631 while (is_idchar[*++p])
633 while (*p == ' ' || *p == '\t') p++;
634 if (! (*p == 0 || *p == '(')) {
635 cpp_error (pfile, "malformed option `%s %s'", option, str);
636 return;
639 ip = cpp_push_buffer (pfile, buf, strlen (buf));
640 do_assert (pfile, NULL, NULL, NULL);
641 cpp_pop_buffer (pfile);
644 /* Append a chain of `struct file_name_list's
645 to the end of the main include chain.
646 FIRST is the beginning of the chain to append, and LAST is the end. */
648 static void
649 append_include_chain (pfile, first, last)
650 cpp_reader *pfile;
651 struct file_name_list *first, *last;
653 struct cpp_options *opts = CPP_OPTIONS (pfile);
654 struct file_name_list *dir;
656 if (!first || !last)
657 return;
659 if (opts->include == 0)
660 opts->include = first;
661 else
662 opts->last_include->next = first;
664 if (opts->first_bracket_include == 0)
665 opts->first_bracket_include = first;
667 for (dir = first; ; dir = dir->next) {
668 int len = strlen (dir->fname) + INCLUDE_LEN_FUDGE;
669 if (len > pfile->max_include_len)
670 pfile->max_include_len = len;
671 if (dir == last)
672 break;
675 last->next = NULL;
676 opts->last_include = last;
679 /* Add output to `deps_buffer' for the -M switch.
680 STRING points to the text to be output.
681 SPACER is ':' for targets, ' ' for dependencies, zero for text
682 to be inserted literally. */
684 static void
685 deps_output (pfile, string, spacer)
686 cpp_reader *pfile;
687 char *string;
688 int spacer;
690 int size = strlen (string);
692 if (size == 0)
693 return;
695 #ifndef MAX_OUTPUT_COLUMNS
696 #define MAX_OUTPUT_COLUMNS 72
697 #endif
698 if (spacer
699 && pfile->deps_column > 0
700 && (pfile->deps_column + size) > MAX_OUTPUT_COLUMNS)
702 deps_output (pfile, " \\\n ", 0);
703 pfile->deps_column = 0;
706 if (pfile->deps_size + size + 8 > pfile->deps_allocated_size)
708 pfile->deps_allocated_size = (pfile->deps_size + size + 50) * 2;
709 pfile->deps_buffer = (char *) xrealloc (pfile->deps_buffer,
710 pfile->deps_allocated_size);
712 if (spacer == ' ' && pfile->deps_column > 0)
713 pfile->deps_buffer[pfile->deps_size++] = ' ';
714 bcopy (string, &pfile->deps_buffer[pfile->deps_size], size);
715 pfile->deps_size += size;
716 pfile->deps_column += size;
717 if (spacer == ':')
718 pfile->deps_buffer[pfile->deps_size++] = ':';
719 pfile->deps_buffer[pfile->deps_size] = 0;
722 /* Given a colon-separated list of file names PATH,
723 add all the names to the search path for include files. */
725 static void
726 path_include (pfile, path)
727 cpp_reader *pfile;
728 char *path;
730 char *p;
732 p = path;
734 if (*p)
735 while (1) {
736 char *q = p;
737 char *name;
738 struct file_name_list *dirtmp;
740 /* Find the end of this name. */
741 while (*q != 0 && *q != PATH_SEPARATOR) q++;
742 if (p == q) {
743 /* An empty name in the path stands for the current directory. */
744 name = (char *) xmalloc (2);
745 name[0] = '.';
746 name[1] = 0;
747 } else {
748 /* Otherwise use the directory that is named. */
749 name = (char *) xmalloc (q - p + 1);
750 bcopy (p, name, q - p);
751 name[q - p] = 0;
754 dirtmp = (struct file_name_list *)
755 xmalloc (sizeof (struct file_name_list));
756 dirtmp->next = 0; /* New one goes on the end */
757 dirtmp->control_macro = 0;
758 dirtmp->c_system_include_path = 0;
759 dirtmp->fname = name;
760 dirtmp->got_name_map = 0;
761 append_include_chain (pfile, dirtmp, dirtmp);
763 /* Advance past this name. */
764 p = q;
765 if (*p == 0)
766 break;
767 /* Skip the colon. */
768 p++;
772 void
773 init_parse_options (opts)
774 struct cpp_options *opts;
776 bzero ((char *) opts, sizeof *opts);
777 opts->in_fname = NULL;
778 opts->out_fname = NULL;
780 /* Initialize is_idchar to allow $. */
781 opts->dollars_in_ident = 1;
782 initialize_char_syntax (opts);
783 opts->dollars_in_ident = DOLLARS_IN_IDENTIFIERS > 0;
785 opts->no_line_commands = 0;
786 opts->no_trigraphs = 1;
787 opts->put_out_comments = 0;
788 opts->print_include_names = 0;
789 opts->dump_macros = dump_none;
790 opts->no_output = 0;
791 opts->cplusplus = 0;
792 opts->cplusplus_comments = 0;
794 opts->verbose = 0;
795 opts->objc = 0;
796 opts->lang_asm = 0;
797 opts->for_lint = 0;
798 opts->chill = 0;
799 opts->pedantic_errors = 0;
800 opts->inhibit_warnings = 0;
801 opts->warn_comments = 0;
802 opts->warn_import = 1;
803 opts->warnings_are_errors = 0;
806 enum cpp_token
807 null_underflow (pfile)
808 cpp_reader *pfile;
810 return CPP_EOF;
814 null_cleanup (pbuf, pfile)
815 cpp_buffer *pbuf;
816 cpp_reader *pfile;
818 return 0;
822 macro_cleanup (pbuf, pfile)
823 cpp_buffer *pbuf;
824 cpp_reader *pfile;
826 HASHNODE *macro = (HASHNODE*)pbuf->data;
827 if (macro->type == T_DISABLED)
828 macro->type = T_MACRO;
829 if (macro->type != T_MACRO || pbuf->buf != macro->value.defn->expansion)
830 free (pbuf->buf);
831 return 0;
835 file_cleanup (pbuf, pfile)
836 cpp_buffer *pbuf;
837 cpp_reader *pfile;
839 if (pbuf->buf)
841 free (pbuf->buf);
842 pbuf->buf = 0;
844 return 0;
847 static void
848 newline_fix (pfile)
849 cpp_reader *pfile;
851 #if 1
852 NEWLINE_FIX;
853 #else
854 register U_CHAR *p = bp;
856 /* First count the backslash-newline pairs here. */
858 while (p[0] == '\\' && p[1] == '\n')
859 p += 2;
861 /* What follows the backslash-newlines is not embarrassing. */
863 if (*p != '/' && *p != '*')
864 return;
866 /* Copy all potentially embarrassing characters
867 that follow the backslash-newline pairs
868 down to where the pairs originally started. */
870 while (*p == '*' || *p == '/')
871 *bp++ = *p++;
873 /* Now write the same number of pairs after the embarrassing chars. */
874 while (bp < p) {
875 *bp++ = '\\';
876 *bp++ = '\n';
878 #endif
881 /* Assuming we have read '/'.
882 If this is the start of a comment (followed by '*' or '/'),
883 skip to the end of the comment, and return ' '.
884 Return EOF if we reached the end of file before the end of the comment.
885 If not the start of a comment, return '/'. */
887 static int
888 skip_comment (pfile, linep)
889 cpp_reader *pfile;
890 long *linep;
892 int c;
893 while (PEEKC() == '\\' && PEEKN(1) == '\n')
895 if (linep)
896 (*linep)++;
897 FORWARD(2);
899 if (PEEKC() == '*')
901 FORWARD(1);
902 for (;;)
904 int prev_c = c;
905 c = GETC ();
906 if (c == EOF)
907 return EOF;
908 while (c == '\\' && PEEKC() == '\n')
910 if (linep)
911 (*linep)++;
912 FORWARD(1), c = GETC();
914 if (prev_c == '*' && c == '/')
915 return ' ';
916 if (c == '\n' && linep)
917 (*linep)++;
920 else if (PEEKC() == '/' && CPP_OPTIONS (pfile)->cplusplus_comments)
922 FORWARD(1);
923 for (;;)
925 c = GETC ();
926 if (c == EOF)
927 return ' '; /* Allow // to be terminated by EOF. */
928 while (c == '\\' && PEEKC() == '\n')
930 FORWARD(1);
931 c = GETC();
932 if (linep)
933 (*linep)++;
935 if (c == '\n')
937 /* Don't consider final '\n' to be part of comment. */
938 FORWARD(-1);
939 return ' ';
943 else
944 return '/';
947 /* Skip whitespace \-newline and comments. Does not macro-expand. */
948 void
949 cpp_skip_hspace (pfile)
950 cpp_reader *pfile;
952 while (1)
954 int c = PEEKC();
955 if (c == EOF)
956 return; /* FIXME */
957 if (is_hor_space[c])
959 if ((c == '\f' || c == '\v') && CPP_PEDANTIC (pfile))
960 cpp_pedwarn (pfile, "%s in preprocessing directive",
961 c == '\f' ? "formfeed" : "vertical tab");
962 FORWARD(1);
964 else if (c == '/')
966 FORWARD (1);
967 c = skip_comment (pfile, NULL);
968 if (c == '/')
969 FORWARD(-1);
970 if (c == EOF || c == '/')
971 return;
973 else if (c == '\\' && PEEKN(1) == '\n') {
974 FORWARD(2);
976 else if (c == '@' && CPP_BUFFER (pfile)->has_escapes
977 && is_hor_space[PEEKN(1)])
978 FORWARD(2);
979 else return;
983 /* Read the rest of the current line.
984 The line is appended to PFILE's output buffer. */
986 void
987 copy_rest_of_line (pfile)
988 cpp_reader *pfile;
990 struct cpp_options *opts = CPP_OPTIONS (pfile);
991 for (;;)
993 int c = GETC();
994 int nextc;
995 switch (c)
997 case EOF:
998 goto end_directive;
999 case '\\':
1000 if (PEEKC() == '\n')
1002 FORWARD (1);
1003 continue;
1005 case '\'':
1006 case '\"':
1007 goto scan_directive_token;
1008 break;
1009 case '/':
1010 nextc = PEEKC();
1011 if (nextc == '*' || (opts->cplusplus_comments && nextc == '*'))
1012 goto scan_directive_token;
1013 break;
1014 case '\f':
1015 case '\v':
1016 if (CPP_PEDANTIC (pfile))
1017 cpp_pedwarn (pfile, "%s in preprocessing directive",
1018 c == '\f' ? "formfeed" : "vertical tab");
1019 break;
1021 case '\n':
1022 FORWARD(-1);
1023 goto end_directive;
1024 scan_directive_token:
1025 FORWARD(-1);
1026 cpp_get_token (pfile);
1027 continue;
1029 CPP_PUTC (pfile, c);
1031 end_directive: ;
1032 CPP_NUL_TERMINATE (pfile);
1035 void
1036 skip_rest_of_line (pfile)
1037 cpp_reader *pfile;
1039 long old = CPP_WRITTEN (pfile);
1040 copy_rest_of_line (pfile);
1041 CPP_SET_WRITTEN (pfile, old);
1044 /* Handle a possible # directive.
1045 '#' has already been read. */
1048 handle_directive (pfile)
1049 cpp_reader *pfile;
1050 { int c;
1051 register struct directive *kt;
1052 int ident_length;
1053 long after_ident;
1054 U_CHAR *ident, *line_end;
1055 long old_written = CPP_WRITTEN (pfile);
1057 cpp_skip_hspace (pfile);
1059 c = PEEKC ();
1060 if (c >= '0' && c <= '9')
1062 /* Handle # followed by a line number. */
1063 if (CPP_PEDANTIC (pfile))
1064 cpp_pedwarn (pfile, "`#' followed by integer");
1065 do_line (pfile, NULL);
1066 goto done_a_directive;
1069 /* Now find the directive name. */
1070 CPP_PUTC (pfile, '#');
1071 parse_name (pfile, GETC());
1072 ident = pfile->token_buffer + old_written + 1;
1073 ident_length = CPP_PWRITTEN (pfile) - ident;
1074 if (ident_length == 0 && PEEKC() == '\n')
1076 /* A line of just `#' becomes blank. */
1077 goto done_a_directive;
1080 #if 0
1081 if (ident_length == 0 || !is_idstart[*ident]) {
1082 U_CHAR *p = ident;
1083 while (is_idchar[*p]) {
1084 if (*p < '0' || *p > '9')
1085 break;
1086 p++;
1088 /* Avoid error for `###' and similar cases unless -pedantic. */
1089 if (p == ident) {
1090 while (*p == '#' || is_hor_space[*p]) p++;
1091 if (*p == '\n') {
1092 if (pedantic && !lang_asm)
1093 cpp_warning (pfile, "invalid preprocessor directive");
1094 return 0;
1098 if (!lang_asm)
1099 cpp_error (pfile, "invalid preprocessor directive name");
1101 return 0;
1103 #endif
1105 * Decode the keyword and call the appropriate expansion
1106 * routine, after moving the input pointer up to the next line.
1108 for (kt = directive_table; ; kt++) {
1109 if (kt->length <= 0)
1110 goto not_a_directive;
1111 if (kt->length == ident_length && !strncmp (kt->name, ident, ident_length))
1112 break;
1115 if (! kt->command_reads_line)
1117 /* Nonzero means do not delete comments within the directive.
1118 #define needs this when -traditional. */
1119 int comments = CPP_TRADITIONAL (pfile) && kt->traditional_comments;
1120 int save_put_out_comments = CPP_OPTIONS (pfile)->put_out_comments;
1121 CPP_OPTIONS (pfile)->put_out_comments = comments;
1122 after_ident = CPP_WRITTEN (pfile);
1123 copy_rest_of_line (pfile);
1124 CPP_OPTIONS (pfile)->put_out_comments = save_put_out_comments;
1127 /* For #pragma and #define, we may want to pass through the directive.
1128 Other directives may create output, but we don't want the directive
1129 itself out, so we pop it now. For example #include may write a #line
1130 command (see comment in do_include), and conditionals may emit
1131 #failed ... #endfailed stuff. But note that popping the buffer
1132 means the parameters to kt->func may point after pfile->limit
1133 so these parameters are invalid as soon as something gets appended
1134 to the token_buffer. */
1136 line_end = CPP_PWRITTEN (pfile);
1137 if (!kt->pass_thru && kt->type != T_DEFINE)
1138 CPP_SET_WRITTEN (pfile, old_written);
1140 (*kt->func) (pfile, kt, pfile->token_buffer + after_ident, line_end);
1141 if (kt->pass_thru
1142 || (kt->type == T_DEFINE
1143 && CPP_OPTIONS (pfile)->dump_macros == dump_definitions))
1145 /* Just leave the entire #define in the output stack. */
1147 else if (kt->type == T_DEFINE
1148 && CPP_OPTIONS (pfile)->dump_macros == dump_names)
1150 U_CHAR *p = pfile->token_buffer + old_written + 7; /* Skip "#define". */
1151 SKIP_WHITE_SPACE (p);
1152 while (is_idchar[*p]) p++;
1153 pfile->limit = p;
1154 CPP_PUTC (pfile, '\n');
1156 else if (kt->type == T_DEFINE)
1157 CPP_SET_WRITTEN (pfile, old_written);
1158 done_a_directive:
1159 return 1;
1161 not_a_directive:
1162 return 0;
1165 /* Pass a directive through to the output file.
1166 BUF points to the contents of the directive, as a contiguous string.
1167 LIMIT points to the first character past the end of the directive.
1168 KEYWORD is the keyword-table entry for the directive. */
1170 static void
1171 pass_thru_directive (buf, limit, pfile, keyword)
1172 U_CHAR *buf, *limit;
1173 cpp_reader *pfile;
1174 struct directive *keyword;
1176 register unsigned keyword_length = keyword->length;
1178 CPP_RESERVE (pfile, 1 + keyword_length + (limit - buf));
1179 CPP_PUTC_Q (pfile, '#');
1180 CPP_PUTS_Q (pfile, keyword->name, keyword_length);
1181 if (limit != buf && buf[0] != ' ')
1182 CPP_PUTC_Q (pfile, ' ');
1183 CPP_PUTS_Q (pfile, buf, limit - buf);
1184 #if 0
1185 CPP_PUTS_Q (pfile, '\n');
1186 /* Count the line we have just made in the output,
1187 to get in sync properly. */
1188 pfile->lineno++;
1189 #endif
1192 /* The arglist structure is built by do_define to tell
1193 collect_definition where the argument names begin. That
1194 is, for a define like "#define f(x,y,z) foo+x-bar*y", the arglist
1195 would contain pointers to the strings x, y, and z.
1196 Collect_definition would then build a DEFINITION node,
1197 with reflist nodes pointing to the places x, y, and z had
1198 appeared. So the arglist is just convenience data passed
1199 between these two routines. It is not kept around after
1200 the current #define has been processed and entered into the
1201 hash table. */
1203 struct arglist {
1204 struct arglist *next;
1205 U_CHAR *name;
1206 int length;
1207 int argno;
1208 char rest_args;
1211 /* Read a replacement list for a macro with parameters.
1212 Build the DEFINITION structure.
1213 Reads characters of text starting at BUF until END.
1214 ARGLIST specifies the formal parameters to look for
1215 in the text of the definition; NARGS is the number of args
1216 in that list, or -1 for a macro name that wants no argument list.
1217 MACRONAME is the macro name itself (so we can avoid recursive expansion)
1218 and NAMELEN is its length in characters.
1220 Note that comments, backslash-newlines, and leading white space
1221 have already been deleted from the argument. */
1223 static DEFINITION *
1224 collect_expansion (pfile, buf, limit, nargs, arglist)
1225 cpp_reader *pfile;
1226 U_CHAR *buf, *limit;
1227 int nargs;
1228 struct arglist *arglist;
1230 DEFINITION *defn;
1231 register U_CHAR *p, *lastp, *exp_p;
1232 struct reflist *endpat = NULL;
1233 /* Pointer to first nonspace after last ## seen. */
1234 U_CHAR *concat = 0;
1235 /* Pointer to first nonspace after last single-# seen. */
1236 U_CHAR *stringify = 0;
1237 int maxsize;
1238 int expected_delimiter = '\0';
1240 /* Scan thru the replacement list, ignoring comments and quoted
1241 strings, picking up on the macro calls. It does a linear search
1242 thru the arg list on every potential symbol. Profiling might say
1243 that something smarter should happen. */
1245 if (limit < buf)
1246 abort ();
1248 /* Find the beginning of the trailing whitespace. */
1249 p = buf;
1250 while (p < limit && is_space[limit[-1]]) limit--;
1252 /* Allocate space for the text in the macro definition.
1253 Leading and trailing whitespace chars need 2 bytes each.
1254 Each other input char may or may not need 1 byte,
1255 so this is an upper bound. The extra 5 are for invented
1256 leading and trailing newline-marker and final null. */
1257 maxsize = (sizeof (DEFINITION)
1258 + (limit - p) + 5);
1259 /* Occurrences of '@' get doubled, so allocate extra space for them. */
1260 while (p < limit)
1261 if (*p++ == '@')
1262 maxsize++;
1263 defn = (DEFINITION *) xcalloc (1, maxsize);
1265 defn->nargs = nargs;
1266 exp_p = defn->expansion = (U_CHAR *) defn + sizeof (DEFINITION);
1267 lastp = exp_p;
1269 p = buf;
1271 /* Add one initial space escape-marker to prevent accidental
1272 token-pasting (often removed by macroexpand). */
1273 *exp_p++ = '@';
1274 *exp_p++ = ' ';
1276 if (limit - p >= 2 && p[0] == '#' && p[1] == '#') {
1277 cpp_error (pfile, "`##' at start of macro definition");
1278 p += 2;
1281 /* Process the main body of the definition. */
1282 while (p < limit) {
1283 int skipped_arg = 0;
1284 register U_CHAR c = *p++;
1286 *exp_p++ = c;
1288 if (!CPP_TRADITIONAL (pfile)) {
1289 switch (c) {
1290 case '\'':
1291 case '\"':
1292 if (expected_delimiter != '\0') {
1293 if (c == expected_delimiter)
1294 expected_delimiter = '\0';
1295 } else
1296 expected_delimiter = c;
1297 break;
1299 case '\\':
1300 if (p < limit && expected_delimiter) {
1301 /* In a string, backslash goes through
1302 and makes next char ordinary. */
1303 *exp_p++ = *p++;
1305 break;
1307 case '@':
1308 /* An '@' in a string or character constant stands for itself,
1309 and does not need to be escaped. */
1310 if (!expected_delimiter)
1311 *exp_p++ = c;
1312 break;
1314 case '#':
1315 /* # is ordinary inside a string. */
1316 if (expected_delimiter)
1317 break;
1318 if (p < limit && *p == '#') {
1319 /* ##: concatenate preceding and following tokens. */
1320 /* Take out the first #, discard preceding whitespace. */
1321 exp_p--;
1322 while (exp_p > lastp && is_hor_space[exp_p[-1]])
1323 --exp_p;
1324 /* Skip the second #. */
1325 p++;
1326 /* Discard following whitespace. */
1327 SKIP_WHITE_SPACE (p);
1328 concat = p;
1329 if (p == limit)
1330 cpp_error (pfile, "`##' at end of macro definition");
1331 } else if (nargs >= 0) {
1332 /* Single #: stringify following argument ref.
1333 Don't leave the # in the expansion. */
1334 exp_p--;
1335 SKIP_WHITE_SPACE (p);
1336 if (p == limit || ! is_idstart[*p])
1337 cpp_error (pfile,
1338 "`#' operator is not followed by a macro argument name");
1339 else
1340 stringify = p;
1342 break;
1344 } else {
1345 /* In -traditional mode, recognize arguments inside strings and
1346 and character constants, and ignore special properties of #.
1347 Arguments inside strings are considered "stringified", but no
1348 extra quote marks are supplied. */
1349 switch (c) {
1350 case '\'':
1351 case '\"':
1352 if (expected_delimiter != '\0') {
1353 if (c == expected_delimiter)
1354 expected_delimiter = '\0';
1355 } else
1356 expected_delimiter = c;
1357 break;
1359 case '\\':
1360 /* Backslash quotes delimiters and itself, but not macro args. */
1361 if (expected_delimiter != 0 && p < limit
1362 && (*p == expected_delimiter || *p == '\\')) {
1363 *exp_p++ = *p++;
1364 continue;
1366 break;
1368 case '/':
1369 if (expected_delimiter != '\0') /* No comments inside strings. */
1370 break;
1371 if (*p == '*') {
1372 /* If we find a comment that wasn't removed by handle_directive,
1373 this must be -traditional. So replace the comment with
1374 nothing at all. */
1375 exp_p--;
1376 p += 1;
1377 while (p < limit && !(p[-2] == '*' && p[-1] == '/'))
1378 p++;
1379 #if 0
1380 /* Mark this as a concatenation-point, as if it had been ##. */
1381 concat = p;
1382 #endif
1384 break;
1388 /* Handle the start of a symbol. */
1389 if (is_idchar[c] && nargs > 0) {
1390 U_CHAR *id_beg = p - 1;
1391 int id_len;
1393 --exp_p;
1394 while (p != limit && is_idchar[*p]) p++;
1395 id_len = p - id_beg;
1397 if (is_idstart[c]) {
1398 register struct arglist *arg;
1400 for (arg = arglist; arg != NULL; arg = arg->next) {
1401 struct reflist *tpat;
1403 if (arg->name[0] == c
1404 && arg->length == id_len
1405 && strncmp (arg->name, id_beg, id_len) == 0) {
1406 if (expected_delimiter && CPP_OPTIONS (pfile)->warn_stringify) {
1407 if (CPP_TRADITIONAL (pfile)) {
1408 cpp_warning (pfile, "macro argument `%.*s' is stringified.",
1409 id_len, arg->name);
1410 } else {
1411 cpp_warning (pfile,
1412 "macro arg `%.*s' would be stringified with -traditional.",
1413 id_len, arg->name);
1416 /* If ANSI, don't actually substitute inside a string. */
1417 if (!CPP_TRADITIONAL (pfile) && expected_delimiter)
1418 break;
1419 /* make a pat node for this arg and append it to the end of
1420 the pat list */
1421 tpat = (struct reflist *) xmalloc (sizeof (struct reflist));
1422 tpat->next = NULL;
1423 tpat->raw_before = concat == id_beg;
1424 tpat->raw_after = 0;
1425 tpat->rest_args = arg->rest_args;
1426 tpat->stringify = (CPP_TRADITIONAL (pfile)
1427 ? expected_delimiter != '\0'
1428 : stringify == id_beg);
1430 if (endpat == NULL)
1431 defn->pattern = tpat;
1432 else
1433 endpat->next = tpat;
1434 endpat = tpat;
1436 tpat->argno = arg->argno;
1437 tpat->nchars = exp_p - lastp;
1439 register U_CHAR *p1 = p;
1440 SKIP_WHITE_SPACE (p1);
1441 if (p1 + 2 <= limit && p1[0] == '#' && p1[1] == '#')
1442 tpat->raw_after = 1;
1444 lastp = exp_p; /* place to start copying from next time */
1445 skipped_arg = 1;
1446 break;
1451 /* If this was not a macro arg, copy it into the expansion. */
1452 if (! skipped_arg) {
1453 register U_CHAR *lim1 = p;
1454 p = id_beg;
1455 while (p != lim1)
1456 *exp_p++ = *p++;
1457 if (stringify == id_beg)
1458 cpp_error (pfile,
1459 "`#' operator should be followed by a macro argument name");
1464 if (!CPP_TRADITIONAL (pfile) && expected_delimiter == 0)
1466 /* If ANSI, put in a "@ " marker to prevent token pasting.
1467 But not if "inside a string" (which in ANSI mode
1468 happens only for -D option). */
1469 *exp_p++ = '@';
1470 *exp_p++ = ' ';
1473 *exp_p = '\0';
1475 defn->length = exp_p - defn->expansion;
1477 /* Crash now if we overrun the allocated size. */
1478 if (defn->length + 1 > maxsize)
1479 abort ();
1481 #if 0
1482 /* This isn't worth the time it takes. */
1483 /* give back excess storage */
1484 defn->expansion = (U_CHAR *) xrealloc (defn->expansion, defn->length + 1);
1485 #endif
1487 return defn;
1491 * special extension string that can be added to the last macro argument to
1492 * allow it to absorb the "rest" of the arguments when expanded. Ex:
1493 * #define wow(a, b...) process (b, a, b)
1494 * { wow (1, 2, 3); } -> { process (2, 3, 1, 2, 3); }
1495 * { wow (one, two); } -> { process (two, one, two); }
1496 * if this "rest_arg" is used with the concat token '##' and if it is not
1497 * supplied then the token attached to with ## will not be outputted. Ex:
1498 * #define wow (a, b...) process (b ## , a, ## b)
1499 * { wow (1, 2); } -> { process (2, 1, 2); }
1500 * { wow (one); } -> { process (one); {
1502 static char rest_extension[] = "...";
1503 #define REST_EXTENSION_LENGTH (sizeof (rest_extension) - 1)
1505 /* Create a DEFINITION node from a #define directive. Arguments are
1506 as for do_define. */
1507 static MACRODEF
1508 create_definition (buf, limit, pfile, predefinition)
1509 U_CHAR *buf, *limit;
1510 cpp_reader *pfile;
1511 int predefinition;
1513 U_CHAR *bp; /* temp ptr into input buffer */
1514 U_CHAR *symname; /* remember where symbol name starts */
1515 int sym_length; /* and how long it is */
1516 int rest_args = 0;
1517 long line, col;
1518 char *file = CPP_BUFFER (pfile) ? CPP_BUFFER (pfile)->nominal_fname : "";
1519 DEFINITION *defn;
1520 int arglengths = 0; /* Accumulate lengths of arg names
1521 plus number of args. */
1522 MACRODEF mdef;
1523 cpp_buf_line_and_col (CPP_BUFFER (pfile), &line, &col);
1525 bp = buf;
1527 while (is_hor_space[*bp])
1528 bp++;
1530 symname = bp; /* remember where it starts */
1532 sym_length = check_macro_name (pfile, bp, "macro");
1533 bp += sym_length;
1535 /* Lossage will occur if identifiers or control keywords are broken
1536 across lines using backslash. This is not the right place to take
1537 care of that. */
1539 if (*bp == '(') {
1540 struct arglist *arg_ptrs = NULL;
1541 int argno = 0;
1543 bp++; /* skip '(' */
1544 SKIP_WHITE_SPACE (bp);
1546 /* Loop over macro argument names. */
1547 while (*bp != ')') {
1548 struct arglist *temp;
1550 temp = (struct arglist *) alloca (sizeof (struct arglist));
1551 temp->name = bp;
1552 temp->next = arg_ptrs;
1553 temp->argno = argno++;
1554 temp->rest_args = 0;
1555 arg_ptrs = temp;
1557 if (rest_args)
1558 cpp_pedwarn (pfile, "another parameter follows `%s'", rest_extension);
1560 if (!is_idstart[*bp])
1561 cpp_pedwarn (pfile, "invalid character in macro parameter name");
1563 /* Find the end of the arg name. */
1564 while (is_idchar[*bp]) {
1565 bp++;
1566 /* do we have a "special" rest-args extension here? */
1567 if (limit - bp > REST_EXTENSION_LENGTH &&
1568 strncmp (rest_extension, bp, REST_EXTENSION_LENGTH) == 0) {
1569 rest_args = 1;
1570 temp->rest_args = 1;
1571 break;
1574 temp->length = bp - temp->name;
1575 if (rest_args == 1)
1576 bp += REST_EXTENSION_LENGTH;
1577 arglengths += temp->length + 2;
1578 SKIP_WHITE_SPACE (bp);
1579 if (temp->length == 0 || (*bp != ',' && *bp != ')')) {
1580 cpp_error (pfile, "badly punctuated parameter list in `#define'");
1581 goto nope;
1583 if (*bp == ',') {
1584 bp++;
1585 SKIP_WHITE_SPACE (bp);
1587 if (bp >= limit) {
1588 cpp_error (pfile, "unterminated parameter list in `#define'");
1589 goto nope;
1592 struct arglist *otemp;
1594 for (otemp = temp->next; otemp != NULL; otemp = otemp->next)
1595 if (temp->length == otemp->length &&
1596 strncmp (temp->name, otemp->name, temp->length) == 0) {
1597 U_CHAR *name;
1599 name = (U_CHAR *) alloca (temp->length + 1);
1600 (void) strncpy (name, temp->name, temp->length);
1601 name[temp->length] = '\0';
1602 cpp_error (pfile,
1603 "duplicate argument name `%s' in `#define'", name);
1604 goto nope;
1609 ++bp; /* skip paren */
1610 SKIP_WHITE_SPACE (bp);
1611 /* now everything from bp before limit is the definition. */
1612 defn = collect_expansion (pfile, bp, limit, argno, arg_ptrs);
1613 defn->rest_args = rest_args;
1615 /* Now set defn->args.argnames to the result of concatenating
1616 the argument names in reverse order
1617 with comma-space between them. */
1618 defn->args.argnames = (U_CHAR *) xmalloc (arglengths + 1);
1620 struct arglist *temp;
1621 int i = 0;
1622 for (temp = arg_ptrs; temp; temp = temp->next) {
1623 bcopy (temp->name, &defn->args.argnames[i], temp->length);
1624 i += temp->length;
1625 if (temp->next != 0) {
1626 defn->args.argnames[i++] = ',';
1627 defn->args.argnames[i++] = ' ';
1630 defn->args.argnames[i] = 0;
1632 } else {
1633 /* Simple expansion or empty definition. */
1635 if (bp < limit)
1637 if (is_hor_space[*bp]) {
1638 bp++;
1639 SKIP_WHITE_SPACE (bp);
1640 } else {
1641 switch (*bp) {
1642 case '!': case '"': case '#': case '%': case '&': case '\'':
1643 case ')': case '*': case '+': case ',': case '-': case '.':
1644 case '/': case ':': case ';': case '<': case '=': case '>':
1645 case '?': case '[': case '\\': case ']': case '^': case '{':
1646 case '|': case '}': case '~':
1647 cpp_warning (pfile, "missing white space after `#define %.*s'",
1648 sym_length, symname);
1649 break;
1651 default:
1652 cpp_pedwarn (pfile, "missing white space after `#define %.*s'",
1653 sym_length, symname);
1654 break;
1658 /* now everything from bp before limit is the definition. */
1659 defn = collect_expansion (pfile, bp, limit, -1, NULL_PTR);
1660 defn->args.argnames = (U_CHAR *) "";
1663 defn->line = line;
1664 defn->file = file;
1666 /* OP is null if this is a predefinition */
1667 defn->predefined = predefinition;
1668 mdef.defn = defn;
1669 mdef.symnam = symname;
1670 mdef.symlen = sym_length;
1672 return mdef;
1674 nope:
1675 mdef.defn = 0;
1676 return mdef;
1679 /* Check a purported macro name SYMNAME, and yield its length.
1680 USAGE is the kind of name this is intended for. */
1682 static int
1683 check_macro_name (pfile, symname, usage)
1684 cpp_reader *pfile;
1685 U_CHAR *symname;
1686 char *usage;
1688 U_CHAR *p;
1689 int sym_length;
1691 for (p = symname; is_idchar[*p]; p++)
1693 sym_length = p - symname;
1694 if (sym_length == 0)
1695 cpp_error (pfile, "invalid %s name", usage);
1696 else if (!is_idstart[*symname]) {
1697 U_CHAR *msg; /* what pain... */
1698 msg = (U_CHAR *) alloca (sym_length + 1);
1699 bcopy (symname, msg, sym_length);
1700 msg[sym_length] = 0;
1701 cpp_error (pfile, "invalid %s name `%s'", usage, msg);
1702 } else {
1703 if (! strncmp (symname, "defined", 7) && sym_length == 7)
1704 cpp_error (pfile, "invalid %s name `defined'", usage);
1706 return sym_length;
1710 * return zero if two DEFINITIONs are isomorphic
1712 static int
1713 compare_defs (d1, d2)
1714 DEFINITION *d1, *d2;
1716 register struct reflist *a1, *a2;
1717 register U_CHAR *p1 = d1->expansion;
1718 register U_CHAR *p2 = d2->expansion;
1719 int first = 1;
1721 if (d1->nargs != d2->nargs)
1722 return 1;
1723 if (strcmp ((char *)d1->args.argnames, (char *)d2->args.argnames))
1724 return 1;
1725 for (a1 = d1->pattern, a2 = d2->pattern; a1 && a2;
1726 a1 = a1->next, a2 = a2->next) {
1727 if (!((a1->nchars == a2->nchars && ! strncmp (p1, p2, a1->nchars))
1728 || ! comp_def_part (first, p1, a1->nchars, p2, a2->nchars, 0))
1729 || a1->argno != a2->argno
1730 || a1->stringify != a2->stringify
1731 || a1->raw_before != a2->raw_before
1732 || a1->raw_after != a2->raw_after)
1733 return 1;
1734 first = 0;
1735 p1 += a1->nchars;
1736 p2 += a2->nchars;
1738 if (a1 != a2)
1739 return 1;
1740 if (comp_def_part (first, p1, d1->length - (p1 - d1->expansion),
1741 p2, d2->length - (p2 - d2->expansion), 1))
1742 return 1;
1743 return 0;
1746 /* Return 1 if two parts of two macro definitions are effectively different.
1747 One of the parts starts at BEG1 and has LEN1 chars;
1748 the other has LEN2 chars at BEG2.
1749 Any sequence of whitespace matches any other sequence of whitespace.
1750 FIRST means these parts are the first of a macro definition;
1751 so ignore leading whitespace entirely.
1752 LAST means these parts are the last of a macro definition;
1753 so ignore trailing whitespace entirely. */
1755 static int
1756 comp_def_part (first, beg1, len1, beg2, len2, last)
1757 int first;
1758 U_CHAR *beg1, *beg2;
1759 int len1, len2;
1760 int last;
1762 register U_CHAR *end1 = beg1 + len1;
1763 register U_CHAR *end2 = beg2 + len2;
1764 if (first) {
1765 while (beg1 != end1 && is_space[*beg1]) beg1++;
1766 while (beg2 != end2 && is_space[*beg2]) beg2++;
1768 if (last) {
1769 while (beg1 != end1 && is_space[end1[-1]]) end1--;
1770 while (beg2 != end2 && is_space[end2[-1]]) end2--;
1772 while (beg1 != end1 && beg2 != end2) {
1773 if (is_space[*beg1] && is_space[*beg2]) {
1774 while (beg1 != end1 && is_space[*beg1]) beg1++;
1775 while (beg2 != end2 && is_space[*beg2]) beg2++;
1776 } else if (*beg1 == *beg2) {
1777 beg1++; beg2++;
1778 } else break;
1780 return (beg1 != end1) || (beg2 != end2);
1783 /* Process a #define command.
1784 BUF points to the contents of the #define command, as a contiguous string.
1785 LIMIT points to the first character past the end of the definition.
1786 KEYWORD is the keyword-table entry for #define,
1787 or NULL for a "predefined" macro. */
1789 static int
1790 do_define (pfile, keyword, buf, limit)
1791 cpp_reader *pfile;
1792 struct directive *keyword;
1793 U_CHAR *buf, *limit;
1795 int hashcode;
1796 MACRODEF mdef;
1797 HASHNODE *hp;
1799 #if 0
1800 /* If this is a precompiler run (with -pcp) pass thru #define commands. */
1801 if (pcp_outfile && keyword)
1802 pass_thru_directive (buf, limit, pfile, keyword);
1803 #endif
1805 mdef = create_definition (buf, limit, pfile, keyword == NULL);
1806 if (mdef.defn == 0)
1807 goto nope;
1809 hashcode = hashf (mdef.symnam, mdef.symlen, HASHSIZE);
1811 if ((hp = cpp_lookup (pfile, mdef.symnam, mdef.symlen, hashcode)) != NULL)
1813 int ok = 0;
1814 /* Redefining a precompiled key is ok. */
1815 if (hp->type == T_PCSTRING)
1816 ok = 1;
1817 /* Redefining a macro is ok if the definitions are the same. */
1818 else if (hp->type == T_MACRO)
1819 ok = ! compare_defs (mdef.defn, hp->value.defn);
1820 /* Redefining a constant is ok with -D. */
1821 else if (hp->type == T_CONST)
1822 ok = ! CPP_OPTIONS (pfile)->done_initializing;
1823 /* Print the warning if it's not ok. */
1824 if (!ok)
1826 U_CHAR *msg; /* what pain... */
1828 /* If we are passing through #define and #undef directives, do
1829 that for this re-definition now. */
1830 if (CPP_OPTIONS (pfile)->debug_output && keyword)
1831 pass_thru_directive (buf, limit, pfile, keyword);
1833 msg = (U_CHAR *) alloca (mdef.symlen + 22);
1834 *msg = '`';
1835 bcopy (mdef.symnam, msg + 1, mdef.symlen);
1836 strcpy ((char *) (msg + mdef.symlen + 1), "' redefined");
1837 cpp_pedwarn (pfile, msg);
1838 if (hp->type == T_MACRO)
1839 cpp_pedwarn_with_file_and_line (pfile, hp->value.defn->file, hp->value.defn->line,
1840 "this is the location of the previous definition");
1842 /* Replace the old definition. */
1843 hp->type = T_MACRO;
1844 hp->value.defn = mdef.defn;
1846 else
1848 /* If we are passing through #define and #undef directives, do
1849 that for this new definition now. */
1850 if (CPP_OPTIONS (pfile)->debug_output && keyword)
1851 pass_thru_directive (buf, limit, pfile, keyword);
1852 install (mdef.symnam, mdef.symlen, T_MACRO, 0,
1853 (char *) mdef.defn, hashcode);
1856 return 0;
1858 nope:
1860 return 1;
1863 /* This structure represents one parsed argument in a macro call.
1864 `raw' points to the argument text as written (`raw_length' is its length).
1865 `expanded' points to the argument's macro-expansion
1866 (its length is `expand_length').
1867 `stringified_length' is the length the argument would have
1868 if stringified.
1869 `use_count' is the number of times this macro arg is substituted
1870 into the macro. If the actual use count exceeds 10,
1871 the value stored is 10. */
1873 /* raw and expanded are relative to ARG_BASE */
1874 #define ARG_BASE ((pfile)->token_buffer)
1876 struct argdata {
1877 /* Strings relative to pfile->token_buffer */
1878 long raw, expanded, stringified;
1879 int raw_length, expand_length;
1880 int stringified_length;
1881 char newlines;
1882 char use_count;
1886 cpp_buffer*
1887 cpp_push_buffer (pfile, buffer, length)
1888 cpp_reader *pfile;
1889 U_CHAR *buffer;
1890 long length;
1892 #ifdef STATIC_BUFFERS
1893 register cpp_buffer *buf = CPP_BUFFER (pfile);
1894 if (buf == pfile->buffer_stack)
1895 fatal ("%s: macro or `#include' recursion too deep", buf->fname);
1896 buf--;
1897 bzero ((char *) buf, sizeof (cpp_buffer));
1898 CPP_BUFFER (pfile) = buf;
1899 #else
1900 register cpp_buffer *buf = (cpp_buffer*) xmalloc (sizeof(cpp_buffer));
1901 bzero ((char *) buf, sizeof (cpp_buffer));
1902 CPP_PREV_BUFFER (buf) = CPP_BUFFER (pfile);
1903 CPP_BUFFER (pfile) = buf;
1904 #endif
1905 buf->if_stack = pfile->if_stack;
1906 buf->cleanup = null_cleanup;
1907 buf->underflow = null_underflow;
1908 buf->buf = buf->cur = buffer;
1909 buf->alimit = buf->rlimit = buffer + length;
1911 return buf;
1914 cpp_buffer*
1915 cpp_pop_buffer (pfile)
1916 cpp_reader *pfile;
1918 cpp_buffer *buf = CPP_BUFFER (pfile);
1919 #ifdef STATIC_BUFFERS
1920 (*buf->cleanup) (buf, pfile);
1921 return ++CPP_BUFFER (pfile);
1922 #else
1923 cpp_buffer *next_buf = CPP_PREV_BUFFER (buf);
1924 (*buf->cleanup) (buf, pfile);
1925 CPP_BUFFER (pfile) = next_buf;
1926 free (buf);
1927 return next_buf;
1928 #endif
1931 /* Scan until CPP_BUFFER (PFILE) is exhausted into PFILE->token_buffer.
1932 Pop the buffer when done. */
1934 void
1935 cpp_scan_buffer (pfile)
1936 cpp_reader *pfile;
1938 cpp_buffer *buffer = CPP_BUFFER (pfile);
1939 for (;;)
1941 enum cpp_token token = cpp_get_token (pfile);
1942 if (token == CPP_EOF) /* Should not happen ... */
1943 break;
1944 if (token == CPP_POP && CPP_BUFFER (pfile) == buffer)
1946 cpp_pop_buffer (pfile);
1947 break;
1953 * Rescan a string (which may have escape marks) into pfile's buffer.
1954 * Place the result in pfile->token_buffer.
1956 * The input is copied before it is scanned, so it is safe to pass
1957 * it something from the token_buffer that will get overwritten
1958 * (because it follows CPP_WRITTEN). This is used by do_include.
1961 static void
1962 cpp_expand_to_buffer (pfile, buf, length)
1963 cpp_reader *pfile;
1964 U_CHAR *buf;
1965 int length;
1967 register cpp_buffer *ip;
1968 cpp_buffer obuf;
1969 U_CHAR *limit = buf + length;
1970 U_CHAR *buf1;
1971 #if 0
1972 int odepth = indepth;
1973 #endif
1975 if (length < 0)
1976 abort ();
1978 /* Set up the input on the input stack. */
1980 buf1 = (U_CHAR *) alloca (length + 1);
1982 register U_CHAR *p1 = buf;
1983 register U_CHAR *p2 = buf1;
1985 while (p1 != limit)
1986 *p2++ = *p1++;
1988 buf1[length] = 0;
1990 ip = cpp_push_buffer (pfile, buf1, length);
1991 ip->has_escapes = 1;
1992 #if 0
1993 ip->lineno = obuf.lineno = 1;
1994 #endif
1996 /* Scan the input, create the output. */
1997 cpp_scan_buffer (pfile);
1999 #if 0
2000 if (indepth != odepth)
2001 abort ();
2002 #endif
2004 CPP_NUL_TERMINATE (pfile);
2008 static void
2009 adjust_position (buf, limit, linep, colp)
2010 U_CHAR *buf;
2011 U_CHAR *limit;
2012 long *linep;
2013 long *colp;
2015 while (buf < limit)
2017 U_CHAR ch = *buf++;
2018 if (ch == '\n')
2019 (*linep)++, (*colp) = 1;
2020 else
2021 (*colp)++;
2025 /* Move line_base forward, updating lineno and colno. */
2027 static void
2028 update_position (pbuf)
2029 register cpp_buffer *pbuf;
2031 unsigned char *old_pos = pbuf->buf + pbuf->line_base;
2032 unsigned char *new_pos = pbuf->cur;
2033 register struct parse_marker *mark;
2034 for (mark = pbuf->marks; mark != NULL; mark = mark->next)
2036 if (pbuf->buf + mark->position < new_pos)
2037 new_pos = pbuf->buf + mark->position;
2039 pbuf->line_base += new_pos - old_pos;
2040 adjust_position (old_pos, new_pos, &pbuf->lineno, &pbuf->colno);
2043 void
2044 cpp_buf_line_and_col (pbuf, linep, colp)
2045 register cpp_buffer *pbuf;
2046 long *linep, *colp;
2048 long dummy;
2049 if (colp == NULL)
2050 colp = &dummy;
2051 if (pbuf)
2053 *linep = pbuf->lineno;
2054 *colp = pbuf->colno;
2055 adjust_position (pbuf->buf + pbuf->line_base, pbuf->cur, linep, colp);
2057 else
2059 *linep = 0;
2060 *colp = 0;
2064 /* Return the cpp_buffer that corresponds to a file (not a macro). */
2066 cpp_buffer*
2067 cpp_file_buffer (pfile)
2068 cpp_reader *pfile;
2070 cpp_buffer *ip = CPP_BUFFER (pfile);
2072 for ( ; ip != CPP_NULL_BUFFER (pfile); ip = CPP_PREV_BUFFER (ip))
2073 if (ip->fname != NULL)
2074 return ip;
2075 return NULL;
2078 static long
2079 count_newlines (buf, limit)
2080 register U_CHAR *buf;
2081 register U_CHAR *limit;
2083 register long count = 0;
2084 while (buf < limit)
2086 U_CHAR ch = *buf++;
2087 if (ch == '\n')
2088 count++;
2090 return count;
2094 * write out a #line command, for instance, after an #include file.
2095 * If CONDITIONAL is nonzero, we can omit the #line if it would
2096 * appear to be a no-op, and we can output a few newlines instead
2097 * if we want to increase the line number by a small amount.
2098 * FILE_CHANGE says whether we are entering a file, leaving, or neither.
2101 static void
2102 output_line_command (pfile, conditional, file_change)
2103 cpp_reader *pfile;
2104 int conditional;
2105 enum file_change_code file_change;
2107 int len;
2108 char *line_cmd_buf, *line_end;
2109 long line, col;
2110 cpp_buffer *ip = CPP_BUFFER (pfile);
2112 if (ip->fname == NULL || CPP_OPTIONS (pfile)->no_output) {
2113 return;
2116 update_position (ip);
2117 line = CPP_BUFFER (pfile)->lineno;
2118 col = CPP_BUFFER (pfile)->colno;
2119 adjust_position (CPP_LINE_BASE (ip), ip->cur, &line, &col);
2121 if (CPP_OPTIONS (pfile)->no_line_commands)
2122 return;
2124 if (conditional) {
2125 if (line == pfile->lineno)
2126 return;
2128 /* If the inherited line number is a little too small,
2129 output some newlines instead of a #line command. */
2130 if (line > pfile->lineno && line < pfile->lineno + 8) {
2131 CPP_RESERVE (pfile, 20);
2132 while (line > pfile->lineno) {
2133 CPP_PUTC_Q (pfile, '\n');
2134 pfile->lineno++;
2136 return;
2140 #if 0
2141 /* Don't output a line number of 0 if we can help it. */
2142 if (ip->lineno == 0 && ip->bufp - ip->buf < ip->length
2143 && *ip->bufp == '\n') {
2144 ip->lineno++;
2145 ip->bufp++;
2147 #endif
2149 CPP_RESERVE (pfile, 4 * strlen (ip->nominal_fname) + 50);
2151 #ifdef OUTPUT_LINE_COMMANDS
2152 static char sharp_line[] = "#line ";
2153 #else
2154 static char sharp_line[] = "# ";
2155 #endif
2156 CPP_PUTS_Q (pfile, sharp_line, sizeof(sharp_line)-1);
2159 sprintf (CPP_PWRITTEN (pfile), "%d ", line);
2160 CPP_ADJUST_WRITTEN (pfile, strlen (CPP_PWRITTEN (pfile)));
2162 quote_string (pfile, ip->nominal_fname);
2163 if (file_change != same_file) {
2164 CPP_PUTC_Q (pfile, ' ');
2165 CPP_PUTC_Q (pfile, file_change == enter_file ? '1' : '2');
2167 /* Tell cc1 if following text comes from a system header file. */
2168 if (ip->system_header_p) {
2169 CPP_PUTC_Q (pfile, ' ');
2170 CPP_PUTC_Q (pfile, '3');
2172 #ifndef NO_IMPLICIT_EXTERN_C
2173 /* Tell cc1plus if following text should be treated as C. */
2174 if (ip->system_header_p == 2 && CPP_OPTIONS (pfile)->cplusplus) {
2175 CPP_PUTC_Q (pfile, ' ');
2176 CPP_PUTC_Q (pfile, '4');
2178 #endif
2179 CPP_PUTC_Q (pfile, '\n');
2180 pfile->lineno = line;
2184 * Parse a macro argument and append the info on PFILE's token_buffer.
2185 * REST_ARGS means to absorb the rest of the args.
2186 * Return nonzero to indicate a syntax error.
2189 static enum cpp_token
2190 macarg (pfile, rest_args)
2191 cpp_reader *pfile;
2192 int rest_args;
2194 int paren = 0;
2195 enum cpp_token token;
2196 long arg_start = CPP_WRITTEN (pfile);
2197 char save_put_out_comments = CPP_OPTIONS (pfile)->put_out_comments;
2198 CPP_OPTIONS (pfile)->put_out_comments = 0;
2200 /* Try to parse as much of the argument as exists at this
2201 input stack level. */
2202 pfile->no_macro_expand++;
2203 for (;;)
2205 token = cpp_get_token (pfile);
2206 switch (token)
2208 case CPP_EOF:
2209 goto done;
2210 case CPP_POP:
2211 /* If we've hit end of file, it's an error (reported by caller).
2212 Ditto if it's the end of cpp_expand_to_buffer text.
2213 If we've hit end of macro, just continue. */
2214 if (! CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
2215 goto done;
2216 break;
2217 case CPP_LPAREN:
2218 paren++;
2219 break;
2220 case CPP_RPAREN:
2221 if (--paren < 0)
2222 goto found;
2223 break;
2224 case CPP_COMMA:
2225 /* if we've returned to lowest level and
2226 we aren't absorbing all args */
2227 if (paren == 0 && rest_args == 0)
2228 goto found;
2229 break;
2230 found:
2231 /* Remove ',' or ')' from argument buffer. */
2232 CPP_ADJUST_WRITTEN (pfile, -1);
2233 goto done;
2234 default: ;
2238 done:
2239 CPP_OPTIONS (pfile)->put_out_comments = save_put_out_comments;
2240 pfile->no_macro_expand--;
2242 return token;
2245 /* Turn newlines to spaces in the string of length LENGTH at START,
2246 except inside of string constants.
2247 The string is copied into itself with its beginning staying fixed. */
2249 static int
2250 change_newlines (start, length)
2251 U_CHAR *start;
2252 int length;
2254 register U_CHAR *ibp;
2255 register U_CHAR *obp;
2256 register U_CHAR *limit;
2257 register int c;
2259 ibp = start;
2260 limit = start + length;
2261 obp = start;
2263 while (ibp < limit) {
2264 *obp++ = c = *ibp++;
2265 switch (c) {
2267 case '\'':
2268 case '\"':
2269 /* Notice and skip strings, so that we don't delete newlines in them. */
2271 int quotec = c;
2272 while (ibp < limit) {
2273 *obp++ = c = *ibp++;
2274 if (c == quotec)
2275 break;
2276 if (c == '\n' && quotec == '\'')
2277 break;
2280 break;
2284 return obp - start;
2288 static struct tm *
2289 timestamp (pfile)
2290 cpp_reader *pfile;
2292 if (!pfile->timebuf) {
2293 time_t t = time ((time_t *)0);
2294 pfile->timebuf = localtime (&t);
2296 return pfile->timebuf;
2299 static char *monthnames[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
2300 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
2304 * expand things like __FILE__. Place the expansion into the output
2305 * buffer *without* rescanning.
2308 static void
2309 special_symbol (hp, pfile)
2310 HASHNODE *hp;
2311 cpp_reader *pfile;
2313 char *buf;
2314 int i, len;
2315 int true_indepth;
2316 cpp_buffer *ip = NULL;
2317 struct tm *timebuf;
2319 int paren = 0; /* For special `defined' keyword */
2321 #if 0
2322 if (pcp_outfile && pcp_inside_if
2323 && hp->type != T_SPEC_DEFINED && hp->type != T_CONST)
2324 cpp_error (pfile,
2325 "Predefined macro `%s' used inside `#if' during precompilation",
2326 hp->name);
2327 #endif
2329 for (ip = CPP_BUFFER (pfile); ; ip = CPP_PREV_BUFFER (ip))
2331 if (ip == NULL)
2333 cpp_error (pfile, "cccp error: not in any file?!");
2334 return; /* the show must go on */
2336 if (ip->fname != NULL)
2337 break;
2340 switch (hp->type)
2342 case T_FILE:
2343 case T_BASE_FILE:
2345 char *string;
2346 if (hp->type == T_BASE_FILE)
2348 while (CPP_PREV_BUFFER (ip))
2349 ip = CPP_PREV_BUFFER (ip);
2351 string = ip->nominal_fname;
2353 if (!string)
2354 string = "";
2355 CPP_RESERVE (pfile, 3 + 4 * strlen (string));
2356 quote_string (pfile, string);
2357 return;
2360 case T_INCLUDE_LEVEL:
2361 true_indepth = 0;
2362 for (ip = CPP_BUFFER (pfile); ip != NULL; ip = CPP_PREV_BUFFER (ip))
2363 if (ip->fname != NULL)
2364 true_indepth++;
2366 buf = (char *) alloca (8); /* Eight bytes ought to be more than enough */
2367 sprintf (buf, "%d", true_indepth - 1);
2368 break;
2370 case T_VERSION:
2371 buf = (char *) alloca (3 + strlen (version_string));
2372 sprintf (buf, "\"%s\"", version_string);
2373 break;
2375 #ifndef NO_BUILTIN_SIZE_TYPE
2376 case T_SIZE_TYPE:
2377 buf = SIZE_TYPE;
2378 break;
2379 #endif
2381 #ifndef NO_BUILTIN_PTRDIFF_TYPE
2382 case T_PTRDIFF_TYPE:
2383 buf = PTRDIFF_TYPE;
2384 break;
2385 #endif
2387 case T_WCHAR_TYPE:
2388 buf = CPP_WCHAR_TYPE (pfile);
2389 break;
2391 case T_USER_LABEL_PREFIX_TYPE:
2392 buf = USER_LABEL_PREFIX;
2393 break;
2395 case T_REGISTER_PREFIX_TYPE:
2396 buf = REGISTER_PREFIX;
2397 break;
2399 case T_CONST:
2400 buf = (char *) alloca (4 * sizeof (int));
2401 sprintf (buf, "%d", hp->value.ival);
2402 #if 0
2403 if (pcp_inside_if && pcp_outfile)
2404 /* Output a precondition for this macro use */
2405 fprintf (pcp_outfile, "#define %s %d\n", hp->name, hp->value.ival);
2406 #endif
2407 break;
2409 case T_SPECLINE:
2411 long line = ip->lineno;
2412 long col = ip->colno;
2413 adjust_position (CPP_LINE_BASE (ip), ip->cur, &line, &col);
2415 buf = (char *) alloca (10);
2416 sprintf (buf, "%d", line);
2418 break;
2420 case T_DATE:
2421 case T_TIME:
2422 buf = (char *) alloca (20);
2423 timebuf = timestamp (pfile);
2424 if (hp->type == T_DATE)
2425 sprintf (buf, "\"%s %2d %4d\"", monthnames[timebuf->tm_mon],
2426 timebuf->tm_mday, timebuf->tm_year + 1900);
2427 else
2428 sprintf (buf, "\"%02d:%02d:%02d\"", timebuf->tm_hour, timebuf->tm_min,
2429 timebuf->tm_sec);
2430 break;
2432 case T_SPEC_DEFINED:
2433 buf = " 0 "; /* Assume symbol is not defined */
2434 ip = CPP_BUFFER (pfile);
2435 SKIP_WHITE_SPACE (ip->cur);
2436 if (*ip->cur == '(')
2438 paren++;
2439 ip->cur++; /* Skip over the paren */
2440 SKIP_WHITE_SPACE (ip->cur);
2443 if (!is_idstart[*ip->cur])
2444 goto oops;
2445 if (hp = cpp_lookup (pfile, ip->cur, -1, -1))
2447 #if 0
2448 if (pcp_outfile && pcp_inside_if
2449 && (hp->type == T_CONST
2450 || (hp->type == T_MACRO && hp->value.defn->predefined)))
2451 /* Output a precondition for this macro use. */
2452 fprintf (pcp_outfile, "#define %s\n", hp->name);
2453 #endif
2454 buf = " 1 ";
2456 #if 0
2457 else
2458 if (pcp_outfile && pcp_inside_if)
2460 /* Output a precondition for this macro use */
2461 U_CHAR *cp = ip->bufp;
2462 fprintf (pcp_outfile, "#undef ");
2463 while (is_idchar[*cp]) /* Ick! */
2464 fputc (*cp++, pcp_outfile);
2465 putc ('\n', pcp_outfile);
2467 #endif
2468 while (is_idchar[*ip->cur])
2469 ++ip->cur;
2470 SKIP_WHITE_SPACE (ip->cur);
2471 if (paren)
2473 if (*ip->cur != ')')
2474 goto oops;
2475 ++ip->cur;
2477 break;
2479 oops:
2481 cpp_error (pfile, "`defined' without an identifier");
2482 break;
2484 default:
2485 cpp_error (pfile, "cccp error: invalid special hash type"); /* time for gdb */
2486 abort ();
2488 len = strlen (buf);
2489 CPP_RESERVE (pfile, len + 1);
2490 CPP_PUTS_Q (pfile, buf, len);
2491 CPP_NUL_TERMINATE_Q (pfile);
2493 return;
2496 /* Initialize the built-in macros. */
2498 static void
2499 initialize_builtins (pfile)
2500 cpp_reader *pfile;
2502 install ("__LINE__", -1, T_SPECLINE, 0, 0, -1);
2503 install ("__DATE__", -1, T_DATE, 0, 0, -1);
2504 install ("__FILE__", -1, T_FILE, 0, 0, -1);
2505 install ("__BASE_FILE__", -1, T_BASE_FILE, 0, 0, -1);
2506 install ("__INCLUDE_LEVEL__", -1, T_INCLUDE_LEVEL, 0, 0, -1);
2507 install ("__VERSION__", -1, T_VERSION, 0, 0, -1);
2508 #ifndef NO_BUILTIN_SIZE_TYPE
2509 install ("__SIZE_TYPE__", -1, T_SIZE_TYPE, 0, 0, -1);
2510 #endif
2511 #ifndef NO_BUILTIN_PTRDIFF_TYPE
2512 install ("__PTRDIFF_TYPE__ ", -1, T_PTRDIFF_TYPE, 0, 0, -1);
2513 #endif
2514 install ("__WCHAR_TYPE__", -1, T_WCHAR_TYPE, 0, 0, -1);
2515 install ("__USER_LABEL_PREFIX__", -1, T_USER_LABEL_PREFIX_TYPE, 0, 0, -1);
2516 install ("__REGISTER_PREFIX__", -1, T_REGISTER_PREFIX_TYPE, 0, 0, -1);
2517 install ("__TIME__", -1, T_TIME, 0, 0, -1);
2518 if (!CPP_TRADITIONAL (pfile))
2519 install ("__STDC__", -1, T_CONST, STDC_VALUE, 0, -1);
2520 if (CPP_OPTIONS (pfile)->objc)
2521 install ("__OBJC__", -1, T_CONST, 1, 0, -1);
2522 /* This is supplied using a -D by the compiler driver
2523 so that it is present only when truly compiling with GNU C. */
2524 /* install ("__GNUC__", -1, T_CONST, 2, 0, -1); */
2526 if (CPP_OPTIONS (pfile)->debug_output)
2528 char directive[2048];
2529 register struct directive *dp = &directive_table[0];
2530 struct tm *timebuf = timestamp (pfile);
2531 cpp_buffer *pbuffer = CPP_BUFFER (pfile);
2533 while (CPP_PREV_BUFFER (pbuffer))
2534 pbuffer = CPP_PREV_BUFFER (pbuffer);
2535 sprintf (directive, " __BASE_FILE__ \"%s\"\n",
2536 pbuffer->nominal_fname);
2537 output_line_command (pfile, 0, same_file);
2538 pass_thru_directive (directive, &directive[strlen (directive)], pfile, dp);
2540 sprintf (directive, " __VERSION__ \"%s\"\n", version_string);
2541 output_line_command (pfile, 0, same_file);
2542 pass_thru_directive (directive, &directive[strlen (directive)], pfile, dp);
2544 #ifndef NO_BUILTIN_SIZE_TYPE
2545 sprintf (directive, " __SIZE_TYPE__ %s\n", SIZE_TYPE);
2546 output_line_command (pfile, 0, same_file);
2547 pass_thru_directive (directive, &directive[strlen (directive)], pfile, dp);
2548 #endif
2550 #ifndef NO_BUILTIN_PTRDIFF_TYPE
2551 sprintf (directive, " __PTRDIFF_TYPE__ %s\n", PTRDIFF_TYPE);
2552 output_line_command (pfile, 0, same_file);
2553 pass_thru_directive (directive, &directive[strlen (directive)], pfile, dp);
2554 #endif
2556 sprintf (directive, " __WCHAR_TYPE__ %s\n", CPP_WCHAR_TYPE (pfile));
2557 output_line_command (pfile, 0, same_file);
2558 pass_thru_directive (directive, &directive[strlen (directive)], pfile, dp);
2560 sprintf (directive, " __DATE__ \"%s %2d %4d\"\n",
2561 monthnames[timebuf->tm_mon],
2562 timebuf->tm_mday, timebuf->tm_year + 1900);
2563 output_line_command (pfile, 0, same_file);
2564 pass_thru_directive (directive, &directive[strlen (directive)], pfile, dp);
2566 sprintf (directive, " __TIME__ \"%02d:%02d:%02d\"\n",
2567 timebuf->tm_hour, timebuf->tm_min, timebuf->tm_sec);
2568 output_line_command (pfile, 0, same_file);
2569 pass_thru_directive (directive, &directive[strlen (directive)], pfile, dp);
2571 if (!CPP_TRADITIONAL (pfile))
2573 sprintf (directive, " __STDC__ 1");
2574 output_line_command (pfile, 0, same_file);
2575 pass_thru_directive (directive, &directive[strlen (directive)],
2576 pfile, dp);
2578 if (CPP_OPTIONS (pfile)->objc)
2580 sprintf (directive, " __OBJC__ 1");
2581 output_line_command (pfile, 0, same_file);
2582 pass_thru_directive (directive, &directive[strlen (directive)],
2583 pfile, dp);
2588 /* Return 1 iff a token ending in C1 followed directly by a token C2
2589 could cause mis-tokenization. */
2591 static int
2592 unsafe_chars (c1, c2)
2593 int c1, c2;
2595 switch (c1)
2597 case '+': case '-':
2598 if (c2 == c1 || c2 == '=')
2599 return 1;
2600 goto letter;
2601 case '.':
2602 case '0': case '1': case '2': case '3': case '4':
2603 case '5': case '6': case '7': case '8': case '9':
2604 case 'e': case 'E':
2605 if (c2 == '-' || c2 == '+')
2606 return 1; /* could extend a pre-processing number */
2607 goto letter;
2608 case 'L':
2609 if (c2 == '\'' || c2 == '\"')
2610 return 1; /* Could turn into L"xxx" or L'xxx'. */
2611 goto letter;
2612 letter:
2613 case '_':
2614 case 'a': case 'b': case 'c': case 'd': case 'f':
2615 case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
2616 case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
2617 case 's': case 't': case 'u': case 'v': case 'w': case 'x':
2618 case 'y': case 'z':
2619 case 'A': case 'B': case 'C': case 'D': case 'F':
2620 case 'G': case 'H': case 'I': case 'J': case 'K':
2621 case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
2622 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
2623 case 'Y': case 'Z':
2624 /* We're in the middle of either a name or a pre-processing number. */
2625 return (is_idchar[c2] || c2 == '.');
2626 case '<': case '>': case '!': case '%': case '#': case ':':
2627 case '^': case '&': case '|': case '*': case '/': case '=':
2628 return (c2 == c1 || c2 == '=');
2630 return 0;
2633 /* Expand a macro call.
2634 HP points to the symbol that is the macro being called.
2635 Put the result of expansion onto the input stack
2636 so that subsequent input by our caller will use it.
2638 If macro wants arguments, caller has already verified that
2639 an argument list follows; arguments come from the input stack. */
2641 static void
2642 macroexpand (pfile, hp)
2643 cpp_reader *pfile;
2644 HASHNODE *hp;
2646 int nargs;
2647 DEFINITION *defn = hp->value.defn;
2648 register U_CHAR *xbuf;
2649 long start_line, start_column;
2650 int xbuf_len;
2651 struct argdata *args;
2652 long old_written = CPP_WRITTEN (pfile);
2653 #if 0
2654 int start_line = instack[indepth].lineno;
2655 #endif
2656 int rest_args, rest_zero;
2657 register int i;
2659 #if 0
2660 CHECK_DEPTH (return;);
2661 #endif
2663 #if 0
2664 /* This macro is being used inside a #if, which means it must be */
2665 /* recorded as a precondition. */
2666 if (pcp_inside_if && pcp_outfile && defn->predefined)
2667 dump_single_macro (hp, pcp_outfile);
2668 #endif
2670 pfile->output_escapes++;
2671 cpp_buf_line_and_col (cpp_file_buffer (pfile), &start_line, &start_column);
2673 nargs = defn->nargs;
2675 if (nargs >= 0)
2677 enum cpp_token token;
2679 args = (struct argdata *) alloca ((nargs + 1) * sizeof (struct argdata));
2681 for (i = 0; i < nargs; i++)
2683 args[i].raw = args[i].expanded = 0;
2684 args[i].raw_length = 0;
2685 args[i].expand_length = args[i].stringified_length = -1;
2686 args[i].use_count = 0;
2689 /* Parse all the macro args that are supplied. I counts them.
2690 The first NARGS args are stored in ARGS.
2691 The rest are discarded. If rest_args is set then we assume
2692 macarg absorbed the rest of the args. */
2693 i = 0;
2694 rest_args = 0;
2695 rest_args = 0;
2696 FORWARD(1); /* Discard the open-parenthesis before the first arg. */
2699 if (rest_args)
2700 continue;
2701 if (i < nargs || (nargs == 0 && i == 0))
2703 /* if we are working on last arg which absorbs rest of args... */
2704 if (i == nargs - 1 && defn->rest_args)
2705 rest_args = 1;
2706 args[i].raw = CPP_WRITTEN (pfile);
2707 token = macarg (pfile, rest_args);
2708 args[i].raw_length = CPP_WRITTEN (pfile) - args[i].raw;
2709 args[i].newlines = 0; /* FIXME */
2711 else
2712 token = macarg (pfile, 0);
2713 if (token == CPP_EOF || token == CPP_POP)
2715 cpp_error_with_line (pfile, start_line, start_column,
2716 "unterminated macro call");
2717 return;
2719 i++;
2720 } while (token == CPP_COMMA);
2722 /* If we got one arg but it was just whitespace, call that 0 args. */
2723 if (i == 1)
2725 register U_CHAR *bp = ARG_BASE + args[0].raw;
2726 register U_CHAR *lim = bp + args[0].raw_length;
2727 /* cpp.texi says for foo ( ) we provide one argument.
2728 However, if foo wants just 0 arguments, treat this as 0. */
2729 if (nargs == 0)
2730 while (bp != lim && is_space[*bp]) bp++;
2731 if (bp == lim)
2732 i = 0;
2735 /* Don't output an error message if we have already output one for
2736 a parse error above. */
2737 rest_zero = 0;
2738 if (nargs == 0 && i > 0)
2740 cpp_error (pfile, "arguments given to macro `%s'", hp->name);
2742 else if (i < nargs)
2744 /* traditional C allows foo() if foo wants one argument. */
2745 if (nargs == 1 && i == 0 && CPP_TRADITIONAL (pfile))
2747 /* the rest args token is allowed to absorb 0 tokens */
2748 else if (i == nargs - 1 && defn->rest_args)
2749 rest_zero = 1;
2750 else if (i == 0)
2751 cpp_error (pfile, "macro `%s' used without args", hp->name);
2752 else if (i == 1)
2753 cpp_error (pfile, "macro `%s' used with just one arg", hp->name);
2754 else
2755 cpp_error (pfile, "macro `%s' used with only %d args",
2756 hp->name, i);
2758 else if (i > nargs)
2760 cpp_error (pfile,
2761 "macro `%s' used with too many (%d) args", hp->name, i);
2765 /* If macro wants zero args, we parsed the arglist for checking only.
2766 Read directly from the macro definition. */
2767 if (nargs <= 0)
2769 xbuf = defn->expansion;
2770 xbuf_len = defn->length;
2772 else
2774 register U_CHAR *exp = defn->expansion;
2775 register int offset; /* offset in expansion,
2776 copied a piece at a time */
2777 register int totlen; /* total amount of exp buffer filled so far */
2779 register struct reflist *ap, *last_ap;
2781 /* Macro really takes args. Compute the expansion of this call. */
2783 /* Compute length in characters of the macro's expansion.
2784 Also count number of times each arg is used. */
2785 xbuf_len = defn->length;
2786 for (ap = defn->pattern; ap != NULL; ap = ap->next)
2788 if (ap->stringify)
2790 register struct argdata *arg = &args[ap->argno];
2791 /* Stringify it it hasn't already been */
2792 if (arg->stringified_length < 0)
2794 int arglen = arg->raw_length;
2795 int escaped = 0;
2796 int in_string = 0;
2797 int c;
2798 /* Initially need_space is -1. Otherwise, 1 means the
2799 previous character was a space, but we suppressed it;
2800 0 means the previous character was a non-space. */
2801 int need_space = -1;
2802 i = 0;
2803 arg->stringified = CPP_WRITTEN (pfile);
2804 if (!CPP_TRADITIONAL (pfile))
2805 CPP_PUTC (pfile, '\"'); /* insert beginning quote */
2806 for (; i < arglen; i++)
2808 c = (ARG_BASE + arg->raw)[i];
2810 if (! in_string)
2812 /* Internal sequences of whitespace are replaced by
2813 one space except within an string or char token.*/
2814 if (is_space[c])
2816 if (CPP_WRITTEN (pfile) > arg->stringified
2817 && (CPP_PWRITTEN (pfile))[-1] == '@')
2819 /* "@ " escape markers are removed */
2820 CPP_ADJUST_WRITTEN (pfile, -1);
2821 continue;
2823 if (need_space == 0)
2824 need_space = 1;
2825 continue;
2827 else if (need_space > 0)
2828 CPP_PUTC (pfile, ' ');
2829 need_space = 0;
2832 if (escaped)
2833 escaped = 0;
2834 else
2836 if (c == '\\')
2837 escaped = 1;
2838 if (in_string)
2840 if (c == in_string)
2841 in_string = 0;
2843 else if (c == '\"' || c == '\'')
2844 in_string = c;
2847 /* Escape these chars */
2848 if (c == '\"' || (in_string && c == '\\'))
2849 CPP_PUTC (pfile, '\\');
2850 if (isprint (c))
2851 CPP_PUTC (pfile, c);
2852 else
2854 CPP_RESERVE (pfile, 4);
2855 sprintf (CPP_PWRITTEN (pfile), "\\%03o",
2856 (unsigned int) c);
2857 CPP_ADJUST_WRITTEN (pfile, 4);
2860 if (!CPP_TRADITIONAL (pfile))
2861 CPP_PUTC (pfile, '\"'); /* insert ending quote */
2862 arg->stringified_length
2863 = CPP_WRITTEN (pfile) - arg->stringified;
2865 xbuf_len += args[ap->argno].stringified_length;
2867 else if (ap->raw_before || ap->raw_after || CPP_TRADITIONAL (pfile))
2868 /* Add 4 for two newline-space markers to prevent
2869 token concatenation. */
2870 xbuf_len += args[ap->argno].raw_length + 4;
2871 else
2873 /* We have an ordinary (expanded) occurrence of the arg.
2874 So compute its expansion, if we have not already. */
2875 if (args[ap->argno].expand_length < 0)
2877 args[ap->argno].expanded = CPP_WRITTEN (pfile);
2878 cpp_expand_to_buffer (pfile,
2879 ARG_BASE + args[ap->argno].raw,
2880 args[ap->argno].raw_length);
2882 args[ap->argno].expand_length
2883 = CPP_WRITTEN (pfile) - args[ap->argno].expanded;
2886 /* Add 4 for two newline-space markers to prevent
2887 token concatenation. */
2888 xbuf_len += args[ap->argno].expand_length + 4;
2890 if (args[ap->argno].use_count < 10)
2891 args[ap->argno].use_count++;
2894 xbuf = (U_CHAR *) xmalloc (xbuf_len + 1);
2896 /* Generate in XBUF the complete expansion
2897 with arguments substituted in.
2898 TOTLEN is the total size generated so far.
2899 OFFSET is the index in the definition
2900 of where we are copying from. */
2901 offset = totlen = 0;
2902 for (last_ap = NULL, ap = defn->pattern; ap != NULL;
2903 last_ap = ap, ap = ap->next)
2905 register struct argdata *arg = &args[ap->argno];
2906 int count_before = totlen;
2908 /* Add chars to XBUF. */
2909 for (i = 0; i < ap->nchars; i++, offset++)
2910 xbuf[totlen++] = exp[offset];
2912 /* If followed by an empty rest arg with concatenation,
2913 delete the last run of nonwhite chars. */
2914 if (rest_zero && totlen > count_before
2915 && ((ap->rest_args && ap->raw_before)
2916 || (last_ap != NULL && last_ap->rest_args
2917 && last_ap->raw_after)))
2919 /* Delete final whitespace. */
2920 while (totlen > count_before && is_space[xbuf[totlen - 1]])
2921 totlen--;
2923 /* Delete the nonwhites before them. */
2924 while (totlen > count_before && ! is_space[xbuf[totlen - 1]])
2925 totlen--;
2928 if (ap->stringify != 0)
2930 bcopy (ARG_BASE + arg->stringified,
2931 xbuf + totlen, arg->stringified_length);
2932 totlen += arg->stringified_length;
2934 else if (ap->raw_before || ap->raw_after || CPP_TRADITIONAL (pfile))
2936 U_CHAR *p1 = ARG_BASE + arg->raw;
2937 U_CHAR *l1 = p1 + arg->raw_length;
2938 if (ap->raw_before)
2940 while (p1 != l1 && is_space[*p1]) p1++;
2941 while (p1 != l1 && is_idchar[*p1])
2942 xbuf[totlen++] = *p1++;
2944 if (ap->raw_after)
2946 /* Arg is concatenated after: delete trailing whitespace,
2947 whitespace markers, and no-reexpansion markers. */
2948 while (p1 != l1)
2950 if (is_space[l1[-1]]) l1--;
2951 else if (l1[-1] == '-')
2953 U_CHAR *p2 = l1 - 1;
2954 /* If a `-' is preceded by an odd number of newlines then it
2955 and the last newline are a no-reexpansion marker. */
2956 while (p2 != p1 && p2[-1] == '\n') p2--;
2957 if ((l1 - 1 - p2) & 1) {
2958 l1 -= 2;
2960 else break;
2962 else break;
2966 bcopy (p1, xbuf + totlen, l1 - p1);
2967 totlen += l1 - p1;
2969 else
2971 U_CHAR *expanded = ARG_BASE + arg->expanded;
2972 if (!ap->raw_before && totlen > 0 && arg->expand_length
2973 && !CPP_TRADITIONAL(pfile)
2974 && unsafe_chars (xbuf[totlen-1], expanded[0]))
2976 xbuf[totlen++] = '@';
2977 xbuf[totlen++] = ' ';
2980 bcopy (expanded, xbuf + totlen, arg->expand_length);
2981 totlen += arg->expand_length;
2983 if (!ap->raw_after && totlen > 0 && offset < defn->length
2984 && !CPP_TRADITIONAL(pfile)
2985 && unsafe_chars (xbuf[totlen-1], exp[offset]))
2987 xbuf[totlen++] = '@';
2988 xbuf[totlen++] = ' ';
2991 /* If a macro argument with newlines is used multiple times,
2992 then only expand the newlines once. This avoids creating
2993 output lines which don't correspond to any input line,
2994 which confuses gdb and gcov. */
2995 if (arg->use_count > 1 && arg->newlines > 0)
2997 /* Don't bother doing change_newlines for subsequent
2998 uses of arg. */
2999 arg->use_count = 1;
3000 arg->expand_length
3001 = change_newlines (expanded, arg->expand_length);
3005 if (totlen > xbuf_len)
3006 abort ();
3009 /* if there is anything left of the definition
3010 after handling the arg list, copy that in too. */
3012 for (i = offset; i < defn->length; i++)
3014 /* if we've reached the end of the macro */
3015 if (exp[i] == ')')
3016 rest_zero = 0;
3017 if (! (rest_zero && last_ap != NULL && last_ap->rest_args
3018 && last_ap->raw_after))
3019 xbuf[totlen++] = exp[i];
3022 xbuf[totlen] = 0;
3023 xbuf_len = totlen;
3027 pfile->output_escapes--;
3029 /* Now put the expansion on the input stack
3030 so our caller will commence reading from it. */
3031 push_macro_expansion (pfile, xbuf, xbuf_len, hp);
3032 CPP_BUFFER (pfile)->has_escapes = 1;
3034 /* Pop the space we've used in the token_buffer for argument expansion. */
3035 CPP_SET_WRITTEN (pfile, old_written);
3037 /* Recursive macro use sometimes works traditionally.
3038 #define foo(x,y) bar (x (y,0), y)
3039 foo (foo, baz) */
3041 if (!CPP_TRADITIONAL (pfile))
3042 hp->type = T_DISABLED;
3045 static void
3046 push_macro_expansion (pfile, xbuf, xbuf_len, hp)
3047 cpp_reader *pfile;
3048 register U_CHAR *xbuf;
3049 int xbuf_len;
3050 HASHNODE *hp;
3052 register cpp_buffer *mbuf = cpp_push_buffer (pfile, xbuf, xbuf_len);
3053 mbuf->cleanup = macro_cleanup;
3054 mbuf->data = hp;
3056 /* The first chars of the expansion should be a "@ " added by
3057 collect_expansion. This is to prevent accidental token-pasting
3058 between the text preceding the macro invocation, and the macro
3059 expansion text.
3061 We would like to avoid adding unneeded spaces (for the sake of
3062 tools that use cpp, such as imake). In some common cases we can
3063 tell that it is safe to omit the space.
3065 The character before the macro invocation cannot have been an
3066 idchar (or else it would have been pasted with the idchars of
3067 the macro name). Therefore, if the first non-space character
3068 of the expansion is an idchar, we do not need the extra space
3069 to prevent token pasting.
3071 Also, we don't need the extra space if the first char is '(',
3072 or some other (less common) characters. */
3074 if (xbuf[0] == '@' && xbuf[1] == ' '
3075 && (is_idchar[xbuf[2]] || xbuf[2] == '(' || xbuf[2] == '\''
3076 || xbuf[2] == '\"'))
3077 mbuf->cur += 2;
3080 /* Like cpp_get_token, except that it does not read past end-of-line.
3081 Also, horizontal space is skipped, and macros are popped. */
3083 static enum cpp_token
3084 get_directive_token (pfile)
3085 cpp_reader *pfile;
3087 for (;;)
3089 long old_written = CPP_WRITTEN (pfile);
3090 enum cpp_token token;
3091 cpp_skip_hspace (pfile);
3092 if (PEEKC () == '\n')
3093 return CPP_VSPACE;
3094 token = cpp_get_token (pfile);
3095 switch (token)
3097 case CPP_POP:
3098 if (! CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
3099 return token;
3100 /* ... else fall though ... */
3101 case CPP_HSPACE: case CPP_COMMENT:
3102 CPP_SET_WRITTEN (pfile, old_written);
3103 break;
3104 default:
3105 return token;
3110 /* Handle #include and #import.
3111 This function expects to see "fname" or <fname> on the input.
3113 The input is normally in part of the output_buffer following
3114 CPP_WRITTEN, and will get overwritten by output_line_command.
3115 I.e. in input file specification has been popped by handle_directive.
3116 This is safe. */
3118 static int
3119 do_include (pfile, keyword, unused1, unused2)
3120 cpp_reader *pfile;
3121 struct directive *keyword;
3122 U_CHAR *unused1, *unused2;
3124 int importing = (keyword->type == T_IMPORT);
3125 int skip_dirs = (keyword->type == T_INCLUDE_NEXT);
3126 char *fname; /* Dynamically allocated fname buffer */
3127 char *pcftry;
3128 char *pcfname;
3129 U_CHAR *fbeg, *fend; /* Beginning and end of fname */
3130 enum cpp_token token;
3132 /* Chain of dirs to search */
3133 struct file_name_list *search_start = CPP_OPTIONS (pfile)->include;
3134 struct file_name_list dsp[1]; /* First in chain, if #include "..." */
3135 struct file_name_list *searchptr = 0;
3136 long old_written = CPP_WRITTEN (pfile);
3138 int flen;
3140 int f; /* file number */
3142 int retried = 0; /* Have already tried macro
3143 expanding the include line*/
3144 int angle_brackets = 0; /* 0 for "...", 1 for <...> */
3145 int pcf = -1;
3146 char *pcfbuf;
3147 char *pcfbuflimit;
3148 int pcfnum;
3149 f= -1; /* JF we iz paranoid! */
3151 if (importing && CPP_OPTIONS (pfile)->warn_import
3152 && !CPP_OPTIONS (pfile)->inhibit_warnings
3153 && !CPP_BUFFER (pfile)->system_header_p && !pfile->import_warning)
3155 pfile->import_warning = 1;
3156 cpp_warning (pfile, "using `#import' is not recommended");
3157 fprintf (stderr, "The fact that a certain header file need not be processed more than once\n");
3158 fprintf (stderr, "should be indicated in the header file, not where it is used.\n");
3159 fprintf (stderr, "The best way to do this is with a conditional of this form:\n\n");
3160 fprintf (stderr, " #ifndef _FOO_H_INCLUDED\n");
3161 fprintf (stderr, " #define _FOO_H_INCLUDED\n");
3162 fprintf (stderr, " ... <real contents of file> ...\n");
3163 fprintf (stderr, " #endif /* Not _FOO_H_INCLUDED */\n\n");
3164 fprintf (stderr, "Then users can use `#include' any number of times.\n");
3165 fprintf (stderr, "GNU C automatically avoids processing the file more than once\n");
3166 fprintf (stderr, "when it is equipped with such a conditional.\n");
3169 pfile->parsing_include_directive++;
3170 token = get_directive_token (pfile);
3171 pfile->parsing_include_directive--;
3173 if (token == CPP_STRING)
3175 /* FIXME - check no trailing garbage */
3176 fbeg = pfile->token_buffer + old_written + 1;
3177 fend = CPP_PWRITTEN (pfile) - 1;
3178 if (fbeg[-1] == '<')
3180 angle_brackets = 1;
3181 /* If -I-, start with the first -I dir after the -I-. */
3182 if (CPP_OPTIONS (pfile)->first_bracket_include)
3183 search_start = CPP_OPTIONS (pfile)->first_bracket_include;
3185 /* If -I- was specified, don't search current dir, only spec'd ones. */
3186 else if (! CPP_OPTIONS (pfile)->ignore_srcdir)
3188 cpp_buffer *fp;
3189 /* We have "filename". Figure out directory this source
3190 file is coming from and put it on the front of the list. */
3192 for (fp = CPP_BUFFER (pfile); fp != NULL; fp = CPP_PREV_BUFFER (fp))
3194 int n;
3195 char *ep,*nam;
3197 if ((nam = fp->nominal_fname) != NULL)
3199 /* Found a named file. Figure out dir of the file,
3200 and put it in front of the search list. */
3201 dsp[0].next = search_start;
3202 search_start = dsp;
3203 #ifndef VMS
3204 ep = rindex (nam, '/');
3205 #else /* VMS */
3206 ep = rindex (nam, ']');
3207 if (ep == NULL) ep = rindex (nam, '>');
3208 if (ep == NULL) ep = rindex (nam, ':');
3209 if (ep != NULL) ep++;
3210 #endif /* VMS */
3211 if (ep != NULL)
3213 n = ep - nam;
3214 dsp[0].fname = (char *) alloca (n + 1);
3215 strncpy (dsp[0].fname, nam, n);
3216 dsp[0].fname[n] = '\0';
3217 if (n + INCLUDE_LEN_FUDGE > pfile->max_include_len)
3218 pfile->max_include_len = n + INCLUDE_LEN_FUDGE;
3220 else
3222 dsp[0].fname = 0; /* Current directory */
3224 dsp[0].got_name_map = 0;
3225 break;
3230 #ifdef VMS
3231 else if (token == CPP_NAME)
3234 * Support '#include xyz' like VAX-C to allow for easy use of all the
3235 * decwindow include files. It defaults to '#include <xyz.h>' (so the
3236 * code from case '<' is repeated here) and generates a warning.
3238 cpp_warning (pfile,
3239 "VAX-C-style include specification found, use '#include <filename.h>' !");
3240 angle_brackets = 1;
3241 /* If -I-, start with the first -I dir after the -I-. */
3242 if (CPP_OPTIONS (pfile)->first_bracket_include)
3243 search_start = CPP_OPTIONS (pfile)->first_bracket_include;
3244 fbeg = pfile->token_buffer + old_written;
3245 fend = CPP_PWRITTEN (pfile);
3247 #endif
3248 else
3250 cpp_error (pfile,
3251 "`#%s' expects \"FILENAME\" or <FILENAME>", keyword->name);
3252 CPP_SET_WRITTEN (pfile, old_written);
3253 skip_rest_of_line (pfile);
3254 return 0;
3257 *fend = 0;
3259 token = get_directive_token (pfile);
3260 if (token != CPP_VSPACE)
3262 cpp_error (pfile, "junk at end of `#include'");
3263 while (token != CPP_VSPACE && token != CPP_EOF && token != CPP_POP)
3264 token = get_directive_token (pfile);
3267 /* For #include_next, skip in the search path
3268 past the dir in which the containing file was found. */
3269 if (skip_dirs)
3271 cpp_buffer *fp;
3272 for (fp = CPP_BUFFER (pfile); fp != NULL; fp = CPP_PREV_BUFFER (fp))
3273 if (fp->fname != NULL)
3275 /* fp->dir is null if the containing file was specified with
3276 an absolute file name. In that case, don't skip anything. */
3277 if (fp->dir == SELF_DIR_DUMMY)
3278 search_start = CPP_OPTIONS (pfile)->include;
3279 else if (fp->dir)
3280 search_start = fp->dir->next;
3281 break;
3285 CPP_SET_WRITTEN (pfile, old_written);
3287 flen = fend - fbeg;
3289 if (flen == 0)
3291 cpp_error (pfile, "empty file name in `#%s'", keyword->name);
3292 return 0;
3295 /* Allocate this permanently, because it gets stored in the definitions
3296 of macros. */
3297 fname = (char *) xmalloc (pfile->max_include_len + flen + 4);
3298 /* + 2 above for slash and terminating null. */
3299 /* + 2 added for '.h' on VMS (to support '#include filename') */
3301 /* If specified file name is absolute, just open it. */
3303 if (*fbeg == '/') {
3304 strncpy (fname, fbeg, flen);
3305 fname[flen] = 0;
3306 if (redundant_include_p (pfile, fname))
3307 return 0;
3308 if (importing)
3309 f = lookup_import (pfile, fname, NULL_PTR);
3310 else
3311 f = open_include_file (pfile, fname, NULL_PTR);
3312 if (f == -2)
3313 return 0; /* Already included this file */
3314 } else {
3315 /* Search directory path, trying to open the file.
3316 Copy each filename tried into FNAME. */
3318 for (searchptr = search_start; searchptr; searchptr = searchptr->next) {
3319 if (searchptr->fname) {
3320 /* The empty string in a search path is ignored.
3321 This makes it possible to turn off entirely
3322 a standard piece of the list. */
3323 if (searchptr->fname[0] == 0)
3324 continue;
3325 strcpy (fname, searchptr->fname);
3326 strcat (fname, "/");
3327 fname[strlen (fname) + flen] = 0;
3328 } else {
3329 fname[0] = 0;
3331 strncat (fname, fbeg, flen);
3332 #ifdef VMS
3333 /* Change this 1/2 Unix 1/2 VMS file specification into a
3334 full VMS file specification */
3335 if (searchptr->fname && (searchptr->fname[0] != 0)) {
3336 /* Fix up the filename */
3337 hack_vms_include_specification (fname);
3338 } else {
3339 /* This is a normal VMS filespec, so use it unchanged. */
3340 strncpy (fname, fbeg, flen);
3341 fname[flen] = 0;
3342 /* if it's '#include filename', add the missing .h */
3343 if (index(fname,'.')==NULL) {
3344 strcat (fname, ".h");
3347 #endif /* VMS */
3348 /* ??? There are currently 3 separate mechanisms for avoiding processing
3349 of redundant include files: #import, #pragma once, and
3350 redundant_include_p. It would be nice if they were unified. */
3351 if (redundant_include_p (pfile, fname))
3352 return 0;
3353 if (importing)
3354 f = lookup_import (pfile, fname, searchptr);
3355 else
3356 f = open_include_file (pfile, fname, searchptr);
3357 if (f == -2)
3358 return 0; /* Already included this file */
3359 #ifdef EACCES
3360 else if (f == -1 && errno == EACCES)
3361 cpp_warning (pfile, "Header file %s exists, but is not readable",
3362 fname);
3363 #endif
3364 if (f >= 0)
3365 break;
3369 if (f < 0)
3371 /* A file that was not found. */
3372 strncpy (fname, fbeg, flen);
3373 fname[flen] = 0;
3374 /* If generating dependencies and -MG was specified, we assume missing
3375 files are leaf files, living in the same directory as the source file
3376 or other similar place; these missing files may be generated from
3377 other files and may not exist yet (eg: y.tab.h). */
3379 if (CPP_OPTIONS(pfile)->print_deps_missing_files
3380 && CPP_PRINT_DEPS (pfile)
3381 > (angle_brackets || (pfile->system_include_depth > 0)))
3383 /* If it was requested as a system header file,
3384 then assume it belongs in the first place to look for such. */
3385 if (angle_brackets)
3387 for (searchptr = search_start; searchptr;
3388 searchptr = searchptr->next)
3390 if (searchptr->fname)
3392 char *p;
3394 if (searchptr->fname[0] == 0)
3395 continue;
3396 p = (char *) alloca (strlen (searchptr->fname)
3397 + strlen (fname) + 2);
3398 strcpy (p, searchptr->fname);
3399 strcat (p, "/");
3400 strcat (p, fname);
3401 deps_output (pfile, p, ' ');
3402 break;
3406 else
3408 /* Otherwise, omit the directory, as if the file existed
3409 in the directory with the source. */
3410 deps_output (pfile, fname, ' ');
3413 /* If -M was specified, and this header file won't be added to the
3414 dependency list, then don't count this as an error, because we can
3415 still produce correct output. Otherwise, we can't produce correct
3416 output, because there may be dependencies we need inside the missing
3417 file, and we don't know what directory this missing file exists in.*/
3418 else if (CPP_PRINT_DEPS (pfile)
3419 && (CPP_PRINT_DEPS (pfile)
3420 <= (angle_brackets || (pfile->system_include_depth > 0))))
3421 cpp_warning (pfile, "No include path in which to find %s", fname);
3422 else if (search_start)
3423 cpp_error_from_errno (pfile, fname);
3424 else
3425 cpp_error (pfile, "No include path in which to find %s", fname);
3427 else {
3428 /* Check to see if this include file is a once-only include file.
3429 If so, give up. */
3431 struct file_name_list* ptr;
3433 for (ptr = pfile->dont_repeat_files; ptr; ptr = ptr->next) {
3434 if (!strcmp (ptr->fname, fname)) {
3435 close (f);
3436 return 0; /* This file was once'd. */
3440 for (ptr = pfile->all_include_files; ptr; ptr = ptr->next) {
3441 if (!strcmp (ptr->fname, fname))
3442 break; /* This file was included before. */
3445 if (ptr == 0) {
3446 /* This is the first time for this file. */
3447 /* Add it to list of files included. */
3449 ptr = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
3450 ptr->control_macro = 0;
3451 ptr->c_system_include_path = 0;
3452 ptr->next = pfile->all_include_files;
3453 pfile->all_include_files = ptr;
3454 ptr->fname = savestring (fname);
3455 ptr->got_name_map = 0;
3457 /* For -M, add this file to the dependencies. */
3458 if (CPP_PRINT_DEPS (pfile)
3459 > (angle_brackets || (pfile->system_include_depth > 0)))
3460 deps_output (pfile, fname, ' ');
3463 /* Handle -H option. */
3464 if (CPP_OPTIONS(pfile)->print_include_names)
3466 cpp_buffer *buf = CPP_BUFFER (pfile);
3467 while ((buf = CPP_PREV_BUFFER (buf)) != NULL)
3468 putc ('.', stderr);
3469 fprintf (stderr, "%s\n", fname);
3472 if (angle_brackets)
3473 pfile->system_include_depth++;
3475 /* Actually process the file. */
3477 /* Record file on "seen" list for #import. */
3478 add_import (pfile, f, fname);
3480 pcftry = (char *) alloca (strlen (fname) + 30);
3481 pcfbuf = 0;
3482 pcfnum = 0;
3484 #if 0
3485 if (!no_precomp)
3487 struct stat stat_f;
3489 fstat (f, &stat_f);
3491 do {
3492 sprintf (pcftry, "%s%d", fname, pcfnum++);
3494 pcf = open (pcftry, O_RDONLY, 0666);
3495 if (pcf != -1)
3497 struct stat s;
3499 fstat (pcf, &s);
3500 if (bcmp ((char *) &stat_f.st_ino, (char *) &s.st_ino,
3501 sizeof (s.st_ino))
3502 || stat_f.st_dev != s.st_dev)
3504 pcfbuf = check_precompiled (pcf, fname, &pcfbuflimit);
3505 /* Don't need it any more. */
3506 close (pcf);
3508 else
3510 /* Don't need it at all. */
3511 close (pcf);
3512 break;
3515 } while (pcf != -1 && !pcfbuf);
3517 #endif
3519 /* Actually process the file */
3520 cpp_push_buffer (pfile, NULL, 0);
3521 if (finclude (pfile, f, fname, is_system_include (pfile, fname),
3522 searchptr != dsp ? searchptr : SELF_DIR_DUMMY))
3524 output_line_command (pfile, 0, enter_file);
3525 pfile->only_seen_white = 2;
3528 if (angle_brackets)
3529 pfile->system_include_depth--;
3531 return 0;
3534 /* Return nonzero if there is no need to include file NAME
3535 because it has already been included and it contains a conditional
3536 to make a repeated include do nothing. */
3538 static int
3539 redundant_include_p (pfile, name)
3540 cpp_reader *pfile;
3541 char *name;
3543 struct file_name_list *l = pfile->all_include_files;
3544 for (; l; l = l->next)
3545 if (! strcmp (name, l->fname)
3546 && l->control_macro
3547 && cpp_lookup (pfile, l->control_macro, -1, -1))
3548 return 1;
3549 return 0;
3552 /* Return nonzero if the given FILENAME is an absolute pathname which
3553 designates a file within one of the known "system" include file
3554 directories. We assume here that if the given FILENAME looks like
3555 it is the name of a file which resides either directly in a "system"
3556 include file directory, or within any subdirectory thereof, then the
3557 given file must be a "system" include file. This function tells us
3558 if we should suppress pedantic errors/warnings for the given FILENAME.
3560 The value is 2 if the file is a C-language system header file
3561 for which C++ should (on most systems) assume `extern "C"'. */
3563 static int
3564 is_system_include (pfile, filename)
3565 cpp_reader *pfile;
3566 register char *filename;
3568 struct file_name_list *searchptr;
3570 for (searchptr = CPP_OPTIONS (pfile)->first_system_include; searchptr;
3571 searchptr = searchptr->next)
3572 if (searchptr->fname) {
3573 register char *sys_dir = searchptr->fname;
3574 register unsigned length = strlen (sys_dir);
3576 if (! strncmp (sys_dir, filename, length) && filename[length] == '/')
3578 if (searchptr->c_system_include_path)
3579 return 2;
3580 else
3581 return 1;
3584 return 0;
3589 * Install a name in the assertion hash table.
3591 * If LEN is >= 0, it is the length of the name.
3592 * Otherwise, compute the length by scanning the entire name.
3594 * If HASH is >= 0, it is the precomputed hash code.
3595 * Otherwise, compute the hash code.
3597 static ASSERTION_HASHNODE *
3598 assertion_install (pfile, name, len, hash)
3599 cpp_reader *pfile;
3600 U_CHAR *name;
3601 int len;
3602 int hash;
3604 register ASSERTION_HASHNODE *hp;
3605 register int i, bucket;
3606 register U_CHAR *p, *q;
3608 i = sizeof (ASSERTION_HASHNODE) + len + 1;
3609 hp = (ASSERTION_HASHNODE *) xmalloc (i);
3610 bucket = hash;
3611 hp->bucket_hdr = &pfile->assertion_hashtab[bucket];
3612 hp->next = pfile->assertion_hashtab[bucket];
3613 pfile->assertion_hashtab[bucket] = hp;
3614 hp->prev = NULL;
3615 if (hp->next != NULL)
3616 hp->next->prev = hp;
3617 hp->length = len;
3618 hp->value = 0;
3619 hp->name = ((U_CHAR *) hp) + sizeof (ASSERTION_HASHNODE);
3620 p = hp->name;
3621 q = name;
3622 for (i = 0; i < len; i++)
3623 *p++ = *q++;
3624 hp->name[len] = 0;
3625 return hp;
3628 * find the most recent hash node for name name (ending with first
3629 * non-identifier char) installed by install
3631 * If LEN is >= 0, it is the length of the name.
3632 * Otherwise, compute the length by scanning the entire name.
3634 * If HASH is >= 0, it is the precomputed hash code.
3635 * Otherwise, compute the hash code.
3638 static ASSERTION_HASHNODE *
3639 assertion_lookup (pfile, name, len, hash)
3640 cpp_reader *pfile;
3641 U_CHAR *name;
3642 int len;
3643 int hash;
3645 register ASSERTION_HASHNODE *bucket;
3647 bucket = pfile->assertion_hashtab[hash];
3648 while (bucket) {
3649 if (bucket->length == len && strncmp (bucket->name, name, len) == 0)
3650 return bucket;
3651 bucket = bucket->next;
3653 return NULL;
3656 static void
3657 delete_assertion (hp)
3658 ASSERTION_HASHNODE *hp;
3660 struct tokenlist_list *tail;
3661 if (hp->prev != NULL)
3662 hp->prev->next = hp->next;
3663 if (hp->next != NULL)
3664 hp->next->prev = hp->prev;
3666 for (tail = hp->value; tail; )
3668 struct tokenlist_list *next = tail->next;
3669 free_token_list (tail->tokens);
3670 free (tail);
3671 tail = next;
3674 /* make sure that the bucket chain header that
3675 the deleted guy was on points to the right thing afterwards. */
3676 if (hp == *hp->bucket_hdr)
3677 *hp->bucket_hdr = hp->next;
3679 free (hp);
3682 /* Convert a character string literal into a nul-terminated string.
3683 The input string is [IN ... LIMIT).
3684 The result is placed in RESULT. RESULT can be the same as IN.
3685 The value returned in the end of the string written to RESULT,
3686 or NULL on error. */
3688 static U_CHAR*
3689 convert_string (pfile, result, in, limit, handle_escapes)
3690 cpp_reader *pfile;
3691 register U_CHAR *result, *in, *limit;
3692 int handle_escapes;
3694 U_CHAR c;
3695 c = *in++;
3696 if (c != '\"')
3697 return NULL;
3698 while (in < limit)
3700 U_CHAR c = *in++;
3701 switch (c)
3703 case '\0':
3704 return NULL;
3705 case '\"':
3706 limit = in;
3707 break;
3708 case '\\':
3709 if (handle_escapes)
3711 char *bpc = (char *) in;
3712 int i = (U_CHAR) cpp_parse_escape (pfile, &bpc);
3713 in = (U_CHAR *) bpc;
3714 if (i >= 0)
3715 *result++ = (U_CHAR)c;
3716 break;
3718 /* else fall through */
3719 default:
3720 *result++ = c;
3723 *result = 0;
3724 return result;
3728 * interpret #line command. Remembers previously seen fnames
3729 * in its very own hash table.
3731 #define FNAME_HASHSIZE 37
3733 static int
3734 do_line (pfile, keyword)
3735 cpp_reader *pfile;
3736 struct directive *keyword;
3738 cpp_buffer *ip = CPP_BUFFER (pfile);
3739 int new_lineno;
3740 long old_written = CPP_WRITTEN (pfile);
3741 enum file_change_code file_change = same_file;
3742 enum cpp_token token;
3743 int i;
3745 token = get_directive_token (pfile);
3747 if (token != CPP_NUMBER
3748 || !isdigit(pfile->token_buffer[old_written]))
3750 cpp_error (pfile, "invalid format `#line' command");
3751 goto bad_line_directive;
3754 /* The Newline at the end of this line remains to be processed.
3755 To put the next line at the specified line number,
3756 we must store a line number now that is one less. */
3757 new_lineno = atoi (pfile->token_buffer + old_written) - 1;
3758 CPP_SET_WRITTEN (pfile, old_written);
3760 /* NEW_LINENO is one less than the actual line number here. */
3761 if (CPP_PEDANTIC (pfile) && new_lineno < 0)
3762 cpp_pedwarn (pfile, "line number out of range in `#line' command");
3764 #if 0 /* #line 10"foo.c" is supposed to be allowed. */
3765 if (PEEKC() && !is_space[PEEKC()]) {
3766 cpp_error (pfile, "invalid format `#line' command");
3767 goto bad_line_directive;
3769 #endif
3771 token = get_directive_token (pfile);
3773 if (token == CPP_STRING) {
3774 U_CHAR *fname = pfile->token_buffer + old_written;
3775 U_CHAR *end_name;
3776 static HASHNODE *fname_table[FNAME_HASHSIZE];
3777 HASHNODE *hp, **hash_bucket;
3778 U_CHAR *p;
3779 long num_start;
3780 int fname_length;
3782 /* Turn the file name, which is a character string literal,
3783 into a null-terminated string. Do this in place. */
3784 end_name = convert_string (pfile, fname, fname, CPP_PWRITTEN (pfile), 1);
3785 if (end_name == NULL)
3787 cpp_error (pfile, "invalid format `#line' command");
3788 goto bad_line_directive;
3791 fname_length = end_name - fname;
3793 num_start = CPP_WRITTEN (pfile);
3794 token = get_directive_token (pfile);
3795 if (token != CPP_VSPACE && token != CPP_EOF && token != CPP_POP) {
3796 p = pfile->token_buffer + num_start;
3797 if (CPP_PEDANTIC (pfile))
3798 cpp_pedwarn (pfile, "garbage at end of `#line' command");
3800 if (token != CPP_NUMBER || *p < '0' || *p > '4' || p[1] != '\0')
3802 cpp_error (pfile, "invalid format `#line' command");
3803 goto bad_line_directive;
3805 if (*p == '1')
3806 file_change = enter_file;
3807 else if (*p == 2)
3808 file_change = leave_file;
3809 else if (*p == 3)
3810 ip->system_header_p = 1;
3811 else /* if (*p == 4) */
3812 ip->system_header_p = 2;
3814 CPP_SET_WRITTEN (pfile, num_start);
3815 token = get_directive_token (pfile);
3816 p = pfile->token_buffer + num_start;
3817 if (token == CPP_NUMBER && p[1] == '\0' && (*p == '3' || *p== '4')) {
3818 ip->system_header_p = *p == 3 ? 1 : 2;
3819 token = get_directive_token (pfile);
3821 if (token != CPP_VSPACE) {
3822 cpp_error (pfile, "invalid format `#line' command");
3823 goto bad_line_directive;
3827 hash_bucket =
3828 &fname_table[hashf (fname, fname_length, FNAME_HASHSIZE)];
3829 for (hp = *hash_bucket; hp != NULL; hp = hp->next)
3830 if (hp->length == fname_length &&
3831 strncmp (hp->value.cpval, fname, fname_length) == 0) {
3832 ip->nominal_fname = hp->value.cpval;
3833 break;
3835 if (hp == 0) {
3836 /* Didn't find it; cons up a new one. */
3837 hp = (HASHNODE *) xcalloc (1, sizeof (HASHNODE) + fname_length + 1);
3838 hp->next = *hash_bucket;
3839 *hash_bucket = hp;
3841 hp->length = fname_length;
3842 ip->nominal_fname = hp->value.cpval = ((char *) hp) + sizeof (HASHNODE);
3843 bcopy (fname, hp->value.cpval, fname_length);
3846 else if (token != CPP_VSPACE && token != CPP_EOF) {
3847 cpp_error (pfile, "invalid format `#line' command");
3848 goto bad_line_directive;
3851 ip->lineno = new_lineno;
3852 bad_line_directive:
3853 skip_rest_of_line (pfile);
3854 CPP_SET_WRITTEN (pfile, old_written);
3855 output_line_command (pfile, 0, file_change);
3856 return 0;
3860 * remove the definition of a symbol from the symbol table.
3861 * according to un*x /lib/cpp, it is not an error to undef
3862 * something that has no definitions, so it isn't one here either.
3865 static int
3866 do_undef (pfile, keyword, buf, limit)
3867 cpp_reader *pfile;
3868 struct directive *keyword;
3869 U_CHAR *buf, *limit;
3871 int sym_length;
3872 HASHNODE *hp;
3873 U_CHAR *orig_buf = buf;
3875 #if 0
3876 /* If this is a precompiler run (with -pcp) pass thru #undef commands. */
3877 if (pcp_outfile && keyword)
3878 pass_thru_directive (buf, limit, pfile, keyword);
3879 #endif
3881 SKIP_WHITE_SPACE (buf);
3882 sym_length = check_macro_name (pfile, buf, "macro");
3884 while ((hp = cpp_lookup (pfile, buf, sym_length, -1)) != NULL)
3886 /* If we are generating additional info for debugging (with -g) we
3887 need to pass through all effective #undef commands. */
3888 if (CPP_OPTIONS (pfile)->debug_output && keyword)
3889 pass_thru_directive (orig_buf, limit, pfile, keyword);
3890 if (hp->type != T_MACRO)
3891 cpp_warning (pfile, "undefining `%s'", hp->name);
3892 delete_macro (hp);
3895 if (CPP_PEDANTIC (pfile)) {
3896 buf += sym_length;
3897 SKIP_WHITE_SPACE (buf);
3898 if (buf != limit)
3899 cpp_pedwarn (pfile, "garbage after `#undef' directive");
3901 return 0;
3905 * Report an error detected by the program we are processing.
3906 * Use the text of the line in the error message.
3907 * (We use error because it prints the filename & line#.)
3910 static int
3911 do_error (pfile, keyword, buf, limit)
3912 cpp_reader *pfile;
3913 struct directive *keyword;
3914 U_CHAR *buf, *limit;
3916 int length = limit - buf;
3917 U_CHAR *copy = (U_CHAR *) xmalloc (length + 1);
3918 bcopy (buf, copy, length);
3919 copy[length] = 0;
3920 SKIP_WHITE_SPACE (copy);
3921 cpp_error (pfile, "#error %s", copy);
3922 return 0;
3926 * Report a warning detected by the program we are processing.
3927 * Use the text of the line in the warning message, then continue.
3928 * (We use error because it prints the filename & line#.)
3931 static int
3932 do_warning (pfile, keyword, buf, limit)
3933 cpp_reader *pfile;
3934 struct directive *keyword;
3935 U_CHAR *buf, *limit;
3937 int length = limit - buf;
3938 U_CHAR *copy = (U_CHAR *) xmalloc (length + 1);
3939 bcopy (buf, copy, length);
3940 copy[length] = 0;
3941 SKIP_WHITE_SPACE (copy);
3942 cpp_warning (pfile, "#warning %s", copy);
3943 return 0;
3946 /* Remember the name of the current file being read from so that we can
3947 avoid ever including it again. */
3949 static int
3950 do_once (pfile)
3951 cpp_reader *pfile;
3953 cpp_buffer *ip = NULL;
3954 struct file_name_list *new;
3956 for (ip = CPP_BUFFER (pfile); ; ip = CPP_PREV_BUFFER (ip))
3958 if (ip == NULL)
3959 return 0;
3960 if (ip->fname != NULL)
3961 break;
3965 new = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
3966 new->next = pfile->dont_repeat_files;
3967 pfile->dont_repeat_files = new;
3968 new->fname = savestring (ip->fname);
3969 new->control_macro = 0;
3970 new->got_name_map = 0;
3971 new->c_system_include_path = 0;
3973 return 0;
3976 /* #ident has already been copied to the output file, so just ignore it. */
3978 static int
3979 do_ident (pfile, keyword, buf, limit)
3980 cpp_reader *pfile;
3981 struct directive *keyword;
3982 U_CHAR *buf, *limit;
3984 /* long old_written = CPP_WRITTEN (pfile);*/
3985 int len;
3987 /* Allow #ident in system headers, since that's not user's fault. */
3988 if (CPP_PEDANTIC (pfile) && !CPP_BUFFER (pfile)->system_header_p)
3989 cpp_pedwarn (pfile, "ANSI C does not allow `#ident'");
3991 /* Leave rest of line to be read by later calls to cpp_get_token. */
3993 return 0;
3996 /* #pragma and its argument line have already been copied to the output file.
3997 Just check for some recognized pragmas that need validation here. */
3999 static int
4000 do_pragma (pfile, keyword, buf, limit)
4001 cpp_reader *pfile;
4002 struct directive *keyword;
4003 U_CHAR *buf, *limit;
4005 while (*buf == ' ' || *buf == '\t')
4006 buf++;
4007 if (!strncmp (buf, "once", 4)) {
4008 /* Allow #pragma once in system headers, since that's not the user's
4009 fault. */
4010 if (!CPP_BUFFER (pfile)->system_header_p)
4011 cpp_warning (pfile, "`#pragma once' is obsolete");
4012 do_once (pfile);
4015 if (!strncmp (buf, "implementation", 14)) {
4016 /* Be quiet about `#pragma implementation' for a file only if it hasn't
4017 been included yet. */
4018 struct file_name_list *ptr;
4019 U_CHAR *p = buf + 14, *fname, *inc_fname;
4020 int fname_len;
4021 SKIP_WHITE_SPACE (p);
4022 if (*p == '\n' || *p != '\"')
4023 return 0;
4025 fname = p + 1;
4026 p = (U_CHAR *) index (fname, '\"');
4027 fname_len = p != NULL ? p - fname : strlen (fname);
4029 for (ptr = pfile->all_include_files; ptr; ptr = ptr->next) {
4030 inc_fname = (U_CHAR *) rindex (ptr->fname, '/');
4031 inc_fname = inc_fname ? inc_fname + 1 : (U_CHAR *) ptr->fname;
4032 if (inc_fname && !strncmp (inc_fname, fname, fname_len))
4033 cpp_warning (pfile,
4034 "`#pragma implementation' for `%s' appears after file is included",
4035 fname);
4039 return 0;
4042 #if 0
4043 /* This was a fun hack, but #pragma seems to start to be useful.
4044 By failing to recognize it, we pass it through unchanged to cc1. */
4047 * the behavior of the #pragma directive is implementation defined.
4048 * this implementation defines it as follows.
4051 static int
4052 do_pragma ()
4054 close (0);
4055 if (open ("/dev/tty", O_RDONLY, 0666) != 0)
4056 goto nope;
4057 close (1);
4058 if (open ("/dev/tty", O_WRONLY, 0666) != 1)
4059 goto nope;
4060 execl ("/usr/games/hack", "#pragma", 0);
4061 execl ("/usr/games/rogue", "#pragma", 0);
4062 execl ("/usr/new/emacs", "-f", "hanoi", "9", "-kill", 0);
4063 execl ("/usr/local/emacs", "-f", "hanoi", "9", "-kill", 0);
4064 nope:
4065 fatal ("You are in a maze of twisty compiler features, all different");
4067 #endif
4069 /* Just ignore #sccs, on systems where we define it at all. */
4071 static int
4072 do_sccs (pfile, keyword, buf, limit)
4073 cpp_reader *pfile;
4074 struct directive *keyword;
4075 U_CHAR *buf, *limit;
4077 if (CPP_PEDANTIC (pfile))
4078 cpp_pedwarn (pfile, "ANSI C does not allow `#sccs'");
4079 return 0;
4083 * handle #if command by
4084 * 1) inserting special `defined' keyword into the hash table
4085 * that gets turned into 0 or 1 by special_symbol (thus,
4086 * if the luser has a symbol called `defined' already, it won't
4087 * work inside the #if command)
4088 * 2) rescan the input into a temporary output buffer
4089 * 3) pass the output buffer to the yacc parser and collect a value
4090 * 4) clean up the mess left from steps 1 and 2.
4091 * 5) call conditional_skip to skip til the next #endif (etc.),
4092 * or not, depending on the value from step 3.
4095 static int
4096 do_if (pfile, keyword, buf, limit)
4097 cpp_reader *pfile;
4098 struct directive *keyword;
4099 U_CHAR *buf, *limit;
4101 HOST_WIDE_INT value = eval_if_expression (pfile, buf, limit - buf);
4102 conditional_skip (pfile, value == 0, T_IF, NULL_PTR);
4103 return 0;
4107 * handle a #elif directive by not changing if_stack either.
4108 * see the comment above do_else.
4111 static int
4112 do_elif (pfile, keyword, buf, limit)
4113 cpp_reader *pfile;
4114 struct directive *keyword;
4115 U_CHAR *buf, *limit;
4117 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack) {
4118 cpp_error (pfile, "`#elif' not within a conditional");
4119 return 0;
4120 } else {
4121 if (pfile->if_stack->type != T_IF && pfile->if_stack->type != T_ELIF) {
4122 cpp_error (pfile, "`#elif' after `#else'");
4123 #if 0
4124 fprintf (stderr, " (matches line %d", pfile->if_stack->lineno);
4125 #endif
4126 if (pfile->if_stack->fname != NULL && CPP_BUFFER (pfile)->fname != NULL
4127 && strcmp (pfile->if_stack->fname,
4128 CPP_BUFFER (pfile)->nominal_fname) != 0)
4129 fprintf (stderr, ", file %s", pfile->if_stack->fname);
4130 fprintf (stderr, ")\n");
4132 pfile->if_stack->type = T_ELIF;
4135 if (pfile->if_stack->if_succeeded)
4136 skip_if_group (pfile, 0);
4137 else {
4138 HOST_WIDE_INT value = eval_if_expression (pfile, buf, limit - buf);
4139 if (value == 0)
4140 skip_if_group (pfile, 0);
4141 else {
4142 ++pfile->if_stack->if_succeeded; /* continue processing input */
4143 output_line_command (pfile, 1, same_file);
4146 return 0;
4150 * evaluate a #if expression in BUF, of length LENGTH,
4151 * then parse the result as a C expression and return the value as an int.
4153 static HOST_WIDE_INT
4154 eval_if_expression (pfile, buf, length)
4155 cpp_reader *pfile;
4156 U_CHAR *buf;
4157 int length;
4159 HASHNODE *save_defined;
4160 HOST_WIDE_INT value;
4161 long old_written = CPP_WRITTEN (pfile);
4163 save_defined = install ("defined", -1, T_SPEC_DEFINED, 0, 0, -1);
4164 pfile->pcp_inside_if = 1;
4166 value = cpp_parse_expr (pfile);
4167 pfile->pcp_inside_if = 0;
4168 delete_macro (save_defined); /* clean up special symbol */
4170 CPP_SET_WRITTEN (pfile, old_written); /* Pop */
4172 return value;
4176 * routine to handle ifdef/ifndef. Try to look up the symbol,
4177 * then do or don't skip to the #endif/#else/#elif depending
4178 * on what directive is actually being processed.
4181 static int
4182 do_xifdef (pfile, keyword, unused1, unused2)
4183 cpp_reader *pfile;
4184 struct directive *keyword;
4185 U_CHAR *unused1, *unused2;
4187 int skip;
4188 cpp_buffer *ip = CPP_BUFFER (pfile);
4189 U_CHAR* ident;
4190 int ident_length;
4191 enum cpp_token token;
4192 int start_of_file = 0;
4193 U_CHAR *control_macro = 0;
4194 int old_written = CPP_WRITTEN (pfile);
4196 /* Detect a #ifndef at start of file (not counting comments). */
4197 if (ip->fname != 0 && keyword->type == T_IFNDEF)
4198 start_of_file = pfile->only_seen_white == 2;
4200 pfile->no_macro_expand++;
4201 token = get_directive_token (pfile);
4202 pfile->no_macro_expand--;
4204 ident = pfile->token_buffer + old_written;
4205 ident_length = CPP_WRITTEN (pfile) - old_written;
4206 CPP_SET_WRITTEN (pfile, old_written); /* Pop */
4208 if (token == CPP_VSPACE || token == CPP_POP || token == CPP_EOF)
4210 skip = (keyword->type == T_IFDEF);
4211 if (! CPP_TRADITIONAL (pfile))
4212 cpp_pedwarn (pfile, "`#%s' with no argument", keyword->name);
4214 else if (token == CPP_NAME)
4216 HASHNODE *hp = cpp_lookup (pfile, ident, ident_length, -1);
4217 skip = (hp == NULL) ^ (keyword->type == T_IFNDEF);
4218 if (start_of_file && !skip)
4220 control_macro = (U_CHAR *) xmalloc (ident_length + 1);
4221 bcopy (ident, control_macro, ident_length + 1);
4224 else
4226 skip = (keyword->type == T_IFDEF);
4227 if (! CPP_TRADITIONAL (pfile))
4228 cpp_error (pfile, "`#%s' with invalid argument", keyword->name);
4231 if (!CPP_TRADITIONAL (pfile))
4232 { int c;
4233 cpp_skip_hspace (pfile);
4234 c = PEEKC ();
4235 if (c != EOF && c != '\n')
4236 cpp_pedwarn (pfile, "garbage at end of `#%s' argument", keyword->name);
4238 skip_rest_of_line (pfile);
4240 #if 0
4241 if (pcp_outfile) {
4242 /* Output a precondition for this macro. */
4243 if (hp && hp->value.defn->predefined)
4244 fprintf (pcp_outfile, "#define %s\n", hp->name);
4245 else {
4246 U_CHAR *cp = buf;
4247 fprintf (pcp_outfile, "#undef ");
4248 while (is_idchar[*cp]) /* Ick! */
4249 fputc (*cp++, pcp_outfile);
4250 putc ('\n', pcp_outfile);
4252 #endif
4254 conditional_skip (pfile, skip, T_IF, control_macro);
4255 return 0;
4258 /* Push TYPE on stack; then, if SKIP is nonzero, skip ahead.
4259 If this is a #ifndef starting at the beginning of a file,
4260 CONTROL_MACRO is the macro name tested by the #ifndef.
4261 Otherwise, CONTROL_MACRO is 0. */
4263 static void
4264 conditional_skip (pfile, skip, type, control_macro)
4265 cpp_reader *pfile;
4266 int skip;
4267 enum node_type type;
4268 U_CHAR *control_macro;
4270 IF_STACK_FRAME *temp;
4272 temp = (IF_STACK_FRAME *) xcalloc (1, sizeof (IF_STACK_FRAME));
4273 temp->fname = CPP_BUFFER (pfile)->nominal_fname;
4274 #if 0
4275 temp->lineno = CPP_BUFFER (pfile)->lineno;
4276 #endif
4277 temp->next = pfile->if_stack;
4278 temp->control_macro = control_macro;
4279 pfile->if_stack = temp;
4281 pfile->if_stack->type = type;
4283 if (skip != 0) {
4284 skip_if_group (pfile, 0);
4285 return;
4286 } else {
4287 ++pfile->if_stack->if_succeeded;
4288 output_line_command (pfile, 1, same_file);
4293 * skip to #endif, #else, or #elif. adjust line numbers, etc.
4294 * leaves input ptr at the sharp sign found.
4295 * If ANY is nonzero, return at next directive of any sort.
4297 static void
4298 skip_if_group (pfile, any)
4299 cpp_reader *pfile;
4300 int any;
4302 int c;
4303 int at_beg_of_line = 1;
4304 struct directive *kt;
4305 IF_STACK_FRAME *save_if_stack = pfile->if_stack; /* don't pop past here */
4306 #if 0
4307 U_CHAR *beg_of_line = bp;
4308 #endif
4309 register int ident_length;
4310 U_CHAR *ident, *after_ident;
4311 struct parse_marker line_start_mark;
4313 parse_set_mark (&line_start_mark, pfile);
4315 if (CPP_OPTIONS (pfile)->output_conditionals) {
4316 static char failed[] = "#failed\n";
4317 CPP_PUTS (pfile, failed, sizeof(failed)-1);
4318 pfile->lineno++;
4319 output_line_command (pfile, 1, same_file);
4322 beg_of_line:
4323 if (CPP_OPTIONS (pfile)->output_conditionals)
4325 cpp_buffer *pbuf = CPP_BUFFER (pfile);
4326 U_CHAR *start_line = pbuf->buf + line_start_mark.position;
4327 CPP_PUTS (pfile, start_line, pbuf->cur - start_line);
4329 parse_move_mark (&line_start_mark, pfile);
4330 if (!CPP_TRADITIONAL (pfile))
4331 cpp_skip_hspace (pfile);
4332 c = GETC();
4333 if (c == '#')
4335 int old_written = CPP_WRITTEN (pfile);
4336 cpp_skip_hspace (pfile);
4338 parse_name (pfile, GETC());
4339 ident_length = CPP_WRITTEN (pfile) - old_written;
4340 ident = pfile->token_buffer + old_written;
4341 pfile->limit = ident;
4342 #if 0
4343 if (ident_length == 0)
4344 goto not_a_directive;
4346 /* Handle # followed by a line number. */
4348 /* Avoid error for `###' and similar cases unless -pedantic. */
4349 #endif
4351 for (kt = directive_table; kt->length >= 0; kt++)
4353 IF_STACK_FRAME *temp;
4354 if (ident_length == kt->length
4355 && strncmp (ident, kt->name, kt->length) == 0)
4357 /* If we are asked to return on next directive, do so now. */
4358 if (any)
4359 goto done;
4361 switch (kt->type)
4363 case T_IF:
4364 case T_IFDEF:
4365 case T_IFNDEF:
4366 temp
4367 = (IF_STACK_FRAME *) xcalloc (1, sizeof (IF_STACK_FRAME));
4368 temp->next = pfile->if_stack;
4369 pfile->if_stack = temp;
4370 #if 0
4371 temp->lineno = CPP_BUFFER(pfile)->lineno;
4372 #endif
4373 temp->fname = CPP_BUFFER(pfile)->nominal_fname;
4374 temp->type = kt->type;
4375 break;
4376 case T_ELSE:
4377 case T_ENDIF:
4378 if (CPP_PEDANTIC (pfile) && pfile->if_stack != save_if_stack)
4379 validate_else (pfile,
4380 kt->type == T_ELSE ? "#else" : "#endif");
4381 case T_ELIF:
4382 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack)
4384 cpp_error (pfile,
4385 "`#%s' not within a conditional", kt->name);
4386 break;
4388 else if (pfile->if_stack == save_if_stack)
4389 goto done; /* found what we came for */
4391 if (kt->type != T_ENDIF)
4393 if (pfile->if_stack->type == T_ELSE)
4394 cpp_error (pfile, "`#else' or `#elif' after `#else'");
4395 pfile->if_stack->type = kt->type;
4396 break;
4399 temp = pfile->if_stack;
4400 pfile->if_stack = temp->next;
4401 free (temp);
4402 break;
4403 default: ;
4405 break;
4407 /* Don't let erroneous code go by. */
4408 if (kt->length < 0 && !CPP_OPTIONS (pfile)->lang_asm
4409 && CPP_PEDANTIC (pfile))
4410 cpp_pedwarn (pfile, "invalid preprocessor directive name");
4412 c = GETC ();
4414 /* We're in the middle of a line. Skip the rest of it. */
4415 for (;;) {
4416 switch (c)
4418 long old;
4419 case EOF:
4420 goto done;
4421 case '/': /* possible comment */
4422 c = skip_comment (pfile, NULL);
4423 if (c == EOF)
4424 goto done;
4425 break;
4426 case '\"':
4427 case '\'':
4428 FORWARD(-1);
4429 old = CPP_WRITTEN (pfile);
4430 cpp_get_token (pfile);
4431 CPP_SET_WRITTEN (pfile, old);
4432 break;
4433 case '\\':
4434 /* Char after backslash loses its special meaning. */
4435 if (PEEKC() == '\n')
4436 FORWARD (1);
4437 break;
4438 case '\n':
4439 goto beg_of_line;
4440 break;
4442 c = GETC ();
4444 done:
4445 if (CPP_OPTIONS (pfile)->output_conditionals) {
4446 static char end_failed[] = "#endfailed\n";
4447 CPP_PUTS (pfile, end_failed, sizeof(end_failed)-1);
4448 pfile->lineno++;
4450 pfile->only_seen_white = 1;
4451 parse_goto_mark (&line_start_mark, pfile);
4452 parse_clear_mark (&line_start_mark);
4456 * handle a #else directive. Do this by just continuing processing
4457 * without changing if_stack ; this is so that the error message
4458 * for missing #endif's etc. will point to the original #if. It
4459 * is possible that something different would be better.
4462 static int
4463 do_else (pfile, keyword, buf, limit)
4464 cpp_reader *pfile;
4465 struct directive *keyword;
4466 U_CHAR *buf, *limit;
4468 cpp_buffer *ip = CPP_BUFFER (pfile);
4470 if (CPP_PEDANTIC (pfile))
4471 validate_else (pfile, "#else");
4472 skip_rest_of_line (pfile);
4474 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack) {
4475 cpp_error (pfile, "`#else' not within a conditional");
4476 return 0;
4477 } else {
4478 /* #ifndef can't have its special treatment for containing the whole file
4479 if it has a #else clause. */
4480 pfile->if_stack->control_macro = 0;
4482 if (pfile->if_stack->type != T_IF && pfile->if_stack->type != T_ELIF) {
4483 cpp_error (pfile, "`#else' after `#else'");
4484 fprintf (stderr, " (matches line %d", pfile->if_stack->lineno);
4485 if (strcmp (pfile->if_stack->fname, ip->nominal_fname) != 0)
4486 fprintf (stderr, ", file %s", pfile->if_stack->fname);
4487 fprintf (stderr, ")\n");
4489 pfile->if_stack->type = T_ELSE;
4492 if (pfile->if_stack->if_succeeded)
4493 skip_if_group (pfile, 0);
4494 else {
4495 ++pfile->if_stack->if_succeeded; /* continue processing input */
4496 output_line_command (pfile, 1, same_file);
4498 return 0;
4502 * unstack after #endif command
4505 static int
4506 do_endif (pfile, keyword, buf, limit)
4507 cpp_reader *pfile;
4508 struct directive *keyword;
4509 U_CHAR *buf, *limit;
4511 if (CPP_PEDANTIC (pfile))
4512 validate_else (pfile, "#endif");
4513 skip_rest_of_line (pfile);
4515 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack)
4516 cpp_error (pfile, "unbalanced `#endif'");
4517 else
4519 IF_STACK_FRAME *temp = pfile->if_stack;
4520 pfile->if_stack = temp->next;
4521 if (temp->control_macro != 0)
4523 /* This #endif matched a #ifndef at the start of the file.
4524 See if it is at the end of the file. */
4525 struct parse_marker start_mark;
4526 int c;
4528 parse_set_mark (&start_mark, pfile);
4530 for (;;)
4532 cpp_skip_hspace (pfile);
4533 c = GETC ();
4534 if (c != '\n')
4535 break;
4537 parse_goto_mark (&start_mark, pfile);
4538 parse_clear_mark (&start_mark);
4540 if (c == EOF)
4542 /* If we get here, this #endif ends a #ifndef
4543 that contains all of the file (aside from whitespace).
4544 Arrange not to include the file again
4545 if the macro that was tested is defined.
4547 Do not do this for the top-level file in a -include or any
4548 file in a -imacros. */
4549 #if 0
4550 FIXME!
4551 if (indepth != 0
4552 && ! (indepth == 1 && pfile->no_record_file)
4553 && ! (pfile->no_record_file && no_output))
4554 #endif
4556 struct file_name_list *ifile = pfile->all_include_files;
4558 for ( ; ifile != NULL; ifile = ifile->next)
4560 if (!strcmp (ifile->fname, CPP_BUFFER (pfile)->fname))
4562 ifile->control_macro = temp->control_macro;
4563 break;
4569 free (temp);
4570 output_line_command (pfile, 1, same_file);
4572 return 0;
4575 /* When an #else or #endif is found while skipping failed conditional,
4576 if -pedantic was specified, this is called to warn about text after
4577 the command name. P points to the first char after the command name. */
4579 static void
4580 validate_else (pfile, directive)
4581 cpp_reader *pfile;
4582 char *directive;
4584 int c;
4585 cpp_skip_hspace (pfile);
4586 c = PEEKC ();
4587 if (c != EOF && c != '\n')
4588 cpp_pedwarn (pfile,
4589 "text following `%s' violates ANSI standard", directive);
4592 /* Get the next token, and add it to the text in pfile->token_buffer.
4593 Return the kind of token we got. */
4596 enum cpp_token
4597 cpp_get_token (pfile)
4598 cpp_reader *pfile;
4600 register int c, c2, c3;
4601 long old_written;
4602 long start_line, start_column;
4603 enum cpp_token token;
4604 struct cpp_options *opts = CPP_OPTIONS (pfile);
4605 CPP_BUFFER (pfile)->prev = CPP_BUFFER (pfile)->cur;
4606 get_next:
4607 c = GETC();
4608 if (c == EOF)
4610 handle_eof:
4611 if (CPP_BUFFER (pfile)->seen_eof)
4613 if (cpp_pop_buffer (pfile) != CPP_NULL_BUFFER (pfile))
4614 goto get_next;
4615 else
4616 return CPP_EOF;
4618 else
4620 cpp_buffer *next_buf
4621 = CPP_PREV_BUFFER (CPP_BUFFER (pfile));
4622 CPP_BUFFER (pfile)->seen_eof = 1;
4623 if (CPP_BUFFER (pfile)->nominal_fname && next_buf != 0)
4625 /* We're about to return from an #include file.
4626 Emit #line information now (as part of the CPP_POP) result.
4627 But the #line refers to the file we will pop to. */
4628 cpp_buffer *cur_buffer = CPP_BUFFER (pfile);
4629 CPP_BUFFER (pfile) = next_buf;
4630 pfile->input_stack_listing_current = 0;
4631 output_line_command (pfile, 0, leave_file);
4632 CPP_BUFFER (pfile) = cur_buffer;
4634 return CPP_POP;
4637 else
4639 switch (c)
4641 long newlines;
4642 struct parse_marker start_mark;
4643 case '/':
4644 if (PEEKC () == '=')
4645 goto op2;
4646 if (opts->put_out_comments)
4647 parse_set_mark (&start_mark, pfile);
4648 newlines = 0;
4649 cpp_buf_line_and_col (cpp_file_buffer (pfile),
4650 &start_line, &start_column);
4651 c = skip_comment (pfile, &newlines);
4652 if (opts->put_out_comments && (c == '/' || c == EOF))
4653 parse_clear_mark (&start_mark);
4654 if (c == '/')
4655 goto randomchar;
4656 if (c == EOF)
4658 cpp_error_with_line (pfile, start_line, start_column,
4659 "unterminated comment");
4660 goto handle_eof;
4662 c = '/'; /* Initial letter of comment. */
4663 return_comment:
4664 /* Comments are equivalent to spaces.
4665 For -traditional, a comment is equivalent to nothing. */
4666 if (opts->put_out_comments)
4668 cpp_buffer *pbuf = CPP_BUFFER (pfile);
4669 long dummy;
4670 U_CHAR *start = pbuf->buf + start_mark.position;
4671 int len = pbuf->cur - start;
4672 CPP_RESERVE(pfile, 1 + len);
4673 CPP_PUTC_Q (pfile, c);
4674 CPP_PUTS_Q (pfile, start, len);
4675 pfile->lineno += newlines;
4676 parse_clear_mark (&start_mark);
4677 return CPP_COMMENT;
4679 else if (CPP_TRADITIONAL (pfile))
4681 return CPP_COMMENT;
4683 else
4685 #if 0
4686 /* This may not work if cpp_get_token is called recursively,
4687 since many places look for horizontal space. */
4688 if (newlines)
4690 /* Copy the newlines into the output buffer, in order to
4691 avoid the pain of a #line every time a multiline comment
4692 is seen. */
4693 CPP_RESERVE(pfile, newlines);
4694 while (--newlines >= 0)
4696 CPP_PUTC_Q (pfile, '\n');
4697 pfile->lineno++;
4699 return CPP_VSPACE;
4701 #endif
4702 CPP_RESERVE(pfile, 1);
4703 CPP_PUTC_Q (pfile, ' ');
4704 return CPP_HSPACE;
4706 #if 0
4707 if (opts->for_lint) {
4708 U_CHAR *argbp;
4709 int cmdlen, arglen;
4710 char *lintcmd = get_lintcmd (ibp, limit, &argbp, &arglen, &cmdlen);
4712 if (lintcmd != NULL) {
4713 /* I believe it is always safe to emit this newline: */
4714 obp[-1] = '\n';
4715 bcopy ("#pragma lint ", (char *) obp, 13);
4716 obp += 13;
4717 bcopy (lintcmd, (char *) obp, cmdlen);
4718 obp += cmdlen;
4720 if (arglen != 0) {
4721 *(obp++) = ' ';
4722 bcopy (argbp, (char *) obp, arglen);
4723 obp += arglen;
4726 /* OK, now bring us back to the state we were in before we entered
4727 this branch. We need #line b/c the newline for the pragma
4728 could fuck things up. */
4729 output_line_command (pfile, 0, same_file);
4730 *(obp++) = ' '; /* just in case, if comments are copied thru */
4731 *(obp++) = '/';
4734 #endif
4736 case '#':
4737 #if 0
4738 /* If this is expanding a macro definition, don't recognize
4739 preprocessor directives. */
4740 if (ip->macro != 0)
4741 goto randomchar;
4742 /* If this is expand_into_temp_buffer, recognize them
4743 only after an actual newline at this level,
4744 not at the beginning of the input level. */
4745 if (ip->fname == 0 && beg_of_line == ip->buf)
4746 goto randomchar;
4747 if (ident_length)
4748 goto specialchar;
4749 #endif
4751 if (!pfile->only_seen_white)
4752 goto randomchar;
4753 if (handle_directive (pfile))
4754 return CPP_DIRECTIVE;
4755 pfile->only_seen_white = 0;
4756 return CPP_OTHER;
4758 case '\"':
4759 case '\'':
4760 /* A single quoted string is treated like a double -- some
4761 programs (e.g., troff) are perverse this way */
4762 cpp_buf_line_and_col (cpp_file_buffer (pfile),
4763 &start_line, &start_column);
4764 old_written = CPP_WRITTEN (pfile);
4765 string:
4766 CPP_PUTC (pfile, c);
4767 while (1)
4769 int cc = GETC();
4770 if (cc == EOF)
4772 if (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
4774 /* try harder: this string crosses a macro expansion
4775 boundary. This can happen naturally if -traditional.
4776 Otherwise, only -D can make a macro with an unmatched
4777 quote. */
4778 cpp_buffer *next_buf
4779 = CPP_PREV_BUFFER (CPP_BUFFER (pfile));
4780 (*CPP_BUFFER (pfile)->cleanup)
4781 (CPP_BUFFER (pfile), pfile);
4782 CPP_BUFFER (pfile) = next_buf;
4783 continue;
4785 if (!CPP_TRADITIONAL (pfile))
4787 cpp_error_with_line (pfile, start_line, start_column,
4788 "unterminated string or character constant");
4789 if (pfile->multiline_string_line != start_line
4790 && pfile->multiline_string_line != 0)
4791 cpp_error_with_line (pfile,
4792 pfile->multiline_string_line, -1,
4793 "possible real start of unterminated constant");
4794 pfile->multiline_string_line = 0;
4796 break;
4798 CPP_PUTC (pfile, cc);
4799 switch (cc)
4801 case '\n':
4802 /* Traditionally, end of line ends a string constant with
4803 no error. So exit the loop and record the new line. */
4804 if (CPP_TRADITIONAL (pfile))
4805 goto while2end;
4806 if (c == '\'')
4808 cpp_error_with_line (pfile, start_line, start_column,
4809 "unterminated character constant");
4810 goto while2end;
4812 if (CPP_PEDANTIC (pfile)
4813 && pfile->multiline_string_line == 0)
4815 cpp_pedwarn_with_line (pfile, start_line, start_column,
4816 "string constant runs past end of line");
4818 if (pfile->multiline_string_line == 0)
4819 pfile->multiline_string_line = start_line;
4820 break;
4822 case '\\':
4823 cc = GETC();
4824 if (cc == '\n')
4826 /* Backslash newline is replaced by nothing at all. */
4827 CPP_ADJUST_WRITTEN (pfile, -1);
4828 pfile->lineno++;
4830 else
4832 /* ANSI stupidly requires that in \\ the second \
4833 is *not* prevented from combining with a newline. */
4834 NEWLINE_FIX1(cc);
4835 if (cc != EOF)
4836 CPP_PUTC (pfile, cc);
4838 break;
4840 case '\"':
4841 case '\'':
4842 if (cc == c)
4843 goto while2end;
4844 break;
4847 while2end:
4848 pfile->lineno += count_newlines (pfile->token_buffer + old_written,
4849 CPP_PWRITTEN (pfile));
4850 pfile->only_seen_white = 0;
4851 return c == '\'' ? CPP_CHAR : CPP_STRING;
4853 case '$':
4854 if (!opts->dollars_in_ident)
4855 goto randomchar;
4856 goto letter;
4858 case ':':
4859 if (opts->cplusplus && PEEKC () == ':')
4860 goto op2;
4861 goto randomchar;
4863 case '&':
4864 case '+':
4865 case '|':
4866 NEWLINE_FIX;
4867 c2 = PEEKC ();
4868 if (c2 == c || c2 == '=')
4869 goto op2;
4870 goto randomchar;
4872 case '*':
4873 case '!':
4874 case '%':
4875 case '=':
4876 case '^':
4877 NEWLINE_FIX;
4878 if (PEEKC () == '=')
4879 goto op2;
4880 goto randomchar;
4882 case '-':
4883 NEWLINE_FIX;
4884 c2 = PEEKC ();
4885 if (c2 == '-' && opts->chill)
4887 /* Chill style comment */
4888 if (opts->put_out_comments)
4889 parse_set_mark (&start_mark, pfile);
4890 FORWARD(1); /* Skip second '-'. */
4891 for (;;)
4893 c = GETC ();
4894 if (c == EOF)
4895 break;
4896 if (c == '\n')
4898 /* Don't consider final '\n' to be part of comment. */
4899 FORWARD(-1);
4900 break;
4903 c = '-';
4904 goto return_comment;
4906 if (c2 == '-' || c2 == '=' || c2 == '>')
4907 goto op2;
4908 goto randomchar;
4910 case '<':
4911 if (pfile->parsing_include_directive)
4913 for (;;)
4915 CPP_PUTC (pfile, c);
4916 if (c == '>')
4917 break;
4918 c = GETC ();
4919 NEWLINE_FIX1 (c);
4920 if (c == '\n' || c == EOF)
4922 cpp_error (pfile,
4923 "missing '>' in `#include <FILENAME>'");
4924 break;
4927 return CPP_STRING;
4929 /* else fall through */
4930 case '>':
4931 NEWLINE_FIX;
4932 c2 = PEEKC ();
4933 if (c2 == '=')
4934 goto op2;
4935 if (c2 != c)
4936 goto randomchar;
4937 FORWARD(1);
4938 CPP_RESERVE (pfile, 4);
4939 CPP_PUTC (pfile, c);
4940 CPP_PUTC (pfile, c2);
4941 NEWLINE_FIX;
4942 c3 = PEEKC ();
4943 if (c3 == '=')
4944 CPP_PUTC_Q (pfile, GETC ());
4945 CPP_NUL_TERMINATE_Q (pfile);
4946 pfile->only_seen_white = 0;
4947 return CPP_OTHER;
4949 case '@':
4950 if (CPP_BUFFER (pfile)->has_escapes)
4952 c = GETC ();
4953 if (c == '-')
4955 if (pfile->output_escapes)
4956 CPP_PUTS (pfile, "@-", 2);
4957 parse_name (pfile, GETC ());
4958 return CPP_NAME;
4960 else if (is_space [c])
4962 CPP_RESERVE (pfile, 2);
4963 if (pfile->output_escapes)
4964 CPP_PUTC_Q (pfile, '@');
4965 CPP_PUTC_Q (pfile, c);
4966 return CPP_HSPACE;
4969 if (pfile->output_escapes)
4971 CPP_PUTS (pfile, "@@", 2);
4972 return CPP_OTHER;
4974 goto randomchar;
4976 case '.':
4977 NEWLINE_FIX;
4978 c2 = PEEKC ();
4979 if (isdigit(c2))
4981 CPP_RESERVE(pfile, 2);
4982 CPP_PUTC_Q (pfile, '.');
4983 c = GETC ();
4984 goto number;
4986 /* FIXME - misses the case "..\\\n." */
4987 if (c2 == '.' && PEEKN(1) == '.')
4989 CPP_RESERVE(pfile, 4);
4990 CPP_PUTC_Q (pfile, '.');
4991 CPP_PUTC_Q (pfile, '.');
4992 CPP_PUTC_Q (pfile, '.');
4993 FORWARD (2);
4994 CPP_NUL_TERMINATE_Q (pfile);
4995 pfile->only_seen_white = 0;
4996 return CPP_3DOTS;
4998 goto randomchar;
5000 op2:
5001 token = CPP_OTHER;
5002 pfile->only_seen_white = 0;
5003 op2any:
5004 CPP_RESERVE(pfile, 3);
5005 CPP_PUTC_Q (pfile, c);
5006 CPP_PUTC_Q (pfile, GETC ());
5007 CPP_NUL_TERMINATE_Q (pfile);
5008 return token;
5010 case 'L':
5011 NEWLINE_FIX;
5012 c2 = PEEKC ();
5013 if ((c2 == '\'' || c2 == '\"') && !CPP_TRADITIONAL (pfile))
5015 CPP_PUTC (pfile, c);
5016 c = GETC ();
5017 goto string;
5019 goto letter;
5021 case '0': case '1': case '2': case '3': case '4':
5022 case '5': case '6': case '7': case '8': case '9':
5023 number:
5024 c2 = '.';
5025 for (;;)
5027 CPP_RESERVE (pfile, 2);
5028 CPP_PUTC_Q (pfile, c);
5029 NEWLINE_FIX;
5030 c = PEEKC ();
5031 if (c == EOF)
5032 break;
5033 if (!is_idchar[c] && c != '.'
5034 && ((c2 != 'e' && c2 != 'E') || (c != '+' && c != '-')))
5035 break;
5036 FORWARD(1);
5037 c2= c;
5039 CPP_NUL_TERMINATE_Q (pfile);
5040 pfile->only_seen_white = 0;
5041 return CPP_NUMBER;
5042 case 'b': case 'c': case 'd': case 'h': case 'o':
5043 case 'B': case 'C': case 'D': case 'H': case 'O':
5044 if (opts->chill && PEEKC () == '\'')
5046 pfile->only_seen_white = 0;
5047 CPP_RESERVE (pfile, 2);
5048 CPP_PUTC_Q (pfile, c);
5049 CPP_PUTC_Q (pfile, '\'');
5050 FORWARD(1);
5051 for (;;)
5053 c = GETC();
5054 if (c == EOF)
5055 goto chill_number_eof;
5056 if (!is_idchar[c])
5058 if (c == '\\' && PEEKC() == '\n')
5060 FORWARD(2);
5061 continue;
5063 break;
5065 CPP_PUTC (pfile, c);
5067 if (c == '\'')
5069 CPP_RESERVE (pfile, 2);
5070 CPP_PUTC_Q (pfile, c);
5071 CPP_NUL_TERMINATE_Q (pfile);
5072 return CPP_STRING;
5074 else
5076 FORWARD(-1);
5077 chill_number_eof:
5078 CPP_NUL_TERMINATE (pfile);
5079 return CPP_NUMBER;
5082 else
5083 goto letter;
5084 case '_':
5085 case 'a': case 'e': case 'f': case 'g': case 'i': case 'j':
5086 case 'k': case 'l': case 'm': case 'n': case 'p': case 'q':
5087 case 'r': case 's': case 't': case 'u': case 'v': case 'w':
5088 case 'x': case 'y': case 'z':
5089 case 'A': case 'E': case 'F': case 'G': case 'I': case 'J':
5090 case 'K': case 'M': case 'N': case 'P': case 'Q': case 'R':
5091 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
5092 case 'Y': case 'Z':
5093 letter:
5095 HASHNODE *hp;
5096 unsigned char *ident;
5097 int before_name_written = CPP_WRITTEN (pfile);
5098 int ident_len;
5099 parse_name (pfile, c);
5100 pfile->only_seen_white = 0;
5101 if (pfile->no_macro_expand)
5102 return CPP_NAME;
5103 ident = pfile->token_buffer + before_name_written;
5104 ident_len = CPP_PWRITTEN (pfile) - ident;
5105 hp = cpp_lookup (pfile, ident, ident_len, -1);
5106 if (!hp)
5107 return CPP_NAME;
5108 if (hp->type == T_DISABLED)
5110 if (pfile->output_escapes)
5111 { /* Return "@-IDENT", followed by '\0'. */
5112 int i;
5113 CPP_RESERVE (pfile, 3);
5114 ident = pfile->token_buffer + before_name_written;
5115 CPP_ADJUST_WRITTEN (pfile, 2);
5116 for (i = ident_len; i >= 0; i--) ident[i+2] = ident[i];
5117 ident[0] = '@';
5118 ident[1] = '-';
5120 return CPP_NAME;
5123 /* If macro wants an arglist, verify that a '(' follows.
5124 first skip all whitespace, copying it to the output
5125 after the macro name. Then, if there is no '(',
5126 decide this is not a macro call and leave things that way. */
5127 if (hp->type == T_MACRO && hp->value.defn->nargs >= 0)
5129 struct parse_marker macro_mark;
5130 int is_macro_call;
5131 while (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
5133 cpp_buffer *next_buf;
5134 cpp_skip_hspace (pfile);
5135 if (PEEKC () != EOF)
5136 break;
5137 next_buf = CPP_PREV_BUFFER (CPP_BUFFER (pfile));
5138 (*CPP_BUFFER (pfile)->cleanup) (CPP_BUFFER (pfile), pfile);
5139 CPP_BUFFER (pfile) = next_buf;
5141 parse_set_mark (&macro_mark, pfile);
5142 for (;;)
5144 cpp_skip_hspace (pfile);
5145 c = PEEKC ();
5146 is_macro_call = c == '(';
5147 if (c != '\n')
5148 break;
5149 FORWARD (1);
5151 if (!is_macro_call)
5152 parse_goto_mark (&macro_mark, pfile);
5153 parse_clear_mark (&macro_mark);
5154 if (!is_macro_call)
5155 return CPP_NAME;
5157 /* This is now known to be a macro call. */
5159 /* it might not actually be a macro. */
5160 if (hp->type != T_MACRO) {
5161 int xbuf_len; U_CHAR *xbuf;
5162 CPP_SET_WRITTEN (pfile, before_name_written);
5163 special_symbol (hp, pfile);
5164 xbuf_len = CPP_WRITTEN (pfile) - before_name_written;
5165 xbuf = (U_CHAR *) xmalloc (xbuf_len + 1);
5166 CPP_SET_WRITTEN (pfile, before_name_written);
5167 bcopy (CPP_PWRITTEN (pfile), xbuf, xbuf_len + 1);
5168 push_macro_expansion (pfile, xbuf, xbuf_len, hp);
5170 else
5172 /* Expand the macro, reading arguments as needed,
5173 and push the expansion on the input stack. */
5174 macroexpand (pfile, hp);
5175 CPP_SET_WRITTEN (pfile, before_name_written);
5178 /* An extra "@ " is added to the end of a macro expansion
5179 to prevent accidental token pasting. We prefer to avoid
5180 unneeded extra spaces (for the sake of cpp-using tools like
5181 imake). Here we remove the space if it is safe to do so. */
5182 if (pfile->buffer->rlimit - pfile->buffer->cur >= 3
5183 && pfile->buffer->rlimit[-2] == '@'
5184 && pfile->buffer->rlimit[-1] == ' ')
5186 int c1 = pfile->buffer->rlimit[-3];
5187 int c2 = CPP_BUF_PEEK (CPP_PREV_BUFFER (CPP_BUFFER (pfile)));
5188 if (c2 == EOF || ! unsafe_chars (c1, c2))
5189 pfile->buffer->rlimit -= 2;
5192 goto get_next;
5194 case ' ': case '\t': case '\v': case '\r':
5195 for (;;)
5197 CPP_PUTC (pfile, c);
5198 c = PEEKC ();
5199 if (c == EOF || !is_hor_space[c])
5200 break;
5201 FORWARD(1);
5203 return CPP_HSPACE;
5205 case '\\':
5206 c2 = PEEKC ();
5207 if (c2 != '\n')
5208 goto randomchar;
5209 token = CPP_HSPACE;
5210 goto op2any;
5212 case '\n':
5213 CPP_PUTC (pfile, c);
5214 if (pfile->only_seen_white == 0)
5215 pfile->only_seen_white = 1;
5216 pfile->lineno++;
5217 output_line_command (pfile, 1, same_file);
5218 return CPP_VSPACE;
5220 case '(': token = CPP_LPAREN; goto char1;
5221 case ')': token = CPP_RPAREN; goto char1;
5222 case '{': token = CPP_LBRACE; goto char1;
5223 case '}': token = CPP_RBRACE; goto char1;
5224 case ',': token = CPP_COMMA; goto char1;
5225 case ';': token = CPP_SEMICOLON; goto char1;
5227 randomchar:
5228 default:
5229 token = CPP_OTHER;
5230 char1:
5231 pfile->only_seen_white = 0;
5232 CPP_PUTC (pfile, c);
5233 return token;
5238 /* Like cpp_get_token, but skip spaces and comments. */
5239 enum cpp_token
5240 cpp_get_non_space_token (pfile)
5241 cpp_reader *pfile;
5243 int old_written = CPP_WRITTEN (pfile);
5244 for (;;)
5246 enum cpp_token token = cpp_get_token (pfile);
5247 if (token != CPP_COMMENT && token != CPP_POP
5248 && token != CPP_HSPACE && token != CPP_VSPACE)
5249 return token;
5250 CPP_SET_WRITTEN (pfile, old_written);
5254 /* Parse an identifier starting with C. */
5257 parse_name (pfile, c)
5258 cpp_reader *pfile; int c;
5260 for (;;)
5262 if (! is_idchar[c])
5264 if (c == '\\' && PEEKC() == '\n')
5266 FORWARD(2);
5267 continue;
5269 FORWARD (-1);
5270 break;
5273 CPP_RESERVE(pfile, 2); /* One more for final NUL. */
5274 CPP_PUTC_Q (pfile, c);
5275 c = GETC();
5276 if (c == EOF)
5277 break;
5279 CPP_NUL_TERMINATE_Q (pfile);
5280 return 1;
5284 /* Maintain and search list of included files, for #import. */
5286 /* Hash a file name for import_hash_table. */
5288 static int
5289 import_hash (f)
5290 char *f;
5292 int val = 0;
5294 while (*f) val += *f++;
5295 return (val%IMPORT_HASH_SIZE);
5298 /* Search for file FILENAME in import_hash_table.
5299 Return -2 if found, either a matching name or a matching inode.
5300 Otherwise, open the file and return a file descriptor if successful
5301 or -1 if unsuccessful. */
5303 static int
5304 lookup_import (pfile, filename, searchptr)
5305 cpp_reader *pfile;
5306 char *filename;
5307 struct file_name_list *searchptr;
5309 struct import_file *i;
5310 int h;
5311 int hashval;
5312 struct stat sb;
5313 int fd;
5315 hashval = import_hash (filename);
5317 /* Attempt to find file in list of already included files */
5318 i = pfile->import_hash_table[hashval];
5320 while (i) {
5321 if (!strcmp (filename, i->name))
5322 return -2; /* return found */
5323 i = i->next;
5325 /* Open it and try a match on inode/dev */
5326 fd = open_include_file (pfile, filename, searchptr);
5327 if (fd < 0)
5328 return fd;
5329 fstat (fd, &sb);
5330 for (h = 0; h < IMPORT_HASH_SIZE; h++) {
5331 i = pfile->import_hash_table[h];
5332 while (i) {
5333 /* Compare the inode and the device.
5334 Supposedly on some systems the inode is not a scalar. */
5335 if (!bcmp ((char *) &i->inode, (char *) &sb.st_ino, sizeof (sb.st_ino))
5336 && i->dev == sb.st_dev) {
5337 close (fd);
5338 return -2; /* return found */
5340 i = i->next;
5343 return fd; /* Not found, return open file */
5346 /* Add the file FNAME, open on descriptor FD, to import_hash_table. */
5348 static void
5349 add_import (pfile, fd, fname)
5350 cpp_reader *pfile;
5351 int fd;
5352 char *fname;
5354 struct import_file *i;
5355 int hashval;
5356 struct stat sb;
5358 hashval = import_hash (fname);
5359 fstat (fd, &sb);
5360 i = (struct import_file *)xmalloc (sizeof (struct import_file));
5361 i->name = (char *)xmalloc (strlen (fname)+1);
5362 strcpy (i->name, fname);
5363 bcopy ((char *) &sb.st_ino, (char *) &i->inode, sizeof (sb.st_ino));
5364 i->dev = sb.st_dev;
5365 i->next = pfile->import_hash_table[hashval];
5366 pfile->import_hash_table[hashval] = i;
5369 /* The file_name_map structure holds a mapping of file names for a
5370 particular directory. This mapping is read from the file named
5371 FILE_NAME_MAP_FILE in that directory. Such a file can be used to
5372 map filenames on a file system with severe filename restrictions,
5373 such as DOS. The format of the file name map file is just a series
5374 of lines with two tokens on each line. The first token is the name
5375 to map, and the second token is the actual name to use. */
5377 struct file_name_map
5379 struct file_name_map *map_next;
5380 char *map_from;
5381 char *map_to;
5384 #define FILE_NAME_MAP_FILE "header.gcc"
5386 /* Read a space delimited string of unlimited length from a stdio
5387 file. */
5389 static char *
5390 read_filename_string (ch, f)
5391 int ch;
5392 FILE *f;
5394 char *alloc, *set;
5395 int len;
5397 len = 20;
5398 set = alloc = xmalloc (len + 1);
5399 if (! is_space[ch])
5401 *set++ = ch;
5402 while ((ch = getc (f)) != EOF && ! is_space[ch])
5404 if (set - alloc == len)
5406 len *= 2;
5407 alloc = xrealloc (alloc, len + 1);
5408 set = alloc + len / 2;
5410 *set++ = ch;
5413 *set = '\0';
5414 ungetc (ch, f);
5415 return alloc;
5418 /* This structure holds a linked list of file name maps, one per directory. */
5419 struct file_name_map_list
5421 struct file_name_map_list *map_list_next;
5422 char *map_list_name;
5423 struct file_name_map *map_list_map;
5426 /* Read the file name map file for DIRNAME. */
5428 static struct file_name_map *
5429 read_name_map (pfile, dirname)
5430 cpp_reader *pfile;
5431 char *dirname;
5433 register struct file_name_map_list *map_list_ptr;
5434 char *name;
5435 FILE *f;
5437 for (map_list_ptr = CPP_OPTIONS (pfile)->map_list; map_list_ptr;
5438 map_list_ptr = map_list_ptr->map_list_next)
5439 if (! strcmp (map_list_ptr->map_list_name, dirname))
5440 return map_list_ptr->map_list_map;
5442 map_list_ptr = ((struct file_name_map_list *)
5443 xmalloc (sizeof (struct file_name_map_list)));
5444 map_list_ptr->map_list_name = savestring (dirname);
5445 map_list_ptr->map_list_map = NULL;
5447 name = (char *) alloca (strlen (dirname) + strlen (FILE_NAME_MAP_FILE) + 2);
5448 strcpy (name, dirname);
5449 if (*dirname)
5450 strcat (name, "/");
5451 strcat (name, FILE_NAME_MAP_FILE);
5452 f = fopen (name, "r");
5453 if (!f)
5454 map_list_ptr->map_list_map = NULL;
5455 else
5457 int ch;
5458 int dirlen = strlen (dirname);
5460 while ((ch = getc (f)) != EOF)
5462 char *from, *to;
5463 struct file_name_map *ptr;
5465 if (is_space[ch])
5466 continue;
5467 from = read_filename_string (ch, f);
5468 while ((ch = getc (f)) != EOF && is_hor_space[ch])
5470 to = read_filename_string (ch, f);
5472 ptr = ((struct file_name_map *)
5473 xmalloc (sizeof (struct file_name_map)));
5474 ptr->map_from = from;
5476 /* Make the real filename absolute. */
5477 if (*to == '/')
5478 ptr->map_to = to;
5479 else
5481 ptr->map_to = xmalloc (dirlen + strlen (to) + 2);
5482 strcpy (ptr->map_to, dirname);
5483 ptr->map_to[dirlen] = '/';
5484 strcpy (ptr->map_to + dirlen + 1, to);
5485 free (to);
5488 ptr->map_next = map_list_ptr->map_list_map;
5489 map_list_ptr->map_list_map = ptr;
5491 while ((ch = getc (f)) != '\n')
5492 if (ch == EOF)
5493 break;
5495 fclose (f);
5498 map_list_ptr->map_list_next = CPP_OPTIONS (pfile)->map_list;
5499 CPP_OPTIONS (pfile)->map_list = map_list_ptr;
5501 return map_list_ptr->map_list_map;
5504 /* Try to open include file FILENAME. SEARCHPTR is the directory
5505 being tried from the include file search path. This function maps
5506 filenames on file systems based on information read by
5507 read_name_map. */
5509 static int
5510 open_include_file (pfile, filename, searchptr)
5511 cpp_reader *pfile;
5512 char *filename;
5513 struct file_name_list *searchptr;
5515 register struct file_name_map *map;
5516 register char *from;
5517 char *p, *dir;
5519 if (searchptr && ! searchptr->got_name_map)
5521 searchptr->name_map = read_name_map (pfile,
5522 searchptr->fname
5523 ? searchptr->fname : ".");
5524 searchptr->got_name_map = 1;
5527 /* First check the mapping for the directory we are using. */
5528 if (searchptr && searchptr->name_map)
5530 from = filename;
5531 if (searchptr->fname)
5532 from += strlen (searchptr->fname) + 1;
5533 for (map = searchptr->name_map; map; map = map->map_next)
5535 if (! strcmp (map->map_from, from))
5537 /* Found a match. */
5538 return open (map->map_to, O_RDONLY, 0666);
5543 /* Try to find a mapping file for the particular directory we are
5544 looking in. Thus #include <sys/types.h> will look up sys/types.h
5545 in /usr/include/header.gcc and look up types.h in
5546 /usr/include/sys/header.gcc. */
5547 p = rindex (filename, '/');
5548 if (! p)
5549 p = filename;
5550 if (searchptr
5551 && searchptr->fname
5552 && strlen (searchptr->fname) == p - filename
5553 && ! strncmp (searchptr->fname, filename, p - filename))
5555 /* FILENAME is in SEARCHPTR, which we've already checked. */
5556 return open (filename, O_RDONLY, 0666);
5559 if (p == filename)
5561 dir = ".";
5562 from = filename;
5564 else
5566 dir = (char *) alloca (p - filename + 1);
5567 bcopy (filename, dir, p - filename);
5568 dir[p - filename] = '\0';
5569 from = p + 1;
5571 for (map = read_name_map (pfile, dir); map; map = map->map_next)
5572 if (! strcmp (map->map_from, from))
5573 return open (map->map_to, O_RDONLY, 0666);
5575 return open (filename, O_RDONLY, 0666);
5578 /* Process the contents of include file FNAME, already open on descriptor F,
5579 with output to OP.
5580 SYSTEM_HEADER_P is 1 if this file resides in any one of the known
5581 "system" include directories (as decided by the `is_system_include'
5582 function above).
5583 DIRPTR is the link in the dir path through which this file was found,
5584 or 0 if the file name was absolute or via the current directory.
5585 Return 1 on success, 0 on failure.
5587 The caller is responsible for the cpp_push_buffer. */
5589 static int
5590 finclude (pfile, f, fname, system_header_p, dirptr)
5591 cpp_reader *pfile;
5592 int f;
5593 char *fname;
5594 int system_header_p;
5595 struct file_name_list *dirptr;
5597 int st_mode;
5598 long st_size;
5599 long i;
5600 int length;
5601 cpp_buffer *fp; /* For input stack frame */
5602 int missing_newline = 0;
5604 if (file_size_and_mode (f, &st_mode, &st_size) < 0)
5606 cpp_perror_with_name (pfile, fname);
5607 close (f);
5608 cpp_pop_buffer (pfile);
5609 return 0;
5612 fp = CPP_BUFFER (pfile);
5613 fp->nominal_fname = fp->fname = fname;
5614 #if 0
5615 fp->length = 0;
5616 #endif
5617 fp->dir = dirptr;
5618 fp->system_header_p = system_header_p;
5619 fp->lineno = 1;
5620 fp->colno = 1;
5621 fp->cleanup = file_cleanup;
5623 if (S_ISREG (st_mode)) {
5624 fp->buf = (U_CHAR *) xmalloc (st_size + 2);
5625 fp->alimit = fp->buf + st_size + 2;
5626 fp->cur = fp->buf;
5628 /* Read the file contents, knowing that st_size is an upper bound
5629 on the number of bytes we can read. */
5630 length = safe_read (f, fp->buf, st_size);
5631 fp->rlimit = fp->buf + length;
5632 if (length < 0) goto nope;
5634 else if (S_ISDIR (st_mode)) {
5635 cpp_error (pfile, "directory `%s' specified in #include", fname);
5636 close (f);
5637 return 0;
5638 } else {
5639 /* Cannot count its file size before reading.
5640 First read the entire file into heap and
5641 copy them into buffer on stack. */
5643 int bsize = 2000;
5645 st_size = 0;
5646 fp->buf = (U_CHAR *) xmalloc (bsize + 2);
5648 for (;;) {
5649 i = safe_read (f, fp->buf + st_size, bsize - st_size);
5650 if (i < 0)
5651 goto nope; /* error! */
5652 st_size += i;
5653 if (st_size != bsize)
5654 break; /* End of file */
5655 bsize *= 2;
5656 fp->buf = (U_CHAR *) xrealloc (fp->buf, bsize + 2);
5658 fp->cur = fp->buf;
5659 length = st_size;
5662 if ((length > 0 && fp->buf[length - 1] != '\n')
5663 /* Backslash-newline at end is not good enough. */
5664 || (length > 1 && fp->buf[length - 2] == '\\')) {
5665 fp->buf[length++] = '\n';
5666 #if 0
5667 missing_newline = 1;
5668 #endif
5670 fp->buf[length] = '\0';
5671 fp->rlimit = fp->buf + length;
5673 /* Close descriptor now, so nesting does not use lots of descriptors. */
5674 close (f);
5676 /* Must do this before calling trigraph_pcp, so that the correct file name
5677 will be printed in warning messages. */
5679 pfile->input_stack_listing_current = 0;
5681 #if 0
5682 if (!no_trigraphs)
5683 trigraph_pcp (fp);
5684 #endif
5686 #if 0
5687 rescan (op, 0);
5689 if (missing_newline)
5690 fp->lineno--;
5692 if (CPP_PEDANTIC (pfile) && missing_newline)
5693 pedwarn ("file does not end in newline");
5695 indepth--;
5696 input_file_stack_tick++;
5697 free (fp->buf);
5698 #endif
5699 return 1;
5701 nope:
5703 cpp_perror_with_name (pfile, fname);
5704 close (f);
5705 free (fp->buf);
5706 return 1;
5710 push_parse_file (pfile, fname)
5711 cpp_reader *pfile;
5712 char *fname;
5714 struct cpp_options *opts = CPP_OPTIONS (pfile);
5715 struct cpp_pending *pend;
5716 char *p;
5717 int f;
5718 cpp_buffer *fp;
5720 /* The code looks at the defaults through this pointer, rather than through
5721 the constant structure above. This pointer gets changed if an environment
5722 variable specifies other defaults. */
5723 struct default_include *include_defaults = include_defaults_array;
5725 /* Add dirs from CPATH after dirs from -I. */
5726 /* There seems to be confusion about what CPATH should do,
5727 so for the moment it is not documented. */
5728 /* Some people say that CPATH should replace the standard include dirs,
5729 but that seems pointless: it comes before them, so it overrides them
5730 anyway. */
5731 p = (char *) getenv ("CPATH");
5732 if (p != 0 && ! opts->no_standard_includes)
5733 path_include (pfile, p);
5735 /* Now that dollars_in_ident is known, initialize is_idchar. */
5736 initialize_char_syntax (opts);
5738 /* Do partial setup of input buffer for the sake of generating
5739 early #line directives (when -g is in effect). */
5740 fp = cpp_push_buffer (pfile, NULL, 0);
5741 if (opts->in_fname == NULL)
5742 opts->in_fname = "";
5743 fp->nominal_fname = fp->fname = opts->in_fname;
5744 fp->lineno = 0;
5746 /* Install __LINE__, etc. Must follow initialize_char_syntax
5747 and option processing. */
5748 initialize_builtins (pfile);
5750 /* Do standard #defines and assertions
5751 that identify system and machine type. */
5753 if (!opts->inhibit_predefs) {
5754 char *p = (char *) alloca (strlen (predefs) + 1);
5755 strcpy (p, predefs);
5756 while (*p) {
5757 char *q;
5758 while (*p == ' ' || *p == '\t')
5759 p++;
5760 /* Handle -D options. */
5761 if (p[0] == '-' && p[1] == 'D') {
5762 q = &p[2];
5763 while (*p && *p != ' ' && *p != '\t')
5764 p++;
5765 if (*p != 0)
5766 *p++= 0;
5767 if (opts->debug_output)
5768 output_line_command (pfile, 0, same_file);
5769 cpp_define (pfile, q);
5770 while (*p == ' ' || *p == '\t')
5771 p++;
5772 } else if (p[0] == '-' && p[1] == 'A') {
5773 /* Handle -A options (assertions). */
5774 char *assertion;
5775 char *past_name;
5776 char *value;
5777 char *past_value;
5778 char *termination;
5779 int save_char;
5781 assertion = &p[2];
5782 past_name = assertion;
5783 /* Locate end of name. */
5784 while (*past_name && *past_name != ' '
5785 && *past_name != '\t' && *past_name != '(')
5786 past_name++;
5787 /* Locate `(' at start of value. */
5788 value = past_name;
5789 while (*value && (*value == ' ' || *value == '\t'))
5790 value++;
5791 if (*value++ != '(')
5792 abort ();
5793 while (*value && (*value == ' ' || *value == '\t'))
5794 value++;
5795 past_value = value;
5796 /* Locate end of value. */
5797 while (*past_value && *past_value != ' '
5798 && *past_value != '\t' && *past_value != ')')
5799 past_value++;
5800 termination = past_value;
5801 while (*termination && (*termination == ' ' || *termination == '\t'))
5802 termination++;
5803 if (*termination++ != ')')
5804 abort ();
5805 if (*termination && *termination != ' ' && *termination != '\t')
5806 abort ();
5807 /* Temporarily null-terminate the value. */
5808 save_char = *termination;
5809 *termination = '\0';
5810 /* Install the assertion. */
5811 make_assertion (pfile, "-A", assertion);
5812 *termination = (char) save_char;
5813 p = termination;
5814 while (*p == ' ' || *p == '\t')
5815 p++;
5816 } else {
5817 abort ();
5822 /* Now handle the command line options. */
5824 /* Do -U's, -D's and -A's in the order they were seen. */
5825 /* First reverse the list. */
5826 opts->pending = nreverse_pending (opts->pending);
5828 for (pend = opts->pending; pend; pend = pend->next)
5830 if (pend->cmd != NULL && pend->cmd[0] == '-')
5832 switch (pend->cmd[1])
5834 case 'U':
5835 if (opts->debug_output)
5836 output_line_command (pfile, 0, same_file);
5837 do_undef (pfile, NULL, pend->arg, pend->arg + strlen (pend->arg));
5838 break;
5839 case 'D':
5840 if (opts->debug_output)
5841 output_line_command (pfile, 0, same_file);
5842 cpp_define (pfile, pend->arg);
5843 break;
5844 case 'A':
5845 make_assertion (pfile, "-A", pend->arg);
5846 break;
5851 opts->done_initializing = 1;
5853 { /* read the appropriate environment variable and if it exists
5854 replace include_defaults with the listed path. */
5855 char *epath = 0;
5856 switch ((opts->objc << 1) + opts->cplusplus)
5858 case 0:
5859 epath = getenv ("C_INCLUDE_PATH");
5860 break;
5861 case 1:
5862 epath = getenv ("CPLUS_INCLUDE_PATH");
5863 break;
5864 case 2:
5865 epath = getenv ("OBJC_INCLUDE_PATH");
5866 break;
5867 case 3:
5868 epath = getenv ("OBJCPLUS_INCLUDE_PATH");
5869 break;
5871 /* If the environment var for this language is set,
5872 add to the default list of include directories. */
5873 if (epath) {
5874 char *nstore = (char *) alloca (strlen (epath) + 2);
5875 int num_dirs;
5876 char *startp, *endp;
5878 for (num_dirs = 1, startp = epath; *startp; startp++)
5879 if (*startp == PATH_SEPARATOR)
5880 num_dirs++;
5881 include_defaults
5882 = (struct default_include *) xmalloc ((num_dirs
5883 * sizeof (struct default_include))
5884 + sizeof (include_defaults_array));
5885 startp = endp = epath;
5886 num_dirs = 0;
5887 while (1) {
5888 /* Handle cases like c:/usr/lib:d:/gcc/lib */
5889 if ((*endp == PATH_SEPARATOR)
5890 || *endp == 0) {
5891 strncpy (nstore, startp, endp-startp);
5892 if (endp == startp)
5893 strcpy (nstore, ".");
5894 else
5895 nstore[endp-startp] = '\0';
5897 include_defaults[num_dirs].fname = savestring (nstore);
5898 include_defaults[num_dirs].cplusplus = opts->cplusplus;
5899 include_defaults[num_dirs].cxx_aware = 1;
5900 num_dirs++;
5901 if (*endp == '\0')
5902 break;
5903 endp = startp = endp + 1;
5904 } else
5905 endp++;
5907 /* Put the usual defaults back in at the end. */
5908 bcopy ((char *) include_defaults_array,
5909 (char *) &include_defaults[num_dirs],
5910 sizeof (include_defaults_array));
5914 append_include_chain (pfile, opts->before_system, opts->last_before_system);
5915 opts->first_system_include = opts->before_system;
5917 /* Unless -fnostdinc,
5918 tack on the standard include file dirs to the specified list */
5919 if (!opts->no_standard_includes) {
5920 struct default_include *p = include_defaults;
5921 char *specd_prefix = opts->include_prefix;
5922 char *default_prefix = savestring (GCC_INCLUDE_DIR);
5923 int default_len = 0;
5924 /* Remove the `include' from /usr/local/lib/gcc.../include. */
5925 if (!strcmp (default_prefix + strlen (default_prefix) - 8, "/include")) {
5926 default_len = strlen (default_prefix) - 7;
5927 default_prefix[default_len] = 0;
5929 /* Search "translated" versions of GNU directories.
5930 These have /usr/local/lib/gcc... replaced by specd_prefix. */
5931 if (specd_prefix != 0 && default_len != 0)
5932 for (p = include_defaults; p->fname; p++) {
5933 /* Some standard dirs are only for C++. */
5934 if (!p->cplusplus
5935 || (opts->cplusplus && !opts->no_standard_cplusplus_includes)) {
5936 /* Does this dir start with the prefix? */
5937 if (!strncmp (p->fname, default_prefix, default_len)) {
5938 /* Yes; change prefix and add to search list. */
5939 struct file_name_list *new
5940 = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
5941 int this_len = strlen (specd_prefix) + strlen (p->fname) - default_len;
5942 char *str = (char *) xmalloc (this_len + 1);
5943 strcpy (str, specd_prefix);
5944 strcat (str, p->fname + default_len);
5945 new->fname = str;
5946 new->control_macro = 0;
5947 new->c_system_include_path = !p->cxx_aware;
5948 new->got_name_map = 0;
5949 append_include_chain (pfile, new, new);
5950 if (opts->first_system_include == 0)
5951 opts->first_system_include = new;
5955 /* Search ordinary names for GNU include directories. */
5956 for (p = include_defaults; p->fname; p++) {
5957 /* Some standard dirs are only for C++. */
5958 if (!p->cplusplus
5959 || (opts->cplusplus && !opts->no_standard_cplusplus_includes)) {
5960 struct file_name_list *new
5961 = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
5962 new->control_macro = 0;
5963 new->c_system_include_path = !p->cxx_aware;
5964 new->fname = p->fname;
5965 new->got_name_map = 0;
5966 append_include_chain (pfile, new, new);
5967 if (opts->first_system_include == 0)
5968 opts->first_system_include = new;
5973 /* Tack the after_include chain at the end of the include chain. */
5974 append_include_chain (pfile, opts->after_include, opts->last_after_include);
5975 if (opts->first_system_include == 0)
5976 opts->first_system_include = opts->after_include;
5978 /* With -v, print the list of dirs to search. */
5979 if (opts->verbose) {
5980 struct file_name_list *p;
5981 fprintf (stderr, "#include \"...\" search starts here:\n");
5982 for (p = opts->include; p; p = p->next) {
5983 if (p == opts->first_bracket_include)
5984 fprintf (stderr, "#include <...> search starts here:\n");
5985 fprintf (stderr, " %s\n", p->fname);
5987 fprintf (stderr, "End of search list.\n");
5990 /* Scan the -imacros files before the main input.
5991 Much like #including them, but with no_output set
5992 so that only their macro definitions matter. */
5994 opts->no_output++; pfile->no_record_file++;
5995 for (pend = opts->pending; pend; pend = pend->next)
5997 if (pend->cmd != NULL && strcmp (pend->cmd, "-imacros") == 0)
5999 int fd = open (pend->arg, O_RDONLY, 0666);
6000 if (fd < 0)
6002 cpp_perror_with_name (pfile, pend->arg);
6003 return FATAL_EXIT_CODE;
6005 cpp_push_buffer (pfile, NULL, 0);
6006 finclude (pfile, fd, pend->arg, 0, NULL_PTR);
6007 cpp_scan_buffer (pfile);
6010 opts->no_output--; pfile->no_record_file--;
6012 /* Copy the entire contents of the main input file into
6013 the stacked input buffer previously allocated for it. */
6014 if (fname == NULL || *fname == 0) {
6015 fname = "";
6016 f = 0;
6017 } else if ((f = open (fname, O_RDONLY, 0666)) < 0)
6018 cpp_pfatal_with_name (pfile, fname);
6020 /* -MG doesn't select the form of output and must be specified with one of
6021 -M or -MM. -MG doesn't make sense with -MD or -MMD since they don't
6022 inhibit compilation. */
6023 if (opts->print_deps_missing_files
6024 && (opts->print_deps == 0 || !opts->no_output))
6025 fatal (pfile, "-MG must be specified with one of -M or -MM");
6027 /* Either of two environment variables can specify output of deps.
6028 Its value is either "OUTPUT_FILE" or "OUTPUT_FILE DEPS_TARGET",
6029 where OUTPUT_FILE is the file to write deps info to
6030 and DEPS_TARGET is the target to mention in the deps. */
6032 if (opts->print_deps == 0
6033 && (getenv ("SUNPRO_DEPENDENCIES") != 0
6034 || getenv ("DEPENDENCIES_OUTPUT") != 0)) {
6035 char *spec = getenv ("DEPENDENCIES_OUTPUT");
6036 char *s;
6037 char *output_file;
6039 if (spec == 0)
6041 spec = getenv ("SUNPRO_DEPENDENCIES");
6042 opts->print_deps = 2;
6044 else
6045 opts->print_deps = 1;
6047 s = spec;
6048 /* Find the space before the DEPS_TARGET, if there is one. */
6049 /* This should use index. (mrs) */
6050 while (*s != 0 && *s != ' ') s++;
6051 if (*s != 0)
6053 opts->deps_target = s + 1;
6054 output_file = (char *) xmalloc (s - spec + 1);
6055 bcopy (spec, output_file, s - spec);
6056 output_file[s - spec] = 0;
6058 else
6060 opts->deps_target = 0;
6061 output_file = spec;
6064 opts->deps_file = output_file;
6065 opts->print_deps_append = 1;
6068 /* For -M, print the expected object file name
6069 as the target of this Make-rule. */
6070 if (opts->print_deps)
6072 pfile->deps_allocated_size = 200;
6073 pfile->deps_buffer = (char *) xmalloc (pfile->deps_allocated_size);
6074 pfile->deps_buffer[0] = 0;
6075 pfile->deps_size = 0;
6076 pfile->deps_column = 0;
6078 if (opts->deps_target)
6079 deps_output (pfile, opts->deps_target, ':');
6080 else if (*opts->in_fname == 0)
6081 deps_output (pfile, "-", ':');
6082 else
6084 char *p, *q;
6085 int len;
6087 /* Discard all directory prefixes from filename. */
6088 if ((q = rindex (opts->in_fname, '/')) != NULL
6089 #ifdef DIR_SEPARATOR
6090 && (q = rindex (opts->in_fname, DIR_SEPARATOR)) != NULL
6091 #endif
6093 ++q;
6094 else
6095 q = opts->in_fname;
6097 /* Copy remainder to mungable area. */
6098 p = (char *) alloca (strlen(q) + 8);
6099 strcpy (p, q);
6101 /* Output P, but remove known suffixes. */
6102 len = strlen (p);
6103 q = p + len;
6104 if (len >= 2
6105 && p[len - 2] == '.'
6106 && index("cCsSm", p[len - 1]))
6107 q = p + (len - 2);
6108 else if (len >= 3
6109 && p[len - 3] == '.'
6110 && p[len - 2] == 'c'
6111 && p[len - 1] == 'c')
6112 q = p + (len - 3);
6113 else if (len >= 4
6114 && p[len - 4] == '.'
6115 && p[len - 3] == 'c'
6116 && p[len - 2] == 'x'
6117 && p[len - 1] == 'x')
6118 q = p + (len - 4);
6119 else if (len >= 4
6120 && p[len - 4] == '.'
6121 && p[len - 3] == 'c'
6122 && p[len - 2] == 'p'
6123 && p[len - 1] == 'p')
6124 q = p + (len - 4);
6126 /* Supply our own suffix. */
6127 #ifndef VMS
6128 strcpy (q, ".o");
6129 #else
6130 strcpy (q, ".obj");
6131 #endif
6133 deps_output (pfile, p, ':');
6134 deps_output (pfile, opts->in_fname, ' ');
6138 #if 0
6139 /* Make sure data ends with a newline. And put a null after it. */
6141 if ((fp->length > 0 && fp->buf[fp->length - 1] != '\n')
6142 /* Backslash-newline at end is not good enough. */
6143 || (fp->length > 1 && fp->buf[fp->length - 2] == '\\')) {
6144 fp->buf[fp->length++] = '\n';
6145 missing_newline = 1;
6147 fp->buf[fp->length] = '\0';
6149 /* Unless inhibited, convert trigraphs in the input. */
6151 if (!no_trigraphs)
6152 trigraph_pcp (fp);
6153 #endif
6155 /* Scan the -include files before the main input.
6156 We push these in reverse order, so that the first one is handled first. */
6158 pfile->no_record_file++;
6159 opts->pending = nreverse_pending (opts->pending);
6160 for (pend = opts->pending; pend; pend = pend->next)
6162 if (pend->cmd != NULL && strcmp (pend->cmd, "-include") == 0)
6164 int fd = open (pend->arg, O_RDONLY, 0666);
6165 if (fd < 0)
6167 cpp_perror_with_name (pfile, pend->arg);
6168 return FATAL_EXIT_CODE;
6170 cpp_push_buffer (pfile, NULL, 0);
6171 finclude (pfile, fd, pend->arg, 0, NULL_PTR);
6174 pfile->no_record_file--;
6176 /* Free the pending list. */
6177 for (pend = opts->pending; pend; )
6179 struct cpp_pending *next = pend->next;
6180 free (pend);
6181 pend = next;
6183 opts->pending = NULL;
6185 #if 0
6186 /* Scan the input, processing macros and directives. */
6188 rescan (&outbuf, 0);
6190 if (missing_newline)
6191 fp->lineno--;
6193 if (CPP_PEDANTIC (pfile) && missing_newline)
6194 pedwarn ("file does not end in newline");
6196 #endif
6197 if (finclude (pfile, f, fname, 0, NULL_PTR))
6198 output_line_command (pfile, 0, same_file);
6199 return SUCCESS_EXIT_CODE;
6202 void
6203 init_parse_file (pfile)
6204 cpp_reader *pfile;
6206 bzero ((char *) pfile, sizeof (cpp_reader));
6207 pfile->get_token = cpp_get_token;
6209 pfile->token_buffer_size = 200;
6210 pfile->token_buffer = (U_CHAR*)xmalloc (pfile->token_buffer_size);
6211 CPP_SET_WRITTEN (pfile, 0);
6213 pfile->system_include_depth = 0;
6214 pfile->dont_repeat_files = 0;
6215 pfile->all_include_files = 0;
6216 pfile->max_include_len = 0;
6217 pfile->timebuf = NULL;
6218 pfile->only_seen_white = 1;
6219 pfile->buffer = CPP_NULL_BUFFER(pfile);
6222 static struct cpp_pending *
6223 nreverse_pending (list)
6224 struct cpp_pending *list;
6227 register struct cpp_pending *prev = 0, *next, *pend;
6228 for (pend = list; pend; pend = next)
6230 next = pend->next;
6231 pend->next = prev;
6232 prev = pend;
6234 return prev;
6237 static void
6238 push_pending (pfile, cmd, arg)
6239 cpp_reader *pfile;
6240 char *cmd;
6241 char *arg;
6243 struct cpp_pending *pend
6244 = (struct cpp_pending*)xmalloc (sizeof (struct cpp_pending));
6245 pend->cmd = cmd;
6246 pend->arg = arg;
6247 pend->next = CPP_OPTIONS (pfile)->pending;
6248 CPP_OPTIONS (pfile)->pending = pend;
6251 /* Handle command-line options in (argc, argv).
6252 Can be called multiple times, to handle multiple sets of options.
6253 Returns if an unrecognized option is seen.
6254 Returns number of handled arguments. */
6257 cpp_handle_options (pfile, argc, argv)
6258 cpp_reader *pfile;
6259 int argc;
6260 char **argv;
6262 int i;
6263 struct cpp_options *opts = CPP_OPTIONS (pfile);
6264 for (i = 0; i < argc; i++) {
6265 if (argv[i][0] != '-') {
6266 if (opts->out_fname != NULL)
6267 fatal ("Usage: %s [switches] input output", argv[0]);
6268 else if (opts->in_fname != NULL)
6269 opts->out_fname = argv[i];
6270 else
6271 opts->in_fname = argv[i];
6272 } else {
6273 switch (argv[i][1]) {
6275 case 'i':
6276 if (!strcmp (argv[i], "-include")
6277 || !strcmp (argv[i], "-imacros")) {
6278 if (i + 1 == argc)
6279 fatal ("Filename missing after `%s' option", argv[i]);
6280 else
6281 push_pending (pfile, argv[i], argv[i+1]), i++;
6283 if (!strcmp (argv[i], "-iprefix")) {
6284 if (i + 1 == argc)
6285 fatal ("Filename missing after `-iprefix' option");
6286 else
6287 opts->include_prefix = argv[++i];
6289 if (!strcmp (argv[i], "-ifoutput")) {
6290 opts->output_conditionals = 1;
6292 if (!strcmp (argv[i], "-isystem")) {
6293 struct file_name_list *dirtmp;
6295 if (i + 1 == argc)
6296 fatal ("Filename missing after `-isystem' option");
6298 dirtmp = (struct file_name_list *)
6299 xmalloc (sizeof (struct file_name_list));
6300 dirtmp->next = 0;
6301 dirtmp->control_macro = 0;
6302 dirtmp->c_system_include_path = 1;
6303 dirtmp->fname = (char *) xmalloc (strlen (argv[i+1]) + 1);
6304 strcpy (dirtmp->fname, argv[++i]);
6305 dirtmp->got_name_map = 0;
6307 if (opts->before_system == 0)
6308 opts->before_system = dirtmp;
6309 else
6310 opts->last_before_system->next = dirtmp;
6311 opts->last_before_system = dirtmp; /* Tail follows the last one */
6313 /* Add directory to end of path for includes,
6314 with the default prefix at the front of its name. */
6315 if (!strcmp (argv[i], "-iwithprefix")) {
6316 struct file_name_list *dirtmp;
6317 char *prefix;
6319 if (opts->include_prefix != 0)
6320 prefix = opts->include_prefix;
6321 else {
6322 prefix = savestring (GCC_INCLUDE_DIR);
6323 /* Remove the `include' from /usr/local/lib/gcc.../include. */
6324 if (!strcmp (prefix + strlen (prefix) - 8, "/include"))
6325 prefix[strlen (prefix) - 7] = 0;
6328 dirtmp = (struct file_name_list *)
6329 xmalloc (sizeof (struct file_name_list));
6330 dirtmp->next = 0; /* New one goes on the end */
6331 dirtmp->control_macro = 0;
6332 dirtmp->c_system_include_path = 0;
6333 if (i + 1 == argc)
6334 fatal ("Directory name missing after `-iwithprefix' option");
6336 dirtmp->fname = (char *) xmalloc (strlen (argv[i+1])
6337 + strlen (prefix) + 1);
6338 strcpy (dirtmp->fname, prefix);
6339 strcat (dirtmp->fname, argv[++i]);
6340 dirtmp->got_name_map = 0;
6342 if (opts->after_include == 0)
6343 opts->after_include = dirtmp;
6344 else
6345 opts->last_after_include->next = dirtmp;
6346 opts->last_after_include = dirtmp; /* Tail follows the last one */
6348 /* Add directory to main path for includes,
6349 with the default prefix at the front of its name. */
6350 if (!strcmp (argv[i], "-iwithprefixbefore")) {
6351 struct file_name_list *dirtmp;
6352 char *prefix;
6354 if (opts->include_prefix != 0)
6355 prefix = opts->include_prefix;
6356 else {
6357 prefix = savestring (GCC_INCLUDE_DIR);
6358 /* Remove the `include' from /usr/local/lib/gcc.../include. */
6359 if (!strcmp (prefix + strlen (prefix) - 8, "/include"))
6360 prefix[strlen (prefix) - 7] = 0;
6363 dirtmp = (struct file_name_list *)
6364 xmalloc (sizeof (struct file_name_list));
6365 dirtmp->next = 0; /* New one goes on the end */
6366 dirtmp->control_macro = 0;
6367 dirtmp->c_system_include_path = 0;
6368 if (i + 1 == argc)
6369 fatal ("Directory name missing after `-iwithprefixbefore' option");
6371 dirtmp->fname = (char *) xmalloc (strlen (argv[i+1])
6372 + strlen (prefix) + 1);
6373 strcpy (dirtmp->fname, prefix);
6374 strcat (dirtmp->fname, argv[++i]);
6375 dirtmp->got_name_map = 0;
6377 append_include_chain (pfile, dirtmp, dirtmp);
6379 /* Add directory to end of path for includes. */
6380 if (!strcmp (argv[i], "-idirafter")) {
6381 struct file_name_list *dirtmp;
6383 dirtmp = (struct file_name_list *)
6384 xmalloc (sizeof (struct file_name_list));
6385 dirtmp->next = 0; /* New one goes on the end */
6386 dirtmp->control_macro = 0;
6387 dirtmp->c_system_include_path = 0;
6388 if (i + 1 == argc)
6389 fatal ("Directory name missing after `-idirafter' option");
6390 else
6391 dirtmp->fname = argv[++i];
6392 dirtmp->got_name_map = 0;
6394 if (opts->after_include == 0)
6395 opts->after_include = dirtmp;
6396 else
6397 opts->last_after_include->next = dirtmp;
6398 opts->last_after_include = dirtmp; /* Tail follows the last one */
6400 break;
6402 case 'o':
6403 if (opts->out_fname != NULL)
6404 fatal ("Output filename specified twice");
6405 if (i + 1 == argc)
6406 fatal ("Filename missing after -o option");
6407 opts->out_fname = argv[++i];
6408 if (!strcmp (opts->out_fname, "-"))
6409 opts->out_fname = "";
6410 break;
6412 case 'p':
6413 if (!strcmp (argv[i], "-pedantic"))
6414 CPP_PEDANTIC (pfile) = 1;
6415 else if (!strcmp (argv[i], "-pedantic-errors")) {
6416 CPP_PEDANTIC (pfile) = 1;
6417 opts->pedantic_errors = 1;
6419 #if 0
6420 else if (!strcmp (argv[i], "-pcp")) {
6421 char *pcp_fname = argv[++i];
6422 pcp_outfile =
6423 ((pcp_fname[0] != '-' || pcp_fname[1] != '\0')
6424 ? fopen (pcp_fname, "w")
6425 : fdopen (dup (fileno (stdout)), "w"));
6426 if (pcp_outfile == 0)
6427 cpp_pfatal_with_name (pfile, pcp_fname);
6428 no_precomp = 1;
6430 #endif
6431 break;
6433 case 't':
6434 if (!strcmp (argv[i], "-traditional")) {
6435 opts->traditional = 1;
6436 if (opts->dollars_in_ident > 0)
6437 opts->dollars_in_ident = 1;
6438 } else if (!strcmp (argv[i], "-trigraphs")) {
6439 if (!opts->chill)
6440 opts->no_trigraphs = 0;
6442 break;
6444 case 'l':
6445 if (! strcmp (argv[i], "-lang-c"))
6446 opts->cplusplus = 0, opts->cplusplus_comments = 0, opts->objc = 0;
6447 if (! strcmp (argv[i], "-lang-c++"))
6448 opts->cplusplus = 1, opts->cplusplus_comments = 1, opts->objc = 0;
6449 if (! strcmp (argv[i], "-lang-c-c++-comments"))
6450 opts->cplusplus = 0, opts->cplusplus_comments = 1, opts->objc = 0;
6451 if (! strcmp (argv[i], "-lang-objc"))
6452 opts->objc = 1, opts->cplusplus = 0, opts->cplusplus_comments = 1;
6453 if (! strcmp (argv[i], "-lang-objc++"))
6454 opts->objc = 1, opts->cplusplus = 1, opts->cplusplus_comments = 1;
6455 if (! strcmp (argv[i], "-lang-asm"))
6456 opts->lang_asm = 1;
6457 if (! strcmp (argv[i], "-lint"))
6458 opts->for_lint = 1;
6459 if (! strcmp (argv[i], "-lang-chill"))
6460 opts->objc = 0, opts->cplusplus = 0, opts->chill = 1,
6461 opts->traditional = 1, opts->no_trigraphs = 1;
6462 break;
6464 case '+':
6465 opts->cplusplus = 1, opts->cplusplus_comments = 1;
6466 break;
6468 case 'w':
6469 opts->inhibit_warnings = 1;
6470 break;
6472 case 'W':
6473 if (!strcmp (argv[i], "-Wtrigraphs"))
6474 opts->warn_trigraphs = 1;
6475 else if (!strcmp (argv[i], "-Wno-trigraphs"))
6476 opts->warn_trigraphs = 0;
6477 else if (!strcmp (argv[i], "-Wcomment"))
6478 opts->warn_comments = 1;
6479 else if (!strcmp (argv[i], "-Wno-comment"))
6480 opts->warn_comments = 0;
6481 else if (!strcmp (argv[i], "-Wcomments"))
6482 opts->warn_comments = 1;
6483 else if (!strcmp (argv[i], "-Wno-comments"))
6484 opts->warn_comments = 0;
6485 else if (!strcmp (argv[i], "-Wtraditional"))
6486 opts->warn_stringify = 1;
6487 else if (!strcmp (argv[i], "-Wno-traditional"))
6488 opts->warn_stringify = 0;
6489 else if (!strcmp (argv[i], "-Wimport"))
6490 opts->warn_import = 1;
6491 else if (!strcmp (argv[i], "-Wno-import"))
6492 opts->warn_import = 0;
6493 else if (!strcmp (argv[i], "-Werror"))
6494 opts->warnings_are_errors = 1;
6495 else if (!strcmp (argv[i], "-Wno-error"))
6496 opts->warnings_are_errors = 0;
6497 else if (!strcmp (argv[i], "-Wall"))
6499 opts->warn_trigraphs = 1;
6500 opts->warn_comments = 1;
6502 break;
6504 case 'M':
6505 /* The style of the choices here is a bit mixed.
6506 The chosen scheme is a hybrid of keeping all options in one string
6507 and specifying each option in a separate argument:
6508 -M|-MM|-MD file|-MMD file [-MG]. An alternative is:
6509 -M|-MM|-MD file|-MMD file|-MG|-MMG; or more concisely:
6510 -M[M][G][D file]. This is awkward to handle in specs, and is not
6511 as extensible. */
6512 /* ??? -MG must be specified in addition to one of -M or -MM.
6513 This can be relaxed in the future without breaking anything.
6514 The converse isn't true. */
6516 /* -MG isn't valid with -MD or -MMD. This is checked for later. */
6517 if (!strcmp (argv[i], "-MG"))
6519 opts->print_deps_missing_files = 1;
6520 break;
6522 if (!strcmp (argv[i], "-M"))
6523 opts->print_deps = 2;
6524 else if (!strcmp (argv[i], "-MM"))
6525 opts->print_deps = 1;
6526 else if (!strcmp (argv[i], "-MD"))
6527 opts->print_deps = 2;
6528 else if (!strcmp (argv[i], "-MMD"))
6529 opts->print_deps = 1;
6530 /* For -MD and -MMD options, write deps on file named by next arg. */
6531 if (!strcmp (argv[i], "-MD") || !strcmp (argv[i], "-MMD"))
6533 if (i+1 == argc)
6534 fatal ("Filename missing after %s option", argv[i]);
6535 opts->deps_file = argv[++i];
6537 else
6539 /* For -M and -MM, write deps on standard output
6540 and suppress the usual output. */
6541 opts->no_output = 1;
6543 break;
6545 case 'd':
6547 char *p = argv[i] + 2;
6548 char c;
6549 while ((c = *p++) != 0) {
6550 /* Arg to -d specifies what parts of macros to dump */
6551 switch (c) {
6552 case 'M':
6553 opts->dump_macros = dump_only;
6554 opts->no_output = 1;
6555 break;
6556 case 'N':
6557 opts->dump_macros = dump_names;
6558 break;
6559 case 'D':
6560 opts->dump_macros = dump_definitions;
6561 break;
6565 break;
6567 case 'g':
6568 if (argv[i][2] == '3')
6569 opts->debug_output = 1;
6570 break;
6572 case 'v':
6573 fprintf (stderr, "GNU CPP version %s", version_string);
6574 #ifdef TARGET_VERSION
6575 TARGET_VERSION;
6576 #endif
6577 fprintf (stderr, "\n");
6578 opts->verbose = 1;
6579 break;
6581 case 'H':
6582 opts->print_include_names = 1;
6583 break;
6585 case 'D':
6586 if (argv[i][2] != 0)
6587 push_pending (pfile, "-D", argv[i] + 2);
6588 else if (i + 1 == argc)
6589 fatal ("Macro name missing after -D option");
6590 else
6591 i++, push_pending (pfile, "-D", argv[i]);
6592 break;
6594 case 'A':
6596 char *p;
6598 if (argv[i][2] != 0)
6599 p = argv[i] + 2;
6600 else if (i + 1 == argc)
6601 fatal ("Assertion missing after -A option");
6602 else
6603 p = argv[++i];
6605 if (!strcmp (p, "-")) {
6606 struct cpp_pending **ptr;
6607 /* -A- eliminates all predefined macros and assertions.
6608 Let's include also any that were specified earlier
6609 on the command line. That way we can get rid of any
6610 that were passed automatically in from GCC. */
6611 int j;
6612 opts->inhibit_predefs = 1;
6613 for (ptr = &opts->pending; *ptr != NULL; )
6615 struct cpp_pending *pend = *ptr;
6616 if (pend->cmd && pend->cmd[0] == '-'
6617 && (pend->cmd[1] == 'D' || pend->cmd[1] == 'A'))
6619 *ptr = pend->next;
6620 free (pend);
6622 else
6623 ptr = &pend->next;
6625 } else {
6626 push_pending (pfile, "-A", p);
6629 break;
6631 case 'U': /* JF #undef something */
6632 if (argv[i][2] != 0)
6633 push_pending (pfile, "-U", argv[i] + 2);
6634 else if (i + 1 == argc)
6635 fatal ("Macro name missing after -U option");
6636 else
6637 push_pending (pfile, "-U", argv[i+1]), i++;
6638 break;
6640 case 'C':
6641 opts->put_out_comments = 1;
6642 break;
6644 case 'E': /* -E comes from cc -E; ignore it. */
6645 break;
6647 case 'P':
6648 opts->no_line_commands = 1;
6649 break;
6651 case '$': /* Don't include $ in identifiers. */
6652 opts->dollars_in_ident = 0;
6653 break;
6655 case 'I': /* Add directory to path for includes. */
6657 struct file_name_list *dirtmp;
6659 if (! CPP_OPTIONS(pfile)->ignore_srcdir
6660 && !strcmp (argv[i] + 2, "-")) {
6661 CPP_OPTIONS (pfile)->ignore_srcdir = 1;
6662 /* Don't use any preceding -I directories for #include <...>. */
6663 CPP_OPTIONS (pfile)->first_bracket_include = 0;
6665 else {
6666 dirtmp = (struct file_name_list *)
6667 xmalloc (sizeof (struct file_name_list));
6668 dirtmp->next = 0; /* New one goes on the end */
6669 dirtmp->control_macro = 0;
6670 dirtmp->c_system_include_path = 0;
6671 if (argv[i][2] != 0)
6672 dirtmp->fname = argv[i] + 2;
6673 else if (i + 1 == argc)
6674 fatal ("Directory name missing after -I option");
6675 else
6676 dirtmp->fname = argv[++i];
6677 dirtmp->got_name_map = 0;
6678 append_include_chain (pfile, dirtmp, dirtmp);
6681 break;
6683 case 'n':
6684 if (!strcmp (argv[i], "-nostdinc"))
6685 /* -nostdinc causes no default include directories.
6686 You must specify all include-file directories with -I. */
6687 opts->no_standard_includes = 1;
6688 else if (!strcmp (argv[i], "-nostdinc++"))
6689 /* -nostdinc++ causes no default C++-specific include directories. */
6690 opts->no_standard_cplusplus_includes = 1;
6691 #if 0
6692 else if (!strcmp (argv[i], "-noprecomp"))
6693 no_precomp = 1;
6694 #endif
6695 break;
6697 case 'u':
6698 /* Sun compiler passes undocumented switch "-undef".
6699 Let's assume it means to inhibit the predefined symbols. */
6700 opts->inhibit_predefs = 1;
6701 break;
6703 case '\0': /* JF handle '-' as file name meaning stdin or stdout */
6704 if (opts->in_fname == NULL) {
6705 opts->in_fname = "";
6706 break;
6707 } else if (opts->out_fname == NULL) {
6708 opts->out_fname = "";
6709 break;
6710 } /* else fall through into error */
6712 default:
6713 return i;
6717 return i;
6720 void
6721 cpp_finish (pfile)
6722 cpp_reader *pfile;
6724 struct cpp_options *opts = CPP_OPTIONS (pfile);
6726 if (opts->print_deps)
6728 /* Stream on which to print the dependency information. */
6729 FILE *deps_stream;
6731 /* Don't actually write the deps file if compilation has failed. */
6732 if (pfile->errors == 0)
6734 char *deps_mode = opts->print_deps_append ? "a" : "w";
6735 if (opts->deps_file == 0)
6736 deps_stream = stdout;
6737 else if ((deps_stream = fopen (opts->deps_file, deps_mode)) == 0)
6738 cpp_pfatal_with_name (pfile, opts->deps_file);
6739 fputs (pfile->deps_buffer, deps_stream);
6740 putc ('\n', deps_stream);
6741 if (opts->deps_file)
6743 if (ferror (deps_stream) || fclose (deps_stream) != 0)
6744 fatal ("I/O error on output");
6750 /* Free resources used by PFILE. */
6752 void
6753 cpp_cleanup (pfile)
6754 cpp_reader *pfile;
6756 int i;
6757 while ( CPP_BUFFER (pfile) != CPP_NULL_BUFFER (pfile))
6758 cpp_pop_buffer (pfile);
6760 if (pfile->token_buffer)
6762 free (pfile->token_buffer);
6763 pfile->token_buffer = NULL;
6766 if (pfile->deps_buffer)
6768 free (pfile->deps_buffer);
6769 pfile->deps_buffer = NULL;
6770 pfile->deps_allocated_size = 0;
6773 while (pfile->if_stack)
6775 IF_STACK_FRAME *temp = pfile->if_stack;
6776 pfile->if_stack = temp->next;
6777 free (temp);
6780 while (pfile->dont_repeat_files)
6782 struct file_name_list *temp = pfile->dont_repeat_files;
6783 pfile->dont_repeat_files = temp->next;
6784 free (temp->fname);
6785 free (temp);
6788 while (pfile->all_include_files)
6790 struct file_name_list *temp = pfile->all_include_files;
6791 pfile->all_include_files = temp->next;
6792 free (temp->fname);
6793 free (temp);
6796 for (i = IMPORT_HASH_SIZE; --i >= 0; )
6798 register struct import_file *imp = pfile->import_hash_table[i];
6799 while (imp)
6801 struct import_file *next = imp->next;
6802 free (imp->name);
6803 free (imp);
6804 imp = next;
6806 pfile->import_hash_table[i] = 0;
6809 for (i = ASSERTION_HASHSIZE; --i >= 0; )
6811 while (pfile->assertion_hashtab[i])
6812 delete_assertion (pfile->assertion_hashtab[i]);
6815 cpp_hash_cleanup (pfile);
6818 static int
6819 do_assert (pfile, keyword, buf, limit)
6820 cpp_reader *pfile;
6821 struct directive *keyword;
6822 U_CHAR *buf, *limit;
6824 long symstart; /* remember where symbol name starts */
6825 int c;
6826 int sym_length; /* and how long it is */
6827 struct arglist *tokens = NULL;
6829 if (CPP_PEDANTIC (pfile) && CPP_OPTIONS (pfile)->done_initializing
6830 && !CPP_BUFFER (pfile)->system_header_p)
6831 cpp_pedwarn (pfile, "ANSI C does not allow `#assert'");
6833 cpp_skip_hspace (pfile);
6834 symstart = CPP_WRITTEN (pfile); /* remember where it starts */
6835 parse_name (pfile, GETC());
6836 sym_length = check_macro_name (pfile, pfile->token_buffer + symstart,
6837 "assertion");
6839 cpp_skip_hspace (pfile);
6840 if (PEEKC() != '(') {
6841 cpp_error (pfile, "missing token-sequence in `#assert'");
6842 goto error;
6846 int error_flag = 0;
6847 tokens = read_token_list (pfile, &error_flag);
6848 if (error_flag)
6849 goto error;
6850 if (tokens == 0) {
6851 cpp_error (pfile, "empty token-sequence in `#assert'");
6852 goto error;
6854 cpp_skip_hspace (pfile);
6855 c = PEEKC ();
6856 if (c != EOF && c != '\n')
6857 cpp_pedwarn (pfile, "junk at end of `#assert'");
6858 skip_rest_of_line (pfile);
6861 /* If this name isn't already an assertion name, make it one.
6862 Error if it was already in use in some other way. */
6865 ASSERTION_HASHNODE *hp;
6866 U_CHAR *symname = pfile->token_buffer + symstart;
6867 int hashcode = hashf (symname, sym_length, ASSERTION_HASHSIZE);
6868 struct tokenlist_list *value
6869 = (struct tokenlist_list *) xmalloc (sizeof (struct tokenlist_list));
6871 hp = assertion_lookup (pfile, symname, sym_length, hashcode);
6872 if (hp == NULL) {
6873 if (sym_length == 7 && ! strncmp (symname, "defined", sym_length))
6874 cpp_error (pfile, "`defined' redefined as assertion");
6875 hp = assertion_install (pfile, symname, sym_length, hashcode);
6878 /* Add the spec'd token-sequence to the list of such. */
6879 value->tokens = tokens;
6880 value->next = hp->value;
6881 hp->value = value;
6883 CPP_SET_WRITTEN (pfile, symstart); /* Pop */
6884 return 0;
6885 error:
6886 CPP_SET_WRITTEN (pfile, symstart); /* Pop */
6887 skip_rest_of_line (pfile);
6888 return 1;
6891 static int
6892 do_unassert (pfile, keyword, buf, limit)
6893 cpp_reader *pfile;
6894 struct directive *keyword;
6895 U_CHAR *buf, *limit;
6897 long symstart; /* remember where symbol name starts */
6898 int sym_length; /* and how long it is */
6899 int c;
6901 struct arglist *tokens = NULL;
6902 int tokens_specified = 0;
6904 if (CPP_PEDANTIC (pfile) && CPP_OPTIONS (pfile)->done_initializing
6905 && !CPP_BUFFER (pfile)->system_header_p)
6906 cpp_pedwarn (pfile, "ANSI C does not allow `#unassert'");
6908 cpp_skip_hspace (pfile);
6910 symstart = CPP_WRITTEN (pfile); /* remember where it starts */
6911 parse_name (pfile, GETC());
6912 sym_length = check_macro_name (pfile, pfile->token_buffer + symstart,
6913 "assertion");
6915 cpp_skip_hspace (pfile);
6916 if (PEEKC() == '(') {
6917 int error_flag = 0;
6919 tokens = read_token_list (pfile, &error_flag);
6920 if (error_flag)
6921 goto error;
6922 if (tokens == 0) {
6923 cpp_error (pfile, "empty token list in `#unassert'");
6924 goto error;
6927 tokens_specified = 1;
6930 cpp_skip_hspace (pfile);
6931 c = PEEKC ();
6932 if (c != EOF && c != '\n')
6933 cpp_error (pfile, "junk at end of `#unassert'");
6934 skip_rest_of_line (pfile);
6937 ASSERTION_HASHNODE *hp;
6938 U_CHAR *symname = pfile->token_buffer + symstart;
6939 int hashcode = hashf (symname, sym_length, ASSERTION_HASHSIZE);
6940 struct tokenlist_list *tail, *prev;
6942 hp = assertion_lookup (pfile, symname, sym_length, hashcode);
6943 if (hp == NULL)
6944 return 1;
6946 /* If no token list was specified, then eliminate this assertion
6947 entirely. */
6948 if (! tokens_specified)
6949 delete_assertion (hp);
6950 else {
6951 /* If a list of tokens was given, then delete any matching list. */
6953 tail = hp->value;
6954 prev = 0;
6955 while (tail) {
6956 struct tokenlist_list *next = tail->next;
6957 if (compare_token_lists (tail->tokens, tokens)) {
6958 if (prev)
6959 prev->next = next;
6960 else
6961 hp->value = tail->next;
6962 free_token_list (tail->tokens);
6963 free (tail);
6964 } else {
6965 prev = tail;
6967 tail = next;
6972 CPP_SET_WRITTEN (pfile, symstart); /* Pop */
6973 return 0;
6974 error:
6975 CPP_SET_WRITTEN (pfile, symstart); /* Pop */
6976 skip_rest_of_line (pfile);
6977 return 1;
6980 /* Test whether there is an assertion named NAME
6981 and optionally whether it has an asserted token list TOKENS.
6982 NAME is not null terminated; its length is SYM_LENGTH.
6983 If TOKENS_SPECIFIED is 0, then don't check for any token list. */
6986 check_assertion (pfile, name, sym_length, tokens_specified, tokens)
6987 cpp_reader *pfile;
6988 U_CHAR *name;
6989 int sym_length;
6990 int tokens_specified;
6991 struct arglist *tokens;
6993 ASSERTION_HASHNODE *hp;
6994 int hashcode = hashf (name, sym_length, ASSERTION_HASHSIZE);
6996 if (CPP_PEDANTIC (pfile) && !CPP_BUFFER (pfile)->system_header_p)
6997 cpp_pedwarn (pfile, "ANSI C does not allow testing assertions");
6999 hp = assertion_lookup (pfile, name, sym_length, hashcode);
7000 if (hp == NULL)
7001 /* It is not an assertion; just return false. */
7002 return 0;
7004 /* If no token list was specified, then value is 1. */
7005 if (! tokens_specified)
7006 return 1;
7009 struct tokenlist_list *tail;
7011 tail = hp->value;
7013 /* If a list of tokens was given,
7014 then succeed if the assertion records a matching list. */
7016 while (tail) {
7017 if (compare_token_lists (tail->tokens, tokens))
7018 return 1;
7019 tail = tail->next;
7022 /* Fail if the assertion has no matching list. */
7023 return 0;
7027 /* Compare two lists of tokens for equality including order of tokens. */
7029 static int
7030 compare_token_lists (l1, l2)
7031 struct arglist *l1, *l2;
7033 while (l1 && l2) {
7034 if (l1->length != l2->length)
7035 return 0;
7036 if (strncmp (l1->name, l2->name, l1->length))
7037 return 0;
7038 l1 = l1->next;
7039 l2 = l2->next;
7042 /* Succeed if both lists end at the same time. */
7043 return l1 == l2;
7046 struct arglist *
7047 reverse_token_list (tokens)
7048 struct arglist *tokens;
7050 register struct arglist *prev = 0, *this, *next;
7051 for (this = tokens; this; this = next)
7053 next = this->next;
7054 this->next = prev;
7055 prev = this;
7057 return prev;
7060 /* Read a space-separated list of tokens ending in a close parenthesis.
7061 Return a list of strings, in the order they were written.
7062 (In case of error, return 0 and store -1 in *ERROR_FLAG.) */
7064 static struct arglist *
7065 read_token_list (pfile, error_flag)
7066 cpp_reader *pfile;
7067 int *error_flag;
7069 struct arglist *token_ptrs = 0;
7070 int depth = 1;
7071 int length;
7073 *error_flag = 0;
7074 FORWARD (1); /* Skip '(' */
7076 /* Loop over the assertion value tokens. */
7077 while (depth > 0)
7079 struct arglist *temp;
7080 long name_written = CPP_WRITTEN (pfile);
7081 int eofp = 0; int c;
7083 cpp_skip_hspace (pfile);
7085 c = GETC ();
7087 /* Find the end of the token. */
7088 if (c == '(')
7090 CPP_PUTC (pfile, c);
7091 depth++;
7093 else if (c == ')')
7095 depth--;
7096 if (depth == 0)
7097 break;
7098 CPP_PUTC (pfile, c);
7100 else if (c == '"' || c == '\'')
7102 FORWARD(-1);
7103 cpp_get_token (pfile);
7105 else if (c == '\n')
7106 break;
7107 else
7109 while (c != EOF && ! is_space[c] && c != '(' && c != ')'
7110 && c != '"' && c != '\'')
7112 CPP_PUTC (pfile, c);
7113 c = GETC();
7115 if (c != EOF) FORWARD(-1);
7118 length = CPP_WRITTEN (pfile) - name_written;
7119 temp = (struct arglist *)
7120 xmalloc (sizeof (struct arglist) + length + 1);
7121 temp->name = (U_CHAR *) (temp + 1);
7122 bcopy ((char *) (pfile->token_buffer + name_written),
7123 (char *) temp->name, length);
7124 temp->name[length] = 0;
7125 temp->next = token_ptrs;
7126 token_ptrs = temp;
7127 temp->length = length;
7129 CPP_ADJUST_WRITTEN (pfile, -length); /* pop */
7131 if (c == EOF || c == '\n')
7132 { /* FIXME */
7133 cpp_error (pfile,
7134 "unterminated token sequence following `#' operator");
7135 return 0;
7139 /* We accumulated the names in reverse order.
7140 Now reverse them to get the proper order. */
7141 return reverse_token_list (token_ptrs);
7144 static void
7145 free_token_list (tokens)
7146 struct arglist *tokens;
7148 while (tokens) {
7149 struct arglist *next = tokens->next;
7150 free (tokens->name);
7151 free (tokens);
7152 tokens = next;
7156 /* Get the file-mode and data size of the file open on FD
7157 and store them in *MODE_POINTER and *SIZE_POINTER. */
7159 static int
7160 file_size_and_mode (fd, mode_pointer, size_pointer)
7161 int fd;
7162 int *mode_pointer;
7163 long int *size_pointer;
7165 struct stat sbuf;
7167 if (fstat (fd, &sbuf) < 0) return (-1);
7168 if (mode_pointer) *mode_pointer = sbuf.st_mode;
7169 if (size_pointer) *size_pointer = sbuf.st_size;
7170 return 0;
7173 /* Read LEN bytes at PTR from descriptor DESC, for file FILENAME,
7174 retrying if necessary. Return a negative value if an error occurs,
7175 otherwise return the actual number of bytes read,
7176 which must be LEN unless end-of-file was reached. */
7178 static int
7179 safe_read (desc, ptr, len)
7180 int desc;
7181 char *ptr;
7182 int len;
7184 int left = len;
7185 while (left > 0) {
7186 int nchars = read (desc, ptr, left);
7187 if (nchars < 0)
7189 #ifdef EINTR
7190 if (errno == EINTR)
7191 continue;
7192 #endif
7193 return nchars;
7195 if (nchars == 0)
7196 break;
7197 ptr += nchars;
7198 left -= nchars;
7200 return len - left;
7203 static char *
7204 savestring (input)
7205 char *input;
7207 unsigned size = strlen (input);
7208 char *output = xmalloc (size + 1);
7209 strcpy (output, input);
7210 return output;
7213 /* Initialize PMARK to remember the current position of PFILE. */
7214 void
7215 parse_set_mark (pmark, pfile)
7216 struct parse_marker *pmark;
7217 cpp_reader *pfile;
7219 cpp_buffer *pbuf = CPP_BUFFER (pfile);
7220 pmark->next = pbuf->marks;
7221 pbuf->marks = pmark;
7222 pmark->buf = pbuf;
7223 pmark->position = pbuf->cur - pbuf->buf;
7226 /* Cleanup PMARK - we no longer need it. */
7227 void
7228 parse_clear_mark (pmark)
7229 struct parse_marker *pmark;
7231 struct parse_marker **pp = &pmark->buf->marks;
7232 for (; ; pp = &(*pp)->next) {
7233 if (*pp == NULL) fatal ("internal error", "in parse_set_mark");
7234 if (*pp == pmark) break;
7236 *pp = pmark->next;
7239 /* Backup the current position of PFILE to that saved in PMARK. */
7241 void
7242 parse_goto_mark (pmark, pfile)
7243 struct parse_marker *pmark;
7244 cpp_reader *pfile;
7246 cpp_buffer *pbuf = CPP_BUFFER (pfile);
7247 if (pbuf != pmark->buf)
7248 fatal ("internal error %s", "parse_goto_mark");
7249 pbuf->cur = pbuf->buf + pmark->position;
7252 /* Reset PMARK to point to the current position of PFILE. (Same
7253 as parse_clear_mark (PMARK), parse_set_mark (PMARK, PFILE) but faster. */
7255 void
7256 parse_move_mark (pmark, pfile)
7257 struct parse_marker *pmark;
7258 cpp_reader *pfile;
7260 cpp_buffer *pbuf = CPP_BUFFER (pfile);
7261 if (pbuf != pmark->buf)
7262 fatal ("internal error %s", "parse_move_mark");
7263 pmark->position = pbuf->cur - pbuf->buf;
7267 cpp_read_check_assertion (pfile)
7268 cpp_reader *pfile;
7270 int name_start = CPP_WRITTEN (pfile);
7271 int name_length, name_written;
7272 int result;
7273 FORWARD (1); /* Skip '#' */
7274 cpp_skip_hspace (pfile);
7275 parse_name (pfile, GETC ());
7276 name_written = CPP_WRITTEN (pfile);
7277 name_length = name_written - name_start;
7278 cpp_skip_hspace (pfile);
7279 if (CPP_BUF_PEEK (CPP_BUFFER (pfile)) == '(')
7281 int error_flag;
7282 struct arglist *token_ptrs = read_token_list (pfile, &error_flag);
7283 result = check_assertion (pfile,
7284 pfile->token_buffer + name_start, name_length,
7285 1, token_ptrs);
7287 else
7288 result = check_assertion (pfile,
7289 pfile->token_buffer + name_start, name_length,
7290 0, NULL_PTR);
7291 CPP_ADJUST_WRITTEN (pfile, - name_length); /* pop */
7292 return result;
7295 void
7296 cpp_print_file_and_line (pfile)
7297 cpp_reader *pfile;
7299 cpp_buffer *ip = cpp_file_buffer (pfile);
7301 if (ip != NULL)
7303 long line, col;
7304 cpp_buf_line_and_col (ip, &line, &col);
7305 cpp_file_line_for_message (pfile, ip->nominal_fname,
7306 line, pfile->show_column ? col : -1);
7310 void
7311 cpp_error (pfile, msg, arg1, arg2, arg3)
7312 cpp_reader *pfile;
7313 char *msg;
7314 char *arg1, *arg2, *arg3;
7316 cpp_print_containing_files (pfile);
7317 cpp_print_file_and_line (pfile);
7318 cpp_message (pfile, 1, msg, arg1, arg2, arg3);
7321 /* Print error message but don't count it. */
7323 void
7324 cpp_warning (pfile, msg, arg1, arg2, arg3)
7325 cpp_reader *pfile;
7326 char *msg;
7327 char *arg1, *arg2, *arg3;
7329 if (CPP_OPTIONS (pfile)->inhibit_warnings)
7330 return;
7332 if (CPP_OPTIONS (pfile)->warnings_are_errors)
7333 pfile->errors++;
7335 cpp_print_containing_files (pfile);
7336 cpp_print_file_and_line (pfile);
7337 cpp_message (pfile, 0, msg, arg1, arg2, arg3);
7340 /* Print an error message and maybe count it. */
7342 void
7343 cpp_pedwarn (pfile, msg, arg1, arg2, arg3)
7344 cpp_reader *pfile;
7345 char *msg;
7346 char *arg1, *arg2, *arg3;
7348 if (CPP_OPTIONS (pfile)->pedantic_errors)
7349 cpp_error (pfile, msg, arg1, arg2, arg3);
7350 else
7351 cpp_warning (pfile, msg, arg1, arg2, arg3);
7354 void
7355 cpp_error_with_line (pfile, line, column, msg, arg1, arg2, arg3)
7356 cpp_reader *pfile;
7357 int line, column;
7358 char *msg;
7359 char *arg1, *arg2, *arg3;
7361 int i;
7362 cpp_buffer *ip = cpp_file_buffer (pfile);
7364 cpp_print_containing_files (pfile);
7366 if (ip != NULL)
7367 cpp_file_line_for_message (pfile, ip->nominal_fname, line, column);
7369 cpp_message (pfile, 1, msg, arg1, arg2, arg3);
7372 static void
7373 cpp_warning_with_line (pfile, line, column, msg, arg1, arg2, arg3)
7374 cpp_reader *pfile;
7375 int line, column;
7376 char *msg;
7377 char *arg1, *arg2, *arg3;
7379 int i;
7380 cpp_buffer *ip;
7382 if (CPP_OPTIONS (pfile)->inhibit_warnings)
7383 return;
7385 if (CPP_OPTIONS (pfile)->warnings_are_errors)
7386 pfile->errors++;
7388 cpp_print_containing_files (pfile);
7390 ip = cpp_file_buffer (pfile);
7392 if (ip != NULL)
7393 cpp_file_line_for_message (pfile, ip->nominal_fname, line, column);
7395 cpp_message (pfile, 0, msg, arg1, arg2, arg3);
7398 void
7399 cpp_pedwarn_with_line (pfile, line, column, msg, arg1, arg2, arg3)
7400 cpp_reader *pfile;
7401 int line;
7402 char *msg;
7403 char *arg1, *arg2, *arg3;
7405 if (CPP_OPTIONS (pfile)->pedantic_errors)
7406 cpp_error_with_line (pfile, column, line, msg, arg1, arg2, arg3);
7407 else
7408 cpp_warning_with_line (pfile, line, column, msg, arg1, arg2, arg3);
7411 /* Report a warning (or an error if pedantic_errors)
7412 giving specified file name and line number, not current. */
7414 void
7415 cpp_pedwarn_with_file_and_line (pfile, file, line, msg, arg1, arg2, arg3)
7416 cpp_reader *pfile;
7417 char *file;
7418 int line;
7419 char *msg;
7420 char *arg1, *arg2, *arg3;
7422 if (!CPP_OPTIONS (pfile)->pedantic_errors
7423 && CPP_OPTIONS (pfile)->inhibit_warnings)
7424 return;
7425 if (file != NULL)
7426 cpp_file_line_for_message (pfile, file, line, -1);
7427 cpp_message (pfile, CPP_OPTIONS (pfile)->pedantic_errors,
7428 msg, arg1, arg2, arg3);
7431 /* This defines "errno" properly for VMS, and gives us EACCES. */
7432 #include <errno.h>
7433 #ifndef errno
7434 extern int errno;
7435 #endif
7437 #ifndef VMS
7438 #ifndef HAVE_STRERROR
7439 extern int sys_nerr;
7440 #if defined(bsd4_4)
7441 extern const char *const sys_errlist[];
7442 #else
7443 extern char *sys_errlist[];
7444 #endif
7445 #else /* HAVE_STRERROR */
7446 char *strerror ();
7447 #endif
7448 #else /* VMS */
7449 char *strerror (int,...);
7450 #endif
7453 * my_strerror - return the descriptive text associated with an `errno' code.
7456 char *
7457 my_strerror (errnum)
7458 int errnum;
7460 char *result;
7462 #ifndef VMS
7463 #ifndef HAVE_STRERROR
7464 result = (char *) ((errnum < sys_nerr) ? sys_errlist[errnum] : 0);
7465 #else
7466 result = strerror (errnum);
7467 #endif
7468 #else /* VMS */
7469 /* VAXCRTL's strerror() takes an optional second argument, which only
7470 matters when the first argument is EVMSERR. However, it's simplest
7471 just to pass it unconditionally. `vaxc$errno' is declared in
7472 <errno.h>, and maintained by the library in parallel with `errno'.
7473 We assume that caller's `errnum' either matches the last setting of
7474 `errno' by the library or else does not have the value `EVMSERR'. */
7476 result = strerror (errnum, vaxc$errno);
7477 #endif
7479 if (!result)
7480 result = "undocumented I/O error";
7482 return result;
7485 /* Error including a message from `errno'. */
7487 void
7488 cpp_error_from_errno (pfile, name)
7489 cpp_reader *pfile;
7490 char *name;
7492 int i;
7493 cpp_buffer *ip = cpp_file_buffer (pfile);
7495 cpp_print_containing_files (pfile);
7497 if (ip != NULL)
7498 cpp_file_line_for_message (pfile, ip->nominal_fname, ip->lineno, -1);
7500 cpp_message (pfile, 1, "%s: %s", name, my_strerror (errno));
7503 void
7504 cpp_perror_with_name (pfile, name)
7505 cpp_reader *pfile;
7506 char *name;
7508 cpp_message (pfile, 1, "%s: %s: %s", progname, name, my_strerror (errno));
7511 /* TODO:
7512 * No pre-compiled header file support.
7514 * Possibly different enum token codes for each C/C++ token.
7516 * Should clean up remaining directives to that do_XXX functions
7517 * only take two arguments and all have command_reads_line.
7519 * Find and cleanup remaining uses of static variables,
7521 * Support for trigraphs.
7523 * Support -dM flag (dump_all_macros).
7525 * Support for_lint flag.