]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/cppfiles.c
expr8.C: New test.
[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 {
c31a6508
ZW
183 cpp_error (pfile, "included file `%s' exists but is not readable",
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
ZW
229 file->fd = fd;
230 return file;
e576beb0
ZW
231}
232
c31a6508
ZW
233/* Return 1 if the file named by FNAME has been included before in
234 any context, 0 otherwise. */
235int
236cpp_included (pfile, fname)
add7091b 237 cpp_reader *pfile;
bcc5cac9 238 const char *fname;
add7091b 239{
d35364d1 240 struct file_name_list *path;
0b3d776a 241 char *name;
c31a6508 242 splay_tree_node nd;
0b3d776a 243
c31a6508 244 if (fname[0] == '/')
0b3d776a 245 {
c31a6508
ZW
246 /* Just look it up. */
247 nd = splay_tree_lookup (pfile->all_include_files, (splay_tree_key) fname);
248 return (nd && nd->value);
0b3d776a 249 }
c31a6508
ZW
250
251 /* Search directory path for the file. */
252 name = (char *) alloca (strlen (fname) + pfile->max_include_len
253 + 2 + INCLUDE_LEN_FUDGE);
254 for (path = CPP_OPTION (pfile, quote_include); path; path = path->next)
0b3d776a 255 {
c31a6508
ZW
256 memcpy (name, path->name, path->nlen);
257 name[path->nlen] = '/';
258 strcpy (&name[path->nlen+1], fname);
259 _cpp_simplify_pathname (name);
260 if (CPP_OPTION (pfile, remap))
261 name = remap_filename (pfile, name, path);
262
263 nd = splay_tree_lookup (pfile->all_include_files, (splay_tree_key) name);
264 if (nd && nd->value)
265 return 1;
0b3d776a 266 }
c31a6508 267 return 0;
add7091b
ZW
268}
269
c31a6508
ZW
270/* Search for include file FNAME in the include chain starting at
271 SEARCH_START. Return 0 if there is no such file (or it's un-openable),
272 otherwise an include_file structure, possibly with a file descriptor
273 open on the file. */
274
275static struct include_file *
276find_include_file (pfile, fname, search_start)
f2d5f0cc
ZW
277 cpp_reader *pfile;
278 const char *fname;
c31a6508 279 struct file_name_list *search_start;
f2d5f0cc 280{
c31a6508
ZW
281 struct file_name_list *path;
282 char *name;
283 struct include_file *file;
add7091b 284
c31a6508
ZW
285 if (fname[0] == '/')
286 return open_include_file (pfile, fname);
287
288 /* Search directory path for the file. */
289 name = (char *) alloca (strlen (fname) + pfile->max_include_len
290 + 2 + INCLUDE_LEN_FUDGE);
291 for (path = search_start; path; path = path->next)
add7091b 292 {
c31a6508
ZW
293 memcpy (name, path->name, path->nlen);
294 name[path->nlen] = '/';
295 strcpy (&name[path->nlen+1], fname);
296 _cpp_simplify_pathname (name);
297 if (CPP_OPTION (pfile, remap))
298 name = remap_filename (pfile, name, path);
299
300 file = open_include_file (pfile, name);
301 if (file)
add7091b 302 {
c31a6508
ZW
303 file->sysp = path->sysp;
304 file->foundhere = path;
305 return file;
add7091b
ZW
306 }
307 }
c31a6508 308 return 0;
add7091b
ZW
309}
310
c31a6508
ZW
311/* #line uses this to save artificial file names. We have to try
312 opening the file because an all_include_files entry is always
313 either + or -, there's no 'I don't know' value. */
314const char *
315_cpp_fake_include (pfile, fname)
add7091b 316 cpp_reader *pfile;
c31a6508 317 const char *fname;
add7091b 318{
c31a6508
ZW
319 splay_tree_node nd;
320 struct include_file *file;
add7091b 321 char *name;
add7091b 322
c31a6508
ZW
323 file = find_include_file (pfile, fname, CPP_OPTION (pfile, quote_include));
324 if (file)
325 return file->name;
add7091b 326
c31a6508
ZW
327 name = xstrdup (fname);
328 _cpp_simplify_pathname (name);
add7091b 329
c31a6508
ZW
330 /* We cannot just blindly insert a node, because there's still the
331 chance that the node already exists but isn't on the search path. */
332 nd = splay_tree_lookup (pfile->all_include_files, (splay_tree_key) name);
333 if (nd)
add7091b 334 {
c31a6508
ZW
335 free (name);
336 return (const char *) nd->key;
add7091b 337 }
0b3d776a 338
c31a6508
ZW
339 splay_tree_insert (pfile->all_include_files, (splay_tree_key) name, 0);
340 return (const char *)name;
add7091b
ZW
341}
342
e605b040
ZW
343/* Not everyone who wants to set system-header-ness on a buffer can
344 see the details of struct include_file. This is an exported interface
345 because fix-header needs it. */
346void
347cpp_make_system_header (pfile, pbuf, flag)
348 cpp_reader *pfile;
349 cpp_buffer *pbuf;
350 int flag;
351{
352 if (flag < 0 || flag > 2)
353 cpp_ice (pfile, "cpp_make_system_header: bad flag %d\n", flag);
354 else if (!pbuf->inc)
355 cpp_ice (pfile, "cpp_make_system_header called on non-file buffer");
356 else
357 pbuf->inc->sysp = flag;
358}
359
c31a6508 360#define PRINT_THIS_DEP(p, b) (CPP_PRINT_DEPS(p) > (b||p->system_include_depth))
168d3732 361void
12cf91fe 362_cpp_execute_include (pfile, f, len, no_reinclude, search_start)
168d3732 363 cpp_reader *pfile;
12cf91fe 364 U_CHAR *f;
168d3732
ZW
365 unsigned int len;
366 int no_reinclude;
367 struct file_name_list *search_start;
368{
c31a6508 369 struct include_file *inc;
12cf91fe 370 char *fname = (char *)f;
168d3732 371 int angle_brackets = fname[0] == '<';
168d3732
ZW
372
373 if (!search_start)
374 {
375 if (angle_brackets)
ae79697b
ZW
376 search_start = CPP_OPTION (pfile, bracket_include);
377 else if (CPP_OPTION (pfile, ignore_srcdir))
378 search_start = CPP_OPTION (pfile, quote_include);
168d3732
ZW
379 else
380 search_start = CPP_BUFFER (pfile)->actual_dir;
381 }
382
383 if (!search_start)
384 {
385 cpp_error (pfile, "No include path in which to find %s", fname);
386 return;
387 }
388
389 /* Remove quote marks. */
390 fname++;
391 len -= 2;
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
c45da1ca
ZW
468/* Push an input buffer and load it up with the contents of FNAME.
469 If FNAME is "" or NULL, read standard input. */
470int
471cpp_read_file (pfile, fname)
472 cpp_reader *pfile;
473 const char *fname;
474{
c31a6508 475 struct include_file *f;
c45da1ca 476
d35364d1
ZW
477 if (fname == NULL)
478 fname = "";
479
c31a6508 480 f = open_include_file (pfile, fname);
c45da1ca 481
c31a6508 482 return read_include_file (pfile, f);
c45da1ca
ZW
483}
484
c31a6508
ZW
485/* Read the file referenced by INC into a new buffer on PFILE's stack.
486 Return 1 if successful, 0 if not. */
add7091b 487
168d3732 488static int
c31a6508 489read_include_file (pfile, inc)
add7091b 490 cpp_reader *pfile;
c31a6508 491 struct include_file *inc;
add7091b
ZW
492{
493 struct stat st;
f8f769ea 494 ssize_t length;
0b3d776a 495 cpp_buffer *fp;
c31a6508 496 int fd = inc->fd;
add7091b 497
d35364d1
ZW
498 fp = cpp_push_buffer (pfile, NULL, 0);
499
500 if (fp == 0)
501 goto push_fail;
502
0b3d776a
ZW
503 if (fstat (fd, &st) < 0)
504 goto perror_fail;
554fbeef 505
f8f769ea
ZW
506 /* If fd points to a plain file, we might be able to mmap it; we can
507 definitely allocate the buffer all at once. If fd is a pipe or
508 terminal, we can't do either. If fd is something weird, like a
509 block device or a directory, we don't want to read it at all.
6458033d
ZW
510
511 Unfortunately, different systems use different st.st_mode values
512 for pipes: some have S_ISFIFO, some S_ISSOCK, some are buggy and
10e56506
ZW
513 zero the entire struct stat except a couple fields. Hence we don't
514 even try to figure out what something is, except for plain files,
f8f769ea 515 directories, and block devices. */
6458033d 516
0b3d776a
ZW
517 if (S_ISREG (st.st_mode))
518 {
f8f769ea
ZW
519 ssize_t st_size;
520
521 /* off_t might have a wider range than ssize_t - in other words,
554fbeef 522 the max size of a file might be bigger than the address
6458033d 523 space. We can't handle a file that large. (Anyone with
f8f769ea 524 a single source file bigger than 2GB needs to rethink
e0fcc0e1
KG
525 their coding style.) Some systems (e.g. AIX 4.1) define
526 SSIZE_MAX to be much smaller than the actual range of the
527 type. Use INTTYPE_MAXIMUM unconditionally to ensure this
528 does not bite us. */
529 if (st.st_size > INTTYPE_MAXIMUM (ssize_t))
554fbeef 530 {
c31a6508 531 cpp_error (pfile, "%s is too large", inc->name);
554fbeef
ZW
532 goto fail;
533 }
f8f769ea
ZW
534 st_size = st.st_size;
535 length = read_file (fp, fd, st_size);
536 if (length == -1)
537 goto perror_fail;
538 if (length < st_size)
c31a6508 539 cpp_warning (pfile, "%s is shorter than expected\n", inc->name);
add7091b 540 }
10e56506 541 else if (S_ISBLK (st.st_mode))
0b3d776a 542 {
c31a6508 543 cpp_error (pfile, "%s is a block device", inc->name);
10e56506 544 goto fail;
0b3d776a 545 }
10e56506 546 else if (S_ISDIR (st.st_mode))
0b3d776a 547 {
c31a6508 548 cpp_error (pfile, "%s is a directory", inc->name);
554fbeef 549 goto fail;
add7091b 550 }
10e56506
ZW
551 else
552 {
f8f769ea
ZW
553 /* 8 kilobytes is a sensible starting size. It ought to be
554 bigger than the kernel pipe buffer, and it's definitely
555 bigger than the majority of C source files. */
556 length = read_with_read (fp, fd, 8 * 1024);
557 if (length == -1)
558 goto perror_fail;
10e56506 559 }
add7091b 560
f8f769ea 561 /* These must be set before prescan. */
c31a6508
ZW
562 fp->inc = inc;
563 fp->nominal_fname = inc->name;
f8f769ea 564
554fbeef 565 if (length == 0)
c31a6508 566 inc->cmacro = NEVER_REREAD;
f8f769ea
ZW
567 else
568 /* Temporary - I hope. */
569 length = _cpp_prescan (pfile, fp, length);
add7091b 570
ff2b53ef 571 fp->rlimit = fp->buf + length;
554fbeef 572 fp->cur = fp->buf;
554fbeef 573 fp->lineno = 1;
099a9dd0 574 fp->line_base = fp->buf;
554fbeef
ZW
575
576 /* The ->actual_dir field is only used when ignore_srcdir is not in effect;
577 see do_include */
ae79697b 578 if (!CPP_OPTION (pfile, ignore_srcdir))
c31a6508 579 fp->actual_dir = actual_directory (pfile, inc->name);
554fbeef 580
add7091b 581 pfile->input_stack_listing_current = 0;
d35364d1 582 pfile->only_seen_white = 2;
add7091b
ZW
583 return 1;
584
0b3d776a 585 perror_fail:
c31a6508
ZW
586 cpp_error_from_errno (pfile, inc->name);
587 /* Do not try to read this file again. */
588 close (fd);
589 inc->fd = -1;
590 inc->cmacro = NEVER_REREAD;
0b3d776a 591 fail:
554fbeef 592 cpp_pop_buffer (pfile);
d35364d1 593 push_fail:
0b3d776a 594 return 0;
add7091b
ZW
595}
596
f8f769ea
ZW
597static ssize_t
598read_file (fp, fd, size)
599 cpp_buffer *fp;
600 int fd;
601 ssize_t size;
602{
603 static int pagesize = -1;
604
605 if (size == 0)
606 return 0;
607
608 if (pagesize == -1)
609 pagesize = getpagesize ();
610
611#if MMAP_THRESHOLD
612 if (size / pagesize >= MMAP_THRESHOLD)
613 {
614 const U_CHAR *result
615 = (const U_CHAR *) mmap (0, size, PROT_READ, MAP_PRIVATE, fd, 0);
616 if (result != (const U_CHAR *)-1)
617 {
618 fp->buf = result;
619 fp->mapped = 1;
620 return size;
621 }
622 }
623 /* If mmap fails, try read. If there's really a problem, read will
624 fail too. */
625#endif
626
627 return read_with_read (fp, fd, size);
628}
629
630static ssize_t
631read_with_read (fp, fd, size)
632 cpp_buffer *fp;
633 int fd;
634 ssize_t size;
635{
636 ssize_t offset, count;
637 U_CHAR *buf;
638
639 buf = (U_CHAR *) xmalloc (size);
640 offset = 0;
641 while ((count = read (fd, buf + offset, size - offset)) > 0)
642 {
643 offset += count;
644 if (offset == size)
645 buf = xrealloc (buf, (size *= 2));
646 }
647 if (count < 0)
648 {
649 free (buf);
650 return -1;
651 }
652 if (offset == 0)
653 {
654 free (buf);
655 return 0;
656 }
657
658 if (offset < size)
659 buf = xrealloc (buf, offset);
660 fp->buf = buf;
661 fp->mapped = 0;
662 return offset;
663}
664
c31a6508
ZW
665/* The file_name_map structure holds a mapping of file names for a
666 particular directory. This mapping is read from the file named
667 FILE_NAME_MAP_FILE in that directory. Such a file can be used to
668 map filenames on a file system with severe filename restrictions,
669 such as DOS. The format of the file name map file is just a series
670 of lines with two tokens on each line. The first token is the name
671 to map, and the second token is the actual name to use. */
672
673struct file_name_map
674{
675 struct file_name_map *map_next;
676 char *map_from;
677 char *map_to;
678};
679
680#define FILE_NAME_MAP_FILE "header.gcc"
681
682/* Read a space delimited string of unlimited length from a stdio
683 file. */
684
685static char *
686read_filename_string (ch, f)
687 int ch;
688 FILE *f;
689{
690 char *alloc, *set;
691 int len;
692
693 len = 20;
694 set = alloc = xmalloc (len + 1);
695 if (! is_space(ch))
696 {
697 *set++ = ch;
698 while ((ch = getc (f)) != EOF && ! is_space(ch))
699 {
700 if (set - alloc == len)
701 {
702 len *= 2;
703 alloc = xrealloc (alloc, len + 1);
704 set = alloc + len / 2;
705 }
706 *set++ = ch;
707 }
708 }
709 *set = '\0';
710 ungetc (ch, f);
711 return alloc;
712}
713
714/* This structure holds a linked list of file name maps, one per directory. */
715
716struct file_name_map_list
717{
718 struct file_name_map_list *map_list_next;
719 char *map_list_name;
720 struct file_name_map *map_list_map;
721};
722
723/* Read the file name map file for DIRNAME. */
724
725static struct file_name_map *
726read_name_map (pfile, dirname)
727 cpp_reader *pfile;
728 const char *dirname;
729{
730 register struct file_name_map_list *map_list_ptr;
731 char *name;
732 FILE *f;
733
734 for (map_list_ptr = CPP_OPTION (pfile, map_list); map_list_ptr;
735 map_list_ptr = map_list_ptr->map_list_next)
736 if (! strcmp (map_list_ptr->map_list_name, dirname))
737 return map_list_ptr->map_list_map;
738
739 map_list_ptr = ((struct file_name_map_list *)
740 xmalloc (sizeof (struct file_name_map_list)));
741 map_list_ptr->map_list_name = xstrdup (dirname);
742
743 name = (char *) alloca (strlen (dirname) + strlen (FILE_NAME_MAP_FILE) + 2);
744 strcpy (name, dirname);
745 if (*dirname)
746 strcat (name, "/");
747 strcat (name, FILE_NAME_MAP_FILE);
748 f = fopen (name, "r");
749 if (!f)
750 map_list_ptr->map_list_map = (struct file_name_map *)-1;
751 else
752 {
753 int ch;
754 int dirlen = strlen (dirname);
755
756 while ((ch = getc (f)) != EOF)
757 {
758 char *from, *to;
759 struct file_name_map *ptr;
760
761 if (is_space(ch))
762 continue;
763 from = read_filename_string (ch, f);
764 while ((ch = getc (f)) != EOF && is_hspace(ch))
765 ;
766 to = read_filename_string (ch, f);
767
768 ptr = ((struct file_name_map *)
769 xmalloc (sizeof (struct file_name_map)));
770 ptr->map_from = from;
771
772 /* Make the real filename absolute. */
773 if (*to == '/')
774 ptr->map_to = to;
775 else
776 {
777 ptr->map_to = xmalloc (dirlen + strlen (to) + 2);
778 strcpy (ptr->map_to, dirname);
779 ptr->map_to[dirlen] = '/';
780 strcpy (ptr->map_to + dirlen + 1, to);
781 free (to);
782 }
783
784 ptr->map_next = map_list_ptr->map_list_map;
785 map_list_ptr->map_list_map = ptr;
786
787 while ((ch = getc (f)) != '\n')
788 if (ch == EOF)
789 break;
790 }
791 fclose (f);
792 }
793
794 map_list_ptr->map_list_next = CPP_OPTION (pfile, map_list);
795 CPP_OPTION (pfile, map_list) = map_list_ptr;
796
797 return map_list_ptr->map_list_map;
798}
799
800/* Remap NAME based on the file_name_map (if any) for LOC. */
801
802static char *
803remap_filename (pfile, name, loc)
804 cpp_reader *pfile;
805 char *name;
806 struct file_name_list *loc;
807{
808 struct file_name_map *map;
809 const char *from, *p, *dir;
810
811 if (! loc->name_map)
812 loc->name_map = read_name_map (pfile,
813 loc->name
814 ? loc->name : ".");
815
816 if (loc->name_map == (struct file_name_map *)-1)
817 return name;
818
819 from = name + strlen (loc->name) + 1;
820
821 for (map = loc->name_map; map; map = map->map_next)
822 if (!strcmp (map->map_from, from))
823 return map->map_to;
824
825 /* Try to find a mapping file for the particular directory we are
826 looking in. Thus #include <sys/types.h> will look up sys/types.h
827 in /usr/include/header.gcc and look up types.h in
828 /usr/include/sys/header.gcc. */
829 p = strrchr (name, '/');
830 if (!p)
831 p = name;
832 if (loc && loc->name
833 && strlen (loc->name) == (size_t) (p - name)
834 && !strncmp (loc->name, name, p - name))
835 /* FILENAME is in SEARCHPTR, which we've already checked. */
836 return name;
837
838 if (p == name)
839 {
840 dir = ".";
841 from = name;
842 }
843 else
844 {
845 char * newdir = (char *) alloca (p - name + 1);
846 memcpy (newdir, name, p - name);
847 newdir[p - name] = '\0';
848 dir = newdir;
849 from = p + 1;
850 }
851
852 for (map = read_name_map (pfile, dir); map; map = map->map_next)
853 if (! strcmp (map->map_from, name))
854 return map->map_to;
855
856 return name;
857}
858
6458033d
ZW
859/* Given a path FNAME, extract the directory component and place it
860 onto the actual_dirs list. Return a pointer to the allocated
861 file_name_list structure. These structures are used to implement
862 current-directory "" include searching. */
863
f1a86df6
ZW
864static struct file_name_list *
865actual_directory (pfile, fname)
866 cpp_reader *pfile;
bcc5cac9 867 const char *fname;
f1a86df6
ZW
868{
869 char *last_slash, *dir;
870 size_t dlen;
871 struct file_name_list *x;
872
c49445e0 873 dir = xstrdup (fname);
7ceb3598 874 last_slash = strrchr (dir, '/');
f1a86df6
ZW
875 if (last_slash)
876 {
877 if (last_slash == dir)
878 {
879 dlen = 1;
880 last_slash[1] = '\0';
881 }
882 else
883 {
884 dlen = last_slash - dir;
885 *last_slash = '\0';
886 }
887 }
888 else
889 {
890 dir[0] = '.';
891 dir[1] = '\0';
892 dlen = 1;
893 }
894
895 if (dlen > pfile->max_include_len)
896 pfile->max_include_len = dlen;
897
898 for (x = pfile->actual_dirs; x; x = x->alloc)
899 if (!strcmp (x->name, dir))
900 {
901 free (dir);
902 return x;
903 }
904
905 /* Not found, make a new one. */
906 x = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
907 x->name = dir;
908 x->nlen = dlen;
ae79697b 909 x->next = CPP_OPTION (pfile, quote_include);
f1a86df6 910 x->alloc = pfile->actual_dirs;
c31a6508 911 x->sysp = CPP_BUFFER (pfile)->inc->sysp;
f1a86df6
ZW
912 x->name_map = NULL;
913
914 pfile->actual_dirs = x;
915 return x;
916}
917
0b3d776a
ZW
918/* Simplify a path name in place, deleting redundant components. This
919 reduces OS overhead and guarantees that equivalent paths compare
920 the same (modulo symlinks).
921
922 Transforms made:
923 foo/bar/../quux foo/quux
924 foo/./bar foo/bar
925 foo//bar foo/bar
926 /../quux /quux
927 //quux //quux (POSIX allows leading // as a namespace escape)
928
929 Guarantees no trailing slashes. All transforms reduce the length
930 of the string.
931 */
0b22d65c 932void
b0699dad 933_cpp_simplify_pathname (path)
21380ab0 934 char *path;
0b3d776a
ZW
935{
936 char *from, *to;
937 char *base;
938 int absolute = 0;
939
509781a4 940#if defined (HAVE_DOS_BASED_FILE_SYSTEM)
0b3d776a
ZW
941 /* Convert all backslashes to slashes. */
942 for (from = path; *from; from++)
943 if (*from == '\\') *from = '/';
944
945 /* Skip over leading drive letter if present. */
946 if (ISALPHA (path[0]) && path[1] == ':')
947 from = to = &path[2];
948 else
949 from = to = path;
950#else
951 from = to = path;
952#endif
953
954 /* Remove redundant initial /s. */
955 if (*from == '/')
956 {
957 absolute = 1;
958 to++;
959 from++;
960 if (*from == '/')
961 {
962 if (*++from == '/')
963 /* 3 or more initial /s are equivalent to 1 /. */
964 while (*++from == '/');
965 else
966 /* On some hosts // differs from /; Posix allows this. */
967 to++;
968 }
969 }
970 base = to;
971
972 for (;;)
973 {
974 while (*from == '/')
975 from++;
976
977 if (from[0] == '.' && from[1] == '/')
978 from += 2;
979 else if (from[0] == '.' && from[1] == '\0')
980 goto done;
981 else if (from[0] == '.' && from[1] == '.' && from[2] == '/')
982 {
983 if (base == to)
984 {
985 if (absolute)
986 from += 3;
987 else
988 {
989 *to++ = *from++;
990 *to++ = *from++;
991 *to++ = *from++;
992 base = to;
993 }
994 }
995 else
996 {
997 to -= 2;
998 while (to > base && *to != '/') to--;
999 if (*to == '/')
1000 to++;
1001 from += 3;
1002 }
1003 }
1004 else if (from[0] == '.' && from[1] == '.' && from[2] == '\0')
1005 {
1006 if (base == to)
1007 {
1008 if (!absolute)
1009 {
1010 *to++ = *from++;
1011 *to++ = *from++;
1012 }
1013 }
1014 else
1015 {
1016 to -= 2;
1017 while (to > base && *to != '/') to--;
1018 if (*to == '/')
1019 to++;
1020 }
1021 goto done;
1022 }
1023 else
1024 /* Copy this component and trailing /, if any. */
1025 while ((*to++ = *from++) != '/')
1026 {
1027 if (!to[-1])
1028 {
1029 to--;
1030 goto done;
1031 }
1032 }
1033
1034 }
1035
1036 done:
1037 /* Trim trailing slash */
1038 if (to[0] == '/' && (!absolute || to > path+1))
1039 to--;
1040
1041 /* Change the empty string to "." so that stat() on the result
1042 will always work. */
1043 if (to == path)
1044 *to++ = '.';
1045
1046 *to = '\0';
1047
1048 return;
1049}
1050
1051/* It is not clear when this should be used if at all, so I've
1052 disabled it until someone who understands VMS can look at it. */
1053#if 0
add7091b
ZW
1054
1055/* Under VMS we need to fix up the "include" specification filename.
1056
1057 Rules for possible conversions
1058
1059 fullname tried paths
1060
1061 name name
1062 ./dir/name [.dir]name
1063 /dir/name dir:name
1064 /name [000000]name, name
1065 dir/name dir:[000000]name, dir:name, dir/name
1066 dir1/dir2/name dir1:[dir2]name, dir1:[000000.dir2]name
1067 path:/name path:[000000]name, path:name
1068 path:/dir/name path:[000000.dir]name, path:[dir]name
1069 path:dir/name path:[dir]name
1070 [path]:[dir]name [path.dir]name
1071 path/[dir]name [path.dir]name
1072
1073 The path:/name input is constructed when expanding <> includes. */
1074
1075
1076static void
1077hack_vms_include_specification (fullname)
1078 char *fullname;
1079{
1080 register char *basename, *unixname, *local_ptr, *first_slash;
1081 int f, check_filename_before_returning, must_revert;
1082 char Local[512];
1083
1084 check_filename_before_returning = 0;
1085 must_revert = 0;
1086 /* See if we can find a 1st slash. If not, there's no path information. */
7ceb3598 1087 first_slash = strchr (fullname, '/');
add7091b
ZW
1088 if (first_slash == 0)
1089 return 0; /* Nothing to do!!! */
1090
1091 /* construct device spec if none given. */
1092
7ceb3598 1093 if (strchr (fullname, ':') == 0)
add7091b
ZW
1094 {
1095
1096 /* If fullname has a slash, take it as device spec. */
1097
1098 if (first_slash == fullname)
1099 {
7ceb3598 1100 first_slash = strchr (fullname + 1, '/'); /* 2nd slash ? */
add7091b
ZW
1101 if (first_slash)
1102 *first_slash = ':'; /* make device spec */
1103 for (basename = fullname; *basename != 0; basename++)
1104 *basename = *(basename+1); /* remove leading slash */
1105 }
1106 else if ((first_slash[-1] != '.') /* keep ':/', './' */
1107 && (first_slash[-1] != ':')
1108 && (first_slash[-1] != ']')) /* or a vms path */
1109 {
1110 *first_slash = ':';
1111 }
1112 else if ((first_slash[1] == '[') /* skip './' in './[dir' */
1113 && (first_slash[-1] == '.'))
1114 fullname += 2;
1115 }
1116
1117 /* Get part after first ':' (basename[-1] == ':')
1118 or last '/' (basename[-1] == '/'). */
1119
1120 basename = base_name (fullname);
1121
1122 local_ptr = Local; /* initialize */
1123
1124 /* We are trying to do a number of things here. First of all, we are
1125 trying to hammer the filenames into a standard format, such that later
1126 processing can handle them.
1127
1128 If the file name contains something like [dir.], then it recognizes this
1129 as a root, and strips the ".]". Later processing will add whatever is
1130 needed to get things working properly.
1131
1132 If no device is specified, then the first directory name is taken to be
1133 a device name (or a rooted logical). */
1134
1135 /* Point to the UNIX filename part (which needs to be fixed!)
1136 but skip vms path information.
1137 [basename != fullname since first_slash != 0]. */
1138
1139 if ((basename[-1] == ':') /* vms path spec. */
1140 || (basename[-1] == ']')
1141 || (basename[-1] == '>'))
1142 unixname = basename;
1143 else
1144 unixname = fullname;
1145
1146 if (*unixname == '/')
1147 unixname++;
1148
1149 /* If the directory spec is not rooted, we can just copy
1150 the UNIX filename part and we are done. */
1151
1152 if (((basename - fullname) > 1)
1153 && ( (basename[-1] == ']')
1154 || (basename[-1] == '>')))
1155 {
1156 if (basename[-2] != '.')
1157 {
1158
1159 /* The VMS part ends in a `]', and the preceding character is not a `.'.
1160 -> PATH]:/name (basename = '/name', unixname = 'name')
1161 We strip the `]', and then splice the two parts of the name in the
86702e31 1162 usual way. Given the default locations for include files,
add7091b
ZW
1163 we will only use this code if the user specifies alternate locations
1164 with the /include (-I) switch on the command line. */
1165
1166 basename -= 1; /* Strip "]" */
1167 unixname--; /* backspace */
1168 }
1169 else
1170 {
1171
1172 /* The VMS part has a ".]" at the end, and this will not do. Later
1173 processing will add a second directory spec, and this would be a syntax
1174 error. Thus we strip the ".]", and thus merge the directory specs.
1175 We also backspace unixname, so that it points to a '/'. This inhibits the
1176 generation of the 000000 root directory spec (which does not belong here
1177 in this case). */
1178
1179 basename -= 2; /* Strip ".]" */
1180 unixname--; /* backspace */
1181 }
1182 }
1183
1184 else
1185
1186 {
1187
1188 /* We drop in here if there is no VMS style directory specification yet.
1189 If there is no device specification either, we make the first dir a
1190 device and try that. If we do not do this, then we will be essentially
1191 searching the users default directory (as if they did a #include "asdf.h").
1192
1193 Then all we need to do is to push a '[' into the output string. Later
1194 processing will fill this in, and close the bracket. */
1195
1196 if ((unixname != fullname) /* vms path spec found. */
1197 && (basename[-1] != ':'))
1198 *local_ptr++ = ':'; /* dev not in spec. take first dir */
1199
1200 *local_ptr++ = '['; /* Open the directory specification */
1201 }
1202
1203 if (unixname == fullname) /* no vms dir spec. */
1204 {
1205 must_revert = 1;
1206 if ((first_slash != 0) /* unix dir spec. */
1207 && (*unixname != '/') /* not beginning with '/' */
1208 && (*unixname != '.')) /* or './' or '../' */
1209 *local_ptr++ = '.'; /* dir is local ! */
1210 }
1211
1212 /* at this point we assume that we have the device spec, and (at least
1213 the opening "[" for a directory specification. We may have directories
1214 specified already.
1215
1216 If there are no other slashes then the filename will be
1217 in the "root" directory. Otherwise, we need to add
1218 directory specifications. */
1219
7ceb3598 1220 if (strchr (unixname, '/') == 0)
add7091b
ZW
1221 {
1222 /* if no directories specified yet and none are following. */
1223 if (local_ptr[-1] == '[')
1224 {
1225 /* Just add "000000]" as the directory string */
1226 strcpy (local_ptr, "000000]");
1227 local_ptr += strlen (local_ptr);
1228 check_filename_before_returning = 1; /* we might need to fool with this later */
1229 }
1230 }
1231 else
1232 {
1233
1234 /* As long as there are still subdirectories to add, do them. */
7ceb3598 1235 while (strchr (unixname, '/') != 0)
add7091b
ZW
1236 {
1237 /* If this token is "." we can ignore it
1238 if it's not at the beginning of a path. */
1239 if ((unixname[0] == '.') && (unixname[1] == '/'))
1240 {
1241 /* remove it at beginning of path. */
1242 if ( ((unixname == fullname) /* no device spec */
1243 && (fullname+2 != basename)) /* starts with ./ */
1244 /* or */
1245 || ((basename[-1] == ':') /* device spec */
1246 && (unixname-1 == basename))) /* and ./ afterwards */
1247 *local_ptr++ = '.'; /* make '[.' start of path. */
1248 unixname += 2;
1249 continue;
1250 }
1251
1252 /* Add a subdirectory spec. Do not duplicate "." */
1253 if ( local_ptr[-1] != '.'
1254 && local_ptr[-1] != '['
1255 && local_ptr[-1] != '<')
1256 *local_ptr++ = '.';
1257
1258 /* If this is ".." then the spec becomes "-" */
1259 if ( (unixname[0] == '.')
1260 && (unixname[1] == '.')
1261 && (unixname[2] == '/'))
1262 {
1263 /* Add "-" and skip the ".." */
1264 if ((local_ptr[-1] == '.')
1265 && (local_ptr[-2] == '['))
1266 local_ptr--; /* prevent [.- */
1267 *local_ptr++ = '-';
1268 unixname += 3;
1269 continue;
1270 }
1271
1272 /* Copy the subdirectory */
1273 while (*unixname != '/')
1274 *local_ptr++= *unixname++;
1275
1276 unixname++; /* Skip the "/" */
1277 }
1278
1279 /* Close the directory specification */
1280 if (local_ptr[-1] == '.') /* no trailing periods */
1281 local_ptr--;
1282
1283 if (local_ptr[-1] == '[') /* no dir needed */
1284 local_ptr--;
1285 else
1286 *local_ptr++ = ']';
1287 }
1288
1289 /* Now add the filename. */
1290
1291 while (*unixname)
1292 *local_ptr++ = *unixname++;
1293 *local_ptr = 0;
1294
1295 /* Now append it to the original VMS spec. */
1296
1297 strcpy ((must_revert==1)?fullname:basename, Local);
1298
1299 /* If we put a [000000] in the filename, try to open it first. If this fails,
1300 remove the [000000], and return that name. This provides flexibility
1301 to the user in that they can use both rooted and non-rooted logical names
1302 to point to the location of the file. */
1303
1304 if (check_filename_before_returning)
1305 {
e576beb0 1306 f = open (fullname, O_RDONLY|O_NONBLOCK);
add7091b
ZW
1307 if (f >= 0)
1308 {
1309 /* The file name is OK as it is, so return it as is. */
1310 close (f);
1311 return 1;
1312 }
1313
1314 /* The filename did not work. Try to remove the [000000] from the name,
1315 and return it. */
1316
7ceb3598
NB
1317 basename = strchr (fullname, '[');
1318 local_ptr = strchr (fullname, ']') + 1;
add7091b
ZW
1319 strcpy (basename, local_ptr); /* this gets rid of it */
1320
1321 }
1322
1323 return 1;
1324}
1325#endif /* VMS */