Zack Weinberg | add7091 | 1998-10-29 11:54:13 +0000 | [diff] [blame] | 1 | /* Part of CPP library. (include file handling) |
Jeff Law | 5e7b4e2 | 2000-02-25 22:59:31 -0700 | [diff] [blame] | 2 | Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1998, |
| 3 | 1999, 2000 Free Software Foundation, Inc. |
Zack Weinberg | add7091 | 1998-10-29 11:54:13 +0000 | [diff] [blame] | 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 |
| 8 | |
| 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 |
| 12 | later version. |
| 13 | |
| 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. |
| 18 | |
| 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 |
Richard Kenner | e38992e | 2000-04-18 20:42:00 +0000 | [diff] [blame] | 21 | Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ |
Zack Weinberg | add7091 | 1998-10-29 11:54:13 +0000 | [diff] [blame] | 22 | |
| 23 | #include "config.h" |
| 24 | #include "system.h" |
Zack Weinberg | add7091 | 1998-10-29 11:54:13 +0000 | [diff] [blame] | 25 | #include "cpplib.h" |
Zack Weinberg | 88ae23e | 2000-03-08 23:35:19 +0000 | [diff] [blame] | 26 | #include "cpphash.h" |
Zack Weinberg | c1212d2 | 2000-02-06 23:46:18 +0000 | [diff] [blame] | 27 | #include "intl.h" |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 28 | #include "mkdeps.h" |
Zack Weinberg | c31a650 | 2000-06-21 18:33:51 +0000 | [diff] [blame] | 29 | #include "splay-tree.h" |
Zack Weinberg | add7091 | 1998-10-29 11:54:13 +0000 | [diff] [blame] | 30 | |
Zack Weinberg | f8f769e | 2000-05-28 05:56:38 +0000 | [diff] [blame] | 31 | #ifdef HAVE_MMAP_FILE |
| 32 | # include <sys/mman.h> |
| 33 | # ifndef MMAP_THRESHOLD |
| 34 | # define MMAP_THRESHOLD 3 /* Minimum page count to mmap the file. */ |
| 35 | # endif |
| 36 | |
| 37 | #else /* No MMAP_FILE */ |
| 38 | # undef MMAP_THRESHOLD |
| 39 | # define MMAP_THRESHOLD 0 |
| 40 | #endif |
| 41 | |
Zack Weinberg | d7a2e0f | 2000-06-01 20:06:57 +0000 | [diff] [blame] | 42 | #ifndef O_BINARY |
| 43 | # define O_BINARY 0 |
| 44 | #endif |
| 45 | |
Zack Weinberg | a58d32c | 2000-09-12 03:42:30 +0000 | [diff] [blame] | 46 | #ifndef INCLUDE_LEN_FUDGE |
| 47 | # define INCLUDE_LEN_FUDGE 0 |
| 48 | #endif |
| 49 | |
| 50 | /* If errno is inspected immediately after a system call fails, it will be |
| 51 | nonzero, and no error number will ever be zero. */ |
| 52 | #ifndef ENOENT |
| 53 | # define ENOENT 0 |
| 54 | #endif |
| 55 | #ifndef ENOTDIR |
| 56 | # define ENOTDIR 0 |
| 57 | #endif |
| 58 | #ifndef ENOMEM |
| 59 | # define ENOMEM 0 |
| 60 | #endif |
| 61 | |
Zack Weinberg | f9a0e96 | 2000-07-13 02:32:41 +0000 | [diff] [blame] | 62 | /* Suppress warning about function macros used w/o arguments in traditional |
| 63 | C. It is unlikely that glibc's strcmp macro helps this file at all. */ |
| 64 | #undef strcmp |
| 65 | |
Neil Booth | 642ce43 | 2000-12-07 23:17:56 +0000 | [diff] [blame] | 66 | /* This structure is used for the table of all includes. */ |
| 67 | struct include_file |
| 68 | { |
| 69 | const char *name; /* actual path name of file */ |
| 70 | const cpp_hashnode *cmacro; /* macro, if any, preventing reinclusion. */ |
| 71 | const struct file_name_list *foundhere; |
| 72 | /* location in search path where file was |
| 73 | found, for #include_next and sysp. */ |
| 74 | const unsigned char *buffer; /* pointer to cached file contents */ |
| 75 | struct stat st; /* copy of stat(2) data for file */ |
| 76 | int fd; /* fd open on file (short term storage only) */ |
| 77 | unsigned short include_count; /* number of times file has been read */ |
| 78 | unsigned short refcnt; /* number of stacked buffers using this file */ |
| 79 | unsigned char mapped; /* file buffer is mmapped */ |
| 80 | unsigned char defined; /* cmacro prevents inclusion in this state */ |
| 81 | }; |
| 82 | |
Neil Booth | 28e0f04 | 2000-12-09 12:06:37 +0000 | [diff] [blame^] | 83 | /* The cmacro works like this: If it's NULL, the file is to be |
| 84 | included again. If it's NEVER_REREAD, the file is never to be |
| 85 | included again. Otherwise it is a macro hashnode, and the file is |
| 86 | to be included again if the macro is defined or not as specified by |
| 87 | DEFINED. */ |
| 88 | #define NEVER_REREAD ((const cpp_hashnode *)-1) |
| 89 | #define DO_NOT_REREAD(inc) \ |
| 90 | ((inc)->cmacro && ((inc)->cmacro == NEVER_REREAD \ |
| 91 | || ((inc)->cmacro->type == NT_MACRO) == (inc)->defined)) |
| 92 | |
Zack Weinberg | a73ac7a | 2000-01-30 18:09:07 +0000 | [diff] [blame] | 93 | static struct file_name_map *read_name_map |
Zack Weinberg | 38b24ee | 2000-03-08 20:37:23 +0000 | [diff] [blame] | 94 | PARAMS ((cpp_reader *, const char *)); |
| 95 | static char *read_filename_string PARAMS ((int, FILE *)); |
| 96 | static char *remap_filename PARAMS ((cpp_reader *, char *, |
| 97 | struct file_name_list *)); |
Zack Weinberg | a73ac7a | 2000-01-30 18:09:07 +0000 | [diff] [blame] | 98 | static struct file_name_list *actual_directory |
Zack Weinberg | 38b24ee | 2000-03-08 20:37:23 +0000 | [diff] [blame] | 99 | PARAMS ((cpp_reader *, const char *)); |
Zack Weinberg | c31a650 | 2000-06-21 18:33:51 +0000 | [diff] [blame] | 100 | static struct include_file *find_include_file |
| 101 | PARAMS ((cpp_reader *, const char *, |
| 102 | struct file_name_list *)); |
Neil Booth | 2047e26 | 2000-09-21 18:01:22 +0000 | [diff] [blame] | 103 | static struct include_file *open_file PARAMS ((cpp_reader *, const char *)); |
Neil Booth | 3cf3593 | 2000-12-05 23:42:43 +0000 | [diff] [blame] | 104 | static void read_include_file PARAMS ((cpp_reader *, struct include_file *)); |
| 105 | static void stack_include_file PARAMS ((cpp_reader *, struct include_file *)); |
Zack Weinberg | a58d32c | 2000-09-12 03:42:30 +0000 | [diff] [blame] | 106 | static void purge_cache PARAMS ((struct include_file *)); |
Zack Weinberg | c31a650 | 2000-06-21 18:33:51 +0000 | [diff] [blame] | 107 | static void destroy_include_file_node PARAMS ((splay_tree_value)); |
Zack Weinberg | c71f835 | 2000-07-05 05:33:57 +0000 | [diff] [blame] | 108 | static int report_missing_guard PARAMS ((splay_tree_node, void *)); |
Zack Weinberg | c31a650 | 2000-06-21 18:33:51 +0000 | [diff] [blame] | 109 | |
Zack Weinberg | 0b3d776 | 1998-11-25 11:56:54 +0000 | [diff] [blame] | 110 | #if 0 |
Kaveh R. Ghazi | f84c201 | 2000-01-19 22:52:43 +0000 | [diff] [blame] | 111 | static void hack_vms_include_specification PARAMS ((char *)); |
Zack Weinberg | 0b3d776 | 1998-11-25 11:56:54 +0000 | [diff] [blame] | 112 | #endif |
Zack Weinberg | add7091 | 1998-10-29 11:54:13 +0000 | [diff] [blame] | 113 | |
Zack Weinberg | c31a650 | 2000-06-21 18:33:51 +0000 | [diff] [blame] | 114 | /* We use a splay tree to store information about all the include |
| 115 | files seen in this compilation. The key of each tree node is the |
| 116 | physical path to the file. The value is 0 if the file does not |
| 117 | exist, or a struct include_file pointer. */ |
| 118 | |
| 119 | static void |
| 120 | destroy_include_file_node (v) |
| 121 | splay_tree_value v; |
Zack Weinberg | 0b3d776 | 1998-11-25 11:56:54 +0000 | [diff] [blame] | 122 | { |
Zack Weinberg | c31a650 | 2000-06-21 18:33:51 +0000 | [diff] [blame] | 123 | struct include_file *f = (struct include_file *)v; |
| 124 | if (f) |
| 125 | { |
Zack Weinberg | a58d32c | 2000-09-12 03:42:30 +0000 | [diff] [blame] | 126 | purge_cache (f); |
Neil Booth | 2047e26 | 2000-09-21 18:01:22 +0000 | [diff] [blame] | 127 | free (f); /* The tree is registered with free to free f->name. */ |
Zack Weinberg | c31a650 | 2000-06-21 18:33:51 +0000 | [diff] [blame] | 128 | } |
Zack Weinberg | d35364d | 2000-03-12 23:46:05 +0000 | [diff] [blame] | 129 | } |
Zack Weinberg | 0b3d776 | 1998-11-25 11:56:54 +0000 | [diff] [blame] | 130 | |
Zack Weinberg | d35364d | 2000-03-12 23:46:05 +0000 | [diff] [blame] | 131 | void |
Zack Weinberg | c71f835 | 2000-07-05 05:33:57 +0000 | [diff] [blame] | 132 | _cpp_init_includes (pfile) |
Zack Weinberg | d35364d | 2000-03-12 23:46:05 +0000 | [diff] [blame] | 133 | cpp_reader *pfile; |
| 134 | { |
| 135 | pfile->all_include_files |
Zack Weinberg | c31a650 | 2000-06-21 18:33:51 +0000 | [diff] [blame] | 136 | = splay_tree_new ((splay_tree_compare_fn) strcmp, |
| 137 | (splay_tree_delete_key_fn) free, |
| 138 | destroy_include_file_node); |
Zack Weinberg | 0b3d776 | 1998-11-25 11:56:54 +0000 | [diff] [blame] | 139 | } |
| 140 | |
Zack Weinberg | c71f835 | 2000-07-05 05:33:57 +0000 | [diff] [blame] | 141 | void |
| 142 | _cpp_cleanup_includes (pfile) |
| 143 | cpp_reader *pfile; |
| 144 | { |
| 145 | splay_tree_delete (pfile->all_include_files); |
| 146 | } |
| 147 | |
Neil Booth | 642ce43 | 2000-12-07 23:17:56 +0000 | [diff] [blame] | 148 | /* Mark a file to not be reread (e.g. #import, read failure). */ |
| 149 | void |
| 150 | _cpp_never_reread (file) |
| 151 | struct include_file *file; |
| 152 | { |
| 153 | file->cmacro = NEVER_REREAD; |
| 154 | } |
| 155 | |
Zack Weinberg | a58d32c | 2000-09-12 03:42:30 +0000 | [diff] [blame] | 156 | /* Given a file name, look it up in the cache; if there is no entry, |
Neil Booth | 2047e26 | 2000-09-21 18:01:22 +0000 | [diff] [blame] | 157 | create one with a non-NULL value (regardless of success in opening |
| 158 | the file). If the file doesn't exist or is inaccessible, this |
| 159 | entry is flagged so we don't attempt to open it again in the |
| 160 | future. If the file isn't open, open it. |
| 161 | |
| 162 | Returns an include_file structure with an open file descriptor on |
| 163 | success, or NULL on failure. */ |
Zack Weinberg | 0b3d776 | 1998-11-25 11:56:54 +0000 | [diff] [blame] | 164 | |
Zack Weinberg | c31a650 | 2000-06-21 18:33:51 +0000 | [diff] [blame] | 165 | static struct include_file * |
Neil Booth | 2047e26 | 2000-09-21 18:01:22 +0000 | [diff] [blame] | 166 | open_file (pfile, filename) |
Zack Weinberg | c31a650 | 2000-06-21 18:33:51 +0000 | [diff] [blame] | 167 | cpp_reader *pfile; |
| 168 | const char *filename; |
Neil Booth | 2047e26 | 2000-09-21 18:01:22 +0000 | [diff] [blame] | 169 | { |
Zack Weinberg | c31a650 | 2000-06-21 18:33:51 +0000 | [diff] [blame] | 170 | splay_tree_node nd; |
Neil Booth | 2047e26 | 2000-09-21 18:01:22 +0000 | [diff] [blame] | 171 | struct include_file *file; |
Zack Weinberg | 0b3d776 | 1998-11-25 11:56:54 +0000 | [diff] [blame] | 172 | |
Zack Weinberg | a58d32c | 2000-09-12 03:42:30 +0000 | [diff] [blame] | 173 | nd = splay_tree_lookup (pfile->all_include_files, (splay_tree_key) filename); |
Zack Weinberg | 0b3d776 | 1998-11-25 11:56:54 +0000 | [diff] [blame] | 174 | |
Zack Weinberg | c31a650 | 2000-06-21 18:33:51 +0000 | [diff] [blame] | 175 | if (nd) |
Neil Booth | 2047e26 | 2000-09-21 18:01:22 +0000 | [diff] [blame] | 176 | { |
| 177 | file = (struct include_file *) nd->value; |
| 178 | |
| 179 | /* Don't retry opening if we failed previously. */ |
| 180 | if (file->fd == -2) |
| 181 | return 0; |
| 182 | |
Nathan Sidwell | def3263 | 2000-11-03 16:03:37 +0000 | [diff] [blame] | 183 | /* Don't reopen an idempotent file. */ |
| 184 | if (DO_NOT_REREAD (file)) |
| 185 | return file; |
| 186 | |
| 187 | /* Don't reopen one which is already loaded. */ |
| 188 | if (file->buffer != NULL) |
| 189 | return file; |
Neil Booth | 2047e26 | 2000-09-21 18:01:22 +0000 | [diff] [blame] | 190 | } |
| 191 | else |
| 192 | { |
Neil Booth | 642ce43 | 2000-12-07 23:17:56 +0000 | [diff] [blame] | 193 | /* In particular, this clears foundhere. */ |
Neil Booth | 2047e26 | 2000-09-21 18:01:22 +0000 | [diff] [blame] | 194 | file = xcnew (struct include_file); |
| 195 | file->name = xstrdup (filename); |
| 196 | splay_tree_insert (pfile->all_include_files, |
| 197 | (splay_tree_key) file->name, |
| 198 | (splay_tree_value) file); |
| 199 | } |
Zack Weinberg | c31a650 | 2000-06-21 18:33:51 +0000 | [diff] [blame] | 200 | |
| 201 | /* We used to open files in nonblocking mode, but that caused more |
| 202 | problems than it solved. Do take care not to acquire a |
| 203 | controlling terminal by mistake (this can't happen on sane |
| 204 | systems, but paranoia is a virtue). |
| 205 | |
| 206 | Use the three-argument form of open even though we aren't |
| 207 | specifying O_CREAT, to defend against broken system headers. |
| 208 | |
| 209 | O_BINARY tells some runtime libraries (notably DJGPP) not to do |
| 210 | newline translation; we can handle DOS line breaks just fine |
| 211 | ourselves. |
| 212 | |
| 213 | Special case: the empty string is translated to stdin. */ |
Zack Weinberg | d450696 | 2000-06-28 19:03:08 +0000 | [diff] [blame] | 214 | |
Zack Weinberg | c31a650 | 2000-06-21 18:33:51 +0000 | [diff] [blame] | 215 | if (filename[0] == '\0') |
Neil Booth | 2047e26 | 2000-09-21 18:01:22 +0000 | [diff] [blame] | 216 | file->fd = 0; |
Zack Weinberg | c31a650 | 2000-06-21 18:33:51 +0000 | [diff] [blame] | 217 | else |
Neil Booth | 2047e26 | 2000-09-21 18:01:22 +0000 | [diff] [blame] | 218 | file->fd = open (filename, O_RDONLY | O_NOCTTY | O_BINARY, 0666); |
Zack Weinberg | d450696 | 2000-06-28 19:03:08 +0000 | [diff] [blame] | 219 | |
Neil Booth | 2047e26 | 2000-09-21 18:01:22 +0000 | [diff] [blame] | 220 | if (file->fd != -1 && fstat (file->fd, &file->st) == 0) |
| 221 | { |
| 222 | /* Mark a regular, zero-length file never-reread now. */ |
| 223 | if (S_ISREG (file->st.st_mode) && file->st.st_size == 0) |
Nathan Sidwell | def3263 | 2000-11-03 16:03:37 +0000 | [diff] [blame] | 224 | { |
Neil Booth | 642ce43 | 2000-12-07 23:17:56 +0000 | [diff] [blame] | 225 | _cpp_never_reread (file); |
Nathan Sidwell | def3263 | 2000-11-03 16:03:37 +0000 | [diff] [blame] | 226 | close (file->fd); |
| 227 | file->fd = -1; |
| 228 | } |
Zack Weinberg | a58d32c | 2000-09-12 03:42:30 +0000 | [diff] [blame] | 229 | |
Neil Booth | 2047e26 | 2000-09-21 18:01:22 +0000 | [diff] [blame] | 230 | return file; |
| 231 | } |
Zack Weinberg | a58d32c | 2000-09-12 03:42:30 +0000 | [diff] [blame] | 232 | |
| 233 | /* Don't issue an error message if the file doesn't exist. */ |
| 234 | if (errno != ENOENT && errno != ENOTDIR) |
| 235 | cpp_error_from_errno (pfile, filename); |
| 236 | |
Neil Booth | 2047e26 | 2000-09-21 18:01:22 +0000 | [diff] [blame] | 237 | /* Create a negative node for this path, and return null. */ |
| 238 | file->fd = -2; |
| 239 | |
Zack Weinberg | a58d32c | 2000-09-12 03:42:30 +0000 | [diff] [blame] | 240 | return 0; |
| 241 | } |
| 242 | |
Neil Booth | 3cf3593 | 2000-12-05 23:42:43 +0000 | [diff] [blame] | 243 | /* Place the file referenced by INC into a new buffer on PFILE's |
| 244 | stack. If there are errors, or the file should not be re-included, |
| 245 | a null buffer is pushed. */ |
Zack Weinberg | a58d32c | 2000-09-12 03:42:30 +0000 | [diff] [blame] | 246 | |
Neil Booth | 3cf3593 | 2000-12-05 23:42:43 +0000 | [diff] [blame] | 247 | static void |
Zack Weinberg | a58d32c | 2000-09-12 03:42:30 +0000 | [diff] [blame] | 248 | stack_include_file (pfile, inc) |
| 249 | cpp_reader *pfile; |
| 250 | struct include_file *inc; |
| 251 | { |
Neil Booth | 27e2564 | 2000-11-27 08:00:04 +0000 | [diff] [blame] | 252 | const char *filename = 0; |
| 253 | unsigned int lineno = 0; |
Zack Weinberg | a58d32c | 2000-09-12 03:42:30 +0000 | [diff] [blame] | 254 | cpp_buffer *fp; |
| 255 | |
Neil Booth | 27e2564 | 2000-11-27 08:00:04 +0000 | [diff] [blame] | 256 | if (pfile->buffer) |
| 257 | { |
| 258 | filename = pfile->buffer->nominal_fname; |
| 259 | lineno = pfile->buffer->lineno; |
| 260 | } |
| 261 | |
Neil Booth | 3cf3593 | 2000-12-05 23:42:43 +0000 | [diff] [blame] | 262 | /* Not in cache? */ |
| 263 | if (! inc->buffer) |
| 264 | read_include_file (pfile, inc); |
Neil Booth | a5c3ccc | 2000-10-30 22:29:00 +0000 | [diff] [blame] | 265 | |
Neil Booth | 3cf3593 | 2000-12-05 23:42:43 +0000 | [diff] [blame] | 266 | /* Push a null buffer. */ |
Zack Weinberg | a58d32c | 2000-09-12 03:42:30 +0000 | [diff] [blame] | 267 | fp = cpp_push_buffer (pfile, NULL, 0); |
Zack Weinberg | a58d32c | 2000-09-12 03:42:30 +0000 | [diff] [blame] | 268 | fp->inc = inc; |
| 269 | fp->nominal_fname = inc->name; |
| 270 | fp->buf = inc->buffer; |
Neil Booth | 3cf3593 | 2000-12-05 23:42:43 +0000 | [diff] [blame] | 271 | fp->rlimit = fp->buf; |
| 272 | if (! DO_NOT_REREAD (inc)) |
| 273 | fp->rlimit += inc->st.st_size; |
Zack Weinberg | a58d32c | 2000-09-12 03:42:30 +0000 | [diff] [blame] | 274 | fp->cur = fp->buf; |
Zack Weinberg | a58d32c | 2000-09-12 03:42:30 +0000 | [diff] [blame] | 275 | fp->line_base = fp->buf; |
Neil Booth | 3cf3593 | 2000-12-05 23:42:43 +0000 | [diff] [blame] | 276 | fp->lineno = 0; /* For _cpp_do_file_change. */ |
| 277 | fp->inc->refcnt++; |
Neil Booth | 642ce43 | 2000-12-07 23:17:56 +0000 | [diff] [blame] | 278 | if (inc->foundhere) |
| 279 | fp->sysp = inc->foundhere->sysp; |
Zack Weinberg | a58d32c | 2000-09-12 03:42:30 +0000 | [diff] [blame] | 280 | |
| 281 | /* The ->actual_dir field is only used when ignore_srcdir is not in effect; |
| 282 | see do_include */ |
| 283 | if (!CPP_OPTION (pfile, ignore_srcdir)) |
| 284 | fp->actual_dir = actual_directory (pfile, inc->name); |
| 285 | |
Neil Booth | 3cf3593 | 2000-12-05 23:42:43 +0000 | [diff] [blame] | 286 | /* Initialise controlling macro state. */ |
| 287 | pfile->mi_state = MI_OUTSIDE; |
| 288 | pfile->mi_cmacro = 0; |
Zack Weinberg | a58d32c | 2000-09-12 03:42:30 +0000 | [diff] [blame] | 289 | pfile->include_depth++; |
| 290 | pfile->input_stack_listing_current = 0; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 291 | |
Neil Booth | 27e2564 | 2000-11-27 08:00:04 +0000 | [diff] [blame] | 292 | _cpp_do_file_change (pfile, FC_ENTER, filename, lineno); |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 293 | |
Neil Booth | 27e2564 | 2000-11-27 08:00:04 +0000 | [diff] [blame] | 294 | fp->lineno = 1; |
Zack Weinberg | a58d32c | 2000-09-12 03:42:30 +0000 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | /* Read the file referenced by INC into the file cache. |
| 298 | |
| 299 | If fd points to a plain file, we might be able to mmap it; we can |
| 300 | definitely allocate the buffer all at once. If fd is a pipe or |
| 301 | terminal, we can't do either. If fd is something weird, like a |
| 302 | block device or a directory, we don't want to read it at all. |
| 303 | |
| 304 | Unfortunately, different systems use different st.st_mode values |
| 305 | for pipes: some have S_ISFIFO, some S_ISSOCK, some are buggy and |
| 306 | zero the entire struct stat except a couple fields. Hence we don't |
| 307 | even try to figure out what something is, except for plain files, |
| 308 | directories, and block devices. |
| 309 | |
| 310 | FIXME: Flush file cache and try again if we run out of memory. */ |
| 311 | |
Neil Booth | 3cf3593 | 2000-12-05 23:42:43 +0000 | [diff] [blame] | 312 | static void |
Zack Weinberg | a58d32c | 2000-09-12 03:42:30 +0000 | [diff] [blame] | 313 | read_include_file (pfile, inc) |
| 314 | cpp_reader *pfile; |
| 315 | struct include_file *inc; |
| 316 | { |
| 317 | ssize_t size, offset, count; |
| 318 | U_CHAR *buf; |
| 319 | #if MMAP_THRESHOLD |
| 320 | static int pagesize = -1; |
| 321 | #endif |
| 322 | |
Neil Booth | 3cf3593 | 2000-12-05 23:42:43 +0000 | [diff] [blame] | 323 | if (DO_NOT_REREAD (inc)) |
| 324 | return; |
| 325 | |
Zack Weinberg | a58d32c | 2000-09-12 03:42:30 +0000 | [diff] [blame] | 326 | if (S_ISREG (inc->st.st_mode)) |
| 327 | { |
| 328 | /* off_t might have a wider range than ssize_t - in other words, |
| 329 | the max size of a file might be bigger than the address |
| 330 | space. We can't handle a file that large. (Anyone with |
| 331 | a single source file bigger than 2GB needs to rethink |
| 332 | their coding style.) Some systems (e.g. AIX 4.1) define |
| 333 | SSIZE_MAX to be much smaller than the actual range of the |
| 334 | type. Use INTTYPE_MAXIMUM unconditionally to ensure this |
| 335 | does not bite us. */ |
| 336 | if (inc->st.st_size > INTTYPE_MAXIMUM (ssize_t)) |
| 337 | { |
| 338 | cpp_error (pfile, "%s is too large", inc->name); |
| 339 | goto fail; |
| 340 | } |
| 341 | size = inc->st.st_size; |
| 342 | |
Laurynas Biveinis | ae0f4de | 2000-09-16 18:17:53 +0000 | [diff] [blame] | 343 | inc->mapped = 0; |
Zack Weinberg | a58d32c | 2000-09-12 03:42:30 +0000 | [diff] [blame] | 344 | #if MMAP_THRESHOLD |
| 345 | if (pagesize == -1) |
| 346 | pagesize = getpagesize (); |
| 347 | |
| 348 | if (size / pagesize >= MMAP_THRESHOLD) |
| 349 | { |
| 350 | buf = (U_CHAR *) mmap (0, size, PROT_READ, MAP_PRIVATE, inc->fd, 0); |
| 351 | if (buf == (U_CHAR *)-1) |
| 352 | goto perror_fail; |
| 353 | inc->mapped = 1; |
| 354 | } |
| 355 | else |
| 356 | #endif |
| 357 | { |
| 358 | buf = (U_CHAR *) xmalloc (size); |
| 359 | offset = 0; |
| 360 | while (offset < size) |
| 361 | { |
| 362 | count = read (inc->fd, buf + offset, size - offset); |
| 363 | if (count < 0) |
| 364 | goto perror_fail; |
| 365 | if (count == 0) |
| 366 | { |
| 367 | cpp_warning (pfile, "%s is shorter than expected", inc->name); |
| 368 | break; |
| 369 | } |
| 370 | offset += count; |
| 371 | } |
Zack Weinberg | a58d32c | 2000-09-12 03:42:30 +0000 | [diff] [blame] | 372 | } |
| 373 | } |
| 374 | else if (S_ISBLK (inc->st.st_mode)) |
| 375 | { |
| 376 | cpp_error (pfile, "%s is a block device", inc->name); |
| 377 | goto fail; |
| 378 | } |
| 379 | else if (S_ISDIR (inc->st.st_mode)) |
| 380 | { |
| 381 | cpp_error (pfile, "%s is a directory", inc->name); |
| 382 | goto fail; |
| 383 | } |
| 384 | else |
| 385 | { |
| 386 | /* 8 kilobytes is a sensible starting size. It ought to be |
| 387 | bigger than the kernel pipe buffer, and it's definitely |
| 388 | bigger than the majority of C source files. */ |
| 389 | size = 8 * 1024; |
| 390 | |
| 391 | buf = (U_CHAR *) xmalloc (size); |
| 392 | offset = 0; |
| 393 | while ((count = read (inc->fd, buf + offset, size - offset)) > 0) |
| 394 | { |
| 395 | offset += count; |
| 396 | if (offset == size) |
| 397 | buf = xrealloc (buf, (size *= 2)); |
| 398 | } |
| 399 | if (count < 0) |
| 400 | goto perror_fail; |
| 401 | |
Zack Weinberg | a58d32c | 2000-09-12 03:42:30 +0000 | [diff] [blame] | 402 | if (offset < size) |
| 403 | buf = xrealloc (buf, offset); |
| 404 | inc->st.st_size = offset; |
| 405 | } |
| 406 | |
| 407 | close (inc->fd); |
| 408 | inc->buffer = buf; |
| 409 | inc->fd = -1; |
Neil Booth | 3cf3593 | 2000-12-05 23:42:43 +0000 | [diff] [blame] | 410 | return; |
Zack Weinberg | a58d32c | 2000-09-12 03:42:30 +0000 | [diff] [blame] | 411 | |
| 412 | perror_fail: |
| 413 | cpp_error_from_errno (pfile, inc->name); |
| 414 | fail: |
| 415 | /* Do not try to read this file again. */ |
| 416 | close (inc->fd); |
| 417 | inc->fd = -1; |
Neil Booth | 642ce43 | 2000-12-07 23:17:56 +0000 | [diff] [blame] | 418 | _cpp_never_reread (inc); |
Neil Booth | 3cf3593 | 2000-12-05 23:42:43 +0000 | [diff] [blame] | 419 | return; |
Zack Weinberg | a58d32c | 2000-09-12 03:42:30 +0000 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | static void |
| 423 | purge_cache (inc) |
| 424 | struct include_file *inc; |
| 425 | { |
| 426 | if (inc->buffer) |
| 427 | { |
Laurynas Biveinis | ae0f4de | 2000-09-16 18:17:53 +0000 | [diff] [blame] | 428 | #if MMAP_THRESHOLD |
Zack Weinberg | a58d32c | 2000-09-12 03:42:30 +0000 | [diff] [blame] | 429 | if (inc->mapped) |
Richard Henderson | 6f84c9b | 2000-09-16 10:48:10 -0700 | [diff] [blame] | 430 | munmap ((PTR) inc->buffer, inc->st.st_size); |
Zack Weinberg | a58d32c | 2000-09-12 03:42:30 +0000 | [diff] [blame] | 431 | else |
Laurynas Biveinis | ae0f4de | 2000-09-16 18:17:53 +0000 | [diff] [blame] | 432 | #endif |
Zack Weinberg | a58d32c | 2000-09-12 03:42:30 +0000 | [diff] [blame] | 433 | free ((PTR) inc->buffer); |
| 434 | inc->buffer = NULL; |
| 435 | } |
Zack Weinberg | add7091 | 1998-10-29 11:54:13 +0000 | [diff] [blame] | 436 | } |
| 437 | |
Zack Weinberg | b0699da | 2000-03-07 20:58:47 +0000 | [diff] [blame] | 438 | /* Return 1 if the file named by FNAME has been included before in |
| 439 | any context, 0 otherwise. */ |
| 440 | int |
| 441 | cpp_included (pfile, fname) |
| 442 | cpp_reader *pfile; |
| 443 | const char *fname; |
| 444 | { |
Zack Weinberg | c31a650 | 2000-06-21 18:33:51 +0000 | [diff] [blame] | 445 | struct file_name_list *path; |
| 446 | char *name; |
| 447 | splay_tree_node nd; |
Zack Weinberg | b0699da | 2000-03-07 20:58:47 +0000 | [diff] [blame] | 448 | |
Zack Weinberg | c31a650 | 2000-06-21 18:33:51 +0000 | [diff] [blame] | 449 | if (fname[0] == '/') |
Zack Weinberg | f2d5f0c | 2000-04-14 23:29:45 +0000 | [diff] [blame] | 450 | { |
Zack Weinberg | c31a650 | 2000-06-21 18:33:51 +0000 | [diff] [blame] | 451 | /* Just look it up. */ |
| 452 | nd = splay_tree_lookup (pfile->all_include_files, (splay_tree_key) fname); |
| 453 | return (nd && nd->value); |
Zack Weinberg | f2d5f0c | 2000-04-14 23:29:45 +0000 | [diff] [blame] | 454 | } |
Zack Weinberg | f2d5f0c | 2000-04-14 23:29:45 +0000 | [diff] [blame] | 455 | |
Zack Weinberg | c31a650 | 2000-06-21 18:33:51 +0000 | [diff] [blame] | 456 | /* Search directory path for the file. */ |
| 457 | name = (char *) alloca (strlen (fname) + pfile->max_include_len |
| 458 | + 2 + INCLUDE_LEN_FUDGE); |
| 459 | for (path = CPP_OPTION (pfile, quote_include); path; path = path->next) |
| 460 | { |
| 461 | memcpy (name, path->name, path->nlen); |
| 462 | name[path->nlen] = '/'; |
| 463 | strcpy (&name[path->nlen+1], fname); |
| 464 | _cpp_simplify_pathname (name); |
| 465 | if (CPP_OPTION (pfile, remap)) |
| 466 | name = remap_filename (pfile, name, path); |
| 467 | |
| 468 | nd = splay_tree_lookup (pfile->all_include_files, (splay_tree_key) name); |
| 469 | if (nd && nd->value) |
| 470 | return 1; |
Zack Weinberg | f2d5f0c | 2000-04-14 23:29:45 +0000 | [diff] [blame] | 471 | } |
Zack Weinberg | c31a650 | 2000-06-21 18:33:51 +0000 | [diff] [blame] | 472 | return 0; |
Zack Weinberg | e576beb | 2000-03-15 22:03:37 +0000 | [diff] [blame] | 473 | } |
| 474 | |
Zack Weinberg | 0b3d776 | 1998-11-25 11:56:54 +0000 | [diff] [blame] | 475 | /* Search for include file FNAME in the include chain starting at |
Zack Weinberg | c31a650 | 2000-06-21 18:33:51 +0000 | [diff] [blame] | 476 | SEARCH_START. Return 0 if there is no such file (or it's un-openable), |
Zack Weinberg | a58d32c | 2000-09-12 03:42:30 +0000 | [diff] [blame] | 477 | otherwise an include_file structure. */ |
Zack Weinberg | c31a650 | 2000-06-21 18:33:51 +0000 | [diff] [blame] | 478 | |
| 479 | static struct include_file * |
| 480 | find_include_file (pfile, fname, search_start) |
Zack Weinberg | add7091 | 1998-10-29 11:54:13 +0000 | [diff] [blame] | 481 | cpp_reader *pfile; |
Kaveh R. Ghazi | bcc5cac | 1999-09-07 15:41:26 +0000 | [diff] [blame] | 482 | const char *fname; |
Zack Weinberg | add7091 | 1998-10-29 11:54:13 +0000 | [diff] [blame] | 483 | struct file_name_list *search_start; |
Zack Weinberg | add7091 | 1998-10-29 11:54:13 +0000 | [diff] [blame] | 484 | { |
Zack Weinberg | d35364d | 2000-03-12 23:46:05 +0000 | [diff] [blame] | 485 | struct file_name_list *path; |
Zack Weinberg | 0b3d776 | 1998-11-25 11:56:54 +0000 | [diff] [blame] | 486 | char *name; |
Zack Weinberg | c31a650 | 2000-06-21 18:33:51 +0000 | [diff] [blame] | 487 | struct include_file *file; |
Zack Weinberg | add7091 | 1998-10-29 11:54:13 +0000 | [diff] [blame] | 488 | |
Zack Weinberg | c31a650 | 2000-06-21 18:33:51 +0000 | [diff] [blame] | 489 | if (fname[0] == '/') |
Neil Booth | 2047e26 | 2000-09-21 18:01:22 +0000 | [diff] [blame] | 490 | return open_file (pfile, fname); |
Zack Weinberg | c31a650 | 2000-06-21 18:33:51 +0000 | [diff] [blame] | 491 | |
| 492 | /* Search directory path for the file. */ |
| 493 | name = (char *) alloca (strlen (fname) + pfile->max_include_len |
| 494 | + 2 + INCLUDE_LEN_FUDGE); |
| 495 | for (path = search_start; path; path = path->next) |
Zack Weinberg | add7091 | 1998-10-29 11:54:13 +0000 | [diff] [blame] | 496 | { |
Zack Weinberg | c31a650 | 2000-06-21 18:33:51 +0000 | [diff] [blame] | 497 | memcpy (name, path->name, path->nlen); |
| 498 | name[path->nlen] = '/'; |
| 499 | strcpy (&name[path->nlen+1], fname); |
| 500 | _cpp_simplify_pathname (name); |
| 501 | if (CPP_OPTION (pfile, remap)) |
| 502 | name = remap_filename (pfile, name, path); |
Zack Weinberg | d35364d | 2000-03-12 23:46:05 +0000 | [diff] [blame] | 503 | |
Neil Booth | 2047e26 | 2000-09-21 18:01:22 +0000 | [diff] [blame] | 504 | file = open_file (pfile, name); |
Zack Weinberg | c31a650 | 2000-06-21 18:33:51 +0000 | [diff] [blame] | 505 | if (file) |
Zack Weinberg | d35364d | 2000-03-12 23:46:05 +0000 | [diff] [blame] | 506 | { |
Zack Weinberg | c31a650 | 2000-06-21 18:33:51 +0000 | [diff] [blame] | 507 | file->foundhere = path; |
| 508 | return file; |
Zack Weinberg | d35364d | 2000-03-12 23:46:05 +0000 | [diff] [blame] | 509 | } |
Zack Weinberg | d35364d | 2000-03-12 23:46:05 +0000 | [diff] [blame] | 510 | } |
Zack Weinberg | c31a650 | 2000-06-21 18:33:51 +0000 | [diff] [blame] | 511 | return 0; |
Zack Weinberg | add7091 | 1998-10-29 11:54:13 +0000 | [diff] [blame] | 512 | } |
| 513 | |
Zack Weinberg | e605b04 | 2000-06-21 23:08:17 +0000 | [diff] [blame] | 514 | /* Not everyone who wants to set system-header-ness on a buffer can |
Neil Booth | 642ce43 | 2000-12-07 23:17:56 +0000 | [diff] [blame] | 515 | see the details of a buffer. This is an exported interface because |
| 516 | fix-header needs it. */ |
Zack Weinberg | e605b04 | 2000-06-21 23:08:17 +0000 | [diff] [blame] | 517 | void |
Neil Booth | 614c7d3 | 2000-12-04 07:32:04 +0000 | [diff] [blame] | 518 | cpp_make_system_header (pfile, syshdr, externc) |
Zack Weinberg | e605b04 | 2000-06-21 23:08:17 +0000 | [diff] [blame] | 519 | cpp_reader *pfile; |
Neil Booth | 614c7d3 | 2000-12-04 07:32:04 +0000 | [diff] [blame] | 520 | int syshdr, externc; |
Zack Weinberg | e605b04 | 2000-06-21 23:08:17 +0000 | [diff] [blame] | 521 | { |
Neil Booth | 614c7d3 | 2000-12-04 07:32:04 +0000 | [diff] [blame] | 522 | int flags = 0; |
| 523 | |
| 524 | /* 1 = system header, 2 = system header to be treated as C. */ |
| 525 | if (syshdr) |
| 526 | flags = 1 + (externc != 0); |
Neil Booth | 642ce43 | 2000-12-07 23:17:56 +0000 | [diff] [blame] | 527 | pfile->buffer->sysp = flags; |
Zack Weinberg | e605b04 | 2000-06-21 23:08:17 +0000 | [diff] [blame] | 528 | } |
| 529 | |
Zack Weinberg | c71f835 | 2000-07-05 05:33:57 +0000 | [diff] [blame] | 530 | /* Report on all files that might benefit from a multiple include guard. |
| 531 | Triggered by -H. */ |
| 532 | void |
| 533 | _cpp_report_missing_guards (pfile) |
| 534 | cpp_reader *pfile; |
| 535 | { |
| 536 | int banner = 0; |
| 537 | splay_tree_foreach (pfile->all_include_files, report_missing_guard, |
| 538 | (PTR) &banner); |
| 539 | } |
| 540 | |
| 541 | static int |
| 542 | report_missing_guard (n, b) |
| 543 | splay_tree_node n; |
| 544 | void *b; |
| 545 | { |
| 546 | struct include_file *f = (struct include_file *) n->value; |
| 547 | int *bannerp = (int *)b; |
| 548 | |
| 549 | if (f && f->cmacro == 0 && f->include_count == 1) |
| 550 | { |
| 551 | if (*bannerp == 0) |
| 552 | { |
| 553 | fputs (_("Multiple include guards may be useful for:\n"), stderr); |
| 554 | *bannerp = 1; |
| 555 | } |
| 556 | fputs (f->name, stderr); |
| 557 | putc ('\n', stderr); |
| 558 | } |
| 559 | return 0; |
| 560 | } |
| 561 | |
Zack Weinberg | c31a650 | 2000-06-21 18:33:51 +0000 | [diff] [blame] | 562 | #define PRINT_THIS_DEP(p, b) (CPP_PRINT_DEPS(p) > (b||p->system_include_depth)) |
| 563 | void |
Neil Booth | 642ce43 | 2000-12-07 23:17:56 +0000 | [diff] [blame] | 564 | _cpp_execute_include (pfile, header, no_reinclude, include_next) |
Zack Weinberg | c31a650 | 2000-06-21 18:33:51 +0000 | [diff] [blame] | 565 | cpp_reader *pfile; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 566 | const cpp_token *header; |
Zack Weinberg | c31a650 | 2000-06-21 18:33:51 +0000 | [diff] [blame] | 567 | int no_reinclude; |
Neil Booth | 642ce43 | 2000-12-07 23:17:56 +0000 | [diff] [blame] | 568 | int include_next; |
Zack Weinberg | c31a650 | 2000-06-21 18:33:51 +0000 | [diff] [blame] | 569 | { |
Neil Booth | 642ce43 | 2000-12-07 23:17:56 +0000 | [diff] [blame] | 570 | struct file_name_list *search_start = 0; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 571 | unsigned int len = header->val.str.len; |
| 572 | unsigned int angle_brackets = header->type == CPP_HEADER_NAME; |
Zack Weinberg | c31a650 | 2000-06-21 18:33:51 +0000 | [diff] [blame] | 573 | struct include_file *inc; |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 574 | char *fname; |
Zack Weinberg | c31a650 | 2000-06-21 18:33:51 +0000 | [diff] [blame] | 575 | |
Neil Booth | 3cf3593 | 2000-12-05 23:42:43 +0000 | [diff] [blame] | 576 | /* Help protect #include or similar from recursion. */ |
| 577 | if (pfile->buffer_stack_depth >= CPP_STACK_MAX) |
| 578 | { |
| 579 | cpp_fatal (pfile, "#include nested too deeply"); |
| 580 | return; |
| 581 | } |
| 582 | |
| 583 | /* Check we've tidied up #include before entering the buffer. */ |
| 584 | if (pfile->context->prev) |
| 585 | { |
| 586 | cpp_ice (pfile, "attempt to push file buffer with contexts stacked"); |
| 587 | return; |
| 588 | } |
| 589 | |
Neil Booth | 642ce43 | 2000-12-07 23:17:56 +0000 | [diff] [blame] | 590 | /* For #include_next, skip in the search path past the dir in which |
| 591 | the current file was found. If this is the last directory in the |
| 592 | search path, don't include anything. If the current file was |
| 593 | specified with an absolute path, use the normal search logic. If |
| 594 | this is the primary source file, use the normal search logic and |
| 595 | generate a warning. */ |
| 596 | if (include_next) |
| 597 | { |
| 598 | if (! pfile->buffer->prev) |
| 599 | cpp_warning (pfile, "#include_next in primary source file"); |
| 600 | else |
| 601 | { |
| 602 | if (pfile->buffer->inc->foundhere) |
| 603 | { |
| 604 | search_start = pfile->buffer->inc->foundhere->next; |
| 605 | if (! search_start) |
| 606 | return; |
| 607 | } |
| 608 | } |
| 609 | } |
| 610 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 611 | fname = alloca (len + 1); |
| 612 | memcpy (fname, header->val.str.text, len); |
| 613 | fname[len] = '\0'; |
| 614 | |
Zack Weinberg | c31a650 | 2000-06-21 18:33:51 +0000 | [diff] [blame] | 615 | if (!search_start) |
| 616 | { |
| 617 | if (angle_brackets) |
| 618 | search_start = CPP_OPTION (pfile, bracket_include); |
| 619 | else if (CPP_OPTION (pfile, ignore_srcdir)) |
| 620 | search_start = CPP_OPTION (pfile, quote_include); |
| 621 | else |
| 622 | search_start = CPP_BUFFER (pfile)->actual_dir; |
Zack Weinberg | c31a650 | 2000-06-21 18:33:51 +0000 | [diff] [blame] | 623 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 624 | if (!search_start) |
| 625 | { |
| 626 | cpp_error (pfile, "No include path in which to find %s", fname); |
| 627 | return; |
| 628 | } |
Zack Weinberg | c31a650 | 2000-06-21 18:33:51 +0000 | [diff] [blame] | 629 | } |
| 630 | |
Zack Weinberg | c31a650 | 2000-06-21 18:33:51 +0000 | [diff] [blame] | 631 | inc = find_include_file (pfile, fname, search_start); |
| 632 | |
| 633 | if (inc) |
| 634 | { |
Zack Weinberg | c31a650 | 2000-06-21 18:33:51 +0000 | [diff] [blame] | 635 | /* For -M, add the file to the dependencies on its first inclusion. */ |
Zack Weinberg | d450696 | 2000-06-28 19:03:08 +0000 | [diff] [blame] | 636 | if (!inc->include_count && PRINT_THIS_DEP (pfile, angle_brackets)) |
Zack Weinberg | c31a650 | 2000-06-21 18:33:51 +0000 | [diff] [blame] | 637 | deps_add_dep (pfile->deps, inc->name); |
Zack Weinberg | d450696 | 2000-06-28 19:03:08 +0000 | [diff] [blame] | 638 | inc->include_count++; |
Zack Weinberg | c31a650 | 2000-06-21 18:33:51 +0000 | [diff] [blame] | 639 | |
Zack Weinberg | c31a650 | 2000-06-21 18:33:51 +0000 | [diff] [blame] | 640 | /* Actually process the file. */ |
Neil Booth | 3cf3593 | 2000-12-05 23:42:43 +0000 | [diff] [blame] | 641 | stack_include_file (pfile, inc); |
Zack Weinberg | a58d32c | 2000-09-12 03:42:30 +0000 | [diff] [blame] | 642 | |
Neil Booth | 3cf3593 | 2000-12-05 23:42:43 +0000 | [diff] [blame] | 643 | if (angle_brackets) |
| 644 | pfile->system_include_depth++; |
| 645 | |
| 646 | if (! DO_NOT_REREAD (inc)) |
| 647 | { |
Zack Weinberg | a58d32c | 2000-09-12 03:42:30 +0000 | [diff] [blame] | 648 | if (no_reinclude) |
Neil Booth | 642ce43 | 2000-12-07 23:17:56 +0000 | [diff] [blame] | 649 | _cpp_never_reread (inc); |
Zack Weinberg | a58d32c | 2000-09-12 03:42:30 +0000 | [diff] [blame] | 650 | |
| 651 | /* Handle -H option. */ |
| 652 | if (CPP_OPTION (pfile, print_include_names)) |
| 653 | { |
| 654 | cpp_buffer *fp = CPP_BUFFER (pfile); |
| 655 | while ((fp = CPP_PREV_BUFFER (fp)) != NULL) |
| 656 | putc ('.', stderr); |
| 657 | fprintf (stderr, " %s\n", inc->name); |
| 658 | } |
Zack Weinberg | c31a650 | 2000-06-21 18:33:51 +0000 | [diff] [blame] | 659 | } |
Neil Booth | 3cf3593 | 2000-12-05 23:42:43 +0000 | [diff] [blame] | 660 | |
Zack Weinberg | c31a650 | 2000-06-21 18:33:51 +0000 | [diff] [blame] | 661 | return; |
| 662 | } |
| 663 | |
| 664 | if (CPP_OPTION (pfile, print_deps_missing_files) |
| 665 | && PRINT_THIS_DEP (pfile, angle_brackets)) |
| 666 | { |
| 667 | if (!angle_brackets) |
| 668 | deps_add_dep (pfile->deps, fname); |
| 669 | else |
| 670 | { |
| 671 | char *p; |
| 672 | struct file_name_list *ptr; |
| 673 | /* If requested as a system header, assume it belongs in |
| 674 | the first system header directory. */ |
| 675 | if (CPP_OPTION (pfile, bracket_include)) |
| 676 | ptr = CPP_OPTION (pfile, bracket_include); |
| 677 | else |
| 678 | ptr = CPP_OPTION (pfile, quote_include); |
| 679 | |
| 680 | p = (char *) alloca (strlen (ptr->name) |
| 681 | + strlen (fname) + 2); |
| 682 | if (*ptr->name != '\0') |
| 683 | { |
| 684 | strcpy (p, ptr->name); |
| 685 | strcat (p, "/"); |
| 686 | } |
| 687 | strcat (p, fname); |
| 688 | _cpp_simplify_pathname (p); |
| 689 | deps_add_dep (pfile->deps, p); |
| 690 | } |
| 691 | } |
| 692 | /* If -M was specified, and this header file won't be added to |
| 693 | the dependency list, then don't count this as an error, |
| 694 | because we can still produce correct output. Otherwise, we |
| 695 | can't produce correct output, because there may be |
| 696 | dependencies we need inside the missing file, and we don't |
| 697 | know what directory this missing file exists in. */ |
| 698 | else if (CPP_PRINT_DEPS (pfile) |
| 699 | && ! PRINT_THIS_DEP (pfile, angle_brackets)) |
| 700 | cpp_warning (pfile, "No include path in which to find %s", fname); |
| 701 | else |
| 702 | cpp_error_from_errno (pfile, fname); |
| 703 | } |
| 704 | |
Nathan Sidwell | f3f751a | 2000-06-30 09:47:49 +0000 | [diff] [blame] | 705 | /* Locate file F, and determine whether it is newer than PFILE. Return -1, |
| 706 | if F cannot be located or dated, 1, if it is newer and 0 if older. */ |
Nathan Sidwell | f3f751a | 2000-06-30 09:47:49 +0000 | [diff] [blame] | 707 | int |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 708 | _cpp_compare_file_date (pfile, f) |
Nathan Sidwell | f3f751a | 2000-06-30 09:47:49 +0000 | [diff] [blame] | 709 | cpp_reader *pfile; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 710 | const cpp_token *f; |
Nathan Sidwell | f3f751a | 2000-06-30 09:47:49 +0000 | [diff] [blame] | 711 | { |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 712 | unsigned int len = f->val.str.len; |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 713 | char *fname; |
| 714 | struct file_name_list *search_start; |
Nathan Sidwell | f3f751a | 2000-06-30 09:47:49 +0000 | [diff] [blame] | 715 | struct include_file *inc; |
Nathan Sidwell | f3f751a | 2000-06-30 09:47:49 +0000 | [diff] [blame] | 716 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 717 | if (f->type == CPP_HEADER_NAME) |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 718 | search_start = CPP_OPTION (pfile, bracket_include); |
| 719 | else if (CPP_OPTION (pfile, ignore_srcdir)) |
| 720 | search_start = CPP_OPTION (pfile, quote_include); |
| 721 | else |
| 722 | search_start = CPP_BUFFER (pfile)->actual_dir; |
Nathan Sidwell | f3f751a | 2000-06-30 09:47:49 +0000 | [diff] [blame] | 723 | |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 724 | fname = alloca (len + 1); |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 725 | memcpy (fname, f->val.str.text, len); |
Nathan Sidwell | f3f751a | 2000-06-30 09:47:49 +0000 | [diff] [blame] | 726 | fname[len] = '\0'; |
Nathan Sidwell | f3f751a | 2000-06-30 09:47:49 +0000 | [diff] [blame] | 727 | inc = find_include_file (pfile, fname, search_start); |
| 728 | |
| 729 | if (!inc) |
| 730 | return -1; |
Zack Weinberg | a58d32c | 2000-09-12 03:42:30 +0000 | [diff] [blame] | 731 | if (inc->fd > 0) |
Nathan Sidwell | f3f751a | 2000-06-30 09:47:49 +0000 | [diff] [blame] | 732 | { |
Nathan Sidwell | f3f751a | 2000-06-30 09:47:49 +0000 | [diff] [blame] | 733 | close (inc->fd); |
| 734 | inc->fd = -1; |
| 735 | } |
Zack Weinberg | a58d32c | 2000-09-12 03:42:30 +0000 | [diff] [blame] | 736 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 737 | return inc->st.st_mtime > CPP_BUFFER (pfile)->inc->st.st_mtime; |
Nathan Sidwell | f3f751a | 2000-06-30 09:47:49 +0000 | [diff] [blame] | 738 | } |
| 739 | |
| 740 | |
Zack Weinberg | c31a650 | 2000-06-21 18:33:51 +0000 | [diff] [blame] | 741 | /* Push an input buffer and load it up with the contents of FNAME. |
| 742 | If FNAME is "" or NULL, read standard input. */ |
| 743 | int |
Neil Booth | 614c7d3 | 2000-12-04 07:32:04 +0000 | [diff] [blame] | 744 | _cpp_read_file (pfile, fname) |
Zack Weinberg | c31a650 | 2000-06-21 18:33:51 +0000 | [diff] [blame] | 745 | cpp_reader *pfile; |
| 746 | const char *fname; |
| 747 | { |
| 748 | struct include_file *f; |
| 749 | |
| 750 | if (fname == NULL) |
| 751 | fname = ""; |
| 752 | |
Neil Booth | 2047e26 | 2000-09-21 18:01:22 +0000 | [diff] [blame] | 753 | f = open_file (pfile, fname); |
Zack Weinberg | c31a650 | 2000-06-21 18:33:51 +0000 | [diff] [blame] | 754 | |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 755 | if (f == NULL) |
| 756 | { |
| 757 | cpp_error_from_errno (pfile, fname); |
| 758 | return 0; |
| 759 | } |
| 760 | |
Neil Booth | 3cf3593 | 2000-12-05 23:42:43 +0000 | [diff] [blame] | 761 | stack_include_file (pfile, f); |
| 762 | return 1; |
Zack Weinberg | c31a650 | 2000-06-21 18:33:51 +0000 | [diff] [blame] | 763 | } |
Zack Weinberg | f2d5f0c | 2000-04-14 23:29:45 +0000 | [diff] [blame] | 764 | |
Zack Weinberg | f9a0e96 | 2000-07-13 02:32:41 +0000 | [diff] [blame] | 765 | /* Do appropriate cleanup when a file buffer is popped off the input |
| 766 | stack. */ |
| 767 | void |
| 768 | _cpp_pop_file_buffer (pfile, buf) |
| 769 | cpp_reader *pfile; |
| 770 | cpp_buffer *buf; |
| 771 | { |
| 772 | struct include_file *inc = buf->inc; |
| 773 | |
| 774 | if (pfile->system_include_depth) |
| 775 | pfile->system_include_depth--; |
| 776 | if (pfile->include_depth) |
| 777 | pfile->include_depth--; |
Zack Weinberg | f9a0e96 | 2000-07-13 02:32:41 +0000 | [diff] [blame] | 778 | pfile->input_stack_listing_current = 0; |
| 779 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 780 | /* Record the inclusion-preventing macro and its definedness. */ |
| 781 | if (pfile->mi_state == MI_OUTSIDE && inc->cmacro != NEVER_REREAD) |
| 782 | { |
| 783 | /* This could be NULL meaning no controlling macro. */ |
| 784 | inc->cmacro = pfile->mi_cmacro; |
| 785 | inc->defined = 1; |
| 786 | } |
| 787 | |
| 788 | /* Invalidate control macros in the #including file. */ |
| 789 | pfile->mi_state = MI_FAILED; |
| 790 | |
Zack Weinberg | a58d32c | 2000-09-12 03:42:30 +0000 | [diff] [blame] | 791 | inc->refcnt--; |
| 792 | if (inc->refcnt == 0 && DO_NOT_REREAD (inc)) |
| 793 | purge_cache (inc); |
Zack Weinberg | f9a0e96 | 2000-07-13 02:32:41 +0000 | [diff] [blame] | 794 | } |
| 795 | |
Zack Weinberg | add7091 | 1998-10-29 11:54:13 +0000 | [diff] [blame] | 796 | /* The file_name_map structure holds a mapping of file names for a |
| 797 | particular directory. This mapping is read from the file named |
| 798 | FILE_NAME_MAP_FILE in that directory. Such a file can be used to |
| 799 | map filenames on a file system with severe filename restrictions, |
| 800 | such as DOS. The format of the file name map file is just a series |
| 801 | of lines with two tokens on each line. The first token is the name |
| 802 | to map, and the second token is the actual name to use. */ |
| 803 | |
| 804 | struct file_name_map |
| 805 | { |
| 806 | struct file_name_map *map_next; |
| 807 | char *map_from; |
| 808 | char *map_to; |
| 809 | }; |
| 810 | |
| 811 | #define FILE_NAME_MAP_FILE "header.gcc" |
| 812 | |
| 813 | /* Read a space delimited string of unlimited length from a stdio |
| 814 | file. */ |
| 815 | |
| 816 | static char * |
| 817 | read_filename_string (ch, f) |
| 818 | int ch; |
| 819 | FILE *f; |
| 820 | { |
| 821 | char *alloc, *set; |
| 822 | int len; |
| 823 | |
| 824 | len = 20; |
| 825 | set = alloc = xmalloc (len + 1); |
Zack Weinberg | a9ae448 | 1999-10-29 04:31:14 +0000 | [diff] [blame] | 826 | if (! is_space(ch)) |
Zack Weinberg | add7091 | 1998-10-29 11:54:13 +0000 | [diff] [blame] | 827 | { |
| 828 | *set++ = ch; |
Zack Weinberg | a9ae448 | 1999-10-29 04:31:14 +0000 | [diff] [blame] | 829 | while ((ch = getc (f)) != EOF && ! is_space(ch)) |
Zack Weinberg | add7091 | 1998-10-29 11:54:13 +0000 | [diff] [blame] | 830 | { |
| 831 | if (set - alloc == len) |
| 832 | { |
| 833 | len *= 2; |
| 834 | alloc = xrealloc (alloc, len + 1); |
| 835 | set = alloc + len / 2; |
| 836 | } |
| 837 | *set++ = ch; |
| 838 | } |
| 839 | } |
| 840 | *set = '\0'; |
| 841 | ungetc (ch, f); |
| 842 | return alloc; |
| 843 | } |
| 844 | |
| 845 | /* This structure holds a linked list of file name maps, one per directory. */ |
| 846 | |
| 847 | struct file_name_map_list |
| 848 | { |
| 849 | struct file_name_map_list *map_list_next; |
| 850 | char *map_list_name; |
| 851 | struct file_name_map *map_list_map; |
| 852 | }; |
| 853 | |
| 854 | /* Read the file name map file for DIRNAME. */ |
| 855 | |
| 856 | static struct file_name_map * |
| 857 | read_name_map (pfile, dirname) |
| 858 | cpp_reader *pfile; |
Kaveh R. Ghazi | 460ee11 | 1999-01-05 19:11:22 +0000 | [diff] [blame] | 859 | const char *dirname; |
Zack Weinberg | add7091 | 1998-10-29 11:54:13 +0000 | [diff] [blame] | 860 | { |
| 861 | register struct file_name_map_list *map_list_ptr; |
| 862 | char *name; |
| 863 | FILE *f; |
| 864 | |
Zack Weinberg | ae79697 | 2000-03-31 23:16:11 +0000 | [diff] [blame] | 865 | for (map_list_ptr = CPP_OPTION (pfile, map_list); map_list_ptr; |
Zack Weinberg | add7091 | 1998-10-29 11:54:13 +0000 | [diff] [blame] | 866 | map_list_ptr = map_list_ptr->map_list_next) |
| 867 | if (! strcmp (map_list_ptr->map_list_name, dirname)) |
| 868 | return map_list_ptr->map_list_map; |
| 869 | |
| 870 | map_list_ptr = ((struct file_name_map_list *) |
| 871 | xmalloc (sizeof (struct file_name_map_list))); |
Zack Weinberg | c49445e | 1998-12-15 11:23:27 +0000 | [diff] [blame] | 872 | map_list_ptr->map_list_name = xstrdup (dirname); |
Laurynas Biveinis | 4b58824 | 2000-07-16 21:22:19 +0000 | [diff] [blame] | 873 | map_list_ptr->map_list_map = NULL; |
Zack Weinberg | add7091 | 1998-10-29 11:54:13 +0000 | [diff] [blame] | 874 | |
| 875 | name = (char *) alloca (strlen (dirname) + strlen (FILE_NAME_MAP_FILE) + 2); |
| 876 | strcpy (name, dirname); |
| 877 | if (*dirname) |
| 878 | strcat (name, "/"); |
| 879 | strcat (name, FILE_NAME_MAP_FILE); |
| 880 | f = fopen (name, "r"); |
| 881 | if (!f) |
Zack Weinberg | 0b3d776 | 1998-11-25 11:56:54 +0000 | [diff] [blame] | 882 | map_list_ptr->map_list_map = (struct file_name_map *)-1; |
Zack Weinberg | add7091 | 1998-10-29 11:54:13 +0000 | [diff] [blame] | 883 | else |
| 884 | { |
| 885 | int ch; |
| 886 | int dirlen = strlen (dirname); |
| 887 | |
| 888 | while ((ch = getc (f)) != EOF) |
| 889 | { |
| 890 | char *from, *to; |
| 891 | struct file_name_map *ptr; |
| 892 | |
Zack Weinberg | a9ae448 | 1999-10-29 04:31:14 +0000 | [diff] [blame] | 893 | if (is_space(ch)) |
Zack Weinberg | add7091 | 1998-10-29 11:54:13 +0000 | [diff] [blame] | 894 | continue; |
| 895 | from = read_filename_string (ch, f); |
Zack Weinberg | a9ae448 | 1999-10-29 04:31:14 +0000 | [diff] [blame] | 896 | while ((ch = getc (f)) != EOF && is_hspace(ch)) |
Zack Weinberg | add7091 | 1998-10-29 11:54:13 +0000 | [diff] [blame] | 897 | ; |
| 898 | to = read_filename_string (ch, f); |
| 899 | |
| 900 | ptr = ((struct file_name_map *) |
| 901 | xmalloc (sizeof (struct file_name_map))); |
| 902 | ptr->map_from = from; |
| 903 | |
| 904 | /* Make the real filename absolute. */ |
| 905 | if (*to == '/') |
| 906 | ptr->map_to = to; |
| 907 | else |
| 908 | { |
| 909 | ptr->map_to = xmalloc (dirlen + strlen (to) + 2); |
| 910 | strcpy (ptr->map_to, dirname); |
| 911 | ptr->map_to[dirlen] = '/'; |
| 912 | strcpy (ptr->map_to + dirlen + 1, to); |
| 913 | free (to); |
| 914 | } |
| 915 | |
| 916 | ptr->map_next = map_list_ptr->map_list_map; |
| 917 | map_list_ptr->map_list_map = ptr; |
| 918 | |
| 919 | while ((ch = getc (f)) != '\n') |
| 920 | if (ch == EOF) |
| 921 | break; |
| 922 | } |
| 923 | fclose (f); |
| 924 | } |
| 925 | |
Zack Weinberg | ae79697 | 2000-03-31 23:16:11 +0000 | [diff] [blame] | 926 | map_list_ptr->map_list_next = CPP_OPTION (pfile, map_list); |
| 927 | CPP_OPTION (pfile, map_list) = map_list_ptr; |
Zack Weinberg | add7091 | 1998-10-29 11:54:13 +0000 | [diff] [blame] | 928 | |
| 929 | return map_list_ptr->map_list_map; |
| 930 | } |
| 931 | |
Zack Weinberg | 0b3d776 | 1998-11-25 11:56:54 +0000 | [diff] [blame] | 932 | /* Remap NAME based on the file_name_map (if any) for LOC. */ |
Zack Weinberg | add7091 | 1998-10-29 11:54:13 +0000 | [diff] [blame] | 933 | |
Zack Weinberg | 0b3d776 | 1998-11-25 11:56:54 +0000 | [diff] [blame] | 934 | static char * |
| 935 | remap_filename (pfile, name, loc) |
Zack Weinberg | add7091 | 1998-10-29 11:54:13 +0000 | [diff] [blame] | 936 | cpp_reader *pfile; |
Zack Weinberg | 0b3d776 | 1998-11-25 11:56:54 +0000 | [diff] [blame] | 937 | char *name; |
| 938 | struct file_name_list *loc; |
Zack Weinberg | add7091 | 1998-10-29 11:54:13 +0000 | [diff] [blame] | 939 | { |
Zack Weinberg | 0b3d776 | 1998-11-25 11:56:54 +0000 | [diff] [blame] | 940 | struct file_name_map *map; |
Kaveh R. Ghazi | 460ee11 | 1999-01-05 19:11:22 +0000 | [diff] [blame] | 941 | const char *from, *p, *dir; |
Zack Weinberg | 0b3d776 | 1998-11-25 11:56:54 +0000 | [diff] [blame] | 942 | |
| 943 | if (! loc->name_map) |
| 944 | loc->name_map = read_name_map (pfile, |
| 945 | loc->name |
| 946 | ? loc->name : "."); |
| 947 | |
| 948 | if (loc->name_map == (struct file_name_map *)-1) |
| 949 | return name; |
| 950 | |
| 951 | from = name + strlen (loc->name) + 1; |
| 952 | |
| 953 | for (map = loc->name_map; map; map = map->map_next) |
| 954 | if (!strcmp (map->map_from, from)) |
| 955 | return map->map_to; |
| 956 | |
| 957 | /* Try to find a mapping file for the particular directory we are |
| 958 | looking in. Thus #include <sys/types.h> will look up sys/types.h |
| 959 | in /usr/include/header.gcc and look up types.h in |
| 960 | /usr/include/sys/header.gcc. */ |
Neil Booth | 7ceb359 | 2000-03-11 00:49:44 +0000 | [diff] [blame] | 961 | p = strrchr (name, '/'); |
Zack Weinberg | 0b3d776 | 1998-11-25 11:56:54 +0000 | [diff] [blame] | 962 | if (!p) |
| 963 | p = name; |
| 964 | if (loc && loc->name |
| 965 | && strlen (loc->name) == (size_t) (p - name) |
| 966 | && !strncmp (loc->name, name, p - name)) |
| 967 | /* FILENAME is in SEARCHPTR, which we've already checked. */ |
| 968 | return name; |
| 969 | |
| 970 | if (p == name) |
Zack Weinberg | add7091 | 1998-10-29 11:54:13 +0000 | [diff] [blame] | 971 | { |
Zack Weinberg | 0b3d776 | 1998-11-25 11:56:54 +0000 | [diff] [blame] | 972 | dir = "."; |
| 973 | from = name; |
Zack Weinberg | add7091 | 1998-10-29 11:54:13 +0000 | [diff] [blame] | 974 | } |
Zack Weinberg | 0b3d776 | 1998-11-25 11:56:54 +0000 | [diff] [blame] | 975 | else |
| 976 | { |
Kaveh R. Ghazi | 460ee11 | 1999-01-05 19:11:22 +0000 | [diff] [blame] | 977 | char * newdir = (char *) alloca (p - name + 1); |
Neil Booth | 7ceb359 | 2000-03-11 00:49:44 +0000 | [diff] [blame] | 978 | memcpy (newdir, name, p - name); |
Kaveh R. Ghazi | 460ee11 | 1999-01-05 19:11:22 +0000 | [diff] [blame] | 979 | newdir[p - name] = '\0'; |
| 980 | dir = newdir; |
Zack Weinberg | 0b3d776 | 1998-11-25 11:56:54 +0000 | [diff] [blame] | 981 | from = p + 1; |
| 982 | } |
| 983 | |
| 984 | for (map = read_name_map (pfile, dir); map; map = map->map_next) |
| 985 | if (! strcmp (map->map_from, name)) |
| 986 | return map->map_to; |
Zack Weinberg | add7091 | 1998-10-29 11:54:13 +0000 | [diff] [blame] | 987 | |
Zack Weinberg | 0b3d776 | 1998-11-25 11:56:54 +0000 | [diff] [blame] | 988 | return name; |
Zack Weinberg | add7091 | 1998-10-29 11:54:13 +0000 | [diff] [blame] | 989 | } |
| 990 | |
Zack Weinberg | 6458033 | 1999-02-09 13:48:34 +0000 | [diff] [blame] | 991 | /* Given a path FNAME, extract the directory component and place it |
| 992 | onto the actual_dirs list. Return a pointer to the allocated |
| 993 | file_name_list structure. These structures are used to implement |
| 994 | current-directory "" include searching. */ |
| 995 | |
Zack Weinberg | f1a86df | 1998-12-07 13:35:20 +0000 | [diff] [blame] | 996 | static struct file_name_list * |
| 997 | actual_directory (pfile, fname) |
| 998 | cpp_reader *pfile; |
Kaveh R. Ghazi | bcc5cac | 1999-09-07 15:41:26 +0000 | [diff] [blame] | 999 | const char *fname; |
Zack Weinberg | f1a86df | 1998-12-07 13:35:20 +0000 | [diff] [blame] | 1000 | { |
| 1001 | char *last_slash, *dir; |
| 1002 | size_t dlen; |
| 1003 | struct file_name_list *x; |
| 1004 | |
Zack Weinberg | c49445e | 1998-12-15 11:23:27 +0000 | [diff] [blame] | 1005 | dir = xstrdup (fname); |
Neil Booth | 7ceb359 | 2000-03-11 00:49:44 +0000 | [diff] [blame] | 1006 | last_slash = strrchr (dir, '/'); |
Zack Weinberg | f1a86df | 1998-12-07 13:35:20 +0000 | [diff] [blame] | 1007 | if (last_slash) |
| 1008 | { |
| 1009 | if (last_slash == dir) |
| 1010 | { |
| 1011 | dlen = 1; |
| 1012 | last_slash[1] = '\0'; |
| 1013 | } |
| 1014 | else |
| 1015 | { |
| 1016 | dlen = last_slash - dir; |
| 1017 | *last_slash = '\0'; |
| 1018 | } |
| 1019 | } |
| 1020 | else |
| 1021 | { |
Greg McGary | 87ae0c7 | 2000-08-24 20:04:10 +0000 | [diff] [blame] | 1022 | free (dir); |
| 1023 | dir = xstrdup ("."); |
Zack Weinberg | f1a86df | 1998-12-07 13:35:20 +0000 | [diff] [blame] | 1024 | dlen = 1; |
| 1025 | } |
| 1026 | |
| 1027 | if (dlen > pfile->max_include_len) |
| 1028 | pfile->max_include_len = dlen; |
| 1029 | |
| 1030 | for (x = pfile->actual_dirs; x; x = x->alloc) |
| 1031 | if (!strcmp (x->name, dir)) |
| 1032 | { |
| 1033 | free (dir); |
| 1034 | return x; |
| 1035 | } |
| 1036 | |
| 1037 | /* Not found, make a new one. */ |
| 1038 | x = (struct file_name_list *) xmalloc (sizeof (struct file_name_list)); |
| 1039 | x->name = dir; |
| 1040 | x->nlen = dlen; |
Zack Weinberg | ae79697 | 2000-03-31 23:16:11 +0000 | [diff] [blame] | 1041 | x->next = CPP_OPTION (pfile, quote_include); |
Zack Weinberg | f1a86df | 1998-12-07 13:35:20 +0000 | [diff] [blame] | 1042 | x->alloc = pfile->actual_dirs; |
Neil Booth | 642ce43 | 2000-12-07 23:17:56 +0000 | [diff] [blame] | 1043 | x->sysp = pfile->buffer->sysp; |
Zack Weinberg | f1a86df | 1998-12-07 13:35:20 +0000 | [diff] [blame] | 1044 | x->name_map = NULL; |
| 1045 | |
| 1046 | pfile->actual_dirs = x; |
| 1047 | return x; |
| 1048 | } |
| 1049 | |
Zack Weinberg | 0b3d776 | 1998-11-25 11:56:54 +0000 | [diff] [blame] | 1050 | /* Simplify a path name in place, deleting redundant components. This |
| 1051 | reduces OS overhead and guarantees that equivalent paths compare |
| 1052 | the same (modulo symlinks). |
| 1053 | |
| 1054 | Transforms made: |
| 1055 | foo/bar/../quux foo/quux |
| 1056 | foo/./bar foo/bar |
| 1057 | foo//bar foo/bar |
| 1058 | /../quux /quux |
| 1059 | //quux //quux (POSIX allows leading // as a namespace escape) |
| 1060 | |
| 1061 | Guarantees no trailing slashes. All transforms reduce the length |
| 1062 | of the string. |
| 1063 | */ |
Zack Weinberg | 0b22d65 | 1999-03-15 18:42:46 +0000 | [diff] [blame] | 1064 | void |
Zack Weinberg | b0699da | 2000-03-07 20:58:47 +0000 | [diff] [blame] | 1065 | _cpp_simplify_pathname (path) |
Kaveh R. Ghazi | 21380ab | 1998-11-26 05:41:29 +0000 | [diff] [blame] | 1066 | char *path; |
Zack Weinberg | 0b3d776 | 1998-11-25 11:56:54 +0000 | [diff] [blame] | 1067 | { |
| 1068 | char *from, *to; |
| 1069 | char *base; |
| 1070 | int absolute = 0; |
| 1071 | |
Mark Elbrecht | 509781a | 1999-04-10 04:27:16 +0000 | [diff] [blame] | 1072 | #if defined (HAVE_DOS_BASED_FILE_SYSTEM) |
Zack Weinberg | 0b3d776 | 1998-11-25 11:56:54 +0000 | [diff] [blame] | 1073 | /* Convert all backslashes to slashes. */ |
| 1074 | for (from = path; *from; from++) |
| 1075 | if (*from == '\\') *from = '/'; |
| 1076 | |
| 1077 | /* Skip over leading drive letter if present. */ |
| 1078 | if (ISALPHA (path[0]) && path[1] == ':') |
| 1079 | from = to = &path[2]; |
| 1080 | else |
| 1081 | from = to = path; |
| 1082 | #else |
| 1083 | from = to = path; |
| 1084 | #endif |
| 1085 | |
| 1086 | /* Remove redundant initial /s. */ |
| 1087 | if (*from == '/') |
| 1088 | { |
| 1089 | absolute = 1; |
| 1090 | to++; |
| 1091 | from++; |
| 1092 | if (*from == '/') |
| 1093 | { |
| 1094 | if (*++from == '/') |
| 1095 | /* 3 or more initial /s are equivalent to 1 /. */ |
| 1096 | while (*++from == '/'); |
| 1097 | else |
| 1098 | /* On some hosts // differs from /; Posix allows this. */ |
| 1099 | to++; |
| 1100 | } |
| 1101 | } |
| 1102 | base = to; |
| 1103 | |
| 1104 | for (;;) |
| 1105 | { |
| 1106 | while (*from == '/') |
| 1107 | from++; |
| 1108 | |
| 1109 | if (from[0] == '.' && from[1] == '/') |
| 1110 | from += 2; |
| 1111 | else if (from[0] == '.' && from[1] == '\0') |
| 1112 | goto done; |
| 1113 | else if (from[0] == '.' && from[1] == '.' && from[2] == '/') |
| 1114 | { |
| 1115 | if (base == to) |
| 1116 | { |
| 1117 | if (absolute) |
| 1118 | from += 3; |
| 1119 | else |
| 1120 | { |
| 1121 | *to++ = *from++; |
| 1122 | *to++ = *from++; |
| 1123 | *to++ = *from++; |
| 1124 | base = to; |
| 1125 | } |
| 1126 | } |
| 1127 | else |
| 1128 | { |
| 1129 | to -= 2; |
| 1130 | while (to > base && *to != '/') to--; |
| 1131 | if (*to == '/') |
| 1132 | to++; |
| 1133 | from += 3; |
| 1134 | } |
| 1135 | } |
| 1136 | else if (from[0] == '.' && from[1] == '.' && from[2] == '\0') |
| 1137 | { |
| 1138 | if (base == to) |
| 1139 | { |
| 1140 | if (!absolute) |
| 1141 | { |
| 1142 | *to++ = *from++; |
| 1143 | *to++ = *from++; |
| 1144 | } |
| 1145 | } |
| 1146 | else |
| 1147 | { |
| 1148 | to -= 2; |
| 1149 | while (to > base && *to != '/') to--; |
| 1150 | if (*to == '/') |
| 1151 | to++; |
| 1152 | } |
| 1153 | goto done; |
| 1154 | } |
| 1155 | else |
| 1156 | /* Copy this component and trailing /, if any. */ |
| 1157 | while ((*to++ = *from++) != '/') |
| 1158 | { |
| 1159 | if (!to[-1]) |
| 1160 | { |
| 1161 | to--; |
| 1162 | goto done; |
| 1163 | } |
| 1164 | } |
| 1165 | |
| 1166 | } |
| 1167 | |
| 1168 | done: |
| 1169 | /* Trim trailing slash */ |
| 1170 | if (to[0] == '/' && (!absolute || to > path+1)) |
| 1171 | to--; |
| 1172 | |
| 1173 | /* Change the empty string to "." so that stat() on the result |
| 1174 | will always work. */ |
| 1175 | if (to == path) |
| 1176 | *to++ = '.'; |
| 1177 | |
| 1178 | *to = '\0'; |
| 1179 | |
| 1180 | return; |
| 1181 | } |
| 1182 | |
| 1183 | /* It is not clear when this should be used if at all, so I've |
| 1184 | disabled it until someone who understands VMS can look at it. */ |
| 1185 | #if 0 |
Zack Weinberg | add7091 | 1998-10-29 11:54:13 +0000 | [diff] [blame] | 1186 | |
| 1187 | /* Under VMS we need to fix up the "include" specification filename. |
| 1188 | |
| 1189 | Rules for possible conversions |
| 1190 | |
| 1191 | fullname tried paths |
| 1192 | |
| 1193 | name name |
| 1194 | ./dir/name [.dir]name |
| 1195 | /dir/name dir:name |
| 1196 | /name [000000]name, name |
| 1197 | dir/name dir:[000000]name, dir:name, dir/name |
| 1198 | dir1/dir2/name dir1:[dir2]name, dir1:[000000.dir2]name |
| 1199 | path:/name path:[000000]name, path:name |
| 1200 | path:/dir/name path:[000000.dir]name, path:[dir]name |
| 1201 | path:dir/name path:[dir]name |
| 1202 | [path]:[dir]name [path.dir]name |
| 1203 | path/[dir]name [path.dir]name |
| 1204 | |
| 1205 | The path:/name input is constructed when expanding <> includes. */ |
| 1206 | |
| 1207 | |
| 1208 | static void |
| 1209 | hack_vms_include_specification (fullname) |
| 1210 | char *fullname; |
| 1211 | { |
| 1212 | register char *basename, *unixname, *local_ptr, *first_slash; |
| 1213 | int f, check_filename_before_returning, must_revert; |
| 1214 | char Local[512]; |
| 1215 | |
| 1216 | check_filename_before_returning = 0; |
| 1217 | must_revert = 0; |
| 1218 | /* See if we can find a 1st slash. If not, there's no path information. */ |
Neil Booth | 7ceb359 | 2000-03-11 00:49:44 +0000 | [diff] [blame] | 1219 | first_slash = strchr (fullname, '/'); |
Zack Weinberg | add7091 | 1998-10-29 11:54:13 +0000 | [diff] [blame] | 1220 | if (first_slash == 0) |
| 1221 | return 0; /* Nothing to do!!! */ |
| 1222 | |
| 1223 | /* construct device spec if none given. */ |
| 1224 | |
Neil Booth | 7ceb359 | 2000-03-11 00:49:44 +0000 | [diff] [blame] | 1225 | if (strchr (fullname, ':') == 0) |
Zack Weinberg | add7091 | 1998-10-29 11:54:13 +0000 | [diff] [blame] | 1226 | { |
| 1227 | |
| 1228 | /* If fullname has a slash, take it as device spec. */ |
| 1229 | |
| 1230 | if (first_slash == fullname) |
| 1231 | { |
Neil Booth | 7ceb359 | 2000-03-11 00:49:44 +0000 | [diff] [blame] | 1232 | first_slash = strchr (fullname + 1, '/'); /* 2nd slash ? */ |
Zack Weinberg | add7091 | 1998-10-29 11:54:13 +0000 | [diff] [blame] | 1233 | if (first_slash) |
| 1234 | *first_slash = ':'; /* make device spec */ |
| 1235 | for (basename = fullname; *basename != 0; basename++) |
| 1236 | *basename = *(basename+1); /* remove leading slash */ |
| 1237 | } |
| 1238 | else if ((first_slash[-1] != '.') /* keep ':/', './' */ |
| 1239 | && (first_slash[-1] != ':') |
| 1240 | && (first_slash[-1] != ']')) /* or a vms path */ |
| 1241 | { |
| 1242 | *first_slash = ':'; |
| 1243 | } |
| 1244 | else if ((first_slash[1] == '[') /* skip './' in './[dir' */ |
| 1245 | && (first_slash[-1] == '.')) |
| 1246 | fullname += 2; |
| 1247 | } |
| 1248 | |
| 1249 | /* Get part after first ':' (basename[-1] == ':') |
| 1250 | or last '/' (basename[-1] == '/'). */ |
| 1251 | |
| 1252 | basename = base_name (fullname); |
| 1253 | |
| 1254 | local_ptr = Local; /* initialize */ |
| 1255 | |
| 1256 | /* We are trying to do a number of things here. First of all, we are |
| 1257 | trying to hammer the filenames into a standard format, such that later |
| 1258 | processing can handle them. |
| 1259 | |
| 1260 | If the file name contains something like [dir.], then it recognizes this |
| 1261 | as a root, and strips the ".]". Later processing will add whatever is |
| 1262 | needed to get things working properly. |
| 1263 | |
| 1264 | If no device is specified, then the first directory name is taken to be |
| 1265 | a device name (or a rooted logical). */ |
| 1266 | |
| 1267 | /* Point to the UNIX filename part (which needs to be fixed!) |
| 1268 | but skip vms path information. |
| 1269 | [basename != fullname since first_slash != 0]. */ |
| 1270 | |
| 1271 | if ((basename[-1] == ':') /* vms path spec. */ |
| 1272 | || (basename[-1] == ']') |
| 1273 | || (basename[-1] == '>')) |
| 1274 | unixname = basename; |
| 1275 | else |
| 1276 | unixname = fullname; |
| 1277 | |
| 1278 | if (*unixname == '/') |
| 1279 | unixname++; |
| 1280 | |
| 1281 | /* If the directory spec is not rooted, we can just copy |
| 1282 | the UNIX filename part and we are done. */ |
| 1283 | |
| 1284 | if (((basename - fullname) > 1) |
| 1285 | && ( (basename[-1] == ']') |
| 1286 | || (basename[-1] == '>'))) |
| 1287 | { |
| 1288 | if (basename[-2] != '.') |
| 1289 | { |
| 1290 | |
| 1291 | /* The VMS part ends in a `]', and the preceding character is not a `.'. |
| 1292 | -> PATH]:/name (basename = '/name', unixname = 'name') |
| 1293 | We strip the `]', and then splice the two parts of the name in the |
Zack Weinberg | 86702e3 | 2000-04-18 22:34:13 +0000 | [diff] [blame] | 1294 | usual way. Given the default locations for include files, |
Zack Weinberg | add7091 | 1998-10-29 11:54:13 +0000 | [diff] [blame] | 1295 | we will only use this code if the user specifies alternate locations |
| 1296 | with the /include (-I) switch on the command line. */ |
| 1297 | |
| 1298 | basename -= 1; /* Strip "]" */ |
| 1299 | unixname--; /* backspace */ |
| 1300 | } |
| 1301 | else |
| 1302 | { |
| 1303 | |
| 1304 | /* The VMS part has a ".]" at the end, and this will not do. Later |
| 1305 | processing will add a second directory spec, and this would be a syntax |
| 1306 | error. Thus we strip the ".]", and thus merge the directory specs. |
| 1307 | We also backspace unixname, so that it points to a '/'. This inhibits the |
| 1308 | generation of the 000000 root directory spec (which does not belong here |
| 1309 | in this case). */ |
| 1310 | |
| 1311 | basename -= 2; /* Strip ".]" */ |
| 1312 | unixname--; /* backspace */ |
| 1313 | } |
| 1314 | } |
| 1315 | |
| 1316 | else |
| 1317 | |
| 1318 | { |
| 1319 | |
| 1320 | /* We drop in here if there is no VMS style directory specification yet. |
| 1321 | If there is no device specification either, we make the first dir a |
| 1322 | device and try that. If we do not do this, then we will be essentially |
| 1323 | searching the users default directory (as if they did a #include "asdf.h"). |
| 1324 | |
| 1325 | Then all we need to do is to push a '[' into the output string. Later |
| 1326 | processing will fill this in, and close the bracket. */ |
| 1327 | |
| 1328 | if ((unixname != fullname) /* vms path spec found. */ |
| 1329 | && (basename[-1] != ':')) |
| 1330 | *local_ptr++ = ':'; /* dev not in spec. take first dir */ |
| 1331 | |
| 1332 | *local_ptr++ = '['; /* Open the directory specification */ |
| 1333 | } |
| 1334 | |
| 1335 | if (unixname == fullname) /* no vms dir spec. */ |
| 1336 | { |
| 1337 | must_revert = 1; |
| 1338 | if ((first_slash != 0) /* unix dir spec. */ |
| 1339 | && (*unixname != '/') /* not beginning with '/' */ |
| 1340 | && (*unixname != '.')) /* or './' or '../' */ |
| 1341 | *local_ptr++ = '.'; /* dir is local ! */ |
| 1342 | } |
| 1343 | |
| 1344 | /* at this point we assume that we have the device spec, and (at least |
| 1345 | the opening "[" for a directory specification. We may have directories |
| 1346 | specified already. |
| 1347 | |
| 1348 | If there are no other slashes then the filename will be |
| 1349 | in the "root" directory. Otherwise, we need to add |
| 1350 | directory specifications. */ |
| 1351 | |
Neil Booth | 7ceb359 | 2000-03-11 00:49:44 +0000 | [diff] [blame] | 1352 | if (strchr (unixname, '/') == 0) |
Zack Weinberg | add7091 | 1998-10-29 11:54:13 +0000 | [diff] [blame] | 1353 | { |
| 1354 | /* if no directories specified yet and none are following. */ |
| 1355 | if (local_ptr[-1] == '[') |
| 1356 | { |
| 1357 | /* Just add "000000]" as the directory string */ |
| 1358 | strcpy (local_ptr, "000000]"); |
| 1359 | local_ptr += strlen (local_ptr); |
| 1360 | check_filename_before_returning = 1; /* we might need to fool with this later */ |
| 1361 | } |
| 1362 | } |
| 1363 | else |
| 1364 | { |
| 1365 | |
| 1366 | /* As long as there are still subdirectories to add, do them. */ |
Neil Booth | 7ceb359 | 2000-03-11 00:49:44 +0000 | [diff] [blame] | 1367 | while (strchr (unixname, '/') != 0) |
Zack Weinberg | add7091 | 1998-10-29 11:54:13 +0000 | [diff] [blame] | 1368 | { |
| 1369 | /* If this token is "." we can ignore it |
| 1370 | if it's not at the beginning of a path. */ |
| 1371 | if ((unixname[0] == '.') && (unixname[1] == '/')) |
| 1372 | { |
| 1373 | /* remove it at beginning of path. */ |
| 1374 | if ( ((unixname == fullname) /* no device spec */ |
| 1375 | && (fullname+2 != basename)) /* starts with ./ */ |
| 1376 | /* or */ |
| 1377 | || ((basename[-1] == ':') /* device spec */ |
| 1378 | && (unixname-1 == basename))) /* and ./ afterwards */ |
| 1379 | *local_ptr++ = '.'; /* make '[.' start of path. */ |
| 1380 | unixname += 2; |
| 1381 | continue; |
| 1382 | } |
| 1383 | |
| 1384 | /* Add a subdirectory spec. Do not duplicate "." */ |
| 1385 | if ( local_ptr[-1] != '.' |
| 1386 | && local_ptr[-1] != '[' |
| 1387 | && local_ptr[-1] != '<') |
| 1388 | *local_ptr++ = '.'; |
| 1389 | |
| 1390 | /* If this is ".." then the spec becomes "-" */ |
| 1391 | if ( (unixname[0] == '.') |
| 1392 | && (unixname[1] == '.') |
| 1393 | && (unixname[2] == '/')) |
| 1394 | { |
| 1395 | /* Add "-" and skip the ".." */ |
| 1396 | if ((local_ptr[-1] == '.') |
| 1397 | && (local_ptr[-2] == '[')) |
| 1398 | local_ptr--; /* prevent [.- */ |
| 1399 | *local_ptr++ = '-'; |
| 1400 | unixname += 3; |
| 1401 | continue; |
| 1402 | } |
| 1403 | |
| 1404 | /* Copy the subdirectory */ |
| 1405 | while (*unixname != '/') |
| 1406 | *local_ptr++= *unixname++; |
| 1407 | |
| 1408 | unixname++; /* Skip the "/" */ |
| 1409 | } |
| 1410 | |
| 1411 | /* Close the directory specification */ |
| 1412 | if (local_ptr[-1] == '.') /* no trailing periods */ |
| 1413 | local_ptr--; |
| 1414 | |
| 1415 | if (local_ptr[-1] == '[') /* no dir needed */ |
| 1416 | local_ptr--; |
| 1417 | else |
| 1418 | *local_ptr++ = ']'; |
| 1419 | } |
| 1420 | |
| 1421 | /* Now add the filename. */ |
| 1422 | |
| 1423 | while (*unixname) |
| 1424 | *local_ptr++ = *unixname++; |
| 1425 | *local_ptr = 0; |
| 1426 | |
| 1427 | /* Now append it to the original VMS spec. */ |
| 1428 | |
| 1429 | strcpy ((must_revert==1)?fullname:basename, Local); |
| 1430 | |
| 1431 | /* If we put a [000000] in the filename, try to open it first. If this fails, |
| 1432 | remove the [000000], and return that name. This provides flexibility |
| 1433 | to the user in that they can use both rooted and non-rooted logical names |
| 1434 | to point to the location of the file. */ |
| 1435 | |
| 1436 | if (check_filename_before_returning) |
| 1437 | { |
Zack Weinberg | e576beb | 2000-03-15 22:03:37 +0000 | [diff] [blame] | 1438 | f = open (fullname, O_RDONLY|O_NONBLOCK); |
Zack Weinberg | add7091 | 1998-10-29 11:54:13 +0000 | [diff] [blame] | 1439 | if (f >= 0) |
| 1440 | { |
| 1441 | /* The file name is OK as it is, so return it as is. */ |
| 1442 | close (f); |
| 1443 | return 1; |
| 1444 | } |
| 1445 | |
| 1446 | /* The filename did not work. Try to remove the [000000] from the name, |
| 1447 | and return it. */ |
| 1448 | |
Neil Booth | 7ceb359 | 2000-03-11 00:49:44 +0000 | [diff] [blame] | 1449 | basename = strchr (fullname, '['); |
| 1450 | local_ptr = strchr (fullname, ']') + 1; |
Zack Weinberg | add7091 | 1998-10-29 11:54:13 +0000 | [diff] [blame] | 1451 | strcpy (basename, local_ptr); /* this gets rid of it */ |
| 1452 | |
| 1453 | } |
| 1454 | |
| 1455 | return 1; |
| 1456 | } |
| 1457 | #endif /* VMS */ |