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
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
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.
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. */
29 #include "splay-tree.h"
32 # include <sys/mman.h>
33 # ifndef MMAP_THRESHOLD
34 # define MMAP_THRESHOLD 3 /* Minimum page count to mmap the file. */
37 #else /* No MMAP_FILE */
38 # undef MMAP_THRESHOLD
39 # define MMAP_THRESHOLD 0
46 /* Suppress warning about function macros used w/o arguments in traditional
47 C. It is unlikely that glibc's strcmp macro helps this file at all. */
50 static struct file_name_map
*read_name_map
51 PARAMS ((cpp_reader
*, const char *));
52 static char *read_filename_string
PARAMS ((int, FILE *));
53 static char *remap_filename
PARAMS ((cpp_reader
*, char *,
54 struct file_name_list
*));
55 static struct file_name_list
*actual_directory
56 PARAMS ((cpp_reader
*, const char *));
57 static struct include_file
*find_include_file
58 PARAMS ((cpp_reader
*, const char *,
59 struct file_name_list
*));
60 static struct include_file
*open_include_file
61 PARAMS ((cpp_reader
*, const char *));
62 static int read_include_file
PARAMS ((cpp_reader
*, struct include_file
*));
63 static ssize_t read_with_read
PARAMS ((cpp_buffer
*, int, ssize_t
));
64 static ssize_t read_file
PARAMS ((cpp_buffer
*, int, ssize_t
));
66 static void destroy_include_file_node
PARAMS ((splay_tree_value
));
67 static int close_cached_fd
PARAMS ((splay_tree_node
, void *));
68 static int report_missing_guard
PARAMS ((splay_tree_node
, void *));
71 static void hack_vms_include_specification
PARAMS ((char *));
74 #ifndef INCLUDE_LEN_FUDGE
75 #define INCLUDE_LEN_FUDGE 0
78 /* We use a splay tree to store information about all the include
79 files seen in this compilation. The key of each tree node is the
80 physical path to the file. The value is 0 if the file does not
81 exist, or a struct include_file pointer. */
84 destroy_include_file_node (v
)
87 struct include_file
*f
= (struct include_file
*)v
;
97 close_cached_fd (n
, dummy
)
99 void *dummy ATTRIBUTE_UNUSED
;
101 struct include_file
*f
= (struct include_file
*)n
->value
;
102 if (f
&& f
->fd
!= -1)
111 _cpp_init_includes (pfile
)
114 pfile
->all_include_files
115 = splay_tree_new ((splay_tree_compare_fn
) strcmp
,
116 (splay_tree_delete_key_fn
) free
,
117 destroy_include_file_node
);
121 _cpp_cleanup_includes (pfile
)
124 splay_tree_delete (pfile
->all_include_files
);
127 /* Given a filename, look it up and possibly open it. If the file
128 does not exist, return NULL. If the file does exist but doesn't
129 need to be reread, return an include_file entry with fd == -1.
130 If it needs to be (re)read, return an include_file entry with
131 fd a file descriptor open on the file. */
133 static struct include_file
*
134 open_include_file (pfile
, filename
)
136 const char *filename
;
139 struct include_file
*file
= 0;
142 nd
= splay_tree_lookup (pfile
->all_include_files
,
143 (splay_tree_key
) filename
);
150 file
= (struct include_file
*)nd
->value
;
152 if (DO_NOT_REREAD (file
))
162 /* File descriptors are cached for files that might be reread. */
165 lseek (file
->fd
, 0, SEEK_SET
);
170 /* We used to open files in nonblocking mode, but that caused more
171 problems than it solved. Do take care not to acquire a
172 controlling terminal by mistake (this can't happen on sane
173 systems, but paranoia is a virtue).
175 Use the three-argument form of open even though we aren't
176 specifying O_CREAT, to defend against broken system headers.
178 O_BINARY tells some runtime libraries (notably DJGPP) not to do
179 newline translation; we can handle DOS line breaks just fine
182 Special case: the empty string is translated to stdin. */
185 if (filename
[0] == '\0')
188 fd
= open (filename
, O_RDONLY
|O_NOCTTY
|O_BINARY
, 0666);
195 cpp_error (pfile
, "included file \"%s\" exists but is not readable",
208 /* Too many files open. Close all cached file descriptors and
210 splay_tree_foreach (pfile
->all_include_files
, close_cached_fd
, 0);
214 /* Nonexistent or inaccessible file. Create a negative node for it. */
218 "node for '%s' exists, open failed, error '%s', value %lx\n",
219 filename
, strerror (errno
), nd
->value
);
220 destroy_include_file_node (nd
->value
);
222 splay_tree_insert (pfile
->all_include_files
,
223 (splay_tree_key
) xstrdup (filename
), 0);
227 /* If we haven't seen this file before, create a positive node for it. */
230 file
= xnew (struct include_file
);
232 file
->include_count
= 0;
235 file
->name
= xstrdup (filename
);
236 splay_tree_insert (pfile
->all_include_files
,
237 (splay_tree_key
) file
->name
,
238 (splay_tree_value
) file
);
242 file
->date
= (time_t) -1;
246 /* Return 1 if the file named by FNAME has been included before in
247 any context, 0 otherwise. */
249 cpp_included (pfile
, fname
)
253 struct file_name_list
*path
;
259 /* Just look it up. */
260 nd
= splay_tree_lookup (pfile
->all_include_files
, (splay_tree_key
) fname
);
261 return (nd
&& nd
->value
);
264 /* Search directory path for the file. */
265 name
= (char *) alloca (strlen (fname
) + pfile
->max_include_len
266 + 2 + INCLUDE_LEN_FUDGE
);
267 for (path
= CPP_OPTION (pfile
, quote_include
); path
; path
= path
->next
)
269 memcpy (name
, path
->name
, path
->nlen
);
270 name
[path
->nlen
] = '/';
271 strcpy (&name
[path
->nlen
+1], fname
);
272 _cpp_simplify_pathname (name
);
273 if (CPP_OPTION (pfile
, remap
))
274 name
= remap_filename (pfile
, name
, path
);
276 nd
= splay_tree_lookup (pfile
->all_include_files
, (splay_tree_key
) name
);
283 /* Search for include file FNAME in the include chain starting at
284 SEARCH_START. Return 0 if there is no such file (or it's un-openable),
285 otherwise an include_file structure, possibly with a file descriptor
288 static struct include_file
*
289 find_include_file (pfile
, fname
, search_start
)
292 struct file_name_list
*search_start
;
294 struct file_name_list
*path
;
296 struct include_file
*file
;
299 return open_include_file (pfile
, fname
);
301 /* Search directory path for the file. */
302 name
= (char *) alloca (strlen (fname
) + pfile
->max_include_len
303 + 2 + INCLUDE_LEN_FUDGE
);
304 for (path
= search_start
; path
; path
= path
->next
)
306 memcpy (name
, path
->name
, path
->nlen
);
307 name
[path
->nlen
] = '/';
308 strcpy (&name
[path
->nlen
+1], fname
);
309 _cpp_simplify_pathname (name
);
310 if (CPP_OPTION (pfile
, remap
))
311 name
= remap_filename (pfile
, name
, path
);
313 file
= open_include_file (pfile
, name
);
316 file
->sysp
= path
->sysp
;
317 file
->foundhere
= path
;
324 /* #line uses this to save artificial file names. We have to try
325 opening the file because an all_include_files entry is always
326 either + or -, there's no 'I don't know' value. */
328 _cpp_fake_include (pfile
, fname
)
333 struct include_file
*file
;
336 file
= find_include_file (pfile
, fname
, CPP_OPTION (pfile
, quote_include
));
340 name
= xstrdup (fname
);
341 _cpp_simplify_pathname (name
);
343 /* We cannot just blindly insert a node, because there's still the
344 chance that the node already exists but isn't on the search path. */
345 nd
= splay_tree_lookup (pfile
->all_include_files
, (splay_tree_key
) name
);
349 return (const char *) nd
->key
;
352 splay_tree_insert (pfile
->all_include_files
, (splay_tree_key
) name
, 0);
353 return (const char *)name
;
356 /* Not everyone who wants to set system-header-ness on a buffer can
357 see the details of struct include_file. This is an exported interface
358 because fix-header needs it. */
360 cpp_make_system_header (pfile
, pbuf
, flag
)
365 if (flag
< 0 || flag
> 2)
366 cpp_ice (pfile
, "cpp_make_system_header: bad flag %d\n", flag
);
368 cpp_ice (pfile
, "cpp_make_system_header called on non-file buffer");
370 pbuf
->inc
->sysp
= flag
;
374 cpp_syshdr_flags (pfile
, pbuf
)
375 cpp_reader
*pfile ATTRIBUTE_UNUSED
;
378 #ifndef NO_IMPLICIT_EXTERN_C
379 if (CPP_OPTION (pfile
, cplusplus
) && pbuf
->inc
->sysp
== 2)
387 /* Report on all files that might benefit from a multiple include guard.
390 _cpp_report_missing_guards (pfile
)
394 splay_tree_foreach (pfile
->all_include_files
, report_missing_guard
,
399 report_missing_guard (n
, b
)
403 struct include_file
*f
= (struct include_file
*) n
->value
;
404 int *bannerp
= (int *)b
;
406 if (f
&& f
->cmacro
== 0 && f
->include_count
== 1)
410 fputs (_("Multiple include guards may be useful for:\n"), stderr
);
413 fputs (f
->name
, stderr
);
419 #define PRINT_THIS_DEP(p, b) (CPP_PRINT_DEPS(p) > (b||p->system_include_depth))
421 _cpp_execute_include (pfile
, f
, len
, no_reinclude
, search_start
, angle_brackets
)
426 struct file_name_list
*search_start
;
429 struct include_file
*inc
;
435 search_start
= CPP_OPTION (pfile
, bracket_include
);
436 else if (CPP_OPTION (pfile
, ignore_srcdir
))
437 search_start
= CPP_OPTION (pfile
, quote_include
);
439 search_start
= CPP_BUFFER (pfile
)->actual_dir
;
444 cpp_error (pfile
, "No include path in which to find %s", f
);
448 fname
= alloca (len
+ 1);
449 memcpy (fname
, f
, len
);
452 inc
= find_include_file (pfile
, fname
, search_start
);
459 /* For -M, add the file to the dependencies on its first inclusion. */
460 if (!inc
->include_count
&& PRINT_THIS_DEP (pfile
, angle_brackets
))
461 deps_add_dep (pfile
->deps
, inc
->name
);
462 inc
->include_count
++;
464 /* Handle -H option. */
465 if (CPP_OPTION (pfile
, print_include_names
))
467 cpp_buffer
*fp
= CPP_BUFFER (pfile
);
468 while ((fp
= CPP_PREV_BUFFER (fp
)) != NULL
)
470 fprintf (stderr
, " %s\n", inc
->name
);
473 /* Actually process the file. */
475 inc
->cmacro
= NEVER_REREAD
;
477 if (read_include_file (pfile
, inc
))
480 pfile
->system_include_depth
++;
485 if (CPP_OPTION (pfile
, print_deps_missing_files
)
486 && PRINT_THIS_DEP (pfile
, angle_brackets
))
489 deps_add_dep (pfile
->deps
, fname
);
493 struct file_name_list
*ptr
;
494 /* If requested as a system header, assume it belongs in
495 the first system header directory. */
496 if (CPP_OPTION (pfile
, bracket_include
))
497 ptr
= CPP_OPTION (pfile
, bracket_include
);
499 ptr
= CPP_OPTION (pfile
, quote_include
);
501 p
= (char *) alloca (strlen (ptr
->name
)
502 + strlen (fname
) + 2);
503 if (*ptr
->name
!= '\0')
505 strcpy (p
, ptr
->name
);
509 _cpp_simplify_pathname (p
);
510 deps_add_dep (pfile
->deps
, p
);
513 /* If -M was specified, and this header file won't be added to
514 the dependency list, then don't count this as an error,
515 because we can still produce correct output. Otherwise, we
516 can't produce correct output, because there may be
517 dependencies we need inside the missing file, and we don't
518 know what directory this missing file exists in. */
519 else if (CPP_PRINT_DEPS (pfile
)
520 && ! PRINT_THIS_DEP (pfile
, angle_brackets
))
521 cpp_warning (pfile
, "No include path in which to find %s", fname
);
523 cpp_error_from_errno (pfile
, fname
);
526 /* Locate file F, and determine whether it is newer than PFILE. Return -1,
527 if F cannot be located or dated, 1, if it is newer and 0 if older. */
530 _cpp_compare_file_date (pfile
, f
, len
, angle_brackets
)
537 struct file_name_list
*search_start
;
538 struct include_file
*inc
;
539 struct include_file
*current_include
= CPP_BUFFER (pfile
)->inc
;
542 search_start
= CPP_OPTION (pfile
, bracket_include
);
543 else if (CPP_OPTION (pfile
, ignore_srcdir
))
544 search_start
= CPP_OPTION (pfile
, quote_include
);
546 search_start
= CPP_BUFFER (pfile
)->actual_dir
;
548 fname
= alloca (len
+ 1);
549 memcpy (fname
, f
, len
);
551 inc
= find_include_file (pfile
, fname
, search_start
);
559 if (fstat (inc
->fd
, &source
) < 0)
565 inc
->date
= source
.st_mtime
;
569 if (inc
->date
== (time_t)-1 || current_include
->date
== (time_t)-1)
571 return inc
->date
> current_include
->date
;
575 /* Push an input buffer and load it up with the contents of FNAME.
576 If FNAME is "" or NULL, read standard input. */
578 cpp_read_file (pfile
, fname
)
582 struct include_file
*f
;
587 f
= open_include_file (pfile
, fname
);
591 cpp_error_from_errno (pfile
, fname
);
595 return read_include_file (pfile
, f
);
598 /* Read the file referenced by INC into a new buffer on PFILE's stack.
599 Return 1 if successful, 0 if not. */
602 read_include_file (pfile
, inc
)
604 struct include_file
*inc
;
611 fp
= cpp_push_buffer (pfile
, NULL
, 0);
616 if (fd
< 0 || fstat (fd
, &st
) < 0)
619 inc
->date
= st
.st_mtime
;
621 /* If fd points to a plain file, we might be able to mmap it; we can
622 definitely allocate the buffer all at once. If fd is a pipe or
623 terminal, we can't do either. If fd is something weird, like a
624 block device or a directory, we don't want to read it at all.
626 Unfortunately, different systems use different st.st_mode values
627 for pipes: some have S_ISFIFO, some S_ISSOCK, some are buggy and
628 zero the entire struct stat except a couple fields. Hence we don't
629 even try to figure out what something is, except for plain files,
630 directories, and block devices. */
632 if (S_ISREG (st
.st_mode
))
636 /* off_t might have a wider range than ssize_t - in other words,
637 the max size of a file might be bigger than the address
638 space. We can't handle a file that large. (Anyone with
639 a single source file bigger than 2GB needs to rethink
640 their coding style.) Some systems (e.g. AIX 4.1) define
641 SSIZE_MAX to be much smaller than the actual range of the
642 type. Use INTTYPE_MAXIMUM unconditionally to ensure this
644 if (st
.st_size
> INTTYPE_MAXIMUM (ssize_t
))
646 cpp_error (pfile
, "%s is too large", inc
->name
);
649 st_size
= st
.st_size
;
650 length
= read_file (fp
, fd
, st_size
);
653 if (length
< st_size
)
654 cpp_warning (pfile
, "%s is shorter than expected\n", inc
->name
);
656 else if (S_ISBLK (st
.st_mode
))
658 cpp_error (pfile
, "%s is a block device", inc
->name
);
661 else if (S_ISDIR (st
.st_mode
))
663 cpp_error (pfile
, "%s is a directory", inc
->name
);
668 /* 8 kilobytes is a sensible starting size. It ought to be
669 bigger than the kernel pipe buffer, and it's definitely
670 bigger than the majority of C source files. */
671 length
= read_with_read (fp
, fd
, 8 * 1024);
676 /* These must be set before prescan. */
678 fp
->nominal_fname
= inc
->name
;
679 pfile
->include_depth
++;
682 inc
->cmacro
= NEVER_REREAD
;
684 fp
->rlimit
= fp
->buf
+ length
;
687 fp
->line_base
= fp
->buf
;
689 /* The ->actual_dir field is only used when ignore_srcdir is not in effect;
691 if (!CPP_OPTION (pfile
, ignore_srcdir
))
692 fp
->actual_dir
= actual_directory (pfile
, inc
->name
);
694 pfile
->input_stack_listing_current
= 0;
695 if (pfile
->cb
.enter_file
)
696 (*pfile
->cb
.enter_file
) (pfile
);
700 cpp_error_from_errno (pfile
, inc
->name
);
701 /* Do not try to read this file again. */
705 inc
->cmacro
= NEVER_REREAD
;
707 cpp_pop_buffer (pfile
);
713 read_file (fp
, fd
, size
)
718 static int pagesize
= -1;
724 pagesize
= getpagesize ();
727 if (size
/ pagesize
>= MMAP_THRESHOLD
)
730 = (const U_CHAR
*) mmap (0, size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
731 if (result
!= (const U_CHAR
*)-1)
738 /* If mmap fails, try read. If there's really a problem, read will
742 return read_with_read (fp
, fd
, size
);
746 read_with_read (fp
, fd
, size
)
751 ssize_t offset
, count
;
754 buf
= (U_CHAR
*) xmalloc (size
);
756 while ((count
= read (fd
, buf
+ offset
, size
- offset
)) > 0)
760 buf
= xrealloc (buf
, (size
*= 2));
774 buf
= xrealloc (buf
, offset
);
780 /* Do appropriate cleanup when a file buffer is popped off the input
783 _cpp_pop_file_buffer (pfile
, buf
)
787 struct include_file
*inc
= buf
->inc
;
789 if (pfile
->system_include_depth
)
790 pfile
->system_include_depth
--;
791 if (pfile
->include_depth
)
792 pfile
->include_depth
--;
793 if (pfile
->potential_control_macro
)
795 if (inc
->cmacro
!= NEVER_REREAD
)
796 inc
->cmacro
= pfile
->potential_control_macro
;
797 pfile
->potential_control_macro
= 0;
799 pfile
->input_stack_listing_current
= 0;
801 /* Discard file buffer. XXX Would be better to cache these instead
802 of the file descriptors. */
803 #ifdef HAVE_MMAP_FILE
805 munmap ((caddr_t
) buf
->buf
, buf
->rlimit
- buf
->buf
);
808 free ((PTR
) buf
->buf
);
810 /* If the file will not be included again, close it. */
811 if (DO_NOT_REREAD (inc
))
818 /* The file_name_map structure holds a mapping of file names for a
819 particular directory. This mapping is read from the file named
820 FILE_NAME_MAP_FILE in that directory. Such a file can be used to
821 map filenames on a file system with severe filename restrictions,
822 such as DOS. The format of the file name map file is just a series
823 of lines with two tokens on each line. The first token is the name
824 to map, and the second token is the actual name to use. */
828 struct file_name_map
*map_next
;
833 #define FILE_NAME_MAP_FILE "header.gcc"
835 /* Read a space delimited string of unlimited length from a stdio
839 read_filename_string (ch
, f
)
847 set
= alloc
= xmalloc (len
+ 1);
851 while ((ch
= getc (f
)) != EOF
&& ! is_space(ch
))
853 if (set
- alloc
== len
)
856 alloc
= xrealloc (alloc
, len
+ 1);
857 set
= alloc
+ len
/ 2;
867 /* This structure holds a linked list of file name maps, one per directory. */
869 struct file_name_map_list
871 struct file_name_map_list
*map_list_next
;
873 struct file_name_map
*map_list_map
;
876 /* Read the file name map file for DIRNAME. */
878 static struct file_name_map
*
879 read_name_map (pfile
, dirname
)
883 register struct file_name_map_list
*map_list_ptr
;
887 for (map_list_ptr
= CPP_OPTION (pfile
, map_list
); map_list_ptr
;
888 map_list_ptr
= map_list_ptr
->map_list_next
)
889 if (! strcmp (map_list_ptr
->map_list_name
, dirname
))
890 return map_list_ptr
->map_list_map
;
892 map_list_ptr
= ((struct file_name_map_list
*)
893 xmalloc (sizeof (struct file_name_map_list
)));
894 map_list_ptr
->map_list_name
= xstrdup (dirname
);
895 map_list_ptr
->map_list_map
= NULL
;
897 name
= (char *) alloca (strlen (dirname
) + strlen (FILE_NAME_MAP_FILE
) + 2);
898 strcpy (name
, dirname
);
901 strcat (name
, FILE_NAME_MAP_FILE
);
902 f
= fopen (name
, "r");
904 map_list_ptr
->map_list_map
= (struct file_name_map
*)-1;
908 int dirlen
= strlen (dirname
);
910 while ((ch
= getc (f
)) != EOF
)
913 struct file_name_map
*ptr
;
917 from
= read_filename_string (ch
, f
);
918 while ((ch
= getc (f
)) != EOF
&& is_hspace(ch
))
920 to
= read_filename_string (ch
, f
);
922 ptr
= ((struct file_name_map
*)
923 xmalloc (sizeof (struct file_name_map
)));
924 ptr
->map_from
= from
;
926 /* Make the real filename absolute. */
931 ptr
->map_to
= xmalloc (dirlen
+ strlen (to
) + 2);
932 strcpy (ptr
->map_to
, dirname
);
933 ptr
->map_to
[dirlen
] = '/';
934 strcpy (ptr
->map_to
+ dirlen
+ 1, to
);
938 ptr
->map_next
= map_list_ptr
->map_list_map
;
939 map_list_ptr
->map_list_map
= ptr
;
941 while ((ch
= getc (f
)) != '\n')
948 map_list_ptr
->map_list_next
= CPP_OPTION (pfile
, map_list
);
949 CPP_OPTION (pfile
, map_list
) = map_list_ptr
;
951 return map_list_ptr
->map_list_map
;
954 /* Remap NAME based on the file_name_map (if any) for LOC. */
957 remap_filename (pfile
, name
, loc
)
960 struct file_name_list
*loc
;
962 struct file_name_map
*map
;
963 const char *from
, *p
, *dir
;
966 loc
->name_map
= read_name_map (pfile
,
970 if (loc
->name_map
== (struct file_name_map
*)-1)
973 from
= name
+ strlen (loc
->name
) + 1;
975 for (map
= loc
->name_map
; map
; map
= map
->map_next
)
976 if (!strcmp (map
->map_from
, from
))
979 /* Try to find a mapping file for the particular directory we are
980 looking in. Thus #include <sys/types.h> will look up sys/types.h
981 in /usr/include/header.gcc and look up types.h in
982 /usr/include/sys/header.gcc. */
983 p
= strrchr (name
, '/');
987 && strlen (loc
->name
) == (size_t) (p
- name
)
988 && !strncmp (loc
->name
, name
, p
- name
))
989 /* FILENAME is in SEARCHPTR, which we've already checked. */
999 char * newdir
= (char *) alloca (p
- name
+ 1);
1000 memcpy (newdir
, name
, p
- name
);
1001 newdir
[p
- name
] = '\0';
1006 for (map
= read_name_map (pfile
, dir
); map
; map
= map
->map_next
)
1007 if (! strcmp (map
->map_from
, name
))
1013 /* Given a path FNAME, extract the directory component and place it
1014 onto the actual_dirs list. Return a pointer to the allocated
1015 file_name_list structure. These structures are used to implement
1016 current-directory "" include searching. */
1018 static struct file_name_list
*
1019 actual_directory (pfile
, fname
)
1023 char *last_slash
, *dir
;
1025 struct file_name_list
*x
;
1027 dir
= xstrdup (fname
);
1028 last_slash
= strrchr (dir
, '/');
1031 if (last_slash
== dir
)
1034 last_slash
[1] = '\0';
1038 dlen
= last_slash
- dir
;
1049 if (dlen
> pfile
->max_include_len
)
1050 pfile
->max_include_len
= dlen
;
1052 for (x
= pfile
->actual_dirs
; x
; x
= x
->alloc
)
1053 if (!strcmp (x
->name
, dir
))
1059 /* Not found, make a new one. */
1060 x
= (struct file_name_list
*) xmalloc (sizeof (struct file_name_list
));
1063 x
->next
= CPP_OPTION (pfile
, quote_include
);
1064 x
->alloc
= pfile
->actual_dirs
;
1065 x
->sysp
= CPP_BUFFER (pfile
)->inc
->sysp
;
1068 pfile
->actual_dirs
= x
;
1072 /* Simplify a path name in place, deleting redundant components. This
1073 reduces OS overhead and guarantees that equivalent paths compare
1074 the same (modulo symlinks).
1077 foo/bar/../quux foo/quux
1081 //quux //quux (POSIX allows leading // as a namespace escape)
1083 Guarantees no trailing slashes. All transforms reduce the length
1087 _cpp_simplify_pathname (path
)
1094 #if defined (HAVE_DOS_BASED_FILE_SYSTEM)
1095 /* Convert all backslashes to slashes. */
1096 for (from
= path
; *from
; from
++)
1097 if (*from
== '\\') *from
= '/';
1099 /* Skip over leading drive letter if present. */
1100 if (ISALPHA (path
[0]) && path
[1] == ':')
1101 from
= to
= &path
[2];
1108 /* Remove redundant initial /s. */
1117 /* 3 or more initial /s are equivalent to 1 /. */
1118 while (*++from
== '/');
1120 /* On some hosts // differs from /; Posix allows this. */
1128 while (*from
== '/')
1131 if (from
[0] == '.' && from
[1] == '/')
1133 else if (from
[0] == '.' && from
[1] == '\0')
1135 else if (from
[0] == '.' && from
[1] == '.' && from
[2] == '/')
1152 while (to
> base
&& *to
!= '/') to
--;
1158 else if (from
[0] == '.' && from
[1] == '.' && from
[2] == '\0')
1171 while (to
> base
&& *to
!= '/') to
--;
1178 /* Copy this component and trailing /, if any. */
1179 while ((*to
++ = *from
++) != '/')
1191 /* Trim trailing slash */
1192 if (to
[0] == '/' && (!absolute
|| to
> path
+1))
1195 /* Change the empty string to "." so that stat() on the result
1196 will always work. */
1205 /* It is not clear when this should be used if at all, so I've
1206 disabled it until someone who understands VMS can look at it. */
1209 /* Under VMS we need to fix up the "include" specification filename.
1211 Rules for possible conversions
1213 fullname tried paths
1216 ./dir/name [.dir]name
1218 /name [000000]name, name
1219 dir/name dir:[000000]name, dir:name, dir/name
1220 dir1/dir2/name dir1:[dir2]name, dir1:[000000.dir2]name
1221 path:/name path:[000000]name, path:name
1222 path:/dir/name path:[000000.dir]name, path:[dir]name
1223 path:dir/name path:[dir]name
1224 [path]:[dir]name [path.dir]name
1225 path/[dir]name [path.dir]name
1227 The path:/name input is constructed when expanding <> includes. */
1231 hack_vms_include_specification (fullname
)
1234 register char *basename
, *unixname
, *local_ptr
, *first_slash
;
1235 int f
, check_filename_before_returning
, must_revert
;
1238 check_filename_before_returning
= 0;
1240 /* See if we can find a 1st slash. If not, there's no path information. */
1241 first_slash
= strchr (fullname
, '/');
1242 if (first_slash
== 0)
1243 return 0; /* Nothing to do!!! */
1245 /* construct device spec if none given. */
1247 if (strchr (fullname
, ':') == 0)
1250 /* If fullname has a slash, take it as device spec. */
1252 if (first_slash
== fullname
)
1254 first_slash
= strchr (fullname
+ 1, '/'); /* 2nd slash ? */
1256 *first_slash
= ':'; /* make device spec */
1257 for (basename
= fullname
; *basename
!= 0; basename
++)
1258 *basename
= *(basename
+1); /* remove leading slash */
1260 else if ((first_slash
[-1] != '.') /* keep ':/', './' */
1261 && (first_slash
[-1] != ':')
1262 && (first_slash
[-1] != ']')) /* or a vms path */
1266 else if ((first_slash
[1] == '[') /* skip './' in './[dir' */
1267 && (first_slash
[-1] == '.'))
1271 /* Get part after first ':' (basename[-1] == ':')
1272 or last '/' (basename[-1] == '/'). */
1274 basename
= base_name (fullname
);
1276 local_ptr
= Local
; /* initialize */
1278 /* We are trying to do a number of things here. First of all, we are
1279 trying to hammer the filenames into a standard format, such that later
1280 processing can handle them.
1282 If the file name contains something like [dir.], then it recognizes this
1283 as a root, and strips the ".]". Later processing will add whatever is
1284 needed to get things working properly.
1286 If no device is specified, then the first directory name is taken to be
1287 a device name (or a rooted logical). */
1289 /* Point to the UNIX filename part (which needs to be fixed!)
1290 but skip vms path information.
1291 [basename != fullname since first_slash != 0]. */
1293 if ((basename
[-1] == ':') /* vms path spec. */
1294 || (basename
[-1] == ']')
1295 || (basename
[-1] == '>'))
1296 unixname
= basename
;
1298 unixname
= fullname
;
1300 if (*unixname
== '/')
1303 /* If the directory spec is not rooted, we can just copy
1304 the UNIX filename part and we are done. */
1306 if (((basename
- fullname
) > 1)
1307 && ( (basename
[-1] == ']')
1308 || (basename
[-1] == '>')))
1310 if (basename
[-2] != '.')
1313 /* The VMS part ends in a `]', and the preceding character is not a `.'.
1314 -> PATH]:/name (basename = '/name', unixname = 'name')
1315 We strip the `]', and then splice the two parts of the name in the
1316 usual way. Given the default locations for include files,
1317 we will only use this code if the user specifies alternate locations
1318 with the /include (-I) switch on the command line. */
1320 basename
-= 1; /* Strip "]" */
1321 unixname
--; /* backspace */
1326 /* The VMS part has a ".]" at the end, and this will not do. Later
1327 processing will add a second directory spec, and this would be a syntax
1328 error. Thus we strip the ".]", and thus merge the directory specs.
1329 We also backspace unixname, so that it points to a '/'. This inhibits the
1330 generation of the 000000 root directory spec (which does not belong here
1333 basename
-= 2; /* Strip ".]" */
1334 unixname
--; /* backspace */
1342 /* We drop in here if there is no VMS style directory specification yet.
1343 If there is no device specification either, we make the first dir a
1344 device and try that. If we do not do this, then we will be essentially
1345 searching the users default directory (as if they did a #include "asdf.h").
1347 Then all we need to do is to push a '[' into the output string. Later
1348 processing will fill this in, and close the bracket. */
1350 if ((unixname
!= fullname
) /* vms path spec found. */
1351 && (basename
[-1] != ':'))
1352 *local_ptr
++ = ':'; /* dev not in spec. take first dir */
1354 *local_ptr
++ = '['; /* Open the directory specification */
1357 if (unixname
== fullname
) /* no vms dir spec. */
1360 if ((first_slash
!= 0) /* unix dir spec. */
1361 && (*unixname
!= '/') /* not beginning with '/' */
1362 && (*unixname
!= '.')) /* or './' or '../' */
1363 *local_ptr
++ = '.'; /* dir is local ! */
1366 /* at this point we assume that we have the device spec, and (at least
1367 the opening "[" for a directory specification. We may have directories
1370 If there are no other slashes then the filename will be
1371 in the "root" directory. Otherwise, we need to add
1372 directory specifications. */
1374 if (strchr (unixname
, '/') == 0)
1376 /* if no directories specified yet and none are following. */
1377 if (local_ptr
[-1] == '[')
1379 /* Just add "000000]" as the directory string */
1380 strcpy (local_ptr
, "000000]");
1381 local_ptr
+= strlen (local_ptr
);
1382 check_filename_before_returning
= 1; /* we might need to fool with this later */
1388 /* As long as there are still subdirectories to add, do them. */
1389 while (strchr (unixname
, '/') != 0)
1391 /* If this token is "." we can ignore it
1392 if it's not at the beginning of a path. */
1393 if ((unixname
[0] == '.') && (unixname
[1] == '/'))
1395 /* remove it at beginning of path. */
1396 if ( ((unixname
== fullname
) /* no device spec */
1397 && (fullname
+2 != basename
)) /* starts with ./ */
1399 || ((basename
[-1] == ':') /* device spec */
1400 && (unixname
-1 == basename
))) /* and ./ afterwards */
1401 *local_ptr
++ = '.'; /* make '[.' start of path. */
1406 /* Add a subdirectory spec. Do not duplicate "." */
1407 if ( local_ptr
[-1] != '.'
1408 && local_ptr
[-1] != '['
1409 && local_ptr
[-1] != '<')
1412 /* If this is ".." then the spec becomes "-" */
1413 if ( (unixname
[0] == '.')
1414 && (unixname
[1] == '.')
1415 && (unixname
[2] == '/'))
1417 /* Add "-" and skip the ".." */
1418 if ((local_ptr
[-1] == '.')
1419 && (local_ptr
[-2] == '['))
1420 local_ptr
--; /* prevent [.- */
1426 /* Copy the subdirectory */
1427 while (*unixname
!= '/')
1428 *local_ptr
++= *unixname
++;
1430 unixname
++; /* Skip the "/" */
1433 /* Close the directory specification */
1434 if (local_ptr
[-1] == '.') /* no trailing periods */
1437 if (local_ptr
[-1] == '[') /* no dir needed */
1443 /* Now add the filename. */
1446 *local_ptr
++ = *unixname
++;
1449 /* Now append it to the original VMS spec. */
1451 strcpy ((must_revert
==1)?fullname
:basename
, Local
);
1453 /* If we put a [000000] in the filename, try to open it first. If this fails,
1454 remove the [000000], and return that name. This provides flexibility
1455 to the user in that they can use both rooted and non-rooted logical names
1456 to point to the location of the file. */
1458 if (check_filename_before_returning
)
1460 f
= open (fullname
, O_RDONLY
|O_NONBLOCK
);
1463 /* The file name is OK as it is, so return it as is. */
1468 /* The filename did not work. Try to remove the [000000] from the name,
1471 basename
= strchr (fullname
, '[');
1472 local_ptr
= strchr (fullname
, ']') + 1;
1473 strcpy (basename
, local_ptr
); /* this gets rid of it */