OSDN Git Service

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