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