]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/cppfiles.c
cppfiles.c (INO_T_EQ): Handle UWIN.
[thirdparty/gcc.git] / gcc / cppfiles.c
1 /* Part of CPP library. (include file handling)
2 Copyright (C) 1986, 87, 89, 92 - 95, 98, 1999 Free Software Foundation, Inc.
3 Written by Per Bothner, 1994.
4 Based on CCCP program by Paul Rubin, June 1986
5 Adapted to ANSI C, Richard Stallman, Jan 1987
6 Split out of cpplib.c, Zack Weinberg, Oct 1998
7
8 This program is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by the
10 Free Software Foundation; either version 2, or (at your option) any
11 later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21
22 In other words, you are welcome to use, share and improve this program.
23 You are forbidden to forbid anyone else to use, share and improve
24 what you give them. Help stamp out software-hoarding! */
25
26 #include "config.h"
27 #include "system.h"
28 #include "cpplib.h"
29
30 /* The entry points to this file are: find_include_file, finclude,
31 include_hash, append_include_chain, deps_output, and file_cleanup.
32 file_cleanup is only called through CPP_BUFFER(pfile)->cleanup,
33 so it's static anyway. */
34
35 static struct include_hash *redundant_include_p
36 PROTO ((cpp_reader *,
37 struct include_hash *,
38 struct file_name_list *));
39 static struct file_name_map *read_name_map PROTO ((cpp_reader *,
40 const char *));
41 static char *read_filename_string PROTO ((int, FILE *));
42 static char *remap_filename PROTO ((cpp_reader *, char *,
43 struct file_name_list *));
44 static long read_and_prescan PROTO ((cpp_reader *, cpp_buffer *,
45 int, size_t));
46 static void simplify_pathname PROTO ((char *));
47 static struct file_name_list *actual_directory PROTO ((cpp_reader *, char *));
48
49 #if 0
50 static void hack_vms_include_specification PROTO ((char *));
51 #endif
52
53 /* Windows does not natively support inodes, and neither does MSDOS.
54 VMS has non-numeric inodes. */
55 #ifdef VMS
56 #define INO_T_EQ(a, b) (!bcmp((char *) &(a), (char *) &(b), sizeof (a)))
57 #elif (defined _WIN32 && !defined CYGWIN && ! defined (_UWIN)) \
58 || defined __MSDOS__
59 #define INO_T_EQ(a, b) 0
60 #else
61 #define INO_T_EQ(a, b) ((a) == (b))
62 #endif
63
64 /* Append an entry for dir DIR to list LIST, simplifying it if
65 possible. SYS says whether this is a system include directory.
66 *** DIR is modified in place. It must be writable and permanently
67 allocated. LIST is a pointer to the head pointer, because we actually
68 *prepend* the dir, and reverse the list later (in merge_include_chains). */
69 void
70 append_include_chain (pfile, list, dir, sysp)
71 cpp_reader *pfile;
72 struct file_name_list **list;
73 const char *dir;
74 int sysp;
75 {
76 struct file_name_list *new;
77 struct stat st;
78 unsigned int len;
79 char * newdir = xstrdup (dir);
80
81 simplify_pathname (newdir);
82 if (stat (newdir, &st))
83 {
84 /* Dirs that don't exist are silently ignored. */
85 if (errno != ENOENT)
86 cpp_perror_with_name (pfile, newdir);
87 return;
88 }
89
90 if (!S_ISDIR (st.st_mode))
91 {
92 cpp_message (pfile, 1, "%s: %s: Not a directory", progname, newdir);
93 return;
94 }
95
96 len = strlen(newdir);
97 if (len > pfile->max_include_len)
98 pfile->max_include_len = len;
99
100 new = (struct file_name_list *)xmalloc (sizeof (struct file_name_list));
101 new->name = newdir;
102 new->nlen = len;
103 new->next = *list;
104 new->ino = st.st_ino;
105 new->dev = st.st_dev;
106 new->sysp = sysp;
107 new->name_map = NULL;
108
109 *list = new;
110 }
111
112 /* Merge the four include chains together in the order quote, bracket,
113 system, after. Remove duplicate dirs (as determined by
114 INO_T_EQ()). The system_include and after_include chains are never
115 referred to again after this function; all access is through the
116 bracket_include path.
117
118 For the future: Check if the directory is empty (but
119 how?) and possibly preload the include hash. */
120
121 void
122 merge_include_chains (opts)
123 struct cpp_options *opts;
124 {
125 struct file_name_list *prev, *next, *cur, *other;
126 struct file_name_list *quote, *brack, *systm, *after;
127 struct file_name_list *qtail, *btail, *stail, *atail;
128
129 qtail = opts->quote_include;
130 btail = opts->bracket_include;
131 stail = opts->system_include;
132 atail = opts->after_include;
133
134 /* Nreverse the four lists. */
135 prev = 0;
136 for (cur = qtail; cur; cur = next)
137 {
138 next = cur->next;
139 cur->next = prev;
140 prev = cur;
141 }
142 quote = prev;
143
144 prev = 0;
145 for (cur = btail; cur; cur = next)
146 {
147 next = cur->next;
148 cur->next = prev;
149 prev = cur;
150 }
151 brack = prev;
152
153 prev = 0;
154 for (cur = stail; cur; cur = next)
155 {
156 next = cur->next;
157 cur->next = prev;
158 prev = cur;
159 }
160 systm = prev;
161
162 prev = 0;
163 for (cur = atail; cur; cur = next)
164 {
165 next = cur->next;
166 cur->next = prev;
167 prev = cur;
168 }
169 after = prev;
170
171 /* Paste together bracket, system, and after include chains. */
172 if (stail)
173 stail->next = after;
174 else
175 systm = after;
176 if (btail)
177 btail->next = systm;
178 else
179 brack = systm;
180
181 /* This is a bit tricky.
182 First we drop dupes from the quote-include list.
183 Then we drop dupes from the bracket-include list.
184 Finally, if qtail and brack are the same directory,
185 we cut out qtail.
186
187 We can't just merge the lists and then uniquify them because
188 then we may lose directories from the <> search path that should
189 be there; consider -Ifoo -Ibar -I- -Ifoo -Iquux. It is however
190 safe to treat -Ibar -Ifoo -I- -Ifoo -Iquux as if written
191 -Ibar -I- -Ifoo -Iquux. */
192
193 for (cur = quote; cur; cur = cur->next)
194 {
195 for (other = quote; other != cur; other = other->next)
196 if (INO_T_EQ (cur->ino, other->ino)
197 && cur->dev == other->dev)
198 {
199 prev->next = cur->next;
200 free (cur->name);
201 free (cur);
202 cur = prev;
203 break;
204 }
205 prev = cur;
206 }
207 qtail = prev;
208
209 for (cur = brack; cur; cur = cur->next)
210 {
211 for (other = brack; other != cur; other = other->next)
212 if (INO_T_EQ (cur->ino, other->ino)
213 && cur->dev == other->dev)
214 {
215 prev->next = cur->next;
216 free (cur->name);
217 free (cur);
218 cur = prev;
219 break;
220 }
221 prev = cur;
222 }
223
224 if (quote)
225 {
226 if (INO_T_EQ (qtail->ino, brack->ino) && qtail->dev == brack->dev)
227 {
228 if (quote == qtail)
229 {
230 free (quote->name);
231 free (quote);
232 quote = brack;
233 }
234 else
235 {
236 cur = quote;
237 while (cur->next != qtail)
238 cur = cur->next;
239 cur->next = brack;
240 free (qtail->name);
241 free (qtail);
242 }
243 }
244 else
245 qtail->next = brack;
246 }
247 else
248 quote = brack;
249
250 opts->quote_include = quote;
251 opts->bracket_include = brack;
252 opts->system_include = NULL;
253 opts->after_include = NULL;
254 }
255
256 /* Look up or add an entry to the table of all includes. This table
257 is indexed by the name as it appears in the #include line. The
258 ->next_this_file chain stores all different files with the same
259 #include name (there are at least three ways this can happen). The
260 hash function could probably be improved a bit. */
261
262 struct include_hash *
263 include_hash (pfile, fname, add)
264 cpp_reader *pfile;
265 char *fname;
266 int add;
267 {
268 unsigned int hash = 0;
269 struct include_hash *l, *m;
270 char *f = fname;
271
272 while (*f)
273 hash += *f++;
274
275 l = pfile->all_include_files[hash % ALL_INCLUDE_HASHSIZE];
276 m = 0;
277 for (; l; m = l, l = l->next)
278 if (!strcmp (l->nshort, fname))
279 return l;
280
281 if (!add)
282 return 0;
283
284 l = (struct include_hash *) xmalloc (sizeof (struct include_hash));
285 l->next = NULL;
286 l->next_this_file = NULL;
287 l->foundhere = NULL;
288 l->buf = NULL;
289 l->limit = NULL;
290 if (m)
291 m->next = l;
292 else
293 pfile->all_include_files[hash % ALL_INCLUDE_HASHSIZE] = l;
294
295 return l;
296 }
297
298 /* Return 0 if the file pointed to by IHASH has never been included before,
299 -1 if it has been included before and need not be again,
300 or a pointer to an IHASH entry which is the file to be reread.
301 "Never before" is with respect to the position in ILIST.
302
303 This will not detect redundancies involving odd uses of the
304 `current directory' rule for "" includes. They aren't quite
305 pathological, but I think they are rare enough not to worry about.
306 The simplest example is:
307
308 top.c:
309 #include "a/a.h"
310 #include "b/b.h"
311
312 a/a.h:
313 #include "../b/b.h"
314
315 and the problem is that for `current directory' includes,
316 ihash->foundhere is not on any of the global include chains,
317 so the test below (i->foundhere == l) may be false even when
318 the directories are in fact the same. */
319
320 static struct include_hash *
321 redundant_include_p (pfile, ihash, ilist)
322 cpp_reader *pfile;
323 struct include_hash *ihash;
324 struct file_name_list *ilist;
325 {
326 struct file_name_list *l;
327 struct include_hash *i;
328
329 if (! ihash->foundhere)
330 return 0;
331
332 for (i = ihash; i; i = i->next_this_file)
333 for (l = ilist; l; l = l->next)
334 if (i->foundhere == l)
335 /* The control_macro works like this: If it's NULL, the file
336 is to be included again. If it's "", the file is never to
337 be included again. If it's a string, the file is not to be
338 included again if the string is the name of a defined macro. */
339 return (i->control_macro
340 && (i->control_macro[0] == '\0'
341 || cpp_lookup (pfile, i->control_macro, -1, -1)))
342 ? (struct include_hash *)-1 : i;
343
344 return 0;
345 }
346
347 static int
348 file_cleanup (pbuf, pfile)
349 cpp_buffer *pbuf;
350 cpp_reader *pfile;
351 {
352 if (pbuf->buf)
353 {
354 free (pbuf->buf);
355 pbuf->buf = 0;
356 }
357 if (pfile->system_include_depth)
358 pfile->system_include_depth--;
359 return 0;
360 }
361
362 /* Search for include file FNAME in the include chain starting at
363 SEARCH_START. Return -2 if this file doesn't need to be included
364 (because it was included already and it's marked idempotent),
365 -1 if an error occurred, or a file descriptor open on the file.
366 *IHASH is set to point to the include hash entry for this file, and
367 *BEFORE is 1 if the file was included before (but needs to be read
368 again). */
369 int
370 find_include_file (pfile, fname, search_start, ihash, before)
371 cpp_reader *pfile;
372 char *fname;
373 struct file_name_list *search_start;
374 struct include_hash **ihash;
375 int *before;
376 {
377 struct file_name_list *l;
378 struct include_hash *ih, *jh;
379 int f, len;
380 char *name;
381
382 ih = include_hash (pfile, fname, 1);
383 jh = redundant_include_p (pfile, ih,
384 fname[0] == '/' ? ABSOLUTE_PATH : search_start);
385
386 if (jh != 0)
387 {
388 *before = 1;
389 *ihash = jh;
390
391 if (jh == (struct include_hash *)-1)
392 return -2;
393 else
394 return open (jh->name, O_RDONLY, 0666);
395 }
396
397 if (ih->foundhere)
398 /* A file is already known by this name, but it's not the same file.
399 Allocate another include_hash block and add it to the next_this_file
400 chain. */
401 {
402 jh = (struct include_hash *)xmalloc (sizeof (struct include_hash));
403 while (ih->next_this_file) ih = ih->next_this_file;
404
405 ih->next_this_file = jh;
406 jh = ih;
407 ih = ih->next_this_file;
408
409 ih->next = NULL;
410 ih->next_this_file = NULL;
411 ih->buf = NULL;
412 ih->limit = NULL;
413 }
414 *before = 0;
415 *ihash = ih;
416 ih->nshort = xstrdup (fname);
417 ih->control_macro = NULL;
418
419 /* If the pathname is absolute, just open it. */
420 if (fname[0] == '/')
421 {
422 ih->foundhere = ABSOLUTE_PATH;
423 ih->name = ih->nshort;
424 return open (ih->name, O_RDONLY, 0666);
425 }
426
427 /* Search directory path, trying to open the file. */
428
429 len = strlen (fname);
430 name = xmalloc (len + pfile->max_include_len + 2 + INCLUDE_LEN_FUDGE);
431
432 for (l = search_start; l; l = l->next)
433 {
434 bcopy (l->name, name, l->nlen);
435 name[l->nlen] = '/';
436 strcpy (&name[l->nlen+1], fname);
437 simplify_pathname (name);
438 if (CPP_OPTIONS (pfile)->remap)
439 name = remap_filename (pfile, name, l);
440
441 f = open (name, O_RDONLY|O_NONBLOCK|O_NOCTTY, 0666);
442 #ifdef EACCES
443 if (f == -1 && errno == EACCES)
444 {
445 cpp_error(pfile, "included file `%s' exists but is not readable",
446 name);
447 return -1;
448 }
449 #endif
450
451 if (f >= 0)
452 {
453 ih->foundhere = l;
454 ih->name = xrealloc (name, strlen (name)+1);
455 return f;
456 }
457 }
458
459 if (jh)
460 {
461 jh->next_this_file = NULL;
462 free (ih);
463 }
464 free (name);
465 *ihash = (struct include_hash *)-1;
466 return -1;
467 }
468
469 /* The file_name_map structure holds a mapping of file names for a
470 particular directory. This mapping is read from the file named
471 FILE_NAME_MAP_FILE in that directory. Such a file can be used to
472 map filenames on a file system with severe filename restrictions,
473 such as DOS. The format of the file name map file is just a series
474 of lines with two tokens on each line. The first token is the name
475 to map, and the second token is the actual name to use. */
476
477 struct file_name_map
478 {
479 struct file_name_map *map_next;
480 char *map_from;
481 char *map_to;
482 };
483
484 #define FILE_NAME_MAP_FILE "header.gcc"
485
486 /* Read a space delimited string of unlimited length from a stdio
487 file. */
488
489 static char *
490 read_filename_string (ch, f)
491 int ch;
492 FILE *f;
493 {
494 char *alloc, *set;
495 int len;
496
497 len = 20;
498 set = alloc = xmalloc (len + 1);
499 if (! is_space[ch])
500 {
501 *set++ = ch;
502 while ((ch = getc (f)) != EOF && ! is_space[ch])
503 {
504 if (set - alloc == len)
505 {
506 len *= 2;
507 alloc = xrealloc (alloc, len + 1);
508 set = alloc + len / 2;
509 }
510 *set++ = ch;
511 }
512 }
513 *set = '\0';
514 ungetc (ch, f);
515 return alloc;
516 }
517
518 /* This structure holds a linked list of file name maps, one per directory. */
519
520 struct file_name_map_list
521 {
522 struct file_name_map_list *map_list_next;
523 char *map_list_name;
524 struct file_name_map *map_list_map;
525 };
526
527 /* Read the file name map file for DIRNAME. */
528
529 static struct file_name_map *
530 read_name_map (pfile, dirname)
531 cpp_reader *pfile;
532 const char *dirname;
533 {
534 register struct file_name_map_list *map_list_ptr;
535 char *name;
536 FILE *f;
537
538 for (map_list_ptr = CPP_OPTIONS (pfile)->map_list; map_list_ptr;
539 map_list_ptr = map_list_ptr->map_list_next)
540 if (! strcmp (map_list_ptr->map_list_name, dirname))
541 return map_list_ptr->map_list_map;
542
543 map_list_ptr = ((struct file_name_map_list *)
544 xmalloc (sizeof (struct file_name_map_list)));
545 map_list_ptr->map_list_name = xstrdup (dirname);
546
547 name = (char *) alloca (strlen (dirname) + strlen (FILE_NAME_MAP_FILE) + 2);
548 strcpy (name, dirname);
549 if (*dirname)
550 strcat (name, "/");
551 strcat (name, FILE_NAME_MAP_FILE);
552 f = fopen (name, "r");
553 if (!f)
554 map_list_ptr->map_list_map = (struct file_name_map *)-1;
555 else
556 {
557 int ch;
558 int dirlen = strlen (dirname);
559
560 while ((ch = getc (f)) != EOF)
561 {
562 char *from, *to;
563 struct file_name_map *ptr;
564
565 if (is_space[ch])
566 continue;
567 from = read_filename_string (ch, f);
568 while ((ch = getc (f)) != EOF && is_hor_space[ch])
569 ;
570 to = read_filename_string (ch, f);
571
572 ptr = ((struct file_name_map *)
573 xmalloc (sizeof (struct file_name_map)));
574 ptr->map_from = from;
575
576 /* Make the real filename absolute. */
577 if (*to == '/')
578 ptr->map_to = to;
579 else
580 {
581 ptr->map_to = xmalloc (dirlen + strlen (to) + 2);
582 strcpy (ptr->map_to, dirname);
583 ptr->map_to[dirlen] = '/';
584 strcpy (ptr->map_to + dirlen + 1, to);
585 free (to);
586 }
587
588 ptr->map_next = map_list_ptr->map_list_map;
589 map_list_ptr->map_list_map = ptr;
590
591 while ((ch = getc (f)) != '\n')
592 if (ch == EOF)
593 break;
594 }
595 fclose (f);
596 }
597
598 map_list_ptr->map_list_next = CPP_OPTIONS (pfile)->map_list;
599 CPP_OPTIONS (pfile)->map_list = map_list_ptr;
600
601 return map_list_ptr->map_list_map;
602 }
603
604 /* Remap NAME based on the file_name_map (if any) for LOC. */
605
606 static char *
607 remap_filename (pfile, name, loc)
608 cpp_reader *pfile;
609 char *name;
610 struct file_name_list *loc;
611 {
612 struct file_name_map *map;
613 const char *from, *p, *dir;
614
615 if (! loc->name_map)
616 loc->name_map = read_name_map (pfile,
617 loc->name
618 ? loc->name : ".");
619
620 if (loc->name_map == (struct file_name_map *)-1)
621 return name;
622
623 from = name + strlen (loc->name) + 1;
624
625 for (map = loc->name_map; map; map = map->map_next)
626 if (!strcmp (map->map_from, from))
627 return map->map_to;
628
629 /* Try to find a mapping file for the particular directory we are
630 looking in. Thus #include <sys/types.h> will look up sys/types.h
631 in /usr/include/header.gcc and look up types.h in
632 /usr/include/sys/header.gcc. */
633 p = rindex (name, '/');
634 if (!p)
635 p = name;
636 if (loc && loc->name
637 && strlen (loc->name) == (size_t) (p - name)
638 && !strncmp (loc->name, name, p - name))
639 /* FILENAME is in SEARCHPTR, which we've already checked. */
640 return name;
641
642 if (p == name)
643 {
644 dir = ".";
645 from = name;
646 }
647 else
648 {
649 char * newdir = (char *) alloca (p - name + 1);
650 bcopy (name, newdir, p - name);
651 newdir[p - name] = '\0';
652 dir = newdir;
653 from = p + 1;
654 }
655
656 for (map = read_name_map (pfile, dir); map; map = map->map_next)
657 if (! strcmp (map->map_from, name))
658 return map->map_to;
659
660 return name;
661 }
662
663 /* Read the contents of FD into the buffer on the top of PFILE's stack.
664 IHASH points to the include hash entry for the file associated with
665 FD.
666
667 The caller is responsible for the cpp_push_buffer. */
668
669 int
670 finclude (pfile, fd, ihash)
671 cpp_reader *pfile;
672 int fd;
673 struct include_hash *ihash;
674 {
675 struct stat st;
676 size_t st_size;
677 long length;
678 cpp_buffer *fp;
679
680 if (fstat (fd, &st) < 0)
681 goto perror_fail;
682 if (fcntl (fd, F_SETFL, 0) == -1) /* turn off nonblocking mode */
683 goto perror_fail;
684
685 fp = CPP_BUFFER (pfile);
686
687 /* If fd points to a plain file, we know how big it is, so we can
688 allocate the buffer all at once. If fd is a pipe or terminal, we
689 can't. Most C source files are 4k or less, so we guess that. If
690 fd is something weird, like a block device or a directory, we
691 don't want to read it at all.
692
693 Unfortunately, different systems use different st.st_mode values
694 for pipes: some have S_ISFIFO, some S_ISSOCK, some are buggy and
695 zero the entire struct stat except a couple fields. Hence the
696 mess below.
697
698 In all cases, read_and_prescan will resize the buffer if it
699 turns out there's more data than we thought. */
700
701 if (S_ISREG (st.st_mode))
702 {
703 /* off_t might have a wider range than size_t - in other words,
704 the max size of a file might be bigger than the address
705 space. We can't handle a file that large. (Anyone with
706 a single source file bigger than 4GB needs to rethink
707 their coding style.) */
708 st_size = (size_t) st.st_size;
709 if ((unsigned HOST_WIDEST_INT) st_size
710 != (unsigned HOST_WIDEST_INT) st.st_size)
711 {
712 cpp_error (pfile, "file `%s' is too large", ihash->name);
713 goto fail;
714 }
715 }
716 else if (S_ISFIFO (st.st_mode) || S_ISSOCK (st.st_mode)
717 /* Some 4.x (x<4) derivatives have a bug that makes fstat() of a
718 socket or pipe return a stat struct with most fields zeroed. */
719 || (st.st_mode == 0 && st.st_nlink == 0 && st.st_size == 0)
720 || (S_ISCHR (st.st_mode) && isatty (fd)))
721 {
722 /* Cannot get its file size before reading. 4k is a decent
723 first guess. */
724 st_size = 4096;
725 }
726 else
727 {
728 cpp_error (pfile, "`%s' is not a file, pipe, or tty", ihash->name);
729 goto fail;
730 }
731
732 /* Read the file, converting end-of-line characters and trigraphs
733 (if enabled). */
734 fp->ihash = ihash;
735 fp->nominal_fname = fp->fname = ihash->name;
736 length = read_and_prescan (pfile, fp, fd, st_size);
737 if (length < 0)
738 goto fail;
739 if (length == 0)
740 ihash->control_macro = ""; /* never re-include */
741
742 close (fd);
743 fp->rlimit = fp->alimit = fp->buf + length;
744 fp->cur = fp->buf;
745 fp->system_header_p = (ihash->foundhere != ABSOLUTE_PATH
746 && ihash->foundhere->sysp);
747 fp->lineno = 1;
748 fp->colno = 1;
749 fp->cleanup = file_cleanup;
750
751 /* The ->actual_dir field is only used when ignore_srcdir is not in effect;
752 see do_include */
753 if (!CPP_OPTIONS (pfile)->ignore_srcdir)
754 fp->actual_dir = actual_directory (pfile, fp->fname);
755
756 pfile->input_stack_listing_current = 0;
757 return 1;
758
759 perror_fail:
760 cpp_error_from_errno (pfile, ihash->name);
761 fail:
762 cpp_pop_buffer (pfile);
763 close (fd);
764 return 0;
765 }
766
767 /* Given a path FNAME, extract the directory component and place it
768 onto the actual_dirs list. Return a pointer to the allocated
769 file_name_list structure. These structures are used to implement
770 current-directory "" include searching. */
771
772 static struct file_name_list *
773 actual_directory (pfile, fname)
774 cpp_reader *pfile;
775 char *fname;
776 {
777 char *last_slash, *dir;
778 size_t dlen;
779 struct file_name_list *x;
780
781 dir = xstrdup (fname);
782 last_slash = rindex (dir, '/');
783 if (last_slash)
784 {
785 if (last_slash == dir)
786 {
787 dlen = 1;
788 last_slash[1] = '\0';
789 }
790 else
791 {
792 dlen = last_slash - dir;
793 *last_slash = '\0';
794 }
795 }
796 else
797 {
798 dir[0] = '.';
799 dir[1] = '\0';
800 dlen = 1;
801 }
802
803 if (dlen > pfile->max_include_len)
804 pfile->max_include_len = dlen;
805
806 for (x = pfile->actual_dirs; x; x = x->alloc)
807 if (!strcmp (x->name, dir))
808 {
809 free (dir);
810 return x;
811 }
812
813 /* Not found, make a new one. */
814 x = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
815 x->name = dir;
816 x->nlen = dlen;
817 x->next = CPP_OPTIONS (pfile)->quote_include;
818 x->alloc = pfile->actual_dirs;
819 x->sysp = 0;
820 x->name_map = NULL;
821
822 pfile->actual_dirs = x;
823 return x;
824 }
825
826 /* Read the entire contents of file DESC into buffer BUF, convert end-of-line
827 markers to canonical form, and convert trigraphs if enabled. Also, make
828 sure there is a newline at the end of the file. LEN is how much room we
829 have to start with (this can be expanded if necessary).
830 Returns -1 on failure, or the actual length of the data to be scanned.
831
832 N.B. This function has been rearranged to out-of-line the uncommon cases
833 as much as possible; this is important to prevent it from being a
834 performance bottleneck. */
835
836 static long
837 read_and_prescan (pfile, fp, desc, len)
838 cpp_reader *pfile;
839 cpp_buffer *fp;
840 int desc;
841 size_t len;
842 {
843 U_CHAR *buf = (U_CHAR *) xmalloc (len);
844 U_CHAR *ip, *op, *line_base;
845 U_CHAR *ibase;
846 unsigned int line;
847 int count;
848 size_t offset;
849 /* 4096 bytes of buffer proper, 2 to detect running off the end without
850 address arithmetic all the time, and 2 for pushback in the case there's
851 a potential trigraph or end-of-line digraph at the end of a block. */
852 #define INTERMED_BUFFER_SIZE 4096
853 U_CHAR intermed[INTERMED_BUFFER_SIZE + 2 + 2];
854
855 offset = 0;
856 op = buf;
857 line_base = buf;
858 line = 1;
859 ibase = intermed + 2;
860
861 for (;;)
862 {
863 read_next:
864
865 count = read (desc, intermed + 2, INTERMED_BUFFER_SIZE);
866 if (count < 0)
867 goto error;
868 else if (count == 0)
869 break;
870
871 offset += count;
872 ip = ibase;
873 ibase = intermed + 2;
874 ibase[count] = ibase[count+1] = '\0';
875
876 if (offset > len)
877 {
878 size_t delta_op;
879 size_t delta_line_base;
880 len *= 2;
881 if (offset > len)
882 /* len overflowed.
883 This could happen if the file is larger than half the
884 maximum address space of the machine. */
885 goto too_big;
886
887 delta_op = op - buf;
888 delta_line_base = line_base - buf;
889 buf = (U_CHAR *) xrealloc (buf, len);
890 op = buf + delta_op;
891 line_base = buf + delta_line_base;
892 }
893
894 for (;;)
895 {
896 unsigned int c;
897 c = *ip++;
898 switch (c)
899 {
900 /* The default case is at the top so gcc will realize
901 it's the common case, and leave c in a register.
902 Also, cache utilization is a little better this way. */
903 default:
904 *op++ = c;
905 break;
906
907 case '\0':
908 goto read_next;
909 case '\r':
910 if (*ip == '\n') ip++;
911 else if (*ip == '\0')
912 {
913 --ibase;
914 intermed[1] = '\r';
915 goto read_next;
916 }
917 *op++ = '\n';
918 line++;
919 line_base = op;
920 break;
921
922 case '\n':
923 if (*ip == '\r') ip++;
924 else if (*ip == '\0')
925 {
926 --ibase;
927 intermed[1] = '\n';
928 goto read_next;
929 }
930 *op++ = '\n';
931 line++;
932 line_base = op;
933 break;
934
935 case '?':
936 if (CPP_OPTIONS (pfile)->trigraphs
937 || CPP_OPTIONS (pfile)->warn_trigraphs)
938 {
939 unsigned int d;
940 /* If we're at the end of the intermediate buffer,
941 we have to shift the ?'s down to the start and
942 come back next pass. */
943 d = ip[0];
944 if (d == '\0')
945 {
946 --ibase;
947 intermed[1] = '?';
948 goto read_next;
949 }
950 if (d != '?')
951 {
952 *op++ = '?';
953 break;
954 }
955 d = ip[1];
956 if (d == '\0')
957 {
958 ibase -= 2;
959 intermed[0] = intermed[1] = '?';
960 goto read_next;
961 }
962 if (!trigraph_table[d])
963 {
964 *op++ = '?';
965 break;
966 }
967
968 if (CPP_OPTIONS (pfile)->warn_trigraphs)
969 cpp_warning_with_line (pfile, line, op-line_base,
970 "trigraph ??%c encountered", d);
971 if (CPP_OPTIONS (pfile)->trigraphs)
972 *op++ = trigraph_table[d];
973 else
974 {
975 *op++ = '?';
976 *op++ = '?';
977 *op++ = d;
978 }
979 ip += 2;
980 }
981 else
982 *op++ = c;
983 }
984 }
985 }
986
987 if (offset == 0)
988 return 0;
989
990 /* Deal with pushed-back chars at true EOF.
991 If two chars were pushed back, they must both be ?'s.
992 If one was, it might be ?, \r, or \n, and \r needs to
993 become \n.
994 We know we have space already. */
995 if (ibase == intermed)
996 {
997 *op++ = '?';
998 *op++ = '?';
999 }
1000 else if (ibase == intermed + 1)
1001 {
1002 if (*ibase == '?')
1003 *op++ = '?';
1004 else
1005 *op++ = '\n';
1006 }
1007
1008 if (op[-1] != '\n' || op[-2] == '\\')
1009 {
1010 if (CPP_PEDANTIC (pfile))
1011 cpp_pedwarn_with_line (pfile, line, op - line_base,
1012 "no newline at end of file");
1013 if (offset + 2 > len)
1014 {
1015 len += 2;
1016 if (offset + 2 > len)
1017 goto too_big;
1018 buf = (U_CHAR *) xrealloc (buf, len);
1019 op = buf + offset;
1020 }
1021 if (op[-1] == '\\')
1022 *op++ = '\n';
1023 *op++ = '\n';
1024 }
1025
1026 fp->buf =
1027 (U_CHAR *) ((len - offset < 20) ? (PTR) buf : xrealloc (buf, op - buf));
1028 return op - buf;
1029
1030 too_big:
1031 cpp_error (pfile, "file is too large");
1032 free (buf);
1033 return -1;
1034
1035 error:
1036 cpp_error_from_errno (pfile, fp->fname);
1037 free (buf);
1038 return -1;
1039 }
1040
1041 /* Add output to `deps_buffer' for the -M switch.
1042 STRING points to the text to be output.
1043 SPACER is ':' for targets, ' ' for dependencies, zero for text
1044 to be inserted literally. */
1045
1046 void
1047 deps_output (pfile, string, spacer)
1048 cpp_reader *pfile;
1049 char *string;
1050 int spacer;
1051 {
1052 int size;
1053 int cr = 0;
1054
1055 if (!*string)
1056 return;
1057
1058 size = strlen (string);
1059
1060 #ifndef MAX_OUTPUT_COLUMNS
1061 #define MAX_OUTPUT_COLUMNS 72
1062 #endif
1063 if (pfile->deps_column > 0
1064 && (pfile->deps_column + size) > MAX_OUTPUT_COLUMNS)
1065 {
1066 size += 5;
1067 cr = 1;
1068 pfile->deps_column = 0;
1069 }
1070
1071 if (pfile->deps_size + size + 8 > pfile->deps_allocated_size)
1072 {
1073 pfile->deps_allocated_size = (pfile->deps_size + size + 50) * 2;
1074 pfile->deps_buffer = (char *) xrealloc (pfile->deps_buffer,
1075 pfile->deps_allocated_size);
1076 }
1077
1078 if (cr)
1079 {
1080 bcopy (" \\\n ", &pfile->deps_buffer[pfile->deps_size], 5);
1081 pfile->deps_size += 5;
1082 }
1083
1084 if (spacer == ' ' && pfile->deps_column > 0)
1085 pfile->deps_buffer[pfile->deps_size++] = ' ';
1086 bcopy (string, &pfile->deps_buffer[pfile->deps_size], size);
1087 pfile->deps_size += size;
1088 pfile->deps_column += size;
1089 if (spacer == ':')
1090 pfile->deps_buffer[pfile->deps_size++] = ':';
1091 pfile->deps_buffer[pfile->deps_size] = 0;
1092 }
1093
1094 /* Simplify a path name in place, deleting redundant components. This
1095 reduces OS overhead and guarantees that equivalent paths compare
1096 the same (modulo symlinks).
1097
1098 Transforms made:
1099 foo/bar/../quux foo/quux
1100 foo/./bar foo/bar
1101 foo//bar foo/bar
1102 /../quux /quux
1103 //quux //quux (POSIX allows leading // as a namespace escape)
1104
1105 Guarantees no trailing slashes. All transforms reduce the length
1106 of the string.
1107 */
1108 static void
1109 simplify_pathname (path)
1110 char *path;
1111 {
1112 char *from, *to;
1113 char *base;
1114 int absolute = 0;
1115
1116 #if defined _WIN32 || defined __MSDOS__
1117 /* Convert all backslashes to slashes. */
1118 for (from = path; *from; from++)
1119 if (*from == '\\') *from = '/';
1120
1121 /* Skip over leading drive letter if present. */
1122 if (ISALPHA (path[0]) && path[1] == ':')
1123 from = to = &path[2];
1124 else
1125 from = to = path;
1126 #else
1127 from = to = path;
1128 #endif
1129
1130 /* Remove redundant initial /s. */
1131 if (*from == '/')
1132 {
1133 absolute = 1;
1134 to++;
1135 from++;
1136 if (*from == '/')
1137 {
1138 if (*++from == '/')
1139 /* 3 or more initial /s are equivalent to 1 /. */
1140 while (*++from == '/');
1141 else
1142 /* On some hosts // differs from /; Posix allows this. */
1143 to++;
1144 }
1145 }
1146 base = to;
1147
1148 for (;;)
1149 {
1150 while (*from == '/')
1151 from++;
1152
1153 if (from[0] == '.' && from[1] == '/')
1154 from += 2;
1155 else if (from[0] == '.' && from[1] == '\0')
1156 goto done;
1157 else if (from[0] == '.' && from[1] == '.' && from[2] == '/')
1158 {
1159 if (base == to)
1160 {
1161 if (absolute)
1162 from += 3;
1163 else
1164 {
1165 *to++ = *from++;
1166 *to++ = *from++;
1167 *to++ = *from++;
1168 base = to;
1169 }
1170 }
1171 else
1172 {
1173 to -= 2;
1174 while (to > base && *to != '/') to--;
1175 if (*to == '/')
1176 to++;
1177 from += 3;
1178 }
1179 }
1180 else if (from[0] == '.' && from[1] == '.' && from[2] == '\0')
1181 {
1182 if (base == to)
1183 {
1184 if (!absolute)
1185 {
1186 *to++ = *from++;
1187 *to++ = *from++;
1188 }
1189 }
1190 else
1191 {
1192 to -= 2;
1193 while (to > base && *to != '/') to--;
1194 if (*to == '/')
1195 to++;
1196 }
1197 goto done;
1198 }
1199 else
1200 /* Copy this component and trailing /, if any. */
1201 while ((*to++ = *from++) != '/')
1202 {
1203 if (!to[-1])
1204 {
1205 to--;
1206 goto done;
1207 }
1208 }
1209
1210 }
1211
1212 done:
1213 /* Trim trailing slash */
1214 if (to[0] == '/' && (!absolute || to > path+1))
1215 to--;
1216
1217 /* Change the empty string to "." so that stat() on the result
1218 will always work. */
1219 if (to == path)
1220 *to++ = '.';
1221
1222 *to = '\0';
1223
1224 return;
1225 }
1226
1227 /* It is not clear when this should be used if at all, so I've
1228 disabled it until someone who understands VMS can look at it. */
1229 #if 0
1230
1231 /* Under VMS we need to fix up the "include" specification filename.
1232
1233 Rules for possible conversions
1234
1235 fullname tried paths
1236
1237 name name
1238 ./dir/name [.dir]name
1239 /dir/name dir:name
1240 /name [000000]name, name
1241 dir/name dir:[000000]name, dir:name, dir/name
1242 dir1/dir2/name dir1:[dir2]name, dir1:[000000.dir2]name
1243 path:/name path:[000000]name, path:name
1244 path:/dir/name path:[000000.dir]name, path:[dir]name
1245 path:dir/name path:[dir]name
1246 [path]:[dir]name [path.dir]name
1247 path/[dir]name [path.dir]name
1248
1249 The path:/name input is constructed when expanding <> includes. */
1250
1251
1252 static void
1253 hack_vms_include_specification (fullname)
1254 char *fullname;
1255 {
1256 register char *basename, *unixname, *local_ptr, *first_slash;
1257 int f, check_filename_before_returning, must_revert;
1258 char Local[512];
1259
1260 check_filename_before_returning = 0;
1261 must_revert = 0;
1262 /* See if we can find a 1st slash. If not, there's no path information. */
1263 first_slash = index (fullname, '/');
1264 if (first_slash == 0)
1265 return 0; /* Nothing to do!!! */
1266
1267 /* construct device spec if none given. */
1268
1269 if (index (fullname, ':') == 0)
1270 {
1271
1272 /* If fullname has a slash, take it as device spec. */
1273
1274 if (first_slash == fullname)
1275 {
1276 first_slash = index (fullname+1, '/'); /* 2nd slash ? */
1277 if (first_slash)
1278 *first_slash = ':'; /* make device spec */
1279 for (basename = fullname; *basename != 0; basename++)
1280 *basename = *(basename+1); /* remove leading slash */
1281 }
1282 else if ((first_slash[-1] != '.') /* keep ':/', './' */
1283 && (first_slash[-1] != ':')
1284 && (first_slash[-1] != ']')) /* or a vms path */
1285 {
1286 *first_slash = ':';
1287 }
1288 else if ((first_slash[1] == '[') /* skip './' in './[dir' */
1289 && (first_slash[-1] == '.'))
1290 fullname += 2;
1291 }
1292
1293 /* Get part after first ':' (basename[-1] == ':')
1294 or last '/' (basename[-1] == '/'). */
1295
1296 basename = base_name (fullname);
1297
1298 local_ptr = Local; /* initialize */
1299
1300 /* We are trying to do a number of things here. First of all, we are
1301 trying to hammer the filenames into a standard format, such that later
1302 processing can handle them.
1303
1304 If the file name contains something like [dir.], then it recognizes this
1305 as a root, and strips the ".]". Later processing will add whatever is
1306 needed to get things working properly.
1307
1308 If no device is specified, then the first directory name is taken to be
1309 a device name (or a rooted logical). */
1310
1311 /* Point to the UNIX filename part (which needs to be fixed!)
1312 but skip vms path information.
1313 [basename != fullname since first_slash != 0]. */
1314
1315 if ((basename[-1] == ':') /* vms path spec. */
1316 || (basename[-1] == ']')
1317 || (basename[-1] == '>'))
1318 unixname = basename;
1319 else
1320 unixname = fullname;
1321
1322 if (*unixname == '/')
1323 unixname++;
1324
1325 /* If the directory spec is not rooted, we can just copy
1326 the UNIX filename part and we are done. */
1327
1328 if (((basename - fullname) > 1)
1329 && ( (basename[-1] == ']')
1330 || (basename[-1] == '>')))
1331 {
1332 if (basename[-2] != '.')
1333 {
1334
1335 /* The VMS part ends in a `]', and the preceding character is not a `.'.
1336 -> PATH]:/name (basename = '/name', unixname = 'name')
1337 We strip the `]', and then splice the two parts of the name in the
1338 usual way. Given the default locations for include files in cccp.c,
1339 we will only use this code if the user specifies alternate locations
1340 with the /include (-I) switch on the command line. */
1341
1342 basename -= 1; /* Strip "]" */
1343 unixname--; /* backspace */
1344 }
1345 else
1346 {
1347
1348 /* The VMS part has a ".]" at the end, and this will not do. Later
1349 processing will add a second directory spec, and this would be a syntax
1350 error. Thus we strip the ".]", and thus merge the directory specs.
1351 We also backspace unixname, so that it points to a '/'. This inhibits the
1352 generation of the 000000 root directory spec (which does not belong here
1353 in this case). */
1354
1355 basename -= 2; /* Strip ".]" */
1356 unixname--; /* backspace */
1357 }
1358 }
1359
1360 else
1361
1362 {
1363
1364 /* We drop in here if there is no VMS style directory specification yet.
1365 If there is no device specification either, we make the first dir a
1366 device and try that. If we do not do this, then we will be essentially
1367 searching the users default directory (as if they did a #include "asdf.h").
1368
1369 Then all we need to do is to push a '[' into the output string. Later
1370 processing will fill this in, and close the bracket. */
1371
1372 if ((unixname != fullname) /* vms path spec found. */
1373 && (basename[-1] != ':'))
1374 *local_ptr++ = ':'; /* dev not in spec. take first dir */
1375
1376 *local_ptr++ = '['; /* Open the directory specification */
1377 }
1378
1379 if (unixname == fullname) /* no vms dir spec. */
1380 {
1381 must_revert = 1;
1382 if ((first_slash != 0) /* unix dir spec. */
1383 && (*unixname != '/') /* not beginning with '/' */
1384 && (*unixname != '.')) /* or './' or '../' */
1385 *local_ptr++ = '.'; /* dir is local ! */
1386 }
1387
1388 /* at this point we assume that we have the device spec, and (at least
1389 the opening "[" for a directory specification. We may have directories
1390 specified already.
1391
1392 If there are no other slashes then the filename will be
1393 in the "root" directory. Otherwise, we need to add
1394 directory specifications. */
1395
1396 if (index (unixname, '/') == 0)
1397 {
1398 /* if no directories specified yet and none are following. */
1399 if (local_ptr[-1] == '[')
1400 {
1401 /* Just add "000000]" as the directory string */
1402 strcpy (local_ptr, "000000]");
1403 local_ptr += strlen (local_ptr);
1404 check_filename_before_returning = 1; /* we might need to fool with this later */
1405 }
1406 }
1407 else
1408 {
1409
1410 /* As long as there are still subdirectories to add, do them. */
1411 while (index (unixname, '/') != 0)
1412 {
1413 /* If this token is "." we can ignore it
1414 if it's not at the beginning of a path. */
1415 if ((unixname[0] == '.') && (unixname[1] == '/'))
1416 {
1417 /* remove it at beginning of path. */
1418 if ( ((unixname == fullname) /* no device spec */
1419 && (fullname+2 != basename)) /* starts with ./ */
1420 /* or */
1421 || ((basename[-1] == ':') /* device spec */
1422 && (unixname-1 == basename))) /* and ./ afterwards */
1423 *local_ptr++ = '.'; /* make '[.' start of path. */
1424 unixname += 2;
1425 continue;
1426 }
1427
1428 /* Add a subdirectory spec. Do not duplicate "." */
1429 if ( local_ptr[-1] != '.'
1430 && local_ptr[-1] != '['
1431 && local_ptr[-1] != '<')
1432 *local_ptr++ = '.';
1433
1434 /* If this is ".." then the spec becomes "-" */
1435 if ( (unixname[0] == '.')
1436 && (unixname[1] == '.')
1437 && (unixname[2] == '/'))
1438 {
1439 /* Add "-" and skip the ".." */
1440 if ((local_ptr[-1] == '.')
1441 && (local_ptr[-2] == '['))
1442 local_ptr--; /* prevent [.- */
1443 *local_ptr++ = '-';
1444 unixname += 3;
1445 continue;
1446 }
1447
1448 /* Copy the subdirectory */
1449 while (*unixname != '/')
1450 *local_ptr++= *unixname++;
1451
1452 unixname++; /* Skip the "/" */
1453 }
1454
1455 /* Close the directory specification */
1456 if (local_ptr[-1] == '.') /* no trailing periods */
1457 local_ptr--;
1458
1459 if (local_ptr[-1] == '[') /* no dir needed */
1460 local_ptr--;
1461 else
1462 *local_ptr++ = ']';
1463 }
1464
1465 /* Now add the filename. */
1466
1467 while (*unixname)
1468 *local_ptr++ = *unixname++;
1469 *local_ptr = 0;
1470
1471 /* Now append it to the original VMS spec. */
1472
1473 strcpy ((must_revert==1)?fullname:basename, Local);
1474
1475 /* If we put a [000000] in the filename, try to open it first. If this fails,
1476 remove the [000000], and return that name. This provides flexibility
1477 to the user in that they can use both rooted and non-rooted logical names
1478 to point to the location of the file. */
1479
1480 if (check_filename_before_returning)
1481 {
1482 f = open (fullname, O_RDONLY, 0666);
1483 if (f >= 0)
1484 {
1485 /* The file name is OK as it is, so return it as is. */
1486 close (f);
1487 return 1;
1488 }
1489
1490 /* The filename did not work. Try to remove the [000000] from the name,
1491 and return it. */
1492
1493 basename = index (fullname, '[');
1494 local_ptr = index (fullname, ']') + 1;
1495 strcpy (basename, local_ptr); /* this gets rid of it */
1496
1497 }
1498
1499 return 1;
1500 }
1501 #endif /* VMS */