1*404b540aSrobert /* Part of CPP library. File handling.
2*404b540aSrobert Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1998,
3*404b540aSrobert 1999, 2000, 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
4*404b540aSrobert Written by Per Bothner, 1994.
5*404b540aSrobert Based on CCCP program by Paul Rubin, June 1986
6*404b540aSrobert Adapted to ANSI C, Richard Stallman, Jan 1987
7*404b540aSrobert Split out of cpplib.c, Zack Weinberg, Oct 1998
8*404b540aSrobert Reimplemented, Neil Booth, Jul 2003
9*404b540aSrobert
10*404b540aSrobert This program is free software; you can redistribute it and/or modify it
11*404b540aSrobert under the terms of the GNU General Public License as published by the
12*404b540aSrobert Free Software Foundation; either version 2, or (at your option) any
13*404b540aSrobert later version.
14*404b540aSrobert
15*404b540aSrobert This program is distributed in the hope that it will be useful,
16*404b540aSrobert but WITHOUT ANY WARRANTY; without even the implied warranty of
17*404b540aSrobert MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18*404b540aSrobert GNU General Public License for more details.
19*404b540aSrobert
20*404b540aSrobert You should have received a copy of the GNU General Public License
21*404b540aSrobert along with this program; if not, write to the Free Software
22*404b540aSrobert Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
23*404b540aSrobert
24*404b540aSrobert #include "config.h"
25*404b540aSrobert #include "system.h"
26*404b540aSrobert #include "cpplib.h"
27*404b540aSrobert #include "internal.h"
28*404b540aSrobert #include "mkdeps.h"
29*404b540aSrobert #include "hashtab.h"
30*404b540aSrobert #include "md5.h"
31*404b540aSrobert #include <dirent.h>
32*404b540aSrobert
33*404b540aSrobert /* Variable length record files on VMS will have a stat size that includes
34*404b540aSrobert record control characters that won't be included in the read size. */
35*404b540aSrobert #ifdef VMS
36*404b540aSrobert # define FAB_C_VAR 2 /* variable length records (see Starlet fabdef.h) */
37*404b540aSrobert # define STAT_SIZE_RELIABLE(ST) ((ST).st_fab_rfm != FAB_C_VAR)
38*404b540aSrobert #else
39*404b540aSrobert # define STAT_SIZE_RELIABLE(ST) true
40*404b540aSrobert #endif
41*404b540aSrobert
42*404b540aSrobert #ifdef __DJGPP__
43*404b540aSrobert #include <io.h>
44*404b540aSrobert /* For DJGPP redirected input is opened in text mode. */
45*404b540aSrobert # define set_stdin_to_binary_mode() \
46*404b540aSrobert if (! isatty (0)) setmode (0, O_BINARY)
47*404b540aSrobert #else
48*404b540aSrobert # define set_stdin_to_binary_mode() /* Nothing */
49*404b540aSrobert #endif
50*404b540aSrobert
51*404b540aSrobert /* This structure represents a file searched for by CPP, whether it
52*404b540aSrobert exists or not. An instance may be pointed to by more than one
53*404b540aSrobert file_hash_entry; at present no reference count is kept. */
54*404b540aSrobert struct _cpp_file
55*404b540aSrobert {
56*404b540aSrobert /* Filename as given to #include or command line switch. */
57*404b540aSrobert const char *name;
58*404b540aSrobert
59*404b540aSrobert /* The full path used to find the file. */
60*404b540aSrobert const char *path;
61*404b540aSrobert
62*404b540aSrobert /* The full path of the pch file. */
63*404b540aSrobert const char *pchname;
64*404b540aSrobert
65*404b540aSrobert /* The file's path with the basename stripped. NULL if it hasn't
66*404b540aSrobert been calculated yet. */
67*404b540aSrobert const char *dir_name;
68*404b540aSrobert
69*404b540aSrobert /* Chain through all files. */
70*404b540aSrobert struct _cpp_file *next_file;
71*404b540aSrobert
72*404b540aSrobert /* The contents of NAME after calling read_file(). */
73*404b540aSrobert const uchar *buffer;
74*404b540aSrobert
75*404b540aSrobert /* The macro, if any, preventing re-inclusion. */
76*404b540aSrobert const cpp_hashnode *cmacro;
77*404b540aSrobert
78*404b540aSrobert /* The directory in the search path where FILE was found. Used for
79*404b540aSrobert #include_next and determining whether a header is a system
80*404b540aSrobert header. */
81*404b540aSrobert cpp_dir *dir;
82*404b540aSrobert
83*404b540aSrobert /* As filled in by stat(2) for the file. */
84*404b540aSrobert struct stat st;
85*404b540aSrobert
86*404b540aSrobert /* File descriptor. Invalid if -1, otherwise open. */
87*404b540aSrobert int fd;
88*404b540aSrobert
89*404b540aSrobert /* Zero if this file was successfully opened and stat()-ed,
90*404b540aSrobert otherwise errno obtained from failure. */
91*404b540aSrobert int err_no;
92*404b540aSrobert
93*404b540aSrobert /* Number of times the file has been stacked for preprocessing. */
94*404b540aSrobert unsigned short stack_count;
95*404b540aSrobert
96*404b540aSrobert /* If opened with #import or contains #pragma once. */
97*404b540aSrobert bool once_only;
98*404b540aSrobert
99*404b540aSrobert /* If read() failed before. */
100*404b540aSrobert bool dont_read;
101*404b540aSrobert
102*404b540aSrobert /* If this file is the main file. */
103*404b540aSrobert bool main_file;
104*404b540aSrobert
105*404b540aSrobert /* If BUFFER above contains the true contents of the file. */
106*404b540aSrobert bool buffer_valid;
107*404b540aSrobert
108*404b540aSrobert /* File is a PCH (on return from find_include_file). */
109*404b540aSrobert bool pch;
110*404b540aSrobert };
111*404b540aSrobert
112*404b540aSrobert /* A singly-linked list for all searches for a given file name, with
113*404b540aSrobert its head pointed to by a slot in FILE_HASH. The file name is what
114*404b540aSrobert appeared between the quotes in a #include directive; it can be
115*404b540aSrobert determined implicitly from the hash table location or explicitly
116*404b540aSrobert from FILE->name.
117*404b540aSrobert
118*404b540aSrobert FILE is a structure containing details about the file that was
119*404b540aSrobert found with that search, or details of how the search failed.
120*404b540aSrobert
121*404b540aSrobert START_DIR is the starting location of the search in the include
122*404b540aSrobert chain. The current directories for "" includes are also hashed in
123*404b540aSrobert the hash table and therefore unique. Files that are looked up
124*404b540aSrobert without using a search path, such as absolute filenames and file
125*404b540aSrobert names from the command line share a special starting directory so
126*404b540aSrobert they don't cause cache hits with normal include-chain lookups.
127*404b540aSrobert
128*404b540aSrobert If START_DIR is NULL then the entry is for a directory, not a file,
129*404b540aSrobert and the directory is in DIR. Since the starting point in a file
130*404b540aSrobert lookup chain is never NULL, this means that simple pointer
131*404b540aSrobert comparisons against START_DIR can be made to determine cache hits
132*404b540aSrobert in file lookups.
133*404b540aSrobert
134*404b540aSrobert If a cache lookup fails because of e.g. an extra "./" in the path,
135*404b540aSrobert then nothing will break. It is just less efficient as CPP will
136*404b540aSrobert have to do more work re-preprocessing the file, and/or comparing
137*404b540aSrobert its contents against earlier once-only files.
138*404b540aSrobert */
139*404b540aSrobert struct file_hash_entry
140*404b540aSrobert {
141*404b540aSrobert struct file_hash_entry *next;
142*404b540aSrobert cpp_dir *start_dir;
143*404b540aSrobert union
144*404b540aSrobert {
145*404b540aSrobert _cpp_file *file;
146*404b540aSrobert cpp_dir *dir;
147*404b540aSrobert } u;
148*404b540aSrobert };
149*404b540aSrobert
150*404b540aSrobert static bool open_file (_cpp_file *file);
151*404b540aSrobert static bool pch_open_file (cpp_reader *pfile, _cpp_file *file,
152*404b540aSrobert bool *invalid_pch);
153*404b540aSrobert static bool find_file_in_dir (cpp_reader *pfile, _cpp_file *file,
154*404b540aSrobert bool *invalid_pch);
155*404b540aSrobert static bool read_file_guts (cpp_reader *pfile, _cpp_file *file);
156*404b540aSrobert static bool read_file (cpp_reader *pfile, _cpp_file *file);
157*404b540aSrobert static bool should_stack_file (cpp_reader *, _cpp_file *file, bool import);
158*404b540aSrobert static struct cpp_dir *search_path_head (cpp_reader *, const char *fname,
159*404b540aSrobert int angle_brackets, enum include_type);
160*404b540aSrobert static const char *dir_name_of_file (_cpp_file *file);
161*404b540aSrobert static void open_file_failed (cpp_reader *pfile, _cpp_file *file, int);
162*404b540aSrobert static struct file_hash_entry *search_cache (struct file_hash_entry *head,
163*404b540aSrobert const cpp_dir *start_dir);
164*404b540aSrobert static _cpp_file *make_cpp_file (cpp_reader *, cpp_dir *, const char *fname);
165*404b540aSrobert static void destroy_cpp_file (_cpp_file *);
166*404b540aSrobert static cpp_dir *make_cpp_dir (cpp_reader *, const char *dir_name, int sysp);
167*404b540aSrobert static void allocate_file_hash_entries (cpp_reader *pfile);
168*404b540aSrobert static struct file_hash_entry *new_file_hash_entry (cpp_reader *pfile);
169*404b540aSrobert static int report_missing_guard (void **slot, void *b);
170*404b540aSrobert static hashval_t file_hash_hash (const void *p);
171*404b540aSrobert static int file_hash_eq (const void *p, const void *q);
172*404b540aSrobert static char *read_filename_string (int ch, FILE *f);
173*404b540aSrobert static void read_name_map (cpp_dir *dir);
174*404b540aSrobert static char *remap_filename (cpp_reader *pfile, _cpp_file *file);
175*404b540aSrobert static char *append_file_to_dir (const char *fname, cpp_dir *dir);
176*404b540aSrobert static bool validate_pch (cpp_reader *, _cpp_file *file, const char *pchname);
177*404b540aSrobert static int pchf_save_compare (const void *e1, const void *e2);
178*404b540aSrobert static int pchf_compare (const void *d_p, const void *e_p);
179*404b540aSrobert static bool check_file_against_entries (cpp_reader *, _cpp_file *, bool);
180*404b540aSrobert
181*404b540aSrobert /* Given a filename in FILE->PATH, with the empty string interpreted
182*404b540aSrobert as <stdin>, open it.
183*404b540aSrobert
184*404b540aSrobert On success FILE contains an open file descriptor and stat
185*404b540aSrobert information for the file. On failure the file descriptor is -1 and
186*404b540aSrobert the appropriate errno is also stored in FILE. Returns TRUE iff
187*404b540aSrobert successful.
188*404b540aSrobert
189*404b540aSrobert We used to open files in nonblocking mode, but that caused more
190*404b540aSrobert problems than it solved. Do take care not to acquire a controlling
191*404b540aSrobert terminal by mistake (this can't happen on sane systems, but
192*404b540aSrobert paranoia is a virtue).
193*404b540aSrobert
194*404b540aSrobert Use the three-argument form of open even though we aren't
195*404b540aSrobert specifying O_CREAT, to defend against broken system headers.
196*404b540aSrobert
197*404b540aSrobert O_BINARY tells some runtime libraries (notably DJGPP) not to do
198*404b540aSrobert newline translation; we can handle DOS line breaks just fine
199*404b540aSrobert ourselves. */
200*404b540aSrobert static bool
open_file(_cpp_file * file)201*404b540aSrobert open_file (_cpp_file *file)
202*404b540aSrobert {
203*404b540aSrobert if (file->path[0] == '\0')
204*404b540aSrobert {
205*404b540aSrobert file->fd = 0;
206*404b540aSrobert set_stdin_to_binary_mode ();
207*404b540aSrobert }
208*404b540aSrobert else
209*404b540aSrobert file->fd = open (file->path, O_RDONLY | O_NOCTTY | O_BINARY, 0666);
210*404b540aSrobert
211*404b540aSrobert if (file->fd != -1)
212*404b540aSrobert {
213*404b540aSrobert if (fstat (file->fd, &file->st) == 0)
214*404b540aSrobert {
215*404b540aSrobert if (!S_ISDIR (file->st.st_mode))
216*404b540aSrobert {
217*404b540aSrobert file->err_no = 0;
218*404b540aSrobert return true;
219*404b540aSrobert }
220*404b540aSrobert
221*404b540aSrobert /* Ignore a directory and continue the search. The file we're
222*404b540aSrobert looking for may be elsewhere in the search path. */
223*404b540aSrobert errno = ENOENT;
224*404b540aSrobert }
225*404b540aSrobert
226*404b540aSrobert close (file->fd);
227*404b540aSrobert file->fd = -1;
228*404b540aSrobert }
229*404b540aSrobert else if (errno == ENOTDIR)
230*404b540aSrobert errno = ENOENT;
231*404b540aSrobert
232*404b540aSrobert file->err_no = errno;
233*404b540aSrobert
234*404b540aSrobert return false;
235*404b540aSrobert }
236*404b540aSrobert
237*404b540aSrobert /* Temporary PCH intercept of opening a file. Try to find a PCH file
238*404b540aSrobert based on FILE->name and FILE->dir, and test those found for
239*404b540aSrobert validity using PFILE->cb.valid_pch. Return true iff a valid file is
240*404b540aSrobert found. Set *INVALID_PCH if a PCH file is found but wasn't valid. */
241*404b540aSrobert
242*404b540aSrobert static bool
pch_open_file(cpp_reader * pfile,_cpp_file * file,bool * invalid_pch)243*404b540aSrobert pch_open_file (cpp_reader *pfile, _cpp_file *file, bool *invalid_pch)
244*404b540aSrobert {
245*404b540aSrobert static const char extension[] = ".gch";
246*404b540aSrobert const char *path = file->path;
247*404b540aSrobert size_t len, flen;
248*404b540aSrobert char *pchname;
249*404b540aSrobert struct stat st;
250*404b540aSrobert bool valid = false;
251*404b540aSrobert
252*404b540aSrobert /* No PCH on <stdin> or if not requested. */
253*404b540aSrobert if (file->name[0] == '\0' || !pfile->cb.valid_pch)
254*404b540aSrobert return false;
255*404b540aSrobert
256*404b540aSrobert flen = strlen (path);
257*404b540aSrobert len = flen + sizeof (extension);
258*404b540aSrobert pchname = XNEWVEC (char, len);
259*404b540aSrobert memcpy (pchname, path, flen);
260*404b540aSrobert memcpy (pchname + flen, extension, sizeof (extension));
261*404b540aSrobert
262*404b540aSrobert if (stat (pchname, &st) == 0)
263*404b540aSrobert {
264*404b540aSrobert DIR *pchdir;
265*404b540aSrobert struct dirent *d;
266*404b540aSrobert size_t dlen, plen = len;
267*404b540aSrobert
268*404b540aSrobert if (!S_ISDIR (st.st_mode))
269*404b540aSrobert valid = validate_pch (pfile, file, pchname);
270*404b540aSrobert else if ((pchdir = opendir (pchname)) != NULL)
271*404b540aSrobert {
272*404b540aSrobert pchname[plen - 1] = '/';
273*404b540aSrobert while ((d = readdir (pchdir)) != NULL)
274*404b540aSrobert {
275*404b540aSrobert dlen = strlen (d->d_name) + 1;
276*404b540aSrobert if ((strcmp (d->d_name, ".") == 0)
277*404b540aSrobert || (strcmp (d->d_name, "..") == 0))
278*404b540aSrobert continue;
279*404b540aSrobert if (dlen + plen > len)
280*404b540aSrobert {
281*404b540aSrobert len += dlen + 64;
282*404b540aSrobert pchname = XRESIZEVEC (char, pchname, len);
283*404b540aSrobert }
284*404b540aSrobert memcpy (pchname + plen, d->d_name, dlen);
285*404b540aSrobert valid = validate_pch (pfile, file, pchname);
286*404b540aSrobert if (valid)
287*404b540aSrobert break;
288*404b540aSrobert }
289*404b540aSrobert closedir (pchdir);
290*404b540aSrobert }
291*404b540aSrobert if (valid)
292*404b540aSrobert file->pch = true;
293*404b540aSrobert else
294*404b540aSrobert *invalid_pch = true;
295*404b540aSrobert }
296*404b540aSrobert
297*404b540aSrobert if (valid)
298*404b540aSrobert file->pchname = pchname;
299*404b540aSrobert else
300*404b540aSrobert free (pchname);
301*404b540aSrobert
302*404b540aSrobert return valid;
303*404b540aSrobert }
304*404b540aSrobert
305*404b540aSrobert /* Try to open the path FILE->name appended to FILE->dir. This is
306*404b540aSrobert where remap and PCH intercept the file lookup process. Return true
307*404b540aSrobert if the file was found, whether or not the open was successful.
308*404b540aSrobert Set *INVALID_PCH to true if a PCH file is found but wasn't valid. */
309*404b540aSrobert
310*404b540aSrobert static bool
find_file_in_dir(cpp_reader * pfile,_cpp_file * file,bool * invalid_pch)311*404b540aSrobert find_file_in_dir (cpp_reader *pfile, _cpp_file *file, bool *invalid_pch)
312*404b540aSrobert {
313*404b540aSrobert char *path;
314*404b540aSrobert
315*404b540aSrobert if (CPP_OPTION (pfile, remap) && (path = remap_filename (pfile, file)))
316*404b540aSrobert ;
317*404b540aSrobert else
318*404b540aSrobert if (file->dir->construct)
319*404b540aSrobert path = file->dir->construct (file->name, file->dir);
320*404b540aSrobert else
321*404b540aSrobert path = append_file_to_dir (file->name, file->dir);
322*404b540aSrobert
323*404b540aSrobert if (path)
324*404b540aSrobert {
325*404b540aSrobert file->path = path;
326*404b540aSrobert if (pch_open_file (pfile, file, invalid_pch))
327*404b540aSrobert return true;
328*404b540aSrobert
329*404b540aSrobert if (open_file (file))
330*404b540aSrobert return true;
331*404b540aSrobert
332*404b540aSrobert if (file->err_no != ENOENT)
333*404b540aSrobert {
334*404b540aSrobert open_file_failed (pfile, file, 0);
335*404b540aSrobert return true;
336*404b540aSrobert }
337*404b540aSrobert
338*404b540aSrobert free (path);
339*404b540aSrobert file->path = file->name;
340*404b540aSrobert }
341*404b540aSrobert else
342*404b540aSrobert {
343*404b540aSrobert file->err_no = ENOENT;
344*404b540aSrobert file->path = NULL;
345*404b540aSrobert }
346*404b540aSrobert
347*404b540aSrobert return false;
348*404b540aSrobert }
349*404b540aSrobert
350*404b540aSrobert /* Return tue iff the missing_header callback found the given HEADER. */
351*404b540aSrobert static bool
search_path_exhausted(cpp_reader * pfile,const char * header,_cpp_file * file)352*404b540aSrobert search_path_exhausted (cpp_reader *pfile, const char *header, _cpp_file *file)
353*404b540aSrobert {
354*404b540aSrobert missing_header_cb func = pfile->cb.missing_header;
355*404b540aSrobert
356*404b540aSrobert /* When the regular search path doesn't work, try context dependent
357*404b540aSrobert headers search paths. */
358*404b540aSrobert if (func
359*404b540aSrobert && file->dir == NULL)
360*404b540aSrobert {
361*404b540aSrobert if ((file->path = func (pfile, header, &file->dir)) != NULL)
362*404b540aSrobert {
363*404b540aSrobert if (open_file (file))
364*404b540aSrobert return true;
365*404b540aSrobert free ((void *)file->path);
366*404b540aSrobert }
367*404b540aSrobert file->path = file->name;
368*404b540aSrobert }
369*404b540aSrobert
370*404b540aSrobert return false;
371*404b540aSrobert }
372*404b540aSrobert
373*404b540aSrobert bool
_cpp_find_failed(_cpp_file * file)374*404b540aSrobert _cpp_find_failed (_cpp_file *file)
375*404b540aSrobert {
376*404b540aSrobert return file->err_no != 0;
377*404b540aSrobert }
378*404b540aSrobert
379*404b540aSrobert /* Given a filename FNAME search for such a file in the include path
380*404b540aSrobert starting from START_DIR. If FNAME is the empty string it is
381*404b540aSrobert interpreted as STDIN if START_DIR is PFILE->no_search_path.
382*404b540aSrobert
383*404b540aSrobert If the file is not found in the file cache fall back to the O/S and
384*404b540aSrobert add the result to our cache.
385*404b540aSrobert
386*404b540aSrobert If the file was not found in the filesystem, or there was an error
387*404b540aSrobert opening it, then ERR_NO is nonzero and FD is -1. If the file was
388*404b540aSrobert found, then ERR_NO is zero and FD could be -1 or an open file
389*404b540aSrobert descriptor. FD can be -1 if the file was found in the cache and
390*404b540aSrobert had previously been closed. To open it again pass the return value
391*404b540aSrobert to open_file().
392*404b540aSrobert */
393*404b540aSrobert _cpp_file *
_cpp_find_file(cpp_reader * pfile,const char * fname,cpp_dir * start_dir,bool fake,int angle_brackets)394*404b540aSrobert _cpp_find_file (cpp_reader *pfile, const char *fname, cpp_dir *start_dir, bool fake, int angle_brackets)
395*404b540aSrobert {
396*404b540aSrobert struct file_hash_entry *entry, **hash_slot;
397*404b540aSrobert _cpp_file *file;
398*404b540aSrobert bool invalid_pch = false;
399*404b540aSrobert
400*404b540aSrobert /* Ensure we get no confusion between cached files and directories. */
401*404b540aSrobert if (start_dir == NULL)
402*404b540aSrobert cpp_error (pfile, CPP_DL_ICE, "NULL directory in find_file");
403*404b540aSrobert
404*404b540aSrobert hash_slot = (struct file_hash_entry **)
405*404b540aSrobert htab_find_slot_with_hash (pfile->file_hash, fname,
406*404b540aSrobert htab_hash_string (fname),
407*404b540aSrobert INSERT);
408*404b540aSrobert
409*404b540aSrobert /* First check the cache before we resort to memory allocation. */
410*404b540aSrobert entry = search_cache (*hash_slot, start_dir);
411*404b540aSrobert if (entry)
412*404b540aSrobert return entry->u.file;
413*404b540aSrobert
414*404b540aSrobert file = make_cpp_file (pfile, start_dir, fname);
415*404b540aSrobert
416*404b540aSrobert /* Try each path in the include chain. */
417*404b540aSrobert for (; !fake ;)
418*404b540aSrobert {
419*404b540aSrobert if (find_file_in_dir (pfile, file, &invalid_pch))
420*404b540aSrobert break;
421*404b540aSrobert
422*404b540aSrobert file->dir = file->dir->next;
423*404b540aSrobert if (file->dir == NULL)
424*404b540aSrobert {
425*404b540aSrobert if (search_path_exhausted (pfile, fname, file))
426*404b540aSrobert {
427*404b540aSrobert /* Although this file must not go in the cache, because
428*404b540aSrobert the file found might depend on things (like the current file)
429*404b540aSrobert that aren't represented in the cache, it still has to go in
430*404b540aSrobert the list of all files so that #import works. */
431*404b540aSrobert file->next_file = pfile->all_files;
432*404b540aSrobert pfile->all_files = file;
433*404b540aSrobert return file;
434*404b540aSrobert }
435*404b540aSrobert
436*404b540aSrobert open_file_failed (pfile, file, angle_brackets);
437*404b540aSrobert if (invalid_pch)
438*404b540aSrobert {
439*404b540aSrobert cpp_error (pfile, CPP_DL_ERROR,
440*404b540aSrobert "one or more PCH files were found, but they were invalid");
441*404b540aSrobert if (!cpp_get_options (pfile)->warn_invalid_pch)
442*404b540aSrobert cpp_error (pfile, CPP_DL_ERROR,
443*404b540aSrobert "use -Winvalid-pch for more information");
444*404b540aSrobert }
445*404b540aSrobert break;
446*404b540aSrobert }
447*404b540aSrobert
448*404b540aSrobert /* Only check the cache for the starting location (done above)
449*404b540aSrobert and the quote and bracket chain heads because there are no
450*404b540aSrobert other possible starting points for searches. */
451*404b540aSrobert if (file->dir != pfile->bracket_include
452*404b540aSrobert && file->dir != pfile->quote_include)
453*404b540aSrobert continue;
454*404b540aSrobert
455*404b540aSrobert entry = search_cache (*hash_slot, file->dir);
456*404b540aSrobert if (entry)
457*404b540aSrobert break;
458*404b540aSrobert }
459*404b540aSrobert
460*404b540aSrobert if (entry)
461*404b540aSrobert {
462*404b540aSrobert /* Cache for START_DIR too, sharing the _cpp_file structure. */
463*404b540aSrobert free ((char *) file->name);
464*404b540aSrobert free (file);
465*404b540aSrobert file = entry->u.file;
466*404b540aSrobert }
467*404b540aSrobert else
468*404b540aSrobert {
469*404b540aSrobert /* This is a new file; put it in the list. */
470*404b540aSrobert file->next_file = pfile->all_files;
471*404b540aSrobert pfile->all_files = file;
472*404b540aSrobert }
473*404b540aSrobert
474*404b540aSrobert /* Store this new result in the hash table. */
475*404b540aSrobert entry = new_file_hash_entry (pfile);
476*404b540aSrobert entry->next = *hash_slot;
477*404b540aSrobert entry->start_dir = start_dir;
478*404b540aSrobert entry->u.file = file;
479*404b540aSrobert *hash_slot = entry;
480*404b540aSrobert
481*404b540aSrobert return file;
482*404b540aSrobert }
483*404b540aSrobert
484*404b540aSrobert /* Read a file into FILE->buffer, returning true on success.
485*404b540aSrobert
486*404b540aSrobert If FILE->fd is something weird, like a block device, we don't want
487*404b540aSrobert to read it at all. Don't even try to figure out what something is,
488*404b540aSrobert except for plain files and block devices, since there is no
489*404b540aSrobert reliable portable way of doing this.
490*404b540aSrobert
491*404b540aSrobert FIXME: Flush file cache and try again if we run out of memory. */
492*404b540aSrobert static bool
read_file_guts(cpp_reader * pfile,_cpp_file * file)493*404b540aSrobert read_file_guts (cpp_reader *pfile, _cpp_file *file)
494*404b540aSrobert {
495*404b540aSrobert ssize_t size, total, count;
496*404b540aSrobert uchar *buf;
497*404b540aSrobert bool regular;
498*404b540aSrobert
499*404b540aSrobert if (S_ISBLK (file->st.st_mode))
500*404b540aSrobert {
501*404b540aSrobert cpp_error (pfile, CPP_DL_ERROR, "%s is a block device", file->path);
502*404b540aSrobert return false;
503*404b540aSrobert }
504*404b540aSrobert
505*404b540aSrobert regular = S_ISREG (file->st.st_mode);
506*404b540aSrobert if (regular)
507*404b540aSrobert {
508*404b540aSrobert /* off_t might have a wider range than ssize_t - in other words,
509*404b540aSrobert the max size of a file might be bigger than the address
510*404b540aSrobert space. We can't handle a file that large. (Anyone with
511*404b540aSrobert a single source file bigger than 2GB needs to rethink
512*404b540aSrobert their coding style.) Some systems (e.g. AIX 4.1) define
513*404b540aSrobert SSIZE_MAX to be much smaller than the actual range of the
514*404b540aSrobert type. Use INTTYPE_MAXIMUM unconditionally to ensure this
515*404b540aSrobert does not bite us. */
516*404b540aSrobert if (file->st.st_size > INTTYPE_MAXIMUM (ssize_t))
517*404b540aSrobert {
518*404b540aSrobert cpp_error (pfile, CPP_DL_ERROR, "%s is too large", file->path);
519*404b540aSrobert return false;
520*404b540aSrobert }
521*404b540aSrobert
522*404b540aSrobert size = file->st.st_size;
523*404b540aSrobert }
524*404b540aSrobert else
525*404b540aSrobert /* 8 kilobytes is a sensible starting size. It ought to be bigger
526*404b540aSrobert than the kernel pipe buffer, and it's definitely bigger than
527*404b540aSrobert the majority of C source files. */
528*404b540aSrobert size = 8 * 1024;
529*404b540aSrobert
530*404b540aSrobert buf = XNEWVEC (uchar, size + 1);
531*404b540aSrobert total = 0;
532*404b540aSrobert while ((count = read (file->fd, buf + total, size - total)) > 0)
533*404b540aSrobert {
534*404b540aSrobert total += count;
535*404b540aSrobert
536*404b540aSrobert if (total == size)
537*404b540aSrobert {
538*404b540aSrobert if (regular)
539*404b540aSrobert break;
540*404b540aSrobert size *= 2;
541*404b540aSrobert buf = XRESIZEVEC (uchar, buf, size + 1);
542*404b540aSrobert }
543*404b540aSrobert }
544*404b540aSrobert
545*404b540aSrobert if (count < 0)
546*404b540aSrobert {
547*404b540aSrobert cpp_errno (pfile, CPP_DL_ERROR, file->path);
548*404b540aSrobert return false;
549*404b540aSrobert }
550*404b540aSrobert
551*404b540aSrobert if (regular && total != size && STAT_SIZE_RELIABLE (file->st))
552*404b540aSrobert cpp_error (pfile, CPP_DL_WARNING,
553*404b540aSrobert "%s is shorter than expected", file->path);
554*404b540aSrobert
555*404b540aSrobert file->buffer = _cpp_convert_input (pfile, CPP_OPTION (pfile, input_charset),
556*404b540aSrobert buf, size, total, &file->st.st_size);
557*404b540aSrobert file->buffer_valid = true;
558*404b540aSrobert
559*404b540aSrobert return true;
560*404b540aSrobert }
561*404b540aSrobert
562*404b540aSrobert /* Convenience wrapper around read_file_guts that opens the file if
563*404b540aSrobert necessary and closes the file descriptor after reading. FILE must
564*404b540aSrobert have been passed through find_file() at some stage. */
565*404b540aSrobert static bool
read_file(cpp_reader * pfile,_cpp_file * file)566*404b540aSrobert read_file (cpp_reader *pfile, _cpp_file *file)
567*404b540aSrobert {
568*404b540aSrobert /* If we already have its contents in memory, succeed immediately. */
569*404b540aSrobert if (file->buffer_valid)
570*404b540aSrobert return true;
571*404b540aSrobert
572*404b540aSrobert /* If an earlier read failed for some reason don't try again. */
573*404b540aSrobert if (file->dont_read || file->err_no)
574*404b540aSrobert return false;
575*404b540aSrobert
576*404b540aSrobert if (file->fd == -1 && !open_file (file))
577*404b540aSrobert {
578*404b540aSrobert open_file_failed (pfile, file, 0);
579*404b540aSrobert return false;
580*404b540aSrobert }
581*404b540aSrobert
582*404b540aSrobert file->dont_read = !read_file_guts (pfile, file);
583*404b540aSrobert close (file->fd);
584*404b540aSrobert file->fd = -1;
585*404b540aSrobert
586*404b540aSrobert return !file->dont_read;
587*404b540aSrobert }
588*404b540aSrobert
589*404b540aSrobert /* Returns TRUE if FILE's contents have been successfully placed in
590*404b540aSrobert FILE->buffer and the file should be stacked, otherwise false. */
591*404b540aSrobert static bool
should_stack_file(cpp_reader * pfile,_cpp_file * file,bool import)592*404b540aSrobert should_stack_file (cpp_reader *pfile, _cpp_file *file, bool import)
593*404b540aSrobert {
594*404b540aSrobert _cpp_file *f;
595*404b540aSrobert
596*404b540aSrobert /* Skip once-only files. */
597*404b540aSrobert if (file->once_only)
598*404b540aSrobert return false;
599*404b540aSrobert
600*404b540aSrobert /* We must mark the file once-only if #import now, before header
601*404b540aSrobert guard checks. Otherwise, undefining the header guard might
602*404b540aSrobert cause the file to be re-stacked. */
603*404b540aSrobert if (import)
604*404b540aSrobert {
605*404b540aSrobert _cpp_mark_file_once_only (pfile, file);
606*404b540aSrobert
607*404b540aSrobert /* Don't stack files that have been stacked before. */
608*404b540aSrobert if (file->stack_count)
609*404b540aSrobert return false;
610*404b540aSrobert }
611*404b540aSrobert
612*404b540aSrobert /* Skip if the file had a header guard and the macro is defined.
613*404b540aSrobert PCH relies on this appearing before the PCH handler below. */
614*404b540aSrobert if (file->cmacro && file->cmacro->type == NT_MACRO)
615*404b540aSrobert return false;
616*404b540aSrobert
617*404b540aSrobert /* Handle PCH files immediately; don't stack them. */
618*404b540aSrobert if (file->pch)
619*404b540aSrobert {
620*404b540aSrobert pfile->cb.read_pch (pfile, file->pchname, file->fd, file->path);
621*404b540aSrobert close (file->fd);
622*404b540aSrobert file->fd = -1;
623*404b540aSrobert return false;
624*404b540aSrobert }
625*404b540aSrobert
626*404b540aSrobert if (!read_file (pfile, file))
627*404b540aSrobert return false;
628*404b540aSrobert
629*404b540aSrobert /* Check the file against the PCH file. This is done before
630*404b540aSrobert checking against files we've already seen, since it may save on
631*404b540aSrobert I/O. */
632*404b540aSrobert if (check_file_against_entries (pfile, file, import))
633*404b540aSrobert {
634*404b540aSrobert /* If this isn't a #import, but yet we can't include the file,
635*404b540aSrobert that means that it was #import-ed in the PCH file,
636*404b540aSrobert so we can never include it again. */
637*404b540aSrobert if (! import)
638*404b540aSrobert _cpp_mark_file_once_only (pfile, file);
639*404b540aSrobert return false;
640*404b540aSrobert }
641*404b540aSrobert
642*404b540aSrobert /* Now we've read the file's contents, we can stack it if there
643*404b540aSrobert are no once-only files. */
644*404b540aSrobert if (!pfile->seen_once_only)
645*404b540aSrobert return true;
646*404b540aSrobert
647*404b540aSrobert /* We may have read the file under a different name. Look
648*404b540aSrobert for likely candidates and compare file contents to be sure. */
649*404b540aSrobert for (f = pfile->all_files; f; f = f->next_file)
650*404b540aSrobert {
651*404b540aSrobert if (f == file)
652*404b540aSrobert continue;
653*404b540aSrobert
654*404b540aSrobert if ((import || f->once_only)
655*404b540aSrobert && f->err_no == 0
656*404b540aSrobert && f->st.st_mtime == file->st.st_mtime
657*404b540aSrobert && f->st.st_size == file->st.st_size)
658*404b540aSrobert {
659*404b540aSrobert _cpp_file *ref_file;
660*404b540aSrobert bool same_file_p = false;
661*404b540aSrobert
662*404b540aSrobert if (f->buffer && !f->buffer_valid)
663*404b540aSrobert {
664*404b540aSrobert /* We already have a buffer but it is not valid, because
665*404b540aSrobert the file is still stacked. Make a new one. */
666*404b540aSrobert ref_file = make_cpp_file (pfile, f->dir, f->name);
667*404b540aSrobert ref_file->path = f->path;
668*404b540aSrobert }
669*404b540aSrobert else
670*404b540aSrobert /* The file is not stacked anymore. We can reuse it. */
671*404b540aSrobert ref_file = f;
672*404b540aSrobert
673*404b540aSrobert same_file_p = read_file (pfile, ref_file)
674*404b540aSrobert /* Size might have changed in read_file(). */
675*404b540aSrobert && ref_file->st.st_size == file->st.st_size
676*404b540aSrobert && !memcmp (ref_file->buffer,
677*404b540aSrobert file->buffer,
678*404b540aSrobert file->st.st_size);
679*404b540aSrobert
680*404b540aSrobert if (f->buffer && !f->buffer_valid)
681*404b540aSrobert {
682*404b540aSrobert ref_file->path = 0;
683*404b540aSrobert destroy_cpp_file (ref_file);
684*404b540aSrobert }
685*404b540aSrobert
686*404b540aSrobert if (same_file_p)
687*404b540aSrobert break;
688*404b540aSrobert }
689*404b540aSrobert }
690*404b540aSrobert
691*404b540aSrobert return f == NULL;
692*404b540aSrobert }
693*404b540aSrobert
694*404b540aSrobert /* Place the file referenced by FILE into a new buffer on the buffer
695*404b540aSrobert stack if possible. IMPORT is true if this stacking attempt is
696*404b540aSrobert because of a #import directive. Returns true if a buffer is
697*404b540aSrobert stacked. */
698*404b540aSrobert bool
_cpp_stack_file(cpp_reader * pfile,_cpp_file * file,bool import)699*404b540aSrobert _cpp_stack_file (cpp_reader *pfile, _cpp_file *file, bool import)
700*404b540aSrobert {
701*404b540aSrobert cpp_buffer *buffer;
702*404b540aSrobert int sysp;
703*404b540aSrobert
704*404b540aSrobert if (!should_stack_file (pfile, file, import))
705*404b540aSrobert return false;
706*404b540aSrobert
707*404b540aSrobert if (pfile->buffer == NULL || file->dir == NULL)
708*404b540aSrobert sysp = 0;
709*404b540aSrobert else
710*404b540aSrobert sysp = MAX (pfile->buffer->sysp, file->dir->sysp);
711*404b540aSrobert
712*404b540aSrobert /* Add the file to the dependencies on its first inclusion. */
713*404b540aSrobert if (CPP_OPTION (pfile, deps.style) > !!sysp && !file->stack_count)
714*404b540aSrobert {
715*404b540aSrobert if (!file->main_file || !CPP_OPTION (pfile, deps.ignore_main_file))
716*404b540aSrobert deps_add_dep (pfile->deps, file->path);
717*404b540aSrobert }
718*404b540aSrobert
719*404b540aSrobert /* Clear buffer_valid since _cpp_clean_line messes it up. */
720*404b540aSrobert file->buffer_valid = false;
721*404b540aSrobert file->stack_count++;
722*404b540aSrobert
723*404b540aSrobert /* Stack the buffer. */
724*404b540aSrobert buffer = cpp_push_buffer (pfile, file->buffer, file->st.st_size,
725*404b540aSrobert CPP_OPTION (pfile, preprocessed));
726*404b540aSrobert buffer->file = file;
727*404b540aSrobert buffer->sysp = sysp;
728*404b540aSrobert
729*404b540aSrobert /* Initialize controlling macro state. */
730*404b540aSrobert pfile->mi_valid = true;
731*404b540aSrobert pfile->mi_cmacro = 0;
732*404b540aSrobert
733*404b540aSrobert /* Generate the call back. */
734*404b540aSrobert _cpp_do_file_change (pfile, LC_ENTER, file->path, 1, sysp);
735*404b540aSrobert
736*404b540aSrobert return true;
737*404b540aSrobert }
738*404b540aSrobert
739*404b540aSrobert /* Mark FILE to be included once only. */
740*404b540aSrobert void
_cpp_mark_file_once_only(cpp_reader * pfile,_cpp_file * file)741*404b540aSrobert _cpp_mark_file_once_only (cpp_reader *pfile, _cpp_file *file)
742*404b540aSrobert {
743*404b540aSrobert pfile->seen_once_only = true;
744*404b540aSrobert file->once_only = true;
745*404b540aSrobert }
746*404b540aSrobert
747*404b540aSrobert /* Return the directory from which searching for FNAME should start,
748*404b540aSrobert considering the directive TYPE and ANGLE_BRACKETS. If there is
749*404b540aSrobert nothing left in the path, returns NULL. */
750*404b540aSrobert static struct cpp_dir *
search_path_head(cpp_reader * pfile,const char * fname,int angle_brackets,enum include_type type)751*404b540aSrobert search_path_head (cpp_reader *pfile, const char *fname, int angle_brackets,
752*404b540aSrobert enum include_type type)
753*404b540aSrobert {
754*404b540aSrobert cpp_dir *dir;
755*404b540aSrobert _cpp_file *file;
756*404b540aSrobert
757*404b540aSrobert if (IS_ABSOLUTE_PATH (fname))
758*404b540aSrobert return &pfile->no_search_path;
759*404b540aSrobert
760*404b540aSrobert /* pfile->buffer is NULL when processing an -include command-line flag. */
761*404b540aSrobert file = pfile->buffer == NULL ? pfile->main_file : pfile->buffer->file;
762*404b540aSrobert
763*404b540aSrobert /* For #include_next, skip in the search path past the dir in which
764*404b540aSrobert the current file was found, but if it was found via an absolute
765*404b540aSrobert path use the normal search logic. */
766*404b540aSrobert if (type == IT_INCLUDE_NEXT && file->dir)
767*404b540aSrobert dir = file->dir->next;
768*404b540aSrobert else if (angle_brackets)
769*404b540aSrobert dir = pfile->bracket_include;
770*404b540aSrobert else if (type == IT_CMDLINE)
771*404b540aSrobert /* -include and -imacros use the #include "" chain with the
772*404b540aSrobert preprocessor's cwd prepended. */
773*404b540aSrobert return make_cpp_dir (pfile, "./", false);
774*404b540aSrobert else if (pfile->quote_ignores_source_dir)
775*404b540aSrobert dir = pfile->quote_include;
776*404b540aSrobert else
777*404b540aSrobert return make_cpp_dir (pfile, dir_name_of_file (file),
778*404b540aSrobert pfile->buffer ? pfile->buffer->sysp : 0);
779*404b540aSrobert
780*404b540aSrobert if (dir == NULL)
781*404b540aSrobert cpp_error (pfile, CPP_DL_ERROR,
782*404b540aSrobert "no include path in which to search for %s", fname);
783*404b540aSrobert
784*404b540aSrobert return dir;
785*404b540aSrobert }
786*404b540aSrobert
787*404b540aSrobert /* Strip the basename from the file's path. It ends with a slash if
788*404b540aSrobert of nonzero length. Note that this procedure also works for
789*404b540aSrobert <stdin>, which is represented by the empty string. */
790*404b540aSrobert static const char *
dir_name_of_file(_cpp_file * file)791*404b540aSrobert dir_name_of_file (_cpp_file *file)
792*404b540aSrobert {
793*404b540aSrobert if (!file->dir_name)
794*404b540aSrobert {
795*404b540aSrobert size_t len = lbasename (file->path) - file->path;
796*404b540aSrobert char *dir_name = XNEWVEC (char, len + 1);
797*404b540aSrobert
798*404b540aSrobert memcpy (dir_name, file->path, len);
799*404b540aSrobert dir_name[len] = '\0';
800*404b540aSrobert file->dir_name = dir_name;
801*404b540aSrobert }
802*404b540aSrobert
803*404b540aSrobert return file->dir_name;
804*404b540aSrobert }
805*404b540aSrobert
806*404b540aSrobert /* Handles #include-family directives (distinguished by TYPE),
807*404b540aSrobert including HEADER, and the command line -imacros and -include.
808*404b540aSrobert Returns true if a buffer was stacked. */
809*404b540aSrobert bool
_cpp_stack_include(cpp_reader * pfile,const char * fname,int angle_brackets,enum include_type type)810*404b540aSrobert _cpp_stack_include (cpp_reader *pfile, const char *fname, int angle_brackets,
811*404b540aSrobert enum include_type type)
812*404b540aSrobert {
813*404b540aSrobert struct cpp_dir *dir;
814*404b540aSrobert _cpp_file *file;
815*404b540aSrobert
816*404b540aSrobert dir = search_path_head (pfile, fname, angle_brackets, type);
817*404b540aSrobert if (!dir)
818*404b540aSrobert return false;
819*404b540aSrobert
820*404b540aSrobert file = _cpp_find_file (pfile, fname, dir, false, angle_brackets);
821*404b540aSrobert
822*404b540aSrobert /* Compensate for the increment in linemap_add. In the case of a
823*404b540aSrobert normal #include, we're currently at the start of the line
824*404b540aSrobert *following* the #include. A separate source_location for this
825*404b540aSrobert location makes no sense (until we do the LC_LEAVE), and
826*404b540aSrobert complicates LAST_SOURCE_LINE_LOCATION. This does not apply if we
827*404b540aSrobert found a PCH file (in which case linemap_add is not called) or we
828*404b540aSrobert were included from the command-line. */
829*404b540aSrobert if (! file->pch && file->err_no == 0 && type != IT_CMDLINE)
830*404b540aSrobert pfile->line_table->highest_location--;
831*404b540aSrobert
832*404b540aSrobert return _cpp_stack_file (pfile, file, type == IT_IMPORT);
833*404b540aSrobert }
834*404b540aSrobert
835*404b540aSrobert /* Could not open FILE. The complication is dependency output. */
836*404b540aSrobert static void
open_file_failed(cpp_reader * pfile,_cpp_file * file,int angle_brackets)837*404b540aSrobert open_file_failed (cpp_reader *pfile, _cpp_file *file, int angle_brackets)
838*404b540aSrobert {
839*404b540aSrobert int sysp = pfile->line_table->highest_line > 1 && pfile->buffer ? pfile->buffer->sysp : 0;
840*404b540aSrobert bool print_dep = CPP_OPTION (pfile, deps.style) > (angle_brackets || !!sysp);
841*404b540aSrobert
842*404b540aSrobert errno = file->err_no;
843*404b540aSrobert if (print_dep && CPP_OPTION (pfile, deps.missing_files) && errno == ENOENT)
844*404b540aSrobert deps_add_dep (pfile->deps, file->name);
845*404b540aSrobert else
846*404b540aSrobert {
847*404b540aSrobert /* If we are outputting dependencies but not for this file then
848*404b540aSrobert don't error because we can still produce correct output. */
849*404b540aSrobert if (CPP_OPTION (pfile, deps.style) && ! print_dep)
850*404b540aSrobert cpp_errno (pfile, CPP_DL_WARNING, file->path);
851*404b540aSrobert else
852*404b540aSrobert cpp_errno (pfile, CPP_DL_ERROR, file->path);
853*404b540aSrobert }
854*404b540aSrobert }
855*404b540aSrobert
856*404b540aSrobert /* Search in the chain beginning at HEAD for a file whose search path
857*404b540aSrobert started at START_DIR != NULL. */
858*404b540aSrobert static struct file_hash_entry *
search_cache(struct file_hash_entry * head,const cpp_dir * start_dir)859*404b540aSrobert search_cache (struct file_hash_entry *head, const cpp_dir *start_dir)
860*404b540aSrobert {
861*404b540aSrobert while (head && head->start_dir != start_dir)
862*404b540aSrobert head = head->next;
863*404b540aSrobert
864*404b540aSrobert return head;
865*404b540aSrobert }
866*404b540aSrobert
867*404b540aSrobert /* Allocate a new _cpp_file structure. */
868*404b540aSrobert static _cpp_file *
make_cpp_file(cpp_reader * pfile,cpp_dir * dir,const char * fname)869*404b540aSrobert make_cpp_file (cpp_reader *pfile, cpp_dir *dir, const char *fname)
870*404b540aSrobert {
871*404b540aSrobert _cpp_file *file;
872*404b540aSrobert
873*404b540aSrobert file = XCNEW (_cpp_file);
874*404b540aSrobert file->main_file = !pfile->buffer;
875*404b540aSrobert file->fd = -1;
876*404b540aSrobert file->dir = dir;
877*404b540aSrobert file->name = xstrdup (fname);
878*404b540aSrobert
879*404b540aSrobert return file;
880*404b540aSrobert }
881*404b540aSrobert
882*404b540aSrobert /* Release a _cpp_file structure. */
883*404b540aSrobert static void
destroy_cpp_file(_cpp_file * file)884*404b540aSrobert destroy_cpp_file (_cpp_file *file)
885*404b540aSrobert {
886*404b540aSrobert if (file->buffer)
887*404b540aSrobert free ((void *) file->buffer);
888*404b540aSrobert free ((void *) file->name);
889*404b540aSrobert free (file);
890*404b540aSrobert }
891*404b540aSrobert
892*404b540aSrobert /* A hash of directory names. The directory names are the path names
893*404b540aSrobert of files which contain a #include "", the included file name is
894*404b540aSrobert appended to this directories.
895*404b540aSrobert
896*404b540aSrobert To avoid duplicate entries we follow the convention that all
897*404b540aSrobert non-empty directory names should end in a '/'. DIR_NAME must be
898*404b540aSrobert stored in permanently allocated memory. */
899*404b540aSrobert static cpp_dir *
make_cpp_dir(cpp_reader * pfile,const char * dir_name,int sysp)900*404b540aSrobert make_cpp_dir (cpp_reader *pfile, const char *dir_name, int sysp)
901*404b540aSrobert {
902*404b540aSrobert struct file_hash_entry *entry, **hash_slot;
903*404b540aSrobert cpp_dir *dir;
904*404b540aSrobert
905*404b540aSrobert hash_slot = (struct file_hash_entry **)
906*404b540aSrobert htab_find_slot_with_hash (pfile->dir_hash, dir_name,
907*404b540aSrobert htab_hash_string (dir_name),
908*404b540aSrobert INSERT);
909*404b540aSrobert
910*404b540aSrobert /* Have we already hashed this directory? */
911*404b540aSrobert for (entry = *hash_slot; entry; entry = entry->next)
912*404b540aSrobert if (entry->start_dir == NULL)
913*404b540aSrobert return entry->u.dir;
914*404b540aSrobert
915*404b540aSrobert dir = XCNEW (cpp_dir);
916*404b540aSrobert dir->next = pfile->quote_include;
917*404b540aSrobert dir->name = (char *) dir_name;
918*404b540aSrobert dir->len = strlen (dir_name);
919*404b540aSrobert dir->sysp = sysp;
920*404b540aSrobert dir->construct = 0;
921*404b540aSrobert
922*404b540aSrobert /* Store this new result in the hash table. */
923*404b540aSrobert entry = new_file_hash_entry (pfile);
924*404b540aSrobert entry->next = *hash_slot;
925*404b540aSrobert entry->start_dir = NULL;
926*404b540aSrobert entry->u.dir = dir;
927*404b540aSrobert *hash_slot = entry;
928*404b540aSrobert
929*404b540aSrobert return dir;
930*404b540aSrobert }
931*404b540aSrobert
932*404b540aSrobert /* Create a new block of memory for file hash entries. */
933*404b540aSrobert static void
allocate_file_hash_entries(cpp_reader * pfile)934*404b540aSrobert allocate_file_hash_entries (cpp_reader *pfile)
935*404b540aSrobert {
936*404b540aSrobert pfile->file_hash_entries_used = 0;
937*404b540aSrobert pfile->file_hash_entries_allocated = 127;
938*404b540aSrobert pfile->file_hash_entries = XNEWVEC (struct file_hash_entry,
939*404b540aSrobert pfile->file_hash_entries_allocated);
940*404b540aSrobert }
941*404b540aSrobert
942*404b540aSrobert /* Return a new file hash entry. */
943*404b540aSrobert static struct file_hash_entry *
new_file_hash_entry(cpp_reader * pfile)944*404b540aSrobert new_file_hash_entry (cpp_reader *pfile)
945*404b540aSrobert {
946*404b540aSrobert if (pfile->file_hash_entries_used == pfile->file_hash_entries_allocated)
947*404b540aSrobert allocate_file_hash_entries (pfile);
948*404b540aSrobert
949*404b540aSrobert return &pfile->file_hash_entries[pfile->file_hash_entries_used++];
950*404b540aSrobert }
951*404b540aSrobert
952*404b540aSrobert /* Returns TRUE if a file FNAME has ever been successfully opened.
953*404b540aSrobert This routine is not intended to correctly handle filenames aliased
954*404b540aSrobert by links or redundant . or .. traversals etc. */
955*404b540aSrobert bool
cpp_included(cpp_reader * pfile,const char * fname)956*404b540aSrobert cpp_included (cpp_reader *pfile, const char *fname)
957*404b540aSrobert {
958*404b540aSrobert struct file_hash_entry *entry;
959*404b540aSrobert
960*404b540aSrobert entry = (struct file_hash_entry *)
961*404b540aSrobert htab_find_with_hash (pfile->file_hash, fname, htab_hash_string (fname));
962*404b540aSrobert
963*404b540aSrobert while (entry && (entry->start_dir == NULL || entry->u.file->err_no))
964*404b540aSrobert entry = entry->next;
965*404b540aSrobert
966*404b540aSrobert return entry != NULL;
967*404b540aSrobert }
968*404b540aSrobert
969*404b540aSrobert /* Calculate the hash value of a file hash entry P. */
970*404b540aSrobert
971*404b540aSrobert static hashval_t
file_hash_hash(const void * p)972*404b540aSrobert file_hash_hash (const void *p)
973*404b540aSrobert {
974*404b540aSrobert struct file_hash_entry *entry = (struct file_hash_entry *) p;
975*404b540aSrobert const char *hname;
976*404b540aSrobert if (entry->start_dir)
977*404b540aSrobert hname = entry->u.file->name;
978*404b540aSrobert else
979*404b540aSrobert hname = entry->u.dir->name;
980*404b540aSrobert
981*404b540aSrobert return htab_hash_string (hname);
982*404b540aSrobert }
983*404b540aSrobert
984*404b540aSrobert /* Compare a string Q against a file hash entry P. */
985*404b540aSrobert static int
file_hash_eq(const void * p,const void * q)986*404b540aSrobert file_hash_eq (const void *p, const void *q)
987*404b540aSrobert {
988*404b540aSrobert struct file_hash_entry *entry = (struct file_hash_entry *) p;
989*404b540aSrobert const char *fname = (const char *) q;
990*404b540aSrobert const char *hname;
991*404b540aSrobert
992*404b540aSrobert if (entry->start_dir)
993*404b540aSrobert hname = entry->u.file->name;
994*404b540aSrobert else
995*404b540aSrobert hname = entry->u.dir->name;
996*404b540aSrobert
997*404b540aSrobert return strcmp (hname, fname) == 0;
998*404b540aSrobert }
999*404b540aSrobert
1000*404b540aSrobert /* Initialize everything in this source file. */
1001*404b540aSrobert void
_cpp_init_files(cpp_reader * pfile)1002*404b540aSrobert _cpp_init_files (cpp_reader *pfile)
1003*404b540aSrobert {
1004*404b540aSrobert pfile->file_hash = htab_create_alloc (127, file_hash_hash, file_hash_eq,
1005*404b540aSrobert NULL, xcalloc, free);
1006*404b540aSrobert pfile->dir_hash = htab_create_alloc (127, file_hash_hash, file_hash_eq,
1007*404b540aSrobert NULL, xcalloc, free);
1008*404b540aSrobert allocate_file_hash_entries (pfile);
1009*404b540aSrobert }
1010*404b540aSrobert
1011*404b540aSrobert /* Finalize everything in this source file. */
1012*404b540aSrobert void
_cpp_cleanup_files(cpp_reader * pfile)1013*404b540aSrobert _cpp_cleanup_files (cpp_reader *pfile)
1014*404b540aSrobert {
1015*404b540aSrobert htab_delete (pfile->file_hash);
1016*404b540aSrobert htab_delete (pfile->dir_hash);
1017*404b540aSrobert }
1018*404b540aSrobert
1019*404b540aSrobert /* Enter a file name in the hash for the sake of cpp_included. */
1020*404b540aSrobert void
_cpp_fake_include(cpp_reader * pfile,const char * fname)1021*404b540aSrobert _cpp_fake_include (cpp_reader *pfile, const char *fname)
1022*404b540aSrobert {
1023*404b540aSrobert _cpp_find_file (pfile, fname, pfile->buffer->file->dir, true, 0);
1024*404b540aSrobert }
1025*404b540aSrobert
1026*404b540aSrobert /* Not everyone who wants to set system-header-ness on a buffer can
1027*404b540aSrobert see the details of a buffer. This is an exported interface because
1028*404b540aSrobert fix-header needs it. */
1029*404b540aSrobert void
cpp_make_system_header(cpp_reader * pfile,int syshdr,int externc)1030*404b540aSrobert cpp_make_system_header (cpp_reader *pfile, int syshdr, int externc)
1031*404b540aSrobert {
1032*404b540aSrobert int flags = 0;
1033*404b540aSrobert const struct line_maps *line_table = pfile->line_table;
1034*404b540aSrobert const struct line_map *map = &line_table->maps[line_table->used-1];
1035*404b540aSrobert
1036*404b540aSrobert /* 1 = system header, 2 = system header to be treated as C. */
1037*404b540aSrobert if (syshdr)
1038*404b540aSrobert flags = 1 + (externc != 0);
1039*404b540aSrobert pfile->buffer->sysp = flags;
1040*404b540aSrobert _cpp_do_file_change (pfile, LC_RENAME, map->to_file,
1041*404b540aSrobert SOURCE_LINE (map, pfile->line_table->highest_line), flags);
1042*404b540aSrobert }
1043*404b540aSrobert
1044*404b540aSrobert /* Allow the client to change the current file. Used by the front end
1045*404b540aSrobert to achieve pseudo-file names like <built-in>.
1046*404b540aSrobert If REASON is LC_LEAVE, then NEW_NAME must be NULL. */
1047*404b540aSrobert void
cpp_change_file(cpp_reader * pfile,enum lc_reason reason,const char * new_name)1048*404b540aSrobert cpp_change_file (cpp_reader *pfile, enum lc_reason reason,
1049*404b540aSrobert const char *new_name)
1050*404b540aSrobert {
1051*404b540aSrobert _cpp_do_file_change (pfile, reason, new_name, 1, 0);
1052*404b540aSrobert }
1053*404b540aSrobert
1054*404b540aSrobert /* Callback function for htab_traverse. */
1055*404b540aSrobert static int
report_missing_guard(void ** slot,void * b)1056*404b540aSrobert report_missing_guard (void **slot, void *b)
1057*404b540aSrobert {
1058*404b540aSrobert struct file_hash_entry *entry = (struct file_hash_entry *) *slot;
1059*404b540aSrobert int *bannerp = (int *) b;
1060*404b540aSrobert
1061*404b540aSrobert /* Skip directories. */
1062*404b540aSrobert if (entry->start_dir != NULL)
1063*404b540aSrobert {
1064*404b540aSrobert _cpp_file *file = entry->u.file;
1065*404b540aSrobert
1066*404b540aSrobert /* We don't want MI guard advice for the main file. */
1067*404b540aSrobert if (file->cmacro == NULL && file->stack_count == 1 && !file->main_file)
1068*404b540aSrobert {
1069*404b540aSrobert if (*bannerp == 0)
1070*404b540aSrobert {
1071*404b540aSrobert fputs (_("Multiple include guards may be useful for:\n"),
1072*404b540aSrobert stderr);
1073*404b540aSrobert *bannerp = 1;
1074*404b540aSrobert }
1075*404b540aSrobert
1076*404b540aSrobert fputs (entry->u.file->path, stderr);
1077*404b540aSrobert putc ('\n', stderr);
1078*404b540aSrobert }
1079*404b540aSrobert }
1080*404b540aSrobert
1081*404b540aSrobert return 0;
1082*404b540aSrobert }
1083*404b540aSrobert
1084*404b540aSrobert /* Report on all files that might benefit from a multiple include guard.
1085*404b540aSrobert Triggered by -H. */
1086*404b540aSrobert void
_cpp_report_missing_guards(cpp_reader * pfile)1087*404b540aSrobert _cpp_report_missing_guards (cpp_reader *pfile)
1088*404b540aSrobert {
1089*404b540aSrobert int banner = 0;
1090*404b540aSrobert
1091*404b540aSrobert htab_traverse (pfile->file_hash, report_missing_guard, &banner);
1092*404b540aSrobert }
1093*404b540aSrobert
1094*404b540aSrobert /* Locate HEADER, and determine whether it is newer than the current
1095*404b540aSrobert file. If it cannot be located or dated, return -1, if it is
1096*404b540aSrobert newer, return 1, otherwise 0. */
1097*404b540aSrobert int
_cpp_compare_file_date(cpp_reader * pfile,const char * fname,int angle_brackets)1098*404b540aSrobert _cpp_compare_file_date (cpp_reader *pfile, const char *fname,
1099*404b540aSrobert int angle_brackets)
1100*404b540aSrobert {
1101*404b540aSrobert _cpp_file *file;
1102*404b540aSrobert struct cpp_dir *dir;
1103*404b540aSrobert
1104*404b540aSrobert dir = search_path_head (pfile, fname, angle_brackets, IT_INCLUDE);
1105*404b540aSrobert if (!dir)
1106*404b540aSrobert return -1;
1107*404b540aSrobert
1108*404b540aSrobert file = _cpp_find_file (pfile, fname, dir, false, angle_brackets);
1109*404b540aSrobert if (file->err_no)
1110*404b540aSrobert return -1;
1111*404b540aSrobert
1112*404b540aSrobert if (file->fd != -1)
1113*404b540aSrobert {
1114*404b540aSrobert close (file->fd);
1115*404b540aSrobert file->fd = -1;
1116*404b540aSrobert }
1117*404b540aSrobert
1118*404b540aSrobert return file->st.st_mtime > pfile->buffer->file->st.st_mtime;
1119*404b540aSrobert }
1120*404b540aSrobert
1121*404b540aSrobert /* Pushes the given file onto the buffer stack. Returns nonzero if
1122*404b540aSrobert successful. */
1123*404b540aSrobert bool
cpp_push_include(cpp_reader * pfile,const char * fname)1124*404b540aSrobert cpp_push_include (cpp_reader *pfile, const char *fname)
1125*404b540aSrobert {
1126*404b540aSrobert return _cpp_stack_include (pfile, fname, false, IT_CMDLINE);
1127*404b540aSrobert }
1128*404b540aSrobert
1129*404b540aSrobert /* Do appropriate cleanup when a file INC's buffer is popped off the
1130*404b540aSrobert input stack. */
1131*404b540aSrobert void
_cpp_pop_file_buffer(cpp_reader * pfile,_cpp_file * file)1132*404b540aSrobert _cpp_pop_file_buffer (cpp_reader *pfile, _cpp_file *file)
1133*404b540aSrobert {
1134*404b540aSrobert /* Record the inclusion-preventing macro, which could be NULL
1135*404b540aSrobert meaning no controlling macro. */
1136*404b540aSrobert if (pfile->mi_valid && file->cmacro == NULL)
1137*404b540aSrobert file->cmacro = pfile->mi_cmacro;
1138*404b540aSrobert
1139*404b540aSrobert /* Invalidate control macros in the #including file. */
1140*404b540aSrobert pfile->mi_valid = false;
1141*404b540aSrobert
1142*404b540aSrobert if (file->buffer)
1143*404b540aSrobert {
1144*404b540aSrobert free ((void *) file->buffer);
1145*404b540aSrobert file->buffer = NULL;
1146*404b540aSrobert file->buffer_valid = false;
1147*404b540aSrobert }
1148*404b540aSrobert }
1149*404b540aSrobert
1150*404b540aSrobert /* Inteface to file statistics record in _cpp_file structure. */
1151*404b540aSrobert struct stat *
_cpp_get_file_stat(_cpp_file * file)1152*404b540aSrobert _cpp_get_file_stat (_cpp_file *file)
1153*404b540aSrobert {
1154*404b540aSrobert return &file->st;
1155*404b540aSrobert }
1156*404b540aSrobert
1157*404b540aSrobert /* Set the include chain for "" to QUOTE, for <> to BRACKET. If
1158*404b540aSrobert QUOTE_IGNORES_SOURCE_DIR, then "" includes do not look in the
1159*404b540aSrobert directory of the including file.
1160*404b540aSrobert
1161*404b540aSrobert If BRACKET does not lie in the QUOTE chain, it is set to QUOTE. */
1162*404b540aSrobert void
cpp_set_include_chains(cpp_reader * pfile,cpp_dir * quote,cpp_dir * bracket,int quote_ignores_source_dir)1163*404b540aSrobert cpp_set_include_chains (cpp_reader *pfile, cpp_dir *quote, cpp_dir *bracket,
1164*404b540aSrobert int quote_ignores_source_dir)
1165*404b540aSrobert {
1166*404b540aSrobert pfile->quote_include = quote;
1167*404b540aSrobert pfile->bracket_include = quote;
1168*404b540aSrobert pfile->quote_ignores_source_dir = quote_ignores_source_dir;
1169*404b540aSrobert
1170*404b540aSrobert for (; quote; quote = quote->next)
1171*404b540aSrobert {
1172*404b540aSrobert quote->name_map = NULL;
1173*404b540aSrobert quote->len = strlen (quote->name);
1174*404b540aSrobert if (quote == bracket)
1175*404b540aSrobert pfile->bracket_include = bracket;
1176*404b540aSrobert }
1177*404b540aSrobert }
1178*404b540aSrobert
1179*404b540aSrobert /* Append the file name to the directory to create the path, but don't
1180*404b540aSrobert turn / into // or // into ///; // may be a namespace escape. */
1181*404b540aSrobert static char *
append_file_to_dir(const char * fname,cpp_dir * dir)1182*404b540aSrobert append_file_to_dir (const char *fname, cpp_dir *dir)
1183*404b540aSrobert {
1184*404b540aSrobert size_t dlen, flen;
1185*404b540aSrobert char *path;
1186*404b540aSrobert
1187*404b540aSrobert dlen = dir->len;
1188*404b540aSrobert flen = strlen (fname);
1189*404b540aSrobert path = XNEWVEC (char, dlen + 1 + flen + 1);
1190*404b540aSrobert memcpy (path, dir->name, dlen);
1191*404b540aSrobert if (dlen && path[dlen - 1] != '/')
1192*404b540aSrobert path[dlen++] = '/';
1193*404b540aSrobert memcpy (&path[dlen], fname, flen + 1);
1194*404b540aSrobert
1195*404b540aSrobert return path;
1196*404b540aSrobert }
1197*404b540aSrobert
1198*404b540aSrobert /* Read a space delimited string of unlimited length from a stdio
1199*404b540aSrobert file F. */
1200*404b540aSrobert static char *
read_filename_string(int ch,FILE * f)1201*404b540aSrobert read_filename_string (int ch, FILE *f)
1202*404b540aSrobert {
1203*404b540aSrobert char *alloc, *set;
1204*404b540aSrobert int len;
1205*404b540aSrobert
1206*404b540aSrobert len = 20;
1207*404b540aSrobert set = alloc = XNEWVEC (char, len + 1);
1208*404b540aSrobert if (! is_space (ch))
1209*404b540aSrobert {
1210*404b540aSrobert *set++ = ch;
1211*404b540aSrobert while ((ch = getc (f)) != EOF && ! is_space (ch))
1212*404b540aSrobert {
1213*404b540aSrobert if (set - alloc == len)
1214*404b540aSrobert {
1215*404b540aSrobert len *= 2;
1216*404b540aSrobert alloc = XRESIZEVEC (char, alloc, len + 1);
1217*404b540aSrobert set = alloc + len / 2;
1218*404b540aSrobert }
1219*404b540aSrobert *set++ = ch;
1220*404b540aSrobert }
1221*404b540aSrobert }
1222*404b540aSrobert *set = '\0';
1223*404b540aSrobert ungetc (ch, f);
1224*404b540aSrobert return alloc;
1225*404b540aSrobert }
1226*404b540aSrobert
1227*404b540aSrobert /* Read the file name map file for DIR. */
1228*404b540aSrobert static void
read_name_map(cpp_dir * dir)1229*404b540aSrobert read_name_map (cpp_dir *dir)
1230*404b540aSrobert {
1231*404b540aSrobert static const char FILE_NAME_MAP_FILE[] = "header.gcc";
1232*404b540aSrobert char *name;
1233*404b540aSrobert FILE *f;
1234*404b540aSrobert size_t len, count = 0, room = 9;
1235*404b540aSrobert
1236*404b540aSrobert len = dir->len;
1237*404b540aSrobert name = (char *) alloca (len + sizeof (FILE_NAME_MAP_FILE) + 1);
1238*404b540aSrobert memcpy (name, dir->name, len);
1239*404b540aSrobert if (len && name[len - 1] != '/')
1240*404b540aSrobert name[len++] = '/';
1241*404b540aSrobert strcpy (name + len, FILE_NAME_MAP_FILE);
1242*404b540aSrobert f = fopen (name, "r");
1243*404b540aSrobert
1244*404b540aSrobert dir->name_map = XNEWVEC (const char *, room);
1245*404b540aSrobert
1246*404b540aSrobert /* Silently return NULL if we cannot open. */
1247*404b540aSrobert if (f)
1248*404b540aSrobert {
1249*404b540aSrobert int ch;
1250*404b540aSrobert
1251*404b540aSrobert while ((ch = getc (f)) != EOF)
1252*404b540aSrobert {
1253*404b540aSrobert char *to;
1254*404b540aSrobert
1255*404b540aSrobert if (is_space (ch))
1256*404b540aSrobert continue;
1257*404b540aSrobert
1258*404b540aSrobert if (count + 2 > room)
1259*404b540aSrobert {
1260*404b540aSrobert room += 8;
1261*404b540aSrobert dir->name_map = XRESIZEVEC (const char *, dir->name_map, room);
1262*404b540aSrobert }
1263*404b540aSrobert
1264*404b540aSrobert dir->name_map[count] = read_filename_string (ch, f);
1265*404b540aSrobert while ((ch = getc (f)) != EOF && is_hspace (ch))
1266*404b540aSrobert ;
1267*404b540aSrobert
1268*404b540aSrobert to = read_filename_string (ch, f);
1269*404b540aSrobert if (IS_ABSOLUTE_PATH (to))
1270*404b540aSrobert dir->name_map[count + 1] = to;
1271*404b540aSrobert else
1272*404b540aSrobert {
1273*404b540aSrobert dir->name_map[count + 1] = append_file_to_dir (to, dir);
1274*404b540aSrobert free (to);
1275*404b540aSrobert }
1276*404b540aSrobert
1277*404b540aSrobert count += 2;
1278*404b540aSrobert while ((ch = getc (f)) != '\n')
1279*404b540aSrobert if (ch == EOF)
1280*404b540aSrobert break;
1281*404b540aSrobert }
1282*404b540aSrobert
1283*404b540aSrobert fclose (f);
1284*404b540aSrobert }
1285*404b540aSrobert
1286*404b540aSrobert /* Terminate the list of maps. */
1287*404b540aSrobert dir->name_map[count] = NULL;
1288*404b540aSrobert }
1289*404b540aSrobert
1290*404b540aSrobert /* Remap a FILE's name based on the file_name_map, if any, for
1291*404b540aSrobert FILE->dir. If the file name has any directory separators,
1292*404b540aSrobert recursively check those directories too. */
1293*404b540aSrobert static char *
remap_filename(cpp_reader * pfile,_cpp_file * file)1294*404b540aSrobert remap_filename (cpp_reader *pfile, _cpp_file *file)
1295*404b540aSrobert {
1296*404b540aSrobert const char *fname, *p;
1297*404b540aSrobert char *new_dir;
1298*404b540aSrobert cpp_dir *dir;
1299*404b540aSrobert size_t index, len;
1300*404b540aSrobert
1301*404b540aSrobert dir = file->dir;
1302*404b540aSrobert fname = file->name;
1303*404b540aSrobert
1304*404b540aSrobert for (;;)
1305*404b540aSrobert {
1306*404b540aSrobert if (!dir->name_map)
1307*404b540aSrobert read_name_map (dir);
1308*404b540aSrobert
1309*404b540aSrobert for (index = 0; dir->name_map[index]; index += 2)
1310*404b540aSrobert if (!strcmp (dir->name_map[index], fname))
1311*404b540aSrobert return xstrdup (dir->name_map[index + 1]);
1312*404b540aSrobert
1313*404b540aSrobert p = strchr (fname, '/');
1314*404b540aSrobert if (!p || p == fname)
1315*404b540aSrobert return NULL;
1316*404b540aSrobert
1317*404b540aSrobert len = dir->len + (p - fname + 1);
1318*404b540aSrobert new_dir = XNEWVEC (char, len + 1);
1319*404b540aSrobert memcpy (new_dir, dir->name, dir->len);
1320*404b540aSrobert memcpy (new_dir + dir->len, fname, p - fname + 1);
1321*404b540aSrobert new_dir[len] = '\0';
1322*404b540aSrobert
1323*404b540aSrobert dir = make_cpp_dir (pfile, new_dir, dir->sysp);
1324*404b540aSrobert fname = p + 1;
1325*404b540aSrobert }
1326*404b540aSrobert }
1327*404b540aSrobert
1328*404b540aSrobert /* Returns true if PCHNAME is a valid PCH file for FILE. */
1329*404b540aSrobert static bool
validate_pch(cpp_reader * pfile,_cpp_file * file,const char * pchname)1330*404b540aSrobert validate_pch (cpp_reader *pfile, _cpp_file *file, const char *pchname)
1331*404b540aSrobert {
1332*404b540aSrobert const char *saved_path = file->path;
1333*404b540aSrobert bool valid = false;
1334*404b540aSrobert
1335*404b540aSrobert file->path = pchname;
1336*404b540aSrobert if (open_file (file))
1337*404b540aSrobert {
1338*404b540aSrobert valid = 1 & pfile->cb.valid_pch (pfile, pchname, file->fd);
1339*404b540aSrobert
1340*404b540aSrobert if (!valid)
1341*404b540aSrobert {
1342*404b540aSrobert close (file->fd);
1343*404b540aSrobert file->fd = -1;
1344*404b540aSrobert }
1345*404b540aSrobert
1346*404b540aSrobert if (CPP_OPTION (pfile, print_include_names))
1347*404b540aSrobert {
1348*404b540aSrobert unsigned int i;
1349*404b540aSrobert for (i = 1; i < pfile->line_table->depth; i++)
1350*404b540aSrobert putc ('.', stderr);
1351*404b540aSrobert fprintf (stderr, "%c %s\n",
1352*404b540aSrobert valid ? '!' : 'x', pchname);
1353*404b540aSrobert }
1354*404b540aSrobert }
1355*404b540aSrobert
1356*404b540aSrobert file->path = saved_path;
1357*404b540aSrobert return valid;
1358*404b540aSrobert }
1359*404b540aSrobert
1360*404b540aSrobert /* Get the path associated with the _cpp_file F. The path includes
1361*404b540aSrobert the base name from the include directive and the directory it was
1362*404b540aSrobert found in via the search path. */
1363*404b540aSrobert
1364*404b540aSrobert const char *
cpp_get_path(struct _cpp_file * f)1365*404b540aSrobert cpp_get_path (struct _cpp_file *f)
1366*404b540aSrobert {
1367*404b540aSrobert return f->path;
1368*404b540aSrobert }
1369*404b540aSrobert
1370*404b540aSrobert /* Get the directory associated with the _cpp_file F. */
1371*404b540aSrobert
1372*404b540aSrobert cpp_dir *
cpp_get_dir(struct _cpp_file * f)1373*404b540aSrobert cpp_get_dir (struct _cpp_file *f)
1374*404b540aSrobert {
1375*404b540aSrobert return f->dir;
1376*404b540aSrobert }
1377*404b540aSrobert
1378*404b540aSrobert /* Get the cpp_buffer currently associated with the cpp_reader
1379*404b540aSrobert PFILE. */
1380*404b540aSrobert
1381*404b540aSrobert cpp_buffer *
cpp_get_buffer(cpp_reader * pfile)1382*404b540aSrobert cpp_get_buffer (cpp_reader *pfile)
1383*404b540aSrobert {
1384*404b540aSrobert return pfile->buffer;
1385*404b540aSrobert }
1386*404b540aSrobert
1387*404b540aSrobert /* Get the _cpp_file associated with the cpp_buffer B. */
1388*404b540aSrobert
1389*404b540aSrobert _cpp_file *
cpp_get_file(cpp_buffer * b)1390*404b540aSrobert cpp_get_file (cpp_buffer *b)
1391*404b540aSrobert {
1392*404b540aSrobert return b->file;
1393*404b540aSrobert }
1394*404b540aSrobert
1395*404b540aSrobert /* Get the previous cpp_buffer given a cpp_buffer B. The previous
1396*404b540aSrobert buffer is the buffer that included the given buffer. */
1397*404b540aSrobert
1398*404b540aSrobert cpp_buffer *
cpp_get_prev(cpp_buffer * b)1399*404b540aSrobert cpp_get_prev (cpp_buffer *b)
1400*404b540aSrobert {
1401*404b540aSrobert return b->prev;
1402*404b540aSrobert }
1403*404b540aSrobert
1404*404b540aSrobert /* This data structure holds the list of header files that were seen
1405*404b540aSrobert while the PCH was being built. The 'entries' field is kept sorted
1406*404b540aSrobert in memcmp() order; yes, this means that on little-endian systems,
1407*404b540aSrobert it's sorted initially by the least-significant byte of 'size', but
1408*404b540aSrobert that's OK. The code does rely on having entries with the same size
1409*404b540aSrobert next to each other. */
1410*404b540aSrobert
1411*404b540aSrobert struct pchf_entry {
1412*404b540aSrobert /* The size of this file. This is used to save running a MD5 checksum
1413*404b540aSrobert if the sizes don't match. */
1414*404b540aSrobert off_t size;
1415*404b540aSrobert /* The MD5 checksum of this file. */
1416*404b540aSrobert unsigned char sum[16];
1417*404b540aSrobert /* Is this file to be included only once? */
1418*404b540aSrobert bool once_only;
1419*404b540aSrobert };
1420*404b540aSrobert
1421*404b540aSrobert struct pchf_data {
1422*404b540aSrobert /* Number of pchf_entry structures. */
1423*404b540aSrobert size_t count;
1424*404b540aSrobert
1425*404b540aSrobert /* Are there any values with once_only set?
1426*404b540aSrobert This is used as an optimisation, it means we don't have to search
1427*404b540aSrobert the structure if we're processing a regular #include. */
1428*404b540aSrobert bool have_once_only;
1429*404b540aSrobert
1430*404b540aSrobert struct pchf_entry entries[1];
1431*404b540aSrobert };
1432*404b540aSrobert
1433*404b540aSrobert static struct pchf_data *pchf;
1434*404b540aSrobert
1435*404b540aSrobert /* A qsort ordering function for pchf_entry structures. */
1436*404b540aSrobert
1437*404b540aSrobert static int
pchf_save_compare(const void * e1,const void * e2)1438*404b540aSrobert pchf_save_compare (const void *e1, const void *e2)
1439*404b540aSrobert {
1440*404b540aSrobert return memcmp (e1, e2, sizeof (struct pchf_entry));
1441*404b540aSrobert }
1442*404b540aSrobert
1443*404b540aSrobert /* Create and write to F a pchf_data structure. */
1444*404b540aSrobert
1445*404b540aSrobert bool
_cpp_save_file_entries(cpp_reader * pfile,FILE * fp)1446*404b540aSrobert _cpp_save_file_entries (cpp_reader *pfile, FILE *fp)
1447*404b540aSrobert {
1448*404b540aSrobert size_t count = 0;
1449*404b540aSrobert struct pchf_data *result;
1450*404b540aSrobert size_t result_size;
1451*404b540aSrobert _cpp_file *f;
1452*404b540aSrobert
1453*404b540aSrobert for (f = pfile->all_files; f; f = f->next_file)
1454*404b540aSrobert ++count;
1455*404b540aSrobert
1456*404b540aSrobert result_size = (sizeof (struct pchf_data)
1457*404b540aSrobert + sizeof (struct pchf_entry) * (count - 1));
1458*404b540aSrobert result = XCNEWVAR (struct pchf_data, result_size);
1459*404b540aSrobert
1460*404b540aSrobert result->count = 0;
1461*404b540aSrobert result->have_once_only = false;
1462*404b540aSrobert
1463*404b540aSrobert for (f = pfile->all_files; f; f = f->next_file)
1464*404b540aSrobert {
1465*404b540aSrobert size_t count;
1466*404b540aSrobert
1467*404b540aSrobert /* This should probably never happen, since if a read error occurred
1468*404b540aSrobert the PCH file shouldn't be written... */
1469*404b540aSrobert if (f->dont_read || f->err_no)
1470*404b540aSrobert continue;
1471*404b540aSrobert
1472*404b540aSrobert if (f->stack_count == 0)
1473*404b540aSrobert continue;
1474*404b540aSrobert
1475*404b540aSrobert count = result->count++;
1476*404b540aSrobert
1477*404b540aSrobert result->entries[count].once_only = f->once_only;
1478*404b540aSrobert /* |= is avoided in the next line because of an HP C compiler bug */
1479*404b540aSrobert result->have_once_only = result->have_once_only | f->once_only;
1480*404b540aSrobert if (f->buffer_valid)
1481*404b540aSrobert md5_buffer ((const char *)f->buffer,
1482*404b540aSrobert f->st.st_size, result->entries[count].sum);
1483*404b540aSrobert else
1484*404b540aSrobert {
1485*404b540aSrobert FILE *ff;
1486*404b540aSrobert int oldfd = f->fd;
1487*404b540aSrobert
1488*404b540aSrobert if (!open_file (f))
1489*404b540aSrobert {
1490*404b540aSrobert open_file_failed (pfile, f, 0);
1491*404b540aSrobert return false;
1492*404b540aSrobert }
1493*404b540aSrobert ff = fdopen (f->fd, "rb");
1494*404b540aSrobert md5_stream (ff, result->entries[count].sum);
1495*404b540aSrobert fclose (ff);
1496*404b540aSrobert f->fd = oldfd;
1497*404b540aSrobert }
1498*404b540aSrobert result->entries[count].size = f->st.st_size;
1499*404b540aSrobert }
1500*404b540aSrobert
1501*404b540aSrobert result_size = (sizeof (struct pchf_data)
1502*404b540aSrobert + sizeof (struct pchf_entry) * (result->count - 1));
1503*404b540aSrobert
1504*404b540aSrobert qsort (result->entries, result->count, sizeof (struct pchf_entry),
1505*404b540aSrobert pchf_save_compare);
1506*404b540aSrobert
1507*404b540aSrobert return fwrite (result, result_size, 1, fp) == 1;
1508*404b540aSrobert }
1509*404b540aSrobert
1510*404b540aSrobert /* Read the pchf_data structure from F. */
1511*404b540aSrobert
1512*404b540aSrobert bool
_cpp_read_file_entries(cpp_reader * pfile ATTRIBUTE_UNUSED,FILE * f)1513*404b540aSrobert _cpp_read_file_entries (cpp_reader *pfile ATTRIBUTE_UNUSED, FILE *f)
1514*404b540aSrobert {
1515*404b540aSrobert struct pchf_data d;
1516*404b540aSrobert
1517*404b540aSrobert if (fread (&d, sizeof (struct pchf_data) - sizeof (struct pchf_entry), 1, f)
1518*404b540aSrobert != 1)
1519*404b540aSrobert return false;
1520*404b540aSrobert
1521*404b540aSrobert pchf = XNEWVAR (struct pchf_data, sizeof (struct pchf_data)
1522*404b540aSrobert + sizeof (struct pchf_entry) * (d.count - 1));
1523*404b540aSrobert memcpy (pchf, &d, sizeof (struct pchf_data) - sizeof (struct pchf_entry));
1524*404b540aSrobert if (fread (pchf->entries, sizeof (struct pchf_entry), d.count, f)
1525*404b540aSrobert != d.count)
1526*404b540aSrobert return false;
1527*404b540aSrobert return true;
1528*404b540aSrobert }
1529*404b540aSrobert
1530*404b540aSrobert /* The parameters for pchf_compare. */
1531*404b540aSrobert
1532*404b540aSrobert struct pchf_compare_data
1533*404b540aSrobert {
1534*404b540aSrobert /* The size of the file we're looking for. */
1535*404b540aSrobert off_t size;
1536*404b540aSrobert
1537*404b540aSrobert /* The MD5 checksum of the file, if it's been computed. */
1538*404b540aSrobert unsigned char sum[16];
1539*404b540aSrobert
1540*404b540aSrobert /* Is SUM valid? */
1541*404b540aSrobert bool sum_computed;
1542*404b540aSrobert
1543*404b540aSrobert /* Do we need to worry about entries that don't have ONCE_ONLY set? */
1544*404b540aSrobert bool check_included;
1545*404b540aSrobert
1546*404b540aSrobert /* The file that we're searching for. */
1547*404b540aSrobert _cpp_file *f;
1548*404b540aSrobert };
1549*404b540aSrobert
1550*404b540aSrobert /* bsearch comparison function; look for D_P in E_P. */
1551*404b540aSrobert
1552*404b540aSrobert static int
pchf_compare(const void * d_p,const void * e_p)1553*404b540aSrobert pchf_compare (const void *d_p, const void *e_p)
1554*404b540aSrobert {
1555*404b540aSrobert const struct pchf_entry *e = (const struct pchf_entry *)e_p;
1556*404b540aSrobert struct pchf_compare_data *d = (struct pchf_compare_data *)d_p;
1557*404b540aSrobert int result;
1558*404b540aSrobert
1559*404b540aSrobert result = memcmp (&d->size, &e->size, sizeof (off_t));
1560*404b540aSrobert if (result != 0)
1561*404b540aSrobert return result;
1562*404b540aSrobert
1563*404b540aSrobert if (! d->sum_computed)
1564*404b540aSrobert {
1565*404b540aSrobert _cpp_file *const f = d->f;
1566*404b540aSrobert
1567*404b540aSrobert md5_buffer ((const char *)f->buffer, f->st.st_size, d->sum);
1568*404b540aSrobert d->sum_computed = true;
1569*404b540aSrobert }
1570*404b540aSrobert
1571*404b540aSrobert result = memcmp (d->sum, e->sum, 16);
1572*404b540aSrobert if (result != 0)
1573*404b540aSrobert return result;
1574*404b540aSrobert
1575*404b540aSrobert if (d->check_included || e->once_only)
1576*404b540aSrobert return 0;
1577*404b540aSrobert else
1578*404b540aSrobert return 1;
1579*404b540aSrobert }
1580*404b540aSrobert
1581*404b540aSrobert /* Check that F is not in a list read from a PCH file (if any).
1582*404b540aSrobert Assumes that f->buffer_valid is true. Return TRUE if the file
1583*404b540aSrobert should not be read. */
1584*404b540aSrobert
1585*404b540aSrobert static bool
check_file_against_entries(cpp_reader * pfile ATTRIBUTE_UNUSED,_cpp_file * f,bool check_included)1586*404b540aSrobert check_file_against_entries (cpp_reader *pfile ATTRIBUTE_UNUSED,
1587*404b540aSrobert _cpp_file *f,
1588*404b540aSrobert bool check_included)
1589*404b540aSrobert {
1590*404b540aSrobert struct pchf_compare_data d;
1591*404b540aSrobert
1592*404b540aSrobert if (pchf == NULL
1593*404b540aSrobert || (! check_included && ! pchf->have_once_only))
1594*404b540aSrobert return false;
1595*404b540aSrobert
1596*404b540aSrobert d.size = f->st.st_size;
1597*404b540aSrobert d.sum_computed = false;
1598*404b540aSrobert d.f = f;
1599*404b540aSrobert d.check_included = check_included;
1600*404b540aSrobert return bsearch (&d, pchf->entries, pchf->count, sizeof (struct pchf_entry),
1601*404b540aSrobert pchf_compare) != NULL;
1602*404b540aSrobert }
1603