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 /* This structure is used for the table of all includes. */
69 const char *name; /* actual path name of file */
70 const cpp_hashnode *cmacro; /* macro, if any, preventing reinclusion. */
71 const struct file_name_list *foundhere;
72 /* location in search path where file was
73 found, for #include_next and sysp. */
74 const unsigned char *buffer; /* pointer to cached file contents */
75 struct stat st; /* copy of stat(2) data for file */
76 int fd; /* fd open on file (short term storage only) */
77 unsigned short include_count; /* number of times file has been read */
78 unsigned short refcnt; /* number of stacked buffers using this file */
79 unsigned char mapped; /* file buffer is mmapped */
80 unsigned char defined; /* cmacro prevents inclusion in this state */
83 static struct file_name_map *read_name_map
84 PARAMS ((cpp_reader *, const char *));
85 static char *read_filename_string PARAMS ((int, FILE *));
86 static char *remap_filename PARAMS ((cpp_reader *, char *,
87 struct file_name_list *));
88 static struct file_name_list *actual_directory
89 PARAMS ((cpp_reader *, const char *));
90 static struct include_file *find_include_file
91 PARAMS ((cpp_reader *, const char *,
92 struct file_name_list *));
93 static struct include_file *open_file PARAMS ((cpp_reader *, const char *));
94 static void read_include_file PARAMS ((cpp_reader *, struct include_file *));
95 static void stack_include_file PARAMS ((cpp_reader *, struct include_file *));
96 static void purge_cache PARAMS ((struct include_file *));
97 static void destroy_include_file_node PARAMS ((splay_tree_value));
98 static int report_missing_guard PARAMS ((splay_tree_node, void *));
101 static void hack_vms_include_specification PARAMS ((char *));
104 /* We use a splay tree to store information about all the include
105 files seen in this compilation. The key of each tree node is the
106 physical path to the file. The value is 0 if the file does not
107 exist, or a struct include_file pointer. */
110 destroy_include_file_node (v)
113 struct include_file *f = (struct include_file *)v;
117 free (f); /* The tree is registered with free to free f->name. */
122 _cpp_init_includes (pfile)
125 pfile->all_include_files
126 = splay_tree_new ((splay_tree_compare_fn) strcmp,
127 (splay_tree_delete_key_fn) free,
128 destroy_include_file_node);
132 _cpp_cleanup_includes (pfile)
135 splay_tree_delete (pfile->all_include_files);
138 /* Mark a file to not be reread (e.g. #import, read failure). */
140 _cpp_never_reread (file)
141 struct include_file *file;
143 file->cmacro = NEVER_REREAD;
146 /* Given a file name, look it up in the cache; if there is no entry,
147 create one with a non-NULL value (regardless of success in opening
148 the file). If the file doesn't exist or is inaccessible, this
149 entry is flagged so we don't attempt to open it again in the
150 future. If the file isn't open, open it.
152 Returns an include_file structure with an open file descriptor on
153 success, or NULL on failure. */
155 static struct include_file *
156 open_file (pfile, filename)
158 const char *filename;
161 struct include_file *file;
163 nd = splay_tree_lookup (pfile->all_include_files, (splay_tree_key) filename);
167 file = (struct include_file *) nd->value;
169 /* Don't retry opening if we failed previously. */
173 /* Don't reopen an idempotent file. */
174 if (DO_NOT_REREAD (file))
177 /* Don't reopen one which is already loaded. */
178 if (file->buffer != NULL)
183 /* In particular, this clears foundhere. */
184 file = xcnew (struct include_file);
185 file->name = xstrdup (filename);
186 splay_tree_insert (pfile->all_include_files,
187 (splay_tree_key) file->name,
188 (splay_tree_value) file);
191 /* We used to open files in nonblocking mode, but that caused more
192 problems than it solved. Do take care not to acquire a
193 controlling terminal by mistake (this can't happen on sane
194 systems, but paranoia is a virtue).
196 Use the three-argument form of open even though we aren't
197 specifying O_CREAT, to defend against broken system headers.
199 O_BINARY tells some runtime libraries (notably DJGPP) not to do
200 newline translation; we can handle DOS line breaks just fine
203 Special case: the empty string is translated to stdin. */
205 if (filename[0] == '\0')
208 file->fd = open (filename, O_RDONLY | O_NOCTTY | O_BINARY, 0666);
210 if (file->fd != -1 && fstat (file->fd, &file->st) == 0)
212 /* Mark a regular, zero-length file never-reread now. */
213 if (S_ISREG (file->st.st_mode) && file->st.st_size == 0)
215 _cpp_never_reread (file);
223 /* Don't issue an error message if the file doesn't exist. */
224 if (errno != ENOENT && errno != ENOTDIR)
225 cpp_error_from_errno (pfile, filename);
227 /* Create a negative node for this path, and return null. */
233 /* Place the file referenced by INC into a new buffer on PFILE's
234 stack. If there are errors, or the file should not be re-included,
235 a null buffer is pushed. */
238 stack_include_file (pfile, inc)
240 struct include_file *inc;
242 const char *filename = 0;
243 unsigned int lineno = 0;
248 filename = pfile->buffer->nominal_fname;
249 lineno = pfile->buffer->lineno;
254 read_include_file (pfile, inc);
256 /* Push a null buffer. */
257 fp = cpp_push_buffer (pfile, NULL, 0);
259 fp->nominal_fname = inc->name;
260 fp->buf = inc->buffer;
261 fp->rlimit = fp->buf;
262 if (! DO_NOT_REREAD (inc))
263 fp->rlimit += inc->st.st_size;
265 fp->line_base = fp->buf;
266 fp->lineno = 0; /* For _cpp_do_file_change. */
269 fp->sysp = inc->foundhere->sysp;
271 /* The ->actual_dir field is only used when ignore_srcdir is not in effect;
273 if (!CPP_OPTION (pfile, ignore_srcdir))
274 fp->actual_dir = actual_directory (pfile, inc->name);
276 /* Initialise controlling macro state. */
277 pfile->mi_state = MI_OUTSIDE;
278 pfile->mi_cmacro = 0;
279 pfile->include_depth++;
280 pfile->input_stack_listing_current = 0;
282 _cpp_do_file_change (pfile, FC_ENTER, filename, lineno);
287 /* Read the file referenced by INC into the file cache.
289 If fd points to a plain file, we might be able to mmap it; we can
290 definitely allocate the buffer all at once. If fd is a pipe or
291 terminal, we can't do either. If fd is something weird, like a
292 block device or a directory, we don't want to read it at all.
294 Unfortunately, different systems use different st.st_mode values
295 for pipes: some have S_ISFIFO, some S_ISSOCK, some are buggy and
296 zero the entire struct stat except a couple fields. Hence we don't
297 even try to figure out what something is, except for plain files,
298 directories, and block devices.
300 FIXME: Flush file cache and try again if we run out of memory. */
303 read_include_file (pfile, inc)
305 struct include_file *inc;
307 ssize_t size, offset, count;
310 static int pagesize = -1;
313 if (DO_NOT_REREAD (inc))
316 if (S_ISREG (inc->st.st_mode))
318 /* off_t might have a wider range than ssize_t - in other words,
319 the max size of a file might be bigger than the address
320 space. We can't handle a file that large. (Anyone with
321 a single source file bigger than 2GB needs to rethink
322 their coding style.) Some systems (e.g. AIX 4.1) define
323 SSIZE_MAX to be much smaller than the actual range of the
324 type. Use INTTYPE_MAXIMUM unconditionally to ensure this
326 if (inc->st.st_size > INTTYPE_MAXIMUM (ssize_t))
328 cpp_error (pfile, "%s is too large", inc->name);
331 size = inc->st.st_size;
336 pagesize = getpagesize ();
338 if (size / pagesize >= MMAP_THRESHOLD)
340 buf = (U_CHAR *) mmap (0, size, PROT_READ, MAP_PRIVATE, inc->fd, 0);
341 if (buf == (U_CHAR *)-1)
348 buf = (U_CHAR *) xmalloc (size);
350 while (offset < size)
352 count = read (inc->fd, buf + offset, size - offset);
357 cpp_warning (pfile, "%s is shorter than expected", inc->name);
364 else if (S_ISBLK (inc->st.st_mode))
366 cpp_error (pfile, "%s is a block device", inc->name);
369 else if (S_ISDIR (inc->st.st_mode))
371 cpp_error (pfile, "%s is a directory", inc->name);
376 /* 8 kilobytes is a sensible starting size. It ought to be
377 bigger than the kernel pipe buffer, and it's definitely
378 bigger than the majority of C source files. */
381 buf = (U_CHAR *) xmalloc (size);
383 while ((count = read (inc->fd, buf + offset, size - offset)) > 0)
387 buf = xrealloc (buf, (size *= 2));
393 buf = xrealloc (buf, offset);
394 inc->st.st_size = offset;
403 cpp_error_from_errno (pfile, inc->name);
405 /* Do not try to read this file again. */
408 _cpp_never_reread (inc);
414 struct include_file *inc;
420 munmap ((PTR) inc->buffer, inc->st.st_size);
423 free ((PTR) inc->buffer);
428 /* Return 1 if the file named by FNAME has been included before in
429 any context, 0 otherwise. */
431 cpp_included (pfile, fname)
435 struct file_name_list *path;
441 /* Just look it up. */
442 nd = splay_tree_lookup (pfile->all_include_files, (splay_tree_key) fname);
443 return (nd && nd->value);
446 /* Search directory path for the file. */
447 name = (char *) alloca (strlen (fname) + pfile->max_include_len
448 + 2 + INCLUDE_LEN_FUDGE);
449 for (path = CPP_OPTION (pfile, quote_include); path; path = path->next)
451 memcpy (name, path->name, path->nlen);
452 name[path->nlen] = '/';
453 strcpy (&name[path->nlen+1], fname);
454 _cpp_simplify_pathname (name);
455 if (CPP_OPTION (pfile, remap))
456 name = remap_filename (pfile, name, path);
458 nd = splay_tree_lookup (pfile->all_include_files, (splay_tree_key) name);
465 /* Search for include file FNAME in the include chain starting at
466 SEARCH_START. Return 0 if there is no such file (or it's un-openable),
467 otherwise an include_file structure. */
469 static struct include_file *
470 find_include_file (pfile, fname, search_start)
473 struct file_name_list *search_start;
475 struct file_name_list *path;
477 struct include_file *file;
480 return open_file (pfile, fname);
482 /* Search directory path for the file. */
483 name = (char *) alloca (strlen (fname) + pfile->max_include_len
484 + 2 + INCLUDE_LEN_FUDGE);
485 for (path = search_start; path; path = path->next)
487 memcpy (name, path->name, path->nlen);
488 name[path->nlen] = '/';
489 strcpy (&name[path->nlen+1], fname);
490 _cpp_simplify_pathname (name);
491 if (CPP_OPTION (pfile, remap))
492 name = remap_filename (pfile, name, path);
494 file = open_file (pfile, name);
497 file->foundhere = path;
504 /* Not everyone who wants to set system-header-ness on a buffer can
505 see the details of a buffer. This is an exported interface because
506 fix-header needs it. */
508 cpp_make_system_header (pfile, syshdr, externc)
514 /* 1 = system header, 2 = system header to be treated as C. */
516 flags = 1 + (externc != 0);
517 pfile->buffer->sysp = flags;
520 /* Report on all files that might benefit from a multiple include guard.
523 _cpp_report_missing_guards (pfile)
527 splay_tree_foreach (pfile->all_include_files, report_missing_guard,
532 report_missing_guard (n, b)
536 struct include_file *f = (struct include_file *) n->value;
537 int *bannerp = (int *)b;
539 if (f && f->cmacro == 0 && f->include_count == 1)
543 fputs (_("Multiple include guards may be useful for:\n"), stderr);
546 fputs (f->name, stderr);
552 #define PRINT_THIS_DEP(p, b) (CPP_PRINT_DEPS(p) > (b||p->system_include_depth))
554 _cpp_execute_include (pfile, header, no_reinclude, include_next)
556 const cpp_token *header;
560 struct file_name_list *search_start = 0;
561 unsigned int len = header->val.str.len;
562 unsigned int angle_brackets = header->type == CPP_HEADER_NAME;
563 struct include_file *inc;
566 /* Help protect #include or similar from recursion. */
567 if (pfile->buffer_stack_depth >= CPP_STACK_MAX)
569 cpp_fatal (pfile, "#include nested too deeply");
573 /* Check we've tidied up #include before entering the buffer. */
574 if (pfile->context->prev)
576 cpp_ice (pfile, "attempt to push file buffer with contexts stacked");
580 /* For #include_next, skip in the search path past the dir in which
581 the current file was found. If this is the last directory in the
582 search path, don't include anything. If the current file was
583 specified with an absolute path, use the normal search logic. If
584 this is the primary source file, use the normal search logic and
585 generate a warning. */
588 if (! pfile->buffer->prev)
589 cpp_warning (pfile, "#include_next in primary source file");
592 if (pfile->buffer->inc->foundhere)
594 search_start = pfile->buffer->inc->foundhere->next;
601 fname = alloca (len + 1);
602 memcpy (fname, header->val.str.text, len);
608 search_start = CPP_OPTION (pfile, bracket_include);
609 else if (CPP_OPTION (pfile, ignore_srcdir))
610 search_start = CPP_OPTION (pfile, quote_include);
612 search_start = CPP_BUFFER (pfile)->actual_dir;
616 cpp_error (pfile, "No include path in which to find %s", fname);
621 inc = find_include_file (pfile, fname, search_start);
625 /* For -M, add the file to the dependencies on its first inclusion. */
626 if (!inc->include_count && PRINT_THIS_DEP (pfile, angle_brackets))
627 deps_add_dep (pfile->deps, inc->name);
628 inc->include_count++;
630 /* Actually process the file. */
631 stack_include_file (pfile, inc);
634 pfile->system_include_depth++;
636 if (! DO_NOT_REREAD (inc))
639 _cpp_never_reread (inc);
641 /* Handle -H option. */
642 if (CPP_OPTION (pfile, print_include_names))
644 cpp_buffer *fp = CPP_BUFFER (pfile);
645 while ((fp = CPP_PREV_BUFFER (fp)) != NULL)
647 fprintf (stderr, " %s\n", inc->name);
654 if (CPP_OPTION (pfile, print_deps_missing_files)
655 && PRINT_THIS_DEP (pfile, angle_brackets))
658 deps_add_dep (pfile->deps, fname);
662 struct file_name_list *ptr;
663 /* If requested as a system header, assume it belongs in
664 the first system header directory. */
665 if (CPP_OPTION (pfile, bracket_include))
666 ptr = CPP_OPTION (pfile, bracket_include);
668 ptr = CPP_OPTION (pfile, quote_include);
670 p = (char *) alloca (strlen (ptr->name)
671 + strlen (fname) + 2);
672 if (*ptr->name != '\0')
674 strcpy (p, ptr->name);
678 _cpp_simplify_pathname (p);
679 deps_add_dep (pfile->deps, p);
682 /* If -M was specified, and this header file won't be added to
683 the dependency list, then don't count this as an error,
684 because we can still produce correct output. Otherwise, we
685 can't produce correct output, because there may be
686 dependencies we need inside the missing file, and we don't
687 know what directory this missing file exists in. */
688 else if (CPP_PRINT_DEPS (pfile)
689 && ! PRINT_THIS_DEP (pfile, angle_brackets))
690 cpp_warning (pfile, "No include path in which to find %s", fname);
692 cpp_error_from_errno (pfile, fname);
695 /* Locate file F, and determine whether it is newer than PFILE. Return -1,
696 if F cannot be located or dated, 1, if it is newer and 0 if older. */
698 _cpp_compare_file_date (pfile, f)
702 unsigned int len = f->val.str.len;
704 struct file_name_list *search_start;
705 struct include_file *inc;
707 if (f->type == CPP_HEADER_NAME)
708 search_start = CPP_OPTION (pfile, bracket_include);
709 else if (CPP_OPTION (pfile, ignore_srcdir))
710 search_start = CPP_OPTION (pfile, quote_include);
712 search_start = CPP_BUFFER (pfile)->actual_dir;
714 fname = alloca (len + 1);
715 memcpy (fname, f->val.str.text, len);
717 inc = find_include_file (pfile, fname, search_start);
727 return inc->st.st_mtime > CPP_BUFFER (pfile)->inc->st.st_mtime;
731 /* Push an input buffer and load it up with the contents of FNAME.
732 If FNAME is "" or NULL, read standard input. */
734 _cpp_read_file (pfile, fname)
738 struct include_file *f;
743 f = open_file (pfile, fname);
747 cpp_error_from_errno (pfile, fname);
751 stack_include_file (pfile, f);
755 /* Do appropriate cleanup when a file buffer is popped off the input
758 _cpp_pop_file_buffer (pfile, buf)
762 struct include_file *inc = buf->inc;
764 if (pfile->system_include_depth)
765 pfile->system_include_depth--;
766 if (pfile->include_depth)
767 pfile->include_depth--;
768 pfile->input_stack_listing_current = 0;
770 /* Record the inclusion-preventing macro and its definedness. */
771 if (pfile->mi_state == MI_OUTSIDE && inc->cmacro != NEVER_REREAD)
773 /* This could be NULL meaning no controlling macro. */
774 inc->cmacro = pfile->mi_cmacro;
778 /* Invalidate control macros in the #including file. */
779 pfile->mi_state = MI_FAILED;
782 if (inc->refcnt == 0 && DO_NOT_REREAD (inc))
786 /* The file_name_map structure holds a mapping of file names for a
787 particular directory. This mapping is read from the file named
788 FILE_NAME_MAP_FILE in that directory. Such a file can be used to
789 map filenames on a file system with severe filename restrictions,
790 such as DOS. The format of the file name map file is just a series
791 of lines with two tokens on each line. The first token is the name
792 to map, and the second token is the actual name to use. */
796 struct file_name_map *map_next;
801 #define FILE_NAME_MAP_FILE "header.gcc"
803 /* Read a space delimited string of unlimited length from a stdio
807 read_filename_string (ch, f)
815 set = alloc = xmalloc (len + 1);
819 while ((ch = getc (f)) != EOF && ! is_space(ch))
821 if (set - alloc == len)
824 alloc = xrealloc (alloc, len + 1);
825 set = alloc + len / 2;
835 /* This structure holds a linked list of file name maps, one per directory. */
837 struct file_name_map_list
839 struct file_name_map_list *map_list_next;
841 struct file_name_map *map_list_map;
844 /* Read the file name map file for DIRNAME. */
846 static struct file_name_map *
847 read_name_map (pfile, dirname)
851 register struct file_name_map_list *map_list_ptr;
855 for (map_list_ptr = CPP_OPTION (pfile, map_list); map_list_ptr;
856 map_list_ptr = map_list_ptr->map_list_next)
857 if (! strcmp (map_list_ptr->map_list_name, dirname))
858 return map_list_ptr->map_list_map;
860 map_list_ptr = ((struct file_name_map_list *)
861 xmalloc (sizeof (struct file_name_map_list)));
862 map_list_ptr->map_list_name = xstrdup (dirname);
863 map_list_ptr->map_list_map = NULL;
865 name = (char *) alloca (strlen (dirname) + strlen (FILE_NAME_MAP_FILE) + 2);
866 strcpy (name, dirname);
869 strcat (name, FILE_NAME_MAP_FILE);
870 f = fopen (name, "r");
872 map_list_ptr->map_list_map = (struct file_name_map *)-1;
876 int dirlen = strlen (dirname);
878 while ((ch = getc (f)) != EOF)
881 struct file_name_map *ptr;
885 from = read_filename_string (ch, f);
886 while ((ch = getc (f)) != EOF && is_hspace(ch))
888 to = read_filename_string (ch, f);
890 ptr = ((struct file_name_map *)
891 xmalloc (sizeof (struct file_name_map)));
892 ptr->map_from = from;
894 /* Make the real filename absolute. */
899 ptr->map_to = xmalloc (dirlen + strlen (to) + 2);
900 strcpy (ptr->map_to, dirname);
901 ptr->map_to[dirlen] = '/';
902 strcpy (ptr->map_to + dirlen + 1, to);
906 ptr->map_next = map_list_ptr->map_list_map;
907 map_list_ptr->map_list_map = ptr;
909 while ((ch = getc (f)) != '\n')
916 map_list_ptr->map_list_next = CPP_OPTION (pfile, map_list);
917 CPP_OPTION (pfile, map_list) = map_list_ptr;
919 return map_list_ptr->map_list_map;
922 /* Remap NAME based on the file_name_map (if any) for LOC. */
925 remap_filename (pfile, name, loc)
928 struct file_name_list *loc;
930 struct file_name_map *map;
931 const char *from, *p, *dir;
934 loc->name_map = read_name_map (pfile,
938 if (loc->name_map == (struct file_name_map *)-1)
941 from = name + strlen (loc->name) + 1;
943 for (map = loc->name_map; map; map = map->map_next)
944 if (!strcmp (map->map_from, from))
947 /* Try to find a mapping file for the particular directory we are
948 looking in. Thus #include <sys/types.h> will look up sys/types.h
949 in /usr/include/header.gcc and look up types.h in
950 /usr/include/sys/header.gcc. */
951 p = strrchr (name, '/');
955 && strlen (loc->name) == (size_t) (p - name)
956 && !strncmp (loc->name, name, p - name))
957 /* FILENAME is in SEARCHPTR, which we've already checked. */
967 char * newdir = (char *) alloca (p - name + 1);
968 memcpy (newdir, name, p - name);
969 newdir[p - name] = '\0';
974 for (map = read_name_map (pfile, dir); map; map = map->map_next)
975 if (! strcmp (map->map_from, name))
981 /* Given a path FNAME, extract the directory component and place it
982 onto the actual_dirs list. Return a pointer to the allocated
983 file_name_list structure. These structures are used to implement
984 current-directory "" include searching. */
986 static struct file_name_list *
987 actual_directory (pfile, fname)
991 char *last_slash, *dir;
993 struct file_name_list *x;
995 dir = xstrdup (fname);
996 last_slash = strrchr (dir, '/');
999 if (last_slash == dir)
1002 last_slash[1] = '\0';
1006 dlen = last_slash - dir;
1013 dir = xstrdup (".");
1017 if (dlen > pfile->max_include_len)
1018 pfile->max_include_len = dlen;
1020 for (x = pfile->actual_dirs; x; x = x->alloc)
1021 if (!strcmp (x->name, dir))
1027 /* Not found, make a new one. */
1028 x = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
1031 x->next = CPP_OPTION (pfile, quote_include);
1032 x->alloc = pfile->actual_dirs;
1033 x->sysp = pfile->buffer->sysp;
1036 pfile->actual_dirs = x;
1040 /* Simplify a path name in place, deleting redundant components. This
1041 reduces OS overhead and guarantees that equivalent paths compare
1042 the same (modulo symlinks).
1045 foo/bar/../quux foo/quux
1049 //quux //quux (POSIX allows leading // as a namespace escape)
1051 Guarantees no trailing slashes. All transforms reduce the length
1055 _cpp_simplify_pathname (path)
1062 #if defined (HAVE_DOS_BASED_FILE_SYSTEM)
1063 /* Convert all backslashes to slashes. */
1064 for (from = path; *from; from++)
1065 if (*from == '\\') *from = '/';
1067 /* Skip over leading drive letter if present. */
1068 if (ISALPHA (path[0]) && path[1] == ':')
1069 from = to = &path[2];
1076 /* Remove redundant initial /s. */
1085 /* 3 or more initial /s are equivalent to 1 /. */
1086 while (*++from == '/');
1088 /* On some hosts // differs from /; Posix allows this. */
1096 while (*from == '/')
1099 if (from[0] == '.' && from[1] == '/')
1101 else if (from[0] == '.' && from[1] == '\0')
1103 else if (from[0] == '.' && from[1] == '.' && from[2] == '/')
1120 while (to > base && *to != '/') to--;
1126 else if (from[0] == '.' && from[1] == '.' && from[2] == '\0')
1139 while (to > base && *to != '/') to--;
1146 /* Copy this component and trailing /, if any. */
1147 while ((*to++ = *from++) != '/')
1159 /* Trim trailing slash */
1160 if (to[0] == '/' && (!absolute || to > path+1))
1163 /* Change the empty string to "." so that stat() on the result
1164 will always work. */
1173 /* It is not clear when this should be used if at all, so I've
1174 disabled it until someone who understands VMS can look at it. */
1177 /* Under VMS we need to fix up the "include" specification filename.
1179 Rules for possible conversions
1181 fullname tried paths
1184 ./dir/name [.dir]name
1186 /name [000000]name, name
1187 dir/name dir:[000000]name, dir:name, dir/name
1188 dir1/dir2/name dir1:[dir2]name, dir1:[000000.dir2]name
1189 path:/name path:[000000]name, path:name
1190 path:/dir/name path:[000000.dir]name, path:[dir]name
1191 path:dir/name path:[dir]name
1192 [path]:[dir]name [path.dir]name
1193 path/[dir]name [path.dir]name
1195 The path:/name input is constructed when expanding <> includes. */
1199 hack_vms_include_specification (fullname)
1202 register char *basename, *unixname, *local_ptr, *first_slash;
1203 int f, check_filename_before_returning, must_revert;
1206 check_filename_before_returning = 0;
1208 /* See if we can find a 1st slash. If not, there's no path information. */
1209 first_slash = strchr (fullname, '/');
1210 if (first_slash == 0)
1211 return 0; /* Nothing to do!!! */
1213 /* construct device spec if none given. */
1215 if (strchr (fullname, ':') == 0)
1218 /* If fullname has a slash, take it as device spec. */
1220 if (first_slash == fullname)
1222 first_slash = strchr (fullname + 1, '/'); /* 2nd slash ? */
1224 *first_slash = ':'; /* make device spec */
1225 for (basename = fullname; *basename != 0; basename++)
1226 *basename = *(basename+1); /* remove leading slash */
1228 else if ((first_slash[-1] != '.') /* keep ':/', './' */
1229 && (first_slash[-1] != ':')
1230 && (first_slash[-1] != ']')) /* or a vms path */
1234 else if ((first_slash[1] == '[') /* skip './' in './[dir' */
1235 && (first_slash[-1] == '.'))
1239 /* Get part after first ':' (basename[-1] == ':')
1240 or last '/' (basename[-1] == '/'). */
1242 basename = base_name (fullname);
1244 local_ptr = Local; /* initialize */
1246 /* We are trying to do a number of things here. First of all, we are
1247 trying to hammer the filenames into a standard format, such that later
1248 processing can handle them.
1250 If the file name contains something like [dir.], then it recognizes this
1251 as a root, and strips the ".]". Later processing will add whatever is
1252 needed to get things working properly.
1254 If no device is specified, then the first directory name is taken to be
1255 a device name (or a rooted logical). */
1257 /* Point to the UNIX filename part (which needs to be fixed!)
1258 but skip vms path information.
1259 [basename != fullname since first_slash != 0]. */
1261 if ((basename[-1] == ':') /* vms path spec. */
1262 || (basename[-1] == ']')
1263 || (basename[-1] == '>'))
1264 unixname = basename;
1266 unixname = fullname;
1268 if (*unixname == '/')
1271 /* If the directory spec is not rooted, we can just copy
1272 the UNIX filename part and we are done. */
1274 if (((basename - fullname) > 1)
1275 && ( (basename[-1] == ']')
1276 || (basename[-1] == '>')))
1278 if (basename[-2] != '.')
1281 /* The VMS part ends in a `]', and the preceding character is not a `.'.
1282 -> PATH]:/name (basename = '/name', unixname = 'name')
1283 We strip the `]', and then splice the two parts of the name in the
1284 usual way. Given the default locations for include files,
1285 we will only use this code if the user specifies alternate locations
1286 with the /include (-I) switch on the command line. */
1288 basename -= 1; /* Strip "]" */
1289 unixname--; /* backspace */
1294 /* The VMS part has a ".]" at the end, and this will not do. Later
1295 processing will add a second directory spec, and this would be a syntax
1296 error. Thus we strip the ".]", and thus merge the directory specs.
1297 We also backspace unixname, so that it points to a '/'. This inhibits the
1298 generation of the 000000 root directory spec (which does not belong here
1301 basename -= 2; /* Strip ".]" */
1302 unixname--; /* backspace */
1310 /* We drop in here if there is no VMS style directory specification yet.
1311 If there is no device specification either, we make the first dir a
1312 device and try that. If we do not do this, then we will be essentially
1313 searching the users default directory (as if they did a #include "asdf.h").
1315 Then all we need to do is to push a '[' into the output string. Later
1316 processing will fill this in, and close the bracket. */
1318 if ((unixname != fullname) /* vms path spec found. */
1319 && (basename[-1] != ':'))
1320 *local_ptr++ = ':'; /* dev not in spec. take first dir */
1322 *local_ptr++ = '['; /* Open the directory specification */
1325 if (unixname == fullname) /* no vms dir spec. */
1328 if ((first_slash != 0) /* unix dir spec. */
1329 && (*unixname != '/') /* not beginning with '/' */
1330 && (*unixname != '.')) /* or './' or '../' */
1331 *local_ptr++ = '.'; /* dir is local ! */
1334 /* at this point we assume that we have the device spec, and (at least
1335 the opening "[" for a directory specification. We may have directories
1338 If there are no other slashes then the filename will be
1339 in the "root" directory. Otherwise, we need to add
1340 directory specifications. */
1342 if (strchr (unixname, '/') == 0)
1344 /* if no directories specified yet and none are following. */
1345 if (local_ptr[-1] == '[')
1347 /* Just add "000000]" as the directory string */
1348 strcpy (local_ptr, "000000]");
1349 local_ptr += strlen (local_ptr);
1350 check_filename_before_returning = 1; /* we might need to fool with this later */
1356 /* As long as there are still subdirectories to add, do them. */
1357 while (strchr (unixname, '/') != 0)
1359 /* If this token is "." we can ignore it
1360 if it's not at the beginning of a path. */
1361 if ((unixname[0] == '.') && (unixname[1] == '/'))
1363 /* remove it at beginning of path. */
1364 if ( ((unixname == fullname) /* no device spec */
1365 && (fullname+2 != basename)) /* starts with ./ */
1367 || ((basename[-1] == ':') /* device spec */
1368 && (unixname-1 == basename))) /* and ./ afterwards */
1369 *local_ptr++ = '.'; /* make '[.' start of path. */
1374 /* Add a subdirectory spec. Do not duplicate "." */
1375 if ( local_ptr[-1] != '.'
1376 && local_ptr[-1] != '['
1377 && local_ptr[-1] != '<')
1380 /* If this is ".." then the spec becomes "-" */
1381 if ( (unixname[0] == '.')
1382 && (unixname[1] == '.')
1383 && (unixname[2] == '/'))
1385 /* Add "-" and skip the ".." */
1386 if ((local_ptr[-1] == '.')
1387 && (local_ptr[-2] == '['))
1388 local_ptr--; /* prevent [.- */
1394 /* Copy the subdirectory */
1395 while (*unixname != '/')
1396 *local_ptr++= *unixname++;
1398 unixname++; /* Skip the "/" */
1401 /* Close the directory specification */
1402 if (local_ptr[-1] == '.') /* no trailing periods */
1405 if (local_ptr[-1] == '[') /* no dir needed */
1411 /* Now add the filename. */
1414 *local_ptr++ = *unixname++;
1417 /* Now append it to the original VMS spec. */
1419 strcpy ((must_revert==1)?fullname:basename, Local);
1421 /* If we put a [000000] in the filename, try to open it first. If this fails,
1422 remove the [000000], and return that name. This provides flexibility
1423 to the user in that they can use both rooted and non-rooted logical names
1424 to point to the location of the file. */
1426 if (check_filename_before_returning)
1428 f = open (fullname, O_RDONLY|O_NONBLOCK);
1431 /* The file name is OK as it is, so return it as is. */
1436 /* The filename did not work. Try to remove the [000000] from the name,
1439 basename = strchr (fullname, '[');
1440 local_ptr = strchr (fullname, ']') + 1;
1441 strcpy (basename, local_ptr); /* this gets rid of it */