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.
23 In other words, you are welcome to use, share and improve this program.
24 You are forbidden to forbid anyone else to use, share and improve
25 what you give them. Help stamp out software-hoarding! */
32 /* The entry points to this file are: find_include_file, finclude,
33 include_hash, append_include_chain, deps_output, and file_cleanup.
34 file_cleanup is only called through CPP_BUFFER(pfile)->cleanup,
35 so it's static anyway. */
37 static struct include_hash
*redundant_include_p
38 PARAMS ((cpp_reader
*,
39 struct include_hash
*,
40 struct file_name_list
*));
41 static struct file_name_map
*read_name_map
42 PARAMS ((cpp_reader
*, const char *));
43 static char *read_filename_string
PARAMS ((int, FILE *));
44 static char *remap_filename
PARAMS ((cpp_reader
*, char *,
45 struct file_name_list
*));
46 static long read_and_prescan
PARAMS ((cpp_reader
*, cpp_buffer
*,
48 static struct file_name_list
*actual_directory
49 PARAMS ((cpp_reader
*, const char *));
50 static void initialize_input_buffer
PARAMS ((cpp_reader
*, int,
52 static int file_cleanup
PARAMS ((cpp_buffer
*, cpp_reader
*));
53 static U_CHAR
*find_position
PARAMS ((U_CHAR
*, U_CHAR
*,
57 static void hack_vms_include_specification
PARAMS ((char *));
60 /* Windows does not natively support inodes, and neither does MSDOS.
61 Cygwin's emulation can generate non-unique inodes, so don't use it.
62 VMS has non-numeric inodes. */
64 #define INO_T_EQ(a, b) (!bcmp((char *) &(a), (char *) &(b), sizeof (a)))
65 #elif (defined _WIN32 && ! defined (_UWIN)) \
67 #define INO_T_EQ(a, b) 0
69 #define INO_T_EQ(a, b) ((a) == (b))
72 #ifndef INCLUDE_LEN_FUDGE
73 #define INCLUDE_LEN_FUDGE 0
76 /* Merge the four include chains together in the order quote, bracket,
77 system, after. Remove duplicate dirs (as determined by
78 INO_T_EQ()). The system_include and after_include chains are never
79 referred to again after this function; all access is through the
82 For the future: Check if the directory is empty (but
83 how?) and possibly preload the include hash. */
86 merge_include_chains (opts
)
87 struct cpp_options
*opts
;
89 struct file_name_list
*prev
, *cur
, *other
;
90 struct file_name_list
*quote
, *brack
, *systm
, *after
;
91 struct file_name_list
*qtail
, *btail
, *stail
, *atail
;
93 qtail
= opts
->pending
->quote_tail
;
94 btail
= opts
->pending
->brack_tail
;
95 stail
= opts
->pending
->systm_tail
;
96 atail
= opts
->pending
->after_tail
;
98 quote
= opts
->pending
->quote_head
;
99 brack
= opts
->pending
->brack_head
;
100 systm
= opts
->pending
->systm_head
;
101 after
= opts
->pending
->after_head
;
103 /* Paste together bracket, system, and after include chains. */
113 /* This is a bit tricky.
114 First we drop dupes from the quote-include list.
115 Then we drop dupes from the bracket-include list.
116 Finally, if qtail and brack are the same directory,
119 We can't just merge the lists and then uniquify them because
120 then we may lose directories from the <> search path that should
121 be there; consider -Ifoo -Ibar -I- -Ifoo -Iquux. It is however
122 safe to treat -Ibar -Ifoo -I- -Ifoo -Iquux as if written
123 -Ibar -I- -Ifoo -Iquux.
125 Note that this algorithm is quadratic in the number of -I switches,
126 which is acceptable since there aren't usually that many of them. */
128 for (cur
= quote
, prev
= NULL
; cur
; cur
= cur
->next
)
130 for (other
= quote
; other
!= cur
; other
= other
->next
)
131 if (INO_T_EQ (cur
->ino
, other
->ino
)
132 && cur
->dev
== other
->dev
)
135 fprintf (stderr
, _("ignoring duplicate directory `%s'\n"),
138 prev
->next
= cur
->next
;
148 for (cur
= brack
; cur
; cur
= cur
->next
)
150 for (other
= brack
; other
!= cur
; other
= other
->next
)
151 if (INO_T_EQ (cur
->ino
, other
->ino
)
152 && cur
->dev
== other
->dev
)
155 fprintf (stderr
, _("ignoring duplicate directory `%s'\n"),
158 prev
->next
= cur
->next
;
169 if (INO_T_EQ (qtail
->ino
, brack
->ino
) && qtail
->dev
== brack
->dev
)
174 fprintf (stderr
, _("ignoring duplicate directory `%s'\n"),
184 while (cur
->next
!= qtail
)
188 fprintf (stderr
, _("ignoring duplicate directory `%s'\n"),
201 opts
->quote_include
= quote
;
202 opts
->bracket_include
= brack
;
205 /* Look up or add an entry to the table of all includes. This table
206 is indexed by the name as it appears in the #include line. The
207 ->next_this_file chain stores all different files with the same
208 #include name (there are at least three ways this can happen). The
209 hash function could probably be improved a bit. */
211 struct include_hash
*
212 include_hash (pfile
, fname
, add
)
217 unsigned int hash
= 0;
218 struct include_hash
*l
, *m
;
219 const char *f
= fname
;
224 l
= pfile
->all_include_files
[hash
% ALL_INCLUDE_HASHSIZE
];
226 for (; l
; m
= l
, l
= l
->next
)
227 if (!strcmp (l
->nshort
, fname
))
233 l
= (struct include_hash
*) xmalloc (sizeof (struct include_hash
));
235 l
->next_this_file
= NULL
;
242 pfile
->all_include_files
[hash
% ALL_INCLUDE_HASHSIZE
] = l
;
247 /* Return 0 if the file pointed to by IHASH has never been included before,
248 -1 if it has been included before and need not be again,
249 or a pointer to an IHASH entry which is the file to be reread.
250 "Never before" is with respect to the position in ILIST.
252 This will not detect redundancies involving odd uses of the
253 `current directory' rule for "" includes. They aren't quite
254 pathological, but I think they are rare enough not to worry about.
255 The simplest example is:
264 and the problem is that for `current directory' includes,
265 ihash->foundhere is not on any of the global include chains,
266 so the test below (i->foundhere == l) may be false even when
267 the directories are in fact the same. */
269 static struct include_hash
*
270 redundant_include_p (pfile
, ihash
, ilist
)
272 struct include_hash
*ihash
;
273 struct file_name_list
*ilist
;
275 struct file_name_list
*l
;
276 struct include_hash
*i
;
278 if (! ihash
->foundhere
)
281 for (i
= ihash
; i
; i
= i
->next_this_file
)
282 for (l
= ilist
; l
; l
= l
->next
)
283 if (i
->foundhere
== l
)
284 /* The control_macro works like this: If it's NULL, the file
285 is to be included again. If it's "", the file is never to
286 be included again. If it's a string, the file is not to be
287 included again if the string is the name of a defined macro. */
288 return (i
->control_macro
289 && (i
->control_macro
[0] == '\0'
290 || cpp_defined (pfile
, i
->control_macro
, -1)))
291 ? (struct include_hash
*)-1 : i
;
297 file_cleanup (pbuf
, pfile
)
306 if (pfile
->system_include_depth
)
307 pfile
->system_include_depth
--;
311 /* Search for include file FNAME in the include chain starting at
312 SEARCH_START. Return -2 if this file doesn't need to be included
313 (because it was included already and it's marked idempotent),
314 -1 if an error occurred, or a file descriptor open on the file.
315 *IHASH is set to point to the include hash entry for this file, and
316 *BEFORE is 1 if the file was included before (but needs to be read
319 find_include_file (pfile
, fname
, search_start
, ihash
, before
)
322 struct file_name_list
*search_start
;
323 struct include_hash
**ihash
;
326 struct file_name_list
*l
;
327 struct include_hash
*ih
, *jh
;
331 ih
= include_hash (pfile
, fname
, 1);
332 jh
= redundant_include_p (pfile
, ih
,
333 fname
[0] == '/' ? ABSOLUTE_PATH
: search_start
);
340 if (jh
== (struct include_hash
*)-1)
343 return open (jh
->name
, O_RDONLY
, 0666);
347 /* A file is already known by this name, but it's not the same file.
348 Allocate another include_hash block and add it to the next_this_file
351 jh
= (struct include_hash
*)xmalloc (sizeof (struct include_hash
));
352 while (ih
->next_this_file
) ih
= ih
->next_this_file
;
354 ih
->next_this_file
= jh
;
356 ih
= ih
->next_this_file
;
359 ih
->next_this_file
= NULL
;
365 ih
->nshort
= xstrdup (fname
);
366 ih
->control_macro
= NULL
;
368 /* If the pathname is absolute, just open it. */
371 ih
->foundhere
= ABSOLUTE_PATH
;
372 ih
->name
= ih
->nshort
;
373 return open (ih
->name
, O_RDONLY
, 0666);
376 /* Search directory path, trying to open the file. */
378 len
= strlen (fname
);
379 name
= xmalloc (len
+ pfile
->max_include_len
+ 2 + INCLUDE_LEN_FUDGE
);
381 for (l
= search_start
; l
; l
= l
->next
)
383 bcopy (l
->name
, name
, l
->nlen
);
385 strcpy (&name
[l
->nlen
+1], fname
);
386 simplify_pathname (name
);
387 if (CPP_OPTIONS (pfile
)->remap
)
388 name
= remap_filename (pfile
, name
, l
);
390 f
= open (name
, O_RDONLY
|O_NONBLOCK
|O_NOCTTY
, 0666);
392 if (f
== -1 && errno
== EACCES
)
394 cpp_error(pfile
, "included file `%s' exists but is not readable",
403 ih
->name
= xrealloc (name
, strlen (name
)+1);
410 jh
->next_this_file
= NULL
;
414 *ihash
= (struct include_hash
*)-1;
418 /* The file_name_map structure holds a mapping of file names for a
419 particular directory. This mapping is read from the file named
420 FILE_NAME_MAP_FILE in that directory. Such a file can be used to
421 map filenames on a file system with severe filename restrictions,
422 such as DOS. The format of the file name map file is just a series
423 of lines with two tokens on each line. The first token is the name
424 to map, and the second token is the actual name to use. */
428 struct file_name_map
*map_next
;
433 #define FILE_NAME_MAP_FILE "header.gcc"
435 /* Read a space delimited string of unlimited length from a stdio
439 read_filename_string (ch
, f
)
447 set
= alloc
= xmalloc (len
+ 1);
451 while ((ch
= getc (f
)) != EOF
&& ! is_space(ch
))
453 if (set
- alloc
== len
)
456 alloc
= xrealloc (alloc
, len
+ 1);
457 set
= alloc
+ len
/ 2;
467 /* This structure holds a linked list of file name maps, one per directory. */
469 struct file_name_map_list
471 struct file_name_map_list
*map_list_next
;
473 struct file_name_map
*map_list_map
;
476 /* Read the file name map file for DIRNAME. */
478 static struct file_name_map
*
479 read_name_map (pfile
, dirname
)
483 register struct file_name_map_list
*map_list_ptr
;
487 for (map_list_ptr
= CPP_OPTIONS (pfile
)->map_list
; map_list_ptr
;
488 map_list_ptr
= map_list_ptr
->map_list_next
)
489 if (! strcmp (map_list_ptr
->map_list_name
, dirname
))
490 return map_list_ptr
->map_list_map
;
492 map_list_ptr
= ((struct file_name_map_list
*)
493 xmalloc (sizeof (struct file_name_map_list
)));
494 map_list_ptr
->map_list_name
= xstrdup (dirname
);
496 name
= (char *) alloca (strlen (dirname
) + strlen (FILE_NAME_MAP_FILE
) + 2);
497 strcpy (name
, dirname
);
500 strcat (name
, FILE_NAME_MAP_FILE
);
501 f
= fopen (name
, "r");
503 map_list_ptr
->map_list_map
= (struct file_name_map
*)-1;
507 int dirlen
= strlen (dirname
);
509 while ((ch
= getc (f
)) != EOF
)
512 struct file_name_map
*ptr
;
516 from
= read_filename_string (ch
, f
);
517 while ((ch
= getc (f
)) != EOF
&& is_hspace(ch
))
519 to
= read_filename_string (ch
, f
);
521 ptr
= ((struct file_name_map
*)
522 xmalloc (sizeof (struct file_name_map
)));
523 ptr
->map_from
= from
;
525 /* Make the real filename absolute. */
530 ptr
->map_to
= xmalloc (dirlen
+ strlen (to
) + 2);
531 strcpy (ptr
->map_to
, dirname
);
532 ptr
->map_to
[dirlen
] = '/';
533 strcpy (ptr
->map_to
+ dirlen
+ 1, to
);
537 ptr
->map_next
= map_list_ptr
->map_list_map
;
538 map_list_ptr
->map_list_map
= ptr
;
540 while ((ch
= getc (f
)) != '\n')
547 map_list_ptr
->map_list_next
= CPP_OPTIONS (pfile
)->map_list
;
548 CPP_OPTIONS (pfile
)->map_list
= map_list_ptr
;
550 return map_list_ptr
->map_list_map
;
553 /* Remap NAME based on the file_name_map (if any) for LOC. */
556 remap_filename (pfile
, name
, loc
)
559 struct file_name_list
*loc
;
561 struct file_name_map
*map
;
562 const char *from
, *p
, *dir
;
565 loc
->name_map
= read_name_map (pfile
,
569 if (loc
->name_map
== (struct file_name_map
*)-1)
572 from
= name
+ strlen (loc
->name
) + 1;
574 for (map
= loc
->name_map
; map
; map
= map
->map_next
)
575 if (!strcmp (map
->map_from
, from
))
578 /* Try to find a mapping file for the particular directory we are
579 looking in. Thus #include <sys/types.h> will look up sys/types.h
580 in /usr/include/header.gcc and look up types.h in
581 /usr/include/sys/header.gcc. */
582 p
= rindex (name
, '/');
586 && strlen (loc
->name
) == (size_t) (p
- name
)
587 && !strncmp (loc
->name
, name
, p
- name
))
588 /* FILENAME is in SEARCHPTR, which we've already checked. */
598 char * newdir
= (char *) alloca (p
- name
+ 1);
599 bcopy (name
, newdir
, p
- name
);
600 newdir
[p
- name
] = '\0';
605 for (map
= read_name_map (pfile
, dir
); map
; map
= map
->map_next
)
606 if (! strcmp (map
->map_from
, name
))
612 /* Read the contents of FD into the buffer on the top of PFILE's stack.
613 IHASH points to the include hash entry for the file associated with
616 The caller is responsible for the cpp_push_buffer. */
619 finclude (pfile
, fd
, ihash
)
622 struct include_hash
*ihash
;
629 if (fstat (fd
, &st
) < 0)
631 if (fcntl (fd
, F_SETFL
, 0) == -1) /* turn off nonblocking mode */
634 fp
= CPP_BUFFER (pfile
);
636 /* If fd points to a plain file, we know how big it is, so we can
637 allocate the buffer all at once. If fd is a pipe or terminal, we
638 can't. Most C source files are 4k or less, so we guess that. If
639 fd is something weird, like a block device or a directory, we
640 don't want to read it at all.
642 Unfortunately, different systems use different st.st_mode values
643 for pipes: some have S_ISFIFO, some S_ISSOCK, some are buggy and
644 zero the entire struct stat except a couple fields. Hence the
647 In all cases, read_and_prescan will resize the buffer if it
648 turns out there's more data than we thought. */
650 if (S_ISREG (st
.st_mode
))
652 /* off_t might have a wider range than size_t - in other words,
653 the max size of a file might be bigger than the address
654 space. We can't handle a file that large. (Anyone with
655 a single source file bigger than 4GB needs to rethink
656 their coding style.) */
657 st_size
= (size_t) st
.st_size
;
658 if ((unsigned HOST_WIDEST_INT
) st_size
659 != (unsigned HOST_WIDEST_INT
) st
.st_size
)
661 cpp_error (pfile
, "file `%s' is too large", ihash
->name
);
665 else if (S_ISFIFO (st
.st_mode
) || S_ISSOCK (st
.st_mode
)
666 /* Permit any kind of character device: the sensible ones are
667 ttys and /dev/null, but weeding out the others is too hard. */
668 || S_ISCHR (st
.st_mode
)
669 /* Some 4.x (x<4) derivatives have a bug that makes fstat() of a
670 socket or pipe return a stat struct with most fields zeroed. */
671 || (st
.st_mode
== 0 && st
.st_nlink
== 0 && st
.st_size
== 0))
673 /* Cannot get its file size before reading. 4k is a decent
679 cpp_error (pfile
, "`%s' is not a file, pipe, or tty", ihash
->name
);
683 if (pfile
->input_buffer
== NULL
)
684 initialize_input_buffer (pfile
, fd
, &st
);
686 /* Read the file, converting end-of-line characters and trigraphs
689 fp
->nominal_fname
= fp
->fname
= ihash
->name
;
690 length
= read_and_prescan (pfile
, fp
, fd
, st_size
);
694 ihash
->control_macro
= ""; /* never re-include */
697 fp
->rlimit
= fp
->alimit
= fp
->buf
+ length
;
699 if (ihash
->foundhere
!= ABSOLUTE_PATH
)
700 fp
->system_header_p
= ihash
->foundhere
->sysp
;
703 fp
->line_base
= fp
->buf
;
704 fp
->cleanup
= file_cleanup
;
706 /* The ->actual_dir field is only used when ignore_srcdir is not in effect;
708 if (!CPP_OPTIONS (pfile
)->ignore_srcdir
)
709 fp
->actual_dir
= actual_directory (pfile
, fp
->fname
);
711 pfile
->input_stack_listing_current
= 0;
715 cpp_error_from_errno (pfile
, ihash
->name
);
717 cpp_pop_buffer (pfile
);
722 /* Given a path FNAME, extract the directory component and place it
723 onto the actual_dirs list. Return a pointer to the allocated
724 file_name_list structure. These structures are used to implement
725 current-directory "" include searching. */
727 static struct file_name_list
*
728 actual_directory (pfile
, fname
)
732 char *last_slash
, *dir
;
734 struct file_name_list
*x
;
736 dir
= xstrdup (fname
);
737 last_slash
= rindex (dir
, '/');
740 if (last_slash
== dir
)
743 last_slash
[1] = '\0';
747 dlen
= last_slash
- dir
;
758 if (dlen
> pfile
->max_include_len
)
759 pfile
->max_include_len
= dlen
;
761 for (x
= pfile
->actual_dirs
; x
; x
= x
->alloc
)
762 if (!strcmp (x
->name
, dir
))
768 /* Not found, make a new one. */
769 x
= (struct file_name_list
*) xmalloc (sizeof (struct file_name_list
));
772 x
->next
= CPP_OPTIONS (pfile
)->quote_include
;
773 x
->alloc
= pfile
->actual_dirs
;
774 x
->sysp
= CPP_BUFFER (pfile
)->system_header_p
;
777 pfile
->actual_dirs
= x
;
781 /* Determine the current line and column. Used only by read_and_prescan. */
783 find_position (start
, limit
, linep
)
786 unsigned long *linep
;
788 unsigned long line
= *linep
;
789 U_CHAR
*lbase
= start
;
790 while (start
< limit
)
792 U_CHAR ch
= *start
++;
793 if (ch
== '\n' || ch
== '\r')
803 /* Read the entire contents of file DESC into buffer BUF. LEN is how
804 much memory to allocate initially; more will be allocated if
805 necessary. Convert end-of-line markers (\n, \r, \r\n, \n\r) to
806 canonical form (\n). If enabled, convert and/or warn about
807 trigraphs. Convert backslash-newline to a one-character escape
808 (\r) and remove it from "embarrassing" places (i.e. the middle of a
809 token). If there is no newline at the end of the file, add one and
810 warn. Returns -1 on failure, or the actual length of the data to
813 This function does a lot of work, and can be a serious performance
814 bottleneck. It has been tuned heavily; make sure you understand it
815 before hacking. The common case - no trigraphs, Unix style line
816 breaks, backslash-newline set off by whitespace, newline at EOF -
817 has been optimized at the expense of the others. The performance
818 penalty for DOS style line breaks (\r\n) is about 15%.
820 Warnings lose particularly heavily since we have to determine the
821 line number, which involves scanning from the beginning of the file
822 or from the last warning. The penalty for the absence of a newline
823 at the end of reload1.c is about 60%. (reload1.c is 329k.)
825 If your file has more than one kind of end-of-line marker, you
826 will get messed-up line numbering. */
828 /* Table of characters that can't be handled in the inner loop.
829 Keep these contiguous to optimize the performance of the code generated
830 for the switch that uses them. */
831 #define SPECCASE_EMPTY 0
832 #define SPECCASE_NUL 1
833 #define SPECCASE_CR 2
834 #define SPECCASE_BACKSLASH 3
835 #define SPECCASE_QUESTION 4
838 read_and_prescan (pfile
, fp
, desc
, len
)
844 U_CHAR
*buf
= (U_CHAR
*) xmalloc (len
);
845 U_CHAR
*ip
, *op
, *line_base
;
847 U_CHAR
*speccase
= pfile
->input_speccase
;
849 unsigned int deferred_newlines
;
857 ibase
= pfile
->input_buffer
+ 2;
858 deferred_newlines
= 0;
864 count
= read (desc
, pfile
->input_buffer
+ 2, pfile
->input_buffer_len
);
872 ibase
= pfile
->input_buffer
+ 2;
873 ibase
[count
] = ibase
[count
+1] = '\0';
878 size_t delta_line_base
;
882 This could happen if the file is larger than half the
883 maximum address space of the machine. */
887 delta_line_base
= line_base
- buf
;
888 buf
= (U_CHAR
*) xrealloc (buf
, len
);
890 line_base
= buf
+ delta_line_base
;
895 unsigned int span
= 0;
897 /* Deal with \-newline in the middle of a token. */
898 if (deferred_newlines
)
900 while (speccase
[ip
[span
]] == SPECCASE_EMPTY
905 memcpy (op
, ip
, span
);
908 /* If ip[0] is SPECCASE_EMPTY, we have hit white space.
909 Dump out the remaining deferred \-newlines. */
910 if (speccase
[ip
[0]] == SPECCASE_EMPTY
)
911 while (deferred_newlines
)
912 deferred_newlines
--, *op
++ = '\r';
916 /* Copy as much as we can without special treatment. */
917 while (speccase
[ip
[span
]] == SPECCASE_EMPTY
) span
++;
918 memcpy (op
, ip
, span
);
922 switch (speccase
[*ip
++])
924 case SPECCASE_NUL
: /* \0 */
928 case SPECCASE_CR
: /* \r */
931 else if (*ip
== '\n')
933 else if (*ip
== '\0')
941 case SPECCASE_BACKSLASH
: /* \ */
944 /* If we're at the end of the intermediate buffer,
945 we have to shift the backslash down to the start
946 and come back next pass. */
952 else if (*ip
== '\n')
955 if (*ip
== '\r') ip
++;
956 if (*ip
== '\n' || *ip
== '\t' || *ip
== ' ')
958 else if (op
[-1] == '\t' || op
[-1] == ' '
959 || op
[-1] == '\r' || op
[-1] == '\n')
964 else if (*ip
== '\r')
967 if (*ip
== '\n') ip
++;
968 else if (*ip
== '\0')
974 else if (*ip
== '\r' || *ip
== '\t' || *ip
== ' ')
984 case SPECCASE_QUESTION
: /* ? */
987 /* If we're at the end of the intermediate buffer,
988 we have to shift the ?'s down to the start and
989 come back next pass. */
1010 * from to from to from to
1011 * ?? = # ?? ) ] ?? ! |
1012 * ?? ( [ ?? ' ^ ?? > }
1013 * ?? / \ ?? < { ?? - ~
1015 if (d
== '=') t
= '#';
1016 else if (d
== ')') t
= ']';
1017 else if (d
== '!') t
= '|';
1018 else if (d
== '(') t
= '[';
1019 else if (d
== '\'') t
= '^';
1020 else if (d
== '>') t
= '}';
1021 else if (d
== '/') t
= '\\';
1022 else if (d
== '<') t
= '{';
1023 else if (d
== '-') t
= '~';
1030 if (CPP_OPTIONS (pfile
)->warn_trigraphs
)
1033 line_base
= find_position (line_base
, op
, &line
);
1034 col
= op
- line_base
+ 1;
1035 if (CPP_OPTIONS (pfile
)->trigraphs
)
1036 cpp_warning_with_line (pfile
, line
, col
,
1037 "trigraph ??%c converted to %c", d
, t
);
1039 cpp_warning_with_line (pfile
, line
, col
,
1040 "trigraph ??%c ignored", d
);
1042 if (CPP_OPTIONS (pfile
)->trigraphs
)
1063 /* Deal with pushed-back chars at true EOF.
1064 This may be any of: ?? ? \ \r \n \\r \\n.
1065 \r must become \n, \\r or \\n must become \r.
1066 We know we have space already. */
1067 if (ibase
== pfile
->input_buffer
)
1077 else if (ibase
== pfile
->input_buffer
+ 1)
1088 line_base
= find_position (line_base
, op
, &line
);
1089 col
= op
- line_base
+ 1;
1090 cpp_warning_with_line (pfile
, line
, col
, "no newline at end of file\n");
1091 if (offset
+ 1 > len
)
1094 if (offset
+ 1 > len
)
1096 buf
= (U_CHAR
*) xrealloc (buf
, len
);
1102 fp
->buf
= ((len
- offset
< 20) ? buf
: (U_CHAR
*)xrealloc (buf
, op
- buf
));
1106 cpp_error (pfile
, "file is too large (>%lu bytes)\n", (unsigned long)offset
);
1111 cpp_error_from_errno (pfile
, fp
->fname
);
1116 /* Initialize the `input_buffer' and `input_speccase' tables.
1117 These are only used by read_and_prescan, but they're large and
1118 somewhat expensive to set up, so we want them allocated once for
1119 the duration of the cpp run. */
1122 initialize_input_buffer (pfile
, fd
, st
)
1130 /* Table of characters that cannot be handled by the
1131 read_and_prescan inner loop. The number of non-EMPTY entries
1132 should be as small as humanly possible. */
1134 tmp
= (U_CHAR
*) xmalloc (1 << CHAR_BIT
);
1135 memset (tmp
, SPECCASE_EMPTY
, 1 << CHAR_BIT
);
1136 tmp
['\0'] = SPECCASE_NUL
;
1137 tmp
['\r'] = SPECCASE_CR
;
1138 tmp
['\\'] = SPECCASE_BACKSLASH
;
1139 if (CPP_OPTIONS (pfile
)->trigraphs
|| CPP_OPTIONS (pfile
)->warn_trigraphs
)
1140 tmp
['?'] = SPECCASE_QUESTION
;
1142 pfile
->input_speccase
= tmp
;
1144 /* Determine the appropriate size for the input buffer. Normal C
1145 source files are smaller than eight K. If we are reading a pipe,
1146 we want to make sure the input buffer is bigger than the kernel's
1150 if (! S_ISREG (st
->st_mode
))
1153 pipe_buf
= fpathconf (fd
, _PC_PIPE_BUF
);
1158 pipe_buf
= PIPE_BUF
;
1165 if (pipe_buf
< 8192)
1167 /* PIPE_BUF bytes of buffer proper, 2 to detect running off the end
1168 without address arithmetic all the time, and 2 for pushback in
1169 the case there's a potential trigraph or end-of-line digraph at
1170 the end of a block. */
1172 tmp
= (U_CHAR
*) xmalloc (pipe_buf
+ 2 + 2);
1173 pfile
->input_buffer
= tmp
;
1174 pfile
->input_buffer_len
= pipe_buf
;
1177 /* Add output to `deps_buffer' for the -M switch.
1178 STRING points to the text to be output.
1179 SPACER is ':' for targets, ' ' for dependencies, zero for text
1180 to be inserted literally. */
1183 deps_output (pfile
, string
, spacer
)
1194 size
= strlen (string
);
1196 #ifndef MAX_OUTPUT_COLUMNS
1197 #define MAX_OUTPUT_COLUMNS 72
1199 if (pfile
->deps_column
> 0
1200 && (pfile
->deps_column
+ size
) > MAX_OUTPUT_COLUMNS
)
1203 pfile
->deps_column
= 0;
1206 if (pfile
->deps_size
+ size
+ cr
+ 8 > pfile
->deps_allocated_size
)
1208 pfile
->deps_allocated_size
= (pfile
->deps_size
+ size
+ 50) * 2;
1209 pfile
->deps_buffer
= (char *) xrealloc (pfile
->deps_buffer
,
1210 pfile
->deps_allocated_size
);
1215 bcopy (" \\\n ", &pfile
->deps_buffer
[pfile
->deps_size
], 5);
1216 pfile
->deps_size
+= 5;
1219 if (spacer
== ' ' && pfile
->deps_column
> 0)
1220 pfile
->deps_buffer
[pfile
->deps_size
++] = ' ';
1221 bcopy (string
, &pfile
->deps_buffer
[pfile
->deps_size
], size
);
1222 pfile
->deps_size
+= size
;
1223 pfile
->deps_column
+= size
+ 1; /* count spacer too */
1225 pfile
->deps_buffer
[pfile
->deps_size
++] = ':';
1226 pfile
->deps_buffer
[pfile
->deps_size
] = 0;
1229 /* Simplify a path name in place, deleting redundant components. This
1230 reduces OS overhead and guarantees that equivalent paths compare
1231 the same (modulo symlinks).
1234 foo/bar/../quux foo/quux
1238 //quux //quux (POSIX allows leading // as a namespace escape)
1240 Guarantees no trailing slashes. All transforms reduce the length
1244 simplify_pathname (path
)
1251 #if defined (HAVE_DOS_BASED_FILE_SYSTEM)
1252 /* Convert all backslashes to slashes. */
1253 for (from
= path
; *from
; from
++)
1254 if (*from
== '\\') *from
= '/';
1256 /* Skip over leading drive letter if present. */
1257 if (ISALPHA (path
[0]) && path
[1] == ':')
1258 from
= to
= &path
[2];
1265 /* Remove redundant initial /s. */
1274 /* 3 or more initial /s are equivalent to 1 /. */
1275 while (*++from
== '/');
1277 /* On some hosts // differs from /; Posix allows this. */
1285 while (*from
== '/')
1288 if (from
[0] == '.' && from
[1] == '/')
1290 else if (from
[0] == '.' && from
[1] == '\0')
1292 else if (from
[0] == '.' && from
[1] == '.' && from
[2] == '/')
1309 while (to
> base
&& *to
!= '/') to
--;
1315 else if (from
[0] == '.' && from
[1] == '.' && from
[2] == '\0')
1328 while (to
> base
&& *to
!= '/') to
--;
1335 /* Copy this component and trailing /, if any. */
1336 while ((*to
++ = *from
++) != '/')
1348 /* Trim trailing slash */
1349 if (to
[0] == '/' && (!absolute
|| to
> path
+1))
1352 /* Change the empty string to "." so that stat() on the result
1353 will always work. */
1362 /* It is not clear when this should be used if at all, so I've
1363 disabled it until someone who understands VMS can look at it. */
1366 /* Under VMS we need to fix up the "include" specification filename.
1368 Rules for possible conversions
1370 fullname tried paths
1373 ./dir/name [.dir]name
1375 /name [000000]name, name
1376 dir/name dir:[000000]name, dir:name, dir/name
1377 dir1/dir2/name dir1:[dir2]name, dir1:[000000.dir2]name
1378 path:/name path:[000000]name, path:name
1379 path:/dir/name path:[000000.dir]name, path:[dir]name
1380 path:dir/name path:[dir]name
1381 [path]:[dir]name [path.dir]name
1382 path/[dir]name [path.dir]name
1384 The path:/name input is constructed when expanding <> includes. */
1388 hack_vms_include_specification (fullname
)
1391 register char *basename
, *unixname
, *local_ptr
, *first_slash
;
1392 int f
, check_filename_before_returning
, must_revert
;
1395 check_filename_before_returning
= 0;
1397 /* See if we can find a 1st slash. If not, there's no path information. */
1398 first_slash
= index (fullname
, '/');
1399 if (first_slash
== 0)
1400 return 0; /* Nothing to do!!! */
1402 /* construct device spec if none given. */
1404 if (index (fullname
, ':') == 0)
1407 /* If fullname has a slash, take it as device spec. */
1409 if (first_slash
== fullname
)
1411 first_slash
= index (fullname
+1, '/'); /* 2nd slash ? */
1413 *first_slash
= ':'; /* make device spec */
1414 for (basename
= fullname
; *basename
!= 0; basename
++)
1415 *basename
= *(basename
+1); /* remove leading slash */
1417 else if ((first_slash
[-1] != '.') /* keep ':/', './' */
1418 && (first_slash
[-1] != ':')
1419 && (first_slash
[-1] != ']')) /* or a vms path */
1423 else if ((first_slash
[1] == '[') /* skip './' in './[dir' */
1424 && (first_slash
[-1] == '.'))
1428 /* Get part after first ':' (basename[-1] == ':')
1429 or last '/' (basename[-1] == '/'). */
1431 basename
= base_name (fullname
);
1433 local_ptr
= Local
; /* initialize */
1435 /* We are trying to do a number of things here. First of all, we are
1436 trying to hammer the filenames into a standard format, such that later
1437 processing can handle them.
1439 If the file name contains something like [dir.], then it recognizes this
1440 as a root, and strips the ".]". Later processing will add whatever is
1441 needed to get things working properly.
1443 If no device is specified, then the first directory name is taken to be
1444 a device name (or a rooted logical). */
1446 /* Point to the UNIX filename part (which needs to be fixed!)
1447 but skip vms path information.
1448 [basename != fullname since first_slash != 0]. */
1450 if ((basename
[-1] == ':') /* vms path spec. */
1451 || (basename
[-1] == ']')
1452 || (basename
[-1] == '>'))
1453 unixname
= basename
;
1455 unixname
= fullname
;
1457 if (*unixname
== '/')
1460 /* If the directory spec is not rooted, we can just copy
1461 the UNIX filename part and we are done. */
1463 if (((basename
- fullname
) > 1)
1464 && ( (basename
[-1] == ']')
1465 || (basename
[-1] == '>')))
1467 if (basename
[-2] != '.')
1470 /* The VMS part ends in a `]', and the preceding character is not a `.'.
1471 -> PATH]:/name (basename = '/name', unixname = 'name')
1472 We strip the `]', and then splice the two parts of the name in the
1473 usual way. Given the default locations for include files in cccp.c,
1474 we will only use this code if the user specifies alternate locations
1475 with the /include (-I) switch on the command line. */
1477 basename
-= 1; /* Strip "]" */
1478 unixname
--; /* backspace */
1483 /* The VMS part has a ".]" at the end, and this will not do. Later
1484 processing will add a second directory spec, and this would be a syntax
1485 error. Thus we strip the ".]", and thus merge the directory specs.
1486 We also backspace unixname, so that it points to a '/'. This inhibits the
1487 generation of the 000000 root directory spec (which does not belong here
1490 basename
-= 2; /* Strip ".]" */
1491 unixname
--; /* backspace */
1499 /* We drop in here if there is no VMS style directory specification yet.
1500 If there is no device specification either, we make the first dir a
1501 device and try that. If we do not do this, then we will be essentially
1502 searching the users default directory (as if they did a #include "asdf.h").
1504 Then all we need to do is to push a '[' into the output string. Later
1505 processing will fill this in, and close the bracket. */
1507 if ((unixname
!= fullname
) /* vms path spec found. */
1508 && (basename
[-1] != ':'))
1509 *local_ptr
++ = ':'; /* dev not in spec. take first dir */
1511 *local_ptr
++ = '['; /* Open the directory specification */
1514 if (unixname
== fullname
) /* no vms dir spec. */
1517 if ((first_slash
!= 0) /* unix dir spec. */
1518 && (*unixname
!= '/') /* not beginning with '/' */
1519 && (*unixname
!= '.')) /* or './' or '../' */
1520 *local_ptr
++ = '.'; /* dir is local ! */
1523 /* at this point we assume that we have the device spec, and (at least
1524 the opening "[" for a directory specification. We may have directories
1527 If there are no other slashes then the filename will be
1528 in the "root" directory. Otherwise, we need to add
1529 directory specifications. */
1531 if (index (unixname
, '/') == 0)
1533 /* if no directories specified yet and none are following. */
1534 if (local_ptr
[-1] == '[')
1536 /* Just add "000000]" as the directory string */
1537 strcpy (local_ptr
, "000000]");
1538 local_ptr
+= strlen (local_ptr
);
1539 check_filename_before_returning
= 1; /* we might need to fool with this later */
1545 /* As long as there are still subdirectories to add, do them. */
1546 while (index (unixname
, '/') != 0)
1548 /* If this token is "." we can ignore it
1549 if it's not at the beginning of a path. */
1550 if ((unixname
[0] == '.') && (unixname
[1] == '/'))
1552 /* remove it at beginning of path. */
1553 if ( ((unixname
== fullname
) /* no device spec */
1554 && (fullname
+2 != basename
)) /* starts with ./ */
1556 || ((basename
[-1] == ':') /* device spec */
1557 && (unixname
-1 == basename
))) /* and ./ afterwards */
1558 *local_ptr
++ = '.'; /* make '[.' start of path. */
1563 /* Add a subdirectory spec. Do not duplicate "." */
1564 if ( local_ptr
[-1] != '.'
1565 && local_ptr
[-1] != '['
1566 && local_ptr
[-1] != '<')
1569 /* If this is ".." then the spec becomes "-" */
1570 if ( (unixname
[0] == '.')
1571 && (unixname
[1] == '.')
1572 && (unixname
[2] == '/'))
1574 /* Add "-" and skip the ".." */
1575 if ((local_ptr
[-1] == '.')
1576 && (local_ptr
[-2] == '['))
1577 local_ptr
--; /* prevent [.- */
1583 /* Copy the subdirectory */
1584 while (*unixname
!= '/')
1585 *local_ptr
++= *unixname
++;
1587 unixname
++; /* Skip the "/" */
1590 /* Close the directory specification */
1591 if (local_ptr
[-1] == '.') /* no trailing periods */
1594 if (local_ptr
[-1] == '[') /* no dir needed */
1600 /* Now add the filename. */
1603 *local_ptr
++ = *unixname
++;
1606 /* Now append it to the original VMS spec. */
1608 strcpy ((must_revert
==1)?fullname
:basename
, Local
);
1610 /* If we put a [000000] in the filename, try to open it first. If this fails,
1611 remove the [000000], and return that name. This provides flexibility
1612 to the user in that they can use both rooted and non-rooted logical names
1613 to point to the location of the file. */
1615 if (check_filename_before_returning
)
1617 f
= open (fullname
, O_RDONLY
, 0666);
1620 /* The file name is OK as it is, so return it as is. */
1625 /* The filename did not work. Try to remove the [000000] from the name,
1628 basename
= index (fullname
, '[');
1629 local_ptr
= index (fullname
, ']') + 1;
1630 strcpy (basename
, local_ptr
); /* this gets rid of it */