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