cppfiles.c (hack_vms_include_specification): Delete function (not used since 1998).
[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 const char *filename = 0;
249 unsigned int lineno = 0;
250 cpp_buffer *fp;
251
252 if (pfile->buffer)
253 {
254 filename = pfile->buffer->nominal_fname;
255 lineno = pfile->buffer->lineno;
256 }
257
258 /* Not in cache? */
259 if (! inc->buffer)
260 read_include_file (pfile, inc);
261
262 /* Push a null buffer. */
263 fp = cpp_push_buffer (pfile, NULL, 0, BUF_FILE, inc->name);
264 fp->inc = inc;
265 fp->buf = inc->buffer;
266 fp->rlimit = fp->buf;
267 if (! DO_NOT_REREAD (inc))
268 fp->rlimit += inc->st.st_size;
269 fp->cur = fp->buf;
270 fp->line_base = fp->buf;
271 fp->lineno = 0; /* For _cpp_do_file_change. */
272 fp->inc->refcnt++;
273 if (inc->foundhere)
274 fp->sysp = inc->foundhere->sysp;
275
276 /* The ->actual_dir field is only used when ignore_srcdir is not in effect;
277 see do_include */
278 if (!CPP_OPTION (pfile, ignore_srcdir))
279 fp->actual_dir = actual_directory (pfile, inc->name);
280
281 /* Initialise controlling macro state. */
282 pfile->mi_state = MI_OUTSIDE;
283 pfile->mi_cmacro = 0;
284 pfile->include_depth++;
285
286 _cpp_do_file_change (pfile, FC_ENTER, filename, lineno);
287
288 fp->lineno = 1;
289 }
290
291 /* Read the file referenced by INC into the file cache.
292
293 If fd points to a plain file, we might be able to mmap it; we can
294 definitely allocate the buffer all at once. If fd is a pipe or
295 terminal, we can't do either. If fd is something weird, like a
296 block device or a directory, we don't want to read it at all.
297
298 Unfortunately, different systems use different st.st_mode values
299 for pipes: some have S_ISFIFO, some S_ISSOCK, some are buggy and
300 zero the entire struct stat except a couple fields. Hence we don't
301 even try to figure out what something is, except for plain files,
302 directories, and block devices.
303
304 FIXME: Flush file cache and try again if we run out of memory. */
305
306 static void
307 read_include_file (pfile, inc)
308 cpp_reader *pfile;
309 struct include_file *inc;
310 {
311 ssize_t size, offset, count;
312 U_CHAR *buf;
313 #if MMAP_THRESHOLD
314 static int pagesize = -1;
315 #endif
316
317 if (DO_NOT_REREAD (inc))
318 return;
319
320 if (S_ISREG (inc->st.st_mode))
321 {
322 /* off_t might have a wider range than ssize_t - in other words,
323 the max size of a file might be bigger than the address
324 space. We can't handle a file that large. (Anyone with
325 a single source file bigger than 2GB needs to rethink
326 their coding style.) Some systems (e.g. AIX 4.1) define
327 SSIZE_MAX to be much smaller than the actual range of the
328 type. Use INTTYPE_MAXIMUM unconditionally to ensure this
329 does not bite us. */
330 if (inc->st.st_size > INTTYPE_MAXIMUM (ssize_t))
331 {
332 cpp_error (pfile, "%s is too large", inc->name);
333 goto fail;
334 }
335 size = inc->st.st_size;
336
337 inc->mapped = 0;
338 #if MMAP_THRESHOLD
339 if (pagesize == -1)
340 pagesize = getpagesize ();
341
342 if (size / pagesize >= MMAP_THRESHOLD)
343 {
344 buf = (U_CHAR *) mmap (0, size, PROT_READ, MAP_PRIVATE, inc->fd, 0);
345 if (buf == (U_CHAR *)-1)
346 goto perror_fail;
347 inc->mapped = 1;
348 }
349 else
350 #endif
351 {
352 buf = (U_CHAR *) xmalloc (size);
353 offset = 0;
354 while (offset < size)
355 {
356 count = read (inc->fd, buf + offset, size - offset);
357 if (count < 0)
358 goto perror_fail;
359 if (count == 0)
360 {
361 cpp_warning (pfile, "%s is shorter than expected", inc->name);
362 break;
363 }
364 offset += count;
365 }
366 }
367 }
368 else if (S_ISBLK (inc->st.st_mode))
369 {
370 cpp_error (pfile, "%s is a block device", inc->name);
371 goto fail;
372 }
373 else if (S_ISDIR (inc->st.st_mode))
374 {
375 cpp_error (pfile, "%s is a directory", inc->name);
376 goto fail;
377 }
378 else
379 {
380 /* 8 kilobytes is a sensible starting size. It ought to be
381 bigger than the kernel pipe buffer, and it's definitely
382 bigger than the majority of C source files. */
383 size = 8 * 1024;
384
385 buf = (U_CHAR *) xmalloc (size);
386 offset = 0;
387 while ((count = read (inc->fd, buf + offset, size - offset)) > 0)
388 {
389 offset += count;
390 if (offset == size)
391 buf = xrealloc (buf, (size *= 2));
392 }
393 if (count < 0)
394 goto perror_fail;
395
396 if (offset < size)
397 buf = xrealloc (buf, offset);
398 inc->st.st_size = offset;
399 }
400
401 close (inc->fd);
402 inc->buffer = buf;
403 inc->fd = -1;
404 return;
405
406 perror_fail:
407 cpp_error_from_errno (pfile, inc->name);
408 fail:
409 /* Do not try to read this file again. */
410 close (inc->fd);
411 inc->fd = -1;
412 _cpp_never_reread (inc);
413 return;
414 }
415
416 static void
417 purge_cache (inc)
418 struct include_file *inc;
419 {
420 if (inc->buffer)
421 {
422 #if MMAP_THRESHOLD
423 if (inc->mapped)
424 munmap ((PTR) inc->buffer, inc->st.st_size);
425 else
426 #endif
427 free ((PTR) inc->buffer);
428 inc->buffer = NULL;
429 }
430 }
431
432 /* Return 1 if the file named by FNAME has been included before in
433 any context, 0 otherwise. */
434 int
435 cpp_included (pfile, fname)
436 cpp_reader *pfile;
437 const char *fname;
438 {
439 struct file_name_list *path;
440 char *name;
441 splay_tree_node nd;
442
443 if (fname[0] == '/')
444 {
445 /* Just look it up. */
446 nd = splay_tree_lookup (pfile->all_include_files, (splay_tree_key) fname);
447 return (nd && nd->value);
448 }
449
450 /* Search directory path for the file. */
451 name = (char *) alloca (strlen (fname) + pfile->max_include_len
452 + 2 + INCLUDE_LEN_FUDGE);
453 for (path = CPP_OPTION (pfile, quote_include); path; path = path->next)
454 {
455 memcpy (name, path->name, path->nlen);
456 name[path->nlen] = '/';
457 strcpy (&name[path->nlen+1], fname);
458 _cpp_simplify_pathname (name);
459 if (CPP_OPTION (pfile, remap))
460 name = remap_filename (pfile, name, path);
461
462 nd = splay_tree_lookup (pfile->all_include_files, (splay_tree_key) name);
463 if (nd && nd->value)
464 return 1;
465 }
466 return 0;
467 }
468
469 /* Search for include file FNAME in the include chain starting at
470 SEARCH_START. Return 0 if there is no such file (or it's un-openable),
471 otherwise an include_file structure. */
472
473 static struct include_file *
474 find_include_file (pfile, fname, search_start)
475 cpp_reader *pfile;
476 const char *fname;
477 struct file_name_list *search_start;
478 {
479 struct file_name_list *path;
480 char *name;
481 struct include_file *file;
482
483 if (fname[0] == '/')
484 return open_file (pfile, fname);
485
486 /* Search directory path for the file. */
487 name = (char *) alloca (strlen (fname) + pfile->max_include_len
488 + 2 + INCLUDE_LEN_FUDGE);
489 for (path = search_start; path; path = path->next)
490 {
491 memcpy (name, path->name, path->nlen);
492 name[path->nlen] = '/';
493 strcpy (&name[path->nlen+1], fname);
494 _cpp_simplify_pathname (name);
495 if (CPP_OPTION (pfile, remap))
496 name = remap_filename (pfile, name, path);
497
498 file = open_file (pfile, name);
499 if (file)
500 {
501 file->foundhere = path;
502 return file;
503 }
504 }
505 return 0;
506 }
507
508 /* Not everyone who wants to set system-header-ness on a buffer can
509 see the details of a buffer. This is an exported interface because
510 fix-header needs it. */
511 void
512 cpp_make_system_header (pfile, syshdr, externc)
513 cpp_reader *pfile;
514 int syshdr, externc;
515 {
516 int flags = 0;
517
518 /* 1 = system header, 2 = system header to be treated as C. */
519 if (syshdr)
520 flags = 1 + (externc != 0);
521 pfile->buffer->sysp = flags;
522 }
523
524 /* Report on all files that might benefit from a multiple include guard.
525 Triggered by -H. */
526 void
527 _cpp_report_missing_guards (pfile)
528 cpp_reader *pfile;
529 {
530 int banner = 0;
531 splay_tree_foreach (pfile->all_include_files, report_missing_guard,
532 (PTR) &banner);
533 }
534
535 static int
536 report_missing_guard (n, b)
537 splay_tree_node n;
538 void *b;
539 {
540 struct include_file *f = (struct include_file *) n->value;
541 int *bannerp = (int *)b;
542
543 if (f && f->cmacro == 0 && f->include_count == 1)
544 {
545 if (*bannerp == 0)
546 {
547 fputs (_("Multiple include guards may be useful for:\n"), stderr);
548 *bannerp = 1;
549 }
550 fputs (f->name, stderr);
551 putc ('\n', stderr);
552 }
553 return 0;
554 }
555
556 #define PRINT_THIS_DEP(p, b) (CPP_PRINT_DEPS(p) > (b||p->system_include_depth))
557 void
558 _cpp_execute_include (pfile, header, no_reinclude, include_next)
559 cpp_reader *pfile;
560 const cpp_token *header;
561 int no_reinclude;
562 int include_next;
563 {
564 struct file_name_list *search_start = 0;
565 unsigned int len = header->val.str.len;
566 unsigned int angle_brackets = header->type == CPP_HEADER_NAME;
567 struct include_file *inc;
568 char *fname;
569
570 /* Help protect #include or similar from recursion. */
571 if (pfile->buffer_stack_depth >= CPP_STACK_MAX)
572 {
573 cpp_fatal (pfile, "#include nested too deeply");
574 return;
575 }
576
577 /* Check we've tidied up #include before entering the buffer. */
578 if (pfile->context->prev)
579 {
580 cpp_ice (pfile, "attempt to push file buffer with contexts stacked");
581 return;
582 }
583
584 /* For #include_next, skip in the search path past the dir in which
585 the current file was found. If this is the last directory in the
586 search path, don't include anything. If the current file was
587 specified with an absolute path, use the normal search logic. If
588 this is the primary source file, use the normal search logic and
589 generate a warning. */
590 if (include_next)
591 {
592 if (! pfile->buffer->prev)
593 cpp_warning (pfile, "#include_next in primary source file");
594 else
595 {
596 if (pfile->buffer->inc->foundhere)
597 {
598 search_start = pfile->buffer->inc->foundhere->next;
599 if (! search_start)
600 return;
601 }
602 }
603 }
604
605 fname = alloca (len + 1);
606 memcpy (fname, header->val.str.text, len);
607 fname[len] = '\0';
608
609 if (!search_start)
610 {
611 if (angle_brackets)
612 search_start = CPP_OPTION (pfile, bracket_include);
613 else if (CPP_OPTION (pfile, ignore_srcdir))
614 search_start = CPP_OPTION (pfile, quote_include);
615 else
616 search_start = CPP_BUFFER (pfile)->actual_dir;
617
618 if (!search_start)
619 {
620 cpp_error (pfile, "No include path in which to find %s", fname);
621 return;
622 }
623 }
624
625 inc = find_include_file (pfile, fname, search_start);
626
627 if (inc)
628 {
629 /* For -M, add the file to the dependencies on its first inclusion. */
630 if (!inc->include_count && PRINT_THIS_DEP (pfile, angle_brackets))
631 deps_add_dep (pfile->deps, inc->name);
632 inc->include_count++;
633
634 /* Actually process the file. */
635 stack_include_file (pfile, inc);
636
637 if (angle_brackets)
638 pfile->system_include_depth++;
639
640 if (! DO_NOT_REREAD (inc))
641 {
642 if (no_reinclude)
643 _cpp_never_reread (inc);
644
645 /* Handle -H option. */
646 if (CPP_OPTION (pfile, print_include_names))
647 {
648 cpp_buffer *fp = CPP_BUFFER (pfile);
649 while ((fp = CPP_PREV_BUFFER (fp)) != NULL)
650 putc ('.', stderr);
651 fprintf (stderr, " %s\n", inc->name);
652 }
653 }
654
655 return;
656 }
657
658 if (CPP_OPTION (pfile, print_deps_missing_files)
659 && PRINT_THIS_DEP (pfile, angle_brackets))
660 {
661 if (!angle_brackets)
662 deps_add_dep (pfile->deps, fname);
663 else
664 {
665 char *p;
666 struct file_name_list *ptr;
667 /* If requested as a system header, assume it belongs in
668 the first system header directory. */
669 if (CPP_OPTION (pfile, bracket_include))
670 ptr = CPP_OPTION (pfile, bracket_include);
671 else
672 ptr = CPP_OPTION (pfile, quote_include);
673
674 p = (char *) alloca (strlen (ptr->name)
675 + strlen (fname) + 2);
676 if (*ptr->name != '\0')
677 {
678 strcpy (p, ptr->name);
679 strcat (p, "/");
680 }
681 strcat (p, fname);
682 _cpp_simplify_pathname (p);
683 deps_add_dep (pfile->deps, p);
684 }
685 }
686 /* If -M was specified, and this header file won't be added to
687 the dependency list, then don't count this as an error,
688 because we can still produce correct output. Otherwise, we
689 can't produce correct output, because there may be
690 dependencies we need inside the missing file, and we don't
691 know what directory this missing file exists in. */
692 else if (CPP_PRINT_DEPS (pfile)
693 && ! PRINT_THIS_DEP (pfile, angle_brackets))
694 cpp_warning (pfile, "No include path in which to find %s", fname);
695 else
696 cpp_error_from_errno (pfile, fname);
697 }
698
699 /* Locate file F, and determine whether it is newer than PFILE. Return -1,
700 if F cannot be located or dated, 1, if it is newer and 0 if older. */
701 int
702 _cpp_compare_file_date (pfile, f)
703 cpp_reader *pfile;
704 const cpp_token *f;
705 {
706 unsigned int len = f->val.str.len;
707 char *fname;
708 struct file_name_list *search_start;
709 struct include_file *inc;
710
711 if (f->type == CPP_HEADER_NAME)
712 search_start = CPP_OPTION (pfile, bracket_include);
713 else if (CPP_OPTION (pfile, ignore_srcdir))
714 search_start = CPP_OPTION (pfile, quote_include);
715 else
716 search_start = CPP_BUFFER (pfile)->actual_dir;
717
718 fname = alloca (len + 1);
719 memcpy (fname, f->val.str.text, len);
720 fname[len] = '\0';
721 inc = find_include_file (pfile, fname, search_start);
722
723 if (!inc)
724 return -1;
725 if (inc->fd > 0)
726 {
727 close (inc->fd);
728 inc->fd = -1;
729 }
730
731 return inc->st.st_mtime > CPP_BUFFER (pfile)->inc->st.st_mtime;
732 }
733
734
735 /* Push an input buffer and load it up with the contents of FNAME.
736 If FNAME is "" or NULL, read standard input. */
737 int
738 _cpp_read_file (pfile, fname)
739 cpp_reader *pfile;
740 const char *fname;
741 {
742 struct include_file *f;
743
744 if (fname == NULL)
745 fname = "";
746
747 f = open_file (pfile, fname);
748
749 if (f == NULL)
750 {
751 cpp_error_from_errno (pfile, fname);
752 return 0;
753 }
754
755 stack_include_file (pfile, f);
756 return 1;
757 }
758
759 /* Do appropriate cleanup when a file buffer is popped off the input
760 stack. */
761 void
762 _cpp_pop_file_buffer (pfile, buf)
763 cpp_reader *pfile;
764 cpp_buffer *buf;
765 {
766 struct include_file *inc = buf->inc;
767
768 if (pfile->system_include_depth)
769 pfile->system_include_depth--;
770 if (pfile->include_depth)
771 pfile->include_depth--;
772
773 /* Record the inclusion-preventing macro and its definedness. */
774 if (pfile->mi_state == MI_OUTSIDE && inc->cmacro != NEVER_REREAD)
775 {
776 /* This could be NULL meaning no controlling macro. */
777 inc->cmacro = pfile->mi_cmacro;
778 inc->defined = 1;
779 }
780
781 /* Invalidate control macros in the #including file. */
782 pfile->mi_state = MI_FAILED;
783
784 inc->refcnt--;
785 if (inc->refcnt == 0 && DO_NOT_REREAD (inc))
786 purge_cache (inc);
787 }
788
789 /* The file_name_map structure holds a mapping of file names for a
790 particular directory. This mapping is read from the file named
791 FILE_NAME_MAP_FILE in that directory. Such a file can be used to
792 map filenames on a file system with severe filename restrictions,
793 such as DOS. The format of the file name map file is just a series
794 of lines with two tokens on each line. The first token is the name
795 to map, and the second token is the actual name to use. */
796
797 struct file_name_map
798 {
799 struct file_name_map *map_next;
800 char *map_from;
801 char *map_to;
802 };
803
804 #define FILE_NAME_MAP_FILE "header.gcc"
805
806 /* Read a space delimited string of unlimited length from a stdio
807 file. */
808
809 static char *
810 read_filename_string (ch, f)
811 int ch;
812 FILE *f;
813 {
814 char *alloc, *set;
815 int len;
816
817 len = 20;
818 set = alloc = xmalloc (len + 1);
819 if (! is_space(ch))
820 {
821 *set++ = ch;
822 while ((ch = getc (f)) != EOF && ! is_space(ch))
823 {
824 if (set - alloc == len)
825 {
826 len *= 2;
827 alloc = xrealloc (alloc, len + 1);
828 set = alloc + len / 2;
829 }
830 *set++ = ch;
831 }
832 }
833 *set = '\0';
834 ungetc (ch, f);
835 return alloc;
836 }
837
838 /* This structure holds a linked list of file name maps, one per directory. */
839
840 struct file_name_map_list
841 {
842 struct file_name_map_list *map_list_next;
843 char *map_list_name;
844 struct file_name_map *map_list_map;
845 };
846
847 /* Read the file name map file for DIRNAME. */
848
849 static struct file_name_map *
850 read_name_map (pfile, dirname)
851 cpp_reader *pfile;
852 const char *dirname;
853 {
854 register struct file_name_map_list *map_list_ptr;
855 char *name;
856 FILE *f;
857
858 /* Check the cache of directories, and mappings in their remap file. */
859 for (map_list_ptr = CPP_OPTION (pfile, map_list); map_list_ptr;
860 map_list_ptr = map_list_ptr->map_list_next)
861 if (! strcmp (map_list_ptr->map_list_name, dirname))
862 return map_list_ptr->map_list_map;
863
864 map_list_ptr = ((struct file_name_map_list *)
865 xmalloc (sizeof (struct file_name_map_list)));
866 map_list_ptr->map_list_name = xstrdup (dirname);
867
868 /* The end of the list ends in NULL. */
869 map_list_ptr->map_list_map = NULL;
870
871 name = (char *) alloca (strlen (dirname) + strlen (FILE_NAME_MAP_FILE) + 2);
872 strcpy (name, dirname);
873 if (*dirname)
874 strcat (name, "/");
875 strcat (name, FILE_NAME_MAP_FILE);
876 f = fopen (name, "r");
877
878 /* Silently return NULL if we cannot open. */
879 if (f)
880 {
881 int ch;
882 int dirlen = strlen (dirname);
883
884 while ((ch = getc (f)) != EOF)
885 {
886 char *from, *to;
887 struct file_name_map *ptr;
888
889 if (is_space(ch))
890 continue;
891 from = read_filename_string (ch, f);
892 while ((ch = getc (f)) != EOF && is_hspace(ch))
893 ;
894 to = read_filename_string (ch, f);
895
896 ptr = ((struct file_name_map *)
897 xmalloc (sizeof (struct file_name_map)));
898 ptr->map_from = from;
899
900 /* Make the real filename absolute. */
901 if (*to == '/')
902 ptr->map_to = to;
903 else
904 {
905 ptr->map_to = xmalloc (dirlen + strlen (to) + 2);
906 strcpy (ptr->map_to, dirname);
907 ptr->map_to[dirlen] = '/';
908 strcpy (ptr->map_to + dirlen + 1, to);
909 free (to);
910 }
911
912 ptr->map_next = map_list_ptr->map_list_map;
913 map_list_ptr->map_list_map = ptr;
914
915 while ((ch = getc (f)) != '\n')
916 if (ch == EOF)
917 break;
918 }
919 fclose (f);
920 }
921
922 /* Add this information to the cache. */
923 map_list_ptr->map_list_next = CPP_OPTION (pfile, map_list);
924 CPP_OPTION (pfile, map_list) = map_list_ptr;
925
926 return map_list_ptr->map_list_map;
927 }
928
929 /* Remap NAME based on the file_name_map (if any) for LOC. */
930
931 static char *
932 remap_filename (pfile, name, loc)
933 cpp_reader *pfile;
934 char *name;
935 struct file_name_list *loc;
936 {
937 struct file_name_map *map;
938 const char *from, *p;
939 char *dir;
940
941 if (! loc->name_map)
942 {
943 loc->name_map = read_name_map (pfile, loc->name ? loc->name : ".");
944 if (! loc->name_map)
945 return name;
946 }
947
948 from = name + strlen (loc->name) + 1;
949
950 for (map = loc->name_map; map; map = map->map_next)
951 if (!strcmp (map->map_from, from))
952 return map->map_to;
953
954 /* Try to find a mapping file for the particular directory we are
955 looking in. Thus #include <sys/types.h> will look up sys/types.h
956 in /usr/include/header.gcc and look up types.h in
957 /usr/include/sys/header.gcc. */
958 p = strrchr (name, '/');
959 if (!p)
960 return name;
961
962 /* We know p != name as absolute paths don't call remap_filename. */
963 if (p == name)
964 cpp_ice (pfile, "absolute file name in remap_filename");
965
966 dir = (char *) alloca (p - name + 1);
967 memcpy (dir, name, p - name);
968 dir[p - name] = '\0';
969 from = p + 1;
970
971 for (map = read_name_map (pfile, dir); map; map = map->map_next)
972 if (! strcmp (map->map_from, from))
973 return map->map_to;
974
975 return name;
976 }
977
978 /* Given a path FNAME, extract the directory component and place it
979 onto the actual_dirs list. Return a pointer to the allocated
980 file_name_list structure. These structures are used to implement
981 current-directory "" include searching. */
982
983 static struct file_name_list *
984 actual_directory (pfile, fname)
985 cpp_reader *pfile;
986 const char *fname;
987 {
988 char *last_slash, *dir;
989 size_t dlen;
990 struct file_name_list *x;
991
992 dir = xstrdup (fname);
993 last_slash = strrchr (dir, '/');
994 if (last_slash)
995 {
996 if (last_slash == dir)
997 {
998 dlen = 1;
999 last_slash[1] = '\0';
1000 }
1001 else
1002 {
1003 dlen = last_slash - dir;
1004 *last_slash = '\0';
1005 }
1006 }
1007 else
1008 {
1009 free (dir);
1010 dir = xstrdup (".");
1011 dlen = 1;
1012 }
1013
1014 if (dlen > pfile->max_include_len)
1015 pfile->max_include_len = dlen;
1016
1017 for (x = pfile->actual_dirs; x; x = x->alloc)
1018 if (!strcmp (x->name, dir))
1019 {
1020 free (dir);
1021 return x;
1022 }
1023
1024 /* Not found, make a new one. */
1025 x = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
1026 x->name = dir;
1027 x->nlen = dlen;
1028 x->next = CPP_OPTION (pfile, quote_include);
1029 x->alloc = pfile->actual_dirs;
1030 x->sysp = pfile->buffer->sysp;
1031 x->name_map = NULL;
1032
1033 pfile->actual_dirs = x;
1034 return x;
1035 }
1036
1037 /* Simplify a path name in place, deleting redundant components. This
1038 reduces OS overhead and guarantees that equivalent paths compare
1039 the same (modulo symlinks).
1040
1041 Transforms made:
1042 foo/bar/../quux foo/quux
1043 foo/./bar foo/bar
1044 foo//bar foo/bar
1045 /../quux /quux
1046 //quux //quux (POSIX allows leading // as a namespace escape)
1047
1048 Guarantees no trailing slashes. All transforms reduce the length
1049 of the string.
1050 */
1051 void
1052 _cpp_simplify_pathname (path)
1053 char *path;
1054 {
1055 char *from, *to;
1056 char *base;
1057 int absolute = 0;
1058
1059 #if defined (HAVE_DOS_BASED_FILE_SYSTEM)
1060 /* Convert all backslashes to slashes. */
1061 for (from = path; *from; from++)
1062 if (*from == '\\') *from = '/';
1063
1064 /* Skip over leading drive letter if present. */
1065 if (ISALPHA (path[0]) && path[1] == ':')
1066 from = to = &path[2];
1067 else
1068 from = to = path;
1069 #else
1070 from = to = path;
1071 #endif
1072
1073 /* Remove redundant initial /s. */
1074 if (*from == '/')
1075 {
1076 absolute = 1;
1077 to++;
1078 from++;
1079 if (*from == '/')
1080 {
1081 if (*++from == '/')
1082 /* 3 or more initial /s are equivalent to 1 /. */
1083 while (*++from == '/');
1084 else
1085 /* On some hosts // differs from /; Posix allows this. */
1086 to++;
1087 }
1088 }
1089 base = to;
1090
1091 for (;;)
1092 {
1093 while (*from == '/')
1094 from++;
1095
1096 if (from[0] == '.' && from[1] == '/')
1097 from += 2;
1098 else if (from[0] == '.' && from[1] == '\0')
1099 goto done;
1100 else if (from[0] == '.' && from[1] == '.' && from[2] == '/')
1101 {
1102 if (base == to)
1103 {
1104 if (absolute)
1105 from += 3;
1106 else
1107 {
1108 *to++ = *from++;
1109 *to++ = *from++;
1110 *to++ = *from++;
1111 base = to;
1112 }
1113 }
1114 else
1115 {
1116 to -= 2;
1117 while (to > base && *to != '/') to--;
1118 if (*to == '/')
1119 to++;
1120 from += 3;
1121 }
1122 }
1123 else if (from[0] == '.' && from[1] == '.' && from[2] == '\0')
1124 {
1125 if (base == to)
1126 {
1127 if (!absolute)
1128 {
1129 *to++ = *from++;
1130 *to++ = *from++;
1131 }
1132 }
1133 else
1134 {
1135 to -= 2;
1136 while (to > base && *to != '/') to--;
1137 if (*to == '/')
1138 to++;
1139 }
1140 goto done;
1141 }
1142 else
1143 /* Copy this component and trailing /, if any. */
1144 while ((*to++ = *from++) != '/')
1145 {
1146 if (!to[-1])
1147 {
1148 to--;
1149 goto done;
1150 }
1151 }
1152
1153 }
1154
1155 done:
1156 /* Trim trailing slash */
1157 if (to[0] == '/' && (!absolute || to > path+1))
1158 to--;
1159
1160 /* Change the empty string to "." so that stat() on the result
1161 will always work. */
1162 if (to == path)
1163 *to++ = '.';
1164
1165 *to = '\0';
1166
1167 return;
1168 }