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. */
31 static IHASH
*redundant_include_p
PARAMS ((cpp_reader
*, IHASH
*,
32 struct file_name_list
*));
33 static IHASH
*make_IHASH
PARAMS ((const char *, const char *,
34 struct file_name_list
*,
35 unsigned int, IHASH
**));
36 static struct file_name_map
*read_name_map
37 PARAMS ((cpp_reader
*, const char *));
38 static char *read_filename_string
PARAMS ((int, FILE *));
39 static char *remap_filename
PARAMS ((cpp_reader
*, char *,
40 struct file_name_list
*));
41 static struct file_name_list
*actual_directory
42 PARAMS ((cpp_reader
*, const char *));
43 static unsigned int hash_IHASH
PARAMS ((const void *));
44 static int eq_IHASH
PARAMS ((const void *, const void *));
45 static int file_cleanup
PARAMS ((cpp_buffer
*, cpp_reader
*));
46 static int find_include_file
PARAMS ((cpp_reader
*, const char *,
47 struct file_name_list
*,
49 static int read_include_file
PARAMS ((cpp_reader
*, int, IHASH
*));
50 static inline int open_include_file
PARAMS ((cpp_reader
*, const char *));
53 static void hack_vms_include_specification
PARAMS ((char *));
56 /* Initial size of include hash table. */
59 #ifndef INCLUDE_LEN_FUDGE
60 #define INCLUDE_LEN_FUDGE 0
63 /* Calculate hash of an IHASH entry. */
68 const IHASH
*i
= (const IHASH
*)x
;
72 /* Compare an existing IHASH structure with a potential one. */
78 const U_CHAR
*a
= ((const IHASH
*)x
)->nshort
;
79 const U_CHAR
*b
= ((const IHASH
*)y
)->nshort
;
80 return !strcmp (a
, b
);
83 /* Init the hash table. In here so it can see the hash and eq functions. */
85 _cpp_init_include_hash (pfile
)
88 pfile
->all_include_files
89 = htab_create (IHASHSIZE
, hash_IHASH
, eq_IHASH
, free
);
92 /* Return 0 if the file pointed to by IHASH has never been included before,
93 -1 if it has been included before and need not be again,
94 or a pointer to an IHASH entry which is the file to be reread.
95 "Never before" is with respect to the position in ILIST.
97 This will not detect redundancies involving odd uses of the
98 `current directory' rule for "" includes. They aren't quite
99 pathological, but I think they are rare enough not to worry about.
100 The simplest example is:
109 and the problem is that for `current directory' includes,
110 ihash->foundhere is not on any of the global include chains,
111 so the test below (i->foundhere == l) may be false even when
112 the directories are in fact the same. */
115 redundant_include_p (pfile
, ihash
, ilist
)
118 struct file_name_list
*ilist
;
120 struct file_name_list
*l
;
123 if (! ihash
->foundhere
)
126 for (i
= ihash
; i
; i
= i
->next_this_file
)
127 for (l
= ilist
; l
; l
= l
->next
)
128 if (i
->foundhere
== l
)
129 /* The control_macro works like this: If it's NULL, the file
130 is to be included again. If it's "", the file is never to
131 be included again. If it's a string, the file is not to be
132 included again if the string is the name of a defined macro. */
133 return (i
->control_macro
134 && (i
->control_macro
[0] == '\0'
135 || cpp_defined (pfile
, i
->control_macro
, -1)))
141 /* Return 1 if the file named by FNAME has been included before in
142 any context, 0 otherwise. */
144 cpp_included (pfile
, fname
)
149 dummy
.nshort
= fname
;
150 dummy
.hash
= _cpp_calc_hash (fname
, strlen (fname
));
151 ptr
= htab_find_with_hash (pfile
->all_include_files
,
152 (const void *)&dummy
, dummy
.hash
);
153 return (ptr
!= NULL
);
156 /* Create an IHASH entry and insert it in SLOT. */
158 make_IHASH (name
, fname
, path
, hash
, slot
)
159 const char *name
, *fname
;
160 struct file_name_list
*path
;
165 if (path
== ABSOLUTE_PATH
)
167 ih
= (IHASH
*) xmalloc (sizeof (IHASH
) + strlen (name
));
168 ih
->nshort
= ih
->name
;
174 if ((s
= strstr (name
, fname
)) != NULL
)
176 ih
= (IHASH
*) xmalloc (sizeof (IHASH
) + strlen (name
));
177 ih
->nshort
= ih
->name
+ (s
- name
);
181 ih
= (IHASH
*) xmalloc (sizeof (IHASH
) + strlen (name
)
182 + strlen (fname
) + 1);
183 ih
->nshort
= ih
->name
+ strlen (name
) + 1;
184 strcpy ((char *)ih
->nshort
, fname
);
187 strcpy ((char *)ih
->name
, name
);
188 ih
->foundhere
= path
;
189 ih
->control_macro
= NULL
;
191 ih
->next_this_file
= *slot
;
197 file_cleanup (pbuf
, pfile
)
202 free ((PTR
) pbuf
->buf
);
203 if (pfile
->system_include_depth
)
204 pfile
->system_include_depth
--;
205 if (pfile
->potential_control_macro
)
207 pbuf
->ihash
->control_macro
= pfile
->potential_control_macro
;
208 pfile
->potential_control_macro
= 0;
210 pfile
->input_stack_listing_current
= 0;
214 /* Centralize calls to open(2) here. This provides a hook for future
215 changes which might, e.g. look for and open a precompiled version
216 of the header. It also means all the magic currently associated
217 with calling open is in one place, and if we ever need more, it'll
220 Open files in nonblocking mode, so we don't get stuck if someone
221 clever has asked cpp to process /dev/rmt0. read_include_file
222 will check that we have a real file to work with. Also take care
223 not to acquire a controlling terminal by mistake (this can't happen
224 on sane systems, but paranoia is a virtue).
226 Use the three-argument form of open even though we aren't
227 specifying O_CREAT, to defend against broken system headers. */
230 open_include_file (pfile
, filename
)
231 cpp_reader
*pfile ATTRIBUTE_UNUSED
;
232 const char *filename
;
234 return open (filename
, O_RDONLY
|O_NONBLOCK
|O_NOCTTY
, 0666);
237 /* Search for include file FNAME in the include chain starting at
238 SEARCH_START. Return -2 if this file doesn't need to be included
239 (because it was included already and it's marked idempotent),
240 -1 if an error occurred, or a file descriptor open on the file.
241 *IHASH is set to point to the include hash entry for this file, and
242 *BEFORE is set to 1 if the file was included before (but needs to be read
245 find_include_file (pfile
, fname
, search_start
, ihash
, before
)
248 struct file_name_list
*search_start
;
252 struct file_name_list
*path
;
258 dummy
.nshort
= fname
;
259 dummy
.hash
= _cpp_calc_hash (fname
, strlen (fname
));
260 path
= (fname
[0] == '/') ? ABSOLUTE_PATH
: search_start
;
261 slot
= (IHASH
**) htab_find_slot_with_hash (pfile
->all_include_files
,
262 (const void *) &dummy
,
265 if (*slot
&& (ih
= redundant_include_p (pfile
, *slot
, path
)))
267 if (ih
== (IHASH
*)-1)
272 return open_include_file (pfile
, ih
->name
);
275 if (path
== ABSOLUTE_PATH
)
277 name
= (char *) fname
;
278 f
= open_include_file (pfile
, name
);
282 /* Search directory path, trying to open the file. */
283 name
= (char *) alloca (strlen (fname
) + pfile
->max_include_len
284 + 2 + INCLUDE_LEN_FUDGE
);
287 memcpy (name
, path
->name
, path
->nlen
);
288 name
[path
->nlen
] = '/';
289 strcpy (&name
[path
->nlen
+1], fname
);
290 _cpp_simplify_pathname (name
);
291 if (CPP_OPTION (pfile
, remap
))
292 name
= remap_filename (pfile
, name
, path
);
294 f
= open_include_file (pfile
, name
);
296 if (f
== -1 && errno
== EACCES
)
299 "included file `%s' exists but is not readable",
313 ih
= make_IHASH (name
, fname
, path
, dummy
.hash
, slot
);
319 /* Create a dummy IHASH entry for FNAME, and return its name pointer.
320 This is used by #line. */
322 _cpp_fake_ihash (pfile
, fname
)
329 dummy
.nshort
= fname
;
330 dummy
.hash
= _cpp_calc_hash (fname
, strlen (fname
));
331 slot
= (IHASH
**) htab_find_slot_with_hash (pfile
->all_include_files
,
332 (const void *) &dummy
,
335 return (*slot
)->name
;
336 ih
= make_IHASH (fname
, 0, ABSOLUTE_PATH
, dummy
.hash
, slot
);
341 /* The file_name_map structure holds a mapping of file names for a
342 particular directory. This mapping is read from the file named
343 FILE_NAME_MAP_FILE in that directory. Such a file can be used to
344 map filenames on a file system with severe filename restrictions,
345 such as DOS. The format of the file name map file is just a series
346 of lines with two tokens on each line. The first token is the name
347 to map, and the second token is the actual name to use. */
351 struct file_name_map
*map_next
;
356 #define FILE_NAME_MAP_FILE "header.gcc"
358 /* Read a space delimited string of unlimited length from a stdio
362 read_filename_string (ch
, f
)
370 set
= alloc
= xmalloc (len
+ 1);
374 while ((ch
= getc (f
)) != EOF
&& ! is_space(ch
))
376 if (set
- alloc
== len
)
379 alloc
= xrealloc (alloc
, len
+ 1);
380 set
= alloc
+ len
/ 2;
390 /* This structure holds a linked list of file name maps, one per directory. */
392 struct file_name_map_list
394 struct file_name_map_list
*map_list_next
;
396 struct file_name_map
*map_list_map
;
399 /* Read the file name map file for DIRNAME. */
401 static struct file_name_map
*
402 read_name_map (pfile
, dirname
)
406 register struct file_name_map_list
*map_list_ptr
;
410 for (map_list_ptr
= CPP_OPTION (pfile
, map_list
); map_list_ptr
;
411 map_list_ptr
= map_list_ptr
->map_list_next
)
412 if (! strcmp (map_list_ptr
->map_list_name
, dirname
))
413 return map_list_ptr
->map_list_map
;
415 map_list_ptr
= ((struct file_name_map_list
*)
416 xmalloc (sizeof (struct file_name_map_list
)));
417 map_list_ptr
->map_list_name
= xstrdup (dirname
);
419 name
= (char *) alloca (strlen (dirname
) + strlen (FILE_NAME_MAP_FILE
) + 2);
420 strcpy (name
, dirname
);
423 strcat (name
, FILE_NAME_MAP_FILE
);
424 f
= fopen (name
, "r");
426 map_list_ptr
->map_list_map
= (struct file_name_map
*)-1;
430 int dirlen
= strlen (dirname
);
432 while ((ch
= getc (f
)) != EOF
)
435 struct file_name_map
*ptr
;
439 from
= read_filename_string (ch
, f
);
440 while ((ch
= getc (f
)) != EOF
&& is_hspace(ch
))
442 to
= read_filename_string (ch
, f
);
444 ptr
= ((struct file_name_map
*)
445 xmalloc (sizeof (struct file_name_map
)));
446 ptr
->map_from
= from
;
448 /* Make the real filename absolute. */
453 ptr
->map_to
= xmalloc (dirlen
+ strlen (to
) + 2);
454 strcpy (ptr
->map_to
, dirname
);
455 ptr
->map_to
[dirlen
] = '/';
456 strcpy (ptr
->map_to
+ dirlen
+ 1, to
);
460 ptr
->map_next
= map_list_ptr
->map_list_map
;
461 map_list_ptr
->map_list_map
= ptr
;
463 while ((ch
= getc (f
)) != '\n')
470 map_list_ptr
->map_list_next
= CPP_OPTION (pfile
, map_list
);
471 CPP_OPTION (pfile
, map_list
) = map_list_ptr
;
473 return map_list_ptr
->map_list_map
;
476 /* Remap NAME based on the file_name_map (if any) for LOC. */
479 remap_filename (pfile
, name
, loc
)
482 struct file_name_list
*loc
;
484 struct file_name_map
*map
;
485 const char *from
, *p
, *dir
;
488 loc
->name_map
= read_name_map (pfile
,
492 if (loc
->name_map
== (struct file_name_map
*)-1)
495 from
= name
+ strlen (loc
->name
) + 1;
497 for (map
= loc
->name_map
; map
; map
= map
->map_next
)
498 if (!strcmp (map
->map_from
, from
))
501 /* Try to find a mapping file for the particular directory we are
502 looking in. Thus #include <sys/types.h> will look up sys/types.h
503 in /usr/include/header.gcc and look up types.h in
504 /usr/include/sys/header.gcc. */
505 p
= strrchr (name
, '/');
509 && strlen (loc
->name
) == (size_t) (p
- name
)
510 && !strncmp (loc
->name
, name
, p
- name
))
511 /* FILENAME is in SEARCHPTR, which we've already checked. */
521 char * newdir
= (char *) alloca (p
- name
+ 1);
522 memcpy (newdir
, name
, p
- name
);
523 newdir
[p
- name
] = '\0';
528 for (map
= read_name_map (pfile
, dir
); map
; map
= map
->map_next
)
529 if (! strcmp (map
->map_from
, name
))
537 _cpp_execute_include (pfile
, fname
, len
, no_reinclude
, search_start
)
542 struct file_name_list
*search_start
;
546 int angle_brackets
= fname
[0] == '<';
552 search_start
= CPP_OPTION (pfile
, bracket_include
);
553 else if (CPP_OPTION (pfile
, ignore_srcdir
))
554 search_start
= CPP_OPTION (pfile
, quote_include
);
556 search_start
= CPP_BUFFER (pfile
)->actual_dir
;
561 cpp_error (pfile
, "No include path in which to find %s", fname
);
565 /* Remove quote marks. */
570 fd
= find_include_file (pfile
, fname
, search_start
, &ihash
, &before
);
577 if (CPP_OPTION (pfile
, print_deps_missing_files
)
578 && CPP_PRINT_DEPS (pfile
) > (angle_brackets
||
579 (pfile
->system_include_depth
> 0)))
582 deps_add_dep (pfile
->deps
, fname
);
586 struct file_name_list
*ptr
;
587 /* If requested as a system header, assume it belongs in
588 the first system header directory. */
589 if (CPP_OPTION (pfile
, bracket_include
))
590 ptr
= CPP_OPTION (pfile
, bracket_include
);
592 ptr
= CPP_OPTION (pfile
, quote_include
);
594 p
= (char *) alloca (strlen (ptr
->name
)
595 + strlen (fname
) + 2);
596 if (*ptr
->name
!= '\0')
598 strcpy (p
, ptr
->name
);
602 deps_add_dep (pfile
->deps
, p
);
605 /* If -M was specified, and this header file won't be added to
606 the dependency list, then don't count this as an error,
607 because we can still produce correct output. Otherwise, we
608 can't produce correct output, because there may be
609 dependencies we need inside the missing file, and we don't
610 know what directory this missing file exists in. */
611 else if (CPP_PRINT_DEPS (pfile
)
612 && (CPP_PRINT_DEPS (pfile
)
613 <= (angle_brackets
|| (pfile
->system_include_depth
> 0))))
614 cpp_warning (pfile
, "No include path in which to find %s", fname
);
616 cpp_error_from_errno (pfile
, fname
);
621 /* For -M, add the file to the dependencies on its first inclusion. */
622 if (!before
&& (CPP_PRINT_DEPS (pfile
)
623 > (angle_brackets
|| (pfile
->system_include_depth
> 0))))
624 deps_add_dep (pfile
->deps
, ihash
->name
);
626 /* Handle -H option. */
627 if (CPP_OPTION (pfile
, print_include_names
))
629 cpp_buffer
*fp
= CPP_BUFFER (pfile
);
630 while ((fp
= CPP_PREV_BUFFER (fp
)) != NULL
)
632 fprintf (stderr
, " %s\n", ihash
->name
);
635 /* Actually process the file. */
637 ihash
->control_macro
= (const U_CHAR
*) "";
639 if (read_include_file (pfile
, fd
, ihash
))
642 pfile
->system_include_depth
++; /* Decremented in file_cleanup. */
647 /* Push an input buffer and load it up with the contents of FNAME.
648 If FNAME is "" or NULL, read standard input. */
650 cpp_read_file (pfile
, fname
)
661 dummy
.nshort
= fname
;
662 /* _cpp_calc_hash doesn't like zero-length strings. */
666 dummy
.hash
= _cpp_calc_hash (fname
, strlen (fname
));
667 slot
= (IHASH
**) htab_find_slot_with_hash (pfile
->all_include_files
,
668 (const void *) &dummy
,
670 if (*slot
&& (ih
= redundant_include_p (pfile
, *slot
, ABSOLUTE_PATH
)))
672 if (ih
== (IHASH
*) -1)
673 return 1; /* Already included. */
676 ih
= make_IHASH (fname
, 0, ABSOLUTE_PATH
, dummy
.hash
, slot
);
681 f
= open_include_file (pfile
, fname
);
683 return read_include_file (pfile
, f
, ih
);
686 /* Read the contents of FD into the buffer on the top of PFILE's stack.
687 IHASH points to the include hash entry for the file associated with
690 The caller is responsible for the cpp_push_buffer. */
693 read_include_file (pfile
, fd
, ihash
)
703 fp
= cpp_push_buffer (pfile
, NULL
, 0);
708 if (fstat (fd
, &st
) < 0)
710 if (fcntl (fd
, F_SETFL
, 0) == -1) /* turn off nonblocking mode */
713 /* If fd points to a plain file, we know how big it is, so we can
714 allocate the buffer all at once. If fd is a pipe or terminal, we
715 can't. Most C source files are 4k or less, so we guess that. If
716 fd is something weird, like a block device or a directory, we
717 don't want to read it at all.
719 Unfortunately, different systems use different st.st_mode values
720 for pipes: some have S_ISFIFO, some S_ISSOCK, some are buggy and
721 zero the entire struct stat except a couple fields. Hence the
724 In all cases, read_and_prescan will resize the buffer if it
725 turns out there's more data than we thought. */
727 if (S_ISREG (st
.st_mode
))
729 /* off_t might have a wider range than size_t - in other words,
730 the max size of a file might be bigger than the address
731 space. We can't handle a file that large. (Anyone with
732 a single source file bigger than 4GB needs to rethink
733 their coding style.) */
734 st_size
= (size_t) st
.st_size
;
735 if ((unsigned HOST_WIDEST_INT
) st_size
736 != (unsigned HOST_WIDEST_INT
) st
.st_size
)
738 cpp_error (pfile
, "file `%s' is too large", ihash
->name
);
742 else if (S_ISFIFO (st
.st_mode
) || S_ISSOCK (st
.st_mode
)
743 /* Permit any kind of character device: the sensible ones are
744 ttys and /dev/null, but weeding out the others is too hard. */
745 || S_ISCHR (st
.st_mode
)
746 /* Some 4.x (x<4) derivatives have a bug that makes fstat() of a
747 socket or pipe return a stat struct with most fields zeroed. */
748 || (st
.st_mode
== 0 && st
.st_nlink
== 0 && st
.st_size
== 0))
750 /* Cannot get its file size before reading. 4k is a decent
756 cpp_error (pfile
, "`%s' is not a file, pipe, or tty", ihash
->name
);
760 /* Read the file, converting end-of-line characters and trigraphs
763 fp
->nominal_fname
= ihash
->name
;
764 length
= _cpp_read_and_prescan (pfile
, fp
, fd
, st_size
);
768 ihash
->control_macro
= (const U_CHAR
*) ""; /* never re-include */
771 fp
->rlimit
= fp
->buf
+ length
;
773 if (ihash
->foundhere
!= ABSOLUTE_PATH
)
774 fp
->system_header_p
= ihash
->foundhere
->sysp
;
776 fp
->line_base
= fp
->buf
;
777 fp
->cleanup
= file_cleanup
;
779 /* The ->actual_dir field is only used when ignore_srcdir is not in effect;
781 if (!CPP_OPTION (pfile
, ignore_srcdir
))
782 fp
->actual_dir
= actual_directory (pfile
, ihash
->name
);
784 pfile
->input_stack_listing_current
= 0;
785 pfile
->only_seen_white
= 2;
789 cpp_error_from_errno (pfile
, ihash
->name
);
791 cpp_pop_buffer (pfile
);
797 /* Given a path FNAME, extract the directory component and place it
798 onto the actual_dirs list. Return a pointer to the allocated
799 file_name_list structure. These structures are used to implement
800 current-directory "" include searching. */
802 static struct file_name_list
*
803 actual_directory (pfile
, fname
)
807 char *last_slash
, *dir
;
809 struct file_name_list
*x
;
811 dir
= xstrdup (fname
);
812 last_slash
= strrchr (dir
, '/');
815 if (last_slash
== dir
)
818 last_slash
[1] = '\0';
822 dlen
= last_slash
- dir
;
833 if (dlen
> pfile
->max_include_len
)
834 pfile
->max_include_len
= dlen
;
836 for (x
= pfile
->actual_dirs
; x
; x
= x
->alloc
)
837 if (!strcmp (x
->name
, dir
))
843 /* Not found, make a new one. */
844 x
= (struct file_name_list
*) xmalloc (sizeof (struct file_name_list
));
847 x
->next
= CPP_OPTION (pfile
, quote_include
);
848 x
->alloc
= pfile
->actual_dirs
;
849 x
->sysp
= CPP_BUFFER (pfile
)->system_header_p
;
852 pfile
->actual_dirs
= x
;
856 /* Simplify a path name in place, deleting redundant components. This
857 reduces OS overhead and guarantees that equivalent paths compare
858 the same (modulo symlinks).
861 foo/bar/../quux foo/quux
865 //quux //quux (POSIX allows leading // as a namespace escape)
867 Guarantees no trailing slashes. All transforms reduce the length
871 _cpp_simplify_pathname (path
)
878 #if defined (HAVE_DOS_BASED_FILE_SYSTEM)
879 /* Convert all backslashes to slashes. */
880 for (from
= path
; *from
; from
++)
881 if (*from
== '\\') *from
= '/';
883 /* Skip over leading drive letter if present. */
884 if (ISALPHA (path
[0]) && path
[1] == ':')
885 from
= to
= &path
[2];
892 /* Remove redundant initial /s. */
901 /* 3 or more initial /s are equivalent to 1 /. */
902 while (*++from
== '/');
904 /* On some hosts // differs from /; Posix allows this. */
915 if (from
[0] == '.' && from
[1] == '/')
917 else if (from
[0] == '.' && from
[1] == '\0')
919 else if (from
[0] == '.' && from
[1] == '.' && from
[2] == '/')
936 while (to
> base
&& *to
!= '/') to
--;
942 else if (from
[0] == '.' && from
[1] == '.' && from
[2] == '\0')
955 while (to
> base
&& *to
!= '/') to
--;
962 /* Copy this component and trailing /, if any. */
963 while ((*to
++ = *from
++) != '/')
975 /* Trim trailing slash */
976 if (to
[0] == '/' && (!absolute
|| to
> path
+1))
979 /* Change the empty string to "." so that stat() on the result
989 /* It is not clear when this should be used if at all, so I've
990 disabled it until someone who understands VMS can look at it. */
993 /* Under VMS we need to fix up the "include" specification filename.
995 Rules for possible conversions
1000 ./dir/name [.dir]name
1002 /name [000000]name, name
1003 dir/name dir:[000000]name, dir:name, dir/name
1004 dir1/dir2/name dir1:[dir2]name, dir1:[000000.dir2]name
1005 path:/name path:[000000]name, path:name
1006 path:/dir/name path:[000000.dir]name, path:[dir]name
1007 path:dir/name path:[dir]name
1008 [path]:[dir]name [path.dir]name
1009 path/[dir]name [path.dir]name
1011 The path:/name input is constructed when expanding <> includes. */
1015 hack_vms_include_specification (fullname
)
1018 register char *basename
, *unixname
, *local_ptr
, *first_slash
;
1019 int f
, check_filename_before_returning
, must_revert
;
1022 check_filename_before_returning
= 0;
1024 /* See if we can find a 1st slash. If not, there's no path information. */
1025 first_slash
= strchr (fullname
, '/');
1026 if (first_slash
== 0)
1027 return 0; /* Nothing to do!!! */
1029 /* construct device spec if none given. */
1031 if (strchr (fullname
, ':') == 0)
1034 /* If fullname has a slash, take it as device spec. */
1036 if (first_slash
== fullname
)
1038 first_slash
= strchr (fullname
+ 1, '/'); /* 2nd slash ? */
1040 *first_slash
= ':'; /* make device spec */
1041 for (basename
= fullname
; *basename
!= 0; basename
++)
1042 *basename
= *(basename
+1); /* remove leading slash */
1044 else if ((first_slash
[-1] != '.') /* keep ':/', './' */
1045 && (first_slash
[-1] != ':')
1046 && (first_slash
[-1] != ']')) /* or a vms path */
1050 else if ((first_slash
[1] == '[') /* skip './' in './[dir' */
1051 && (first_slash
[-1] == '.'))
1055 /* Get part after first ':' (basename[-1] == ':')
1056 or last '/' (basename[-1] == '/'). */
1058 basename
= base_name (fullname
);
1060 local_ptr
= Local
; /* initialize */
1062 /* We are trying to do a number of things here. First of all, we are
1063 trying to hammer the filenames into a standard format, such that later
1064 processing can handle them.
1066 If the file name contains something like [dir.], then it recognizes this
1067 as a root, and strips the ".]". Later processing will add whatever is
1068 needed to get things working properly.
1070 If no device is specified, then the first directory name is taken to be
1071 a device name (or a rooted logical). */
1073 /* Point to the UNIX filename part (which needs to be fixed!)
1074 but skip vms path information.
1075 [basename != fullname since first_slash != 0]. */
1077 if ((basename
[-1] == ':') /* vms path spec. */
1078 || (basename
[-1] == ']')
1079 || (basename
[-1] == '>'))
1080 unixname
= basename
;
1082 unixname
= fullname
;
1084 if (*unixname
== '/')
1087 /* If the directory spec is not rooted, we can just copy
1088 the UNIX filename part and we are done. */
1090 if (((basename
- fullname
) > 1)
1091 && ( (basename
[-1] == ']')
1092 || (basename
[-1] == '>')))
1094 if (basename
[-2] != '.')
1097 /* The VMS part ends in a `]', and the preceding character is not a `.'.
1098 -> PATH]:/name (basename = '/name', unixname = 'name')
1099 We strip the `]', and then splice the two parts of the name in the
1100 usual way. Given the default locations for include files,
1101 we will only use this code if the user specifies alternate locations
1102 with the /include (-I) switch on the command line. */
1104 basename
-= 1; /* Strip "]" */
1105 unixname
--; /* backspace */
1110 /* The VMS part has a ".]" at the end, and this will not do. Later
1111 processing will add a second directory spec, and this would be a syntax
1112 error. Thus we strip the ".]", and thus merge the directory specs.
1113 We also backspace unixname, so that it points to a '/'. This inhibits the
1114 generation of the 000000 root directory spec (which does not belong here
1117 basename
-= 2; /* Strip ".]" */
1118 unixname
--; /* backspace */
1126 /* We drop in here if there is no VMS style directory specification yet.
1127 If there is no device specification either, we make the first dir a
1128 device and try that. If we do not do this, then we will be essentially
1129 searching the users default directory (as if they did a #include "asdf.h").
1131 Then all we need to do is to push a '[' into the output string. Later
1132 processing will fill this in, and close the bracket. */
1134 if ((unixname
!= fullname
) /* vms path spec found. */
1135 && (basename
[-1] != ':'))
1136 *local_ptr
++ = ':'; /* dev not in spec. take first dir */
1138 *local_ptr
++ = '['; /* Open the directory specification */
1141 if (unixname
== fullname
) /* no vms dir spec. */
1144 if ((first_slash
!= 0) /* unix dir spec. */
1145 && (*unixname
!= '/') /* not beginning with '/' */
1146 && (*unixname
!= '.')) /* or './' or '../' */
1147 *local_ptr
++ = '.'; /* dir is local ! */
1150 /* at this point we assume that we have the device spec, and (at least
1151 the opening "[" for a directory specification. We may have directories
1154 If there are no other slashes then the filename will be
1155 in the "root" directory. Otherwise, we need to add
1156 directory specifications. */
1158 if (strchr (unixname
, '/') == 0)
1160 /* if no directories specified yet and none are following. */
1161 if (local_ptr
[-1] == '[')
1163 /* Just add "000000]" as the directory string */
1164 strcpy (local_ptr
, "000000]");
1165 local_ptr
+= strlen (local_ptr
);
1166 check_filename_before_returning
= 1; /* we might need to fool with this later */
1172 /* As long as there are still subdirectories to add, do them. */
1173 while (strchr (unixname
, '/') != 0)
1175 /* If this token is "." we can ignore it
1176 if it's not at the beginning of a path. */
1177 if ((unixname
[0] == '.') && (unixname
[1] == '/'))
1179 /* remove it at beginning of path. */
1180 if ( ((unixname
== fullname
) /* no device spec */
1181 && (fullname
+2 != basename
)) /* starts with ./ */
1183 || ((basename
[-1] == ':') /* device spec */
1184 && (unixname
-1 == basename
))) /* and ./ afterwards */
1185 *local_ptr
++ = '.'; /* make '[.' start of path. */
1190 /* Add a subdirectory spec. Do not duplicate "." */
1191 if ( local_ptr
[-1] != '.'
1192 && local_ptr
[-1] != '['
1193 && local_ptr
[-1] != '<')
1196 /* If this is ".." then the spec becomes "-" */
1197 if ( (unixname
[0] == '.')
1198 && (unixname
[1] == '.')
1199 && (unixname
[2] == '/'))
1201 /* Add "-" and skip the ".." */
1202 if ((local_ptr
[-1] == '.')
1203 && (local_ptr
[-2] == '['))
1204 local_ptr
--; /* prevent [.- */
1210 /* Copy the subdirectory */
1211 while (*unixname
!= '/')
1212 *local_ptr
++= *unixname
++;
1214 unixname
++; /* Skip the "/" */
1217 /* Close the directory specification */
1218 if (local_ptr
[-1] == '.') /* no trailing periods */
1221 if (local_ptr
[-1] == '[') /* no dir needed */
1227 /* Now add the filename. */
1230 *local_ptr
++ = *unixname
++;
1233 /* Now append it to the original VMS spec. */
1235 strcpy ((must_revert
==1)?fullname
:basename
, Local
);
1237 /* If we put a [000000] in the filename, try to open it first. If this fails,
1238 remove the [000000], and return that name. This provides flexibility
1239 to the user in that they can use both rooted and non-rooted logical names
1240 to point to the location of the file. */
1242 if (check_filename_before_returning
)
1244 f
= open (fullname
, O_RDONLY
|O_NONBLOCK
);
1247 /* The file name is OK as it is, so return it as is. */
1252 /* The filename did not work. Try to remove the [000000] from the name,
1255 basename
= strchr (fullname
, '[');
1256 local_ptr
= strchr (fullname
, ']') + 1;
1257 strcpy (basename
, local_ptr
); /* this gets rid of it */