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 struct file_name_list
*actual_directory
PROTO ((cpp_reader
*, char *));
49 static void hack_vms_include_specification
PROTO ((char *));
52 /* Windows does not natively support inodes, and neither does MSDOS.
53 VMS has non-numeric inodes. */
55 #define INO_T_EQ(a, b) (!bcmp((char *) &(a), (char *) &(b), sizeof (a)))
56 #elif (defined _WIN32 && !defined CYGWIN && ! defined (_UWIN)) \
58 #define INO_T_EQ(a, b) 0
60 #define INO_T_EQ(a, b) ((a) == (b))
63 /* Merge the four include chains together in the order quote, bracket,
64 system, after. Remove duplicate dirs (as determined by
65 INO_T_EQ()). The system_include and after_include chains are never
66 referred to again after this function; all access is through the
69 For the future: Check if the directory is empty (but
70 how?) and possibly preload the include hash. */
73 merge_include_chains (opts
)
74 struct cpp_options
*opts
;
76 struct file_name_list
*prev
, *cur
, *other
;
77 struct file_name_list
*quote
, *brack
, *systm
, *after
;
78 struct file_name_list
*qtail
, *btail
, *stail
, *atail
;
80 qtail
= opts
->pending
->quote_tail
;
81 btail
= opts
->pending
->brack_tail
;
82 stail
= opts
->pending
->systm_tail
;
83 atail
= opts
->pending
->after_tail
;
85 quote
= opts
->pending
->quote_head
;
86 brack
= opts
->pending
->brack_head
;
87 systm
= opts
->pending
->systm_head
;
88 after
= opts
->pending
->after_head
;
90 /* Paste together bracket, system, and after include chains. */
100 /* This is a bit tricky.
101 First we drop dupes from the quote-include list.
102 Then we drop dupes from the bracket-include list.
103 Finally, if qtail and brack are the same directory,
106 We can't just merge the lists and then uniquify them because
107 then we may lose directories from the <> search path that should
108 be there; consider -Ifoo -Ibar -I- -Ifoo -Iquux. It is however
109 safe to treat -Ibar -Ifoo -I- -Ifoo -Iquux as if written
110 -Ibar -I- -Ifoo -Iquux.
112 Note that this algorithm is quadratic in the number of -I switches,
113 which is acceptable since there aren't usually that many of them. */
115 for (cur
= quote
, prev
= NULL
; cur
; cur
= cur
->next
)
117 for (other
= quote
; other
!= cur
; other
= other
->next
)
118 if (INO_T_EQ (cur
->ino
, other
->ino
)
119 && cur
->dev
== other
->dev
)
122 cpp_notice ("ignoring duplicate directory `%s'\n", cur
->name
);
124 prev
->next
= cur
->next
;
134 for (cur
= brack
; cur
; cur
= cur
->next
)
136 for (other
= brack
; other
!= cur
; other
= other
->next
)
137 if (INO_T_EQ (cur
->ino
, other
->ino
)
138 && cur
->dev
== other
->dev
)
141 cpp_notice ("ignoring duplicate directory `%s'\n", cur
->name
);
143 prev
->next
= cur
->next
;
154 if (INO_T_EQ (qtail
->ino
, brack
->ino
) && qtail
->dev
== brack
->dev
)
159 cpp_notice ("ignoring duplicate directory `%s'\n",
169 while (cur
->next
!= qtail
)
173 cpp_notice ("ignoring duplicate directory `%s'\n",
186 opts
->quote_include
= quote
;
187 opts
->bracket_include
= brack
;
190 /* Look up or add an entry to the table of all includes. This table
191 is indexed by the name as it appears in the #include line. The
192 ->next_this_file chain stores all different files with the same
193 #include name (there are at least three ways this can happen). The
194 hash function could probably be improved a bit. */
196 struct include_hash
*
197 include_hash (pfile
, fname
, add
)
202 unsigned int hash
= 0;
203 struct include_hash
*l
, *m
;
209 l
= pfile
->all_include_files
[hash
% ALL_INCLUDE_HASHSIZE
];
211 for (; l
; m
= l
, l
= l
->next
)
212 if (!strcmp (l
->nshort
, fname
))
218 l
= (struct include_hash
*) xmalloc (sizeof (struct include_hash
));
220 l
->next_this_file
= NULL
;
227 pfile
->all_include_files
[hash
% ALL_INCLUDE_HASHSIZE
] = l
;
232 /* Return 0 if the file pointed to by IHASH has never been included before,
233 -1 if it has been included before and need not be again,
234 or a pointer to an IHASH entry which is the file to be reread.
235 "Never before" is with respect to the position in ILIST.
237 This will not detect redundancies involving odd uses of the
238 `current directory' rule for "" includes. They aren't quite
239 pathological, but I think they are rare enough not to worry about.
240 The simplest example is:
249 and the problem is that for `current directory' includes,
250 ihash->foundhere is not on any of the global include chains,
251 so the test below (i->foundhere == l) may be false even when
252 the directories are in fact the same. */
254 static struct include_hash
*
255 redundant_include_p (pfile
, ihash
, ilist
)
257 struct include_hash
*ihash
;
258 struct file_name_list
*ilist
;
260 struct file_name_list
*l
;
261 struct include_hash
*i
;
263 if (! ihash
->foundhere
)
266 for (i
= ihash
; i
; i
= i
->next_this_file
)
267 for (l
= ilist
; l
; l
= l
->next
)
268 if (i
->foundhere
== l
)
269 /* The control_macro works like this: If it's NULL, the file
270 is to be included again. If it's "", the file is never to
271 be included again. If it's a string, the file is not to be
272 included again if the string is the name of a defined macro. */
273 return (i
->control_macro
274 && (i
->control_macro
[0] == '\0'
275 || cpp_lookup (pfile
, i
->control_macro
, -1, -1)))
276 ? (struct include_hash
*)-1 : i
;
282 file_cleanup (pbuf
, pfile
)
291 if (pfile
->system_include_depth
)
292 pfile
->system_include_depth
--;
296 /* Search for include file FNAME in the include chain starting at
297 SEARCH_START. Return -2 if this file doesn't need to be included
298 (because it was included already and it's marked idempotent),
299 -1 if an error occurred, or a file descriptor open on the file.
300 *IHASH is set to point to the include hash entry for this file, and
301 *BEFORE is 1 if the file was included before (but needs to be read
304 find_include_file (pfile
, fname
, search_start
, ihash
, before
)
307 struct file_name_list
*search_start
;
308 struct include_hash
**ihash
;
311 struct file_name_list
*l
;
312 struct include_hash
*ih
, *jh
;
316 ih
= include_hash (pfile
, fname
, 1);
317 jh
= redundant_include_p (pfile
, ih
,
318 fname
[0] == '/' ? ABSOLUTE_PATH
: search_start
);
325 if (jh
== (struct include_hash
*)-1)
328 return open (jh
->name
, O_RDONLY
, 0666);
332 /* A file is already known by this name, but it's not the same file.
333 Allocate another include_hash block and add it to the next_this_file
336 jh
= (struct include_hash
*)xmalloc (sizeof (struct include_hash
));
337 while (ih
->next_this_file
) ih
= ih
->next_this_file
;
339 ih
->next_this_file
= jh
;
341 ih
= ih
->next_this_file
;
344 ih
->next_this_file
= NULL
;
350 ih
->nshort
= xstrdup (fname
);
351 ih
->control_macro
= NULL
;
353 /* If the pathname is absolute, just open it. */
356 ih
->foundhere
= ABSOLUTE_PATH
;
357 ih
->name
= ih
->nshort
;
358 return open (ih
->name
, O_RDONLY
, 0666);
361 /* Search directory path, trying to open the file. */
363 len
= strlen (fname
);
364 name
= xmalloc (len
+ pfile
->max_include_len
+ 2 + INCLUDE_LEN_FUDGE
);
366 for (l
= search_start
; l
; l
= l
->next
)
368 bcopy (l
->name
, name
, l
->nlen
);
370 strcpy (&name
[l
->nlen
+1], fname
);
371 simplify_pathname (name
);
372 if (CPP_OPTIONS (pfile
)->remap
)
373 name
= remap_filename (pfile
, name
, l
);
375 f
= open (name
, O_RDONLY
|O_NONBLOCK
|O_NOCTTY
, 0666);
377 if (f
== -1 && errno
== EACCES
)
379 cpp_error(pfile
, "included file `%s' exists but is not readable",
388 ih
->name
= xrealloc (name
, strlen (name
)+1);
395 jh
->next_this_file
= NULL
;
399 *ihash
= (struct include_hash
*)-1;
403 /* The file_name_map structure holds a mapping of file names for a
404 particular directory. This mapping is read from the file named
405 FILE_NAME_MAP_FILE in that directory. Such a file can be used to
406 map filenames on a file system with severe filename restrictions,
407 such as DOS. The format of the file name map file is just a series
408 of lines with two tokens on each line. The first token is the name
409 to map, and the second token is the actual name to use. */
413 struct file_name_map
*map_next
;
418 #define FILE_NAME_MAP_FILE "header.gcc"
420 /* Read a space delimited string of unlimited length from a stdio
424 read_filename_string (ch
, f
)
432 set
= alloc
= xmalloc (len
+ 1);
436 while ((ch
= getc (f
)) != EOF
&& ! is_space
[ch
])
438 if (set
- alloc
== len
)
441 alloc
= xrealloc (alloc
, len
+ 1);
442 set
= alloc
+ len
/ 2;
452 /* This structure holds a linked list of file name maps, one per directory. */
454 struct file_name_map_list
456 struct file_name_map_list
*map_list_next
;
458 struct file_name_map
*map_list_map
;
461 /* Read the file name map file for DIRNAME. */
463 static struct file_name_map
*
464 read_name_map (pfile
, dirname
)
468 register struct file_name_map_list
*map_list_ptr
;
472 for (map_list_ptr
= CPP_OPTIONS (pfile
)->map_list
; map_list_ptr
;
473 map_list_ptr
= map_list_ptr
->map_list_next
)
474 if (! strcmp (map_list_ptr
->map_list_name
, dirname
))
475 return map_list_ptr
->map_list_map
;
477 map_list_ptr
= ((struct file_name_map_list
*)
478 xmalloc (sizeof (struct file_name_map_list
)));
479 map_list_ptr
->map_list_name
= xstrdup (dirname
);
481 name
= (char *) alloca (strlen (dirname
) + strlen (FILE_NAME_MAP_FILE
) + 2);
482 strcpy (name
, dirname
);
485 strcat (name
, FILE_NAME_MAP_FILE
);
486 f
= fopen (name
, "r");
488 map_list_ptr
->map_list_map
= (struct file_name_map
*)-1;
492 int dirlen
= strlen (dirname
);
494 while ((ch
= getc (f
)) != EOF
)
497 struct file_name_map
*ptr
;
501 from
= read_filename_string (ch
, f
);
502 while ((ch
= getc (f
)) != EOF
&& is_hor_space
[ch
])
504 to
= read_filename_string (ch
, f
);
506 ptr
= ((struct file_name_map
*)
507 xmalloc (sizeof (struct file_name_map
)));
508 ptr
->map_from
= from
;
510 /* Make the real filename absolute. */
515 ptr
->map_to
= xmalloc (dirlen
+ strlen (to
) + 2);
516 strcpy (ptr
->map_to
, dirname
);
517 ptr
->map_to
[dirlen
] = '/';
518 strcpy (ptr
->map_to
+ dirlen
+ 1, to
);
522 ptr
->map_next
= map_list_ptr
->map_list_map
;
523 map_list_ptr
->map_list_map
= ptr
;
525 while ((ch
= getc (f
)) != '\n')
532 map_list_ptr
->map_list_next
= CPP_OPTIONS (pfile
)->map_list
;
533 CPP_OPTIONS (pfile
)->map_list
= map_list_ptr
;
535 return map_list_ptr
->map_list_map
;
538 /* Remap NAME based on the file_name_map (if any) for LOC. */
541 remap_filename (pfile
, name
, loc
)
544 struct file_name_list
*loc
;
546 struct file_name_map
*map
;
547 const char *from
, *p
, *dir
;
550 loc
->name_map
= read_name_map (pfile
,
554 if (loc
->name_map
== (struct file_name_map
*)-1)
557 from
= name
+ strlen (loc
->name
) + 1;
559 for (map
= loc
->name_map
; map
; map
= map
->map_next
)
560 if (!strcmp (map
->map_from
, from
))
563 /* Try to find a mapping file for the particular directory we are
564 looking in. Thus #include <sys/types.h> will look up sys/types.h
565 in /usr/include/header.gcc and look up types.h in
566 /usr/include/sys/header.gcc. */
567 p
= rindex (name
, '/');
571 && strlen (loc
->name
) == (size_t) (p
- name
)
572 && !strncmp (loc
->name
, name
, p
- name
))
573 /* FILENAME is in SEARCHPTR, which we've already checked. */
583 char * newdir
= (char *) alloca (p
- name
+ 1);
584 bcopy (name
, newdir
, p
- name
);
585 newdir
[p
- name
] = '\0';
590 for (map
= read_name_map (pfile
, dir
); map
; map
= map
->map_next
)
591 if (! strcmp (map
->map_from
, name
))
597 /* Read the contents of FD into the buffer on the top of PFILE's stack.
598 IHASH points to the include hash entry for the file associated with
601 The caller is responsible for the cpp_push_buffer. */
604 finclude (pfile
, fd
, ihash
)
607 struct include_hash
*ihash
;
614 if (fstat (fd
, &st
) < 0)
616 if (fcntl (fd
, F_SETFL
, 0) == -1) /* turn off nonblocking mode */
619 fp
= CPP_BUFFER (pfile
);
621 /* If fd points to a plain file, we know how big it is, so we can
622 allocate the buffer all at once. If fd is a pipe or terminal, we
623 can't. Most C source files are 4k or less, so we guess that. If
624 fd is something weird, like a block device or a directory, we
625 don't want to read it at all.
627 Unfortunately, different systems use different st.st_mode values
628 for pipes: some have S_ISFIFO, some S_ISSOCK, some are buggy and
629 zero the entire struct stat except a couple fields. Hence the
632 In all cases, read_and_prescan will resize the buffer if it
633 turns out there's more data than we thought. */
635 if (S_ISREG (st
.st_mode
))
637 /* off_t might have a wider range than size_t - in other words,
638 the max size of a file might be bigger than the address
639 space. We can't handle a file that large. (Anyone with
640 a single source file bigger than 4GB needs to rethink
641 their coding style.) */
642 st_size
= (size_t) st
.st_size
;
643 if ((unsigned HOST_WIDEST_INT
) st_size
644 != (unsigned HOST_WIDEST_INT
) st
.st_size
)
646 cpp_error (pfile
, "file `%s' is too large", ihash
->name
);
650 else if (S_ISFIFO (st
.st_mode
) || S_ISSOCK (st
.st_mode
)
651 /* Some 4.x (x<4) derivatives have a bug that makes fstat() of a
652 socket or pipe return a stat struct with most fields zeroed. */
653 || (st
.st_mode
== 0 && st
.st_nlink
== 0 && st
.st_size
== 0)
654 || (S_ISCHR (st
.st_mode
) && isatty (fd
)))
656 /* Cannot get its file size before reading. 4k is a decent
662 cpp_error (pfile
, "`%s' is not a file, pipe, or tty", ihash
->name
);
666 /* Read the file, converting end-of-line characters and trigraphs
669 fp
->nominal_fname
= fp
->fname
= ihash
->name
;
670 length
= read_and_prescan (pfile
, fp
, fd
, st_size
);
674 ihash
->control_macro
= ""; /* never re-include */
677 fp
->rlimit
= fp
->alimit
= fp
->buf
+ length
;
679 if (ihash
->foundhere
!= ABSOLUTE_PATH
)
680 fp
->system_header_p
= ihash
->foundhere
->sysp
;
683 fp
->line_base
= fp
->buf
;
684 fp
->cleanup
= file_cleanup
;
686 /* The ->actual_dir field is only used when ignore_srcdir is not in effect;
688 if (!CPP_OPTIONS (pfile
)->ignore_srcdir
)
689 fp
->actual_dir
= actual_directory (pfile
, fp
->fname
);
691 pfile
->input_stack_listing_current
= 0;
695 cpp_error_from_errno (pfile
, ihash
->name
);
697 cpp_pop_buffer (pfile
);
702 /* Given a path FNAME, extract the directory component and place it
703 onto the actual_dirs list. Return a pointer to the allocated
704 file_name_list structure. These structures are used to implement
705 current-directory "" include searching. */
707 static struct file_name_list
*
708 actual_directory (pfile
, fname
)
712 char *last_slash
, *dir
;
714 struct file_name_list
*x
;
716 dir
= xstrdup (fname
);
717 last_slash
= rindex (dir
, '/');
720 if (last_slash
== dir
)
723 last_slash
[1] = '\0';
727 dlen
= last_slash
- dir
;
738 if (dlen
> pfile
->max_include_len
)
739 pfile
->max_include_len
= dlen
;
741 for (x
= pfile
->actual_dirs
; x
; x
= x
->alloc
)
742 if (!strcmp (x
->name
, dir
))
748 /* Not found, make a new one. */
749 x
= (struct file_name_list
*) xmalloc (sizeof (struct file_name_list
));
752 x
->next
= CPP_OPTIONS (pfile
)->quote_include
;
753 x
->alloc
= pfile
->actual_dirs
;
754 x
->sysp
= CPP_BUFFER (pfile
)->system_header_p
;
757 pfile
->actual_dirs
= x
;
761 /* Almost but not quite the same as adjust_position in cpplib.c.
762 Used only by read_and_prescan. */
764 find_position (start
, limit
, linep
, colp
)
767 unsigned long *linep
;
770 unsigned long line
= *linep
, col
= 0;
771 while (start
< limit
)
773 U_CHAR ch
= *start
++;
774 if (ch
== '\n' || ch
== '\r')
779 *linep
= line
, *colp
= col
;
782 /* Read the entire contents of file DESC into buffer BUF. LEN is how
783 much memory to allocate initially; more will be allocated if
784 necessary. Convert end-of-line markers (\n, \r, \r\n, \n\r) to
785 canonical form (\n). If enabled, convert and/or warn about
786 trigraphs. Convert backslash-newline to a one-character escape
787 (\r) and remove it from "embarrassing" places (i.e. the middle of a
788 token). If there is no newline at the end of the file, add one and
789 warn. Returns -1 on failure, or the actual length of the data to
792 This function does a lot of work, and can be a serious performance
793 bottleneck. It has been tuned heavily; make sure you understand it
794 before hacking. The common case - no trigraphs, Unix style line
795 breaks, backslash-newline set off by whitespace, newline at EOF -
796 has been optimized at the expense of the others. The performance
797 penalty for DOS style line breaks (\r\n) is about 15%.
799 Warnings lose particularly heavily since we have to determine the
800 line number, which involves scanning from the beginning of the file
801 or from the last warning. The penalty for the absence of a newline
802 at the end of reload1.c is about 60%. (reload1.c is 329k.)
804 If your file has more than one kind of end-of-line marker, you
805 will get messed-up line numbering. */
808 #define PIPE_BUF 4096
812 read_and_prescan (pfile
, fp
, desc
, len
)
818 U_CHAR
*buf
= (U_CHAR
*) xmalloc (len
);
819 U_CHAR
*ip
, *op
, *line_base
;
822 unsigned int deferred_newlines
;
825 /* PIPE_BUF bytes of buffer proper, 2 to detect running off the end
826 without address arithmetic all the time, and 2 for pushback in
827 the case there's a potential trigraph or end-of-line digraph at
828 the end of a block. */
829 U_CHAR intermed
[PIPE_BUF
+ 2 + 2];
831 /* Table of characters that can't be handled in the inner loop.
832 Keep these contiguous to optimize the performance of the code generated
833 for the switch that uses them. */
834 #define SPECCASE_EMPTY 0
835 #define SPECCASE_NUL 1
836 #define SPECCASE_CR 2
837 #define SPECCASE_BACKSLASH 3
838 #define SPECCASE_QUESTION 4
839 U_CHAR speccase
[256];
845 ibase
= intermed
+ 2;
846 deferred_newlines
= 0;
848 memset (speccase
, SPECCASE_EMPTY
, sizeof (speccase
));
849 speccase
['\0'] = SPECCASE_NUL
;
850 speccase
['\r'] = SPECCASE_CR
;
851 speccase
['\\'] = SPECCASE_BACKSLASH
;
852 if (CPP_OPTIONS (pfile
)->trigraphs
|| CPP_OPTIONS (pfile
)->warn_trigraphs
)
853 speccase
['?'] = SPECCASE_QUESTION
;
859 count
= read (desc
, intermed
+ 2, PIPE_BUF
);
867 ibase
= intermed
+ 2;
868 ibase
[count
] = ibase
[count
+1] = '\0';
873 size_t delta_line_base
;
877 This could happen if the file is larger than half the
878 maximum address space of the machine. */
882 delta_line_base
= line_base
- buf
;
883 buf
= (U_CHAR
*) xrealloc (buf
, len
);
885 line_base
= buf
+ delta_line_base
;
890 unsigned int span
= 0;
892 /* Deal with \-newline in the middle of a token. */
893 if (deferred_newlines
)
895 while (speccase
[ip
[span
]] == SPECCASE_EMPTY
900 memcpy (op
, ip
, span
);
903 if (*ip
== '\n' || *ip
== '\t'
904 || *ip
== ' ' || *ip
== ' ')
905 while (deferred_newlines
)
906 deferred_newlines
--, *op
++ = '\r';
910 /* Copy as much as we can without special treatment. */
911 while (speccase
[ip
[span
]] == SPECCASE_EMPTY
) span
++;
912 memcpy (op
, ip
, span
);
916 switch (speccase
[*ip
++])
918 case SPECCASE_NUL
: /* \0 */
922 case SPECCASE_CR
: /* \r */
925 else if (*ip
== '\0')
931 else if (ip
[-2] == '\n')
936 case SPECCASE_BACKSLASH
: /* \ */
939 /* If we're at the end of the intermediate buffer,
940 we have to shift the backslash down to the start
941 and come back next pass. */
948 else if (*ip
== '\n')
951 if (*ip
== '\r') ip
++;
952 if (*ip
== '\n' || *ip
== '\t' || *ip
== ' ')
954 else if (op
[-1] == '\t' || op
[-1] == ' '
955 || op
[-1] == '\r' || op
[-1] == '\n')
962 else if (*ip
== '\r')
965 if (*ip
== '\n') ip
++;
966 else if (*ip
== '\0')
973 else if (*ip
== '\r' || *ip
== '\t' || *ip
== ' ')
985 case SPECCASE_QUESTION
: /* ? */
988 /* If we're at the end of the intermediate buffer,
989 we have to shift the ?'s down to the start and
990 come back next pass. */
1007 intermed
[0] = intermed
[1] = '?';
1010 if (!trigraph_table
[d
])
1016 if (CPP_OPTIONS (pfile
)->warn_trigraphs
)
1019 find_position (line_base
, op
, &line
, &col
);
1020 line_base
= op
- col
;
1021 cpp_warning_with_line (pfile
, line
, col
,
1022 "trigraph ??%c encountered", d
);
1024 if (CPP_OPTIONS (pfile
)->trigraphs
)
1026 if (trigraph_table
[d
] == '\\')
1029 *op
++ = trigraph_table
[d
];
1046 /* Deal with pushed-back chars at true EOF.
1047 This may be any of: ?? ? \ \r \n \\r \\n.
1048 \r must become \n, \\r or \\n must become \r.
1049 We know we have space already. */
1050 if (ibase
== intermed
)
1060 else if (ibase
== intermed
+ 1)
1071 find_position (line_base
, op
, &line
, &col
);
1072 cpp_warning_with_line (pfile
, line
, col
, "no newline at end of file\n");
1073 if (offset
+ 1 > len
)
1076 if (offset
+ 1 > len
)
1078 buf
= (U_CHAR
*) xrealloc (buf
, len
);
1084 fp
->buf
= ((len
- offset
< 20) ? buf
: (U_CHAR
*)xrealloc (buf
, op
- buf
));
1088 cpp_error (pfile
, "file is too large (>%lu bytes)\n", (unsigned long)offset
);
1093 cpp_error_from_errno (pfile
, fp
->fname
);
1098 /* Add output to `deps_buffer' for the -M switch.
1099 STRING points to the text to be output.
1100 SPACER is ':' for targets, ' ' for dependencies, zero for text
1101 to be inserted literally. */
1104 deps_output (pfile
, string
, spacer
)
1115 size
= strlen (string
);
1117 #ifndef MAX_OUTPUT_COLUMNS
1118 #define MAX_OUTPUT_COLUMNS 72
1120 if (pfile
->deps_column
> 0
1121 && (pfile
->deps_column
+ size
) > MAX_OUTPUT_COLUMNS
)
1124 pfile
->deps_column
= 0;
1127 if (pfile
->deps_size
+ size
+ cr
+ 8 > pfile
->deps_allocated_size
)
1129 pfile
->deps_allocated_size
= (pfile
->deps_size
+ size
+ 50) * 2;
1130 pfile
->deps_buffer
= (char *) xrealloc (pfile
->deps_buffer
,
1131 pfile
->deps_allocated_size
);
1136 bcopy (" \\\n ", &pfile
->deps_buffer
[pfile
->deps_size
], 5);
1137 pfile
->deps_size
+= 5;
1140 if (spacer
== ' ' && pfile
->deps_column
> 0)
1141 pfile
->deps_buffer
[pfile
->deps_size
++] = ' ';
1142 bcopy (string
, &pfile
->deps_buffer
[pfile
->deps_size
], size
);
1143 pfile
->deps_size
+= size
;
1144 pfile
->deps_column
+= size
;
1146 pfile
->deps_buffer
[pfile
->deps_size
++] = ':';
1147 pfile
->deps_buffer
[pfile
->deps_size
] = 0;
1150 /* Simplify a path name in place, deleting redundant components. This
1151 reduces OS overhead and guarantees that equivalent paths compare
1152 the same (modulo symlinks).
1155 foo/bar/../quux foo/quux
1159 //quux //quux (POSIX allows leading // as a namespace escape)
1161 Guarantees no trailing slashes. All transforms reduce the length
1165 simplify_pathname (path
)
1172 #if defined (HAVE_DOS_BASED_FILE_SYSTEM)
1173 /* Convert all backslashes to slashes. */
1174 for (from
= path
; *from
; from
++)
1175 if (*from
== '\\') *from
= '/';
1177 /* Skip over leading drive letter if present. */
1178 if (ISALPHA (path
[0]) && path
[1] == ':')
1179 from
= to
= &path
[2];
1186 /* Remove redundant initial /s. */
1195 /* 3 or more initial /s are equivalent to 1 /. */
1196 while (*++from
== '/');
1198 /* On some hosts // differs from /; Posix allows this. */
1206 while (*from
== '/')
1209 if (from
[0] == '.' && from
[1] == '/')
1211 else if (from
[0] == '.' && from
[1] == '\0')
1213 else if (from
[0] == '.' && from
[1] == '.' && from
[2] == '/')
1230 while (to
> base
&& *to
!= '/') to
--;
1236 else if (from
[0] == '.' && from
[1] == '.' && from
[2] == '\0')
1249 while (to
> base
&& *to
!= '/') to
--;
1256 /* Copy this component and trailing /, if any. */
1257 while ((*to
++ = *from
++) != '/')
1269 /* Trim trailing slash */
1270 if (to
[0] == '/' && (!absolute
|| to
> path
+1))
1273 /* Change the empty string to "." so that stat() on the result
1274 will always work. */
1283 /* It is not clear when this should be used if at all, so I've
1284 disabled it until someone who understands VMS can look at it. */
1287 /* Under VMS we need to fix up the "include" specification filename.
1289 Rules for possible conversions
1291 fullname tried paths
1294 ./dir/name [.dir]name
1296 /name [000000]name, name
1297 dir/name dir:[000000]name, dir:name, dir/name
1298 dir1/dir2/name dir1:[dir2]name, dir1:[000000.dir2]name
1299 path:/name path:[000000]name, path:name
1300 path:/dir/name path:[000000.dir]name, path:[dir]name
1301 path:dir/name path:[dir]name
1302 [path]:[dir]name [path.dir]name
1303 path/[dir]name [path.dir]name
1305 The path:/name input is constructed when expanding <> includes. */
1309 hack_vms_include_specification (fullname
)
1312 register char *basename
, *unixname
, *local_ptr
, *first_slash
;
1313 int f
, check_filename_before_returning
, must_revert
;
1316 check_filename_before_returning
= 0;
1318 /* See if we can find a 1st slash. If not, there's no path information. */
1319 first_slash
= index (fullname
, '/');
1320 if (first_slash
== 0)
1321 return 0; /* Nothing to do!!! */
1323 /* construct device spec if none given. */
1325 if (index (fullname
, ':') == 0)
1328 /* If fullname has a slash, take it as device spec. */
1330 if (first_slash
== fullname
)
1332 first_slash
= index (fullname
+1, '/'); /* 2nd slash ? */
1334 *first_slash
= ':'; /* make device spec */
1335 for (basename
= fullname
; *basename
!= 0; basename
++)
1336 *basename
= *(basename
+1); /* remove leading slash */
1338 else if ((first_slash
[-1] != '.') /* keep ':/', './' */
1339 && (first_slash
[-1] != ':')
1340 && (first_slash
[-1] != ']')) /* or a vms path */
1344 else if ((first_slash
[1] == '[') /* skip './' in './[dir' */
1345 && (first_slash
[-1] == '.'))
1349 /* Get part after first ':' (basename[-1] == ':')
1350 or last '/' (basename[-1] == '/'). */
1352 basename
= base_name (fullname
);
1354 local_ptr
= Local
; /* initialize */
1356 /* We are trying to do a number of things here. First of all, we are
1357 trying to hammer the filenames into a standard format, such that later
1358 processing can handle them.
1360 If the file name contains something like [dir.], then it recognizes this
1361 as a root, and strips the ".]". Later processing will add whatever is
1362 needed to get things working properly.
1364 If no device is specified, then the first directory name is taken to be
1365 a device name (or a rooted logical). */
1367 /* Point to the UNIX filename part (which needs to be fixed!)
1368 but skip vms path information.
1369 [basename != fullname since first_slash != 0]. */
1371 if ((basename
[-1] == ':') /* vms path spec. */
1372 || (basename
[-1] == ']')
1373 || (basename
[-1] == '>'))
1374 unixname
= basename
;
1376 unixname
= fullname
;
1378 if (*unixname
== '/')
1381 /* If the directory spec is not rooted, we can just copy
1382 the UNIX filename part and we are done. */
1384 if (((basename
- fullname
) > 1)
1385 && ( (basename
[-1] == ']')
1386 || (basename
[-1] == '>')))
1388 if (basename
[-2] != '.')
1391 /* The VMS part ends in a `]', and the preceding character is not a `.'.
1392 -> PATH]:/name (basename = '/name', unixname = 'name')
1393 We strip the `]', and then splice the two parts of the name in the
1394 usual way. Given the default locations for include files in cccp.c,
1395 we will only use this code if the user specifies alternate locations
1396 with the /include (-I) switch on the command line. */
1398 basename
-= 1; /* Strip "]" */
1399 unixname
--; /* backspace */
1404 /* The VMS part has a ".]" at the end, and this will not do. Later
1405 processing will add a second directory spec, and this would be a syntax
1406 error. Thus we strip the ".]", and thus merge the directory specs.
1407 We also backspace unixname, so that it points to a '/'. This inhibits the
1408 generation of the 000000 root directory spec (which does not belong here
1411 basename
-= 2; /* Strip ".]" */
1412 unixname
--; /* backspace */
1420 /* We drop in here if there is no VMS style directory specification yet.
1421 If there is no device specification either, we make the first dir a
1422 device and try that. If we do not do this, then we will be essentially
1423 searching the users default directory (as if they did a #include "asdf.h").
1425 Then all we need to do is to push a '[' into the output string. Later
1426 processing will fill this in, and close the bracket. */
1428 if ((unixname
!= fullname
) /* vms path spec found. */
1429 && (basename
[-1] != ':'))
1430 *local_ptr
++ = ':'; /* dev not in spec. take first dir */
1432 *local_ptr
++ = '['; /* Open the directory specification */
1435 if (unixname
== fullname
) /* no vms dir spec. */
1438 if ((first_slash
!= 0) /* unix dir spec. */
1439 && (*unixname
!= '/') /* not beginning with '/' */
1440 && (*unixname
!= '.')) /* or './' or '../' */
1441 *local_ptr
++ = '.'; /* dir is local ! */
1444 /* at this point we assume that we have the device spec, and (at least
1445 the opening "[" for a directory specification. We may have directories
1448 If there are no other slashes then the filename will be
1449 in the "root" directory. Otherwise, we need to add
1450 directory specifications. */
1452 if (index (unixname
, '/') == 0)
1454 /* if no directories specified yet and none are following. */
1455 if (local_ptr
[-1] == '[')
1457 /* Just add "000000]" as the directory string */
1458 strcpy (local_ptr
, "000000]");
1459 local_ptr
+= strlen (local_ptr
);
1460 check_filename_before_returning
= 1; /* we might need to fool with this later */
1466 /* As long as there are still subdirectories to add, do them. */
1467 while (index (unixname
, '/') != 0)
1469 /* If this token is "." we can ignore it
1470 if it's not at the beginning of a path. */
1471 if ((unixname
[0] == '.') && (unixname
[1] == '/'))
1473 /* remove it at beginning of path. */
1474 if ( ((unixname
== fullname
) /* no device spec */
1475 && (fullname
+2 != basename
)) /* starts with ./ */
1477 || ((basename
[-1] == ':') /* device spec */
1478 && (unixname
-1 == basename
))) /* and ./ afterwards */
1479 *local_ptr
++ = '.'; /* make '[.' start of path. */
1484 /* Add a subdirectory spec. Do not duplicate "." */
1485 if ( local_ptr
[-1] != '.'
1486 && local_ptr
[-1] != '['
1487 && local_ptr
[-1] != '<')
1490 /* If this is ".." then the spec becomes "-" */
1491 if ( (unixname
[0] == '.')
1492 && (unixname
[1] == '.')
1493 && (unixname
[2] == '/'))
1495 /* Add "-" and skip the ".." */
1496 if ((local_ptr
[-1] == '.')
1497 && (local_ptr
[-2] == '['))
1498 local_ptr
--; /* prevent [.- */
1504 /* Copy the subdirectory */
1505 while (*unixname
!= '/')
1506 *local_ptr
++= *unixname
++;
1508 unixname
++; /* Skip the "/" */
1511 /* Close the directory specification */
1512 if (local_ptr
[-1] == '.') /* no trailing periods */
1515 if (local_ptr
[-1] == '[') /* no dir needed */
1521 /* Now add the filename. */
1524 *local_ptr
++ = *unixname
++;
1527 /* Now append it to the original VMS spec. */
1529 strcpy ((must_revert
==1)?fullname
:basename
, Local
);
1531 /* If we put a [000000] in the filename, try to open it first. If this fails,
1532 remove the [000000], and return that name. This provides flexibility
1533 to the user in that they can use both rooted and non-rooted logical names
1534 to point to the location of the file. */
1536 if (check_filename_before_returning
)
1538 f
= open (fullname
, O_RDONLY
, 0666);
1541 /* The file name is OK as it is, so return it as is. */
1546 /* The filename did not work. Try to remove the [000000] from the name,
1549 basename
= index (fullname
, '[');
1550 local_ptr
= index (fullname
, ']') + 1;
1551 strcpy (basename
, local_ptr
); /* this gets rid of it */