blob: aeca30d627b35c6c60f72df5888d5939773195e9 [file] [log] [blame]
Zack Weinbergadd70911998-10-29 11:54:13 +00001/* Part of CPP library. (include file handling)
Jeff Law5e7b4e22000-02-25 22:59:31 -07002 Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1998,
3 1999, 2000 Free Software Foundation, Inc.
Zack Weinbergadd70911998-10-29 11:54:13 +00004 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
9This program is free software; you can redistribute it and/or modify it
10under the terms of the GNU General Public License as published by the
11Free Software Foundation; either version 2, or (at your option) any
12later version.
13
14This program is distributed in the hope that it will be useful,
15but WITHOUT ANY WARRANTY; without even the implied warranty of
16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17GNU General Public License for more details.
18
19You should have received a copy of the GNU General Public License
20along with this program; if not, write to the Free Software
Richard Kennere38992e2000-04-18 20:42:00 +000021Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
Zack Weinbergadd70911998-10-29 11:54:13 +000022
23#include "config.h"
24#include "system.h"
Zack Weinbergadd70911998-10-29 11:54:13 +000025#include "cpplib.h"
Zack Weinberg88ae23e2000-03-08 23:35:19 +000026#include "cpphash.h"
Zack Weinbergc1212d22000-02-06 23:46:18 +000027#include "intl.h"
Zack Weinberg168d3732000-03-14 06:34:11 +000028#include "mkdeps.h"
Zack Weinbergc31a6502000-06-21 18:33:51 +000029#include "splay-tree.h"
Zack Weinbergadd70911998-10-29 11:54:13 +000030
Zack Weinbergf8f769e2000-05-28 05:56:38 +000031#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 Weinbergd7a2e0f2000-06-01 20:06:57 +000042#ifndef O_BINARY
43# define O_BINARY 0
44#endif
45
Zack Weinberga58d32c2000-09-12 03:42:30 +000046#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 Weinbergf9a0e962000-07-13 02:32:41 +000062/* 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 Booth642ce432000-12-07 23:17:56 +000066/* This structure is used for the table of all includes. */
67struct 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 Booth28e0f042000-12-09 12:06:37 +000083/* 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 Weinberga73ac7a2000-01-30 18:09:07 +000093static struct file_name_map *read_name_map
Zack Weinberg38b24ee2000-03-08 20:37:23 +000094 PARAMS ((cpp_reader *, const char *));
95static char *read_filename_string PARAMS ((int, FILE *));
96static char *remap_filename PARAMS ((cpp_reader *, char *,
97 struct file_name_list *));
Zack Weinberga73ac7a2000-01-30 18:09:07 +000098static struct file_name_list *actual_directory
Zack Weinberg38b24ee2000-03-08 20:37:23 +000099 PARAMS ((cpp_reader *, const char *));
Zack Weinbergc31a6502000-06-21 18:33:51 +0000100static struct include_file *find_include_file
101 PARAMS ((cpp_reader *, const char *,
102 struct file_name_list *));
Neil Booth2047e262000-09-21 18:01:22 +0000103static struct include_file *open_file PARAMS ((cpp_reader *, const char *));
Neil Booth3cf35932000-12-05 23:42:43 +0000104static void read_include_file PARAMS ((cpp_reader *, struct include_file *));
105static void stack_include_file PARAMS ((cpp_reader *, struct include_file *));
Zack Weinberga58d32c2000-09-12 03:42:30 +0000106static void purge_cache PARAMS ((struct include_file *));
Zack Weinbergc31a6502000-06-21 18:33:51 +0000107static void destroy_include_file_node PARAMS ((splay_tree_value));
Zack Weinbergc71f8352000-07-05 05:33:57 +0000108static int report_missing_guard PARAMS ((splay_tree_node, void *));
Zack Weinbergc31a6502000-06-21 18:33:51 +0000109
Zack Weinberg0b3d7761998-11-25 11:56:54 +0000110#if 0
Kaveh R. Ghazif84c2012000-01-19 22:52:43 +0000111static void hack_vms_include_specification PARAMS ((char *));
Zack Weinberg0b3d7761998-11-25 11:56:54 +0000112#endif
Zack Weinbergadd70911998-10-29 11:54:13 +0000113
Zack Weinbergc31a6502000-06-21 18:33:51 +0000114/* 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
119static void
120destroy_include_file_node (v)
121 splay_tree_value v;
Zack Weinberg0b3d7761998-11-25 11:56:54 +0000122{
Zack Weinbergc31a6502000-06-21 18:33:51 +0000123 struct include_file *f = (struct include_file *)v;
124 if (f)
125 {
Zack Weinberga58d32c2000-09-12 03:42:30 +0000126 purge_cache (f);
Neil Booth2047e262000-09-21 18:01:22 +0000127 free (f); /* The tree is registered with free to free f->name. */
Zack Weinbergc31a6502000-06-21 18:33:51 +0000128 }
Zack Weinbergd35364d2000-03-12 23:46:05 +0000129}
Zack Weinberg0b3d7761998-11-25 11:56:54 +0000130
Zack Weinbergd35364d2000-03-12 23:46:05 +0000131void
Zack Weinbergc71f8352000-07-05 05:33:57 +0000132_cpp_init_includes (pfile)
Zack Weinbergd35364d2000-03-12 23:46:05 +0000133 cpp_reader *pfile;
134{
135 pfile->all_include_files
Zack Weinbergc31a6502000-06-21 18:33:51 +0000136 = splay_tree_new ((splay_tree_compare_fn) strcmp,
137 (splay_tree_delete_key_fn) free,
138 destroy_include_file_node);
Zack Weinberg0b3d7761998-11-25 11:56:54 +0000139}
140
Zack Weinbergc71f8352000-07-05 05:33:57 +0000141void
142_cpp_cleanup_includes (pfile)
143 cpp_reader *pfile;
144{
145 splay_tree_delete (pfile->all_include_files);
146}
147
Neil Booth642ce432000-12-07 23:17:56 +0000148/* Mark a file to not be reread (e.g. #import, read failure). */
149void
150_cpp_never_reread (file)
151 struct include_file *file;
152{
153 file->cmacro = NEVER_REREAD;
154}
155
Zack Weinberga58d32c2000-09-12 03:42:30 +0000156/* Given a file name, look it up in the cache; if there is no entry,
Neil Booth2047e262000-09-21 18:01:22 +0000157 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 Weinberg0b3d7761998-11-25 11:56:54 +0000164
Zack Weinbergc31a6502000-06-21 18:33:51 +0000165static struct include_file *
Neil Booth2047e262000-09-21 18:01:22 +0000166open_file (pfile, filename)
Zack Weinbergc31a6502000-06-21 18:33:51 +0000167 cpp_reader *pfile;
168 const char *filename;
Neil Booth2047e262000-09-21 18:01:22 +0000169{
Zack Weinbergc31a6502000-06-21 18:33:51 +0000170 splay_tree_node nd;
Neil Booth2047e262000-09-21 18:01:22 +0000171 struct include_file *file;
Zack Weinberg0b3d7761998-11-25 11:56:54 +0000172
Zack Weinberga58d32c2000-09-12 03:42:30 +0000173 nd = splay_tree_lookup (pfile->all_include_files, (splay_tree_key) filename);
Zack Weinberg0b3d7761998-11-25 11:56:54 +0000174
Zack Weinbergc31a6502000-06-21 18:33:51 +0000175 if (nd)
Neil Booth2047e262000-09-21 18:01:22 +0000176 {
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 Sidwelldef32632000-11-03 16:03:37 +0000183 /* 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 Booth2047e262000-09-21 18:01:22 +0000190 }
191 else
192 {
Neil Booth642ce432000-12-07 23:17:56 +0000193 /* In particular, this clears foundhere. */
Neil Booth2047e262000-09-21 18:01:22 +0000194 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 Weinbergc31a6502000-06-21 18:33:51 +0000200
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 Weinbergd4506962000-06-28 19:03:08 +0000214
Zack Weinbergc31a6502000-06-21 18:33:51 +0000215 if (filename[0] == '\0')
Neil Booth2047e262000-09-21 18:01:22 +0000216 file->fd = 0;
Zack Weinbergc31a6502000-06-21 18:33:51 +0000217 else
Neil Booth2047e262000-09-21 18:01:22 +0000218 file->fd = open (filename, O_RDONLY | O_NOCTTY | O_BINARY, 0666);
Zack Weinbergd4506962000-06-28 19:03:08 +0000219
Neil Booth2047e262000-09-21 18:01:22 +0000220 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 Sidwelldef32632000-11-03 16:03:37 +0000224 {
Neil Booth642ce432000-12-07 23:17:56 +0000225 _cpp_never_reread (file);
Nathan Sidwelldef32632000-11-03 16:03:37 +0000226 close (file->fd);
227 file->fd = -1;
228 }
Zack Weinberga58d32c2000-09-12 03:42:30 +0000229
Neil Booth2047e262000-09-21 18:01:22 +0000230 return file;
231 }
Zack Weinberga58d32c2000-09-12 03:42:30 +0000232
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 Booth2047e262000-09-21 18:01:22 +0000237 /* Create a negative node for this path, and return null. */
238 file->fd = -2;
239
Zack Weinberga58d32c2000-09-12 03:42:30 +0000240 return 0;
241}
242
Neil Booth3cf35932000-12-05 23:42:43 +0000243/* 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 Weinberga58d32c2000-09-12 03:42:30 +0000246
Neil Booth3cf35932000-12-05 23:42:43 +0000247static void
Zack Weinberga58d32c2000-09-12 03:42:30 +0000248stack_include_file (pfile, inc)
249 cpp_reader *pfile;
250 struct include_file *inc;
251{
Neil Booth27e25642000-11-27 08:00:04 +0000252 const char *filename = 0;
253 unsigned int lineno = 0;
Zack Weinberga58d32c2000-09-12 03:42:30 +0000254 cpp_buffer *fp;
255
Neil Booth27e25642000-11-27 08:00:04 +0000256 if (pfile->buffer)
257 {
258 filename = pfile->buffer->nominal_fname;
259 lineno = pfile->buffer->lineno;
260 }
261
Neil Booth3cf35932000-12-05 23:42:43 +0000262 /* Not in cache? */
263 if (! inc->buffer)
264 read_include_file (pfile, inc);
Neil Bootha5c3ccc2000-10-30 22:29:00 +0000265
Neil Booth3cf35932000-12-05 23:42:43 +0000266 /* Push a null buffer. */
Zack Weinberga58d32c2000-09-12 03:42:30 +0000267 fp = cpp_push_buffer (pfile, NULL, 0);
Zack Weinberga58d32c2000-09-12 03:42:30 +0000268 fp->inc = inc;
269 fp->nominal_fname = inc->name;
270 fp->buf = inc->buffer;
Neil Booth3cf35932000-12-05 23:42:43 +0000271 fp->rlimit = fp->buf;
272 if (! DO_NOT_REREAD (inc))
273 fp->rlimit += inc->st.st_size;
Zack Weinberga58d32c2000-09-12 03:42:30 +0000274 fp->cur = fp->buf;
Zack Weinberga58d32c2000-09-12 03:42:30 +0000275 fp->line_base = fp->buf;
Neil Booth3cf35932000-12-05 23:42:43 +0000276 fp->lineno = 0; /* For _cpp_do_file_change. */
277 fp->inc->refcnt++;
Neil Booth642ce432000-12-07 23:17:56 +0000278 if (inc->foundhere)
279 fp->sysp = inc->foundhere->sysp;
Zack Weinberga58d32c2000-09-12 03:42:30 +0000280
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 Booth3cf35932000-12-05 23:42:43 +0000286 /* Initialise controlling macro state. */
287 pfile->mi_state = MI_OUTSIDE;
288 pfile->mi_cmacro = 0;
Zack Weinberga58d32c2000-09-12 03:42:30 +0000289 pfile->include_depth++;
290 pfile->input_stack_listing_current = 0;
Neil Booth93c803682000-10-28 17:59:06 +0000291
Neil Booth27e25642000-11-27 08:00:04 +0000292 _cpp_do_file_change (pfile, FC_ENTER, filename, lineno);
Neil Booth93c803682000-10-28 17:59:06 +0000293
Neil Booth27e25642000-11-27 08:00:04 +0000294 fp->lineno = 1;
Zack Weinberga58d32c2000-09-12 03:42:30 +0000295}
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 Booth3cf35932000-12-05 23:42:43 +0000312static void
Zack Weinberga58d32c2000-09-12 03:42:30 +0000313read_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 Booth3cf35932000-12-05 23:42:43 +0000323 if (DO_NOT_REREAD (inc))
324 return;
325
Zack Weinberga58d32c2000-09-12 03:42:30 +0000326 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 Biveinisae0f4de2000-09-16 18:17:53 +0000343 inc->mapped = 0;
Zack Weinberga58d32c2000-09-12 03:42:30 +0000344#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 Weinberga58d32c2000-09-12 03:42:30 +0000372 }
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 Weinberga58d32c2000-09-12 03:42:30 +0000402 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 Booth3cf35932000-12-05 23:42:43 +0000410 return;
Zack Weinberga58d32c2000-09-12 03:42:30 +0000411
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 Booth642ce432000-12-07 23:17:56 +0000418 _cpp_never_reread (inc);
Neil Booth3cf35932000-12-05 23:42:43 +0000419 return;
Zack Weinberga58d32c2000-09-12 03:42:30 +0000420}
421
422static void
423purge_cache (inc)
424 struct include_file *inc;
425{
426 if (inc->buffer)
427 {
Laurynas Biveinisae0f4de2000-09-16 18:17:53 +0000428#if MMAP_THRESHOLD
Zack Weinberga58d32c2000-09-12 03:42:30 +0000429 if (inc->mapped)
Richard Henderson6f84c9b2000-09-16 10:48:10 -0700430 munmap ((PTR) inc->buffer, inc->st.st_size);
Zack Weinberga58d32c2000-09-12 03:42:30 +0000431 else
Laurynas Biveinisae0f4de2000-09-16 18:17:53 +0000432#endif
Zack Weinberga58d32c2000-09-12 03:42:30 +0000433 free ((PTR) inc->buffer);
434 inc->buffer = NULL;
435 }
Zack Weinbergadd70911998-10-29 11:54:13 +0000436}
437
Zack Weinbergb0699da2000-03-07 20:58:47 +0000438/* Return 1 if the file named by FNAME has been included before in
439 any context, 0 otherwise. */
440int
441cpp_included (pfile, fname)
442 cpp_reader *pfile;
443 const char *fname;
444{
Zack Weinbergc31a6502000-06-21 18:33:51 +0000445 struct file_name_list *path;
446 char *name;
447 splay_tree_node nd;
Zack Weinbergb0699da2000-03-07 20:58:47 +0000448
Zack Weinbergc31a6502000-06-21 18:33:51 +0000449 if (fname[0] == '/')
Zack Weinbergf2d5f0c2000-04-14 23:29:45 +0000450 {
Zack Weinbergc31a6502000-06-21 18:33:51 +0000451 /* Just look it up. */
452 nd = splay_tree_lookup (pfile->all_include_files, (splay_tree_key) fname);
453 return (nd && nd->value);
Zack Weinbergf2d5f0c2000-04-14 23:29:45 +0000454 }
Zack Weinbergf2d5f0c2000-04-14 23:29:45 +0000455
Zack Weinbergc31a6502000-06-21 18:33:51 +0000456 /* 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 Weinbergf2d5f0c2000-04-14 23:29:45 +0000471 }
Zack Weinbergc31a6502000-06-21 18:33:51 +0000472 return 0;
Zack Weinberge576beb2000-03-15 22:03:37 +0000473}
474
Zack Weinberg0b3d7761998-11-25 11:56:54 +0000475/* Search for include file FNAME in the include chain starting at
Zack Weinbergc31a6502000-06-21 18:33:51 +0000476 SEARCH_START. Return 0 if there is no such file (or it's un-openable),
Zack Weinberga58d32c2000-09-12 03:42:30 +0000477 otherwise an include_file structure. */
Zack Weinbergc31a6502000-06-21 18:33:51 +0000478
479static struct include_file *
480find_include_file (pfile, fname, search_start)
Zack Weinbergadd70911998-10-29 11:54:13 +0000481 cpp_reader *pfile;
Kaveh R. Ghazibcc5cac1999-09-07 15:41:26 +0000482 const char *fname;
Zack Weinbergadd70911998-10-29 11:54:13 +0000483 struct file_name_list *search_start;
Zack Weinbergadd70911998-10-29 11:54:13 +0000484{
Zack Weinbergd35364d2000-03-12 23:46:05 +0000485 struct file_name_list *path;
Zack Weinberg0b3d7761998-11-25 11:56:54 +0000486 char *name;
Zack Weinbergc31a6502000-06-21 18:33:51 +0000487 struct include_file *file;
Zack Weinbergadd70911998-10-29 11:54:13 +0000488
Zack Weinbergc31a6502000-06-21 18:33:51 +0000489 if (fname[0] == '/')
Neil Booth2047e262000-09-21 18:01:22 +0000490 return open_file (pfile, fname);
Zack Weinbergc31a6502000-06-21 18:33:51 +0000491
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 Weinbergadd70911998-10-29 11:54:13 +0000496 {
Zack Weinbergc31a6502000-06-21 18:33:51 +0000497 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 Weinbergd35364d2000-03-12 23:46:05 +0000503
Neil Booth2047e262000-09-21 18:01:22 +0000504 file = open_file (pfile, name);
Zack Weinbergc31a6502000-06-21 18:33:51 +0000505 if (file)
Zack Weinbergd35364d2000-03-12 23:46:05 +0000506 {
Zack Weinbergc31a6502000-06-21 18:33:51 +0000507 file->foundhere = path;
508 return file;
Zack Weinbergd35364d2000-03-12 23:46:05 +0000509 }
Zack Weinbergd35364d2000-03-12 23:46:05 +0000510 }
Zack Weinbergc31a6502000-06-21 18:33:51 +0000511 return 0;
Zack Weinbergadd70911998-10-29 11:54:13 +0000512}
513
Zack Weinberge605b042000-06-21 23:08:17 +0000514/* Not everyone who wants to set system-header-ness on a buffer can
Neil Booth642ce432000-12-07 23:17:56 +0000515 see the details of a buffer. This is an exported interface because
516 fix-header needs it. */
Zack Weinberge605b042000-06-21 23:08:17 +0000517void
Neil Booth614c7d32000-12-04 07:32:04 +0000518cpp_make_system_header (pfile, syshdr, externc)
Zack Weinberge605b042000-06-21 23:08:17 +0000519 cpp_reader *pfile;
Neil Booth614c7d32000-12-04 07:32:04 +0000520 int syshdr, externc;
Zack Weinberge605b042000-06-21 23:08:17 +0000521{
Neil Booth614c7d32000-12-04 07:32:04 +0000522 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 Booth642ce432000-12-07 23:17:56 +0000527 pfile->buffer->sysp = flags;
Zack Weinberge605b042000-06-21 23:08:17 +0000528}
529
Zack Weinbergc71f8352000-07-05 05:33:57 +0000530/* Report on all files that might benefit from a multiple include guard.
531 Triggered by -H. */
532void
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
541static int
542report_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 Weinbergc31a6502000-06-21 18:33:51 +0000562#define PRINT_THIS_DEP(p, b) (CPP_PRINT_DEPS(p) > (b||p->system_include_depth))
563void
Neil Booth642ce432000-12-07 23:17:56 +0000564_cpp_execute_include (pfile, header, no_reinclude, include_next)
Zack Weinbergc31a6502000-06-21 18:33:51 +0000565 cpp_reader *pfile;
Neil Booth93c803682000-10-28 17:59:06 +0000566 const cpp_token *header;
Zack Weinbergc31a6502000-06-21 18:33:51 +0000567 int no_reinclude;
Neil Booth642ce432000-12-07 23:17:56 +0000568 int include_next;
Zack Weinbergc31a6502000-06-21 18:33:51 +0000569{
Neil Booth642ce432000-12-07 23:17:56 +0000570 struct file_name_list *search_start = 0;
Neil Booth93c803682000-10-28 17:59:06 +0000571 unsigned int len = header->val.str.len;
572 unsigned int angle_brackets = header->type == CPP_HEADER_NAME;
Zack Weinbergc31a6502000-06-21 18:33:51 +0000573 struct include_file *inc;
Zack Weinberg041c3192000-07-04 01:58:21 +0000574 char *fname;
Zack Weinbergc31a6502000-06-21 18:33:51 +0000575
Neil Booth3cf35932000-12-05 23:42:43 +0000576 /* 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 Booth642ce432000-12-07 23:17:56 +0000590 /* 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 Booth93c803682000-10-28 17:59:06 +0000611 fname = alloca (len + 1);
612 memcpy (fname, header->val.str.text, len);
613 fname[len] = '\0';
614
Zack Weinbergc31a6502000-06-21 18:33:51 +0000615 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 Weinbergc31a6502000-06-21 18:33:51 +0000623
Neil Booth93c803682000-10-28 17:59:06 +0000624 if (!search_start)
625 {
626 cpp_error (pfile, "No include path in which to find %s", fname);
627 return;
628 }
Zack Weinbergc31a6502000-06-21 18:33:51 +0000629 }
630
Zack Weinbergc31a6502000-06-21 18:33:51 +0000631 inc = find_include_file (pfile, fname, search_start);
632
633 if (inc)
634 {
Zack Weinbergc31a6502000-06-21 18:33:51 +0000635 /* For -M, add the file to the dependencies on its first inclusion. */
Zack Weinbergd4506962000-06-28 19:03:08 +0000636 if (!inc->include_count && PRINT_THIS_DEP (pfile, angle_brackets))
Zack Weinbergc31a6502000-06-21 18:33:51 +0000637 deps_add_dep (pfile->deps, inc->name);
Zack Weinbergd4506962000-06-28 19:03:08 +0000638 inc->include_count++;
Zack Weinbergc31a6502000-06-21 18:33:51 +0000639
Zack Weinbergc31a6502000-06-21 18:33:51 +0000640 /* Actually process the file. */
Neil Booth3cf35932000-12-05 23:42:43 +0000641 stack_include_file (pfile, inc);
Zack Weinberga58d32c2000-09-12 03:42:30 +0000642
Neil Booth3cf35932000-12-05 23:42:43 +0000643 if (angle_brackets)
644 pfile->system_include_depth++;
645
646 if (! DO_NOT_REREAD (inc))
647 {
Zack Weinberga58d32c2000-09-12 03:42:30 +0000648 if (no_reinclude)
Neil Booth642ce432000-12-07 23:17:56 +0000649 _cpp_never_reread (inc);
Zack Weinberga58d32c2000-09-12 03:42:30 +0000650
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 Weinbergc31a6502000-06-21 18:33:51 +0000659 }
Neil Booth3cf35932000-12-05 23:42:43 +0000660
Zack Weinbergc31a6502000-06-21 18:33:51 +0000661 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 Sidwellf3f751a2000-06-30 09:47:49 +0000705/* 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 Sidwellf3f751a2000-06-30 09:47:49 +0000707int
Neil Booth93c803682000-10-28 17:59:06 +0000708_cpp_compare_file_date (pfile, f)
Nathan Sidwellf3f751a2000-06-30 09:47:49 +0000709 cpp_reader *pfile;
Neil Booth93c803682000-10-28 17:59:06 +0000710 const cpp_token *f;
Nathan Sidwellf3f751a2000-06-30 09:47:49 +0000711{
Neil Booth93c803682000-10-28 17:59:06 +0000712 unsigned int len = f->val.str.len;
Zack Weinberg041c3192000-07-04 01:58:21 +0000713 char *fname;
714 struct file_name_list *search_start;
Nathan Sidwellf3f751a2000-06-30 09:47:49 +0000715 struct include_file *inc;
Nathan Sidwellf3f751a2000-06-30 09:47:49 +0000716
Neil Booth93c803682000-10-28 17:59:06 +0000717 if (f->type == CPP_HEADER_NAME)
Zack Weinberg041c3192000-07-04 01:58:21 +0000718 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 Sidwellf3f751a2000-06-30 09:47:49 +0000723
Zack Weinberg041c3192000-07-04 01:58:21 +0000724 fname = alloca (len + 1);
Neil Booth93c803682000-10-28 17:59:06 +0000725 memcpy (fname, f->val.str.text, len);
Nathan Sidwellf3f751a2000-06-30 09:47:49 +0000726 fname[len] = '\0';
Nathan Sidwellf3f751a2000-06-30 09:47:49 +0000727 inc = find_include_file (pfile, fname, search_start);
728
729 if (!inc)
730 return -1;
Zack Weinberga58d32c2000-09-12 03:42:30 +0000731 if (inc->fd > 0)
Nathan Sidwellf3f751a2000-06-30 09:47:49 +0000732 {
Nathan Sidwellf3f751a2000-06-30 09:47:49 +0000733 close (inc->fd);
734 inc->fd = -1;
735 }
Zack Weinberga58d32c2000-09-12 03:42:30 +0000736
Neil Booth93c803682000-10-28 17:59:06 +0000737 return inc->st.st_mtime > CPP_BUFFER (pfile)->inc->st.st_mtime;
Nathan Sidwellf3f751a2000-06-30 09:47:49 +0000738}
739
740
Zack Weinbergc31a6502000-06-21 18:33:51 +0000741/* Push an input buffer and load it up with the contents of FNAME.
742 If FNAME is "" or NULL, read standard input. */
743int
Neil Booth614c7d32000-12-04 07:32:04 +0000744_cpp_read_file (pfile, fname)
Zack Weinbergc31a6502000-06-21 18:33:51 +0000745 cpp_reader *pfile;
746 const char *fname;
747{
748 struct include_file *f;
749
750 if (fname == NULL)
751 fname = "";
752
Neil Booth2047e262000-09-21 18:01:22 +0000753 f = open_file (pfile, fname);
Zack Weinbergc31a6502000-06-21 18:33:51 +0000754
Zack Weinberg041c3192000-07-04 01:58:21 +0000755 if (f == NULL)
756 {
757 cpp_error_from_errno (pfile, fname);
758 return 0;
759 }
760
Neil Booth3cf35932000-12-05 23:42:43 +0000761 stack_include_file (pfile, f);
762 return 1;
Zack Weinbergc31a6502000-06-21 18:33:51 +0000763}
Zack Weinbergf2d5f0c2000-04-14 23:29:45 +0000764
Zack Weinbergf9a0e962000-07-13 02:32:41 +0000765/* Do appropriate cleanup when a file buffer is popped off the input
766 stack. */
767void
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 Weinbergf9a0e962000-07-13 02:32:41 +0000778 pfile->input_stack_listing_current = 0;
779
Neil Booth93c803682000-10-28 17:59:06 +0000780 /* 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 Weinberga58d32c2000-09-12 03:42:30 +0000791 inc->refcnt--;
792 if (inc->refcnt == 0 && DO_NOT_REREAD (inc))
793 purge_cache (inc);
Zack Weinbergf9a0e962000-07-13 02:32:41 +0000794}
795
Zack Weinbergadd70911998-10-29 11:54:13 +0000796/* 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
804struct 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
816static char *
817read_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 Weinberga9ae4481999-10-29 04:31:14 +0000826 if (! is_space(ch))
Zack Weinbergadd70911998-10-29 11:54:13 +0000827 {
828 *set++ = ch;
Zack Weinberga9ae4481999-10-29 04:31:14 +0000829 while ((ch = getc (f)) != EOF && ! is_space(ch))
Zack Weinbergadd70911998-10-29 11:54:13 +0000830 {
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
847struct 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
856static struct file_name_map *
857read_name_map (pfile, dirname)
858 cpp_reader *pfile;
Kaveh R. Ghazi460ee111999-01-05 19:11:22 +0000859 const char *dirname;
Zack Weinbergadd70911998-10-29 11:54:13 +0000860{
861 register struct file_name_map_list *map_list_ptr;
862 char *name;
863 FILE *f;
864
Zack Weinbergae796972000-03-31 23:16:11 +0000865 for (map_list_ptr = CPP_OPTION (pfile, map_list); map_list_ptr;
Zack Weinbergadd70911998-10-29 11:54:13 +0000866 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 Weinbergc49445e1998-12-15 11:23:27 +0000872 map_list_ptr->map_list_name = xstrdup (dirname);
Laurynas Biveinis4b588242000-07-16 21:22:19 +0000873 map_list_ptr->map_list_map = NULL;
Zack Weinbergadd70911998-10-29 11:54:13 +0000874
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 Weinberg0b3d7761998-11-25 11:56:54 +0000882 map_list_ptr->map_list_map = (struct file_name_map *)-1;
Zack Weinbergadd70911998-10-29 11:54:13 +0000883 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 Weinberga9ae4481999-10-29 04:31:14 +0000893 if (is_space(ch))
Zack Weinbergadd70911998-10-29 11:54:13 +0000894 continue;
895 from = read_filename_string (ch, f);
Zack Weinberga9ae4481999-10-29 04:31:14 +0000896 while ((ch = getc (f)) != EOF && is_hspace(ch))
Zack Weinbergadd70911998-10-29 11:54:13 +0000897 ;
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 Weinbergae796972000-03-31 23:16:11 +0000926 map_list_ptr->map_list_next = CPP_OPTION (pfile, map_list);
927 CPP_OPTION (pfile, map_list) = map_list_ptr;
Zack Weinbergadd70911998-10-29 11:54:13 +0000928
929 return map_list_ptr->map_list_map;
930}
931
Zack Weinberg0b3d7761998-11-25 11:56:54 +0000932/* Remap NAME based on the file_name_map (if any) for LOC. */
Zack Weinbergadd70911998-10-29 11:54:13 +0000933
Zack Weinberg0b3d7761998-11-25 11:56:54 +0000934static char *
935remap_filename (pfile, name, loc)
Zack Weinbergadd70911998-10-29 11:54:13 +0000936 cpp_reader *pfile;
Zack Weinberg0b3d7761998-11-25 11:56:54 +0000937 char *name;
938 struct file_name_list *loc;
Zack Weinbergadd70911998-10-29 11:54:13 +0000939{
Zack Weinberg0b3d7761998-11-25 11:56:54 +0000940 struct file_name_map *map;
Kaveh R. Ghazi460ee111999-01-05 19:11:22 +0000941 const char *from, *p, *dir;
Zack Weinberg0b3d7761998-11-25 11:56:54 +0000942
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 Booth7ceb3592000-03-11 00:49:44 +0000961 p = strrchr (name, '/');
Zack Weinberg0b3d7761998-11-25 11:56:54 +0000962 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 Weinbergadd70911998-10-29 11:54:13 +0000971 {
Zack Weinberg0b3d7761998-11-25 11:56:54 +0000972 dir = ".";
973 from = name;
Zack Weinbergadd70911998-10-29 11:54:13 +0000974 }
Zack Weinberg0b3d7761998-11-25 11:56:54 +0000975 else
976 {
Kaveh R. Ghazi460ee111999-01-05 19:11:22 +0000977 char * newdir = (char *) alloca (p - name + 1);
Neil Booth7ceb3592000-03-11 00:49:44 +0000978 memcpy (newdir, name, p - name);
Kaveh R. Ghazi460ee111999-01-05 19:11:22 +0000979 newdir[p - name] = '\0';
980 dir = newdir;
Zack Weinberg0b3d7761998-11-25 11:56:54 +0000981 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 Weinbergadd70911998-10-29 11:54:13 +0000987
Zack Weinberg0b3d7761998-11-25 11:56:54 +0000988 return name;
Zack Weinbergadd70911998-10-29 11:54:13 +0000989}
990
Zack Weinberg64580331999-02-09 13:48:34 +0000991/* 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 Weinbergf1a86df1998-12-07 13:35:20 +0000996static struct file_name_list *
997actual_directory (pfile, fname)
998 cpp_reader *pfile;
Kaveh R. Ghazibcc5cac1999-09-07 15:41:26 +0000999 const char *fname;
Zack Weinbergf1a86df1998-12-07 13:35:20 +00001000{
1001 char *last_slash, *dir;
1002 size_t dlen;
1003 struct file_name_list *x;
1004
Zack Weinbergc49445e1998-12-15 11:23:27 +00001005 dir = xstrdup (fname);
Neil Booth7ceb3592000-03-11 00:49:44 +00001006 last_slash = strrchr (dir, '/');
Zack Weinbergf1a86df1998-12-07 13:35:20 +00001007 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 McGary87ae0c72000-08-24 20:04:10 +00001022 free (dir);
1023 dir = xstrdup (".");
Zack Weinbergf1a86df1998-12-07 13:35:20 +00001024 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 Weinbergae796972000-03-31 23:16:11 +00001041 x->next = CPP_OPTION (pfile, quote_include);
Zack Weinbergf1a86df1998-12-07 13:35:20 +00001042 x->alloc = pfile->actual_dirs;
Neil Booth642ce432000-12-07 23:17:56 +00001043 x->sysp = pfile->buffer->sysp;
Zack Weinbergf1a86df1998-12-07 13:35:20 +00001044 x->name_map = NULL;
1045
1046 pfile->actual_dirs = x;
1047 return x;
1048}
1049
Zack Weinberg0b3d7761998-11-25 11:56:54 +00001050/* 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 Weinberg0b22d651999-03-15 18:42:46 +00001064void
Zack Weinbergb0699da2000-03-07 20:58:47 +00001065_cpp_simplify_pathname (path)
Kaveh R. Ghazi21380ab1998-11-26 05:41:29 +00001066 char *path;
Zack Weinberg0b3d7761998-11-25 11:56:54 +00001067{
1068 char *from, *to;
1069 char *base;
1070 int absolute = 0;
1071
Mark Elbrecht509781a1999-04-10 04:27:16 +00001072#if defined (HAVE_DOS_BASED_FILE_SYSTEM)
Zack Weinberg0b3d7761998-11-25 11:56:54 +00001073 /* 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 Weinbergadd70911998-10-29 11:54:13 +00001186
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
1208static void
1209hack_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 Booth7ceb3592000-03-11 00:49:44 +00001219 first_slash = strchr (fullname, '/');
Zack Weinbergadd70911998-10-29 11:54:13 +00001220 if (first_slash == 0)
1221 return 0; /* Nothing to do!!! */
1222
1223 /* construct device spec if none given. */
1224
Neil Booth7ceb3592000-03-11 00:49:44 +00001225 if (strchr (fullname, ':') == 0)
Zack Weinbergadd70911998-10-29 11:54:13 +00001226 {
1227
1228 /* If fullname has a slash, take it as device spec. */
1229
1230 if (first_slash == fullname)
1231 {
Neil Booth7ceb3592000-03-11 00:49:44 +00001232 first_slash = strchr (fullname + 1, '/'); /* 2nd slash ? */
Zack Weinbergadd70911998-10-29 11:54:13 +00001233 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 Weinberg86702e32000-04-18 22:34:13 +00001294 usual way. Given the default locations for include files,
Zack Weinbergadd70911998-10-29 11:54:13 +00001295 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 Booth7ceb3592000-03-11 00:49:44 +00001352 if (strchr (unixname, '/') == 0)
Zack Weinbergadd70911998-10-29 11:54:13 +00001353 {
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 Booth7ceb3592000-03-11 00:49:44 +00001367 while (strchr (unixname, '/') != 0)
Zack Weinbergadd70911998-10-29 11:54:13 +00001368 {
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 Weinberge576beb2000-03-15 22:03:37 +00001438 f = open (fullname, O_RDONLY|O_NONBLOCK);
Zack Weinbergadd70911998-10-29 11:54:13 +00001439 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 Booth7ceb3592000-03-11 00:49:44 +00001449 basename = strchr (fullname, '[');
1450 local_ptr = strchr (fullname, ']') + 1;
Zack Weinbergadd70911998-10-29 11:54:13 +00001451 strcpy (basename, local_ptr); /* this gets rid of it */
1452
1453 }
1454
1455 return 1;
1456}
1457#endif /* VMS */