1 /* Part of CPP library. (include file handling)
2 Copyright (C) 1986, 87, 89, 92 - 95, 1998 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 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 void add_import PROTO ((cpp_reader *, int, char *));
36 static int lookup_import PROTO ((cpp_reader *, char *,
37 struct file_name_list *));
38 static int redundant_include_p PROTO ((cpp_reader *, char *));
39 static struct file_name_map *read_name_map PROTO ((cpp_reader *, char *));
40 static char *read_filename_string PROTO ((int, FILE *));
41 static int open_include_file PROTO ((cpp_reader *, char *,
42 struct file_name_list *));
43 static int safe_read PROTO ((int, char *, int));
45 /* Not safe to prototype these. */
46 extern char *xmalloc();
47 extern char *xrealloc();
49 /* Append a chain of `struct file_name_list's
50 to the end of the main include chain.
51 FIRST is the beginning of the chain to append, and LAST is the end. */
54 append_include_chain (pfile, first, last)
56 struct file_name_list *first, *last;
58 struct cpp_options *opts = CPP_OPTIONS (pfile);
59 struct file_name_list *dir;
64 if (opts->include == 0)
65 opts->include = first;
67 opts->last_include->next = first;
69 if (opts->first_bracket_include == 0)
70 opts->first_bracket_include = first;
72 for (dir = first; ; dir = dir->next) {
73 int len = strlen (dir->fname) + INCLUDE_LEN_FUDGE;
74 if (len > pfile->max_include_len)
75 pfile->max_include_len = len;
81 opts->last_include = last;
84 /* Add output to `deps_buffer' for the -M switch.
85 STRING points to the text to be output.
86 SPACER is ':' for targets, ' ' for dependencies, zero for text
87 to be inserted literally. */
90 deps_output (pfile, string, spacer)
101 hack_vms_include_specification (string);
104 size = strlen (string);
106 #ifndef MAX_OUTPUT_COLUMNS
107 #define MAX_OUTPUT_COLUMNS 72
110 && pfile->deps_column > 0
111 && (pfile->deps_column + size) > MAX_OUTPUT_COLUMNS)
113 deps_output (pfile, " \\\n ", 0);
114 pfile->deps_column = 0;
117 if (pfile->deps_size + size + 8 > pfile->deps_allocated_size)
119 pfile->deps_allocated_size = (pfile->deps_size + size + 50) * 2;
120 pfile->deps_buffer = (char *) xrealloc (pfile->deps_buffer,
121 pfile->deps_allocated_size);
123 if (spacer == ' ' && pfile->deps_column > 0)
124 pfile->deps_buffer[pfile->deps_size++] = ' ';
125 bcopy (string, &pfile->deps_buffer[pfile->deps_size], size);
126 pfile->deps_size += size;
127 pfile->deps_column += size;
129 pfile->deps_buffer[pfile->deps_size++] = ':';
130 pfile->deps_buffer[pfile->deps_size] = 0;
134 file_cleanup (pbuf, pfile)
136 cpp_reader *pfile ATTRIBUTE_UNUSED;
147 find_include_file (pfile, fbeg, flen, fname,
148 importing, search_start, foundhere)
154 struct file_name_list *search_start;
155 struct file_name_list **foundhere;
157 struct file_name_list *searchptr;
160 /* If specified file name is absolute, just open it. */
164 strcpy (fname, fbeg);
166 hack_vms_include_specification (fname);
168 if (redundant_include_p (pfile, fname))
171 f = lookup_import (pfile, fname, NULL_PTR);
173 f = open_include_file (pfile, fname, NULL_PTR);
175 return -2; /* Already included this file */
179 /* Search directory path, trying to open the file.
180 Copy each filename tried into FNAME. */
182 for (searchptr = search_start; searchptr; searchptr = searchptr->next)
185 if (searchptr->fname)
187 /* The empty string in a search path is ignored.
188 This makes it possible to turn off entirely
189 a standard piece of the list. */
190 if (searchptr->fname[0] == 0)
193 l = strlen (searchptr->fname);
195 bcopy (searchptr->fname, fname, l);
199 bcopy (fbeg, &fname[l], flen);
200 fname[flen+l] = '\0';
202 hack_vms_include_specification (fname);
204 /* ??? There are currently 3 separate mechanisms for avoiding processing
205 of redundant include files: #import, #pragma once, and
206 redundant_include_p. It would be nice if they were unified. */
207 if (redundant_include_p (pfile, fname))
210 f = lookup_import (pfile, fname, searchptr);
212 f = open_include_file (pfile, fname, searchptr);
214 return -2; /* Already included this file */
216 else if (f == -1 && errno == EACCES)
217 cpp_warning (pfile, "Header file %s exists, but is not readable",
227 /* A file that was not found. */
228 bcopy (fbeg, fname, flen);
235 /* Check to see if this include file is a once-only include file.
238 struct file_name_list *ptr;
240 for (ptr = pfile->dont_repeat_files; ptr; ptr = ptr->next)
241 if (!strcmp (ptr->fname, fname))
244 return -2; /* This file was once'd. */
248 /* Record file on "seen" list for #import. */
249 add_import (pfile, f, fname);
251 *foundhere = searchptr;
255 /* Return nonzero if there is no need to include file NAME
256 because it has already been included and it contains a conditional
257 to make a repeated include do nothing. */
260 redundant_include_p (pfile, name)
264 struct file_name_list *l = pfile->all_include_files;
265 for (; l; l = l->next)
266 if (! strcmp (name, l->fname)
268 && cpp_lookup (pfile, l->control_macro, -1, -1))
275 /* Maintain and search list of included files, for #import. */
277 /* Hash a file name for import_hash_table. */
285 while (*f) val += *f++;
286 return (val%IMPORT_HASH_SIZE);
289 /* Search for file FILENAME in import_hash_table.
290 Return -2 if found, either a matching name or a matching inode.
291 Otherwise, open the file and return a file descriptor if successful
292 or -1 if unsuccessful. */
295 lookup_import (pfile, filename, searchptr)
298 struct file_name_list *searchptr;
300 struct import_file *i;
306 hashval = import_hash (filename);
308 /* Attempt to find file in list of already included files */
309 i = pfile->import_hash_table[hashval];
312 if (!strcmp (filename, i->name))
313 return -2; /* return found */
316 /* Open it and try a match on inode/dev */
317 fd = open_include_file (pfile, filename, searchptr);
321 for (h = 0; h < IMPORT_HASH_SIZE; h++) {
322 i = pfile->import_hash_table[h];
324 /* Compare the inode and the device.
325 Supposedly on some systems the inode is not a scalar. */
326 if (!bcmp ((char *) &i->inode, (char *) &sb.st_ino, sizeof (sb.st_ino))
327 && i->dev == sb.st_dev) {
329 return -2; /* return found */
334 return fd; /* Not found, return open file */
337 /* Add the file FNAME, open on descriptor FD, to import_hash_table. */
340 add_import (pfile, fd, fname)
345 struct import_file *i;
349 hashval = import_hash (fname);
351 i = (struct import_file *)xmalloc (sizeof (struct import_file));
352 i->name = (char *)xmalloc (strlen (fname)+1);
353 strcpy (i->name, fname);
354 bcopy ((char *) &sb.st_ino, (char *) &i->inode, sizeof (sb.st_ino));
356 i->next = pfile->import_hash_table[hashval];
357 pfile->import_hash_table[hashval] = i;
360 /* The file_name_map structure holds a mapping of file names for a
361 particular directory. This mapping is read from the file named
362 FILE_NAME_MAP_FILE in that directory. Such a file can be used to
363 map filenames on a file system with severe filename restrictions,
364 such as DOS. The format of the file name map file is just a series
365 of lines with two tokens on each line. The first token is the name
366 to map, and the second token is the actual name to use. */
370 struct file_name_map *map_next;
375 #define FILE_NAME_MAP_FILE "header.gcc"
377 /* Read a space delimited string of unlimited length from a stdio
381 read_filename_string (ch, f)
389 set = alloc = xmalloc (len + 1);
393 while ((ch = getc (f)) != EOF && ! is_space[ch])
395 if (set - alloc == len)
398 alloc = xrealloc (alloc, len + 1);
399 set = alloc + len / 2;
409 /* This structure holds a linked list of file name maps, one per directory. */
411 struct file_name_map_list
413 struct file_name_map_list *map_list_next;
415 struct file_name_map *map_list_map;
418 /* Read the file name map file for DIRNAME. */
420 static struct file_name_map *
421 read_name_map (pfile, dirname)
425 register struct file_name_map_list *map_list_ptr;
429 for (map_list_ptr = CPP_OPTIONS (pfile)->map_list; map_list_ptr;
430 map_list_ptr = map_list_ptr->map_list_next)
431 if (! strcmp (map_list_ptr->map_list_name, dirname))
432 return map_list_ptr->map_list_map;
434 map_list_ptr = ((struct file_name_map_list *)
435 xmalloc (sizeof (struct file_name_map_list)));
436 map_list_ptr->map_list_name = savestring (dirname);
437 map_list_ptr->map_list_map = NULL;
439 name = (char *) alloca (strlen (dirname) + strlen (FILE_NAME_MAP_FILE) + 2);
440 strcpy (name, dirname);
443 strcat (name, FILE_NAME_MAP_FILE);
444 f = fopen (name, "r");
446 map_list_ptr->map_list_map = NULL;
450 int dirlen = strlen (dirname);
452 while ((ch = getc (f)) != EOF)
455 struct file_name_map *ptr;
459 from = read_filename_string (ch, f);
460 while ((ch = getc (f)) != EOF && is_hor_space[ch])
462 to = read_filename_string (ch, f);
464 ptr = ((struct file_name_map *)
465 xmalloc (sizeof (struct file_name_map)));
466 ptr->map_from = from;
468 /* Make the real filename absolute. */
473 ptr->map_to = xmalloc (dirlen + strlen (to) + 2);
474 strcpy (ptr->map_to, dirname);
475 ptr->map_to[dirlen] = '/';
476 strcpy (ptr->map_to + dirlen + 1, to);
480 ptr->map_next = map_list_ptr->map_list_map;
481 map_list_ptr->map_list_map = ptr;
483 while ((ch = getc (f)) != '\n')
490 map_list_ptr->map_list_next = CPP_OPTIONS (pfile)->map_list;
491 CPP_OPTIONS (pfile)->map_list = map_list_ptr;
493 return map_list_ptr->map_list_map;
496 /* Try to open include file FILENAME. SEARCHPTR is the directory
497 being tried from the include file search path. This function maps
498 filenames on file systems based on information read by
502 open_include_file (pfile, filename, searchptr)
505 struct file_name_list *searchptr;
507 if (CPP_OPTIONS (pfile)->remap)
509 register struct file_name_map *map;
513 if (searchptr && ! searchptr->got_name_map)
515 searchptr->name_map = read_name_map (pfile,
517 ? searchptr->fname : ".");
518 searchptr->got_name_map = 1;
521 /* First check the mapping for the directory we are using. */
522 if (searchptr && searchptr->name_map)
525 if (searchptr->fname)
526 from += strlen (searchptr->fname) + 1;
527 for (map = searchptr->name_map; map; map = map->map_next)
529 if (! strcmp (map->map_from, from))
532 return open (map->map_to, O_RDONLY, 0666);
537 /* Try to find a mapping file for the particular directory we are
538 looking in. Thus #include <sys/types.h> will look up sys/types.h
539 in /usr/include/header.gcc and look up types.h in
540 /usr/include/sys/header.gcc. */
541 p = rindex (filename, '/');
546 && strlen (searchptr->fname) == (size_t) (p - filename)
547 && ! strncmp (searchptr->fname, filename, p - filename))
549 /* FILENAME is in SEARCHPTR, which we've already checked. */
550 return open (filename, O_RDONLY, 0666);
560 dir = (char *) alloca (p - filename + 1);
561 bcopy (filename, dir, p - filename);
562 dir[p - filename] = '\0';
565 for (map = read_name_map (pfile, dir); map; map = map->map_next)
566 if (! strcmp (map->map_from, from))
567 return open (map->map_to, O_RDONLY, 0666);
570 return open (filename, O_RDONLY, 0666);
573 /* Process the contents of include file FNAME, already open on descriptor F,
575 SYSTEM_HEADER_P is 1 if this file resides in any one of the known
576 "system" include directories (as decided by the `is_system_include'
578 DIRPTR is the link in the dir path through which this file was found,
579 or 0 if the file name was absolute or via the current directory.
580 Return 1 on success, 0 on failure.
582 The caller is responsible for the cpp_push_buffer. */
585 finclude (pfile, f, fname, system_header_p, dirptr)
590 struct file_name_list *dirptr;
596 cpp_buffer *fp; /* For input stack frame */
598 int missing_newline = 0;
601 if (fstat (f, &st) < 0)
603 cpp_perror_with_name (pfile, fname);
605 cpp_pop_buffer (pfile);
609 fp = CPP_BUFFER (pfile);
610 fp->nominal_fname = fp->fname = fname;
615 fp->system_header_p = system_header_p;
618 fp->cleanup = file_cleanup;
620 if (S_ISREG (st.st_mode)) {
621 st_size = (size_t) st.st_size;
622 if (st_size != st.st_size || st_size + 2 < st_size) {
623 cpp_error (pfile, "file `%s' too large", fname);
627 fp->buf = (U_CHAR *) xmalloc (st_size + 2);
628 fp->alimit = fp->buf + st_size + 2;
631 /* Read the file contents, knowing that st_size is an upper bound
632 on the number of bytes we can read. */
633 length = safe_read (f, fp->buf, st_size);
634 fp->rlimit = fp->buf + length;
635 if (length < 0) goto nope;
637 else if (S_ISDIR (st.st_mode)) {
638 cpp_error (pfile, "directory `%s' specified in #include", fname);
642 /* Cannot count its file size before reading.
643 First read the entire file into heap and
644 copy them into buffer on stack. */
649 fp->buf = (U_CHAR *) xmalloc (bsize + 2);
652 i = safe_read (f, fp->buf + st_size, bsize - st_size);
654 goto nope; /* error! */
656 if (st_size != bsize)
657 break; /* End of file */
659 fp->buf = (U_CHAR *) xrealloc (fp->buf, bsize + 2);
665 if ((length > 0 && fp->buf[length - 1] != '\n')
666 /* Backslash-newline at end is not good enough. */
667 || (length > 1 && fp->buf[length - 2] == '\\')) {
668 fp->buf[length++] = '\n';
673 fp->buf[length] = '\0';
674 fp->rlimit = fp->buf + length;
676 /* Close descriptor now, so nesting does not use lots of descriptors. */
679 /* Must do this before calling trigraph_pcp, so that the correct file name
680 will be printed in warning messages. */
682 pfile->input_stack_listing_current = 0;
695 if (CPP_PEDANTIC (pfile) && missing_newline)
696 pedwarn ("file does not end in newline");
699 input_file_stack_tick++;
706 cpp_perror_with_name (pfile, fname);
712 /* Read LEN bytes at PTR from descriptor DESC, for file FILENAME,
713 retrying if necessary. If MAX_READ_LEN is defined, read at most
714 that bytes at a time. Return a negative value if an error occurs,
715 otherwise return the actual number of bytes read,
716 which must be LEN unless end-of-file was reached. */
719 safe_read (desc, ptr, len)
724 int left, rcount, nchars;
730 if (rcount > MAX_READ_LEN)
731 rcount = MAX_READ_LEN;
733 nchars = read (desc, ptr, rcount);
752 /* Under VMS we need to fix up the "include" specification filename.
754 Rules for possible conversions
759 ./dir/name [.dir]name
761 /name [000000]name, name
762 dir/name dir:[000000]name, dir:name, dir/name
763 dir1/dir2/name dir1:[dir2]name, dir1:[000000.dir2]name
764 path:/name path:[000000]name, path:name
765 path:/dir/name path:[000000.dir]name, path:[dir]name
766 path:dir/name path:[dir]name
767 [path]:[dir]name [path.dir]name
768 path/[dir]name [path.dir]name
770 The path:/name input is constructed when expanding <> includes. */
774 hack_vms_include_specification (fullname)
777 register char *basename, *unixname, *local_ptr, *first_slash;
778 int f, check_filename_before_returning, must_revert;
781 check_filename_before_returning = 0;
783 /* See if we can find a 1st slash. If not, there's no path information. */
784 first_slash = index (fullname, '/');
785 if (first_slash == 0)
786 return 0; /* Nothing to do!!! */
788 /* construct device spec if none given. */
790 if (index (fullname, ':') == 0)
793 /* If fullname has a slash, take it as device spec. */
795 if (first_slash == fullname)
797 first_slash = index (fullname+1, '/'); /* 2nd slash ? */
799 *first_slash = ':'; /* make device spec */
800 for (basename = fullname; *basename != 0; basename++)
801 *basename = *(basename+1); /* remove leading slash */
803 else if ((first_slash[-1] != '.') /* keep ':/', './' */
804 && (first_slash[-1] != ':')
805 && (first_slash[-1] != ']')) /* or a vms path */
809 else if ((first_slash[1] == '[') /* skip './' in './[dir' */
810 && (first_slash[-1] == '.'))
814 /* Get part after first ':' (basename[-1] == ':')
815 or last '/' (basename[-1] == '/'). */
817 basename = base_name (fullname);
819 local_ptr = Local; /* initialize */
821 /* We are trying to do a number of things here. First of all, we are
822 trying to hammer the filenames into a standard format, such that later
823 processing can handle them.
825 If the file name contains something like [dir.], then it recognizes this
826 as a root, and strips the ".]". Later processing will add whatever is
827 needed to get things working properly.
829 If no device is specified, then the first directory name is taken to be
830 a device name (or a rooted logical). */
832 /* Point to the UNIX filename part (which needs to be fixed!)
833 but skip vms path information.
834 [basename != fullname since first_slash != 0]. */
836 if ((basename[-1] == ':') /* vms path spec. */
837 || (basename[-1] == ']')
838 || (basename[-1] == '>'))
843 if (*unixname == '/')
846 /* If the directory spec is not rooted, we can just copy
847 the UNIX filename part and we are done. */
849 if (((basename - fullname) > 1)
850 && ( (basename[-1] == ']')
851 || (basename[-1] == '>')))
853 if (basename[-2] != '.')
856 /* The VMS part ends in a `]', and the preceding character is not a `.'.
857 -> PATH]:/name (basename = '/name', unixname = 'name')
858 We strip the `]', and then splice the two parts of the name in the
859 usual way. Given the default locations for include files in cccp.c,
860 we will only use this code if the user specifies alternate locations
861 with the /include (-I) switch on the command line. */
863 basename -= 1; /* Strip "]" */
864 unixname--; /* backspace */
869 /* The VMS part has a ".]" at the end, and this will not do. Later
870 processing will add a second directory spec, and this would be a syntax
871 error. Thus we strip the ".]", and thus merge the directory specs.
872 We also backspace unixname, so that it points to a '/'. This inhibits the
873 generation of the 000000 root directory spec (which does not belong here
876 basename -= 2; /* Strip ".]" */
877 unixname--; /* backspace */
885 /* We drop in here if there is no VMS style directory specification yet.
886 If there is no device specification either, we make the first dir a
887 device and try that. If we do not do this, then we will be essentially
888 searching the users default directory (as if they did a #include "asdf.h").
890 Then all we need to do is to push a '[' into the output string. Later
891 processing will fill this in, and close the bracket. */
893 if ((unixname != fullname) /* vms path spec found. */
894 && (basename[-1] != ':'))
895 *local_ptr++ = ':'; /* dev not in spec. take first dir */
897 *local_ptr++ = '['; /* Open the directory specification */
900 if (unixname == fullname) /* no vms dir spec. */
903 if ((first_slash != 0) /* unix dir spec. */
904 && (*unixname != '/') /* not beginning with '/' */
905 && (*unixname != '.')) /* or './' or '../' */
906 *local_ptr++ = '.'; /* dir is local ! */
909 /* at this point we assume that we have the device spec, and (at least
910 the opening "[" for a directory specification. We may have directories
913 If there are no other slashes then the filename will be
914 in the "root" directory. Otherwise, we need to add
915 directory specifications. */
917 if (index (unixname, '/') == 0)
919 /* if no directories specified yet and none are following. */
920 if (local_ptr[-1] == '[')
922 /* Just add "000000]" as the directory string */
923 strcpy (local_ptr, "000000]");
924 local_ptr += strlen (local_ptr);
925 check_filename_before_returning = 1; /* we might need to fool with this later */
931 /* As long as there are still subdirectories to add, do them. */
932 while (index (unixname, '/') != 0)
934 /* If this token is "." we can ignore it
935 if it's not at the beginning of a path. */
936 if ((unixname[0] == '.') && (unixname[1] == '/'))
938 /* remove it at beginning of path. */
939 if ( ((unixname == fullname) /* no device spec */
940 && (fullname+2 != basename)) /* starts with ./ */
942 || ((basename[-1] == ':') /* device spec */
943 && (unixname-1 == basename))) /* and ./ afterwards */
944 *local_ptr++ = '.'; /* make '[.' start of path. */
949 /* Add a subdirectory spec. Do not duplicate "." */
950 if ( local_ptr[-1] != '.'
951 && local_ptr[-1] != '['
952 && local_ptr[-1] != '<')
955 /* If this is ".." then the spec becomes "-" */
956 if ( (unixname[0] == '.')
957 && (unixname[1] == '.')
958 && (unixname[2] == '/'))
960 /* Add "-" and skip the ".." */
961 if ((local_ptr[-1] == '.')
962 && (local_ptr[-2] == '['))
963 local_ptr--; /* prevent [.- */
969 /* Copy the subdirectory */
970 while (*unixname != '/')
971 *local_ptr++= *unixname++;
973 unixname++; /* Skip the "/" */
976 /* Close the directory specification */
977 if (local_ptr[-1] == '.') /* no trailing periods */
980 if (local_ptr[-1] == '[') /* no dir needed */
986 /* Now add the filename. */
989 *local_ptr++ = *unixname++;
992 /* Now append it to the original VMS spec. */
994 strcpy ((must_revert==1)?fullname:basename, Local);
996 /* If we put a [000000] in the filename, try to open it first. If this fails,
997 remove the [000000], and return that name. This provides flexibility
998 to the user in that they can use both rooted and non-rooted logical names
999 to point to the location of the file. */
1001 if (check_filename_before_returning)
1003 f = open (fullname, O_RDONLY, 0666);
1006 /* The file name is OK as it is, so return it as is. */
1011 /* The filename did not work. Try to remove the [000000] from the name,
1014 basename = index (fullname, '[');
1015 local_ptr = index (fullname, ']') + 1;
1016 strcpy (basename, local_ptr); /* this gets rid of it */