]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/cppfiles.c
gjavah.c (HANDLE_METHOD): Call print_method_info with a NULL stream argument on the...
[thirdparty/gcc.git] / gcc / cppfiles.c
CommitLineData
add7091b 1/* Part of CPP library. (include file handling)
5e7b4e25
JL
2 Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1998,
3 1999, 2000 Free Software Foundation, Inc.
add7091b
ZW
4 Written by Per Bothner, 1994.
5 Based on CCCP program by Paul Rubin, June 1986
6 Adapted to ANSI C, Richard Stallman, Jan 1987
7 Split out of cpplib.c, Zack Weinberg, Oct 1998
8
9This program is free software; you can redistribute it and/or modify it
10under the terms of the GNU General Public License as published by the
11Free Software Foundation; either version 2, or (at your option) any
12later version.
13
14This program is distributed in the hope that it will be useful,
15but WITHOUT ANY WARRANTY; without even the implied warranty of
16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17GNU General Public License for more details.
18
19You should have received a copy of the GNU General Public License
20along with this program; if not, write to the Free Software
e38992e8 21Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
add7091b
ZW
22
23#include "config.h"
24#include "system.h"
add7091b 25#include "cpplib.h"
88ae23e7 26#include "cpphash.h"
c1212d2f 27#include "intl.h"
168d3732 28#include "mkdeps.h"
c31a6508 29#include "splay-tree.h"
add7091b 30
f8f769ea
ZW
31#ifdef HAVE_MMAP_FILE
32# include <sys/mman.h>
33# ifndef MMAP_THRESHOLD
34# define MMAP_THRESHOLD 3 /* Minimum page count to mmap the file. */
35# endif
36
37#else /* No MMAP_FILE */
38# undef MMAP_THRESHOLD
39# define MMAP_THRESHOLD 0
40#endif
41
d7a2e0f7
ZW
42#ifndef O_BINARY
43# define O_BINARY 0
44#endif
45
a73ac7a5 46static struct file_name_map *read_name_map
38b24ee2
ZW
47 PARAMS ((cpp_reader *, const char *));
48static char *read_filename_string PARAMS ((int, FILE *));
49static char *remap_filename PARAMS ((cpp_reader *, char *,
50 struct file_name_list *));
a73ac7a5 51static struct file_name_list *actual_directory
38b24ee2 52 PARAMS ((cpp_reader *, const char *));
c31a6508
ZW
53static struct include_file *find_include_file
54 PARAMS ((cpp_reader *, const char *,
55 struct file_name_list *));
56static struct include_file *open_include_file
57 PARAMS ((cpp_reader *, const char *));
58static int read_include_file PARAMS ((cpp_reader *, struct include_file *));
f8f769ea
ZW
59static ssize_t read_with_read PARAMS ((cpp_buffer *, int, ssize_t));
60static ssize_t read_file PARAMS ((cpp_buffer *, int, ssize_t));
2c826217 61
c31a6508 62static void destroy_include_file_node PARAMS ((splay_tree_value));
d4506961 63static int close_cached_fd PARAMS ((splay_tree_node, void *));
c31a6508 64
0b3d776a 65#if 0
f84c2018 66static void hack_vms_include_specification PARAMS ((char *));
0b3d776a 67#endif
add7091b 68
bb52fa7f
ZW
69#ifndef INCLUDE_LEN_FUDGE
70#define INCLUDE_LEN_FUDGE 0
71#endif
72
c31a6508
ZW
73/* We use a splay tree to store information about all the include
74 files seen in this compilation. The key of each tree node is the
75 physical path to the file. The value is 0 if the file does not
76 exist, or a struct include_file pointer. */
add7091b 77
c31a6508
ZW
78static void
79destroy_include_file_node (v)
80 splay_tree_value v;
d35364d1 81{
c31a6508
ZW
82 struct include_file *f = (struct include_file *)v;
83 if (f)
84 {
85 if (f->fd != -1)
86 close (f->fd);
87 free (f);
88 }
d35364d1 89}
add7091b 90
d4506961
ZW
91static int
92close_cached_fd (n, dummy)
93 splay_tree_node n;
94 void *dummy ATTRIBUTE_UNUSED;
95{
96 struct include_file *f = (struct include_file *)n->value;
97 if (f && f->fd != -1)
98 {
99 close (f->fd);
100 f->fd = -1;
101 }
102 return 0;
103}
104
d35364d1 105void
c31a6508 106_cpp_init_include_table (pfile)
d35364d1
ZW
107 cpp_reader *pfile;
108{
109 pfile->all_include_files
c31a6508
ZW
110 = splay_tree_new ((splay_tree_compare_fn) strcmp,
111 (splay_tree_delete_key_fn) free,
112 destroy_include_file_node);
0b3d776a 113}
add7091b 114
c31a6508
ZW
115/* Given a filename, look it up and possibly open it. If the file
116 does not exist, return NULL. If the file does exist but doesn't
117 need to be reread, return an include_file entry with fd == -1.
118 If it needs to be (re)read, return an include_file entry with
119 fd a file descriptor open on the file. */
add7091b 120
c31a6508
ZW
121static struct include_file *
122open_include_file (pfile, filename)
123 cpp_reader *pfile;
124 const char *filename;
125{
126 splay_tree_node nd;
127 struct include_file *file = 0;
128 int fd;
add7091b 129
c31a6508
ZW
130 nd = splay_tree_lookup (pfile->all_include_files,
131 (splay_tree_key) filename);
add7091b 132
c31a6508
ZW
133 if (nd)
134 {
135 if (nd->value == 0)
136 return 0;
add7091b 137
c31a6508 138 file = (struct include_file *)nd->value;
0b3d776a 139
c31a6508
ZW
140 if (DO_NOT_REREAD (file))
141 {
142 if (file->fd != -1)
143 {
144 close (file->fd);
145 file->fd = -1;
146 }
147 return file;
148 }
add7091b 149
c31a6508
ZW
150 /* File descriptors are cached for files that might be reread. */
151 if (file->fd != -1)
152 {
153 lseek (file->fd, 0, SEEK_SET);
154 return file;
155 }
156 }
add7091b 157
c31a6508
ZW
158 /* We used to open files in nonblocking mode, but that caused more
159 problems than it solved. Do take care not to acquire a
160 controlling terminal by mistake (this can't happen on sane
161 systems, but paranoia is a virtue).
add7091b 162
c31a6508
ZW
163 Use the three-argument form of open even though we aren't
164 specifying O_CREAT, to defend against broken system headers.
add7091b 165
c31a6508
ZW
166 O_BINARY tells some runtime libraries (notably DJGPP) not to do
167 newline translation; we can handle DOS line breaks just fine
168 ourselves.
b0699dad 169
c31a6508 170 Special case: the empty string is translated to stdin. */
d4506961
ZW
171 retry:
172
c31a6508
ZW
173 if (filename[0] == '\0')
174 fd = 0;
f2d5f0cc 175 else
c31a6508
ZW
176 fd = open (filename, O_RDONLY|O_NOCTTY|O_BINARY, 0666);
177
178 if (fd == -1)
f2d5f0cc 179 {
c31a6508
ZW
180#ifdef EACCES
181 if (errno == EACCES)
f2d5f0cc 182 {
041c3194 183 cpp_error (pfile, "included file \"%s\" exists but is not readable",
c31a6508 184 filename);
f2d5f0cc 185 }
c31a6508 186#endif
d4506961
ZW
187 if (0
188#ifdef EMFILE
189 || errno == EMFILE
190#endif
191#ifdef ENFILE
192 || errno == ENFILE
193#endif
194 )
195 {
196 /* Too many files open. Close all cached file descriptors and
197 try again. */
198 splay_tree_foreach (pfile->all_include_files, close_cached_fd, 0);
199 goto retry;
200 }
201
c31a6508
ZW
202 /* Nonexistent or inaccessible file. Create a negative node for it. */
203 if (nd)
f2d5f0cc 204 {
c31a6508
ZW
205 cpp_ice (pfile,
206 "node for '%s' exists, open failed, error '%s', value %lx\n",
207 filename, strerror (errno), nd->value);
208 destroy_include_file_node (nd->value);
f2d5f0cc 209 }
c31a6508
ZW
210 splay_tree_insert (pfile->all_include_files,
211 (splay_tree_key) xstrdup (filename), 0);
212 return 0;
f2d5f0cc 213 }
d7a2e0f7 214
c31a6508
ZW
215 /* If we haven't seen this file before, create a positive node for it. */
216 if (!nd)
217 {
218 file = xnew (struct include_file);
219 file->cmacro = 0;
d4506961 220 file->include_count = 0;
c31a6508
ZW
221 file->sysp = 0;
222 file->foundhere = 0;
223 file->name = xstrdup (filename);
224 splay_tree_insert (pfile->all_include_files,
225 (splay_tree_key) file->name,
226 (splay_tree_value) file);
227 }
e576beb0 228
c31a6508 229 file->fd = fd;
f3f751ad 230 file->date = (time_t) -1;
c31a6508 231 return file;
e576beb0
ZW
232}
233
c31a6508
ZW
234/* Return 1 if the file named by FNAME has been included before in
235 any context, 0 otherwise. */
236int
237cpp_included (pfile, fname)
add7091b 238 cpp_reader *pfile;
bcc5cac9 239 const char *fname;
add7091b 240{
d35364d1 241 struct file_name_list *path;
0b3d776a 242 char *name;
c31a6508 243 splay_tree_node nd;
0b3d776a 244
c31a6508 245 if (fname[0] == '/')
0b3d776a 246 {
c31a6508
ZW
247 /* Just look it up. */
248 nd = splay_tree_lookup (pfile->all_include_files, (splay_tree_key) fname);
249 return (nd && nd->value);
0b3d776a 250 }
c31a6508
ZW
251
252 /* Search directory path for the file. */
253 name = (char *) alloca (strlen (fname) + pfile->max_include_len
254 + 2 + INCLUDE_LEN_FUDGE);
255 for (path = CPP_OPTION (pfile, quote_include); path; path = path->next)
0b3d776a 256 {
c31a6508
ZW
257 memcpy (name, path->name, path->nlen);
258 name[path->nlen] = '/';
259 strcpy (&name[path->nlen+1], fname);
260 _cpp_simplify_pathname (name);
261 if (CPP_OPTION (pfile, remap))
262 name = remap_filename (pfile, name, path);
263
264 nd = splay_tree_lookup (pfile->all_include_files, (splay_tree_key) name);
265 if (nd && nd->value)
266 return 1;
0b3d776a 267 }
c31a6508 268 return 0;
add7091b
ZW
269}
270
c31a6508
ZW
271/* Search for include file FNAME in the include chain starting at
272 SEARCH_START. Return 0 if there is no such file (or it's un-openable),
273 otherwise an include_file structure, possibly with a file descriptor
274 open on the file. */
275
276static struct include_file *
277find_include_file (pfile, fname, search_start)
f2d5f0cc
ZW
278 cpp_reader *pfile;
279 const char *fname;
c31a6508 280 struct file_name_list *search_start;
f2d5f0cc 281{
c31a6508
ZW
282 struct file_name_list *path;
283 char *name;
284 struct include_file *file;
add7091b 285
c31a6508
ZW
286 if (fname[0] == '/')
287 return open_include_file (pfile, fname);
288
289 /* Search directory path for the file. */
290 name = (char *) alloca (strlen (fname) + pfile->max_include_len
291 + 2 + INCLUDE_LEN_FUDGE);
292 for (path = search_start; path; path = path->next)
add7091b 293 {
c31a6508
ZW
294 memcpy (name, path->name, path->nlen);
295 name[path->nlen] = '/';
296 strcpy (&name[path->nlen+1], fname);
297 _cpp_simplify_pathname (name);
298 if (CPP_OPTION (pfile, remap))
299 name = remap_filename (pfile, name, path);
300
301 file = open_include_file (pfile, name);
302 if (file)
add7091b 303 {
c31a6508
ZW
304 file->sysp = path->sysp;
305 file->foundhere = path;
306 return file;
add7091b
ZW
307 }
308 }
c31a6508 309 return 0;
add7091b
ZW
310}
311
c31a6508
ZW
312/* #line uses this to save artificial file names. We have to try
313 opening the file because an all_include_files entry is always
314 either + or -, there's no 'I don't know' value. */
315const char *
316_cpp_fake_include (pfile, fname)
add7091b 317 cpp_reader *pfile;
c31a6508 318 const char *fname;
add7091b 319{
c31a6508
ZW
320 splay_tree_node nd;
321 struct include_file *file;
add7091b 322 char *name;
add7091b 323
c31a6508
ZW
324 file = find_include_file (pfile, fname, CPP_OPTION (pfile, quote_include));
325 if (file)
326 return file->name;
add7091b 327
c31a6508
ZW
328 name = xstrdup (fname);
329 _cpp_simplify_pathname (name);
add7091b 330
c31a6508
ZW
331 /* We cannot just blindly insert a node, because there's still the
332 chance that the node already exists but isn't on the search path. */
333 nd = splay_tree_lookup (pfile->all_include_files, (splay_tree_key) name);
334 if (nd)
add7091b 335 {
c31a6508
ZW
336 free (name);
337 return (const char *) nd->key;
add7091b 338 }
0b3d776a 339
c31a6508
ZW
340 splay_tree_insert (pfile->all_include_files, (splay_tree_key) name, 0);
341 return (const char *)name;
add7091b
ZW
342}
343
e605b040
ZW
344/* Not everyone who wants to set system-header-ness on a buffer can
345 see the details of struct include_file. This is an exported interface
346 because fix-header needs it. */
347void
348cpp_make_system_header (pfile, pbuf, flag)
349 cpp_reader *pfile;
350 cpp_buffer *pbuf;
351 int flag;
352{
353 if (flag < 0 || flag > 2)
354 cpp_ice (pfile, "cpp_make_system_header: bad flag %d\n", flag);
355 else if (!pbuf->inc)
356 cpp_ice (pfile, "cpp_make_system_header called on non-file buffer");
357 else
358 pbuf->inc->sysp = flag;
359}
360
c31a6508 361#define PRINT_THIS_DEP(p, b) (CPP_PRINT_DEPS(p) > (b||p->system_include_depth))
168d3732 362void
041c3194 363_cpp_execute_include (pfile, f, len, no_reinclude, search_start, angle_brackets)
168d3732 364 cpp_reader *pfile;
041c3194 365 const U_CHAR *f;
168d3732
ZW
366 unsigned int len;
367 int no_reinclude;
368 struct file_name_list *search_start;
041c3194 369 int angle_brackets;
168d3732 370{
c31a6508 371 struct include_file *inc;
041c3194 372 char *fname;
168d3732
ZW
373
374 if (!search_start)
375 {
376 if (angle_brackets)
ae79697b
ZW
377 search_start = CPP_OPTION (pfile, bracket_include);
378 else if (CPP_OPTION (pfile, ignore_srcdir))
379 search_start = CPP_OPTION (pfile, quote_include);
168d3732
ZW
380 else
381 search_start = CPP_BUFFER (pfile)->actual_dir;
382 }
383
384 if (!search_start)
385 {
386 cpp_error (pfile, "No include path in which to find %s", fname);
387 return;
388 }
389
041c3194
ZW
390 fname = alloca (len + 1);
391 memcpy (fname, f, len);
168d3732
ZW
392 fname[len] = '\0';
393
c31a6508 394 inc = find_include_file (pfile, fname, search_start);
168d3732 395
c31a6508 396 if (inc)
168d3732 397 {
c31a6508
ZW
398 if (inc->fd == -1)
399 return;
400
401 /* For -M, add the file to the dependencies on its first inclusion. */
d4506961 402 if (!inc->include_count && PRINT_THIS_DEP (pfile, angle_brackets))
c31a6508 403 deps_add_dep (pfile->deps, inc->name);
d4506961 404 inc->include_count++;
c31a6508
ZW
405
406 /* Handle -H option. */
407 if (CPP_OPTION (pfile, print_include_names))
408 {
409 cpp_buffer *fp = CPP_BUFFER (pfile);
410 while ((fp = CPP_PREV_BUFFER (fp)) != NULL)
411 putc ('.', stderr);
412 fprintf (stderr, " %s\n", inc->name);
168d3732 413 }
168d3732 414
c31a6508
ZW
415 /* Actually process the file. */
416 if (no_reinclude)
417 inc->cmacro = NEVER_REREAD;
418
419 if (read_include_file (pfile, inc))
420 {
421 if (angle_brackets)
422 pfile->system_include_depth++;
423 }
168d3732
ZW
424 return;
425 }
c31a6508
ZW
426
427 if (CPP_OPTION (pfile, print_deps_missing_files)
428 && PRINT_THIS_DEP (pfile, angle_brackets))
168d3732 429 {
c31a6508
ZW
430 if (!angle_brackets)
431 deps_add_dep (pfile->deps, fname);
432 else
433 {
434 char *p;
435 struct file_name_list *ptr;
436 /* If requested as a system header, assume it belongs in
437 the first system header directory. */
438 if (CPP_OPTION (pfile, bracket_include))
439 ptr = CPP_OPTION (pfile, bracket_include);
440 else
441 ptr = CPP_OPTION (pfile, quote_include);
168d3732 442
c31a6508
ZW
443 p = (char *) alloca (strlen (ptr->name)
444 + strlen (fname) + 2);
445 if (*ptr->name != '\0')
446 {
447 strcpy (p, ptr->name);
448 strcat (p, "/");
449 }
450 strcat (p, fname);
451 _cpp_simplify_pathname (p);
452 deps_add_dep (pfile->deps, p);
453 }
168d3732 454 }
c31a6508
ZW
455 /* If -M was specified, and this header file won't be added to
456 the dependency list, then don't count this as an error,
457 because we can still produce correct output. Otherwise, we
458 can't produce correct output, because there may be
459 dependencies we need inside the missing file, and we don't
460 know what directory this missing file exists in. */
461 else if (CPP_PRINT_DEPS (pfile)
462 && ! PRINT_THIS_DEP (pfile, angle_brackets))
463 cpp_warning (pfile, "No include path in which to find %s", fname);
464 else
465 cpp_error_from_errno (pfile, fname);
168d3732
ZW
466}
467
f3f751ad
NS
468/* Locate file F, and determine whether it is newer than PFILE. Return -1,
469 if F cannot be located or dated, 1, if it is newer and 0 if older. */
470
471int
041c3194 472_cpp_compare_file_date (pfile, f, len, angle_brackets)
f3f751ad 473 cpp_reader *pfile;
041c3194 474 const U_CHAR *f;
f3f751ad 475 unsigned int len;
041c3194 476 int angle_brackets;
f3f751ad 477{
041c3194
ZW
478 char *fname;
479 struct file_name_list *search_start;
f3f751ad 480 struct include_file *inc;
041c3194 481 struct include_file *current_include = CPP_BUFFER (pfile)->inc;
f3f751ad 482
041c3194
ZW
483 if (angle_brackets)
484 search_start = CPP_OPTION (pfile, bracket_include);
485 else if (CPP_OPTION (pfile, ignore_srcdir))
486 search_start = CPP_OPTION (pfile, quote_include);
487 else
488 search_start = CPP_BUFFER (pfile)->actual_dir;
f3f751ad 489
041c3194
ZW
490 fname = alloca (len + 1);
491 memcpy (fname, f, len);
f3f751ad 492 fname[len] = '\0';
f3f751ad
NS
493 inc = find_include_file (pfile, fname, search_start);
494
495 if (!inc)
496 return -1;
497 if (inc->fd >= 0)
498 {
499 struct stat source;
500
501 if (fstat (inc->fd, &source) < 0)
502 {
503 close (inc->fd);
504 inc->fd = -1;
505 return -1;
506 }
507 inc->date = source.st_mtime;
508 close (inc->fd);
509 inc->fd = -1;
510 }
511 if (inc->date == (time_t)-1 || current_include->date == (time_t)-1)
512 return -1;
513 return inc->date > current_include->date;
514}
515
516
c45da1ca
ZW
517/* Push an input buffer and load it up with the contents of FNAME.
518 If FNAME is "" or NULL, read standard input. */
519int
520cpp_read_file (pfile, fname)
521 cpp_reader *pfile;
522 const char *fname;
523{
c31a6508 524 struct include_file *f;
c45da1ca 525
d35364d1
ZW
526 if (fname == NULL)
527 fname = "";
528
c31a6508 529 f = open_include_file (pfile, fname);
c45da1ca 530
041c3194
ZW
531 if (f == NULL)
532 {
533 cpp_error_from_errno (pfile, fname);
534 return 0;
535 }
536
c31a6508 537 return read_include_file (pfile, f);
c45da1ca
ZW
538}
539
c31a6508
ZW
540/* Read the file referenced by INC into a new buffer on PFILE's stack.
541 Return 1 if successful, 0 if not. */
add7091b 542
168d3732 543static int
c31a6508 544read_include_file (pfile, inc)
add7091b 545 cpp_reader *pfile;
c31a6508 546 struct include_file *inc;
add7091b
ZW
547{
548 struct stat st;
f8f769ea 549 ssize_t length;
0b3d776a 550 cpp_buffer *fp;
c31a6508 551 int fd = inc->fd;
add7091b 552
041c3194
ZW
553 /* Ensures we dump our current line before entering an include file. */
554 if (CPP_BUFFER (pfile) && pfile->printer)
555 cpp_output_tokens (pfile, pfile->printer,
556 CPP_BUF_LINE (CPP_BUFFER (pfile)));
557
d35364d1
ZW
558 fp = cpp_push_buffer (pfile, NULL, 0);
559
560 if (fp == 0)
561 goto push_fail;
562
041c3194 563 if (fd < 0 || fstat (fd, &st) < 0)
0b3d776a 564 goto perror_fail;
f3f751ad
NS
565
566 inc->date = st.st_mtime;
554fbeef 567
f8f769ea
ZW
568 /* If fd points to a plain file, we might be able to mmap it; we can
569 definitely allocate the buffer all at once. If fd is a pipe or
570 terminal, we can't do either. If fd is something weird, like a
571 block device or a directory, we don't want to read it at all.
6458033d
ZW
572
573 Unfortunately, different systems use different st.st_mode values
574 for pipes: some have S_ISFIFO, some S_ISSOCK, some are buggy and
10e56506
ZW
575 zero the entire struct stat except a couple fields. Hence we don't
576 even try to figure out what something is, except for plain files,
f8f769ea 577 directories, and block devices. */
6458033d 578
0b3d776a
ZW
579 if (S_ISREG (st.st_mode))
580 {
f8f769ea
ZW
581 ssize_t st_size;
582
583 /* off_t might have a wider range than ssize_t - in other words,
554fbeef 584 the max size of a file might be bigger than the address
6458033d 585 space. We can't handle a file that large. (Anyone with
f8f769ea 586 a single source file bigger than 2GB needs to rethink
e0fcc0e1
KG
587 their coding style.) Some systems (e.g. AIX 4.1) define
588 SSIZE_MAX to be much smaller than the actual range of the
589 type. Use INTTYPE_MAXIMUM unconditionally to ensure this
590 does not bite us. */
591 if (st.st_size > INTTYPE_MAXIMUM (ssize_t))
554fbeef 592 {
c31a6508 593 cpp_error (pfile, "%s is too large", inc->name);
554fbeef
ZW
594 goto fail;
595 }
f8f769ea
ZW
596 st_size = st.st_size;
597 length = read_file (fp, fd, st_size);
598 if (length == -1)
599 goto perror_fail;
600 if (length < st_size)
c31a6508 601 cpp_warning (pfile, "%s is shorter than expected\n", inc->name);
add7091b 602 }
10e56506 603 else if (S_ISBLK (st.st_mode))
0b3d776a 604 {
c31a6508 605 cpp_error (pfile, "%s is a block device", inc->name);
10e56506 606 goto fail;
0b3d776a 607 }
10e56506 608 else if (S_ISDIR (st.st_mode))
0b3d776a 609 {
c31a6508 610 cpp_error (pfile, "%s is a directory", inc->name);
554fbeef 611 goto fail;
add7091b 612 }
10e56506
ZW
613 else
614 {
f8f769ea
ZW
615 /* 8 kilobytes is a sensible starting size. It ought to be
616 bigger than the kernel pipe buffer, and it's definitely
617 bigger than the majority of C source files. */
618 length = read_with_read (fp, fd, 8 * 1024);
619 if (length == -1)
620 goto perror_fail;
10e56506 621 }
add7091b 622
f8f769ea 623 /* These must be set before prescan. */
c31a6508
ZW
624 fp->inc = inc;
625 fp->nominal_fname = inc->name;
fb753f88 626 pfile->include_depth++;
f8f769ea 627
554fbeef 628 if (length == 0)
c31a6508 629 inc->cmacro = NEVER_REREAD;
add7091b 630
ff2b53ef 631 fp->rlimit = fp->buf + length;
554fbeef 632 fp->cur = fp->buf;
554fbeef 633 fp->lineno = 1;
099a9dd0 634 fp->line_base = fp->buf;
554fbeef
ZW
635
636 /* The ->actual_dir field is only used when ignore_srcdir is not in effect;
637 see do_include */
ae79697b 638 if (!CPP_OPTION (pfile, ignore_srcdir))
c31a6508 639 fp->actual_dir = actual_directory (pfile, inc->name);
554fbeef 640
add7091b 641 pfile->input_stack_listing_current = 0;
add7091b
ZW
642 return 1;
643
0b3d776a 644 perror_fail:
c31a6508
ZW
645 cpp_error_from_errno (pfile, inc->name);
646 /* Do not try to read this file again. */
041c3194
ZW
647 if (fd != -1)
648 close (fd);
c31a6508
ZW
649 inc->fd = -1;
650 inc->cmacro = NEVER_REREAD;
0b3d776a 651 fail:
554fbeef 652 cpp_pop_buffer (pfile);
d35364d1 653 push_fail:
0b3d776a 654 return 0;
add7091b
ZW
655}
656
f8f769ea
ZW
657static ssize_t
658read_file (fp, fd, size)
659 cpp_buffer *fp;
660 int fd;
661 ssize_t size;
662{
663 static int pagesize = -1;
664
665 if (size == 0)
666 return 0;
667
668 if (pagesize == -1)
669 pagesize = getpagesize ();
670
671#if MMAP_THRESHOLD
672 if (size / pagesize >= MMAP_THRESHOLD)
673 {
674 const U_CHAR *result
675 = (const U_CHAR *) mmap (0, size, PROT_READ, MAP_PRIVATE, fd, 0);
676 if (result != (const U_CHAR *)-1)
677 {
678 fp->buf = result;
679 fp->mapped = 1;
680 return size;
681 }
682 }
683 /* If mmap fails, try read. If there's really a problem, read will
684 fail too. */
685#endif
686
687 return read_with_read (fp, fd, size);
688}
689
690static ssize_t
691read_with_read (fp, fd, size)
692 cpp_buffer *fp;
693 int fd;
694 ssize_t size;
695{
696 ssize_t offset, count;
697 U_CHAR *buf;
698
699 buf = (U_CHAR *) xmalloc (size);
700 offset = 0;
701 while ((count = read (fd, buf + offset, size - offset)) > 0)
702 {
703 offset += count;
704 if (offset == size)
705 buf = xrealloc (buf, (size *= 2));
706 }
707 if (count < 0)
708 {
709 free (buf);
710 return -1;
711 }
712 if (offset == 0)
713 {
714 free (buf);
715 return 0;
716 }
717
718 if (offset < size)
719 buf = xrealloc (buf, offset);
720 fp->buf = buf;
721 fp->mapped = 0;
722 return offset;
723}
724
c31a6508
ZW
725/* The file_name_map structure holds a mapping of file names for a
726 particular directory. This mapping is read from the file named
727 FILE_NAME_MAP_FILE in that directory. Such a file can be used to
728 map filenames on a file system with severe filename restrictions,
729 such as DOS. The format of the file name map file is just a series
730 of lines with two tokens on each line. The first token is the name
731 to map, and the second token is the actual name to use. */
732
733struct file_name_map
734{
735 struct file_name_map *map_next;
736 char *map_from;
737 char *map_to;
738};
739
740#define FILE_NAME_MAP_FILE "header.gcc"
741
742/* Read a space delimited string of unlimited length from a stdio
743 file. */
744
745static char *
746read_filename_string (ch, f)
747 int ch;
748 FILE *f;
749{
750 char *alloc, *set;
751 int len;
752
753 len = 20;
754 set = alloc = xmalloc (len + 1);
755 if (! is_space(ch))
756 {
757 *set++ = ch;
758 while ((ch = getc (f)) != EOF && ! is_space(ch))
759 {
760 if (set - alloc == len)
761 {
762 len *= 2;
763 alloc = xrealloc (alloc, len + 1);
764 set = alloc + len / 2;
765 }
766 *set++ = ch;
767 }
768 }
769 *set = '\0';
770 ungetc (ch, f);
771 return alloc;
772}
773
774/* This structure holds a linked list of file name maps, one per directory. */
775
776struct file_name_map_list
777{
778 struct file_name_map_list *map_list_next;
779 char *map_list_name;
780 struct file_name_map *map_list_map;
781};
782
783/* Read the file name map file for DIRNAME. */
784
785static struct file_name_map *
786read_name_map (pfile, dirname)
787 cpp_reader *pfile;
788 const char *dirname;
789{
790 register struct file_name_map_list *map_list_ptr;
791 char *name;
792 FILE *f;
793
794 for (map_list_ptr = CPP_OPTION (pfile, map_list); map_list_ptr;
795 map_list_ptr = map_list_ptr->map_list_next)
796 if (! strcmp (map_list_ptr->map_list_name, dirname))
797 return map_list_ptr->map_list_map;
798
799 map_list_ptr = ((struct file_name_map_list *)
800 xmalloc (sizeof (struct file_name_map_list)));
801 map_list_ptr->map_list_name = xstrdup (dirname);
802
803 name = (char *) alloca (strlen (dirname) + strlen (FILE_NAME_MAP_FILE) + 2);
804 strcpy (name, dirname);
805 if (*dirname)
806 strcat (name, "/");
807 strcat (name, FILE_NAME_MAP_FILE);
808 f = fopen (name, "r");
809 if (!f)
810 map_list_ptr->map_list_map = (struct file_name_map *)-1;
811 else
812 {
813 int ch;
814 int dirlen = strlen (dirname);
815
816 while ((ch = getc (f)) != EOF)
817 {
818 char *from, *to;
819 struct file_name_map *ptr;
820
821 if (is_space(ch))
822 continue;
823 from = read_filename_string (ch, f);
824 while ((ch = getc (f)) != EOF && is_hspace(ch))
825 ;
826 to = read_filename_string (ch, f);
827
828 ptr = ((struct file_name_map *)
829 xmalloc (sizeof (struct file_name_map)));
830 ptr->map_from = from;
831
832 /* Make the real filename absolute. */
833 if (*to == '/')
834 ptr->map_to = to;
835 else
836 {
837 ptr->map_to = xmalloc (dirlen + strlen (to) + 2);
838 strcpy (ptr->map_to, dirname);
839 ptr->map_to[dirlen] = '/';
840 strcpy (ptr->map_to + dirlen + 1, to);
841 free (to);
842 }
843
844 ptr->map_next = map_list_ptr->map_list_map;
845 map_list_ptr->map_list_map = ptr;
846
847 while ((ch = getc (f)) != '\n')
848 if (ch == EOF)
849 break;
850 }
851 fclose (f);
852 }
853
854 map_list_ptr->map_list_next = CPP_OPTION (pfile, map_list);
855 CPP_OPTION (pfile, map_list) = map_list_ptr;
856
857 return map_list_ptr->map_list_map;
858}
859
860/* Remap NAME based on the file_name_map (if any) for LOC. */
861
862static char *
863remap_filename (pfile, name, loc)
864 cpp_reader *pfile;
865 char *name;
866 struct file_name_list *loc;
867{
868 struct file_name_map *map;
869 const char *from, *p, *dir;
870
871 if (! loc->name_map)
872 loc->name_map = read_name_map (pfile,
873 loc->name
874 ? loc->name : ".");
875
876 if (loc->name_map == (struct file_name_map *)-1)
877 return name;
878
879 from = name + strlen (loc->name) + 1;
880
881 for (map = loc->name_map; map; map = map->map_next)
882 if (!strcmp (map->map_from, from))
883 return map->map_to;
884
885 /* Try to find a mapping file for the particular directory we are
886 looking in. Thus #include <sys/types.h> will look up sys/types.h
887 in /usr/include/header.gcc and look up types.h in
888 /usr/include/sys/header.gcc. */
889 p = strrchr (name, '/');
890 if (!p)
891 p = name;
892 if (loc && loc->name
893 && strlen (loc->name) == (size_t) (p - name)
894 && !strncmp (loc->name, name, p - name))
895 /* FILENAME is in SEARCHPTR, which we've already checked. */
896 return name;
897
898 if (p == name)
899 {
900 dir = ".";
901 from = name;
902 }
903 else
904 {
905 char * newdir = (char *) alloca (p - name + 1);
906 memcpy (newdir, name, p - name);
907 newdir[p - name] = '\0';
908 dir = newdir;
909 from = p + 1;
910 }
911
912 for (map = read_name_map (pfile, dir); map; map = map->map_next)
913 if (! strcmp (map->map_from, name))
914 return map->map_to;
915
916 return name;
917}
918
6458033d
ZW
919/* Given a path FNAME, extract the directory component and place it
920 onto the actual_dirs list. Return a pointer to the allocated
921 file_name_list structure. These structures are used to implement
922 current-directory "" include searching. */
923
f1a86df6
ZW
924static struct file_name_list *
925actual_directory (pfile, fname)
926 cpp_reader *pfile;
bcc5cac9 927 const char *fname;
f1a86df6
ZW
928{
929 char *last_slash, *dir;
930 size_t dlen;
931 struct file_name_list *x;
932
c49445e0 933 dir = xstrdup (fname);
7ceb3598 934 last_slash = strrchr (dir, '/');
f1a86df6
ZW
935 if (last_slash)
936 {
937 if (last_slash == dir)
938 {
939 dlen = 1;
940 last_slash[1] = '\0';
941 }
942 else
943 {
944 dlen = last_slash - dir;
945 *last_slash = '\0';
946 }
947 }
948 else
949 {
950 dir[0] = '.';
951 dir[1] = '\0';
952 dlen = 1;
953 }
954
955 if (dlen > pfile->max_include_len)
956 pfile->max_include_len = dlen;
957
958 for (x = pfile->actual_dirs; x; x = x->alloc)
959 if (!strcmp (x->name, dir))
960 {
961 free (dir);
962 return x;
963 }
964
965 /* Not found, make a new one. */
966 x = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
967 x->name = dir;
968 x->nlen = dlen;
ae79697b 969 x->next = CPP_OPTION (pfile, quote_include);
f1a86df6 970 x->alloc = pfile->actual_dirs;
c31a6508 971 x->sysp = CPP_BUFFER (pfile)->inc->sysp;
f1a86df6
ZW
972 x->name_map = NULL;
973
974 pfile->actual_dirs = x;
975 return x;
976}
977
0b3d776a
ZW
978/* Simplify a path name in place, deleting redundant components. This
979 reduces OS overhead and guarantees that equivalent paths compare
980 the same (modulo symlinks).
981
982 Transforms made:
983 foo/bar/../quux foo/quux
984 foo/./bar foo/bar
985 foo//bar foo/bar
986 /../quux /quux
987 //quux //quux (POSIX allows leading // as a namespace escape)
988
989 Guarantees no trailing slashes. All transforms reduce the length
990 of the string.
991 */
0b22d65c 992void
b0699dad 993_cpp_simplify_pathname (path)
21380ab0 994 char *path;
0b3d776a
ZW
995{
996 char *from, *to;
997 char *base;
998 int absolute = 0;
999
509781a4 1000#if defined (HAVE_DOS_BASED_FILE_SYSTEM)
0b3d776a
ZW
1001 /* Convert all backslashes to slashes. */
1002 for (from = path; *from; from++)
1003 if (*from == '\\') *from = '/';
1004
1005 /* Skip over leading drive letter if present. */
1006 if (ISALPHA (path[0]) && path[1] == ':')
1007 from = to = &path[2];
1008 else
1009 from = to = path;
1010#else
1011 from = to = path;
1012#endif
1013
1014 /* Remove redundant initial /s. */
1015 if (*from == '/')
1016 {
1017 absolute = 1;
1018 to++;
1019 from++;
1020 if (*from == '/')
1021 {
1022 if (*++from == '/')
1023 /* 3 or more initial /s are equivalent to 1 /. */
1024 while (*++from == '/');
1025 else
1026 /* On some hosts // differs from /; Posix allows this. */
1027 to++;
1028 }
1029 }
1030 base = to;
1031
1032 for (;;)
1033 {
1034 while (*from == '/')
1035 from++;
1036
1037 if (from[0] == '.' && from[1] == '/')
1038 from += 2;
1039 else if (from[0] == '.' && from[1] == '\0')
1040 goto done;
1041 else if (from[0] == '.' && from[1] == '.' && from[2] == '/')
1042 {
1043 if (base == to)
1044 {
1045 if (absolute)
1046 from += 3;
1047 else
1048 {
1049 *to++ = *from++;
1050 *to++ = *from++;
1051 *to++ = *from++;
1052 base = to;
1053 }
1054 }
1055 else
1056 {
1057 to -= 2;
1058 while (to > base && *to != '/') to--;
1059 if (*to == '/')
1060 to++;
1061 from += 3;
1062 }
1063 }
1064 else if (from[0] == '.' && from[1] == '.' && from[2] == '\0')
1065 {
1066 if (base == to)
1067 {
1068 if (!absolute)
1069 {
1070 *to++ = *from++;
1071 *to++ = *from++;
1072 }
1073 }
1074 else
1075 {
1076 to -= 2;
1077 while (to > base && *to != '/') to--;
1078 if (*to == '/')
1079 to++;
1080 }
1081 goto done;
1082 }
1083 else
1084 /* Copy this component and trailing /, if any. */
1085 while ((*to++ = *from++) != '/')
1086 {
1087 if (!to[-1])
1088 {
1089 to--;
1090 goto done;
1091 }
1092 }
1093
1094 }
1095
1096 done:
1097 /* Trim trailing slash */
1098 if (to[0] == '/' && (!absolute || to > path+1))
1099 to--;
1100
1101 /* Change the empty string to "." so that stat() on the result
1102 will always work. */
1103 if (to == path)
1104 *to++ = '.';
1105
1106 *to = '\0';
1107
1108 return;
1109}
1110
1111/* It is not clear when this should be used if at all, so I've
1112 disabled it until someone who understands VMS can look at it. */
1113#if 0
add7091b
ZW
1114
1115/* Under VMS we need to fix up the "include" specification filename.
1116
1117 Rules for possible conversions
1118
1119 fullname tried paths
1120
1121 name name
1122 ./dir/name [.dir]name
1123 /dir/name dir:name
1124 /name [000000]name, name
1125 dir/name dir:[000000]name, dir:name, dir/name
1126 dir1/dir2/name dir1:[dir2]name, dir1:[000000.dir2]name
1127 path:/name path:[000000]name, path:name
1128 path:/dir/name path:[000000.dir]name, path:[dir]name
1129 path:dir/name path:[dir]name
1130 [path]:[dir]name [path.dir]name
1131 path/[dir]name [path.dir]name
1132
1133 The path:/name input is constructed when expanding <> includes. */
1134
1135
1136static void
1137hack_vms_include_specification (fullname)
1138 char *fullname;
1139{
1140 register char *basename, *unixname, *local_ptr, *first_slash;
1141 int f, check_filename_before_returning, must_revert;
1142 char Local[512];
1143
1144 check_filename_before_returning = 0;
1145 must_revert = 0;
1146 /* See if we can find a 1st slash. If not, there's no path information. */
7ceb3598 1147 first_slash = strchr (fullname, '/');
add7091b
ZW
1148 if (first_slash == 0)
1149 return 0; /* Nothing to do!!! */
1150
1151 /* construct device spec if none given. */
1152
7ceb3598 1153 if (strchr (fullname, ':') == 0)
add7091b
ZW
1154 {
1155
1156 /* If fullname has a slash, take it as device spec. */
1157
1158 if (first_slash == fullname)
1159 {
7ceb3598 1160 first_slash = strchr (fullname + 1, '/'); /* 2nd slash ? */
add7091b
ZW
1161 if (first_slash)
1162 *first_slash = ':'; /* make device spec */
1163 for (basename = fullname; *basename != 0; basename++)
1164 *basename = *(basename+1); /* remove leading slash */
1165 }
1166 else if ((first_slash[-1] != '.') /* keep ':/', './' */
1167 && (first_slash[-1] != ':')
1168 && (first_slash[-1] != ']')) /* or a vms path */
1169 {
1170 *first_slash = ':';
1171 }
1172 else if ((first_slash[1] == '[') /* skip './' in './[dir' */
1173 && (first_slash[-1] == '.'))
1174 fullname += 2;
1175 }
1176
1177 /* Get part after first ':' (basename[-1] == ':')
1178 or last '/' (basename[-1] == '/'). */
1179
1180 basename = base_name (fullname);
1181
1182 local_ptr = Local; /* initialize */
1183
1184 /* We are trying to do a number of things here. First of all, we are
1185 trying to hammer the filenames into a standard format, such that later
1186 processing can handle them.
1187
1188 If the file name contains something like [dir.], then it recognizes this
1189 as a root, and strips the ".]". Later processing will add whatever is
1190 needed to get things working properly.
1191
1192 If no device is specified, then the first directory name is taken to be
1193 a device name (or a rooted logical). */
1194
1195 /* Point to the UNIX filename part (which needs to be fixed!)
1196 but skip vms path information.
1197 [basename != fullname since first_slash != 0]. */
1198
1199 if ((basename[-1] == ':') /* vms path spec. */
1200 || (basename[-1] == ']')
1201 || (basename[-1] == '>'))
1202 unixname = basename;
1203 else
1204 unixname = fullname;
1205
1206 if (*unixname == '/')
1207 unixname++;
1208
1209 /* If the directory spec is not rooted, we can just copy
1210 the UNIX filename part and we are done. */
1211
1212 if (((basename - fullname) > 1)
1213 && ( (basename[-1] == ']')
1214 || (basename[-1] == '>')))
1215 {
1216 if (basename[-2] != '.')
1217 {
1218
1219 /* The VMS part ends in a `]', and the preceding character is not a `.'.
1220 -> PATH]:/name (basename = '/name', unixname = 'name')
1221 We strip the `]', and then splice the two parts of the name in the
86702e31 1222 usual way. Given the default locations for include files,
add7091b
ZW
1223 we will only use this code if the user specifies alternate locations
1224 with the /include (-I) switch on the command line. */
1225
1226 basename -= 1; /* Strip "]" */
1227 unixname--; /* backspace */
1228 }
1229 else
1230 {
1231
1232 /* The VMS part has a ".]" at the end, and this will not do. Later
1233 processing will add a second directory spec, and this would be a syntax
1234 error. Thus we strip the ".]", and thus merge the directory specs.
1235 We also backspace unixname, so that it points to a '/'. This inhibits the
1236 generation of the 000000 root directory spec (which does not belong here
1237 in this case). */
1238
1239 basename -= 2; /* Strip ".]" */
1240 unixname--; /* backspace */
1241 }
1242 }
1243
1244 else
1245
1246 {
1247
1248 /* We drop in here if there is no VMS style directory specification yet.
1249 If there is no device specification either, we make the first dir a
1250 device and try that. If we do not do this, then we will be essentially
1251 searching the users default directory (as if they did a #include "asdf.h").
1252
1253 Then all we need to do is to push a '[' into the output string. Later
1254 processing will fill this in, and close the bracket. */
1255
1256 if ((unixname != fullname) /* vms path spec found. */
1257 && (basename[-1] != ':'))
1258 *local_ptr++ = ':'; /* dev not in spec. take first dir */
1259
1260 *local_ptr++ = '['; /* Open the directory specification */
1261 }
1262
1263 if (unixname == fullname) /* no vms dir spec. */
1264 {
1265 must_revert = 1;
1266 if ((first_slash != 0) /* unix dir spec. */
1267 && (*unixname != '/') /* not beginning with '/' */
1268 && (*unixname != '.')) /* or './' or '../' */
1269 *local_ptr++ = '.'; /* dir is local ! */
1270 }
1271
1272 /* at this point we assume that we have the device spec, and (at least
1273 the opening "[" for a directory specification. We may have directories
1274 specified already.
1275
1276 If there are no other slashes then the filename will be
1277 in the "root" directory. Otherwise, we need to add
1278 directory specifications. */
1279
7ceb3598 1280 if (strchr (unixname, '/') == 0)
add7091b
ZW
1281 {
1282 /* if no directories specified yet and none are following. */
1283 if (local_ptr[-1] == '[')
1284 {
1285 /* Just add "000000]" as the directory string */
1286 strcpy (local_ptr, "000000]");
1287 local_ptr += strlen (local_ptr);
1288 check_filename_before_returning = 1; /* we might need to fool with this later */
1289 }
1290 }
1291 else
1292 {
1293
1294 /* As long as there are still subdirectories to add, do them. */
7ceb3598 1295 while (strchr (unixname, '/') != 0)
add7091b
ZW
1296 {
1297 /* If this token is "." we can ignore it
1298 if it's not at the beginning of a path. */
1299 if ((unixname[0] == '.') && (unixname[1] == '/'))
1300 {
1301 /* remove it at beginning of path. */
1302 if ( ((unixname == fullname) /* no device spec */
1303 && (fullname+2 != basename)) /* starts with ./ */
1304 /* or */
1305 || ((basename[-1] == ':') /* device spec */
1306 && (unixname-1 == basename))) /* and ./ afterwards */
1307 *local_ptr++ = '.'; /* make '[.' start of path. */
1308 unixname += 2;
1309 continue;
1310 }
1311
1312 /* Add a subdirectory spec. Do not duplicate "." */
1313 if ( local_ptr[-1] != '.'
1314 && local_ptr[-1] != '['
1315 && local_ptr[-1] != '<')
1316 *local_ptr++ = '.';
1317
1318 /* If this is ".." then the spec becomes "-" */
1319 if ( (unixname[0] == '.')
1320 && (unixname[1] == '.')
1321 && (unixname[2] == '/'))
1322 {
1323 /* Add "-" and skip the ".." */
1324 if ((local_ptr[-1] == '.')
1325 && (local_ptr[-2] == '['))
1326 local_ptr--; /* prevent [.- */
1327 *local_ptr++ = '-';
1328 unixname += 3;
1329 continue;
1330 }
1331
1332 /* Copy the subdirectory */
1333 while (*unixname != '/')
1334 *local_ptr++= *unixname++;
1335
1336 unixname++; /* Skip the "/" */
1337 }
1338
1339 /* Close the directory specification */
1340 if (local_ptr[-1] == '.') /* no trailing periods */
1341 local_ptr--;
1342
1343 if (local_ptr[-1] == '[') /* no dir needed */
1344 local_ptr--;
1345 else
1346 *local_ptr++ = ']';
1347 }
1348
1349 /* Now add the filename. */
1350
1351 while (*unixname)
1352 *local_ptr++ = *unixname++;
1353 *local_ptr = 0;
1354
1355 /* Now append it to the original VMS spec. */
1356
1357 strcpy ((must_revert==1)?fullname:basename, Local);
1358
1359 /* If we put a [000000] in the filename, try to open it first. If this fails,
1360 remove the [000000], and return that name. This provides flexibility
1361 to the user in that they can use both rooted and non-rooted logical names
1362 to point to the location of the file. */
1363
1364 if (check_filename_before_returning)
1365 {
e576beb0 1366 f = open (fullname, O_RDONLY|O_NONBLOCK);
add7091b
ZW
1367 if (f >= 0)
1368 {
1369 /* The file name is OK as it is, so return it as is. */
1370 close (f);
1371 return 1;
1372 }
1373
1374 /* The filename did not work. Try to remove the [000000] from the name,
1375 and return it. */
1376
7ceb3598
NB
1377 basename = strchr (fullname, '[');
1378 local_ptr = strchr (fullname, ']') + 1;
add7091b
ZW
1379 strcpy (basename, local_ptr); /* this gets rid of it */
1380
1381 }
1382
1383 return 1;
1384}
1385#endif /* VMS */