96ce7e48511dabf31463ad42f4e328c3471de078
[gcc.git] / gcc / cppfiles.c
1 /* Part of CPP library. (include file handling)
2 Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1998,
3 1999, 2000 Free Software Foundation, Inc.
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
9 This program is free software; you can redistribute it and/or modify it
10 under the terms of the GNU General Public License as published by the
11 Free Software Foundation; either version 2, or (at your option) any
12 later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
22
23 #include "config.h"
24 #include "system.h"
25 #include "cpplib.h"
26 #include "cpphash.h"
27 #include "intl.h"
28 #include "mkdeps.h"
29 #include "splay-tree.h"
30
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
42 #ifndef O_BINARY
43 # define O_BINARY 0
44 #endif
45
46 #ifndef INCLUDE_LEN_FUDGE
47 # define INCLUDE_LEN_FUDGE 0
48 #endif
49
50 /* If errno is inspected immediately after a system call fails, it will be
51 nonzero, and no error number will ever be zero. */
52 #ifndef ENOENT
53 # define ENOENT 0
54 #endif
55 #ifndef ENOTDIR
56 # define ENOTDIR 0
57 #endif
58 #ifndef ENOMEM
59 # define ENOMEM 0
60 #endif
61
62 /* Suppress warning about function macros used w/o arguments in traditional
63 C. It is unlikely that glibc's strcmp macro helps this file at all. */
64 #undef strcmp
65
66 /* This structure is used for the table of all includes. */
67 struct include_file
68 {
69 const char *name; /* actual path name of file */
70 const cpp_hashnode *cmacro; /* macro, if any, preventing reinclusion. */
71 const struct file_name_list *foundhere;
72 /* location in search path where file was
73 found, for #include_next and sysp. */
74 const unsigned char *buffer; /* pointer to cached file contents */
75 struct stat st; /* copy of stat(2) data for file */
76 int fd; /* fd open on file (short term storage only) */
77 unsigned short include_count; /* number of times file has been read */
78 unsigned short refcnt; /* number of stacked buffers using this file */
79 unsigned char mapped; /* file buffer is mmapped */
80 unsigned char defined; /* cmacro prevents inclusion in this state */
81 };
82
83 /* The cmacro works like this: If it's NULL, the file is to be
84 included again. If it's NEVER_REREAD, the file is never to be
85 included again. Otherwise it is a macro hashnode, and the file is
86 to be included again if the macro is defined or not as specified by
87 DEFINED. */
88 #define NEVER_REREAD ((const cpp_hashnode *)-1)
89 #define DO_NOT_REREAD(inc) \
90 ((inc)->cmacro && ((inc)->cmacro == NEVER_REREAD \
91 || ((inc)->cmacro->type == NT_MACRO) == (inc)->defined))
92
93 static struct file_name_map *read_name_map
94 PARAMS ((cpp_reader *, const char *));
95 static char *read_filename_string PARAMS ((int, FILE *));
96 static char *remap_filename PARAMS ((cpp_reader *, char *,
97 struct file_name_list *));
98 static struct file_name_list *actual_directory
99 PARAMS ((cpp_reader *, const char *));
100 static struct include_file *find_include_file
101 PARAMS ((cpp_reader *, const char *,
102 struct file_name_list *));
103 static struct include_file *open_file PARAMS ((cpp_reader *, const char *));
104 static void read_include_file PARAMS ((cpp_reader *, struct include_file *));
105 static void stack_include_file PARAMS ((cpp_reader *, struct include_file *));
106 static void purge_cache PARAMS ((struct include_file *));
107 static void destroy_include_file_node PARAMS ((splay_tree_value));
108 static int report_missing_guard PARAMS ((splay_tree_node, void *));
109
110 /* We use a splay tree to store information about all the include
111 files seen in this compilation. The key of each tree node is the
112 physical path to the file. The value is 0 if the file does not
113 exist, or a struct include_file pointer. */
114
115 static void
116 destroy_include_file_node (v)
117 splay_tree_value v;
118 {
119 struct include_file *f = (struct include_file *)v;
120 if (f)
121 {
122 purge_cache (f);
123 free (f); /* The tree is registered with free to free f->name. */
124 }
125 }
126
127 void
128 _cpp_init_includes (pfile)
129 cpp_reader *pfile;
130 {
131 pfile->all_include_files
132 = splay_tree_new ((splay_tree_compare_fn) strcmp,
133 (splay_tree_delete_key_fn) free,
134 destroy_include_file_node);
135 }
136
137 void
138 _cpp_cleanup_includes (pfile)
139 cpp_reader *pfile;
140 {
141 splay_tree_delete (pfile->all_include_files);
142 }
143
144 /* Mark a file to not be reread (e.g. #import, read failure). */
145 void
146 _cpp_never_reread (file)
147 struct include_file *file;
148 {
149 file->cmacro = NEVER_REREAD;
150 }
151
152 /* Given a file name, look it up in the cache; if there is no entry,
153 create one with a non-NULL value (regardless of success in opening
154 the file). If the file doesn't exist or is inaccessible, this
155 entry is flagged so we don't attempt to open it again in the
156 future. If the file isn't open, open it.
157
158 Returns an include_file structure with an open file descriptor on
159 success, or NULL on failure. */
160
161 static struct include_file *
162 open_file (pfile, filename)
163 cpp_reader *pfile;
164 const char *filename;
165 {
166 splay_tree_node nd;
167 struct include_file *file;
168
169 nd = splay_tree_lookup (pfile->all_include_files, (splay_tree_key) filename);
170
171 if (nd)
172 {
173 file = (struct include_file *) nd->value;
174
175 /* Don't retry opening if we failed previously. */
176 if (file->fd == -2)
177 return 0;
178
179 /* Don't reopen an idempotent file. */
180 if (DO_NOT_REREAD (file))
181 return file;
182
183 /* Don't reopen one which is already loaded. */
184 if (file->buffer != NULL)
185 return file;
186 }
187 else
188 {
189 /* In particular, this clears foundhere. */
190 file = xcnew (struct include_file);
191 file->name = xstrdup (filename);
192 splay_tree_insert (pfile->all_include_files,
193 (splay_tree_key) file->name,
194 (splay_tree_value) file);
195 }
196
197 /* We used to open files in nonblocking mode, but that caused more
198 problems than it solved. Do take care not to acquire a
199 controlling terminal by mistake (this can't happen on sane
200 systems, but paranoia is a virtue).
201
202 Use the three-argument form of open even though we aren't
203 specifying O_CREAT, to defend against broken system headers.
204
205 O_BINARY tells some runtime libraries (notably DJGPP) not to do
206 newline translation; we can handle DOS line breaks just fine
207 ourselves.
208
209 Special case: the empty string is translated to stdin. */
210
211 if (filename[0] == '\0')
212 file->fd = 0;
213 else
214 file->fd = open (filename, O_RDONLY | O_NOCTTY | O_BINARY, 0666);
215
216 if (file->fd != -1 && fstat (file->fd, &file->st) == 0)
217 {
218 /* Mark a regular, zero-length file never-reread now. */
219 if (S_ISREG (file->st.st_mode) && file->st.st_size == 0)
220 {
221 _cpp_never_reread (file);
222 close (file->fd);
223 file->fd = -1;
224 }
225
226 return file;
227 }
228
229 /* Don't issue an error message if the file doesn't exist. */
230 if (errno != ENOENT && errno != ENOTDIR)
231 cpp_error_from_errno (pfile, filename);
232
233 /* Create a negative node for this path, and return null. */
234 file->fd = -2;
235
236 return 0;
237 }
238
239 /* Place the file referenced by INC into a new buffer on PFILE's
240 stack. If there are errors, or the file should not be re-included,
241 a null buffer is pushed. */
242
243 static void
244 stack_include_file (pfile, inc)
245 cpp_reader *pfile;
246 struct include_file *inc;
247 {
248 cpp_buffer *fp;
249
250 /* Not in cache? */
251 if (! inc->buffer)
252 read_include_file (pfile, inc);
253
254 /* Push a null buffer. */
255 fp = cpp_push_buffer (pfile, NULL, 0, BUF_FILE, inc->name);
256 fp->inc = inc;
257 fp->buf = inc->buffer;
258 fp->rlimit = fp->buf;
259 if (! DO_NOT_REREAD (inc))
260 fp->rlimit += inc->st.st_size;
261 fp->cur = fp->buf;
262 fp->line_base = fp->buf;
263 fp->inc->refcnt++;
264 if (inc->foundhere)
265 fp->sysp = inc->foundhere->sysp;
266
267 /* The ->actual_dir field is only used when ignore_srcdir is not in effect;
268 see do_include */
269 if (!CPP_OPTION (pfile, ignore_srcdir))
270 fp->actual_dir = actual_directory (pfile, inc->name);
271
272 /* Initialise controlling macro state. */
273 pfile->mi_state = MI_OUTSIDE;
274 pfile->mi_cmacro = 0;
275 pfile->include_depth++;
276 }
277
278 /* Read the file referenced by INC into the file cache.
279
280 If fd points to a plain file, we might be able to mmap it; we can
281 definitely allocate the buffer all at once. If fd is a pipe or
282 terminal, we can't do either. If fd is something weird, like a
283 block device or a directory, we don't want to read it at all.
284
285 Unfortunately, different systems use different st.st_mode values
286 for pipes: some have S_ISFIFO, some S_ISSOCK, some are buggy and
287 zero the entire struct stat except a couple fields. Hence we don't
288 even try to figure out what something is, except for plain files,
289 directories, and block devices.
290
291 FIXME: Flush file cache and try again if we run out of memory. */
292
293 static void
294 read_include_file (pfile, inc)
295 cpp_reader *pfile;
296 struct include_file *inc;
297 {
298 ssize_t size, offset, count;
299 U_CHAR *buf;
300 #if MMAP_THRESHOLD
301 static int pagesize = -1;
302 #endif
303
304 if (DO_NOT_REREAD (inc))
305 return;
306
307 if (S_ISREG (inc->st.st_mode))
308 {
309 /* off_t might have a wider range than ssize_t - in other words,
310 the max size of a file might be bigger than the address
311 space. We can't handle a file that large. (Anyone with
312 a single source file bigger than 2GB needs to rethink
313 their coding style.) Some systems (e.g. AIX 4.1) define
314 SSIZE_MAX to be much smaller than the actual range of the
315 type. Use INTTYPE_MAXIMUM unconditionally to ensure this
316 does not bite us. */
317 if (inc->st.st_size > INTTYPE_MAXIMUM (ssize_t))
318 {
319 cpp_error (pfile, "%s is too large", inc->name);
320 goto fail;
321 }
322 size = inc->st.st_size;
323
324 inc->mapped = 0;
325 #if MMAP_THRESHOLD
326 if (pagesize == -1)
327 pagesize = getpagesize ();
328
329 if (size / pagesize >= MMAP_THRESHOLD)
330 {
331 buf = (U_CHAR *) mmap (0, size, PROT_READ, MAP_PRIVATE, inc->fd, 0);
332 if (buf == (U_CHAR *)-1)
333 goto perror_fail;
334 inc->mapped = 1;
335 }
336 else
337 #endif
338 {
339 buf = (U_CHAR *) xmalloc (size);
340 offset = 0;
341 while (offset < size)
342 {
343 count = read (inc->fd, buf + offset, size - offset);
344 if (count < 0)
345 goto perror_fail;
346 if (count == 0)
347 {
348 cpp_warning (pfile, "%s is shorter than expected", inc->name);
349 break;
350 }
351 offset += count;
352 }
353 }
354 }
355 else if (S_ISBLK (inc->st.st_mode))
356 {
357 cpp_error (pfile, "%s is a block device", inc->name);
358 goto fail;
359 }
360 else if (S_ISDIR (inc->st.st_mode))
361 {
362 cpp_error (pfile, "%s is a directory", inc->name);
363 goto fail;
364 }
365 else
366 {
367 /* 8 kilobytes is a sensible starting size. It ought to be
368 bigger than the kernel pipe buffer, and it's definitely
369 bigger than the majority of C source files. */
370 size = 8 * 1024;
371
372 buf = (U_CHAR *) xmalloc (size);
373 offset = 0;
374 while ((count = read (inc->fd, buf + offset, size - offset)) > 0)
375 {
376 offset += count;
377 if (offset == size)
378 buf = xrealloc (buf, (size *= 2));
379 }
380 if (count < 0)
381 goto perror_fail;
382
383 if (offset < size)
384 buf = xrealloc (buf, offset);
385 inc->st.st_size = offset;
386 }
387
388 close (inc->fd);
389 inc->buffer = buf;
390 inc->fd = -1;
391 return;
392
393 perror_fail:
394 cpp_error_from_errno (pfile, inc->name);
395 fail:
396 /* Do not try to read this file again. */
397 close (inc->fd);
398 inc->fd = -1;
399 _cpp_never_reread (inc);
400 return;
401 }
402
403 static void
404 purge_cache (inc)
405 struct include_file *inc;
406 {
407 if (inc->buffer)
408 {
409 #if MMAP_THRESHOLD
410 if (inc->mapped)
411 munmap ((PTR) inc->buffer, inc->st.st_size);
412 else
413 #endif
414 free ((PTR) inc->buffer);
415 inc->buffer = NULL;
416 }
417 }
418
419 /* Return 1 if the file named by FNAME has been included before in
420 any context, 0 otherwise. */
421 int
422 cpp_included (pfile, fname)
423 cpp_reader *pfile;
424 const char *fname;
425 {
426 struct file_name_list *path;
427 char *name;
428 splay_tree_node nd;
429
430 if (fname[0] == '/')
431 {
432 /* Just look it up. */
433 nd = splay_tree_lookup (pfile->all_include_files, (splay_tree_key) fname);
434 return (nd && nd->value);
435 }
436
437 /* Search directory path for the file. */
438 name = (char *) alloca (strlen (fname) + pfile->max_include_len
439 + 2 + INCLUDE_LEN_FUDGE);
440 for (path = CPP_OPTION (pfile, quote_include); path; path = path->next)
441 {
442 memcpy (name, path->name, path->nlen);
443 name[path->nlen] = '/';
444 strcpy (&name[path->nlen+1], fname);
445 _cpp_simplify_pathname (name);
446 if (CPP_OPTION (pfile, remap))
447 name = remap_filename (pfile, name, path);
448
449 nd = splay_tree_lookup (pfile->all_include_files, (splay_tree_key) name);
450 if (nd && nd->value)
451 return 1;
452 }
453 return 0;
454 }
455
456 /* Search for include file FNAME in the include chain starting at
457 SEARCH_START. Return 0 if there is no such file (or it's un-openable),
458 otherwise an include_file structure. */
459
460 static struct include_file *
461 find_include_file (pfile, fname, search_start)
462 cpp_reader *pfile;
463 const char *fname;
464 struct file_name_list *search_start;
465 {
466 struct file_name_list *path;
467 char *name;
468 struct include_file *file;
469
470 if (fname[0] == '/')
471 return open_file (pfile, fname);
472
473 /* Search directory path for the file. */
474 name = (char *) alloca (strlen (fname) + pfile->max_include_len
475 + 2 + INCLUDE_LEN_FUDGE);
476 for (path = search_start; path; path = path->next)
477 {
478 memcpy (name, path->name, path->nlen);
479 name[path->nlen] = '/';
480 strcpy (&name[path->nlen+1], fname);
481 _cpp_simplify_pathname (name);
482 if (CPP_OPTION (pfile, remap))
483 name = remap_filename (pfile, name, path);
484
485 file = open_file (pfile, name);
486 if (file)
487 {
488 file->foundhere = path;
489 return file;
490 }
491 }
492 return 0;
493 }
494
495 /* Not everyone who wants to set system-header-ness on a buffer can
496 see the details of a buffer. This is an exported interface because
497 fix-header needs it. */
498 void
499 cpp_make_system_header (pfile, syshdr, externc)
500 cpp_reader *pfile;
501 int syshdr, externc;
502 {
503 int flags = 0;
504
505 /* 1 = system header, 2 = system header to be treated as C. */
506 if (syshdr)
507 flags = 1 + (externc != 0);
508 pfile->buffer->sysp = flags;
509 }
510
511 /* Report on all files that might benefit from a multiple include guard.
512 Triggered by -H. */
513 void
514 _cpp_report_missing_guards (pfile)
515 cpp_reader *pfile;
516 {
517 int banner = 0;
518 splay_tree_foreach (pfile->all_include_files, report_missing_guard,
519 (PTR) &banner);
520 }
521
522 static int
523 report_missing_guard (n, b)
524 splay_tree_node n;
525 void *b;
526 {
527 struct include_file *f = (struct include_file *) n->value;
528 int *bannerp = (int *)b;
529
530 if (f && f->cmacro == 0 && f->include_count == 1)
531 {
532 if (*bannerp == 0)
533 {
534 fputs (_("Multiple include guards may be useful for:\n"), stderr);
535 *bannerp = 1;
536 }
537 fputs (f->name, stderr);
538 putc ('\n', stderr);
539 }
540 return 0;
541 }
542
543 #define PRINT_THIS_DEP(p, b) (CPP_PRINT_DEPS(p) > (b||p->system_include_depth))
544 void
545 _cpp_execute_include (pfile, header, no_reinclude, include_next)
546 cpp_reader *pfile;
547 const cpp_token *header;
548 int no_reinclude;
549 int include_next;
550 {
551 struct file_name_list *search_start = 0;
552 unsigned int len = header->val.str.len;
553 unsigned int angle_brackets = header->type == CPP_HEADER_NAME;
554 struct include_file *inc;
555 char *fname;
556
557 /* Help protect #include or similar from recursion. */
558 if (pfile->buffer_stack_depth >= CPP_STACK_MAX)
559 {
560 cpp_fatal (pfile, "#include nested too deeply");
561 return;
562 }
563
564 /* Check we've tidied up #include before entering the buffer. */
565 if (pfile->context->prev)
566 {
567 cpp_ice (pfile, "attempt to push file buffer with contexts stacked");
568 return;
569 }
570
571 /* For #include_next, skip in the search path past the dir in which
572 the current file was found. If this is the last directory in the
573 search path, don't include anything. If the current file was
574 specified with an absolute path, use the normal search logic. If
575 this is the primary source file, use the normal search logic and
576 generate a warning. */
577 if (include_next)
578 {
579 if (! pfile->buffer->prev)
580 cpp_warning (pfile, "#include_next in primary source file");
581 else
582 {
583 if (pfile->buffer->inc->foundhere)
584 {
585 search_start = pfile->buffer->inc->foundhere->next;
586 if (! search_start)
587 return;
588 }
589 }
590 }
591
592 fname = alloca (len + 1);
593 memcpy (fname, header->val.str.text, len);
594 fname[len] = '\0';
595
596 if (!search_start)
597 {
598 if (angle_brackets)
599 search_start = CPP_OPTION (pfile, bracket_include);
600 else if (CPP_OPTION (pfile, ignore_srcdir))
601 search_start = CPP_OPTION (pfile, quote_include);
602 else
603 search_start = CPP_BUFFER (pfile)->actual_dir;
604
605 if (!search_start)
606 {
607 cpp_error (pfile, "No include path in which to find %s", fname);
608 return;
609 }
610 }
611
612 inc = find_include_file (pfile, fname, search_start);
613
614 if (inc)
615 {
616 /* For -M, add the file to the dependencies on its first inclusion. */
617 if (!inc->include_count && PRINT_THIS_DEP (pfile, angle_brackets))
618 deps_add_dep (pfile->deps, inc->name);
619 inc->include_count++;
620
621 /* Actually process the file. */
622 stack_include_file (pfile, inc);
623
624 if (angle_brackets)
625 pfile->system_include_depth++;
626
627 if (! DO_NOT_REREAD (inc))
628 {
629 if (no_reinclude)
630 _cpp_never_reread (inc);
631
632 /* Handle -H option. */
633 if (CPP_OPTION (pfile, print_include_names))
634 {
635 cpp_buffer *fp = CPP_BUFFER (pfile);
636 while ((fp = CPP_PREV_BUFFER (fp)) != NULL)
637 putc ('.', stderr);
638 fprintf (stderr, " %s\n", inc->name);
639 }
640 }
641
642 return;
643 }
644
645 if (CPP_OPTION (pfile, print_deps_missing_files)
646 && PRINT_THIS_DEP (pfile, angle_brackets))
647 {
648 if (!angle_brackets)
649 deps_add_dep (pfile->deps, fname);
650 else
651 {
652 char *p;
653 struct file_name_list *ptr;
654 /* If requested as a system header, assume it belongs in
655 the first system header directory. */
656 if (CPP_OPTION (pfile, bracket_include))
657 ptr = CPP_OPTION (pfile, bracket_include);
658 else
659 ptr = CPP_OPTION (pfile, quote_include);
660
661 p = (char *) alloca (strlen (ptr->name)
662 + strlen (fname) + 2);
663 if (*ptr->name != '\0')
664 {
665 strcpy (p, ptr->name);
666 strcat (p, "/");
667 }
668 strcat (p, fname);
669 _cpp_simplify_pathname (p);
670 deps_add_dep (pfile->deps, p);
671 }
672 }
673 /* If -M was specified, and this header file won't be added to
674 the dependency list, then don't count this as an error,
675 because we can still produce correct output. Otherwise, we
676 can't produce correct output, because there may be
677 dependencies we need inside the missing file, and we don't
678 know what directory this missing file exists in. */
679 else if (CPP_PRINT_DEPS (pfile)
680 && ! PRINT_THIS_DEP (pfile, angle_brackets))
681 cpp_warning (pfile, "No include path in which to find %s", fname);
682 else
683 cpp_error_from_errno (pfile, fname);
684 }
685
686 /* Locate file F, and determine whether it is newer than PFILE. Return -1,
687 if F cannot be located or dated, 1, if it is newer and 0 if older. */
688 int
689 _cpp_compare_file_date (pfile, f)
690 cpp_reader *pfile;
691 const cpp_token *f;
692 {
693 unsigned int len = f->val.str.len;
694 char *fname;
695 struct file_name_list *search_start;
696 struct include_file *inc;
697
698 if (f->type == CPP_HEADER_NAME)
699 search_start = CPP_OPTION (pfile, bracket_include);
700 else if (CPP_OPTION (pfile, ignore_srcdir))
701 search_start = CPP_OPTION (pfile, quote_include);
702 else
703 search_start = CPP_BUFFER (pfile)->actual_dir;
704
705 fname = alloca (len + 1);
706 memcpy (fname, f->val.str.text, len);
707 fname[len] = '\0';
708 inc = find_include_file (pfile, fname, search_start);
709
710 if (!inc)
711 return -1;
712 if (inc->fd > 0)
713 {
714 close (inc->fd);
715 inc->fd = -1;
716 }
717
718 return inc->st.st_mtime > CPP_BUFFER (pfile)->inc->st.st_mtime;
719 }
720
721
722 /* Push an input buffer and load it up with the contents of FNAME.
723 If FNAME is "" or NULL, read standard input. */
724 int
725 _cpp_read_file (pfile, fname)
726 cpp_reader *pfile;
727 const char *fname;
728 {
729 struct include_file *f;
730
731 if (fname == NULL)
732 fname = "";
733
734 f = open_file (pfile, fname);
735
736 if (f == NULL)
737 {
738 cpp_error_from_errno (pfile, fname);
739 return 0;
740 }
741
742 stack_include_file (pfile, f);
743 return 1;
744 }
745
746 /* Do appropriate cleanup when a file buffer is popped off the input
747 stack. */
748 void
749 _cpp_pop_file_buffer (pfile, buf)
750 cpp_reader *pfile;
751 cpp_buffer *buf;
752 {
753 struct include_file *inc = buf->inc;
754
755 if (pfile->system_include_depth)
756 pfile->system_include_depth--;
757 if (pfile->include_depth)
758 pfile->include_depth--;
759
760 /* Record the inclusion-preventing macro and its definedness. */
761 if (pfile->mi_state == MI_OUTSIDE && inc->cmacro != NEVER_REREAD)
762 {
763 /* This could be NULL meaning no controlling macro. */
764 inc->cmacro = pfile->mi_cmacro;
765 inc->defined = 1;
766 }
767
768 /* Invalidate control macros in the #including file. */
769 pfile->mi_state = MI_FAILED;
770
771 inc->refcnt--;
772 if (inc->refcnt == 0 && DO_NOT_REREAD (inc))
773 purge_cache (inc);
774 }
775
776 /* The file_name_map structure holds a mapping of file names for a
777 particular directory. This mapping is read from the file named
778 FILE_NAME_MAP_FILE in that directory. Such a file can be used to
779 map filenames on a file system with severe filename restrictions,
780 such as DOS. The format of the file name map file is just a series
781 of lines with two tokens on each line. The first token is the name
782 to map, and the second token is the actual name to use. */
783
784 struct file_name_map
785 {
786 struct file_name_map *map_next;
787 char *map_from;
788 char *map_to;
789 };
790
791 #define FILE_NAME_MAP_FILE "header.gcc"
792
793 /* Read a space delimited string of unlimited length from a stdio
794 file. */
795
796 static char *
797 read_filename_string (ch, f)
798 int ch;
799 FILE *f;
800 {
801 char *alloc, *set;
802 int len;
803
804 len = 20;
805 set = alloc = xmalloc (len + 1);
806 if (! is_space(ch))
807 {
808 *set++ = ch;
809 while ((ch = getc (f)) != EOF && ! is_space(ch))
810 {
811 if (set - alloc == len)
812 {
813 len *= 2;
814 alloc = xrealloc (alloc, len + 1);
815 set = alloc + len / 2;
816 }
817 *set++ = ch;
818 }
819 }
820 *set = '\0';
821 ungetc (ch, f);
822 return alloc;
823 }
824
825 /* This structure holds a linked list of file name maps, one per directory. */
826
827 struct file_name_map_list
828 {
829 struct file_name_map_list *map_list_next;
830 char *map_list_name;
831 struct file_name_map *map_list_map;
832 };
833
834 /* Read the file name map file for DIRNAME. */
835
836 static struct file_name_map *
837 read_name_map (pfile, dirname)
838 cpp_reader *pfile;
839 const char *dirname;
840 {
841 register struct file_name_map_list *map_list_ptr;
842 char *name;
843 FILE *f;
844
845 /* Check the cache of directories, and mappings in their remap file. */
846 for (map_list_ptr = CPP_OPTION (pfile, map_list); map_list_ptr;
847 map_list_ptr = map_list_ptr->map_list_next)
848 if (! strcmp (map_list_ptr->map_list_name, dirname))
849 return map_list_ptr->map_list_map;
850
851 map_list_ptr = ((struct file_name_map_list *)
852 xmalloc (sizeof (struct file_name_map_list)));
853 map_list_ptr->map_list_name = xstrdup (dirname);
854
855 /* The end of the list ends in NULL. */
856 map_list_ptr->map_list_map = NULL;
857
858 name = (char *) alloca (strlen (dirname) + strlen (FILE_NAME_MAP_FILE) + 2);
859 strcpy (name, dirname);
860 if (*dirname)
861 strcat (name, "/");
862 strcat (name, FILE_NAME_MAP_FILE);
863 f = fopen (name, "r");
864
865 /* Silently return NULL if we cannot open. */
866 if (f)
867 {
868 int ch;
869 int dirlen = strlen (dirname);
870
871 while ((ch = getc (f)) != EOF)
872 {
873 char *from, *to;
874 struct file_name_map *ptr;
875
876 if (is_space(ch))
877 continue;
878 from = read_filename_string (ch, f);
879 while ((ch = getc (f)) != EOF && is_hspace(ch))
880 ;
881 to = read_filename_string (ch, f);
882
883 ptr = ((struct file_name_map *)
884 xmalloc (sizeof (struct file_name_map)));
885 ptr->map_from = from;
886
887 /* Make the real filename absolute. */
888 if (*to == '/')
889 ptr->map_to = to;
890 else
891 {
892 ptr->map_to = xmalloc (dirlen + strlen (to) + 2);
893 strcpy (ptr->map_to, dirname);
894 ptr->map_to[dirlen] = '/';
895 strcpy (ptr->map_to + dirlen + 1, to);
896 free (to);
897 }
898
899 ptr->map_next = map_list_ptr->map_list_map;
900 map_list_ptr->map_list_map = ptr;
901
902 while ((ch = getc (f)) != '\n')
903 if (ch == EOF)
904 break;
905 }
906 fclose (f);
907 }
908
909 /* Add this information to the cache. */
910 map_list_ptr->map_list_next = CPP_OPTION (pfile, map_list);
911 CPP_OPTION (pfile, map_list) = map_list_ptr;
912
913 return map_list_ptr->map_list_map;
914 }
915
916 /* Remap NAME based on the file_name_map (if any) for LOC. */
917
918 static char *
919 remap_filename (pfile, name, loc)
920 cpp_reader *pfile;
921 char *name;
922 struct file_name_list *loc;
923 {
924 struct file_name_map *map;
925 const char *from, *p;
926 char *dir;
927
928 if (! loc->name_map)
929 {
930 loc->name_map = read_name_map (pfile, loc->name ? loc->name : ".");
931 if (! loc->name_map)
932 return name;
933 }
934
935 from = name + strlen (loc->name) + 1;
936
937 for (map = loc->name_map; map; map = map->map_next)
938 if (!strcmp (map->map_from, from))
939 return map->map_to;
940
941 /* Try to find a mapping file for the particular directory we are
942 looking in. Thus #include <sys/types.h> will look up sys/types.h
943 in /usr/include/header.gcc and look up types.h in
944 /usr/include/sys/header.gcc. */
945 p = strrchr (name, '/');
946 if (!p)
947 return name;
948
949 /* We know p != name as absolute paths don't call remap_filename. */
950 if (p == name)
951 cpp_ice (pfile, "absolute file name in remap_filename");
952
953 dir = (char *) alloca (p - name + 1);
954 memcpy (dir, name, p - name);
955 dir[p - name] = '\0';
956 from = p + 1;
957
958 for (map = read_name_map (pfile, dir); map; map = map->map_next)
959 if (! strcmp (map->map_from, from))
960 return map->map_to;
961
962 return name;
963 }
964
965 /* Given a path FNAME, extract the directory component and place it
966 onto the actual_dirs list. Return a pointer to the allocated
967 file_name_list structure. These structures are used to implement
968 current-directory "" include searching. */
969
970 static struct file_name_list *
971 actual_directory (pfile, fname)
972 cpp_reader *pfile;
973 const char *fname;
974 {
975 char *last_slash, *dir;
976 size_t dlen;
977 struct file_name_list *x;
978
979 dir = xstrdup (fname);
980 last_slash = strrchr (dir, '/');
981 if (last_slash)
982 {
983 if (last_slash == dir)
984 {
985 dlen = 1;
986 last_slash[1] = '\0';
987 }
988 else
989 {
990 dlen = last_slash - dir;
991 *last_slash = '\0';
992 }
993 }
994 else
995 {
996 free (dir);
997 dir = xstrdup (".");
998 dlen = 1;
999 }
1000
1001 if (dlen > pfile->max_include_len)
1002 pfile->max_include_len = dlen;
1003
1004 for (x = pfile->actual_dirs; x; x = x->alloc)
1005 if (!strcmp (x->name, dir))
1006 {
1007 free (dir);
1008 return x;
1009 }
1010
1011 /* Not found, make a new one. */
1012 x = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
1013 x->name = dir;
1014 x->nlen = dlen;
1015 x->next = CPP_OPTION (pfile, quote_include);
1016 x->alloc = pfile->actual_dirs;
1017 x->sysp = pfile->buffer->sysp;
1018 x->name_map = NULL;
1019
1020 pfile->actual_dirs = x;
1021 return x;
1022 }
1023
1024 /* Simplify a path name in place, deleting redundant components. This
1025 reduces OS overhead and guarantees that equivalent paths compare
1026 the same (modulo symlinks).
1027
1028 Transforms made:
1029 foo/bar/../quux foo/quux
1030 foo/./bar foo/bar
1031 foo//bar foo/bar
1032 /../quux /quux
1033 //quux //quux (POSIX allows leading // as a namespace escape)
1034
1035 Guarantees no trailing slashes. All transforms reduce the length
1036 of the string.
1037 */
1038 void
1039 _cpp_simplify_pathname (path)
1040 char *path;
1041 {
1042 char *from, *to;
1043 char *base;
1044 int absolute = 0;
1045
1046 #if defined (HAVE_DOS_BASED_FILE_SYSTEM)
1047 /* Convert all backslashes to slashes. */
1048 for (from = path; *from; from++)
1049 if (*from == '\\') *from = '/';
1050
1051 /* Skip over leading drive letter if present. */
1052 if (ISALPHA (path[0]) && path[1] == ':')
1053 from = to = &path[2];
1054 else
1055 from = to = path;
1056 #else
1057 from = to = path;
1058 #endif
1059
1060 /* Remove redundant initial /s. */
1061 if (*from == '/')
1062 {
1063 absolute = 1;
1064 to++;
1065 from++;
1066 if (*from == '/')
1067 {
1068 if (*++from == '/')
1069 /* 3 or more initial /s are equivalent to 1 /. */
1070 while (*++from == '/');
1071 else
1072 /* On some hosts // differs from /; Posix allows this. */
1073 to++;
1074 }
1075 }
1076 base = to;
1077
1078 for (;;)
1079 {
1080 while (*from == '/')
1081 from++;
1082
1083 if (from[0] == '.' && from[1] == '/')
1084 from += 2;
1085 else if (from[0] == '.' && from[1] == '\0')
1086 goto done;
1087 else if (from[0] == '.' && from[1] == '.' && from[2] == '/')
1088 {
1089 if (base == to)
1090 {
1091 if (absolute)
1092 from += 3;
1093 else
1094 {
1095 *to++ = *from++;
1096 *to++ = *from++;
1097 *to++ = *from++;
1098 base = to;
1099 }
1100 }
1101 else
1102 {
1103 to -= 2;
1104 while (to > base && *to != '/') to--;
1105 if (*to == '/')
1106 to++;
1107 from += 3;
1108 }
1109 }
1110 else if (from[0] == '.' && from[1] == '.' && from[2] == '\0')
1111 {
1112 if (base == to)
1113 {
1114 if (!absolute)
1115 {
1116 *to++ = *from++;
1117 *to++ = *from++;
1118 }
1119 }
1120 else
1121 {
1122 to -= 2;
1123 while (to > base && *to != '/') to--;
1124 if (*to == '/')
1125 to++;
1126 }
1127 goto done;
1128 }
1129 else
1130 /* Copy this component and trailing /, if any. */
1131 while ((*to++ = *from++) != '/')
1132 {
1133 if (!to[-1])
1134 {
1135 to--;
1136 goto done;
1137 }
1138 }
1139
1140 }
1141
1142 done:
1143 /* Trim trailing slash */
1144 if (to[0] == '/' && (!absolute || to > path+1))
1145 to--;
1146
1147 /* Change the empty string to "." so that stat() on the result
1148 will always work. */
1149 if (to == path)
1150 *to++ = '.';
1151
1152 *to = '\0';
1153
1154 return;
1155 }