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