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 *open_file PARAMS ((cpp_reader *, const char *));
77 static void read_include_file PARAMS ((cpp_reader *, struct include_file *));
78 static void stack_include_file PARAMS ((cpp_reader *, struct include_file *));
79 static void purge_cache PARAMS ((struct include_file *));
80 static void destroy_include_file_node PARAMS ((splay_tree_value));
81 static int report_missing_guard PARAMS ((splay_tree_node, void *));
84 static void hack_vms_include_specification PARAMS ((char *));
87 /* We use a splay tree to store information about all the include
88 files seen in this compilation. The key of each tree node is the
89 physical path to the file. The value is 0 if the file does not
90 exist, or a struct include_file pointer. */
93 destroy_include_file_node (v)
96 struct include_file *f = (struct include_file *)v;
100 free (f); /* The tree is registered with free to free f->name. */
105 _cpp_init_includes (pfile)
108 pfile->all_include_files
109 = splay_tree_new ((splay_tree_compare_fn) strcmp,
110 (splay_tree_delete_key_fn) free,
111 destroy_include_file_node);
115 _cpp_cleanup_includes (pfile)
118 splay_tree_delete (pfile->all_include_files);
121 /* Given a file name, look it up in the cache; if there is no entry,
122 create one with a non-NULL value (regardless of success in opening
123 the file). If the file doesn't exist or is inaccessible, this
124 entry is flagged so we don't attempt to open it again in the
125 future. If the file isn't open, open it.
127 Returns an include_file structure with an open file descriptor on
128 success, or NULL on failure. */
130 static struct include_file *
131 open_file (pfile, filename)
133 const char *filename;
136 struct include_file *file;
138 nd = splay_tree_lookup (pfile->all_include_files, (splay_tree_key) filename);
142 file = (struct include_file *) nd->value;
144 /* Don't retry opening if we failed previously. */
148 /* Don't reopen an idempotent file. */
149 if (DO_NOT_REREAD (file))
152 /* Don't reopen one which is already loaded. */
153 if (file->buffer != NULL)
158 file = xcnew (struct include_file);
159 file->name = xstrdup (filename);
160 splay_tree_insert (pfile->all_include_files,
161 (splay_tree_key) file->name,
162 (splay_tree_value) file);
165 /* We used to open files in nonblocking mode, but that caused more
166 problems than it solved. Do take care not to acquire a
167 controlling terminal by mistake (this can't happen on sane
168 systems, but paranoia is a virtue).
170 Use the three-argument form of open even though we aren't
171 specifying O_CREAT, to defend against broken system headers.
173 O_BINARY tells some runtime libraries (notably DJGPP) not to do
174 newline translation; we can handle DOS line breaks just fine
177 Special case: the empty string is translated to stdin. */
179 if (filename[0] == '\0')
182 file->fd = open (filename, O_RDONLY | O_NOCTTY | O_BINARY, 0666);
184 if (file->fd != -1 && fstat (file->fd, &file->st) == 0)
186 /* Mark a regular, zero-length file never-reread now. */
187 if (S_ISREG (file->st.st_mode) && file->st.st_size == 0)
189 file->cmacro = NEVER_REREAD;
197 /* Don't issue an error message if the file doesn't exist. */
198 if (errno != ENOENT && errno != ENOTDIR)
199 cpp_error_from_errno (pfile, filename);
201 /* Create a negative node for this path, and return null. */
207 /* Place the file referenced by INC into a new buffer on PFILE's
208 stack. If there are errors, or the file should not be re-included,
209 a null buffer is pushed. */
212 stack_include_file (pfile, inc)
214 struct include_file *inc;
216 const char *filename = 0;
217 unsigned int lineno = 0;
222 filename = pfile->buffer->nominal_fname;
223 lineno = pfile->buffer->lineno;
228 read_include_file (pfile, inc);
230 /* Push a null buffer. */
231 fp = cpp_push_buffer (pfile, NULL, 0);
233 fp->nominal_fname = inc->name;
234 fp->buf = inc->buffer;
235 fp->rlimit = fp->buf;
236 if (! DO_NOT_REREAD (inc))
237 fp->rlimit += inc->st.st_size;
239 fp->line_base = fp->buf;
240 fp->lineno = 0; /* For _cpp_do_file_change. */
243 /* The ->actual_dir field is only used when ignore_srcdir is not in effect;
245 if (!CPP_OPTION (pfile, ignore_srcdir))
246 fp->actual_dir = actual_directory (pfile, inc->name);
248 /* Initialise controlling macro state. */
249 pfile->mi_state = MI_OUTSIDE;
250 pfile->mi_cmacro = 0;
251 pfile->include_depth++;
252 pfile->input_stack_listing_current = 0;
254 _cpp_do_file_change (pfile, FC_ENTER, filename, lineno);
259 /* Read the file referenced by INC into the file cache.
261 If fd points to a plain file, we might be able to mmap it; we can
262 definitely allocate the buffer all at once. If fd is a pipe or
263 terminal, we can't do either. If fd is something weird, like a
264 block device or a directory, we don't want to read it at all.
266 Unfortunately, different systems use different st.st_mode values
267 for pipes: some have S_ISFIFO, some S_ISSOCK, some are buggy and
268 zero the entire struct stat except a couple fields. Hence we don't
269 even try to figure out what something is, except for plain files,
270 directories, and block devices.
272 FIXME: Flush file cache and try again if we run out of memory. */
275 read_include_file (pfile, inc)
277 struct include_file *inc;
279 ssize_t size, offset, count;
282 static int pagesize = -1;
285 if (DO_NOT_REREAD (inc))
288 if (S_ISREG (inc->st.st_mode))
290 /* off_t might have a wider range than ssize_t - in other words,
291 the max size of a file might be bigger than the address
292 space. We can't handle a file that large. (Anyone with
293 a single source file bigger than 2GB needs to rethink
294 their coding style.) Some systems (e.g. AIX 4.1) define
295 SSIZE_MAX to be much smaller than the actual range of the
296 type. Use INTTYPE_MAXIMUM unconditionally to ensure this
298 if (inc->st.st_size > INTTYPE_MAXIMUM (ssize_t))
300 cpp_error (pfile, "%s is too large", inc->name);
303 size = inc->st.st_size;
308 pagesize = getpagesize ();
310 if (size / pagesize >= MMAP_THRESHOLD)
312 buf = (U_CHAR *) mmap (0, size, PROT_READ, MAP_PRIVATE, inc->fd, 0);
313 if (buf == (U_CHAR *)-1)
320 buf = (U_CHAR *) xmalloc (size);
322 while (offset < size)
324 count = read (inc->fd, buf + offset, size - offset);
329 cpp_warning (pfile, "%s is shorter than expected", inc->name);
336 else if (S_ISBLK (inc->st.st_mode))
338 cpp_error (pfile, "%s is a block device", inc->name);
341 else if (S_ISDIR (inc->st.st_mode))
343 cpp_error (pfile, "%s is a directory", inc->name);
348 /* 8 kilobytes is a sensible starting size. It ought to be
349 bigger than the kernel pipe buffer, and it's definitely
350 bigger than the majority of C source files. */
353 buf = (U_CHAR *) xmalloc (size);
355 while ((count = read (inc->fd, buf + offset, size - offset)) > 0)
359 buf = xrealloc (buf, (size *= 2));
365 buf = xrealloc (buf, offset);
366 inc->st.st_size = offset;
375 cpp_error_from_errno (pfile, inc->name);
377 /* Do not try to read this file again. */
380 inc->cmacro = NEVER_REREAD;
386 struct include_file *inc;
392 munmap ((PTR) inc->buffer, inc->st.st_size);
395 free ((PTR) inc->buffer);
400 /* Return 1 if the file named by FNAME has been included before in
401 any context, 0 otherwise. */
403 cpp_included (pfile, fname)
407 struct file_name_list *path;
413 /* Just look it up. */
414 nd = splay_tree_lookup (pfile->all_include_files, (splay_tree_key) fname);
415 return (nd && nd->value);
418 /* Search directory path for the file. */
419 name = (char *) alloca (strlen (fname) + pfile->max_include_len
420 + 2 + INCLUDE_LEN_FUDGE);
421 for (path = CPP_OPTION (pfile, quote_include); path; path = path->next)
423 memcpy (name, path->name, path->nlen);
424 name[path->nlen] = '/';
425 strcpy (&name[path->nlen+1], fname);
426 _cpp_simplify_pathname (name);
427 if (CPP_OPTION (pfile, remap))
428 name = remap_filename (pfile, name, path);
430 nd = splay_tree_lookup (pfile->all_include_files, (splay_tree_key) name);
437 /* Search for include file FNAME in the include chain starting at
438 SEARCH_START. Return 0 if there is no such file (or it's un-openable),
439 otherwise an include_file structure. */
441 static struct include_file *
442 find_include_file (pfile, fname, search_start)
445 struct file_name_list *search_start;
447 struct file_name_list *path;
449 struct include_file *file;
452 return open_file (pfile, fname);
454 /* Search directory path for the file. */
455 name = (char *) alloca (strlen (fname) + pfile->max_include_len
456 + 2 + INCLUDE_LEN_FUDGE);
457 for (path = search_start; path; path = path->next)
459 memcpy (name, path->name, path->nlen);
460 name[path->nlen] = '/';
461 strcpy (&name[path->nlen+1], fname);
462 _cpp_simplify_pathname (name);
463 if (CPP_OPTION (pfile, remap))
464 name = remap_filename (pfile, name, path);
466 file = open_file (pfile, name);
469 file->sysp = path->sysp;
470 file->foundhere = path;
477 /* #line uses this to save artificial file names. We have to stat the
478 file because an all_include_files entry is always either + or -,
479 there's no 'I don't know' value. */
481 _cpp_fake_include (pfile, fname)
486 struct include_file *file;
489 file = find_include_file (pfile, fname, CPP_OPTION (pfile, quote_include));
500 name = xstrdup (fname);
501 _cpp_simplify_pathname (name);
503 /* We cannot just blindly insert a node, because there's still the
504 chance that the node already exists but isn't on the search path. */
505 nd = splay_tree_lookup (pfile->all_include_files, (splay_tree_key) name);
509 return (const char *) nd->key;
512 file = xcnew (struct include_file);
515 splay_tree_insert (pfile->all_include_files, (splay_tree_key) name,
516 (splay_tree_value) file);
521 /* Not everyone who wants to set system-header-ness on a buffer can
522 see the details of struct include_file. This is an exported interface
523 because fix-header needs it. */
525 cpp_make_system_header (pfile, syshdr, externc)
531 /* 1 = system header, 2 = system header to be treated as C. */
533 flags = 1 + (externc != 0);
534 pfile->buffer->inc->sysp = flags;
537 /* Report on all files that might benefit from a multiple include guard.
540 _cpp_report_missing_guards (pfile)
544 splay_tree_foreach (pfile->all_include_files, report_missing_guard,
549 report_missing_guard (n, b)
553 struct include_file *f = (struct include_file *) n->value;
554 int *bannerp = (int *)b;
556 if (f && f->cmacro == 0 && f->include_count == 1)
560 fputs (_("Multiple include guards may be useful for:\n"), stderr);
563 fputs (f->name, stderr);
569 #define PRINT_THIS_DEP(p, b) (CPP_PRINT_DEPS(p) > (b||p->system_include_depth))
571 _cpp_execute_include (pfile, header, no_reinclude, search_start)
573 const cpp_token *header;
575 struct file_name_list *search_start;
577 unsigned int len = header->val.str.len;
578 unsigned int angle_brackets = header->type == CPP_HEADER_NAME;
579 struct include_file *inc;
582 /* Help protect #include or similar from recursion. */
583 if (pfile->buffer_stack_depth >= CPP_STACK_MAX)
585 cpp_fatal (pfile, "#include nested too deeply");
589 /* Check we've tidied up #include before entering the buffer. */
590 if (pfile->context->prev)
592 cpp_ice (pfile, "attempt to push file buffer with contexts stacked");
596 fname = alloca (len + 1);
597 memcpy (fname, header->val.str.text, len);
603 search_start = CPP_OPTION (pfile, bracket_include);
604 else if (CPP_OPTION (pfile, ignore_srcdir))
605 search_start = CPP_OPTION (pfile, quote_include);
607 search_start = CPP_BUFFER (pfile)->actual_dir;
611 cpp_error (pfile, "No include path in which to find %s", fname);
616 inc = find_include_file (pfile, fname, search_start);
620 /* For -M, add the file to the dependencies on its first inclusion. */
621 if (!inc->include_count && PRINT_THIS_DEP (pfile, angle_brackets))
622 deps_add_dep (pfile->deps, inc->name);
623 inc->include_count++;
625 /* Actually process the file. */
626 stack_include_file (pfile, inc);
629 pfile->system_include_depth++;
631 if (! DO_NOT_REREAD (inc))
634 inc->cmacro = NEVER_REREAD;
636 /* Handle -H option. */
637 if (CPP_OPTION (pfile, print_include_names))
639 cpp_buffer *fp = CPP_BUFFER (pfile);
640 while ((fp = CPP_PREV_BUFFER (fp)) != NULL)
642 fprintf (stderr, " %s\n", inc->name);
649 if (CPP_OPTION (pfile, print_deps_missing_files)
650 && PRINT_THIS_DEP (pfile, angle_brackets))
653 deps_add_dep (pfile->deps, fname);
657 struct file_name_list *ptr;
658 /* If requested as a system header, assume it belongs in
659 the first system header directory. */
660 if (CPP_OPTION (pfile, bracket_include))
661 ptr = CPP_OPTION (pfile, bracket_include);
663 ptr = CPP_OPTION (pfile, quote_include);
665 p = (char *) alloca (strlen (ptr->name)
666 + strlen (fname) + 2);
667 if (*ptr->name != '\0')
669 strcpy (p, ptr->name);
673 _cpp_simplify_pathname (p);
674 deps_add_dep (pfile->deps, p);
677 /* If -M was specified, and this header file won't be added to
678 the dependency list, then don't count this as an error,
679 because we can still produce correct output. Otherwise, we
680 can't produce correct output, because there may be
681 dependencies we need inside the missing file, and we don't
682 know what directory this missing file exists in. */
683 else if (CPP_PRINT_DEPS (pfile)
684 && ! PRINT_THIS_DEP (pfile, angle_brackets))
685 cpp_warning (pfile, "No include path in which to find %s", fname);
687 cpp_error_from_errno (pfile, fname);
690 /* Locate file F, and determine whether it is newer than PFILE. Return -1,
691 if F cannot be located or dated, 1, if it is newer and 0 if older. */
693 _cpp_compare_file_date (pfile, f)
697 unsigned int len = f->val.str.len;
699 struct file_name_list *search_start;
700 struct include_file *inc;
702 if (f->type == CPP_HEADER_NAME)
703 search_start = CPP_OPTION (pfile, bracket_include);
704 else if (CPP_OPTION (pfile, ignore_srcdir))
705 search_start = CPP_OPTION (pfile, quote_include);
707 search_start = CPP_BUFFER (pfile)->actual_dir;
709 fname = alloca (len + 1);
710 memcpy (fname, f->val.str.text, len);
712 inc = find_include_file (pfile, fname, search_start);
722 return inc->st.st_mtime > CPP_BUFFER (pfile)->inc->st.st_mtime;
726 /* Push an input buffer and load it up with the contents of FNAME.
727 If FNAME is "" or NULL, read standard input. */
729 _cpp_read_file (pfile, fname)
733 struct include_file *f;
738 f = open_file (pfile, fname);
742 cpp_error_from_errno (pfile, fname);
746 stack_include_file (pfile, f);
750 /* Do appropriate cleanup when a file buffer is popped off the input
753 _cpp_pop_file_buffer (pfile, buf)
757 struct include_file *inc = buf->inc;
759 if (pfile->system_include_depth)
760 pfile->system_include_depth--;
761 if (pfile->include_depth)
762 pfile->include_depth--;
763 pfile->input_stack_listing_current = 0;
765 /* Record the inclusion-preventing macro and its definedness. */
766 if (pfile->mi_state == MI_OUTSIDE && inc->cmacro != NEVER_REREAD)
768 /* This could be NULL meaning no controlling macro. */
769 inc->cmacro = pfile->mi_cmacro;
773 /* Invalidate control macros in the #including file. */
774 pfile->mi_state = MI_FAILED;
777 if (inc->refcnt == 0 && DO_NOT_REREAD (inc))
781 /* The file_name_map structure holds a mapping of file names for a
782 particular directory. This mapping is read from the file named
783 FILE_NAME_MAP_FILE in that directory. Such a file can be used to
784 map filenames on a file system with severe filename restrictions,
785 such as DOS. The format of the file name map file is just a series
786 of lines with two tokens on each line. The first token is the name
787 to map, and the second token is the actual name to use. */
791 struct file_name_map *map_next;
796 #define FILE_NAME_MAP_FILE "header.gcc"
798 /* Read a space delimited string of unlimited length from a stdio
802 read_filename_string (ch, f)
810 set = alloc = xmalloc (len + 1);
814 while ((ch = getc (f)) != EOF && ! is_space(ch))
816 if (set - alloc == len)
819 alloc = xrealloc (alloc, len + 1);
820 set = alloc + len / 2;
830 /* This structure holds a linked list of file name maps, one per directory. */
832 struct file_name_map_list
834 struct file_name_map_list *map_list_next;
836 struct file_name_map *map_list_map;
839 /* Read the file name map file for DIRNAME. */
841 static struct file_name_map *
842 read_name_map (pfile, dirname)
846 register struct file_name_map_list *map_list_ptr;
850 for (map_list_ptr = CPP_OPTION (pfile, map_list); map_list_ptr;
851 map_list_ptr = map_list_ptr->map_list_next)
852 if (! strcmp (map_list_ptr->map_list_name, dirname))
853 return map_list_ptr->map_list_map;
855 map_list_ptr = ((struct file_name_map_list *)
856 xmalloc (sizeof (struct file_name_map_list)));
857 map_list_ptr->map_list_name = xstrdup (dirname);
858 map_list_ptr->map_list_map = NULL;
860 name = (char *) alloca (strlen (dirname) + strlen (FILE_NAME_MAP_FILE) + 2);
861 strcpy (name, dirname);
864 strcat (name, FILE_NAME_MAP_FILE);
865 f = fopen (name, "r");
867 map_list_ptr->map_list_map = (struct file_name_map *)-1;
871 int dirlen = strlen (dirname);
873 while ((ch = getc (f)) != EOF)
876 struct file_name_map *ptr;
880 from = read_filename_string (ch, f);
881 while ((ch = getc (f)) != EOF && is_hspace(ch))
883 to = read_filename_string (ch, f);
885 ptr = ((struct file_name_map *)
886 xmalloc (sizeof (struct file_name_map)));
887 ptr->map_from = from;
889 /* Make the real filename absolute. */
894 ptr->map_to = xmalloc (dirlen + strlen (to) + 2);
895 strcpy (ptr->map_to, dirname);
896 ptr->map_to[dirlen] = '/';
897 strcpy (ptr->map_to + dirlen + 1, to);
901 ptr->map_next = map_list_ptr->map_list_map;
902 map_list_ptr->map_list_map = ptr;
904 while ((ch = getc (f)) != '\n')
911 map_list_ptr->map_list_next = CPP_OPTION (pfile, map_list);
912 CPP_OPTION (pfile, map_list) = map_list_ptr;
914 return map_list_ptr->map_list_map;
917 /* Remap NAME based on the file_name_map (if any) for LOC. */
920 remap_filename (pfile, name, loc)
923 struct file_name_list *loc;
925 struct file_name_map *map;
926 const char *from, *p, *dir;
929 loc->name_map = read_name_map (pfile,
933 if (loc->name_map == (struct file_name_map *)-1)
936 from = name + strlen (loc->name) + 1;
938 for (map = loc->name_map; map; map = map->map_next)
939 if (!strcmp (map->map_from, from))
942 /* Try to find a mapping file for the particular directory we are
943 looking in. Thus #include <sys/types.h> will look up sys/types.h
944 in /usr/include/header.gcc and look up types.h in
945 /usr/include/sys/header.gcc. */
946 p = strrchr (name, '/');
950 && strlen (loc->name) == (size_t) (p - name)
951 && !strncmp (loc->name, name, p - name))
952 /* FILENAME is in SEARCHPTR, which we've already checked. */
962 char * newdir = (char *) alloca (p - name + 1);
963 memcpy (newdir, name, p - name);
964 newdir[p - name] = '\0';
969 for (map = read_name_map (pfile, dir); map; map = map->map_next)
970 if (! strcmp (map->map_from, name))
976 /* Given a path FNAME, extract the directory component and place it
977 onto the actual_dirs list. Return a pointer to the allocated
978 file_name_list structure. These structures are used to implement
979 current-directory "" include searching. */
981 static struct file_name_list *
982 actual_directory (pfile, fname)
986 char *last_slash, *dir;
988 struct file_name_list *x;
990 dir = xstrdup (fname);
991 last_slash = strrchr (dir, '/');
994 if (last_slash == dir)
997 last_slash[1] = '\0';
1001 dlen = last_slash - dir;
1008 dir = xstrdup (".");
1012 if (dlen > pfile->max_include_len)
1013 pfile->max_include_len = dlen;
1015 for (x = pfile->actual_dirs; x; x = x->alloc)
1016 if (!strcmp (x->name, dir))
1022 /* Not found, make a new one. */
1023 x = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
1026 x->next = CPP_OPTION (pfile, quote_include);
1027 x->alloc = pfile->actual_dirs;
1028 x->sysp = CPP_BUFFER (pfile)->inc->sysp;
1031 pfile->actual_dirs = x;
1035 /* Simplify a path name in place, deleting redundant components. This
1036 reduces OS overhead and guarantees that equivalent paths compare
1037 the same (modulo symlinks).
1040 foo/bar/../quux foo/quux
1044 //quux //quux (POSIX allows leading // as a namespace escape)
1046 Guarantees no trailing slashes. All transforms reduce the length
1050 _cpp_simplify_pathname (path)
1057 #if defined (HAVE_DOS_BASED_FILE_SYSTEM)
1058 /* Convert all backslashes to slashes. */
1059 for (from = path; *from; from++)
1060 if (*from == '\\') *from = '/';
1062 /* Skip over leading drive letter if present. */
1063 if (ISALPHA (path[0]) && path[1] == ':')
1064 from = to = &path[2];
1071 /* Remove redundant initial /s. */
1080 /* 3 or more initial /s are equivalent to 1 /. */
1081 while (*++from == '/');
1083 /* On some hosts // differs from /; Posix allows this. */
1091 while (*from == '/')
1094 if (from[0] == '.' && from[1] == '/')
1096 else if (from[0] == '.' && from[1] == '\0')
1098 else if (from[0] == '.' && from[1] == '.' && from[2] == '/')
1115 while (to > base && *to != '/') to--;
1121 else if (from[0] == '.' && from[1] == '.' && from[2] == '\0')
1134 while (to > base && *to != '/') to--;
1141 /* Copy this component and trailing /, if any. */
1142 while ((*to++ = *from++) != '/')
1154 /* Trim trailing slash */
1155 if (to[0] == '/' && (!absolute || to > path+1))
1158 /* Change the empty string to "." so that stat() on the result
1159 will always work. */
1168 /* It is not clear when this should be used if at all, so I've
1169 disabled it until someone who understands VMS can look at it. */
1172 /* Under VMS we need to fix up the "include" specification filename.
1174 Rules for possible conversions
1176 fullname tried paths
1179 ./dir/name [.dir]name
1181 /name [000000]name, name
1182 dir/name dir:[000000]name, dir:name, dir/name
1183 dir1/dir2/name dir1:[dir2]name, dir1:[000000.dir2]name
1184 path:/name path:[000000]name, path:name
1185 path:/dir/name path:[000000.dir]name, path:[dir]name
1186 path:dir/name path:[dir]name
1187 [path]:[dir]name [path.dir]name
1188 path/[dir]name [path.dir]name
1190 The path:/name input is constructed when expanding <> includes. */
1194 hack_vms_include_specification (fullname)
1197 register char *basename, *unixname, *local_ptr, *first_slash;
1198 int f, check_filename_before_returning, must_revert;
1201 check_filename_before_returning = 0;
1203 /* See if we can find a 1st slash. If not, there's no path information. */
1204 first_slash = strchr (fullname, '/');
1205 if (first_slash == 0)
1206 return 0; /* Nothing to do!!! */
1208 /* construct device spec if none given. */
1210 if (strchr (fullname, ':') == 0)
1213 /* If fullname has a slash, take it as device spec. */
1215 if (first_slash == fullname)
1217 first_slash = strchr (fullname + 1, '/'); /* 2nd slash ? */
1219 *first_slash = ':'; /* make device spec */
1220 for (basename = fullname; *basename != 0; basename++)
1221 *basename = *(basename+1); /* remove leading slash */
1223 else if ((first_slash[-1] != '.') /* keep ':/', './' */
1224 && (first_slash[-1] != ':')
1225 && (first_slash[-1] != ']')) /* or a vms path */
1229 else if ((first_slash[1] == '[') /* skip './' in './[dir' */
1230 && (first_slash[-1] == '.'))
1234 /* Get part after first ':' (basename[-1] == ':')
1235 or last '/' (basename[-1] == '/'). */
1237 basename = base_name (fullname);
1239 local_ptr = Local; /* initialize */
1241 /* We are trying to do a number of things here. First of all, we are
1242 trying to hammer the filenames into a standard format, such that later
1243 processing can handle them.
1245 If the file name contains something like [dir.], then it recognizes this
1246 as a root, and strips the ".]". Later processing will add whatever is
1247 needed to get things working properly.
1249 If no device is specified, then the first directory name is taken to be
1250 a device name (or a rooted logical). */
1252 /* Point to the UNIX filename part (which needs to be fixed!)
1253 but skip vms path information.
1254 [basename != fullname since first_slash != 0]. */
1256 if ((basename[-1] == ':') /* vms path spec. */
1257 || (basename[-1] == ']')
1258 || (basename[-1] == '>'))
1259 unixname = basename;
1261 unixname = fullname;
1263 if (*unixname == '/')
1266 /* If the directory spec is not rooted, we can just copy
1267 the UNIX filename part and we are done. */
1269 if (((basename - fullname) > 1)
1270 && ( (basename[-1] == ']')
1271 || (basename[-1] == '>')))
1273 if (basename[-2] != '.')
1276 /* The VMS part ends in a `]', and the preceding character is not a `.'.
1277 -> PATH]:/name (basename = '/name', unixname = 'name')
1278 We strip the `]', and then splice the two parts of the name in the
1279 usual way. Given the default locations for include files,
1280 we will only use this code if the user specifies alternate locations
1281 with the /include (-I) switch on the command line. */
1283 basename -= 1; /* Strip "]" */
1284 unixname--; /* backspace */
1289 /* The VMS part has a ".]" at the end, and this will not do. Later
1290 processing will add a second directory spec, and this would be a syntax
1291 error. Thus we strip the ".]", and thus merge the directory specs.
1292 We also backspace unixname, so that it points to a '/'. This inhibits the
1293 generation of the 000000 root directory spec (which does not belong here
1296 basename -= 2; /* Strip ".]" */
1297 unixname--; /* backspace */
1305 /* We drop in here if there is no VMS style directory specification yet.
1306 If there is no device specification either, we make the first dir a
1307 device and try that. If we do not do this, then we will be essentially
1308 searching the users default directory (as if they did a #include "asdf.h").
1310 Then all we need to do is to push a '[' into the output string. Later
1311 processing will fill this in, and close the bracket. */
1313 if ((unixname != fullname) /* vms path spec found. */
1314 && (basename[-1] != ':'))
1315 *local_ptr++ = ':'; /* dev not in spec. take first dir */
1317 *local_ptr++ = '['; /* Open the directory specification */
1320 if (unixname == fullname) /* no vms dir spec. */
1323 if ((first_slash != 0) /* unix dir spec. */
1324 && (*unixname != '/') /* not beginning with '/' */
1325 && (*unixname != '.')) /* or './' or '../' */
1326 *local_ptr++ = '.'; /* dir is local ! */
1329 /* at this point we assume that we have the device spec, and (at least
1330 the opening "[" for a directory specification. We may have directories
1333 If there are no other slashes then the filename will be
1334 in the "root" directory. Otherwise, we need to add
1335 directory specifications. */
1337 if (strchr (unixname, '/') == 0)
1339 /* if no directories specified yet and none are following. */
1340 if (local_ptr[-1] == '[')
1342 /* Just add "000000]" as the directory string */
1343 strcpy (local_ptr, "000000]");
1344 local_ptr += strlen (local_ptr);
1345 check_filename_before_returning = 1; /* we might need to fool with this later */
1351 /* As long as there are still subdirectories to add, do them. */
1352 while (strchr (unixname, '/') != 0)
1354 /* If this token is "." we can ignore it
1355 if it's not at the beginning of a path. */
1356 if ((unixname[0] == '.') && (unixname[1] == '/'))
1358 /* remove it at beginning of path. */
1359 if ( ((unixname == fullname) /* no device spec */
1360 && (fullname+2 != basename)) /* starts with ./ */
1362 || ((basename[-1] == ':') /* device spec */
1363 && (unixname-1 == basename))) /* and ./ afterwards */
1364 *local_ptr++ = '.'; /* make '[.' start of path. */
1369 /* Add a subdirectory spec. Do not duplicate "." */
1370 if ( local_ptr[-1] != '.'
1371 && local_ptr[-1] != '['
1372 && local_ptr[-1] != '<')
1375 /* If this is ".." then the spec becomes "-" */
1376 if ( (unixname[0] == '.')
1377 && (unixname[1] == '.')
1378 && (unixname[2] == '/'))
1380 /* Add "-" and skip the ".." */
1381 if ((local_ptr[-1] == '.')
1382 && (local_ptr[-2] == '['))
1383 local_ptr--; /* prevent [.- */
1389 /* Copy the subdirectory */
1390 while (*unixname != '/')
1391 *local_ptr++= *unixname++;
1393 unixname++; /* Skip the "/" */
1396 /* Close the directory specification */
1397 if (local_ptr[-1] == '.') /* no trailing periods */
1400 if (local_ptr[-1] == '[') /* no dir needed */
1406 /* Now add the filename. */
1409 *local_ptr++ = *unixname++;
1412 /* Now append it to the original VMS spec. */
1414 strcpy ((must_revert==1)?fullname:basename, Local);
1416 /* If we put a [000000] in the filename, try to open it first. If this fails,
1417 remove the [000000], and return that name. This provides flexibility
1418 to the user in that they can use both rooted and non-rooted logical names
1419 to point to the location of the file. */
1421 if (check_filename_before_returning)
1423 f = open (fullname, O_RDONLY|O_NONBLOCK);
1426 /* The file name is OK as it is, so return it as is. */
1431 /* The filename did not work. Try to remove the [000000] from the name,
1434 basename = strchr (fullname, '[');
1435 local_ptr = strchr (fullname, ']') + 1;
1436 strcpy (basename, local_ptr); /* this gets rid of it */