cppfiles.c (open_include_file): New function.
[gcc.git] / gcc / cppfiles.c
1 /* Part of CPP library. (include file handling)
2 Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1998,
3 1999, 2000 Free Software Foundation, Inc.
4 Written by Per Bothner, 1994.
5 Based on CCCP program by Paul Rubin, June 1986
6 Adapted to ANSI C, Richard Stallman, Jan 1987
7 Split out of cpplib.c, Zack Weinberg, Oct 1998
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
21 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22
23 In other words, you are welcome to use, share and improve this program.
24 You are forbidden to forbid anyone else to use, share and improve
25 what you give them. Help stamp out software-hoarding! */
26
27 #include "config.h"
28 #include "system.h"
29 #include "cpplib.h"
30 #include "cpphash.h"
31 #include "hashtab.h"
32 #include "intl.h"
33 #include "mkdeps.h"
34
35 static IHASH *redundant_include_p PARAMS ((cpp_reader *, IHASH *,
36 struct file_name_list *));
37 static struct file_name_map *read_name_map
38 PARAMS ((cpp_reader *, const char *));
39 static char *read_filename_string PARAMS ((int, FILE *));
40 static char *remap_filename PARAMS ((cpp_reader *, char *,
41 struct file_name_list *));
42 static struct file_name_list *actual_directory
43 PARAMS ((cpp_reader *, const char *));
44 static unsigned int hash_IHASH PARAMS ((const void *));
45 static int eq_IHASH PARAMS ((const void *, const void *));
46 static int file_cleanup PARAMS ((cpp_buffer *, cpp_reader *));
47 static int find_include_file PARAMS ((cpp_reader *, const char *,
48 struct file_name_list *,
49 IHASH **, int *));
50 static int read_include_file PARAMS ((cpp_reader *, int, IHASH *));
51 static inline int open_include_file PARAMS ((cpp_reader *, const char *));
52
53 #if 0
54 static void hack_vms_include_specification PARAMS ((char *));
55 #endif
56
57 /* Initial size of include hash table. */
58 #define IHASHSIZE 50
59
60 #ifndef INCLUDE_LEN_FUDGE
61 #define INCLUDE_LEN_FUDGE 0
62 #endif
63
64 /* Calculate hash of an IHASH entry. */
65 static unsigned int
66 hash_IHASH (x)
67 const void *x;
68 {
69 IHASH *i = (IHASH *)x;
70 unsigned int r = 0, len = 0;
71 const U_CHAR *s = i->nshort;
72
73 if (i->hash != (unsigned long)-1)
74 return i->hash;
75
76 do
77 len++, r = r * 67 + (*s++ - 113);
78 while (*s && *s != '.');
79 i->hash = r + len;
80 return r + len;
81 }
82
83 /* Compare an existing IHASH structure with a potential one. */
84 static int
85 eq_IHASH (x, y)
86 const void *x;
87 const void *y;
88 {
89 const U_CHAR *a = ((const IHASH *)x)->nshort;
90 const U_CHAR *b = ((const IHASH *)y)->nshort;
91 return !strcmp (a, b);
92 }
93
94 /* Init the hash table. In here so it can see the hash and eq functions. */
95 void
96 _cpp_init_include_hash (pfile)
97 cpp_reader *pfile;
98 {
99 pfile->all_include_files
100 = htab_create (IHASHSIZE, hash_IHASH, eq_IHASH, free);
101 }
102
103 /* Return 0 if the file pointed to by IHASH has never been included before,
104 -1 if it has been included before and need not be again,
105 or a pointer to an IHASH entry which is the file to be reread.
106 "Never before" is with respect to the position in ILIST.
107
108 This will not detect redundancies involving odd uses of the
109 `current directory' rule for "" includes. They aren't quite
110 pathological, but I think they are rare enough not to worry about.
111 The simplest example is:
112
113 top.c:
114 #include "a/a.h"
115 #include "b/b.h"
116
117 a/a.h:
118 #include "../b/b.h"
119
120 and the problem is that for `current directory' includes,
121 ihash->foundhere is not on any of the global include chains,
122 so the test below (i->foundhere == l) may be false even when
123 the directories are in fact the same. */
124
125 static IHASH *
126 redundant_include_p (pfile, ihash, ilist)
127 cpp_reader *pfile;
128 IHASH *ihash;
129 struct file_name_list *ilist;
130 {
131 struct file_name_list *l;
132 IHASH *i;
133
134 if (! ihash->foundhere)
135 return 0;
136
137 for (i = ihash; i; i = i->next_this_file)
138 for (l = ilist; l; l = l->next)
139 if (i->foundhere == l)
140 /* The control_macro works like this: If it's NULL, the file
141 is to be included again. If it's "", the file is never to
142 be included again. If it's a string, the file is not to be
143 included again if the string is the name of a defined macro. */
144 return (i->control_macro
145 && (i->control_macro[0] == '\0'
146 || cpp_defined (pfile, i->control_macro, -1)))
147 ? (IHASH *)-1 : i;
148
149 return 0;
150 }
151
152 /* Return 1 if the file named by FNAME has been included before in
153 any context, 0 otherwise. */
154 int
155 cpp_included (pfile, fname)
156 cpp_reader *pfile;
157 const char *fname;
158 {
159 IHASH dummy, *ptr;
160 dummy.nshort = fname;
161 dummy.hash = -1;
162 ptr = htab_find (pfile->all_include_files, (const void *)&dummy);
163 return (ptr != NULL);
164 }
165
166 static int
167 file_cleanup (pbuf, pfile)
168 cpp_buffer *pbuf;
169 cpp_reader *pfile;
170 {
171 if (pbuf->buf)
172 free ((PTR) pbuf->buf);
173 if (pfile->system_include_depth)
174 pfile->system_include_depth--;
175 return 0;
176 }
177
178 /* Centralize calls to open(2) here. This provides a hook for future
179 changes which might, e.g. look for and open a precompiled version
180 of the header. It also means all the magic currently associated
181 with calling open is in one place, and if we ever need more, it'll
182 be in one place too.
183
184 Open files in nonblocking mode, so we don't get stuck if someone
185 clever has asked cpp to process /dev/rmt0. read_include_file
186 will check that we have a real file to work with. Also take care
187 not to acquire a controlling terminal by mistake (this can't happen
188 on sane systems, but paranoia is a virtue).
189
190 Use the three-argument form of open even though we aren't
191 specifying O_CREAT, to defend against broken system headers. */
192
193 static inline int
194 open_include_file (pfile, filename)
195 cpp_reader *pfile ATTRIBUTE_UNUSED;
196 const char *filename;
197 {
198 return open (filename, O_RDONLY|O_NONBLOCK|O_NOCTTY, 0666);
199 }
200
201 /* Search for include file FNAME in the include chain starting at
202 SEARCH_START. Return -2 if this file doesn't need to be included
203 (because it was included already and it's marked idempotent),
204 -1 if an error occurred, or a file descriptor open on the file.
205 *IHASH is set to point to the include hash entry for this file, and
206 *BEFORE is set to 1 if the file was included before (but needs to be read
207 again). */
208 static int
209 find_include_file (pfile, fname, search_start, ihash, before)
210 cpp_reader *pfile;
211 const char *fname;
212 struct file_name_list *search_start;
213 IHASH **ihash;
214 int *before;
215 {
216 struct file_name_list *path;
217 IHASH *ih, **slot;
218 IHASH dummy;
219 int f;
220 char *name;
221
222 dummy.hash = -1;
223 dummy.nshort = fname;
224 path = (fname[0] == '/') ? ABSOLUTE_PATH : search_start;
225 slot = (IHASH **) htab_find_slot (pfile->all_include_files,
226 (const void *)&dummy, 1);
227
228 if (*slot && (ih = redundant_include_p (pfile, *slot, path)))
229 {
230 if (ih == (IHASH *)-1)
231 return -2;
232
233 *before = 1;
234 *ihash = ih;
235 return open_include_file (pfile, ih->name);
236 }
237
238 if (path == ABSOLUTE_PATH)
239 {
240 name = (char *) fname;
241 f = open_include_file (pfile, name);
242 }
243 else
244 {
245 /* Search directory path, trying to open the file. */
246 name = alloca (strlen (fname) + pfile->max_include_len
247 + 2 + INCLUDE_LEN_FUDGE);
248 do
249 {
250 memcpy (name, path->name, path->nlen);
251 name[path->nlen] = '/';
252 strcpy (&name[path->nlen+1], fname);
253 _cpp_simplify_pathname (name);
254 if (CPP_OPTIONS (pfile)->remap)
255 name = remap_filename (pfile, name, path);
256
257 f = open_include_file (pfile, name);
258 #ifdef EACCES
259 if (f == -1 && errno == EACCES)
260 {
261 cpp_error (pfile,
262 "included file `%s' exists but is not readable",
263 name);
264 return -1;
265 }
266 #endif
267 if (f >= 0)
268 break;
269 path = path->next;
270 }
271 while (path);
272 }
273 if (f == -1)
274 return -1;
275
276 if (path == ABSOLUTE_PATH)
277 {
278 ih = (IHASH *) xmalloc (sizeof (IHASH) + strlen (name));
279 ih->nshort = ih->name;
280 }
281 else
282 {
283 ih = (IHASH *) xmalloc (sizeof (IHASH) + strlen (name)
284 + strlen (fname) + 1);
285 ih->nshort = ih->name + strlen (fname) + 1;
286 strcpy ((char *)ih->nshort, fname);
287 }
288 strcpy ((char *)ih->name, name);
289 ih->foundhere = path;
290 ih->control_macro = NULL;
291 ih->hash = dummy.hash;
292
293 ih->next_this_file = *slot;
294 *slot = ih;
295
296 *before = 0;
297 *ihash = ih;
298 return f;
299 }
300
301 /* The file_name_map structure holds a mapping of file names for a
302 particular directory. This mapping is read from the file named
303 FILE_NAME_MAP_FILE in that directory. Such a file can be used to
304 map filenames on a file system with severe filename restrictions,
305 such as DOS. The format of the file name map file is just a series
306 of lines with two tokens on each line. The first token is the name
307 to map, and the second token is the actual name to use. */
308
309 struct file_name_map
310 {
311 struct file_name_map *map_next;
312 char *map_from;
313 char *map_to;
314 };
315
316 #define FILE_NAME_MAP_FILE "header.gcc"
317
318 /* Read a space delimited string of unlimited length from a stdio
319 file. */
320
321 static char *
322 read_filename_string (ch, f)
323 int ch;
324 FILE *f;
325 {
326 char *alloc, *set;
327 int len;
328
329 len = 20;
330 set = alloc = xmalloc (len + 1);
331 if (! is_space(ch))
332 {
333 *set++ = ch;
334 while ((ch = getc (f)) != EOF && ! is_space(ch))
335 {
336 if (set - alloc == len)
337 {
338 len *= 2;
339 alloc = xrealloc (alloc, len + 1);
340 set = alloc + len / 2;
341 }
342 *set++ = ch;
343 }
344 }
345 *set = '\0';
346 ungetc (ch, f);
347 return alloc;
348 }
349
350 /* This structure holds a linked list of file name maps, one per directory. */
351
352 struct file_name_map_list
353 {
354 struct file_name_map_list *map_list_next;
355 char *map_list_name;
356 struct file_name_map *map_list_map;
357 };
358
359 /* Read the file name map file for DIRNAME. */
360
361 static struct file_name_map *
362 read_name_map (pfile, dirname)
363 cpp_reader *pfile;
364 const char *dirname;
365 {
366 register struct file_name_map_list *map_list_ptr;
367 char *name;
368 FILE *f;
369
370 for (map_list_ptr = CPP_OPTIONS (pfile)->map_list; map_list_ptr;
371 map_list_ptr = map_list_ptr->map_list_next)
372 if (! strcmp (map_list_ptr->map_list_name, dirname))
373 return map_list_ptr->map_list_map;
374
375 map_list_ptr = ((struct file_name_map_list *)
376 xmalloc (sizeof (struct file_name_map_list)));
377 map_list_ptr->map_list_name = xstrdup (dirname);
378
379 name = (char *) alloca (strlen (dirname) + strlen (FILE_NAME_MAP_FILE) + 2);
380 strcpy (name, dirname);
381 if (*dirname)
382 strcat (name, "/");
383 strcat (name, FILE_NAME_MAP_FILE);
384 f = fopen (name, "r");
385 if (!f)
386 map_list_ptr->map_list_map = (struct file_name_map *)-1;
387 else
388 {
389 int ch;
390 int dirlen = strlen (dirname);
391
392 while ((ch = getc (f)) != EOF)
393 {
394 char *from, *to;
395 struct file_name_map *ptr;
396
397 if (is_space(ch))
398 continue;
399 from = read_filename_string (ch, f);
400 while ((ch = getc (f)) != EOF && is_hspace(ch))
401 ;
402 to = read_filename_string (ch, f);
403
404 ptr = ((struct file_name_map *)
405 xmalloc (sizeof (struct file_name_map)));
406 ptr->map_from = from;
407
408 /* Make the real filename absolute. */
409 if (*to == '/')
410 ptr->map_to = to;
411 else
412 {
413 ptr->map_to = xmalloc (dirlen + strlen (to) + 2);
414 strcpy (ptr->map_to, dirname);
415 ptr->map_to[dirlen] = '/';
416 strcpy (ptr->map_to + dirlen + 1, to);
417 free (to);
418 }
419
420 ptr->map_next = map_list_ptr->map_list_map;
421 map_list_ptr->map_list_map = ptr;
422
423 while ((ch = getc (f)) != '\n')
424 if (ch == EOF)
425 break;
426 }
427 fclose (f);
428 }
429
430 map_list_ptr->map_list_next = CPP_OPTIONS (pfile)->map_list;
431 CPP_OPTIONS (pfile)->map_list = map_list_ptr;
432
433 return map_list_ptr->map_list_map;
434 }
435
436 /* Remap NAME based on the file_name_map (if any) for LOC. */
437
438 static char *
439 remap_filename (pfile, name, loc)
440 cpp_reader *pfile;
441 char *name;
442 struct file_name_list *loc;
443 {
444 struct file_name_map *map;
445 const char *from, *p, *dir;
446
447 if (! loc->name_map)
448 loc->name_map = read_name_map (pfile,
449 loc->name
450 ? loc->name : ".");
451
452 if (loc->name_map == (struct file_name_map *)-1)
453 return name;
454
455 from = name + strlen (loc->name) + 1;
456
457 for (map = loc->name_map; map; map = map->map_next)
458 if (!strcmp (map->map_from, from))
459 return map->map_to;
460
461 /* Try to find a mapping file for the particular directory we are
462 looking in. Thus #include <sys/types.h> will look up sys/types.h
463 in /usr/include/header.gcc and look up types.h in
464 /usr/include/sys/header.gcc. */
465 p = strrchr (name, '/');
466 if (!p)
467 p = name;
468 if (loc && loc->name
469 && strlen (loc->name) == (size_t) (p - name)
470 && !strncmp (loc->name, name, p - name))
471 /* FILENAME is in SEARCHPTR, which we've already checked. */
472 return name;
473
474 if (p == name)
475 {
476 dir = ".";
477 from = name;
478 }
479 else
480 {
481 char * newdir = (char *) alloca (p - name + 1);
482 memcpy (newdir, name, p - name);
483 newdir[p - name] = '\0';
484 dir = newdir;
485 from = p + 1;
486 }
487
488 for (map = read_name_map (pfile, dir); map; map = map->map_next)
489 if (! strcmp (map->map_from, name))
490 return map->map_to;
491
492 return name;
493 }
494
495
496 void
497 _cpp_execute_include (pfile, fname, len, no_reinclude, search_start)
498 cpp_reader *pfile;
499 char *fname;
500 unsigned int len;
501 int no_reinclude;
502 struct file_name_list *search_start;
503 {
504 IHASH *ihash;
505 int fd;
506 int angle_brackets = fname[0] == '<';
507 int before;
508
509 if (!search_start)
510 {
511 if (angle_brackets)
512 search_start = CPP_OPTIONS (pfile)->bracket_include;
513 else if (CPP_OPTIONS (pfile)->ignore_srcdir)
514 search_start = CPP_OPTIONS (pfile)->quote_include;
515 else
516 search_start = CPP_BUFFER (pfile)->actual_dir;
517 }
518
519 if (!search_start)
520 {
521 cpp_error (pfile, "No include path in which to find %s", fname);
522 return;
523 }
524
525 /* Remove quote marks. */
526 fname++;
527 len -= 2;
528 fname[len] = '\0';
529
530 fd = find_include_file (pfile, fname, search_start, &ihash, &before);
531
532 if (fd == -2)
533 return;
534
535 if (fd == -1)
536 {
537 if (CPP_OPTIONS (pfile)->print_deps_missing_files
538 && CPP_PRINT_DEPS (pfile) > (angle_brackets ||
539 (pfile->system_include_depth > 0)))
540 {
541 if (!angle_brackets)
542 deps_add_dep (pfile->deps, fname);
543 else
544 {
545 char *p;
546 struct file_name_list *ptr;
547 /* If requested as a system header, assume it belongs in
548 the first system header directory. */
549 if (CPP_OPTIONS (pfile)->bracket_include)
550 ptr = CPP_OPTIONS (pfile)->bracket_include;
551 else
552 ptr = CPP_OPTIONS (pfile)->quote_include;
553
554 p = (char *) alloca (strlen (ptr->name)
555 + strlen (fname) + 2);
556 if (*ptr->name != '\0')
557 {
558 strcpy (p, ptr->name);
559 strcat (p, "/");
560 }
561 strcat (p, fname);
562 deps_add_dep (pfile->deps, p);
563 }
564 }
565 /* If -M was specified, and this header file won't be added to
566 the dependency list, then don't count this as an error,
567 because we can still produce correct output. Otherwise, we
568 can't produce correct output, because there may be
569 dependencies we need inside the missing file, and we don't
570 know what directory this missing file exists in. */
571 else if (CPP_PRINT_DEPS (pfile)
572 && (CPP_PRINT_DEPS (pfile)
573 <= (angle_brackets || (pfile->system_include_depth > 0))))
574 cpp_warning (pfile, "No include path in which to find %s", fname);
575 else
576 cpp_error_from_errno (pfile, fname);
577
578 return;
579 }
580
581 /* For -M, add the file to the dependencies on its first inclusion. */
582 if (!before && (CPP_PRINT_DEPS (pfile)
583 > (angle_brackets || (pfile->system_include_depth > 0))))
584 deps_add_dep (pfile->deps, ihash->name);
585
586 /* Handle -H option. */
587 if (CPP_OPTIONS(pfile)->print_include_names)
588 {
589 cpp_buffer *fp = CPP_BUFFER (pfile);
590 while ((fp = CPP_PREV_BUFFER (fp)) != NULL)
591 putc ('.', stderr);
592 fprintf (stderr, " %s\n", ihash->name);
593 }
594
595 /* Actually process the file */
596
597 if (no_reinclude)
598 ihash->control_macro = (const U_CHAR *) "";
599
600 if (read_include_file (pfile, fd, ihash))
601 {
602 _cpp_output_line_command (pfile, enter_file);
603 if (angle_brackets)
604 pfile->system_include_depth++; /* Decremented in file_cleanup. */
605 }
606 }
607
608
609 /* Push an input buffer and load it up with the contents of FNAME.
610 If FNAME is "" or NULL, read standard input. */
611 int
612 cpp_read_file (pfile, fname)
613 cpp_reader *pfile;
614 const char *fname;
615 {
616 IHASH *ih, **slot;
617 IHASH dummy;
618 int f;
619
620 if (fname == NULL)
621 fname = "";
622
623 dummy.hash = -1;
624 dummy.nshort = fname;
625 slot = (IHASH **) htab_find_slot (pfile->all_include_files,
626 (const void *) &dummy, 1);
627 if (*slot && (ih = redundant_include_p (pfile, *slot, ABSOLUTE_PATH)))
628 {
629 if (ih == (IHASH *)-1)
630 return 1; /* Already included. */
631 }
632 else
633 {
634 ih = (IHASH *) xmalloc (sizeof (IHASH) + strlen (fname));
635 ih->control_macro = 0;
636 ih->foundhere = ABSOLUTE_PATH; /* well sort of ... */
637 ih->hash = dummy.hash;
638 strcpy ((char *)ih->name, fname);
639 ih->nshort = ih->name;
640
641 ih->next_this_file = *slot;
642 *slot = ih;
643 }
644
645 if (*fname == '\0')
646 f = 0;
647 else
648 f = open_include_file (pfile, fname);
649
650 return read_include_file (pfile, f, ih);
651 }
652
653 /* Read the contents of FD into the buffer on the top of PFILE's stack.
654 IHASH points to the include hash entry for the file associated with
655 FD.
656
657 The caller is responsible for the cpp_push_buffer. */
658
659 static int
660 read_include_file (pfile, fd, ihash)
661 cpp_reader *pfile;
662 int fd;
663 IHASH *ihash;
664 {
665 struct stat st;
666 size_t st_size;
667 long length;
668 cpp_buffer *fp;
669
670 fp = cpp_push_buffer (pfile, NULL, 0);
671
672 if (fp == 0)
673 goto push_fail;
674
675 if (fstat (fd, &st) < 0)
676 goto perror_fail;
677 if (fcntl (fd, F_SETFL, 0) == -1) /* turn off nonblocking mode */
678 goto perror_fail;
679
680 /* If fd points to a plain file, we know how big it is, so we can
681 allocate the buffer all at once. If fd is a pipe or terminal, we
682 can't. Most C source files are 4k or less, so we guess that. If
683 fd is something weird, like a block device or a directory, we
684 don't want to read it at all.
685
686 Unfortunately, different systems use different st.st_mode values
687 for pipes: some have S_ISFIFO, some S_ISSOCK, some are buggy and
688 zero the entire struct stat except a couple fields. Hence the
689 mess below.
690
691 In all cases, read_and_prescan will resize the buffer if it
692 turns out there's more data than we thought. */
693
694 if (S_ISREG (st.st_mode))
695 {
696 /* off_t might have a wider range than size_t - in other words,
697 the max size of a file might be bigger than the address
698 space. We can't handle a file that large. (Anyone with
699 a single source file bigger than 4GB needs to rethink
700 their coding style.) */
701 st_size = (size_t) st.st_size;
702 if ((unsigned HOST_WIDEST_INT) st_size
703 != (unsigned HOST_WIDEST_INT) st.st_size)
704 {
705 cpp_error (pfile, "file `%s' is too large", ihash->name);
706 goto fail;
707 }
708 }
709 else if (S_ISFIFO (st.st_mode) || S_ISSOCK (st.st_mode)
710 /* Permit any kind of character device: the sensible ones are
711 ttys and /dev/null, but weeding out the others is too hard. */
712 || S_ISCHR (st.st_mode)
713 /* Some 4.x (x<4) derivatives have a bug that makes fstat() of a
714 socket or pipe return a stat struct with most fields zeroed. */
715 || (st.st_mode == 0 && st.st_nlink == 0 && st.st_size == 0))
716 {
717 /* Cannot get its file size before reading. 4k is a decent
718 first guess. */
719 st_size = 4096;
720 }
721 else
722 {
723 cpp_error (pfile, "`%s' is not a file, pipe, or tty", ihash->name);
724 goto fail;
725 }
726
727 /* Read the file, converting end-of-line characters and trigraphs
728 (if enabled). */
729 fp->ihash = ihash;
730 fp->nominal_fname = ihash->name;
731 length = _cpp_read_and_prescan (pfile, fp, fd, st_size);
732 if (length < 0)
733 goto fail;
734 if (length == 0)
735 ihash->control_macro = (const U_CHAR *) ""; /* never re-include */
736
737 close (fd);
738 fp->rlimit = fp->alimit = fp->buf + length;
739 fp->cur = fp->buf;
740 if (ihash->foundhere != ABSOLUTE_PATH)
741 fp->system_header_p = ihash->foundhere->sysp;
742 fp->lineno = 1;
743 fp->colno = 1;
744 fp->line_base = fp->buf;
745 fp->cleanup = file_cleanup;
746
747 /* The ->actual_dir field is only used when ignore_srcdir is not in effect;
748 see do_include */
749 if (!CPP_OPTIONS (pfile)->ignore_srcdir)
750 fp->actual_dir = actual_directory (pfile, ihash->name);
751
752 pfile->input_stack_listing_current = 0;
753 pfile->only_seen_white = 2;
754 return 1;
755
756 perror_fail:
757 cpp_error_from_errno (pfile, ihash->name);
758 fail:
759 cpp_pop_buffer (pfile);
760 push_fail:
761 close (fd);
762 return 0;
763 }
764
765 /* Given a path FNAME, extract the directory component and place it
766 onto the actual_dirs list. Return a pointer to the allocated
767 file_name_list structure. These structures are used to implement
768 current-directory "" include searching. */
769
770 static struct file_name_list *
771 actual_directory (pfile, fname)
772 cpp_reader *pfile;
773 const char *fname;
774 {
775 char *last_slash, *dir;
776 size_t dlen;
777 struct file_name_list *x;
778
779 dir = xstrdup (fname);
780 last_slash = strrchr (dir, '/');
781 if (last_slash)
782 {
783 if (last_slash == dir)
784 {
785 dlen = 1;
786 last_slash[1] = '\0';
787 }
788 else
789 {
790 dlen = last_slash - dir;
791 *last_slash = '\0';
792 }
793 }
794 else
795 {
796 dir[0] = '.';
797 dir[1] = '\0';
798 dlen = 1;
799 }
800
801 if (dlen > pfile->max_include_len)
802 pfile->max_include_len = dlen;
803
804 for (x = pfile->actual_dirs; x; x = x->alloc)
805 if (!strcmp (x->name, dir))
806 {
807 free (dir);
808 return x;
809 }
810
811 /* Not found, make a new one. */
812 x = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
813 x->name = dir;
814 x->nlen = dlen;
815 x->next = CPP_OPTIONS (pfile)->quote_include;
816 x->alloc = pfile->actual_dirs;
817 x->sysp = CPP_BUFFER (pfile)->system_header_p;
818 x->name_map = NULL;
819
820 pfile->actual_dirs = x;
821 return x;
822 }
823
824 /* Simplify a path name in place, deleting redundant components. This
825 reduces OS overhead and guarantees that equivalent paths compare
826 the same (modulo symlinks).
827
828 Transforms made:
829 foo/bar/../quux foo/quux
830 foo/./bar foo/bar
831 foo//bar foo/bar
832 /../quux /quux
833 //quux //quux (POSIX allows leading // as a namespace escape)
834
835 Guarantees no trailing slashes. All transforms reduce the length
836 of the string.
837 */
838 void
839 _cpp_simplify_pathname (path)
840 char *path;
841 {
842 char *from, *to;
843 char *base;
844 int absolute = 0;
845
846 #if defined (HAVE_DOS_BASED_FILE_SYSTEM)
847 /* Convert all backslashes to slashes. */
848 for (from = path; *from; from++)
849 if (*from == '\\') *from = '/';
850
851 /* Skip over leading drive letter if present. */
852 if (ISALPHA (path[0]) && path[1] == ':')
853 from = to = &path[2];
854 else
855 from = to = path;
856 #else
857 from = to = path;
858 #endif
859
860 /* Remove redundant initial /s. */
861 if (*from == '/')
862 {
863 absolute = 1;
864 to++;
865 from++;
866 if (*from == '/')
867 {
868 if (*++from == '/')
869 /* 3 or more initial /s are equivalent to 1 /. */
870 while (*++from == '/');
871 else
872 /* On some hosts // differs from /; Posix allows this. */
873 to++;
874 }
875 }
876 base = to;
877
878 for (;;)
879 {
880 while (*from == '/')
881 from++;
882
883 if (from[0] == '.' && from[1] == '/')
884 from += 2;
885 else if (from[0] == '.' && from[1] == '\0')
886 goto done;
887 else if (from[0] == '.' && from[1] == '.' && from[2] == '/')
888 {
889 if (base == to)
890 {
891 if (absolute)
892 from += 3;
893 else
894 {
895 *to++ = *from++;
896 *to++ = *from++;
897 *to++ = *from++;
898 base = to;
899 }
900 }
901 else
902 {
903 to -= 2;
904 while (to > base && *to != '/') to--;
905 if (*to == '/')
906 to++;
907 from += 3;
908 }
909 }
910 else if (from[0] == '.' && from[1] == '.' && from[2] == '\0')
911 {
912 if (base == to)
913 {
914 if (!absolute)
915 {
916 *to++ = *from++;
917 *to++ = *from++;
918 }
919 }
920 else
921 {
922 to -= 2;
923 while (to > base && *to != '/') to--;
924 if (*to == '/')
925 to++;
926 }
927 goto done;
928 }
929 else
930 /* Copy this component and trailing /, if any. */
931 while ((*to++ = *from++) != '/')
932 {
933 if (!to[-1])
934 {
935 to--;
936 goto done;
937 }
938 }
939
940 }
941
942 done:
943 /* Trim trailing slash */
944 if (to[0] == '/' && (!absolute || to > path+1))
945 to--;
946
947 /* Change the empty string to "." so that stat() on the result
948 will always work. */
949 if (to == path)
950 *to++ = '.';
951
952 *to = '\0';
953
954 return;
955 }
956
957 /* It is not clear when this should be used if at all, so I've
958 disabled it until someone who understands VMS can look at it. */
959 #if 0
960
961 /* Under VMS we need to fix up the "include" specification filename.
962
963 Rules for possible conversions
964
965 fullname tried paths
966
967 name name
968 ./dir/name [.dir]name
969 /dir/name dir:name
970 /name [000000]name, name
971 dir/name dir:[000000]name, dir:name, dir/name
972 dir1/dir2/name dir1:[dir2]name, dir1:[000000.dir2]name
973 path:/name path:[000000]name, path:name
974 path:/dir/name path:[000000.dir]name, path:[dir]name
975 path:dir/name path:[dir]name
976 [path]:[dir]name [path.dir]name
977 path/[dir]name [path.dir]name
978
979 The path:/name input is constructed when expanding <> includes. */
980
981
982 static void
983 hack_vms_include_specification (fullname)
984 char *fullname;
985 {
986 register char *basename, *unixname, *local_ptr, *first_slash;
987 int f, check_filename_before_returning, must_revert;
988 char Local[512];
989
990 check_filename_before_returning = 0;
991 must_revert = 0;
992 /* See if we can find a 1st slash. If not, there's no path information. */
993 first_slash = strchr (fullname, '/');
994 if (first_slash == 0)
995 return 0; /* Nothing to do!!! */
996
997 /* construct device spec if none given. */
998
999 if (strchr (fullname, ':') == 0)
1000 {
1001
1002 /* If fullname has a slash, take it as device spec. */
1003
1004 if (first_slash == fullname)
1005 {
1006 first_slash = strchr (fullname + 1, '/'); /* 2nd slash ? */
1007 if (first_slash)
1008 *first_slash = ':'; /* make device spec */
1009 for (basename = fullname; *basename != 0; basename++)
1010 *basename = *(basename+1); /* remove leading slash */
1011 }
1012 else if ((first_slash[-1] != '.') /* keep ':/', './' */
1013 && (first_slash[-1] != ':')
1014 && (first_slash[-1] != ']')) /* or a vms path */
1015 {
1016 *first_slash = ':';
1017 }
1018 else if ((first_slash[1] == '[') /* skip './' in './[dir' */
1019 && (first_slash[-1] == '.'))
1020 fullname += 2;
1021 }
1022
1023 /* Get part after first ':' (basename[-1] == ':')
1024 or last '/' (basename[-1] == '/'). */
1025
1026 basename = base_name (fullname);
1027
1028 local_ptr = Local; /* initialize */
1029
1030 /* We are trying to do a number of things here. First of all, we are
1031 trying to hammer the filenames into a standard format, such that later
1032 processing can handle them.
1033
1034 If the file name contains something like [dir.], then it recognizes this
1035 as a root, and strips the ".]". Later processing will add whatever is
1036 needed to get things working properly.
1037
1038 If no device is specified, then the first directory name is taken to be
1039 a device name (or a rooted logical). */
1040
1041 /* Point to the UNIX filename part (which needs to be fixed!)
1042 but skip vms path information.
1043 [basename != fullname since first_slash != 0]. */
1044
1045 if ((basename[-1] == ':') /* vms path spec. */
1046 || (basename[-1] == ']')
1047 || (basename[-1] == '>'))
1048 unixname = basename;
1049 else
1050 unixname = fullname;
1051
1052 if (*unixname == '/')
1053 unixname++;
1054
1055 /* If the directory spec is not rooted, we can just copy
1056 the UNIX filename part and we are done. */
1057
1058 if (((basename - fullname) > 1)
1059 && ( (basename[-1] == ']')
1060 || (basename[-1] == '>')))
1061 {
1062 if (basename[-2] != '.')
1063 {
1064
1065 /* The VMS part ends in a `]', and the preceding character is not a `.'.
1066 -> PATH]:/name (basename = '/name', unixname = 'name')
1067 We strip the `]', and then splice the two parts of the name in the
1068 usual way. Given the default locations for include files in cccp.c,
1069 we will only use this code if the user specifies alternate locations
1070 with the /include (-I) switch on the command line. */
1071
1072 basename -= 1; /* Strip "]" */
1073 unixname--; /* backspace */
1074 }
1075 else
1076 {
1077
1078 /* The VMS part has a ".]" at the end, and this will not do. Later
1079 processing will add a second directory spec, and this would be a syntax
1080 error. Thus we strip the ".]", and thus merge the directory specs.
1081 We also backspace unixname, so that it points to a '/'. This inhibits the
1082 generation of the 000000 root directory spec (which does not belong here
1083 in this case). */
1084
1085 basename -= 2; /* Strip ".]" */
1086 unixname--; /* backspace */
1087 }
1088 }
1089
1090 else
1091
1092 {
1093
1094 /* We drop in here if there is no VMS style directory specification yet.
1095 If there is no device specification either, we make the first dir a
1096 device and try that. If we do not do this, then we will be essentially
1097 searching the users default directory (as if they did a #include "asdf.h").
1098
1099 Then all we need to do is to push a '[' into the output string. Later
1100 processing will fill this in, and close the bracket. */
1101
1102 if ((unixname != fullname) /* vms path spec found. */
1103 && (basename[-1] != ':'))
1104 *local_ptr++ = ':'; /* dev not in spec. take first dir */
1105
1106 *local_ptr++ = '['; /* Open the directory specification */
1107 }
1108
1109 if (unixname == fullname) /* no vms dir spec. */
1110 {
1111 must_revert = 1;
1112 if ((first_slash != 0) /* unix dir spec. */
1113 && (*unixname != '/') /* not beginning with '/' */
1114 && (*unixname != '.')) /* or './' or '../' */
1115 *local_ptr++ = '.'; /* dir is local ! */
1116 }
1117
1118 /* at this point we assume that we have the device spec, and (at least
1119 the opening "[" for a directory specification. We may have directories
1120 specified already.
1121
1122 If there are no other slashes then the filename will be
1123 in the "root" directory. Otherwise, we need to add
1124 directory specifications. */
1125
1126 if (strchr (unixname, '/') == 0)
1127 {
1128 /* if no directories specified yet and none are following. */
1129 if (local_ptr[-1] == '[')
1130 {
1131 /* Just add "000000]" as the directory string */
1132 strcpy (local_ptr, "000000]");
1133 local_ptr += strlen (local_ptr);
1134 check_filename_before_returning = 1; /* we might need to fool with this later */
1135 }
1136 }
1137 else
1138 {
1139
1140 /* As long as there are still subdirectories to add, do them. */
1141 while (strchr (unixname, '/') != 0)
1142 {
1143 /* If this token is "." we can ignore it
1144 if it's not at the beginning of a path. */
1145 if ((unixname[0] == '.') && (unixname[1] == '/'))
1146 {
1147 /* remove it at beginning of path. */
1148 if ( ((unixname == fullname) /* no device spec */
1149 && (fullname+2 != basename)) /* starts with ./ */
1150 /* or */
1151 || ((basename[-1] == ':') /* device spec */
1152 && (unixname-1 == basename))) /* and ./ afterwards */
1153 *local_ptr++ = '.'; /* make '[.' start of path. */
1154 unixname += 2;
1155 continue;
1156 }
1157
1158 /* Add a subdirectory spec. Do not duplicate "." */
1159 if ( local_ptr[-1] != '.'
1160 && local_ptr[-1] != '['
1161 && local_ptr[-1] != '<')
1162 *local_ptr++ = '.';
1163
1164 /* If this is ".." then the spec becomes "-" */
1165 if ( (unixname[0] == '.')
1166 && (unixname[1] == '.')
1167 && (unixname[2] == '/'))
1168 {
1169 /* Add "-" and skip the ".." */
1170 if ((local_ptr[-1] == '.')
1171 && (local_ptr[-2] == '['))
1172 local_ptr--; /* prevent [.- */
1173 *local_ptr++ = '-';
1174 unixname += 3;
1175 continue;
1176 }
1177
1178 /* Copy the subdirectory */
1179 while (*unixname != '/')
1180 *local_ptr++= *unixname++;
1181
1182 unixname++; /* Skip the "/" */
1183 }
1184
1185 /* Close the directory specification */
1186 if (local_ptr[-1] == '.') /* no trailing periods */
1187 local_ptr--;
1188
1189 if (local_ptr[-1] == '[') /* no dir needed */
1190 local_ptr--;
1191 else
1192 *local_ptr++ = ']';
1193 }
1194
1195 /* Now add the filename. */
1196
1197 while (*unixname)
1198 *local_ptr++ = *unixname++;
1199 *local_ptr = 0;
1200
1201 /* Now append it to the original VMS spec. */
1202
1203 strcpy ((must_revert==1)?fullname:basename, Local);
1204
1205 /* If we put a [000000] in the filename, try to open it first. If this fails,
1206 remove the [000000], and return that name. This provides flexibility
1207 to the user in that they can use both rooted and non-rooted logical names
1208 to point to the location of the file. */
1209
1210 if (check_filename_before_returning)
1211 {
1212 f = open (fullname, O_RDONLY|O_NONBLOCK);
1213 if (f >= 0)
1214 {
1215 /* The file name is OK as it is, so return it as is. */
1216 close (f);
1217 return 1;
1218 }
1219
1220 /* The filename did not work. Try to remove the [000000] from the name,
1221 and return it. */
1222
1223 basename = strchr (fullname, '[');
1224 local_ptr = strchr (fullname, ']') + 1;
1225 strcpy (basename, local_ptr); /* this gets rid of it */
1226
1227 }
1228
1229 return 1;
1230 }
1231 #endif /* VMS */