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 #ifndef INCLUDE_LEN_FUDGE
47 # define INCLUDE_LEN_FUDGE 0
50 /* If errno is inspected immediately after a system call fails, it will be
51 nonzero, and no error number will ever be zero. */
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. */
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
*lookup_include_file
77 PARAMS ((cpp_reader
*, const char *));
78 static int read_include_file
PARAMS ((cpp_reader
*, struct include_file
*));
79 static int stack_include_file
PARAMS ((cpp_reader
*, struct include_file
*));
80 static void purge_cache
PARAMS ((struct include_file
*));
81 static void destroy_include_file_node
PARAMS ((splay_tree_value
));
82 static int report_missing_guard
PARAMS ((splay_tree_node
, void *));
85 static void hack_vms_include_specification
PARAMS ((char *));
88 /* We use a splay tree to store information about all the include
89 files seen in this compilation. The key of each tree node is the
90 physical path to the file. The value is 0 if the file does not
91 exist, or a struct include_file pointer. */
94 destroy_include_file_node (v
)
97 struct include_file
*f
= (struct include_file
*)v
;
106 _cpp_init_includes (pfile
)
109 pfile
->all_include_files
110 = splay_tree_new ((splay_tree_compare_fn
) strcmp
,
111 (splay_tree_delete_key_fn
) free
,
112 destroy_include_file_node
);
116 _cpp_cleanup_includes (pfile
)
119 splay_tree_delete (pfile
->all_include_files
);
122 /* Given a file name, look it up in the cache; if there is no entry,
123 create one. Returns 0 if the file doesn't exist or is
124 inaccessible, otherwise the cache entry. */
126 static struct include_file
*
127 lookup_include_file (pfile
, filename
)
129 const char *filename
;
132 struct include_file
*file
= 0;
136 nd
= splay_tree_lookup (pfile
->all_include_files
, (splay_tree_key
) filename
);
139 return (struct include_file
*)nd
->value
;
141 /* We used to open files in nonblocking mode, but that caused more
142 problems than it solved. Do take care not to acquire a
143 controlling terminal by mistake (this can't happen on sane
144 systems, but paranoia is a virtue).
146 Use the three-argument form of open even though we aren't
147 specifying O_CREAT, to defend against broken system headers.
149 O_BINARY tells some runtime libraries (notably DJGPP) not to do
150 newline translation; we can handle DOS line breaks just fine
153 Special case: the empty string is translated to stdin. */
155 if (filename
[0] == '\0')
158 fd
= open (filename
, O_RDONLY
|O_NOCTTY
|O_BINARY
, 0666);
162 if (fstat (fd
, &st
) < 0)
165 file
= xcnew (struct include_file
);
166 file
->name
= xstrdup (filename
);
170 /* If the file is plain and zero length, mark it never-reread now. */
171 if (S_ISREG (st
.st_mode
) && st
.st_size
== 0)
172 file
->cmacro
= NEVER_REREAD
;
174 splay_tree_insert (pfile
->all_include_files
,
175 (splay_tree_key
) file
->name
, (splay_tree_value
) file
);
180 /* Don't issue an error message if the file doesn't exist. */
181 if (errno
!= ENOENT
&& errno
!= ENOTDIR
)
182 cpp_error_from_errno (pfile
, filename
);
184 /* Create a negative node for this path. */
185 splay_tree_insert (pfile
->all_include_files
,
186 (splay_tree_key
) xstrdup (filename
), 0);
190 /* Place the file referenced by INC into a new buffer on PFILE's stack.
191 Return 1 if successful, 0 if not. */
194 stack_include_file (pfile
, inc
)
196 struct include_file
*inc
;
200 if (DO_NOT_REREAD (inc
))
203 if (inc
->buffer
== NULL
)
204 if (read_include_file (pfile
, inc
) == 0)
207 fp
= cpp_push_buffer (pfile
, NULL
, 0);
212 fp
->nominal_fname
= inc
->name
;
213 fp
->buf
= inc
->buffer
;
214 fp
->rlimit
= fp
->buf
+ inc
->st
.st_size
;
217 fp
->line_base
= fp
->buf
;
219 /* The ->actual_dir field is only used when ignore_srcdir is not in effect;
221 if (!CPP_OPTION (pfile
, ignore_srcdir
))
222 fp
->actual_dir
= actual_directory (pfile
, inc
->name
);
225 pfile
->include_depth
++;
226 pfile
->input_stack_listing_current
= 0;
227 if (pfile
->cb
.enter_file
)
228 (*pfile
->cb
.enter_file
) (pfile
);
232 /* Read the file referenced by INC into the file cache.
234 If fd points to a plain file, we might be able to mmap it; we can
235 definitely allocate the buffer all at once. If fd is a pipe or
236 terminal, we can't do either. If fd is something weird, like a
237 block device or a directory, we don't want to read it at all.
239 Unfortunately, different systems use different st.st_mode values
240 for pipes: some have S_ISFIFO, some S_ISSOCK, some are buggy and
241 zero the entire struct stat except a couple fields. Hence we don't
242 even try to figure out what something is, except for plain files,
243 directories, and block devices.
245 FIXME: Flush file cache and try again if we run out of memory. */
248 read_include_file (pfile
, inc
)
250 struct include_file
*inc
;
252 ssize_t size
, offset
, count
;
255 static int pagesize
= -1;
258 if (S_ISREG (inc
->st
.st_mode
))
260 /* off_t might have a wider range than ssize_t - in other words,
261 the max size of a file might be bigger than the address
262 space. We can't handle a file that large. (Anyone with
263 a single source file bigger than 2GB needs to rethink
264 their coding style.) Some systems (e.g. AIX 4.1) define
265 SSIZE_MAX to be much smaller than the actual range of the
266 type. Use INTTYPE_MAXIMUM unconditionally to ensure this
268 if (inc
->st
.st_size
> INTTYPE_MAXIMUM (ssize_t
))
270 cpp_error (pfile
, "%s is too large", inc
->name
);
273 size
= inc
->st
.st_size
;
278 pagesize
= getpagesize ();
280 if (size
/ pagesize
>= MMAP_THRESHOLD
)
282 buf
= (U_CHAR
*) mmap (0, size
, PROT_READ
, MAP_PRIVATE
, inc
->fd
, 0);
283 if (buf
== (U_CHAR
*)-1)
290 buf
= (U_CHAR
*) xmalloc (size
);
292 while (offset
< size
)
294 count
= read (inc
->fd
, buf
+ offset
, size
- offset
);
299 cpp_warning (pfile
, "%s is shorter than expected", inc
->name
);
306 else if (S_ISBLK (inc
->st
.st_mode
))
308 cpp_error (pfile
, "%s is a block device", inc
->name
);
311 else if (S_ISDIR (inc
->st
.st_mode
))
313 cpp_error (pfile
, "%s is a directory", inc
->name
);
318 /* 8 kilobytes is a sensible starting size. It ought to be
319 bigger than the kernel pipe buffer, and it's definitely
320 bigger than the majority of C source files. */
323 buf
= (U_CHAR
*) xmalloc (size
);
325 while ((count
= read (inc
->fd
, buf
+ offset
, size
- offset
)) > 0)
329 buf
= xrealloc (buf
, (size
*= 2));
341 buf
= xrealloc (buf
, offset
);
342 inc
->st
.st_size
= offset
;
351 cpp_error_from_errno (pfile
, inc
->name
);
353 /* Do not try to read this file again. */
356 inc
->cmacro
= NEVER_REREAD
;
362 struct include_file
*inc
;
368 munmap ((PTR
) inc
->buffer
, inc
->st
.st_size
);
371 free ((PTR
) inc
->buffer
);
376 /* Return 1 if the file named by FNAME has been included before in
377 any context, 0 otherwise. */
379 cpp_included (pfile
, fname
)
383 struct file_name_list
*path
;
389 /* Just look it up. */
390 nd
= splay_tree_lookup (pfile
->all_include_files
, (splay_tree_key
) fname
);
391 return (nd
&& nd
->value
);
394 /* Search directory path for the file. */
395 name
= (char *) alloca (strlen (fname
) + pfile
->max_include_len
396 + 2 + INCLUDE_LEN_FUDGE
);
397 for (path
= CPP_OPTION (pfile
, quote_include
); path
; path
= path
->next
)
399 memcpy (name
, path
->name
, path
->nlen
);
400 name
[path
->nlen
] = '/';
401 strcpy (&name
[path
->nlen
+1], fname
);
402 _cpp_simplify_pathname (name
);
403 if (CPP_OPTION (pfile
, remap
))
404 name
= remap_filename (pfile
, name
, path
);
406 nd
= splay_tree_lookup (pfile
->all_include_files
, (splay_tree_key
) name
);
413 /* Search for include file FNAME in the include chain starting at
414 SEARCH_START. Return 0 if there is no such file (or it's un-openable),
415 otherwise an include_file structure. */
417 static struct include_file
*
418 find_include_file (pfile
, fname
, search_start
)
421 struct file_name_list
*search_start
;
423 struct file_name_list
*path
;
425 struct include_file
*file
;
428 return lookup_include_file (pfile
, fname
);
430 /* Search directory path for the file. */
431 name
= (char *) alloca (strlen (fname
) + pfile
->max_include_len
432 + 2 + INCLUDE_LEN_FUDGE
);
433 for (path
= search_start
; path
; path
= path
->next
)
435 memcpy (name
, path
->name
, path
->nlen
);
436 name
[path
->nlen
] = '/';
437 strcpy (&name
[path
->nlen
+1], fname
);
438 _cpp_simplify_pathname (name
);
439 if (CPP_OPTION (pfile
, remap
))
440 name
= remap_filename (pfile
, name
, path
);
442 file
= lookup_include_file (pfile
, name
);
445 file
->sysp
= path
->sysp
;
446 file
->foundhere
= path
;
453 /* #line uses this to save artificial file names. We have to stat the
454 file because an all_include_files entry is always either + or -,
455 there's no 'I don't know' value. */
457 _cpp_fake_include (pfile
, fname
)
462 struct include_file
*file
;
465 file
= find_include_file (pfile
, fname
, CPP_OPTION (pfile
, quote_include
));
476 name
= xstrdup (fname
);
477 _cpp_simplify_pathname (name
);
479 /* We cannot just blindly insert a node, because there's still the
480 chance that the node already exists but isn't on the search path. */
481 nd
= splay_tree_lookup (pfile
->all_include_files
, (splay_tree_key
) name
);
485 return (const char *) nd
->key
;
488 splay_tree_insert (pfile
->all_include_files
, (splay_tree_key
) name
, 0);
489 return (const char *)name
;
492 /* Not everyone who wants to set system-header-ness on a buffer can
493 see the details of struct include_file. This is an exported interface
494 because fix-header needs it. */
496 cpp_make_system_header (pfile
, pbuf
, flag
)
501 if (flag
< 0 || flag
> 2)
502 cpp_ice (pfile
, "cpp_make_system_header: bad flag %d\n", flag
);
504 cpp_ice (pfile
, "cpp_make_system_header called on non-file buffer");
506 pbuf
->inc
->sysp
= flag
;
510 cpp_syshdr_flags (pfile
, pbuf
)
511 cpp_reader
*pfile ATTRIBUTE_UNUSED
;
514 #ifndef NO_IMPLICIT_EXTERN_C
515 if (CPP_OPTION (pfile
, cplusplus
) && pbuf
->inc
->sysp
== 2)
523 /* Report on all files that might benefit from a multiple include guard.
526 _cpp_report_missing_guards (pfile
)
530 splay_tree_foreach (pfile
->all_include_files
, report_missing_guard
,
535 report_missing_guard (n
, b
)
539 struct include_file
*f
= (struct include_file
*) n
->value
;
540 int *bannerp
= (int *)b
;
542 if (f
&& f
->cmacro
== 0 && f
->include_count
== 1)
546 fputs (_("Multiple include guards may be useful for:\n"), stderr
);
549 fputs (f
->name
, stderr
);
555 #define PRINT_THIS_DEP(p, b) (CPP_PRINT_DEPS(p) > (b||p->system_include_depth))
557 _cpp_execute_include (pfile
, f
, len
, no_reinclude
, search_start
, angle_brackets
)
562 struct file_name_list
*search_start
;
565 struct include_file
*inc
;
571 search_start
= CPP_OPTION (pfile
, bracket_include
);
572 else if (CPP_OPTION (pfile
, ignore_srcdir
))
573 search_start
= CPP_OPTION (pfile
, quote_include
);
575 search_start
= CPP_BUFFER (pfile
)->actual_dir
;
580 cpp_error (pfile
, "No include path in which to find %s", f
);
584 fname
= alloca (len
+ 1);
585 memcpy (fname
, f
, len
);
588 inc
= find_include_file (pfile
, fname
, search_start
);
592 /* For -M, add the file to the dependencies on its first inclusion. */
593 if (!inc
->include_count
&& PRINT_THIS_DEP (pfile
, angle_brackets
))
594 deps_add_dep (pfile
->deps
, inc
->name
);
595 inc
->include_count
++;
597 /* Actually process the file. */
598 if (stack_include_file (pfile
, inc
))
601 pfile
->system_include_depth
++;
604 inc
->cmacro
= NEVER_REREAD
;
606 /* Handle -H option. */
607 if (CPP_OPTION (pfile
, print_include_names
))
609 cpp_buffer
*fp
= CPP_BUFFER (pfile
);
610 while ((fp
= CPP_PREV_BUFFER (fp
)) != NULL
)
612 fprintf (stderr
, " %s\n", inc
->name
);
618 if (CPP_OPTION (pfile
, print_deps_missing_files
)
619 && PRINT_THIS_DEP (pfile
, angle_brackets
))
622 deps_add_dep (pfile
->deps
, fname
);
626 struct file_name_list
*ptr
;
627 /* If requested as a system header, assume it belongs in
628 the first system header directory. */
629 if (CPP_OPTION (pfile
, bracket_include
))
630 ptr
= CPP_OPTION (pfile
, bracket_include
);
632 ptr
= CPP_OPTION (pfile
, quote_include
);
634 p
= (char *) alloca (strlen (ptr
->name
)
635 + strlen (fname
) + 2);
636 if (*ptr
->name
!= '\0')
638 strcpy (p
, ptr
->name
);
642 _cpp_simplify_pathname (p
);
643 deps_add_dep (pfile
->deps
, p
);
646 /* If -M was specified, and this header file won't be added to
647 the dependency list, then don't count this as an error,
648 because we can still produce correct output. Otherwise, we
649 can't produce correct output, because there may be
650 dependencies we need inside the missing file, and we don't
651 know what directory this missing file exists in. */
652 else if (CPP_PRINT_DEPS (pfile
)
653 && ! PRINT_THIS_DEP (pfile
, angle_brackets
))
654 cpp_warning (pfile
, "No include path in which to find %s", fname
);
656 cpp_error_from_errno (pfile
, fname
);
659 /* Locate file F, and determine whether it is newer than PFILE. Return -1,
660 if F cannot be located or dated, 1, if it is newer and 0 if older. */
663 _cpp_compare_file_date (pfile
, f
, len
, angle_brackets
)
670 struct file_name_list
*search_start
;
671 struct include_file
*inc
;
672 struct include_file
*current_include
= CPP_BUFFER (pfile
)->inc
;
675 search_start
= CPP_OPTION (pfile
, bracket_include
);
676 else if (CPP_OPTION (pfile
, ignore_srcdir
))
677 search_start
= CPP_OPTION (pfile
, quote_include
);
679 search_start
= CPP_BUFFER (pfile
)->actual_dir
;
681 fname
= alloca (len
+ 1);
682 memcpy (fname
, f
, len
);
684 inc
= find_include_file (pfile
, fname
, search_start
);
694 return inc
->st
.st_mtime
> current_include
->st
.st_mtime
;
698 /* Push an input buffer and load it up with the contents of FNAME.
699 If FNAME is "" or NULL, read standard input. */
701 cpp_read_file (pfile
, fname
)
705 struct include_file
*f
;
710 f
= lookup_include_file (pfile
, fname
);
714 cpp_error_from_errno (pfile
, fname
);
718 return stack_include_file (pfile
, f
);
721 /* Do appropriate cleanup when a file buffer is popped off the input
724 _cpp_pop_file_buffer (pfile
, buf
)
728 struct include_file
*inc
= buf
->inc
;
730 if (pfile
->system_include_depth
)
731 pfile
->system_include_depth
--;
732 if (pfile
->include_depth
)
733 pfile
->include_depth
--;
734 if (pfile
->potential_control_macro
)
736 if (inc
->cmacro
!= NEVER_REREAD
)
737 inc
->cmacro
= pfile
->potential_control_macro
;
738 pfile
->potential_control_macro
= 0;
740 pfile
->input_stack_listing_current
= 0;
743 if (inc
->refcnt
== 0 && DO_NOT_REREAD (inc
))
747 /* The file_name_map structure holds a mapping of file names for a
748 particular directory. This mapping is read from the file named
749 FILE_NAME_MAP_FILE in that directory. Such a file can be used to
750 map filenames on a file system with severe filename restrictions,
751 such as DOS. The format of the file name map file is just a series
752 of lines with two tokens on each line. The first token is the name
753 to map, and the second token is the actual name to use. */
757 struct file_name_map
*map_next
;
762 #define FILE_NAME_MAP_FILE "header.gcc"
764 /* Read a space delimited string of unlimited length from a stdio
768 read_filename_string (ch
, f
)
776 set
= alloc
= xmalloc (len
+ 1);
780 while ((ch
= getc (f
)) != EOF
&& ! is_space(ch
))
782 if (set
- alloc
== len
)
785 alloc
= xrealloc (alloc
, len
+ 1);
786 set
= alloc
+ len
/ 2;
796 /* This structure holds a linked list of file name maps, one per directory. */
798 struct file_name_map_list
800 struct file_name_map_list
*map_list_next
;
802 struct file_name_map
*map_list_map
;
805 /* Read the file name map file for DIRNAME. */
807 static struct file_name_map
*
808 read_name_map (pfile
, dirname
)
812 register struct file_name_map_list
*map_list_ptr
;
816 for (map_list_ptr
= CPP_OPTION (pfile
, map_list
); map_list_ptr
;
817 map_list_ptr
= map_list_ptr
->map_list_next
)
818 if (! strcmp (map_list_ptr
->map_list_name
, dirname
))
819 return map_list_ptr
->map_list_map
;
821 map_list_ptr
= ((struct file_name_map_list
*)
822 xmalloc (sizeof (struct file_name_map_list
)));
823 map_list_ptr
->map_list_name
= xstrdup (dirname
);
824 map_list_ptr
->map_list_map
= NULL
;
826 name
= (char *) alloca (strlen (dirname
) + strlen (FILE_NAME_MAP_FILE
) + 2);
827 strcpy (name
, dirname
);
830 strcat (name
, FILE_NAME_MAP_FILE
);
831 f
= fopen (name
, "r");
833 map_list_ptr
->map_list_map
= (struct file_name_map
*)-1;
837 int dirlen
= strlen (dirname
);
839 while ((ch
= getc (f
)) != EOF
)
842 struct file_name_map
*ptr
;
846 from
= read_filename_string (ch
, f
);
847 while ((ch
= getc (f
)) != EOF
&& is_hspace(ch
))
849 to
= read_filename_string (ch
, f
);
851 ptr
= ((struct file_name_map
*)
852 xmalloc (sizeof (struct file_name_map
)));
853 ptr
->map_from
= from
;
855 /* Make the real filename absolute. */
860 ptr
->map_to
= xmalloc (dirlen
+ strlen (to
) + 2);
861 strcpy (ptr
->map_to
, dirname
);
862 ptr
->map_to
[dirlen
] = '/';
863 strcpy (ptr
->map_to
+ dirlen
+ 1, to
);
867 ptr
->map_next
= map_list_ptr
->map_list_map
;
868 map_list_ptr
->map_list_map
= ptr
;
870 while ((ch
= getc (f
)) != '\n')
877 map_list_ptr
->map_list_next
= CPP_OPTION (pfile
, map_list
);
878 CPP_OPTION (pfile
, map_list
) = map_list_ptr
;
880 return map_list_ptr
->map_list_map
;
883 /* Remap NAME based on the file_name_map (if any) for LOC. */
886 remap_filename (pfile
, name
, loc
)
889 struct file_name_list
*loc
;
891 struct file_name_map
*map
;
892 const char *from
, *p
, *dir
;
895 loc
->name_map
= read_name_map (pfile
,
899 if (loc
->name_map
== (struct file_name_map
*)-1)
902 from
= name
+ strlen (loc
->name
) + 1;
904 for (map
= loc
->name_map
; map
; map
= map
->map_next
)
905 if (!strcmp (map
->map_from
, from
))
908 /* Try to find a mapping file for the particular directory we are
909 looking in. Thus #include <sys/types.h> will look up sys/types.h
910 in /usr/include/header.gcc and look up types.h in
911 /usr/include/sys/header.gcc. */
912 p
= strrchr (name
, '/');
916 && strlen (loc
->name
) == (size_t) (p
- name
)
917 && !strncmp (loc
->name
, name
, p
- name
))
918 /* FILENAME is in SEARCHPTR, which we've already checked. */
928 char * newdir
= (char *) alloca (p
- name
+ 1);
929 memcpy (newdir
, name
, p
- name
);
930 newdir
[p
- name
] = '\0';
935 for (map
= read_name_map (pfile
, dir
); map
; map
= map
->map_next
)
936 if (! strcmp (map
->map_from
, name
))
942 /* Given a path FNAME, extract the directory component and place it
943 onto the actual_dirs list. Return a pointer to the allocated
944 file_name_list structure. These structures are used to implement
945 current-directory "" include searching. */
947 static struct file_name_list
*
948 actual_directory (pfile
, fname
)
952 char *last_slash
, *dir
;
954 struct file_name_list
*x
;
956 dir
= xstrdup (fname
);
957 last_slash
= strrchr (dir
, '/');
960 if (last_slash
== dir
)
963 last_slash
[1] = '\0';
967 dlen
= last_slash
- dir
;
978 if (dlen
> pfile
->max_include_len
)
979 pfile
->max_include_len
= dlen
;
981 for (x
= pfile
->actual_dirs
; x
; x
= x
->alloc
)
982 if (!strcmp (x
->name
, dir
))
988 /* Not found, make a new one. */
989 x
= (struct file_name_list
*) xmalloc (sizeof (struct file_name_list
));
992 x
->next
= CPP_OPTION (pfile
, quote_include
);
993 x
->alloc
= pfile
->actual_dirs
;
994 x
->sysp
= CPP_BUFFER (pfile
)->inc
->sysp
;
997 pfile
->actual_dirs
= x
;
1001 /* Simplify a path name in place, deleting redundant components. This
1002 reduces OS overhead and guarantees that equivalent paths compare
1003 the same (modulo symlinks).
1006 foo/bar/../quux foo/quux
1010 //quux //quux (POSIX allows leading // as a namespace escape)
1012 Guarantees no trailing slashes. All transforms reduce the length
1016 _cpp_simplify_pathname (path
)
1023 #if defined (HAVE_DOS_BASED_FILE_SYSTEM)
1024 /* Convert all backslashes to slashes. */
1025 for (from
= path
; *from
; from
++)
1026 if (*from
== '\\') *from
= '/';
1028 /* Skip over leading drive letter if present. */
1029 if (ISALPHA (path
[0]) && path
[1] == ':')
1030 from
= to
= &path
[2];
1037 /* Remove redundant initial /s. */
1046 /* 3 or more initial /s are equivalent to 1 /. */
1047 while (*++from
== '/');
1049 /* On some hosts // differs from /; Posix allows this. */
1057 while (*from
== '/')
1060 if (from
[0] == '.' && from
[1] == '/')
1062 else if (from
[0] == '.' && from
[1] == '\0')
1064 else if (from
[0] == '.' && from
[1] == '.' && from
[2] == '/')
1081 while (to
> base
&& *to
!= '/') to
--;
1087 else if (from
[0] == '.' && from
[1] == '.' && from
[2] == '\0')
1100 while (to
> base
&& *to
!= '/') to
--;
1107 /* Copy this component and trailing /, if any. */
1108 while ((*to
++ = *from
++) != '/')
1120 /* Trim trailing slash */
1121 if (to
[0] == '/' && (!absolute
|| to
> path
+1))
1124 /* Change the empty string to "." so that stat() on the result
1125 will always work. */
1134 /* It is not clear when this should be used if at all, so I've
1135 disabled it until someone who understands VMS can look at it. */
1138 /* Under VMS we need to fix up the "include" specification filename.
1140 Rules for possible conversions
1142 fullname tried paths
1145 ./dir/name [.dir]name
1147 /name [000000]name, name
1148 dir/name dir:[000000]name, dir:name, dir/name
1149 dir1/dir2/name dir1:[dir2]name, dir1:[000000.dir2]name
1150 path:/name path:[000000]name, path:name
1151 path:/dir/name path:[000000.dir]name, path:[dir]name
1152 path:dir/name path:[dir]name
1153 [path]:[dir]name [path.dir]name
1154 path/[dir]name [path.dir]name
1156 The path:/name input is constructed when expanding <> includes. */
1160 hack_vms_include_specification (fullname
)
1163 register char *basename
, *unixname
, *local_ptr
, *first_slash
;
1164 int f
, check_filename_before_returning
, must_revert
;
1167 check_filename_before_returning
= 0;
1169 /* See if we can find a 1st slash. If not, there's no path information. */
1170 first_slash
= strchr (fullname
, '/');
1171 if (first_slash
== 0)
1172 return 0; /* Nothing to do!!! */
1174 /* construct device spec if none given. */
1176 if (strchr (fullname
, ':') == 0)
1179 /* If fullname has a slash, take it as device spec. */
1181 if (first_slash
== fullname
)
1183 first_slash
= strchr (fullname
+ 1, '/'); /* 2nd slash ? */
1185 *first_slash
= ':'; /* make device spec */
1186 for (basename
= fullname
; *basename
!= 0; basename
++)
1187 *basename
= *(basename
+1); /* remove leading slash */
1189 else if ((first_slash
[-1] != '.') /* keep ':/', './' */
1190 && (first_slash
[-1] != ':')
1191 && (first_slash
[-1] != ']')) /* or a vms path */
1195 else if ((first_slash
[1] == '[') /* skip './' in './[dir' */
1196 && (first_slash
[-1] == '.'))
1200 /* Get part after first ':' (basename[-1] == ':')
1201 or last '/' (basename[-1] == '/'). */
1203 basename
= base_name (fullname
);
1205 local_ptr
= Local
; /* initialize */
1207 /* We are trying to do a number of things here. First of all, we are
1208 trying to hammer the filenames into a standard format, such that later
1209 processing can handle them.
1211 If the file name contains something like [dir.], then it recognizes this
1212 as a root, and strips the ".]". Later processing will add whatever is
1213 needed to get things working properly.
1215 If no device is specified, then the first directory name is taken to be
1216 a device name (or a rooted logical). */
1218 /* Point to the UNIX filename part (which needs to be fixed!)
1219 but skip vms path information.
1220 [basename != fullname since first_slash != 0]. */
1222 if ((basename
[-1] == ':') /* vms path spec. */
1223 || (basename
[-1] == ']')
1224 || (basename
[-1] == '>'))
1225 unixname
= basename
;
1227 unixname
= fullname
;
1229 if (*unixname
== '/')
1232 /* If the directory spec is not rooted, we can just copy
1233 the UNIX filename part and we are done. */
1235 if (((basename
- fullname
) > 1)
1236 && ( (basename
[-1] == ']')
1237 || (basename
[-1] == '>')))
1239 if (basename
[-2] != '.')
1242 /* The VMS part ends in a `]', and the preceding character is not a `.'.
1243 -> PATH]:/name (basename = '/name', unixname = 'name')
1244 We strip the `]', and then splice the two parts of the name in the
1245 usual way. Given the default locations for include files,
1246 we will only use this code if the user specifies alternate locations
1247 with the /include (-I) switch on the command line. */
1249 basename
-= 1; /* Strip "]" */
1250 unixname
--; /* backspace */
1255 /* The VMS part has a ".]" at the end, and this will not do. Later
1256 processing will add a second directory spec, and this would be a syntax
1257 error. Thus we strip the ".]", and thus merge the directory specs.
1258 We also backspace unixname, so that it points to a '/'. This inhibits the
1259 generation of the 000000 root directory spec (which does not belong here
1262 basename
-= 2; /* Strip ".]" */
1263 unixname
--; /* backspace */
1271 /* We drop in here if there is no VMS style directory specification yet.
1272 If there is no device specification either, we make the first dir a
1273 device and try that. If we do not do this, then we will be essentially
1274 searching the users default directory (as if they did a #include "asdf.h").
1276 Then all we need to do is to push a '[' into the output string. Later
1277 processing will fill this in, and close the bracket. */
1279 if ((unixname
!= fullname
) /* vms path spec found. */
1280 && (basename
[-1] != ':'))
1281 *local_ptr
++ = ':'; /* dev not in spec. take first dir */
1283 *local_ptr
++ = '['; /* Open the directory specification */
1286 if (unixname
== fullname
) /* no vms dir spec. */
1289 if ((first_slash
!= 0) /* unix dir spec. */
1290 && (*unixname
!= '/') /* not beginning with '/' */
1291 && (*unixname
!= '.')) /* or './' or '../' */
1292 *local_ptr
++ = '.'; /* dir is local ! */
1295 /* at this point we assume that we have the device spec, and (at least
1296 the opening "[" for a directory specification. We may have directories
1299 If there are no other slashes then the filename will be
1300 in the "root" directory. Otherwise, we need to add
1301 directory specifications. */
1303 if (strchr (unixname
, '/') == 0)
1305 /* if no directories specified yet and none are following. */
1306 if (local_ptr
[-1] == '[')
1308 /* Just add "000000]" as the directory string */
1309 strcpy (local_ptr
, "000000]");
1310 local_ptr
+= strlen (local_ptr
);
1311 check_filename_before_returning
= 1; /* we might need to fool with this later */
1317 /* As long as there are still subdirectories to add, do them. */
1318 while (strchr (unixname
, '/') != 0)
1320 /* If this token is "." we can ignore it
1321 if it's not at the beginning of a path. */
1322 if ((unixname
[0] == '.') && (unixname
[1] == '/'))
1324 /* remove it at beginning of path. */
1325 if ( ((unixname
== fullname
) /* no device spec */
1326 && (fullname
+2 != basename
)) /* starts with ./ */
1328 || ((basename
[-1] == ':') /* device spec */
1329 && (unixname
-1 == basename
))) /* and ./ afterwards */
1330 *local_ptr
++ = '.'; /* make '[.' start of path. */
1335 /* Add a subdirectory spec. Do not duplicate "." */
1336 if ( local_ptr
[-1] != '.'
1337 && local_ptr
[-1] != '['
1338 && local_ptr
[-1] != '<')
1341 /* If this is ".." then the spec becomes "-" */
1342 if ( (unixname
[0] == '.')
1343 && (unixname
[1] == '.')
1344 && (unixname
[2] == '/'))
1346 /* Add "-" and skip the ".." */
1347 if ((local_ptr
[-1] == '.')
1348 && (local_ptr
[-2] == '['))
1349 local_ptr
--; /* prevent [.- */
1355 /* Copy the subdirectory */
1356 while (*unixname
!= '/')
1357 *local_ptr
++= *unixname
++;
1359 unixname
++; /* Skip the "/" */
1362 /* Close the directory specification */
1363 if (local_ptr
[-1] == '.') /* no trailing periods */
1366 if (local_ptr
[-1] == '[') /* no dir needed */
1372 /* Now add the filename. */
1375 *local_ptr
++ = *unixname
++;
1378 /* Now append it to the original VMS spec. */
1380 strcpy ((must_revert
==1)?fullname
:basename
, Local
);
1382 /* If we put a [000000] in the filename, try to open it first. If this fails,
1383 remove the [000000], and return that name. This provides flexibility
1384 to the user in that they can use both rooted and non-rooted logical names
1385 to point to the location of the file. */
1387 if (check_filename_before_returning
)
1389 f
= open (fullname
, O_RDONLY
|O_NONBLOCK
);
1392 /* The file name is OK as it is, so return it as is. */
1397 /* The filename did not work. Try to remove the [000000] from the name,
1400 basename
= strchr (fullname
, '[');
1401 local_ptr
= strchr (fullname
, ']') + 1;
1402 strcpy (basename
, local_ptr
); /* this gets rid of it */