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! */
33 static IHASH
*include_hash
PARAMS ((cpp_reader
*, const char *, int));
34 static IHASH
*redundant_include_p
PARAMS ((cpp_reader
*, IHASH
*,
35 struct file_name_list
*));
36 static struct file_name_map
*read_name_map
37 PARAMS ((cpp_reader
*, const char *));
38 static char *read_filename_string
PARAMS ((int, FILE *));
39 static char *remap_filename
PARAMS ((cpp_reader
*, char *,
40 struct file_name_list
*));
41 static long read_and_prescan
PARAMS ((cpp_reader
*, cpp_buffer
*,
43 static struct file_name_list
*actual_directory
44 PARAMS ((cpp_reader
*, const char *));
45 static void init_input_buffer
PARAMS ((cpp_reader
*, int, struct stat
*));
46 static int file_cleanup
PARAMS ((cpp_buffer
*, cpp_reader
*));
47 static U_CHAR
*find_position
PARAMS ((U_CHAR
*, U_CHAR
*, unsigned long *));
50 static void hack_vms_include_specification
PARAMS ((char *));
53 #ifndef INCLUDE_LEN_FUDGE
54 #define INCLUDE_LEN_FUDGE 0
57 /* Look up or add an entry to the table of all includes. This table
58 is indexed by the name as it appears in the #include line. The
59 ->next_this_file chain stores all different files with the same
60 #include name (there are at least three ways this can happen). The
61 hash function could probably be improved a bit. */
64 include_hash (pfile
, fname
, add
)
69 unsigned int hash
= 0;
71 const char *f
= fname
;
76 l
= pfile
->all_include_files
[hash
% ALL_INCLUDE_HASHSIZE
];
78 for (; l
; m
= l
, l
= l
->next
)
79 if (!strcmp (l
->nshort
, fname
))
85 l
= (IHASH
*) xmalloc (sizeof (IHASH
));
87 l
->next_this_file
= NULL
;
94 pfile
->all_include_files
[hash
% ALL_INCLUDE_HASHSIZE
] = l
;
99 /* Return 0 if the file pointed to by IHASH has never been included before,
100 -1 if it has been included before and need not be again,
101 or a pointer to an IHASH entry which is the file to be reread.
102 "Never before" is with respect to the position in ILIST.
104 This will not detect redundancies involving odd uses of the
105 `current directory' rule for "" includes. They aren't quite
106 pathological, but I think they are rare enough not to worry about.
107 The simplest example is:
116 and the problem is that for `current directory' includes,
117 ihash->foundhere is not on any of the global include chains,
118 so the test below (i->foundhere == l) may be false even when
119 the directories are in fact the same. */
122 redundant_include_p (pfile
, ihash
, ilist
)
125 struct file_name_list
*ilist
;
127 struct file_name_list
*l
;
130 if (! ihash
->foundhere
)
133 for (i
= ihash
; i
; i
= i
->next_this_file
)
134 for (l
= ilist
; l
; l
= l
->next
)
135 if (i
->foundhere
== l
)
136 /* The control_macro works like this: If it's NULL, the file
137 is to be included again. If it's "", the file is never to
138 be included again. If it's a string, the file is not to be
139 included again if the string is the name of a defined macro. */
140 return (i
->control_macro
141 && (i
->control_macro
[0] == '\0'
142 || cpp_defined (pfile
, i
->control_macro
, -1)))
148 /* Return 1 if the file named by FNAME has been included before in
149 any context, 0 otherwise. */
151 cpp_included (pfile
, fname
)
157 ptr
= include_hash (pfile
, fname
, 0);
158 return (ptr
!= NULL
);
162 file_cleanup (pbuf
, pfile
)
168 free ((PTR
) pbuf
->buf
);
171 if (pfile
->system_include_depth
)
172 pfile
->system_include_depth
--;
176 /* Search for include file FNAME in the include chain starting at
177 SEARCH_START. Return -2 if this file doesn't need to be included
178 (because it was included already and it's marked idempotent),
179 -1 if an error occurred, or a file descriptor open on the file.
180 *IHASH is set to point to the include hash entry for this file, and
181 *BEFORE is 1 if the file was included before (but needs to be read
184 _cpp_find_include_file (pfile
, fname
, search_start
, ihash
, before
)
187 struct file_name_list
*search_start
;
191 struct file_name_list
*l
;
196 ih
= include_hash (pfile
, fname
, 1);
197 jh
= redundant_include_p (pfile
, ih
,
198 fname
[0] == '/' ? ABSOLUTE_PATH
: search_start
);
205 if (jh
== (IHASH
*)-1)
208 return open (jh
->name
, O_RDONLY
, 0666);
212 /* A file is already known by this name, but it's not the same file.
213 Allocate another include_hash block and add it to the next_this_file
216 jh
= (IHASH
*) xmalloc (sizeof (IHASH
));
217 while (ih
->next_this_file
) ih
= ih
->next_this_file
;
219 ih
->next_this_file
= jh
;
221 ih
= ih
->next_this_file
;
224 ih
->next_this_file
= NULL
;
231 ih
->nshort
= xstrdup (fname
);
232 ih
->control_macro
= NULL
;
234 /* If the pathname is absolute, just open it. */
237 ih
->foundhere
= ABSOLUTE_PATH
;
238 ih
->name
= ih
->nshort
;
239 return open (ih
->name
, O_RDONLY
, 0666);
242 /* Search directory path, trying to open the file. */
244 len
= strlen (fname
);
245 name
= xmalloc (len
+ pfile
->max_include_len
+ 2 + INCLUDE_LEN_FUDGE
);
247 for (l
= search_start
; l
; l
= l
->next
)
249 memcpy (name
, l
->name
, l
->nlen
);
251 strcpy (&name
[l
->nlen
+1], fname
);
252 _cpp_simplify_pathname (name
);
253 if (CPP_OPTIONS (pfile
)->remap
)
254 name
= remap_filename (pfile
, name
, l
);
256 f
= open (name
, O_RDONLY
|O_NONBLOCK
|O_NOCTTY
, 0666);
258 if (f
== -1 && errno
== EACCES
)
260 cpp_error(pfile
, "included file `%s' exists but is not readable",
269 ih
->name
= xrealloc (name
, strlen (name
) + 1);
276 jh
->next_this_file
= NULL
;
280 *ihash
= (IHASH
*)-1;
284 /* The file_name_map structure holds a mapping of file names for a
285 particular directory. This mapping is read from the file named
286 FILE_NAME_MAP_FILE in that directory. Such a file can be used to
287 map filenames on a file system with severe filename restrictions,
288 such as DOS. The format of the file name map file is just a series
289 of lines with two tokens on each line. The first token is the name
290 to map, and the second token is the actual name to use. */
294 struct file_name_map
*map_next
;
299 #define FILE_NAME_MAP_FILE "header.gcc"
301 /* Read a space delimited string of unlimited length from a stdio
305 read_filename_string (ch
, f
)
313 set
= alloc
= xmalloc (len
+ 1);
317 while ((ch
= getc (f
)) != EOF
&& ! is_space(ch
))
319 if (set
- alloc
== len
)
322 alloc
= xrealloc (alloc
, len
+ 1);
323 set
= alloc
+ len
/ 2;
333 /* This structure holds a linked list of file name maps, one per directory. */
335 struct file_name_map_list
337 struct file_name_map_list
*map_list_next
;
339 struct file_name_map
*map_list_map
;
342 /* Read the file name map file for DIRNAME. */
344 static struct file_name_map
*
345 read_name_map (pfile
, dirname
)
349 register struct file_name_map_list
*map_list_ptr
;
353 for (map_list_ptr
= CPP_OPTIONS (pfile
)->map_list
; map_list_ptr
;
354 map_list_ptr
= map_list_ptr
->map_list_next
)
355 if (! strcmp (map_list_ptr
->map_list_name
, dirname
))
356 return map_list_ptr
->map_list_map
;
358 map_list_ptr
= ((struct file_name_map_list
*)
359 xmalloc (sizeof (struct file_name_map_list
)));
360 map_list_ptr
->map_list_name
= xstrdup (dirname
);
362 name
= (char *) alloca (strlen (dirname
) + strlen (FILE_NAME_MAP_FILE
) + 2);
363 strcpy (name
, dirname
);
366 strcat (name
, FILE_NAME_MAP_FILE
);
367 f
= fopen (name
, "r");
369 map_list_ptr
->map_list_map
= (struct file_name_map
*)-1;
373 int dirlen
= strlen (dirname
);
375 while ((ch
= getc (f
)) != EOF
)
378 struct file_name_map
*ptr
;
382 from
= read_filename_string (ch
, f
);
383 while ((ch
= getc (f
)) != EOF
&& is_hspace(ch
))
385 to
= read_filename_string (ch
, f
);
387 ptr
= ((struct file_name_map
*)
388 xmalloc (sizeof (struct file_name_map
)));
389 ptr
->map_from
= from
;
391 /* Make the real filename absolute. */
396 ptr
->map_to
= xmalloc (dirlen
+ strlen (to
) + 2);
397 strcpy (ptr
->map_to
, dirname
);
398 ptr
->map_to
[dirlen
] = '/';
399 strcpy (ptr
->map_to
+ dirlen
+ 1, to
);
403 ptr
->map_next
= map_list_ptr
->map_list_map
;
404 map_list_ptr
->map_list_map
= ptr
;
406 while ((ch
= getc (f
)) != '\n')
413 map_list_ptr
->map_list_next
= CPP_OPTIONS (pfile
)->map_list
;
414 CPP_OPTIONS (pfile
)->map_list
= map_list_ptr
;
416 return map_list_ptr
->map_list_map
;
419 /* Remap NAME based on the file_name_map (if any) for LOC. */
422 remap_filename (pfile
, name
, loc
)
425 struct file_name_list
*loc
;
427 struct file_name_map
*map
;
428 const char *from
, *p
, *dir
;
431 loc
->name_map
= read_name_map (pfile
,
435 if (loc
->name_map
== (struct file_name_map
*)-1)
438 from
= name
+ strlen (loc
->name
) + 1;
440 for (map
= loc
->name_map
; map
; map
= map
->map_next
)
441 if (!strcmp (map
->map_from
, from
))
444 /* Try to find a mapping file for the particular directory we are
445 looking in. Thus #include <sys/types.h> will look up sys/types.h
446 in /usr/include/header.gcc and look up types.h in
447 /usr/include/sys/header.gcc. */
448 p
= strrchr (name
, '/');
452 && strlen (loc
->name
) == (size_t) (p
- name
)
453 && !strncmp (loc
->name
, name
, p
- name
))
454 /* FILENAME is in SEARCHPTR, which we've already checked. */
464 char * newdir
= (char *) alloca (p
- name
+ 1);
465 memcpy (newdir
, name
, p
- name
);
466 newdir
[p
- name
] = '\0';
471 for (map
= read_name_map (pfile
, dir
); map
; map
= map
->map_next
)
472 if (! strcmp (map
->map_from
, name
))
478 /* Push an input buffer and load it up with the contents of FNAME.
479 If FNAME is "" or NULL, read standard input. */
481 cpp_read_file (pfile
, fname
)
488 if (fname
== NULL
|| *fname
== 0)
494 /* Open the file in nonblocking mode, so we don't get stuck if
495 someone clever has asked cpp to process /dev/rmt0.
496 _cpp_read_include_file will check that we have a real file to
497 work with. Also take care not to acquire a controlling terminal
498 by mistake (this can't happen on sane systems, but paranoia is a
500 else if ((f
= open (fname
, O_RDONLY
|O_NONBLOCK
|O_NOCTTY
, 0666)) < 0)
502 cpp_notice_from_errno (pfile
, fname
);
506 /* Push the buffer. */
507 if (!cpp_push_buffer (pfile
, NULL
, 0))
510 /* Gin up an include_hash structure for this file and feed it
513 ih_fake
= (IHASH
*) xmalloc (sizeof (IHASH
));
515 ih_fake
->next_this_file
= 0;
516 ih_fake
->foundhere
= ABSOLUTE_PATH
; /* well sort of ... */
517 ih_fake
->name
= fname
;
518 ih_fake
->control_macro
= 0;
519 ih_fake
->buf
= (char *)-1;
521 if (!_cpp_read_include_file (pfile
, f
, ih_fake
))
522 goto failed_finclude
;
527 /* If finclude fails, it pops the buffer. */
535 /* Read the contents of FD into the buffer on the top of PFILE's stack.
536 IHASH points to the include hash entry for the file associated with
539 The caller is responsible for the cpp_push_buffer. */
542 _cpp_read_include_file (pfile
, fd
, ihash
)
552 if (fstat (fd
, &st
) < 0)
554 if (fcntl (fd
, F_SETFL
, 0) == -1) /* turn off nonblocking mode */
557 fp
= CPP_BUFFER (pfile
);
559 /* If fd points to a plain file, we know how big it is, so we can
560 allocate the buffer all at once. If fd is a pipe or terminal, we
561 can't. Most C source files are 4k or less, so we guess that. If
562 fd is something weird, like a block device or a directory, we
563 don't want to read it at all.
565 Unfortunately, different systems use different st.st_mode values
566 for pipes: some have S_ISFIFO, some S_ISSOCK, some are buggy and
567 zero the entire struct stat except a couple fields. Hence the
570 In all cases, read_and_prescan will resize the buffer if it
571 turns out there's more data than we thought. */
573 if (S_ISREG (st
.st_mode
))
575 /* off_t might have a wider range than size_t - in other words,
576 the max size of a file might be bigger than the address
577 space. We can't handle a file that large. (Anyone with
578 a single source file bigger than 4GB needs to rethink
579 their coding style.) */
580 st_size
= (size_t) st
.st_size
;
581 if ((unsigned HOST_WIDEST_INT
) st_size
582 != (unsigned HOST_WIDEST_INT
) st
.st_size
)
584 cpp_error (pfile
, "file `%s' is too large", ihash
->name
);
588 else if (S_ISFIFO (st
.st_mode
) || S_ISSOCK (st
.st_mode
)
589 /* Permit any kind of character device: the sensible ones are
590 ttys and /dev/null, but weeding out the others is too hard. */
591 || S_ISCHR (st
.st_mode
)
592 /* Some 4.x (x<4) derivatives have a bug that makes fstat() of a
593 socket or pipe return a stat struct with most fields zeroed. */
594 || (st
.st_mode
== 0 && st
.st_nlink
== 0 && st
.st_size
== 0))
596 /* Cannot get its file size before reading. 4k is a decent
602 cpp_error (pfile
, "`%s' is not a file, pipe, or tty", ihash
->name
);
606 if (pfile
->input_buffer
== NULL
)
607 init_input_buffer (pfile
, fd
, &st
);
609 /* Read the file, converting end-of-line characters and trigraphs
612 fp
->nominal_fname
= ihash
->name
;
613 length
= read_and_prescan (pfile
, fp
, fd
, st_size
);
617 ihash
->control_macro
= (const U_CHAR
*) ""; /* never re-include */
620 fp
->rlimit
= fp
->alimit
= fp
->buf
+ length
;
622 if (ihash
->foundhere
!= ABSOLUTE_PATH
)
623 fp
->system_header_p
= ihash
->foundhere
->sysp
;
626 fp
->line_base
= fp
->buf
;
627 fp
->cleanup
= file_cleanup
;
629 /* The ->actual_dir field is only used when ignore_srcdir is not in effect;
631 if (!CPP_OPTIONS (pfile
)->ignore_srcdir
)
632 fp
->actual_dir
= actual_directory (pfile
, ihash
->name
);
634 pfile
->input_stack_listing_current
= 0;
638 cpp_error_from_errno (pfile
, ihash
->name
);
640 cpp_pop_buffer (pfile
);
645 /* Given a path FNAME, extract the directory component and place it
646 onto the actual_dirs list. Return a pointer to the allocated
647 file_name_list structure. These structures are used to implement
648 current-directory "" include searching. */
650 static struct file_name_list
*
651 actual_directory (pfile
, fname
)
655 char *last_slash
, *dir
;
657 struct file_name_list
*x
;
659 dir
= xstrdup (fname
);
660 last_slash
= strrchr (dir
, '/');
663 if (last_slash
== dir
)
666 last_slash
[1] = '\0';
670 dlen
= last_slash
- dir
;
681 if (dlen
> pfile
->max_include_len
)
682 pfile
->max_include_len
= dlen
;
684 for (x
= pfile
->actual_dirs
; x
; x
= x
->alloc
)
685 if (!strcmp (x
->name
, dir
))
691 /* Not found, make a new one. */
692 x
= (struct file_name_list
*) xmalloc (sizeof (struct file_name_list
));
695 x
->next
= CPP_OPTIONS (pfile
)->quote_include
;
696 x
->alloc
= pfile
->actual_dirs
;
697 x
->sysp
= CPP_BUFFER (pfile
)->system_header_p
;
700 pfile
->actual_dirs
= x
;
704 /* Determine the current line and column. Used only by read_and_prescan. */
706 find_position (start
, limit
, linep
)
709 unsigned long *linep
;
711 unsigned long line
= *linep
;
712 U_CHAR
*lbase
= start
;
713 while (start
< limit
)
715 U_CHAR ch
= *start
++;
716 if (ch
== '\n' || ch
== '\r')
726 /* Read the entire contents of file DESC into buffer BUF. LEN is how
727 much memory to allocate initially; more will be allocated if
728 necessary. Convert end-of-line markers (\n, \r, \r\n, \n\r) to
729 canonical form (\n). If enabled, convert and/or warn about
730 trigraphs. Convert backslash-newline to a one-character escape
731 (\r) and remove it from "embarrassing" places (i.e. the middle of a
732 token). If there is no newline at the end of the file, add one and
733 warn. Returns -1 on failure, or the actual length of the data to
736 This function does a lot of work, and can be a serious performance
737 bottleneck. It has been tuned heavily; make sure you understand it
738 before hacking. The common case - no trigraphs, Unix style line
739 breaks, backslash-newline set off by whitespace, newline at EOF -
740 has been optimized at the expense of the others. The performance
741 penalty for DOS style line breaks (\r\n) is about 15%.
743 Warnings lose particularly heavily since we have to determine the
744 line number, which involves scanning from the beginning of the file
745 or from the last warning. The penalty for the absence of a newline
746 at the end of reload1.c is about 60%. (reload1.c is 329k.)
748 If your file has more than one kind of end-of-line marker, you
749 will get messed-up line numbering. */
751 /* Table of characters that can't be handled in the inner loop.
752 Keep these contiguous to optimize the performance of the code generated
753 for the switch that uses them. */
754 #define SPECCASE_EMPTY 0
755 #define SPECCASE_NUL 1
756 #define SPECCASE_CR 2
757 #define SPECCASE_BACKSLASH 3
758 #define SPECCASE_QUESTION 4
761 read_and_prescan (pfile
, fp
, desc
, len
)
767 U_CHAR
*buf
= (U_CHAR
*) xmalloc (len
);
768 U_CHAR
*ip
, *op
, *line_base
;
770 U_CHAR
*speccase
= pfile
->input_speccase
;
772 unsigned int deferred_newlines
;
780 ibase
= pfile
->input_buffer
+ 2;
781 deferred_newlines
= 0;
787 count
= read (desc
, pfile
->input_buffer
+ 2, pfile
->input_buffer_len
);
795 ibase
= pfile
->input_buffer
+ 2;
796 ibase
[count
] = ibase
[count
+1] = '\0';
801 size_t delta_line_base
;
805 This could happen if the file is larger than half the
806 maximum address space of the machine. */
810 delta_line_base
= line_base
- buf
;
811 buf
= (U_CHAR
*) xrealloc (buf
, len
);
813 line_base
= buf
+ delta_line_base
;
818 unsigned int span
= 0;
820 /* Deal with \-newline in the middle of a token. */
821 if (deferred_newlines
)
823 while (speccase
[ip
[span
]] == SPECCASE_EMPTY
828 memcpy (op
, ip
, span
);
831 /* If ip[0] is SPECCASE_EMPTY, we have hit white space.
832 Dump out the remaining deferred \-newlines. */
833 if (speccase
[ip
[0]] == SPECCASE_EMPTY
)
834 while (deferred_newlines
)
835 deferred_newlines
--, *op
++ = '\r';
839 /* Copy as much as we can without special treatment. */
840 while (speccase
[ip
[span
]] == SPECCASE_EMPTY
) span
++;
841 memcpy (op
, ip
, span
);
845 switch (speccase
[*ip
++])
847 case SPECCASE_NUL
: /* \0 */
851 case SPECCASE_CR
: /* \r */
854 else if (*ip
== '\n')
856 else if (*ip
== '\0')
864 case SPECCASE_BACKSLASH
: /* \ */
867 /* If we're at the end of the intermediate buffer,
868 we have to shift the backslash down to the start
869 and come back next pass. */
875 else if (*ip
== '\n')
878 if (*ip
== '\r') ip
++;
879 if (*ip
== '\n' || *ip
== '\t' || *ip
== ' ')
881 else if (op
[-1] == '\t' || op
[-1] == ' '
882 || op
[-1] == '\r' || op
[-1] == '\n')
887 else if (*ip
== '\r')
890 if (*ip
== '\n') ip
++;
891 else if (*ip
== '\0')
897 else if (*ip
== '\r' || *ip
== '\t' || *ip
== ' ')
907 case SPECCASE_QUESTION
: /* ? */
910 /* If we're at the end of the intermediate buffer,
911 we have to shift the ?'s down to the start and
912 come back next pass. */
933 * from to from to from to
934 * ?? = # ?? ) ] ?? ! |
935 * ?? ( [ ?? ' ^ ?? > }
936 * ?? / \ ?? < { ?? - ~
938 if (d
== '=') t
= '#';
939 else if (d
== ')') t
= ']';
940 else if (d
== '!') t
= '|';
941 else if (d
== '(') t
= '[';
942 else if (d
== '\'') t
= '^';
943 else if (d
== '>') t
= '}';
944 else if (d
== '/') t
= '\\';
945 else if (d
== '<') t
= '{';
946 else if (d
== '-') t
= '~';
953 if (CPP_OPTIONS (pfile
)->warn_trigraphs
)
956 line_base
= find_position (line_base
, op
, &line
);
957 col
= op
- line_base
+ 1;
958 if (CPP_OPTIONS (pfile
)->trigraphs
)
959 cpp_warning_with_line (pfile
, line
, col
,
960 "trigraph ??%c converted to %c", d
, t
);
962 cpp_warning_with_line (pfile
, line
, col
,
963 "trigraph ??%c ignored", d
);
965 if (CPP_OPTIONS (pfile
)->trigraphs
)
986 /* Deal with pushed-back chars at true EOF.
987 This may be any of: ?? ? \ \r \n \\r \\n.
988 \r must become \n, \\r or \\n must become \r.
989 We know we have space already. */
990 if (ibase
== pfile
->input_buffer
)
1000 else if (ibase
== pfile
->input_buffer
+ 1)
1011 line_base
= find_position (line_base
, op
, &line
);
1012 col
= op
- line_base
+ 1;
1013 cpp_warning_with_line (pfile
, line
, col
, "no newline at end of file\n");
1014 if (offset
+ 1 > len
)
1017 if (offset
+ 1 > len
)
1019 buf
= (U_CHAR
*) xrealloc (buf
, len
);
1025 fp
->buf
= ((len
- offset
< 20) ? buf
: (U_CHAR
*)xrealloc (buf
, op
- buf
));
1029 cpp_error (pfile
, "file is too large (>%lu bytes)\n", (unsigned long)offset
);
1034 cpp_error_from_errno (pfile
, fp
->ihash
->name
);
1039 /* Initialize the `input_buffer' and `input_speccase' tables.
1040 These are only used by read_and_prescan, but they're large and
1041 somewhat expensive to set up, so we want them allocated once for
1042 the duration of the cpp run. */
1045 init_input_buffer (pfile
, fd
, st
)
1053 /* Table of characters that cannot be handled by the
1054 read_and_prescan inner loop. The number of non-EMPTY entries
1055 should be as small as humanly possible. */
1057 tmp
= (U_CHAR
*) xmalloc (1 << CHAR_BIT
);
1058 memset (tmp
, SPECCASE_EMPTY
, 1 << CHAR_BIT
);
1059 tmp
['\0'] = SPECCASE_NUL
;
1060 tmp
['\r'] = SPECCASE_CR
;
1061 tmp
['\\'] = SPECCASE_BACKSLASH
;
1062 if (CPP_OPTIONS (pfile
)->trigraphs
|| CPP_OPTIONS (pfile
)->warn_trigraphs
)
1063 tmp
['?'] = SPECCASE_QUESTION
;
1065 pfile
->input_speccase
= tmp
;
1067 /* Determine the appropriate size for the input buffer. Normal C
1068 source files are smaller than eight K. If we are reading a pipe,
1069 we want to make sure the input buffer is bigger than the kernel's
1073 if (! S_ISREG (st
->st_mode
))
1076 pipe_buf
= fpathconf (fd
, _PC_PIPE_BUF
);
1081 pipe_buf
= PIPE_BUF
;
1088 if (pipe_buf
< 8192)
1090 /* PIPE_BUF bytes of buffer proper, 2 to detect running off the end
1091 without address arithmetic all the time, and 2 for pushback in
1092 the case there's a potential trigraph or end-of-line digraph at
1093 the end of a block. */
1095 tmp
= (U_CHAR
*) xmalloc (pipe_buf
+ 2 + 2);
1096 pfile
->input_buffer
= tmp
;
1097 pfile
->input_buffer_len
= pipe_buf
;
1100 /* Simplify a path name in place, deleting redundant components. This
1101 reduces OS overhead and guarantees that equivalent paths compare
1102 the same (modulo symlinks).
1105 foo/bar/../quux foo/quux
1109 //quux //quux (POSIX allows leading // as a namespace escape)
1111 Guarantees no trailing slashes. All transforms reduce the length
1115 _cpp_simplify_pathname (path
)
1122 #if defined (HAVE_DOS_BASED_FILE_SYSTEM)
1123 /* Convert all backslashes to slashes. */
1124 for (from
= path
; *from
; from
++)
1125 if (*from
== '\\') *from
= '/';
1127 /* Skip over leading drive letter if present. */
1128 if (ISALPHA (path
[0]) && path
[1] == ':')
1129 from
= to
= &path
[2];
1136 /* Remove redundant initial /s. */
1145 /* 3 or more initial /s are equivalent to 1 /. */
1146 while (*++from
== '/');
1148 /* On some hosts // differs from /; Posix allows this. */
1156 while (*from
== '/')
1159 if (from
[0] == '.' && from
[1] == '/')
1161 else if (from
[0] == '.' && from
[1] == '\0')
1163 else if (from
[0] == '.' && from
[1] == '.' && from
[2] == '/')
1180 while (to
> base
&& *to
!= '/') to
--;
1186 else if (from
[0] == '.' && from
[1] == '.' && from
[2] == '\0')
1199 while (to
> base
&& *to
!= '/') to
--;
1206 /* Copy this component and trailing /, if any. */
1207 while ((*to
++ = *from
++) != '/')
1219 /* Trim trailing slash */
1220 if (to
[0] == '/' && (!absolute
|| to
> path
+1))
1223 /* Change the empty string to "." so that stat() on the result
1224 will always work. */
1233 /* It is not clear when this should be used if at all, so I've
1234 disabled it until someone who understands VMS can look at it. */
1237 /* Under VMS we need to fix up the "include" specification filename.
1239 Rules for possible conversions
1241 fullname tried paths
1244 ./dir/name [.dir]name
1246 /name [000000]name, name
1247 dir/name dir:[000000]name, dir:name, dir/name
1248 dir1/dir2/name dir1:[dir2]name, dir1:[000000.dir2]name
1249 path:/name path:[000000]name, path:name
1250 path:/dir/name path:[000000.dir]name, path:[dir]name
1251 path:dir/name path:[dir]name
1252 [path]:[dir]name [path.dir]name
1253 path/[dir]name [path.dir]name
1255 The path:/name input is constructed when expanding <> includes. */
1259 hack_vms_include_specification (fullname
)
1262 register char *basename
, *unixname
, *local_ptr
, *first_slash
;
1263 int f
, check_filename_before_returning
, must_revert
;
1266 check_filename_before_returning
= 0;
1268 /* See if we can find a 1st slash. If not, there's no path information. */
1269 first_slash
= strchr (fullname
, '/');
1270 if (first_slash
== 0)
1271 return 0; /* Nothing to do!!! */
1273 /* construct device spec if none given. */
1275 if (strchr (fullname
, ':') == 0)
1278 /* If fullname has a slash, take it as device spec. */
1280 if (first_slash
== fullname
)
1282 first_slash
= strchr (fullname
+ 1, '/'); /* 2nd slash ? */
1284 *first_slash
= ':'; /* make device spec */
1285 for (basename
= fullname
; *basename
!= 0; basename
++)
1286 *basename
= *(basename
+1); /* remove leading slash */
1288 else if ((first_slash
[-1] != '.') /* keep ':/', './' */
1289 && (first_slash
[-1] != ':')
1290 && (first_slash
[-1] != ']')) /* or a vms path */
1294 else if ((first_slash
[1] == '[') /* skip './' in './[dir' */
1295 && (first_slash
[-1] == '.'))
1299 /* Get part after first ':' (basename[-1] == ':')
1300 or last '/' (basename[-1] == '/'). */
1302 basename
= base_name (fullname
);
1304 local_ptr
= Local
; /* initialize */
1306 /* We are trying to do a number of things here. First of all, we are
1307 trying to hammer the filenames into a standard format, such that later
1308 processing can handle them.
1310 If the file name contains something like [dir.], then it recognizes this
1311 as a root, and strips the ".]". Later processing will add whatever is
1312 needed to get things working properly.
1314 If no device is specified, then the first directory name is taken to be
1315 a device name (or a rooted logical). */
1317 /* Point to the UNIX filename part (which needs to be fixed!)
1318 but skip vms path information.
1319 [basename != fullname since first_slash != 0]. */
1321 if ((basename
[-1] == ':') /* vms path spec. */
1322 || (basename
[-1] == ']')
1323 || (basename
[-1] == '>'))
1324 unixname
= basename
;
1326 unixname
= fullname
;
1328 if (*unixname
== '/')
1331 /* If the directory spec is not rooted, we can just copy
1332 the UNIX filename part and we are done. */
1334 if (((basename
- fullname
) > 1)
1335 && ( (basename
[-1] == ']')
1336 || (basename
[-1] == '>')))
1338 if (basename
[-2] != '.')
1341 /* The VMS part ends in a `]', and the preceding character is not a `.'.
1342 -> PATH]:/name (basename = '/name', unixname = 'name')
1343 We strip the `]', and then splice the two parts of the name in the
1344 usual way. Given the default locations for include files in cccp.c,
1345 we will only use this code if the user specifies alternate locations
1346 with the /include (-I) switch on the command line. */
1348 basename
-= 1; /* Strip "]" */
1349 unixname
--; /* backspace */
1354 /* The VMS part has a ".]" at the end, and this will not do. Later
1355 processing will add a second directory spec, and this would be a syntax
1356 error. Thus we strip the ".]", and thus merge the directory specs.
1357 We also backspace unixname, so that it points to a '/'. This inhibits the
1358 generation of the 000000 root directory spec (which does not belong here
1361 basename
-= 2; /* Strip ".]" */
1362 unixname
--; /* backspace */
1370 /* We drop in here if there is no VMS style directory specification yet.
1371 If there is no device specification either, we make the first dir a
1372 device and try that. If we do not do this, then we will be essentially
1373 searching the users default directory (as if they did a #include "asdf.h").
1375 Then all we need to do is to push a '[' into the output string. Later
1376 processing will fill this in, and close the bracket. */
1378 if ((unixname
!= fullname
) /* vms path spec found. */
1379 && (basename
[-1] != ':'))
1380 *local_ptr
++ = ':'; /* dev not in spec. take first dir */
1382 *local_ptr
++ = '['; /* Open the directory specification */
1385 if (unixname
== fullname
) /* no vms dir spec. */
1388 if ((first_slash
!= 0) /* unix dir spec. */
1389 && (*unixname
!= '/') /* not beginning with '/' */
1390 && (*unixname
!= '.')) /* or './' or '../' */
1391 *local_ptr
++ = '.'; /* dir is local ! */
1394 /* at this point we assume that we have the device spec, and (at least
1395 the opening "[" for a directory specification. We may have directories
1398 If there are no other slashes then the filename will be
1399 in the "root" directory. Otherwise, we need to add
1400 directory specifications. */
1402 if (strchr (unixname
, '/') == 0)
1404 /* if no directories specified yet and none are following. */
1405 if (local_ptr
[-1] == '[')
1407 /* Just add "000000]" as the directory string */
1408 strcpy (local_ptr
, "000000]");
1409 local_ptr
+= strlen (local_ptr
);
1410 check_filename_before_returning
= 1; /* we might need to fool with this later */
1416 /* As long as there are still subdirectories to add, do them. */
1417 while (strchr (unixname
, '/') != 0)
1419 /* If this token is "." we can ignore it
1420 if it's not at the beginning of a path. */
1421 if ((unixname
[0] == '.') && (unixname
[1] == '/'))
1423 /* remove it at beginning of path. */
1424 if ( ((unixname
== fullname
) /* no device spec */
1425 && (fullname
+2 != basename
)) /* starts with ./ */
1427 || ((basename
[-1] == ':') /* device spec */
1428 && (unixname
-1 == basename
))) /* and ./ afterwards */
1429 *local_ptr
++ = '.'; /* make '[.' start of path. */
1434 /* Add a subdirectory spec. Do not duplicate "." */
1435 if ( local_ptr
[-1] != '.'
1436 && local_ptr
[-1] != '['
1437 && local_ptr
[-1] != '<')
1440 /* If this is ".." then the spec becomes "-" */
1441 if ( (unixname
[0] == '.')
1442 && (unixname
[1] == '.')
1443 && (unixname
[2] == '/'))
1445 /* Add "-" and skip the ".." */
1446 if ((local_ptr
[-1] == '.')
1447 && (local_ptr
[-2] == '['))
1448 local_ptr
--; /* prevent [.- */
1454 /* Copy the subdirectory */
1455 while (*unixname
!= '/')
1456 *local_ptr
++= *unixname
++;
1458 unixname
++; /* Skip the "/" */
1461 /* Close the directory specification */
1462 if (local_ptr
[-1] == '.') /* no trailing periods */
1465 if (local_ptr
[-1] == '[') /* no dir needed */
1471 /* Now add the filename. */
1474 *local_ptr
++ = *unixname
++;
1477 /* Now append it to the original VMS spec. */
1479 strcpy ((must_revert
==1)?fullname
:basename
, Local
);
1481 /* If we put a [000000] in the filename, try to open it first. If this fails,
1482 remove the [000000], and return that name. This provides flexibility
1483 to the user in that they can use both rooted and non-rooted logical names
1484 to point to the location of the file. */
1486 if (check_filename_before_returning
)
1488 f
= open (fullname
, O_RDONLY
, 0666);
1491 /* The file name is OK as it is, so return it as is. */
1496 /* The filename did not work. Try to remove the [000000] from the name,
1499 basename
= strchr (fullname
, '[');
1500 local_ptr
= strchr (fullname
, ']') + 1;
1501 strcpy (basename
, local_ptr
); /* this gets rid of it */