1 /* Part of CPP library. (include file handling)
2 Copyright (C) 1986, 87, 89, 92 - 95, 98, 1999 Free Software Foundation, Inc.
3 Written by Per Bothner, 1994.
4 Based on CCCP program by Paul Rubin, June 1986
5 Adapted to ANSI C, Richard Stallman, Jan 1987
6 Split out of cpplib.c, Zack Weinberg, Oct 1998
8 This program is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by the
10 Free Software Foundation; either version 2, or (at your option) any
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 In other words, you are welcome to use, share and improve this program.
23 You are forbidden to forbid anyone else to use, share and improve
24 what you give them. Help stamp out software-hoarding! */
30 /* The entry points to this file are: find_include_file, finclude,
31 include_hash, append_include_chain, deps_output, and file_cleanup.
32 file_cleanup is only called through CPP_BUFFER(pfile)->cleanup,
33 so it's static anyway. */
35 static struct include_hash
*redundant_include_p
37 struct include_hash
*,
38 struct file_name_list
*));
39 static struct file_name_map
*read_name_map
PROTO ((cpp_reader
*,
41 static char *read_filename_string
PROTO ((int, FILE *));
42 static char *remap_filename
PROTO ((cpp_reader
*, char *,
43 struct file_name_list
*));
44 static long read_and_prescan
PROTO ((cpp_reader
*, cpp_buffer
*,
46 static void simplify_pathname
PROTO ((char *));
47 static struct file_name_list
*actual_directory
PROTO ((cpp_reader
*, char *));
50 static void hack_vms_include_specification
PROTO ((char *));
53 /* Windows does not natively support inodes, and neither does MSDOS.
54 VMS has non-numeric inodes. */
56 #define INO_T_EQ(a, b) (!bcmp((char *) &(a), (char *) &(b), sizeof (a)))
57 #elif (defined _WIN32 && !defined CYGWIN && ! defined (_UWIN)) \
59 #define INO_T_EQ(a, b) 0
61 #define INO_T_EQ(a, b) ((a) == (b))
64 /* Append an entry for dir DIR to list LIST, simplifying it if
65 possible. SYS says whether this is a system include directory.
66 *** DIR is modified in place. It must be writable and permanently
67 allocated. LIST is a pointer to the head pointer, because we actually
68 *prepend* the dir, and reverse the list later (in merge_include_chains). */
70 append_include_chain (pfile
, list
, dir
, sysp
)
72 struct file_name_list
**list
;
76 struct file_name_list
*new;
79 char * newdir
= xstrdup (dir
);
81 simplify_pathname (newdir
);
82 if (stat (newdir
, &st
))
84 /* Dirs that don't exist are silently ignored. */
86 cpp_perror_with_name (pfile
, newdir
);
90 if (!S_ISDIR (st
.st_mode
))
92 cpp_message (pfile
, 1, "%s: %s: Not a directory", progname
, newdir
);
97 if (len
> pfile
->max_include_len
)
98 pfile
->max_include_len
= len
;
100 new = (struct file_name_list
*)xmalloc (sizeof (struct file_name_list
));
104 new->ino
= st
.st_ino
;
105 new->dev
= st
.st_dev
;
107 new->name_map
= NULL
;
112 /* Merge the four include chains together in the order quote, bracket,
113 system, after. Remove duplicate dirs (as determined by
114 INO_T_EQ()). The system_include and after_include chains are never
115 referred to again after this function; all access is through the
116 bracket_include path.
118 For the future: Check if the directory is empty (but
119 how?) and possibly preload the include hash. */
122 merge_include_chains (opts
)
123 struct cpp_options
*opts
;
125 struct file_name_list
*prev
, *next
, *cur
, *other
;
126 struct file_name_list
*quote
, *brack
, *systm
, *after
;
127 struct file_name_list
*qtail
, *btail
, *stail
, *atail
;
129 qtail
= opts
->quote_include
;
130 btail
= opts
->bracket_include
;
131 stail
= opts
->system_include
;
132 atail
= opts
->after_include
;
134 /* Nreverse the four lists. */
136 for (cur
= qtail
; cur
; cur
= next
)
145 for (cur
= btail
; cur
; cur
= next
)
154 for (cur
= stail
; cur
; cur
= next
)
163 for (cur
= atail
; cur
; cur
= next
)
171 /* Paste together bracket, system, and after include chains. */
181 /* This is a bit tricky.
182 First we drop dupes from the quote-include list.
183 Then we drop dupes from the bracket-include list.
184 Finally, if qtail and brack are the same directory,
187 We can't just merge the lists and then uniquify them because
188 then we may lose directories from the <> search path that should
189 be there; consider -Ifoo -Ibar -I- -Ifoo -Iquux. It is however
190 safe to treat -Ibar -Ifoo -I- -Ifoo -Iquux as if written
191 -Ibar -I- -Ifoo -Iquux. */
193 for (cur
= quote
; cur
; cur
= cur
->next
)
195 for (other
= quote
; other
!= cur
; other
= other
->next
)
196 if (INO_T_EQ (cur
->ino
, other
->ino
)
197 && cur
->dev
== other
->dev
)
199 prev
->next
= cur
->next
;
209 for (cur
= brack
; cur
; cur
= cur
->next
)
211 for (other
= brack
; other
!= cur
; other
= other
->next
)
212 if (INO_T_EQ (cur
->ino
, other
->ino
)
213 && cur
->dev
== other
->dev
)
215 prev
->next
= cur
->next
;
226 if (INO_T_EQ (qtail
->ino
, brack
->ino
) && qtail
->dev
== brack
->dev
)
237 while (cur
->next
!= qtail
)
250 opts
->quote_include
= quote
;
251 opts
->bracket_include
= brack
;
252 opts
->system_include
= NULL
;
253 opts
->after_include
= NULL
;
256 /* Look up or add an entry to the table of all includes. This table
257 is indexed by the name as it appears in the #include line. The
258 ->next_this_file chain stores all different files with the same
259 #include name (there are at least three ways this can happen). The
260 hash function could probably be improved a bit. */
262 struct include_hash
*
263 include_hash (pfile
, fname
, add
)
268 unsigned int hash
= 0;
269 struct include_hash
*l
, *m
;
275 l
= pfile
->all_include_files
[hash
% ALL_INCLUDE_HASHSIZE
];
277 for (; l
; m
= l
, l
= l
->next
)
278 if (!strcmp (l
->nshort
, fname
))
284 l
= (struct include_hash
*) xmalloc (sizeof (struct include_hash
));
286 l
->next_this_file
= NULL
;
293 pfile
->all_include_files
[hash
% ALL_INCLUDE_HASHSIZE
] = l
;
298 /* Return 0 if the file pointed to by IHASH has never been included before,
299 -1 if it has been included before and need not be again,
300 or a pointer to an IHASH entry which is the file to be reread.
301 "Never before" is with respect to the position in ILIST.
303 This will not detect redundancies involving odd uses of the
304 `current directory' rule for "" includes. They aren't quite
305 pathological, but I think they are rare enough not to worry about.
306 The simplest example is:
315 and the problem is that for `current directory' includes,
316 ihash->foundhere is not on any of the global include chains,
317 so the test below (i->foundhere == l) may be false even when
318 the directories are in fact the same. */
320 static struct include_hash
*
321 redundant_include_p (pfile
, ihash
, ilist
)
323 struct include_hash
*ihash
;
324 struct file_name_list
*ilist
;
326 struct file_name_list
*l
;
327 struct include_hash
*i
;
329 if (! ihash
->foundhere
)
332 for (i
= ihash
; i
; i
= i
->next_this_file
)
333 for (l
= ilist
; l
; l
= l
->next
)
334 if (i
->foundhere
== l
)
335 /* The control_macro works like this: If it's NULL, the file
336 is to be included again. If it's "", the file is never to
337 be included again. If it's a string, the file is not to be
338 included again if the string is the name of a defined macro. */
339 return (i
->control_macro
340 && (i
->control_macro
[0] == '\0'
341 || cpp_lookup (pfile
, i
->control_macro
, -1, -1)))
342 ? (struct include_hash
*)-1 : i
;
348 file_cleanup (pbuf
, pfile
)
357 if (pfile
->system_include_depth
)
358 pfile
->system_include_depth
--;
362 /* Search for include file FNAME in the include chain starting at
363 SEARCH_START. Return -2 if this file doesn't need to be included
364 (because it was included already and it's marked idempotent),
365 -1 if an error occurred, or a file descriptor open on the file.
366 *IHASH is set to point to the include hash entry for this file, and
367 *BEFORE is 1 if the file was included before (but needs to be read
370 find_include_file (pfile
, fname
, search_start
, ihash
, before
)
373 struct file_name_list
*search_start
;
374 struct include_hash
**ihash
;
377 struct file_name_list
*l
;
378 struct include_hash
*ih
, *jh
;
382 ih
= include_hash (pfile
, fname
, 1);
383 jh
= redundant_include_p (pfile
, ih
,
384 fname
[0] == '/' ? ABSOLUTE_PATH
: search_start
);
391 if (jh
== (struct include_hash
*)-1)
394 return open (jh
->name
, O_RDONLY
, 0666);
398 /* A file is already known by this name, but it's not the same file.
399 Allocate another include_hash block and add it to the next_this_file
402 jh
= (struct include_hash
*)xmalloc (sizeof (struct include_hash
));
403 while (ih
->next_this_file
) ih
= ih
->next_this_file
;
405 ih
->next_this_file
= jh
;
407 ih
= ih
->next_this_file
;
410 ih
->next_this_file
= NULL
;
416 ih
->nshort
= xstrdup (fname
);
417 ih
->control_macro
= NULL
;
419 /* If the pathname is absolute, just open it. */
422 ih
->foundhere
= ABSOLUTE_PATH
;
423 ih
->name
= ih
->nshort
;
424 return open (ih
->name
, O_RDONLY
, 0666);
427 /* Search directory path, trying to open the file. */
429 len
= strlen (fname
);
430 name
= xmalloc (len
+ pfile
->max_include_len
+ 2 + INCLUDE_LEN_FUDGE
);
432 for (l
= search_start
; l
; l
= l
->next
)
434 bcopy (l
->name
, name
, l
->nlen
);
436 strcpy (&name
[l
->nlen
+1], fname
);
437 simplify_pathname (name
);
438 if (CPP_OPTIONS (pfile
)->remap
)
439 name
= remap_filename (pfile
, name
, l
);
441 f
= open (name
, O_RDONLY
|O_NONBLOCK
|O_NOCTTY
, 0666);
443 if (f
== -1 && errno
== EACCES
)
445 cpp_error(pfile
, "included file `%s' exists but is not readable",
454 ih
->name
= xrealloc (name
, strlen (name
)+1);
461 jh
->next_this_file
= NULL
;
465 *ihash
= (struct include_hash
*)-1;
469 /* The file_name_map structure holds a mapping of file names for a
470 particular directory. This mapping is read from the file named
471 FILE_NAME_MAP_FILE in that directory. Such a file can be used to
472 map filenames on a file system with severe filename restrictions,
473 such as DOS. The format of the file name map file is just a series
474 of lines with two tokens on each line. The first token is the name
475 to map, and the second token is the actual name to use. */
479 struct file_name_map
*map_next
;
484 #define FILE_NAME_MAP_FILE "header.gcc"
486 /* Read a space delimited string of unlimited length from a stdio
490 read_filename_string (ch
, f
)
498 set
= alloc
= xmalloc (len
+ 1);
502 while ((ch
= getc (f
)) != EOF
&& ! is_space
[ch
])
504 if (set
- alloc
== len
)
507 alloc
= xrealloc (alloc
, len
+ 1);
508 set
= alloc
+ len
/ 2;
518 /* This structure holds a linked list of file name maps, one per directory. */
520 struct file_name_map_list
522 struct file_name_map_list
*map_list_next
;
524 struct file_name_map
*map_list_map
;
527 /* Read the file name map file for DIRNAME. */
529 static struct file_name_map
*
530 read_name_map (pfile
, dirname
)
534 register struct file_name_map_list
*map_list_ptr
;
538 for (map_list_ptr
= CPP_OPTIONS (pfile
)->map_list
; map_list_ptr
;
539 map_list_ptr
= map_list_ptr
->map_list_next
)
540 if (! strcmp (map_list_ptr
->map_list_name
, dirname
))
541 return map_list_ptr
->map_list_map
;
543 map_list_ptr
= ((struct file_name_map_list
*)
544 xmalloc (sizeof (struct file_name_map_list
)));
545 map_list_ptr
->map_list_name
= xstrdup (dirname
);
547 name
= (char *) alloca (strlen (dirname
) + strlen (FILE_NAME_MAP_FILE
) + 2);
548 strcpy (name
, dirname
);
551 strcat (name
, FILE_NAME_MAP_FILE
);
552 f
= fopen (name
, "r");
554 map_list_ptr
->map_list_map
= (struct file_name_map
*)-1;
558 int dirlen
= strlen (dirname
);
560 while ((ch
= getc (f
)) != EOF
)
563 struct file_name_map
*ptr
;
567 from
= read_filename_string (ch
, f
);
568 while ((ch
= getc (f
)) != EOF
&& is_hor_space
[ch
])
570 to
= read_filename_string (ch
, f
);
572 ptr
= ((struct file_name_map
*)
573 xmalloc (sizeof (struct file_name_map
)));
574 ptr
->map_from
= from
;
576 /* Make the real filename absolute. */
581 ptr
->map_to
= xmalloc (dirlen
+ strlen (to
) + 2);
582 strcpy (ptr
->map_to
, dirname
);
583 ptr
->map_to
[dirlen
] = '/';
584 strcpy (ptr
->map_to
+ dirlen
+ 1, to
);
588 ptr
->map_next
= map_list_ptr
->map_list_map
;
589 map_list_ptr
->map_list_map
= ptr
;
591 while ((ch
= getc (f
)) != '\n')
598 map_list_ptr
->map_list_next
= CPP_OPTIONS (pfile
)->map_list
;
599 CPP_OPTIONS (pfile
)->map_list
= map_list_ptr
;
601 return map_list_ptr
->map_list_map
;
604 /* Remap NAME based on the file_name_map (if any) for LOC. */
607 remap_filename (pfile
, name
, loc
)
610 struct file_name_list
*loc
;
612 struct file_name_map
*map
;
613 const char *from
, *p
, *dir
;
616 loc
->name_map
= read_name_map (pfile
,
620 if (loc
->name_map
== (struct file_name_map
*)-1)
623 from
= name
+ strlen (loc
->name
) + 1;
625 for (map
= loc
->name_map
; map
; map
= map
->map_next
)
626 if (!strcmp (map
->map_from
, from
))
629 /* Try to find a mapping file for the particular directory we are
630 looking in. Thus #include <sys/types.h> will look up sys/types.h
631 in /usr/include/header.gcc and look up types.h in
632 /usr/include/sys/header.gcc. */
633 p
= rindex (name
, '/');
637 && strlen (loc
->name
) == (size_t) (p
- name
)
638 && !strncmp (loc
->name
, name
, p
- name
))
639 /* FILENAME is in SEARCHPTR, which we've already checked. */
649 char * newdir
= (char *) alloca (p
- name
+ 1);
650 bcopy (name
, newdir
, p
- name
);
651 newdir
[p
- name
] = '\0';
656 for (map
= read_name_map (pfile
, dir
); map
; map
= map
->map_next
)
657 if (! strcmp (map
->map_from
, name
))
663 /* Read the contents of FD into the buffer on the top of PFILE's stack.
664 IHASH points to the include hash entry for the file associated with
667 The caller is responsible for the cpp_push_buffer. */
670 finclude (pfile
, fd
, ihash
)
673 struct include_hash
*ihash
;
680 if (fstat (fd
, &st
) < 0)
682 if (fcntl (fd
, F_SETFL
, 0) == -1) /* turn off nonblocking mode */
685 fp
= CPP_BUFFER (pfile
);
687 /* If fd points to a plain file, we know how big it is, so we can
688 allocate the buffer all at once. If fd is a pipe or terminal, we
689 can't. Most C source files are 4k or less, so we guess that. If
690 fd is something weird, like a block device or a directory, we
691 don't want to read it at all.
693 Unfortunately, different systems use different st.st_mode values
694 for pipes: some have S_ISFIFO, some S_ISSOCK, some are buggy and
695 zero the entire struct stat except a couple fields. Hence the
698 In all cases, read_and_prescan will resize the buffer if it
699 turns out there's more data than we thought. */
701 if (S_ISREG (st
.st_mode
))
703 /* off_t might have a wider range than size_t - in other words,
704 the max size of a file might be bigger than the address
705 space. We can't handle a file that large. (Anyone with
706 a single source file bigger than 4GB needs to rethink
707 their coding style.) */
708 st_size
= (size_t) st
.st_size
;
709 if ((unsigned HOST_WIDEST_INT
) st_size
710 != (unsigned HOST_WIDEST_INT
) st
.st_size
)
712 cpp_error (pfile
, "file `%s' is too large", ihash
->name
);
716 else if (S_ISFIFO (st
.st_mode
) || S_ISSOCK (st
.st_mode
)
717 /* Some 4.x (x<4) derivatives have a bug that makes fstat() of a
718 socket or pipe return a stat struct with most fields zeroed. */
719 || (st
.st_mode
== 0 && st
.st_nlink
== 0 && st
.st_size
== 0)
720 || (S_ISCHR (st
.st_mode
) && isatty (fd
)))
722 /* Cannot get its file size before reading. 4k is a decent
728 cpp_error (pfile
, "`%s' is not a file, pipe, or tty", ihash
->name
);
732 /* Read the file, converting end-of-line characters and trigraphs
735 fp
->nominal_fname
= fp
->fname
= ihash
->name
;
736 length
= read_and_prescan (pfile
, fp
, fd
, st_size
);
740 ihash
->control_macro
= ""; /* never re-include */
743 fp
->rlimit
= fp
->alimit
= fp
->buf
+ length
;
745 fp
->system_header_p
= (ihash
->foundhere
!= ABSOLUTE_PATH
746 && ihash
->foundhere
->sysp
);
749 fp
->cleanup
= file_cleanup
;
751 /* The ->actual_dir field is only used when ignore_srcdir is not in effect;
753 if (!CPP_OPTIONS (pfile
)->ignore_srcdir
)
754 fp
->actual_dir
= actual_directory (pfile
, fp
->fname
);
756 pfile
->input_stack_listing_current
= 0;
760 cpp_error_from_errno (pfile
, ihash
->name
);
762 cpp_pop_buffer (pfile
);
767 /* Given a path FNAME, extract the directory component and place it
768 onto the actual_dirs list. Return a pointer to the allocated
769 file_name_list structure. These structures are used to implement
770 current-directory "" include searching. */
772 static struct file_name_list
*
773 actual_directory (pfile
, fname
)
777 char *last_slash
, *dir
;
779 struct file_name_list
*x
;
781 dir
= xstrdup (fname
);
782 last_slash
= rindex (dir
, '/');
785 if (last_slash
== dir
)
788 last_slash
[1] = '\0';
792 dlen
= last_slash
- dir
;
803 if (dlen
> pfile
->max_include_len
)
804 pfile
->max_include_len
= dlen
;
806 for (x
= pfile
->actual_dirs
; x
; x
= x
->alloc
)
807 if (!strcmp (x
->name
, dir
))
813 /* Not found, make a new one. */
814 x
= (struct file_name_list
*) xmalloc (sizeof (struct file_name_list
));
817 x
->next
= CPP_OPTIONS (pfile
)->quote_include
;
818 x
->alloc
= pfile
->actual_dirs
;
822 pfile
->actual_dirs
= x
;
826 /* Read the entire contents of file DESC into buffer BUF, convert end-of-line
827 markers to canonical form, and convert trigraphs if enabled. Also, make
828 sure there is a newline at the end of the file. LEN is how much room we
829 have to start with (this can be expanded if necessary).
830 Returns -1 on failure, or the actual length of the data to be scanned.
832 N.B. This function has been rearranged to out-of-line the uncommon cases
833 as much as possible; this is important to prevent it from being a
834 performance bottleneck. */
837 read_and_prescan (pfile
, fp
, desc
, len
)
843 U_CHAR
*buf
= (U_CHAR
*) xmalloc (len
);
844 U_CHAR
*ip
, *op
, *line_base
;
849 /* 4096 bytes of buffer proper, 2 to detect running off the end without
850 address arithmetic all the time, and 2 for pushback in the case there's
851 a potential trigraph or end-of-line digraph at the end of a block. */
852 #define INTERMED_BUFFER_SIZE 4096
853 U_CHAR intermed
[INTERMED_BUFFER_SIZE
+ 2 + 2];
859 ibase
= intermed
+ 2;
865 count
= read (desc
, intermed
+ 2, INTERMED_BUFFER_SIZE
);
873 ibase
= intermed
+ 2;
874 ibase
[count
] = ibase
[count
+1] = '\0';
879 size_t delta_line_base
;
883 This could happen if the file is larger than half the
884 maximum address space of the machine. */
888 delta_line_base
= line_base
- buf
;
889 buf
= (U_CHAR
*) xrealloc (buf
, len
);
891 line_base
= buf
+ delta_line_base
;
900 /* The default case is at the top so gcc will realize
901 it's the common case, and leave c in a register.
902 Also, cache utilization is a little better this way. */
910 if (*ip
== '\n') ip
++;
911 else if (*ip
== '\0')
923 if (*ip
== '\r') ip
++;
924 else if (*ip
== '\0')
936 if (CPP_OPTIONS (pfile
)->trigraphs
937 || CPP_OPTIONS (pfile
)->warn_trigraphs
)
940 /* If we're at the end of the intermediate buffer,
941 we have to shift the ?'s down to the start and
942 come back next pass. */
959 intermed
[0] = intermed
[1] = '?';
962 if (!trigraph_table
[d
])
968 if (CPP_OPTIONS (pfile
)->warn_trigraphs
)
969 cpp_warning_with_line (pfile
, line
, op
-line_base
,
970 "trigraph ??%c encountered", d
);
971 if (CPP_OPTIONS (pfile
)->trigraphs
)
972 *op
++ = trigraph_table
[d
];
990 /* Deal with pushed-back chars at true EOF.
991 If two chars were pushed back, they must both be ?'s.
992 If one was, it might be ?, \r, or \n, and \r needs to
994 We know we have space already. */
995 if (ibase
== intermed
)
1000 else if (ibase
== intermed
+ 1)
1008 if (op
[-1] != '\n' || op
[-2] == '\\')
1010 if (CPP_PEDANTIC (pfile
))
1011 cpp_pedwarn_with_line (pfile
, line
, op
- line_base
,
1012 "no newline at end of file");
1013 if (offset
+ 2 > len
)
1016 if (offset
+ 2 > len
)
1018 buf
= (U_CHAR
*) xrealloc (buf
, len
);
1027 (U_CHAR
*) ((len
- offset
< 20) ? (PTR
) buf
: xrealloc (buf
, op
- buf
));
1031 cpp_error (pfile
, "file is too large");
1036 cpp_error_from_errno (pfile
, fp
->fname
);
1041 /* Add output to `deps_buffer' for the -M switch.
1042 STRING points to the text to be output.
1043 SPACER is ':' for targets, ' ' for dependencies, zero for text
1044 to be inserted literally. */
1047 deps_output (pfile
, string
, spacer
)
1058 size
= strlen (string
);
1060 #ifndef MAX_OUTPUT_COLUMNS
1061 #define MAX_OUTPUT_COLUMNS 72
1063 if (pfile
->deps_column
> 0
1064 && (pfile
->deps_column
+ size
) > MAX_OUTPUT_COLUMNS
)
1068 pfile
->deps_column
= 0;
1071 if (pfile
->deps_size
+ size
+ 8 > pfile
->deps_allocated_size
)
1073 pfile
->deps_allocated_size
= (pfile
->deps_size
+ size
+ 50) * 2;
1074 pfile
->deps_buffer
= (char *) xrealloc (pfile
->deps_buffer
,
1075 pfile
->deps_allocated_size
);
1080 bcopy (" \\\n ", &pfile
->deps_buffer
[pfile
->deps_size
], 5);
1081 pfile
->deps_size
+= 5;
1084 if (spacer
== ' ' && pfile
->deps_column
> 0)
1085 pfile
->deps_buffer
[pfile
->deps_size
++] = ' ';
1086 bcopy (string
, &pfile
->deps_buffer
[pfile
->deps_size
], size
);
1087 pfile
->deps_size
+= size
;
1088 pfile
->deps_column
+= size
;
1090 pfile
->deps_buffer
[pfile
->deps_size
++] = ':';
1091 pfile
->deps_buffer
[pfile
->deps_size
] = 0;
1094 /* Simplify a path name in place, deleting redundant components. This
1095 reduces OS overhead and guarantees that equivalent paths compare
1096 the same (modulo symlinks).
1099 foo/bar/../quux foo/quux
1103 //quux //quux (POSIX allows leading // as a namespace escape)
1105 Guarantees no trailing slashes. All transforms reduce the length
1109 simplify_pathname (path
)
1116 #if defined _WIN32 || defined __MSDOS__
1117 /* Convert all backslashes to slashes. */
1118 for (from
= path
; *from
; from
++)
1119 if (*from
== '\\') *from
= '/';
1121 /* Skip over leading drive letter if present. */
1122 if (ISALPHA (path
[0]) && path
[1] == ':')
1123 from
= to
= &path
[2];
1130 /* Remove redundant initial /s. */
1139 /* 3 or more initial /s are equivalent to 1 /. */
1140 while (*++from
== '/');
1142 /* On some hosts // differs from /; Posix allows this. */
1150 while (*from
== '/')
1153 if (from
[0] == '.' && from
[1] == '/')
1155 else if (from
[0] == '.' && from
[1] == '\0')
1157 else if (from
[0] == '.' && from
[1] == '.' && from
[2] == '/')
1174 while (to
> base
&& *to
!= '/') to
--;
1180 else if (from
[0] == '.' && from
[1] == '.' && from
[2] == '\0')
1193 while (to
> base
&& *to
!= '/') to
--;
1200 /* Copy this component and trailing /, if any. */
1201 while ((*to
++ = *from
++) != '/')
1213 /* Trim trailing slash */
1214 if (to
[0] == '/' && (!absolute
|| to
> path
+1))
1217 /* Change the empty string to "." so that stat() on the result
1218 will always work. */
1227 /* It is not clear when this should be used if at all, so I've
1228 disabled it until someone who understands VMS can look at it. */
1231 /* Under VMS we need to fix up the "include" specification filename.
1233 Rules for possible conversions
1235 fullname tried paths
1238 ./dir/name [.dir]name
1240 /name [000000]name, name
1241 dir/name dir:[000000]name, dir:name, dir/name
1242 dir1/dir2/name dir1:[dir2]name, dir1:[000000.dir2]name
1243 path:/name path:[000000]name, path:name
1244 path:/dir/name path:[000000.dir]name, path:[dir]name
1245 path:dir/name path:[dir]name
1246 [path]:[dir]name [path.dir]name
1247 path/[dir]name [path.dir]name
1249 The path:/name input is constructed when expanding <> includes. */
1253 hack_vms_include_specification (fullname
)
1256 register char *basename
, *unixname
, *local_ptr
, *first_slash
;
1257 int f
, check_filename_before_returning
, must_revert
;
1260 check_filename_before_returning
= 0;
1262 /* See if we can find a 1st slash. If not, there's no path information. */
1263 first_slash
= index (fullname
, '/');
1264 if (first_slash
== 0)
1265 return 0; /* Nothing to do!!! */
1267 /* construct device spec if none given. */
1269 if (index (fullname
, ':') == 0)
1272 /* If fullname has a slash, take it as device spec. */
1274 if (first_slash
== fullname
)
1276 first_slash
= index (fullname
+1, '/'); /* 2nd slash ? */
1278 *first_slash
= ':'; /* make device spec */
1279 for (basename
= fullname
; *basename
!= 0; basename
++)
1280 *basename
= *(basename
+1); /* remove leading slash */
1282 else if ((first_slash
[-1] != '.') /* keep ':/', './' */
1283 && (first_slash
[-1] != ':')
1284 && (first_slash
[-1] != ']')) /* or a vms path */
1288 else if ((first_slash
[1] == '[') /* skip './' in './[dir' */
1289 && (first_slash
[-1] == '.'))
1293 /* Get part after first ':' (basename[-1] == ':')
1294 or last '/' (basename[-1] == '/'). */
1296 basename
= base_name (fullname
);
1298 local_ptr
= Local
; /* initialize */
1300 /* We are trying to do a number of things here. First of all, we are
1301 trying to hammer the filenames into a standard format, such that later
1302 processing can handle them.
1304 If the file name contains something like [dir.], then it recognizes this
1305 as a root, and strips the ".]". Later processing will add whatever is
1306 needed to get things working properly.
1308 If no device is specified, then the first directory name is taken to be
1309 a device name (or a rooted logical). */
1311 /* Point to the UNIX filename part (which needs to be fixed!)
1312 but skip vms path information.
1313 [basename != fullname since first_slash != 0]. */
1315 if ((basename
[-1] == ':') /* vms path spec. */
1316 || (basename
[-1] == ']')
1317 || (basename
[-1] == '>'))
1318 unixname
= basename
;
1320 unixname
= fullname
;
1322 if (*unixname
== '/')
1325 /* If the directory spec is not rooted, we can just copy
1326 the UNIX filename part and we are done. */
1328 if (((basename
- fullname
) > 1)
1329 && ( (basename
[-1] == ']')
1330 || (basename
[-1] == '>')))
1332 if (basename
[-2] != '.')
1335 /* The VMS part ends in a `]', and the preceding character is not a `.'.
1336 -> PATH]:/name (basename = '/name', unixname = 'name')
1337 We strip the `]', and then splice the two parts of the name in the
1338 usual way. Given the default locations for include files in cccp.c,
1339 we will only use this code if the user specifies alternate locations
1340 with the /include (-I) switch on the command line. */
1342 basename
-= 1; /* Strip "]" */
1343 unixname
--; /* backspace */
1348 /* The VMS part has a ".]" at the end, and this will not do. Later
1349 processing will add a second directory spec, and this would be a syntax
1350 error. Thus we strip the ".]", and thus merge the directory specs.
1351 We also backspace unixname, so that it points to a '/'. This inhibits the
1352 generation of the 000000 root directory spec (which does not belong here
1355 basename
-= 2; /* Strip ".]" */
1356 unixname
--; /* backspace */
1364 /* We drop in here if there is no VMS style directory specification yet.
1365 If there is no device specification either, we make the first dir a
1366 device and try that. If we do not do this, then we will be essentially
1367 searching the users default directory (as if they did a #include "asdf.h").
1369 Then all we need to do is to push a '[' into the output string. Later
1370 processing will fill this in, and close the bracket. */
1372 if ((unixname
!= fullname
) /* vms path spec found. */
1373 && (basename
[-1] != ':'))
1374 *local_ptr
++ = ':'; /* dev not in spec. take first dir */
1376 *local_ptr
++ = '['; /* Open the directory specification */
1379 if (unixname
== fullname
) /* no vms dir spec. */
1382 if ((first_slash
!= 0) /* unix dir spec. */
1383 && (*unixname
!= '/') /* not beginning with '/' */
1384 && (*unixname
!= '.')) /* or './' or '../' */
1385 *local_ptr
++ = '.'; /* dir is local ! */
1388 /* at this point we assume that we have the device spec, and (at least
1389 the opening "[" for a directory specification. We may have directories
1392 If there are no other slashes then the filename will be
1393 in the "root" directory. Otherwise, we need to add
1394 directory specifications. */
1396 if (index (unixname
, '/') == 0)
1398 /* if no directories specified yet and none are following. */
1399 if (local_ptr
[-1] == '[')
1401 /* Just add "000000]" as the directory string */
1402 strcpy (local_ptr
, "000000]");
1403 local_ptr
+= strlen (local_ptr
);
1404 check_filename_before_returning
= 1; /* we might need to fool with this later */
1410 /* As long as there are still subdirectories to add, do them. */
1411 while (index (unixname
, '/') != 0)
1413 /* If this token is "." we can ignore it
1414 if it's not at the beginning of a path. */
1415 if ((unixname
[0] == '.') && (unixname
[1] == '/'))
1417 /* remove it at beginning of path. */
1418 if ( ((unixname
== fullname
) /* no device spec */
1419 && (fullname
+2 != basename
)) /* starts with ./ */
1421 || ((basename
[-1] == ':') /* device spec */
1422 && (unixname
-1 == basename
))) /* and ./ afterwards */
1423 *local_ptr
++ = '.'; /* make '[.' start of path. */
1428 /* Add a subdirectory spec. Do not duplicate "." */
1429 if ( local_ptr
[-1] != '.'
1430 && local_ptr
[-1] != '['
1431 && local_ptr
[-1] != '<')
1434 /* If this is ".." then the spec becomes "-" */
1435 if ( (unixname
[0] == '.')
1436 && (unixname
[1] == '.')
1437 && (unixname
[2] == '/'))
1439 /* Add "-" and skip the ".." */
1440 if ((local_ptr
[-1] == '.')
1441 && (local_ptr
[-2] == '['))
1442 local_ptr
--; /* prevent [.- */
1448 /* Copy the subdirectory */
1449 while (*unixname
!= '/')
1450 *local_ptr
++= *unixname
++;
1452 unixname
++; /* Skip the "/" */
1455 /* Close the directory specification */
1456 if (local_ptr
[-1] == '.') /* no trailing periods */
1459 if (local_ptr
[-1] == '[') /* no dir needed */
1465 /* Now add the filename. */
1468 *local_ptr
++ = *unixname
++;
1471 /* Now append it to the original VMS spec. */
1473 strcpy ((must_revert
==1)?fullname
:basename
, Local
);
1475 /* If we put a [000000] in the filename, try to open it first. If this fails,
1476 remove the [000000], and return that name. This provides flexibility
1477 to the user in that they can use both rooted and non-rooted logical names
1478 to point to the location of the file. */
1480 if (check_filename_before_returning
)
1482 f
= open (fullname
, O_RDONLY
, 0666);
1485 /* The file name is OK as it is, so return it as is. */
1490 /* The filename did not work. Try to remove the [000000] from the name,
1493 basename
= index (fullname
, '[');
1494 local_ptr
= index (fullname
, ']') + 1;
1495 strcpy (basename
, local_ptr
); /* this gets rid of it */