1169695Skan/* Part of CPP library. File handling. 2169695Skan Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1998, 3259268Spfg 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007 4259268Spfg Free Software Foundation, Inc. 5169695Skan Written by Per Bothner, 1994. 6169695Skan Based on CCCP program by Paul Rubin, June 1986 7169695Skan Adapted to ANSI C, Richard Stallman, Jan 1987 8169695Skan Split out of cpplib.c, Zack Weinberg, Oct 1998 9169695Skan Reimplemented, Neil Booth, Jul 2003 10169695Skan 11169695SkanThis program is free software; you can redistribute it and/or modify it 12169695Skanunder the terms of the GNU General Public License as published by the 13169695SkanFree Software Foundation; either version 2, or (at your option) any 14169695Skanlater version. 15169695Skan 16169695SkanThis program is distributed in the hope that it will be useful, 17169695Skanbut WITHOUT ANY WARRANTY; without even the implied warranty of 18169695SkanMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 19169695SkanGNU General Public License for more details. 20169695Skan 21169695SkanYou should have received a copy of the GNU General Public License 22169695Skanalong with this program; if not, write to the Free Software 23169695SkanFoundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ 24169695Skan 25169695Skan#include "config.h" 26169695Skan#include "system.h" 27169695Skan#include "cpplib.h" 28169695Skan#include "internal.h" 29169695Skan#include "mkdeps.h" 30259268Spfg#include "obstack.h" 31169695Skan#include "hashtab.h" 32169695Skan#include "md5.h" 33169695Skan#include <dirent.h> 34169695Skan 35169695Skan/* Variable length record files on VMS will have a stat size that includes 36169695Skan record control characters that won't be included in the read size. */ 37169695Skan#ifdef VMS 38169695Skan# define FAB_C_VAR 2 /* variable length records (see Starlet fabdef.h) */ 39169695Skan# define STAT_SIZE_RELIABLE(ST) ((ST).st_fab_rfm != FAB_C_VAR) 40169695Skan#else 41169695Skan# define STAT_SIZE_RELIABLE(ST) true 42169695Skan#endif 43169695Skan 44169695Skan#ifdef __DJGPP__ 45169695Skan#include <io.h> 46169695Skan /* For DJGPP redirected input is opened in text mode. */ 47169695Skan# define set_stdin_to_binary_mode() \ 48169695Skan if (! isatty (0)) setmode (0, O_BINARY) 49169695Skan#else 50169695Skan# define set_stdin_to_binary_mode() /* Nothing */ 51169695Skan#endif 52169695Skan 53169695Skan/* This structure represents a file searched for by CPP, whether it 54169695Skan exists or not. An instance may be pointed to by more than one 55169695Skan file_hash_entry; at present no reference count is kept. */ 56169695Skanstruct _cpp_file 57169695Skan{ 58169695Skan /* Filename as given to #include or command line switch. */ 59169695Skan const char *name; 60169695Skan 61169695Skan /* The full path used to find the file. */ 62169695Skan const char *path; 63169695Skan 64169695Skan /* The full path of the pch file. */ 65169695Skan const char *pchname; 66169695Skan 67169695Skan /* The file's path with the basename stripped. NULL if it hasn't 68169695Skan been calculated yet. */ 69169695Skan const char *dir_name; 70169695Skan 71169695Skan /* Chain through all files. */ 72169695Skan struct _cpp_file *next_file; 73169695Skan 74169695Skan /* The contents of NAME after calling read_file(). */ 75169695Skan const uchar *buffer; 76169695Skan 77169695Skan /* The macro, if any, preventing re-inclusion. */ 78169695Skan const cpp_hashnode *cmacro; 79169695Skan 80169695Skan /* The directory in the search path where FILE was found. Used for 81169695Skan #include_next and determining whether a header is a system 82169695Skan header. */ 83169695Skan cpp_dir *dir; 84169695Skan 85169695Skan /* As filled in by stat(2) for the file. */ 86169695Skan struct stat st; 87169695Skan 88169695Skan /* File descriptor. Invalid if -1, otherwise open. */ 89169695Skan int fd; 90169695Skan 91169695Skan /* Zero if this file was successfully opened and stat()-ed, 92169695Skan otherwise errno obtained from failure. */ 93169695Skan int err_no; 94169695Skan 95169695Skan /* Number of times the file has been stacked for preprocessing. */ 96169695Skan unsigned short stack_count; 97169695Skan 98169695Skan /* If opened with #import or contains #pragma once. */ 99169695Skan bool once_only; 100169695Skan 101169695Skan /* If read() failed before. */ 102169695Skan bool dont_read; 103169695Skan 104169695Skan /* If this file is the main file. */ 105169695Skan bool main_file; 106169695Skan 107169695Skan /* If BUFFER above contains the true contents of the file. */ 108169695Skan bool buffer_valid; 109169695Skan 110169695Skan /* File is a PCH (on return from find_include_file). */ 111169695Skan bool pch; 112169695Skan}; 113169695Skan 114169695Skan/* A singly-linked list for all searches for a given file name, with 115169695Skan its head pointed to by a slot in FILE_HASH. The file name is what 116169695Skan appeared between the quotes in a #include directive; it can be 117169695Skan determined implicitly from the hash table location or explicitly 118169695Skan from FILE->name. 119169695Skan 120169695Skan FILE is a structure containing details about the file that was 121169695Skan found with that search, or details of how the search failed. 122169695Skan 123169695Skan START_DIR is the starting location of the search in the include 124169695Skan chain. The current directories for "" includes are also hashed in 125169695Skan the hash table and therefore unique. Files that are looked up 126169695Skan without using a search path, such as absolute filenames and file 127169695Skan names from the command line share a special starting directory so 128169695Skan they don't cause cache hits with normal include-chain lookups. 129169695Skan 130169695Skan If START_DIR is NULL then the entry is for a directory, not a file, 131169695Skan and the directory is in DIR. Since the starting point in a file 132169695Skan lookup chain is never NULL, this means that simple pointer 133169695Skan comparisons against START_DIR can be made to determine cache hits 134169695Skan in file lookups. 135169695Skan 136169695Skan If a cache lookup fails because of e.g. an extra "./" in the path, 137169695Skan then nothing will break. It is just less efficient as CPP will 138169695Skan have to do more work re-preprocessing the file, and/or comparing 139169695Skan its contents against earlier once-only files. 140169695Skan*/ 141169695Skanstruct file_hash_entry 142169695Skan{ 143169695Skan struct file_hash_entry *next; 144169695Skan cpp_dir *start_dir; 145169695Skan union 146169695Skan { 147169695Skan _cpp_file *file; 148169695Skan cpp_dir *dir; 149169695Skan } u; 150169695Skan}; 151169695Skan 152169695Skanstatic bool open_file (_cpp_file *file); 153169695Skanstatic bool pch_open_file (cpp_reader *pfile, _cpp_file *file, 154169695Skan bool *invalid_pch); 155169695Skanstatic bool find_file_in_dir (cpp_reader *pfile, _cpp_file *file, 156169695Skan bool *invalid_pch); 157169695Skanstatic bool read_file_guts (cpp_reader *pfile, _cpp_file *file); 158169695Skanstatic bool read_file (cpp_reader *pfile, _cpp_file *file); 159169695Skanstatic bool should_stack_file (cpp_reader *, _cpp_file *file, bool import); 160169695Skanstatic struct cpp_dir *search_path_head (cpp_reader *, const char *fname, 161169695Skan int angle_brackets, enum include_type); 162169695Skanstatic const char *dir_name_of_file (_cpp_file *file); 163169695Skanstatic void open_file_failed (cpp_reader *pfile, _cpp_file *file, int); 164169695Skanstatic struct file_hash_entry *search_cache (struct file_hash_entry *head, 165169695Skan const cpp_dir *start_dir); 166169695Skanstatic _cpp_file *make_cpp_file (cpp_reader *, cpp_dir *, const char *fname); 167169695Skanstatic void destroy_cpp_file (_cpp_file *); 168169695Skanstatic cpp_dir *make_cpp_dir (cpp_reader *, const char *dir_name, int sysp); 169169695Skanstatic void allocate_file_hash_entries (cpp_reader *pfile); 170169695Skanstatic struct file_hash_entry *new_file_hash_entry (cpp_reader *pfile); 171169695Skanstatic int report_missing_guard (void **slot, void *b); 172169695Skanstatic hashval_t file_hash_hash (const void *p); 173169695Skanstatic int file_hash_eq (const void *p, const void *q); 174169695Skanstatic char *read_filename_string (int ch, FILE *f); 175169695Skanstatic void read_name_map (cpp_dir *dir); 176169695Skanstatic char *remap_filename (cpp_reader *pfile, _cpp_file *file); 177169695Skanstatic char *append_file_to_dir (const char *fname, cpp_dir *dir); 178169695Skanstatic bool validate_pch (cpp_reader *, _cpp_file *file, const char *pchname); 179169695Skanstatic int pchf_save_compare (const void *e1, const void *e2); 180169695Skanstatic int pchf_compare (const void *d_p, const void *e_p); 181169695Skanstatic bool check_file_against_entries (cpp_reader *, _cpp_file *, bool); 182169695Skan 183169695Skan/* Given a filename in FILE->PATH, with the empty string interpreted 184169695Skan as <stdin>, open it. 185169695Skan 186169695Skan On success FILE contains an open file descriptor and stat 187169695Skan information for the file. On failure the file descriptor is -1 and 188169695Skan the appropriate errno is also stored in FILE. Returns TRUE iff 189169695Skan successful. 190169695Skan 191169695Skan We used to open files in nonblocking mode, but that caused more 192169695Skan problems than it solved. Do take care not to acquire a controlling 193169695Skan terminal by mistake (this can't happen on sane systems, but 194169695Skan paranoia is a virtue). 195169695Skan 196169695Skan Use the three-argument form of open even though we aren't 197169695Skan specifying O_CREAT, to defend against broken system headers. 198169695Skan 199169695Skan O_BINARY tells some runtime libraries (notably DJGPP) not to do 200169695Skan newline translation; we can handle DOS line breaks just fine 201169695Skan ourselves. */ 202169695Skanstatic bool 203169695Skanopen_file (_cpp_file *file) 204169695Skan{ 205169695Skan if (file->path[0] == '\0') 206169695Skan { 207169695Skan file->fd = 0; 208169695Skan set_stdin_to_binary_mode (); 209169695Skan } 210169695Skan else 211169695Skan file->fd = open (file->path, O_RDONLY | O_NOCTTY | O_BINARY, 0666); 212169695Skan 213169695Skan if (file->fd != -1) 214169695Skan { 215169695Skan if (fstat (file->fd, &file->st) == 0) 216169695Skan { 217169695Skan if (!S_ISDIR (file->st.st_mode)) 218169695Skan { 219169695Skan file->err_no = 0; 220169695Skan return true; 221169695Skan } 222169695Skan 223169695Skan /* Ignore a directory and continue the search. The file we're 224169695Skan looking for may be elsewhere in the search path. */ 225169695Skan errno = ENOENT; 226169695Skan } 227169695Skan 228169695Skan close (file->fd); 229169695Skan file->fd = -1; 230169695Skan } 231169695Skan else if (errno == ENOTDIR) 232169695Skan errno = ENOENT; 233169695Skan 234169695Skan file->err_no = errno; 235169695Skan 236169695Skan return false; 237169695Skan} 238169695Skan 239169695Skan/* Temporary PCH intercept of opening a file. Try to find a PCH file 240169695Skan based on FILE->name and FILE->dir, and test those found for 241169695Skan validity using PFILE->cb.valid_pch. Return true iff a valid file is 242169695Skan found. Set *INVALID_PCH if a PCH file is found but wasn't valid. */ 243169695Skan 244169695Skanstatic bool 245169695Skanpch_open_file (cpp_reader *pfile, _cpp_file *file, bool *invalid_pch) 246169695Skan{ 247169695Skan static const char extension[] = ".gch"; 248169695Skan const char *path = file->path; 249169695Skan size_t len, flen; 250169695Skan char *pchname; 251169695Skan struct stat st; 252169695Skan bool valid = false; 253169695Skan 254169695Skan /* No PCH on <stdin> or if not requested. */ 255169695Skan if (file->name[0] == '\0' || !pfile->cb.valid_pch) 256169695Skan return false; 257169695Skan 258169695Skan flen = strlen (path); 259169695Skan len = flen + sizeof (extension); 260169695Skan pchname = XNEWVEC (char, len); 261169695Skan memcpy (pchname, path, flen); 262169695Skan memcpy (pchname + flen, extension, sizeof (extension)); 263169695Skan 264169695Skan if (stat (pchname, &st) == 0) 265169695Skan { 266169695Skan DIR *pchdir; 267169695Skan struct dirent *d; 268169695Skan size_t dlen, plen = len; 269169695Skan 270169695Skan if (!S_ISDIR (st.st_mode)) 271169695Skan valid = validate_pch (pfile, file, pchname); 272169695Skan else if ((pchdir = opendir (pchname)) != NULL) 273169695Skan { 274169695Skan pchname[plen - 1] = '/'; 275169695Skan while ((d = readdir (pchdir)) != NULL) 276169695Skan { 277169695Skan dlen = strlen (d->d_name) + 1; 278169695Skan if ((strcmp (d->d_name, ".") == 0) 279169695Skan || (strcmp (d->d_name, "..") == 0)) 280169695Skan continue; 281169695Skan if (dlen + plen > len) 282169695Skan { 283169695Skan len += dlen + 64; 284169695Skan pchname = XRESIZEVEC (char, pchname, len); 285169695Skan } 286169695Skan memcpy (pchname + plen, d->d_name, dlen); 287169695Skan valid = validate_pch (pfile, file, pchname); 288169695Skan if (valid) 289169695Skan break; 290169695Skan } 291169695Skan closedir (pchdir); 292169695Skan } 293169695Skan if (valid) 294169695Skan file->pch = true; 295169695Skan else 296169695Skan *invalid_pch = true; 297169695Skan } 298169695Skan 299169695Skan if (valid) 300169695Skan file->pchname = pchname; 301169695Skan else 302169695Skan free (pchname); 303169695Skan 304169695Skan return valid; 305169695Skan} 306169695Skan 307169695Skan/* Try to open the path FILE->name appended to FILE->dir. This is 308169695Skan where remap and PCH intercept the file lookup process. Return true 309169695Skan if the file was found, whether or not the open was successful. 310169695Skan Set *INVALID_PCH to true if a PCH file is found but wasn't valid. */ 311169695Skan 312169695Skanstatic bool 313169695Skanfind_file_in_dir (cpp_reader *pfile, _cpp_file *file, bool *invalid_pch) 314169695Skan{ 315169695Skan char *path; 316169695Skan 317169695Skan if (CPP_OPTION (pfile, remap) && (path = remap_filename (pfile, file))) 318169695Skan ; 319169695Skan else 320169695Skan if (file->dir->construct) 321169695Skan path = file->dir->construct (file->name, file->dir); 322169695Skan else 323169695Skan path = append_file_to_dir (file->name, file->dir); 324169695Skan 325169695Skan if (path) 326169695Skan { 327259268Spfg hashval_t hv = htab_hash_string (path); 328259268Spfg char *copy; 329259268Spfg void **pp; 330259268Spfg 331259268Spfg if (htab_find_with_hash (pfile->nonexistent_file_hash, path, hv) != NULL) 332259268Spfg { 333259268Spfg file->err_no = ENOENT; 334259268Spfg return false; 335259268Spfg } 336259268Spfg 337169695Skan file->path = path; 338169695Skan if (pch_open_file (pfile, file, invalid_pch)) 339169695Skan return true; 340169695Skan 341169695Skan if (open_file (file)) 342169695Skan return true; 343169695Skan 344169695Skan if (file->err_no != ENOENT) 345169695Skan { 346169695Skan open_file_failed (pfile, file, 0); 347169695Skan return true; 348169695Skan } 349169695Skan 350259268Spfg /* We copy the path name onto an obstack partly so that we don't 351259268Spfg leak the memory, but mostly so that we don't fragment the 352259268Spfg heap. */ 353259268Spfg copy = obstack_copy0 (&pfile->nonexistent_file_ob, path, 354259268Spfg strlen (path)); 355169695Skan free (path); 356259268Spfg pp = htab_find_slot_with_hash (pfile->nonexistent_file_hash, 357259268Spfg copy, hv, INSERT); 358259268Spfg *pp = copy; 359259268Spfg 360169695Skan file->path = file->name; 361169695Skan } 362169695Skan else 363169695Skan { 364169695Skan file->err_no = ENOENT; 365169695Skan file->path = NULL; 366169695Skan } 367169695Skan 368169695Skan return false; 369169695Skan} 370169695Skan 371169695Skan/* Return tue iff the missing_header callback found the given HEADER. */ 372169695Skanstatic bool 373169695Skansearch_path_exhausted (cpp_reader *pfile, const char *header, _cpp_file *file) 374169695Skan{ 375169695Skan missing_header_cb func = pfile->cb.missing_header; 376169695Skan 377169695Skan /* When the regular search path doesn't work, try context dependent 378169695Skan headers search paths. */ 379169695Skan if (func 380169695Skan && file->dir == NULL) 381169695Skan { 382169695Skan if ((file->path = func (pfile, header, &file->dir)) != NULL) 383169695Skan { 384169695Skan if (open_file (file)) 385169695Skan return true; 386169695Skan free ((void *)file->path); 387169695Skan } 388169695Skan file->path = file->name; 389169695Skan } 390169695Skan 391169695Skan return false; 392169695Skan} 393169695Skan 394169695Skanbool 395169695Skan_cpp_find_failed (_cpp_file *file) 396169695Skan{ 397169695Skan return file->err_no != 0; 398169695Skan} 399169695Skan 400169695Skan/* Given a filename FNAME search for such a file in the include path 401169695Skan starting from START_DIR. If FNAME is the empty string it is 402169695Skan interpreted as STDIN if START_DIR is PFILE->no_search_path. 403169695Skan 404169695Skan If the file is not found in the file cache fall back to the O/S and 405169695Skan add the result to our cache. 406169695Skan 407169695Skan If the file was not found in the filesystem, or there was an error 408169695Skan opening it, then ERR_NO is nonzero and FD is -1. If the file was 409169695Skan found, then ERR_NO is zero and FD could be -1 or an open file 410169695Skan descriptor. FD can be -1 if the file was found in the cache and 411169695Skan had previously been closed. To open it again pass the return value 412169695Skan to open_file(). 413169695Skan*/ 414169695Skan_cpp_file * 415169695Skan_cpp_find_file (cpp_reader *pfile, const char *fname, cpp_dir *start_dir, bool fake, int angle_brackets) 416169695Skan{ 417169695Skan struct file_hash_entry *entry, **hash_slot; 418169695Skan _cpp_file *file; 419169695Skan bool invalid_pch = false; 420259268Spfg bool saw_bracket_include = false; 421259268Spfg bool saw_quote_include = false; 422259268Spfg struct cpp_dir *found_in_cache = NULL; 423169695Skan 424169695Skan /* Ensure we get no confusion between cached files and directories. */ 425169695Skan if (start_dir == NULL) 426169695Skan cpp_error (pfile, CPP_DL_ICE, "NULL directory in find_file"); 427169695Skan 428169695Skan hash_slot = (struct file_hash_entry **) 429169695Skan htab_find_slot_with_hash (pfile->file_hash, fname, 430169695Skan htab_hash_string (fname), 431169695Skan INSERT); 432169695Skan 433169695Skan /* First check the cache before we resort to memory allocation. */ 434169695Skan entry = search_cache (*hash_slot, start_dir); 435169695Skan if (entry) 436169695Skan return entry->u.file; 437169695Skan 438169695Skan file = make_cpp_file (pfile, start_dir, fname); 439169695Skan 440169695Skan /* Try each path in the include chain. */ 441169695Skan for (; !fake ;) 442169695Skan { 443169695Skan if (find_file_in_dir (pfile, file, &invalid_pch)) 444169695Skan break; 445169695Skan 446169695Skan file->dir = file->dir->next; 447169695Skan if (file->dir == NULL) 448169695Skan { 449169695Skan if (search_path_exhausted (pfile, fname, file)) 450169695Skan { 451169695Skan /* Although this file must not go in the cache, because 452169695Skan the file found might depend on things (like the current file) 453169695Skan that aren't represented in the cache, it still has to go in 454169695Skan the list of all files so that #import works. */ 455169695Skan file->next_file = pfile->all_files; 456169695Skan pfile->all_files = file; 457169695Skan return file; 458169695Skan } 459169695Skan 460169695Skan open_file_failed (pfile, file, angle_brackets); 461169695Skan if (invalid_pch) 462169695Skan { 463169695Skan cpp_error (pfile, CPP_DL_ERROR, 464169695Skan "one or more PCH files were found, but they were invalid"); 465169695Skan if (!cpp_get_options (pfile)->warn_invalid_pch) 466169695Skan cpp_error (pfile, CPP_DL_ERROR, 467169695Skan "use -Winvalid-pch for more information"); 468169695Skan } 469169695Skan break; 470169695Skan } 471169695Skan 472169695Skan /* Only check the cache for the starting location (done above) 473169695Skan and the quote and bracket chain heads because there are no 474169695Skan other possible starting points for searches. */ 475259268Spfg if (file->dir == pfile->bracket_include) 476259268Spfg saw_bracket_include = true; 477259268Spfg else if (file->dir == pfile->quote_include) 478259268Spfg saw_quote_include = true; 479259268Spfg else 480169695Skan continue; 481169695Skan 482169695Skan entry = search_cache (*hash_slot, file->dir); 483169695Skan if (entry) 484259268Spfg { 485259268Spfg found_in_cache = file->dir; 486259268Spfg break; 487259268Spfg } 488169695Skan } 489169695Skan 490169695Skan if (entry) 491169695Skan { 492169695Skan /* Cache for START_DIR too, sharing the _cpp_file structure. */ 493169695Skan free ((char *) file->name); 494169695Skan free (file); 495169695Skan file = entry->u.file; 496169695Skan } 497169695Skan else 498169695Skan { 499169695Skan /* This is a new file; put it in the list. */ 500169695Skan file->next_file = pfile->all_files; 501169695Skan pfile->all_files = file; 502169695Skan } 503169695Skan 504169695Skan /* Store this new result in the hash table. */ 505169695Skan entry = new_file_hash_entry (pfile); 506169695Skan entry->next = *hash_slot; 507169695Skan entry->start_dir = start_dir; 508169695Skan entry->u.file = file; 509169695Skan *hash_slot = entry; 510169695Skan 511259268Spfg /* If we passed the quote or bracket chain heads, cache them also. 512259268Spfg This speeds up processing if there are lots of -I options. */ 513259268Spfg if (saw_bracket_include 514259268Spfg && pfile->bracket_include != start_dir 515259268Spfg && found_in_cache != pfile->bracket_include) 516259268Spfg { 517259268Spfg entry = new_file_hash_entry (pfile); 518259268Spfg entry->next = *hash_slot; 519259268Spfg entry->start_dir = pfile->bracket_include; 520259268Spfg entry->u.file = file; 521259268Spfg *hash_slot = entry; 522259268Spfg } 523259268Spfg if (saw_quote_include 524259268Spfg && pfile->quote_include != start_dir 525259268Spfg && found_in_cache != pfile->quote_include) 526259268Spfg { 527259268Spfg entry = new_file_hash_entry (pfile); 528259268Spfg entry->next = *hash_slot; 529259268Spfg entry->start_dir = pfile->quote_include; 530259268Spfg entry->u.file = file; 531259268Spfg *hash_slot = entry; 532259268Spfg } 533259268Spfg 534169695Skan return file; 535169695Skan} 536169695Skan 537169695Skan/* Read a file into FILE->buffer, returning true on success. 538169695Skan 539169695Skan If FILE->fd is something weird, like a block device, we don't want 540169695Skan to read it at all. Don't even try to figure out what something is, 541169695Skan except for plain files and block devices, since there is no 542169695Skan reliable portable way of doing this. 543169695Skan 544169695Skan FIXME: Flush file cache and try again if we run out of memory. */ 545169695Skanstatic bool 546169695Skanread_file_guts (cpp_reader *pfile, _cpp_file *file) 547169695Skan{ 548169695Skan ssize_t size, total, count; 549264032Sdim off_t offset; 550169695Skan uchar *buf; 551169695Skan bool regular; 552169695Skan 553169695Skan if (S_ISBLK (file->st.st_mode)) 554169695Skan { 555169695Skan cpp_error (pfile, CPP_DL_ERROR, "%s is a block device", file->path); 556169695Skan return false; 557169695Skan } 558169695Skan 559169695Skan regular = S_ISREG (file->st.st_mode); 560169695Skan if (regular) 561169695Skan { 562169695Skan /* off_t might have a wider range than ssize_t - in other words, 563169695Skan the max size of a file might be bigger than the address 564169695Skan space. We can't handle a file that large. (Anyone with 565169695Skan a single source file bigger than 2GB needs to rethink 566169695Skan their coding style.) Some systems (e.g. AIX 4.1) define 567169695Skan SSIZE_MAX to be much smaller than the actual range of the 568169695Skan type. Use INTTYPE_MAXIMUM unconditionally to ensure this 569169695Skan does not bite us. */ 570288133Sdim if (file->st.st_size > SSIZE_MAX) 571169695Skan { 572169695Skan cpp_error (pfile, CPP_DL_ERROR, "%s is too large", file->path); 573169695Skan return false; 574169695Skan } 575169695Skan 576169695Skan size = file->st.st_size; 577264032Sdim 578264032Sdim if ((offset = lseek(file->fd, 0, SEEK_CUR)) < 0) 579264032Sdim { 580264032Sdim cpp_error (pfile, CPP_DL_ERROR, "%s has no current position", 581264032Sdim file->path); 582264032Sdim return false; 583264032Sdim } 584288133Sdim else if (offset > SSIZE_MAX || (ssize_t)offset > size) 585264032Sdim { 586264032Sdim cpp_error (pfile, CPP_DL_ERROR, "current position of %s is too large", 587264032Sdim file->path); 588264032Sdim return false; 589264032Sdim } 590264032Sdim 591264032Sdim size -= (ssize_t)offset; 592169695Skan } 593169695Skan else 594169695Skan /* 8 kilobytes is a sensible starting size. It ought to be bigger 595169695Skan than the kernel pipe buffer, and it's definitely bigger than 596169695Skan the majority of C source files. */ 597169695Skan size = 8 * 1024; 598169695Skan 599169695Skan buf = XNEWVEC (uchar, size + 1); 600169695Skan total = 0; 601169695Skan while ((count = read (file->fd, buf + total, size - total)) > 0) 602169695Skan { 603169695Skan total += count; 604169695Skan 605169695Skan if (total == size) 606169695Skan { 607169695Skan if (regular) 608169695Skan break; 609169695Skan size *= 2; 610169695Skan buf = XRESIZEVEC (uchar, buf, size + 1); 611169695Skan } 612169695Skan } 613169695Skan 614169695Skan if (count < 0) 615169695Skan { 616169695Skan cpp_errno (pfile, CPP_DL_ERROR, file->path); 617169695Skan return false; 618169695Skan } 619169695Skan 620169695Skan if (regular && total != size && STAT_SIZE_RELIABLE (file->st)) 621169695Skan cpp_error (pfile, CPP_DL_WARNING, 622169695Skan "%s is shorter than expected", file->path); 623169695Skan 624169695Skan file->buffer = _cpp_convert_input (pfile, CPP_OPTION (pfile, input_charset), 625169695Skan buf, size, total, &file->st.st_size); 626169695Skan file->buffer_valid = true; 627169695Skan 628169695Skan return true; 629169695Skan} 630169695Skan 631169695Skan/* Convenience wrapper around read_file_guts that opens the file if 632169695Skan necessary and closes the file descriptor after reading. FILE must 633169695Skan have been passed through find_file() at some stage. */ 634169695Skanstatic bool 635169695Skanread_file (cpp_reader *pfile, _cpp_file *file) 636169695Skan{ 637169695Skan /* If we already have its contents in memory, succeed immediately. */ 638169695Skan if (file->buffer_valid) 639169695Skan return true; 640169695Skan 641169695Skan /* If an earlier read failed for some reason don't try again. */ 642169695Skan if (file->dont_read || file->err_no) 643169695Skan return false; 644169695Skan 645169695Skan if (file->fd == -1 && !open_file (file)) 646169695Skan { 647169695Skan open_file_failed (pfile, file, 0); 648169695Skan return false; 649169695Skan } 650169695Skan 651169695Skan file->dont_read = !read_file_guts (pfile, file); 652169695Skan close (file->fd); 653169695Skan file->fd = -1; 654169695Skan 655169695Skan return !file->dont_read; 656169695Skan} 657169695Skan 658169695Skan/* Returns TRUE if FILE's contents have been successfully placed in 659169695Skan FILE->buffer and the file should be stacked, otherwise false. */ 660169695Skanstatic bool 661169695Skanshould_stack_file (cpp_reader *pfile, _cpp_file *file, bool import) 662169695Skan{ 663169695Skan _cpp_file *f; 664169695Skan 665169695Skan /* Skip once-only files. */ 666169695Skan if (file->once_only) 667169695Skan return false; 668169695Skan 669169695Skan /* We must mark the file once-only if #import now, before header 670169695Skan guard checks. Otherwise, undefining the header guard might 671169695Skan cause the file to be re-stacked. */ 672169695Skan if (import) 673169695Skan { 674169695Skan _cpp_mark_file_once_only (pfile, file); 675169695Skan 676169695Skan /* Don't stack files that have been stacked before. */ 677169695Skan if (file->stack_count) 678169695Skan return false; 679169695Skan } 680169695Skan 681169695Skan /* Skip if the file had a header guard and the macro is defined. 682169695Skan PCH relies on this appearing before the PCH handler below. */ 683169695Skan if (file->cmacro && file->cmacro->type == NT_MACRO) 684169695Skan return false; 685169695Skan 686169695Skan /* Handle PCH files immediately; don't stack them. */ 687169695Skan if (file->pch) 688169695Skan { 689169695Skan pfile->cb.read_pch (pfile, file->pchname, file->fd, file->path); 690169695Skan close (file->fd); 691169695Skan file->fd = -1; 692169695Skan return false; 693169695Skan } 694169695Skan 695169695Skan if (!read_file (pfile, file)) 696169695Skan return false; 697169695Skan 698169695Skan /* Check the file against the PCH file. This is done before 699169695Skan checking against files we've already seen, since it may save on 700169695Skan I/O. */ 701169695Skan if (check_file_against_entries (pfile, file, import)) 702169695Skan { 703169695Skan /* If this isn't a #import, but yet we can't include the file, 704169695Skan that means that it was #import-ed in the PCH file, 705169695Skan so we can never include it again. */ 706169695Skan if (! import) 707169695Skan _cpp_mark_file_once_only (pfile, file); 708169695Skan return false; 709169695Skan } 710169695Skan 711169695Skan /* Now we've read the file's contents, we can stack it if there 712169695Skan are no once-only files. */ 713169695Skan if (!pfile->seen_once_only) 714169695Skan return true; 715169695Skan 716169695Skan /* We may have read the file under a different name. Look 717169695Skan for likely candidates and compare file contents to be sure. */ 718169695Skan for (f = pfile->all_files; f; f = f->next_file) 719169695Skan { 720169695Skan if (f == file) 721169695Skan continue; 722169695Skan 723169695Skan if ((import || f->once_only) 724169695Skan && f->err_no == 0 725169695Skan && f->st.st_mtime == file->st.st_mtime 726169695Skan && f->st.st_size == file->st.st_size) 727169695Skan { 728169695Skan _cpp_file *ref_file; 729169695Skan bool same_file_p = false; 730169695Skan 731169695Skan if (f->buffer && !f->buffer_valid) 732169695Skan { 733169695Skan /* We already have a buffer but it is not valid, because 734169695Skan the file is still stacked. Make a new one. */ 735169695Skan ref_file = make_cpp_file (pfile, f->dir, f->name); 736169695Skan ref_file->path = f->path; 737169695Skan } 738169695Skan else 739169695Skan /* The file is not stacked anymore. We can reuse it. */ 740169695Skan ref_file = f; 741169695Skan 742169695Skan same_file_p = read_file (pfile, ref_file) 743169695Skan /* Size might have changed in read_file(). */ 744169695Skan && ref_file->st.st_size == file->st.st_size 745169695Skan && !memcmp (ref_file->buffer, 746169695Skan file->buffer, 747169695Skan file->st.st_size); 748169695Skan 749169695Skan if (f->buffer && !f->buffer_valid) 750169695Skan { 751169695Skan ref_file->path = 0; 752169695Skan destroy_cpp_file (ref_file); 753169695Skan } 754169695Skan 755169695Skan if (same_file_p) 756169695Skan break; 757169695Skan } 758169695Skan } 759169695Skan 760169695Skan return f == NULL; 761169695Skan} 762169695Skan 763169695Skan/* Place the file referenced by FILE into a new buffer on the buffer 764169695Skan stack if possible. IMPORT is true if this stacking attempt is 765169695Skan because of a #import directive. Returns true if a buffer is 766169695Skan stacked. */ 767169695Skanbool 768169695Skan_cpp_stack_file (cpp_reader *pfile, _cpp_file *file, bool import) 769169695Skan{ 770169695Skan cpp_buffer *buffer; 771169695Skan int sysp; 772169695Skan 773169695Skan if (!should_stack_file (pfile, file, import)) 774169695Skan return false; 775169695Skan 776169695Skan if (pfile->buffer == NULL || file->dir == NULL) 777169695Skan sysp = 0; 778169695Skan else 779169695Skan sysp = MAX (pfile->buffer->sysp, file->dir->sysp); 780169695Skan 781169695Skan /* Add the file to the dependencies on its first inclusion. */ 782169695Skan if (CPP_OPTION (pfile, deps.style) > !!sysp && !file->stack_count) 783169695Skan { 784169695Skan if (!file->main_file || !CPP_OPTION (pfile, deps.ignore_main_file)) 785169695Skan deps_add_dep (pfile->deps, file->path); 786169695Skan } 787169695Skan 788169695Skan /* Clear buffer_valid since _cpp_clean_line messes it up. */ 789169695Skan file->buffer_valid = false; 790169695Skan file->stack_count++; 791169695Skan 792169695Skan /* Stack the buffer. */ 793169695Skan buffer = cpp_push_buffer (pfile, file->buffer, file->st.st_size, 794259405Spfg CPP_OPTION (pfile, preprocessed) 795259405Spfg && !CPP_OPTION (pfile, directives_only)); 796169695Skan buffer->file = file; 797169695Skan buffer->sysp = sysp; 798169695Skan 799169695Skan /* Initialize controlling macro state. */ 800169695Skan pfile->mi_valid = true; 801169695Skan pfile->mi_cmacro = 0; 802169695Skan 803169695Skan /* Generate the call back. */ 804169695Skan _cpp_do_file_change (pfile, LC_ENTER, file->path, 1, sysp); 805169695Skan 806169695Skan return true; 807169695Skan} 808169695Skan 809169695Skan/* Mark FILE to be included once only. */ 810169695Skanvoid 811169695Skan_cpp_mark_file_once_only (cpp_reader *pfile, _cpp_file *file) 812169695Skan{ 813169695Skan pfile->seen_once_only = true; 814169695Skan file->once_only = true; 815169695Skan} 816169695Skan 817169695Skan/* Return the directory from which searching for FNAME should start, 818169695Skan considering the directive TYPE and ANGLE_BRACKETS. If there is 819169695Skan nothing left in the path, returns NULL. */ 820169695Skanstatic struct cpp_dir * 821169695Skansearch_path_head (cpp_reader *pfile, const char *fname, int angle_brackets, 822169695Skan enum include_type type) 823169695Skan{ 824169695Skan cpp_dir *dir; 825169695Skan _cpp_file *file; 826169695Skan 827169695Skan if (IS_ABSOLUTE_PATH (fname)) 828169695Skan return &pfile->no_search_path; 829169695Skan 830169695Skan /* pfile->buffer is NULL when processing an -include command-line flag. */ 831169695Skan file = pfile->buffer == NULL ? pfile->main_file : pfile->buffer->file; 832169695Skan 833169695Skan /* For #include_next, skip in the search path past the dir in which 834169695Skan the current file was found, but if it was found via an absolute 835169695Skan path use the normal search logic. */ 836169695Skan if (type == IT_INCLUDE_NEXT && file->dir) 837169695Skan dir = file->dir->next; 838169695Skan else if (angle_brackets) 839169695Skan dir = pfile->bracket_include; 840169695Skan else if (type == IT_CMDLINE) 841169695Skan /* -include and -imacros use the #include "" chain with the 842169695Skan preprocessor's cwd prepended. */ 843169695Skan return make_cpp_dir (pfile, "./", false); 844169695Skan else if (pfile->quote_ignores_source_dir) 845169695Skan dir = pfile->quote_include; 846169695Skan else 847169695Skan return make_cpp_dir (pfile, dir_name_of_file (file), 848169695Skan pfile->buffer ? pfile->buffer->sysp : 0); 849169695Skan 850169695Skan if (dir == NULL) 851169695Skan cpp_error (pfile, CPP_DL_ERROR, 852169695Skan "no include path in which to search for %s", fname); 853169695Skan 854169695Skan return dir; 855169695Skan} 856169695Skan 857169695Skan/* Strip the basename from the file's path. It ends with a slash if 858169695Skan of nonzero length. Note that this procedure also works for 859169695Skan <stdin>, which is represented by the empty string. */ 860169695Skanstatic const char * 861169695Skandir_name_of_file (_cpp_file *file) 862169695Skan{ 863169695Skan if (!file->dir_name) 864169695Skan { 865169695Skan size_t len = lbasename (file->path) - file->path; 866169695Skan char *dir_name = XNEWVEC (char, len + 1); 867169695Skan 868169695Skan memcpy (dir_name, file->path, len); 869169695Skan dir_name[len] = '\0'; 870169695Skan file->dir_name = dir_name; 871169695Skan } 872169695Skan 873169695Skan return file->dir_name; 874169695Skan} 875169695Skan 876169695Skan/* Handles #include-family directives (distinguished by TYPE), 877169695Skan including HEADER, and the command line -imacros and -include. 878169695Skan Returns true if a buffer was stacked. */ 879169695Skanbool 880169695Skan_cpp_stack_include (cpp_reader *pfile, const char *fname, int angle_brackets, 881169695Skan enum include_type type) 882169695Skan{ 883169695Skan struct cpp_dir *dir; 884169695Skan _cpp_file *file; 885169695Skan 886169695Skan dir = search_path_head (pfile, fname, angle_brackets, type); 887169695Skan if (!dir) 888169695Skan return false; 889169695Skan 890169695Skan file = _cpp_find_file (pfile, fname, dir, false, angle_brackets); 891169695Skan 892169695Skan /* Compensate for the increment in linemap_add. In the case of a 893169695Skan normal #include, we're currently at the start of the line 894169695Skan *following* the #include. A separate source_location for this 895169695Skan location makes no sense (until we do the LC_LEAVE), and 896169695Skan complicates LAST_SOURCE_LINE_LOCATION. This does not apply if we 897169695Skan found a PCH file (in which case linemap_add is not called) or we 898169695Skan were included from the command-line. */ 899169695Skan if (! file->pch && file->err_no == 0 && type != IT_CMDLINE) 900169695Skan pfile->line_table->highest_location--; 901169695Skan 902169695Skan return _cpp_stack_file (pfile, file, type == IT_IMPORT); 903169695Skan} 904169695Skan 905169695Skan/* Could not open FILE. The complication is dependency output. */ 906169695Skanstatic void 907169695Skanopen_file_failed (cpp_reader *pfile, _cpp_file *file, int angle_brackets) 908169695Skan{ 909169695Skan int sysp = pfile->line_table->highest_line > 1 && pfile->buffer ? pfile->buffer->sysp : 0; 910169695Skan bool print_dep = CPP_OPTION (pfile, deps.style) > (angle_brackets || !!sysp); 911169695Skan 912169695Skan errno = file->err_no; 913169695Skan if (print_dep && CPP_OPTION (pfile, deps.missing_files) && errno == ENOENT) 914169695Skan deps_add_dep (pfile->deps, file->name); 915169695Skan else 916169695Skan { 917169695Skan /* If we are outputting dependencies but not for this file then 918169695Skan don't error because we can still produce correct output. */ 919169695Skan if (CPP_OPTION (pfile, deps.style) && ! print_dep) 920169695Skan cpp_errno (pfile, CPP_DL_WARNING, file->path); 921169695Skan else 922169695Skan cpp_errno (pfile, CPP_DL_ERROR, file->path); 923169695Skan } 924169695Skan} 925169695Skan 926169695Skan/* Search in the chain beginning at HEAD for a file whose search path 927169695Skan started at START_DIR != NULL. */ 928169695Skanstatic struct file_hash_entry * 929169695Skansearch_cache (struct file_hash_entry *head, const cpp_dir *start_dir) 930169695Skan{ 931169695Skan while (head && head->start_dir != start_dir) 932169695Skan head = head->next; 933169695Skan 934169695Skan return head; 935169695Skan} 936169695Skan 937169695Skan/* Allocate a new _cpp_file structure. */ 938169695Skanstatic _cpp_file * 939169695Skanmake_cpp_file (cpp_reader *pfile, cpp_dir *dir, const char *fname) 940169695Skan{ 941169695Skan _cpp_file *file; 942169695Skan 943169695Skan file = XCNEW (_cpp_file); 944169695Skan file->main_file = !pfile->buffer; 945169695Skan file->fd = -1; 946169695Skan file->dir = dir; 947169695Skan file->name = xstrdup (fname); 948169695Skan 949169695Skan return file; 950169695Skan} 951169695Skan 952169695Skan/* Release a _cpp_file structure. */ 953169695Skanstatic void 954169695Skandestroy_cpp_file (_cpp_file *file) 955169695Skan{ 956169695Skan if (file->buffer) 957169695Skan free ((void *) file->buffer); 958169695Skan free ((void *) file->name); 959169695Skan free (file); 960169695Skan} 961169695Skan 962169695Skan/* A hash of directory names. The directory names are the path names 963169695Skan of files which contain a #include "", the included file name is 964169695Skan appended to this directories. 965169695Skan 966169695Skan To avoid duplicate entries we follow the convention that all 967169695Skan non-empty directory names should end in a '/'. DIR_NAME must be 968169695Skan stored in permanently allocated memory. */ 969169695Skanstatic cpp_dir * 970169695Skanmake_cpp_dir (cpp_reader *pfile, const char *dir_name, int sysp) 971169695Skan{ 972169695Skan struct file_hash_entry *entry, **hash_slot; 973169695Skan cpp_dir *dir; 974169695Skan 975169695Skan hash_slot = (struct file_hash_entry **) 976169695Skan htab_find_slot_with_hash (pfile->dir_hash, dir_name, 977169695Skan htab_hash_string (dir_name), 978169695Skan INSERT); 979169695Skan 980169695Skan /* Have we already hashed this directory? */ 981169695Skan for (entry = *hash_slot; entry; entry = entry->next) 982169695Skan if (entry->start_dir == NULL) 983169695Skan return entry->u.dir; 984169695Skan 985169695Skan dir = XCNEW (cpp_dir); 986169695Skan dir->next = pfile->quote_include; 987169695Skan dir->name = (char *) dir_name; 988169695Skan dir->len = strlen (dir_name); 989169695Skan dir->sysp = sysp; 990169695Skan dir->construct = 0; 991169695Skan 992169695Skan /* Store this new result in the hash table. */ 993169695Skan entry = new_file_hash_entry (pfile); 994169695Skan entry->next = *hash_slot; 995169695Skan entry->start_dir = NULL; 996169695Skan entry->u.dir = dir; 997169695Skan *hash_slot = entry; 998169695Skan 999169695Skan return dir; 1000169695Skan} 1001169695Skan 1002169695Skan/* Create a new block of memory for file hash entries. */ 1003169695Skanstatic void 1004169695Skanallocate_file_hash_entries (cpp_reader *pfile) 1005169695Skan{ 1006169695Skan pfile->file_hash_entries_used = 0; 1007169695Skan pfile->file_hash_entries_allocated = 127; 1008169695Skan pfile->file_hash_entries = XNEWVEC (struct file_hash_entry, 1009169695Skan pfile->file_hash_entries_allocated); 1010169695Skan} 1011169695Skan 1012169695Skan/* Return a new file hash entry. */ 1013169695Skanstatic struct file_hash_entry * 1014169695Skannew_file_hash_entry (cpp_reader *pfile) 1015169695Skan{ 1016169695Skan if (pfile->file_hash_entries_used == pfile->file_hash_entries_allocated) 1017169695Skan allocate_file_hash_entries (pfile); 1018169695Skan 1019169695Skan return &pfile->file_hash_entries[pfile->file_hash_entries_used++]; 1020169695Skan} 1021169695Skan 1022169695Skan/* Returns TRUE if a file FNAME has ever been successfully opened. 1023169695Skan This routine is not intended to correctly handle filenames aliased 1024169695Skan by links or redundant . or .. traversals etc. */ 1025169695Skanbool 1026169695Skancpp_included (cpp_reader *pfile, const char *fname) 1027169695Skan{ 1028169695Skan struct file_hash_entry *entry; 1029169695Skan 1030169695Skan entry = (struct file_hash_entry *) 1031169695Skan htab_find_with_hash (pfile->file_hash, fname, htab_hash_string (fname)); 1032169695Skan 1033169695Skan while (entry && (entry->start_dir == NULL || entry->u.file->err_no)) 1034169695Skan entry = entry->next; 1035169695Skan 1036169695Skan return entry != NULL; 1037169695Skan} 1038169695Skan 1039169695Skan/* Calculate the hash value of a file hash entry P. */ 1040169695Skan 1041169695Skanstatic hashval_t 1042169695Skanfile_hash_hash (const void *p) 1043169695Skan{ 1044169695Skan struct file_hash_entry *entry = (struct file_hash_entry *) p; 1045169695Skan const char *hname; 1046169695Skan if (entry->start_dir) 1047169695Skan hname = entry->u.file->name; 1048169695Skan else 1049169695Skan hname = entry->u.dir->name; 1050169695Skan 1051169695Skan return htab_hash_string (hname); 1052169695Skan} 1053169695Skan 1054169695Skan/* Compare a string Q against a file hash entry P. */ 1055169695Skanstatic int 1056169695Skanfile_hash_eq (const void *p, const void *q) 1057169695Skan{ 1058169695Skan struct file_hash_entry *entry = (struct file_hash_entry *) p; 1059169695Skan const char *fname = (const char *) q; 1060169695Skan const char *hname; 1061169695Skan 1062169695Skan if (entry->start_dir) 1063169695Skan hname = entry->u.file->name; 1064169695Skan else 1065169695Skan hname = entry->u.dir->name; 1066169695Skan 1067169695Skan return strcmp (hname, fname) == 0; 1068169695Skan} 1069169695Skan 1070259268Spfg/* Compare entries in the nonexistent file hash table. These are just 1071259268Spfg strings. */ 1072259268Spfgstatic int 1073259268Spfgnonexistent_file_hash_eq (const void *p, const void *q) 1074259268Spfg{ 1075259268Spfg return strcmp (p, q) == 0; 1076259268Spfg} 1077259268Spfg 1078169695Skan/* Initialize everything in this source file. */ 1079169695Skanvoid 1080169695Skan_cpp_init_files (cpp_reader *pfile) 1081169695Skan{ 1082169695Skan pfile->file_hash = htab_create_alloc (127, file_hash_hash, file_hash_eq, 1083169695Skan NULL, xcalloc, free); 1084169695Skan pfile->dir_hash = htab_create_alloc (127, file_hash_hash, file_hash_eq, 1085169695Skan NULL, xcalloc, free); 1086169695Skan allocate_file_hash_entries (pfile); 1087259268Spfg pfile->nonexistent_file_hash = htab_create_alloc (127, htab_hash_string, 1088259268Spfg nonexistent_file_hash_eq, 1089259268Spfg NULL, xcalloc, free); 1090259268Spfg _obstack_begin (&pfile->nonexistent_file_ob, 0, 0, 1091259268Spfg (void *(*) (long)) xmalloc, 1092259268Spfg (void (*) (void *)) free); 1093169695Skan} 1094169695Skan 1095169695Skan/* Finalize everything in this source file. */ 1096169695Skanvoid 1097169695Skan_cpp_cleanup_files (cpp_reader *pfile) 1098169695Skan{ 1099169695Skan htab_delete (pfile->file_hash); 1100169695Skan htab_delete (pfile->dir_hash); 1101259268Spfg htab_delete (pfile->nonexistent_file_hash); 1102259268Spfg obstack_free (&pfile->nonexistent_file_ob, 0); 1103169695Skan} 1104169695Skan 1105169695Skan/* Enter a file name in the hash for the sake of cpp_included. */ 1106169695Skanvoid 1107169695Skan_cpp_fake_include (cpp_reader *pfile, const char *fname) 1108169695Skan{ 1109169695Skan _cpp_find_file (pfile, fname, pfile->buffer->file->dir, true, 0); 1110169695Skan} 1111169695Skan 1112169695Skan/* Not everyone who wants to set system-header-ness on a buffer can 1113169695Skan see the details of a buffer. This is an exported interface because 1114169695Skan fix-header needs it. */ 1115169695Skanvoid 1116169695Skancpp_make_system_header (cpp_reader *pfile, int syshdr, int externc) 1117169695Skan{ 1118169695Skan int flags = 0; 1119169695Skan const struct line_maps *line_table = pfile->line_table; 1120169695Skan const struct line_map *map = &line_table->maps[line_table->used-1]; 1121169695Skan 1122169695Skan /* 1 = system header, 2 = system header to be treated as C. */ 1123169695Skan if (syshdr) 1124169695Skan flags = 1 + (externc != 0); 1125169695Skan pfile->buffer->sysp = flags; 1126169695Skan _cpp_do_file_change (pfile, LC_RENAME, map->to_file, 1127169695Skan SOURCE_LINE (map, pfile->line_table->highest_line), flags); 1128169695Skan} 1129169695Skan 1130169695Skan/* Allow the client to change the current file. Used by the front end 1131169695Skan to achieve pseudo-file names like <built-in>. 1132169695Skan If REASON is LC_LEAVE, then NEW_NAME must be NULL. */ 1133169695Skanvoid 1134169695Skancpp_change_file (cpp_reader *pfile, enum lc_reason reason, 1135169695Skan const char *new_name) 1136169695Skan{ 1137169695Skan _cpp_do_file_change (pfile, reason, new_name, 1, 0); 1138169695Skan} 1139169695Skan 1140169695Skan/* Callback function for htab_traverse. */ 1141169695Skanstatic int 1142169695Skanreport_missing_guard (void **slot, void *b) 1143169695Skan{ 1144169695Skan struct file_hash_entry *entry = (struct file_hash_entry *) *slot; 1145169695Skan int *bannerp = (int *) b; 1146169695Skan 1147169695Skan /* Skip directories. */ 1148169695Skan if (entry->start_dir != NULL) 1149169695Skan { 1150169695Skan _cpp_file *file = entry->u.file; 1151169695Skan 1152169695Skan /* We don't want MI guard advice for the main file. */ 1153169695Skan if (file->cmacro == NULL && file->stack_count == 1 && !file->main_file) 1154169695Skan { 1155169695Skan if (*bannerp == 0) 1156169695Skan { 1157169695Skan fputs (_("Multiple include guards may be useful for:\n"), 1158169695Skan stderr); 1159169695Skan *bannerp = 1; 1160169695Skan } 1161169695Skan 1162169695Skan fputs (entry->u.file->path, stderr); 1163169695Skan putc ('\n', stderr); 1164169695Skan } 1165169695Skan } 1166169695Skan 1167169695Skan return 0; 1168169695Skan} 1169169695Skan 1170169695Skan/* Report on all files that might benefit from a multiple include guard. 1171169695Skan Triggered by -H. */ 1172169695Skanvoid 1173169695Skan_cpp_report_missing_guards (cpp_reader *pfile) 1174169695Skan{ 1175169695Skan int banner = 0; 1176169695Skan 1177169695Skan htab_traverse (pfile->file_hash, report_missing_guard, &banner); 1178169695Skan} 1179169695Skan 1180169695Skan/* Locate HEADER, and determine whether it is newer than the current 1181169695Skan file. If it cannot be located or dated, return -1, if it is 1182169695Skan newer, return 1, otherwise 0. */ 1183169695Skanint 1184169695Skan_cpp_compare_file_date (cpp_reader *pfile, const char *fname, 1185169695Skan int angle_brackets) 1186169695Skan{ 1187169695Skan _cpp_file *file; 1188169695Skan struct cpp_dir *dir; 1189169695Skan 1190169695Skan dir = search_path_head (pfile, fname, angle_brackets, IT_INCLUDE); 1191169695Skan if (!dir) 1192169695Skan return -1; 1193169695Skan 1194169695Skan file = _cpp_find_file (pfile, fname, dir, false, angle_brackets); 1195169695Skan if (file->err_no) 1196169695Skan return -1; 1197169695Skan 1198169695Skan if (file->fd != -1) 1199169695Skan { 1200169695Skan close (file->fd); 1201169695Skan file->fd = -1; 1202169695Skan } 1203169695Skan 1204169695Skan return file->st.st_mtime > pfile->buffer->file->st.st_mtime; 1205169695Skan} 1206169695Skan 1207169695Skan/* Pushes the given file onto the buffer stack. Returns nonzero if 1208169695Skan successful. */ 1209169695Skanbool 1210169695Skancpp_push_include (cpp_reader *pfile, const char *fname) 1211169695Skan{ 1212169695Skan return _cpp_stack_include (pfile, fname, false, IT_CMDLINE); 1213169695Skan} 1214169695Skan 1215169695Skan/* Do appropriate cleanup when a file INC's buffer is popped off the 1216169695Skan input stack. */ 1217169695Skanvoid 1218169695Skan_cpp_pop_file_buffer (cpp_reader *pfile, _cpp_file *file) 1219169695Skan{ 1220169695Skan /* Record the inclusion-preventing macro, which could be NULL 1221169695Skan meaning no controlling macro. */ 1222169695Skan if (pfile->mi_valid && file->cmacro == NULL) 1223169695Skan file->cmacro = pfile->mi_cmacro; 1224169695Skan 1225169695Skan /* Invalidate control macros in the #including file. */ 1226169695Skan pfile->mi_valid = false; 1227169695Skan 1228169695Skan if (file->buffer) 1229169695Skan { 1230169695Skan free ((void *) file->buffer); 1231169695Skan file->buffer = NULL; 1232169695Skan file->buffer_valid = false; 1233169695Skan } 1234169695Skan} 1235169695Skan 1236169695Skan/* Inteface to file statistics record in _cpp_file structure. */ 1237169695Skanstruct stat * 1238169695Skan_cpp_get_file_stat (_cpp_file *file) 1239169695Skan{ 1240169695Skan return &file->st; 1241169695Skan} 1242169695Skan 1243169695Skan/* Set the include chain for "" to QUOTE, for <> to BRACKET. If 1244169695Skan QUOTE_IGNORES_SOURCE_DIR, then "" includes do not look in the 1245169695Skan directory of the including file. 1246169695Skan 1247169695Skan If BRACKET does not lie in the QUOTE chain, it is set to QUOTE. */ 1248169695Skanvoid 1249169695Skancpp_set_include_chains (cpp_reader *pfile, cpp_dir *quote, cpp_dir *bracket, 1250169695Skan int quote_ignores_source_dir) 1251169695Skan{ 1252169695Skan pfile->quote_include = quote; 1253169695Skan pfile->bracket_include = quote; 1254169695Skan pfile->quote_ignores_source_dir = quote_ignores_source_dir; 1255169695Skan 1256169695Skan for (; quote; quote = quote->next) 1257169695Skan { 1258169695Skan quote->name_map = NULL; 1259169695Skan quote->len = strlen (quote->name); 1260169695Skan if (quote == bracket) 1261169695Skan pfile->bracket_include = bracket; 1262169695Skan } 1263169695Skan} 1264169695Skan 1265169695Skan/* Append the file name to the directory to create the path, but don't 1266169695Skan turn / into // or // into ///; // may be a namespace escape. */ 1267169695Skanstatic char * 1268169695Skanappend_file_to_dir (const char *fname, cpp_dir *dir) 1269169695Skan{ 1270169695Skan size_t dlen, flen; 1271169695Skan char *path; 1272169695Skan 1273169695Skan dlen = dir->len; 1274169695Skan flen = strlen (fname); 1275169695Skan path = XNEWVEC (char, dlen + 1 + flen + 1); 1276169695Skan memcpy (path, dir->name, dlen); 1277169695Skan if (dlen && path[dlen - 1] != '/') 1278169695Skan path[dlen++] = '/'; 1279169695Skan memcpy (&path[dlen], fname, flen + 1); 1280169695Skan 1281169695Skan return path; 1282169695Skan} 1283169695Skan 1284169695Skan/* Read a space delimited string of unlimited length from a stdio 1285169695Skan file F. */ 1286169695Skanstatic char * 1287169695Skanread_filename_string (int ch, FILE *f) 1288169695Skan{ 1289169695Skan char *alloc, *set; 1290169695Skan int len; 1291169695Skan 1292169695Skan len = 20; 1293169695Skan set = alloc = XNEWVEC (char, len + 1); 1294169695Skan if (! is_space (ch)) 1295169695Skan { 1296169695Skan *set++ = ch; 1297169695Skan while ((ch = getc (f)) != EOF && ! is_space (ch)) 1298169695Skan { 1299169695Skan if (set - alloc == len) 1300169695Skan { 1301169695Skan len *= 2; 1302169695Skan alloc = XRESIZEVEC (char, alloc, len + 1); 1303169695Skan set = alloc + len / 2; 1304169695Skan } 1305169695Skan *set++ = ch; 1306169695Skan } 1307169695Skan } 1308169695Skan *set = '\0'; 1309169695Skan ungetc (ch, f); 1310169695Skan return alloc; 1311169695Skan} 1312169695Skan 1313169695Skan/* Read the file name map file for DIR. */ 1314169695Skanstatic void 1315169695Skanread_name_map (cpp_dir *dir) 1316169695Skan{ 1317169695Skan static const char FILE_NAME_MAP_FILE[] = "header.gcc"; 1318169695Skan char *name; 1319169695Skan FILE *f; 1320169695Skan size_t len, count = 0, room = 9; 1321169695Skan 1322169695Skan len = dir->len; 1323169695Skan name = (char *) alloca (len + sizeof (FILE_NAME_MAP_FILE) + 1); 1324169695Skan memcpy (name, dir->name, len); 1325169695Skan if (len && name[len - 1] != '/') 1326169695Skan name[len++] = '/'; 1327169695Skan strcpy (name + len, FILE_NAME_MAP_FILE); 1328169695Skan f = fopen (name, "r"); 1329169695Skan 1330169695Skan dir->name_map = XNEWVEC (const char *, room); 1331169695Skan 1332169695Skan /* Silently return NULL if we cannot open. */ 1333169695Skan if (f) 1334169695Skan { 1335169695Skan int ch; 1336169695Skan 1337169695Skan while ((ch = getc (f)) != EOF) 1338169695Skan { 1339169695Skan char *to; 1340169695Skan 1341169695Skan if (is_space (ch)) 1342169695Skan continue; 1343169695Skan 1344169695Skan if (count + 2 > room) 1345169695Skan { 1346169695Skan room += 8; 1347169695Skan dir->name_map = XRESIZEVEC (const char *, dir->name_map, room); 1348169695Skan } 1349169695Skan 1350169695Skan dir->name_map[count] = read_filename_string (ch, f); 1351169695Skan while ((ch = getc (f)) != EOF && is_hspace (ch)) 1352169695Skan ; 1353169695Skan 1354169695Skan to = read_filename_string (ch, f); 1355169695Skan if (IS_ABSOLUTE_PATH (to)) 1356169695Skan dir->name_map[count + 1] = to; 1357169695Skan else 1358169695Skan { 1359169695Skan dir->name_map[count + 1] = append_file_to_dir (to, dir); 1360169695Skan free (to); 1361169695Skan } 1362169695Skan 1363169695Skan count += 2; 1364169695Skan while ((ch = getc (f)) != '\n') 1365169695Skan if (ch == EOF) 1366169695Skan break; 1367169695Skan } 1368169695Skan 1369169695Skan fclose (f); 1370169695Skan } 1371169695Skan 1372169695Skan /* Terminate the list of maps. */ 1373169695Skan dir->name_map[count] = NULL; 1374169695Skan} 1375169695Skan 1376169695Skan/* Remap a FILE's name based on the file_name_map, if any, for 1377169695Skan FILE->dir. If the file name has any directory separators, 1378169695Skan recursively check those directories too. */ 1379169695Skanstatic char * 1380169695Skanremap_filename (cpp_reader *pfile, _cpp_file *file) 1381169695Skan{ 1382169695Skan const char *fname, *p; 1383169695Skan char *new_dir; 1384169695Skan cpp_dir *dir; 1385169695Skan size_t index, len; 1386169695Skan 1387169695Skan dir = file->dir; 1388169695Skan fname = file->name; 1389169695Skan 1390169695Skan for (;;) 1391169695Skan { 1392169695Skan if (!dir->name_map) 1393169695Skan read_name_map (dir); 1394169695Skan 1395169695Skan for (index = 0; dir->name_map[index]; index += 2) 1396169695Skan if (!strcmp (dir->name_map[index], fname)) 1397169695Skan return xstrdup (dir->name_map[index + 1]); 1398169695Skan 1399169695Skan p = strchr (fname, '/'); 1400169695Skan if (!p || p == fname) 1401169695Skan return NULL; 1402169695Skan 1403169695Skan len = dir->len + (p - fname + 1); 1404169695Skan new_dir = XNEWVEC (char, len + 1); 1405169695Skan memcpy (new_dir, dir->name, dir->len); 1406169695Skan memcpy (new_dir + dir->len, fname, p - fname + 1); 1407169695Skan new_dir[len] = '\0'; 1408169695Skan 1409169695Skan dir = make_cpp_dir (pfile, new_dir, dir->sysp); 1410169695Skan fname = p + 1; 1411169695Skan } 1412169695Skan} 1413169695Skan 1414169695Skan/* Returns true if PCHNAME is a valid PCH file for FILE. */ 1415169695Skanstatic bool 1416169695Skanvalidate_pch (cpp_reader *pfile, _cpp_file *file, const char *pchname) 1417169695Skan{ 1418169695Skan const char *saved_path = file->path; 1419169695Skan bool valid = false; 1420169695Skan 1421169695Skan file->path = pchname; 1422169695Skan if (open_file (file)) 1423169695Skan { 1424169695Skan valid = 1 & pfile->cb.valid_pch (pfile, pchname, file->fd); 1425169695Skan 1426169695Skan if (!valid) 1427169695Skan { 1428169695Skan close (file->fd); 1429169695Skan file->fd = -1; 1430169695Skan } 1431169695Skan 1432169695Skan if (CPP_OPTION (pfile, print_include_names)) 1433169695Skan { 1434169695Skan unsigned int i; 1435169695Skan for (i = 1; i < pfile->line_table->depth; i++) 1436169695Skan putc ('.', stderr); 1437169695Skan fprintf (stderr, "%c %s\n", 1438169695Skan valid ? '!' : 'x', pchname); 1439169695Skan } 1440169695Skan } 1441169695Skan 1442169695Skan file->path = saved_path; 1443169695Skan return valid; 1444169695Skan} 1445169695Skan 1446169695Skan/* Get the path associated with the _cpp_file F. The path includes 1447169695Skan the base name from the include directive and the directory it was 1448169695Skan found in via the search path. */ 1449169695Skan 1450169695Skanconst char * 1451169695Skancpp_get_path (struct _cpp_file *f) 1452169695Skan{ 1453169695Skan return f->path; 1454169695Skan} 1455169695Skan 1456169695Skan/* Get the directory associated with the _cpp_file F. */ 1457169695Skan 1458169695Skancpp_dir * 1459169695Skancpp_get_dir (struct _cpp_file *f) 1460169695Skan{ 1461169695Skan return f->dir; 1462169695Skan} 1463169695Skan 1464169695Skan/* Get the cpp_buffer currently associated with the cpp_reader 1465169695Skan PFILE. */ 1466169695Skan 1467169695Skancpp_buffer * 1468169695Skancpp_get_buffer (cpp_reader *pfile) 1469169695Skan{ 1470169695Skan return pfile->buffer; 1471169695Skan} 1472169695Skan 1473169695Skan/* Get the _cpp_file associated with the cpp_buffer B. */ 1474169695Skan 1475169695Skan_cpp_file * 1476169695Skancpp_get_file (cpp_buffer *b) 1477169695Skan{ 1478169695Skan return b->file; 1479169695Skan} 1480169695Skan 1481169695Skan/* Get the previous cpp_buffer given a cpp_buffer B. The previous 1482169695Skan buffer is the buffer that included the given buffer. */ 1483169695Skan 1484169695Skancpp_buffer * 1485169695Skancpp_get_prev (cpp_buffer *b) 1486169695Skan{ 1487169695Skan return b->prev; 1488169695Skan} 1489169695Skan 1490169695Skan/* This data structure holds the list of header files that were seen 1491169695Skan while the PCH was being built. The 'entries' field is kept sorted 1492169695Skan in memcmp() order; yes, this means that on little-endian systems, 1493169695Skan it's sorted initially by the least-significant byte of 'size', but 1494169695Skan that's OK. The code does rely on having entries with the same size 1495169695Skan next to each other. */ 1496169695Skan 1497169695Skanstruct pchf_entry { 1498169695Skan /* The size of this file. This is used to save running a MD5 checksum 1499169695Skan if the sizes don't match. */ 1500169695Skan off_t size; 1501169695Skan /* The MD5 checksum of this file. */ 1502169695Skan unsigned char sum[16]; 1503169695Skan /* Is this file to be included only once? */ 1504169695Skan bool once_only; 1505169695Skan}; 1506169695Skan 1507169695Skanstruct pchf_data { 1508169695Skan /* Number of pchf_entry structures. */ 1509169695Skan size_t count; 1510169695Skan 1511169695Skan /* Are there any values with once_only set? 1512169695Skan This is used as an optimisation, it means we don't have to search 1513169695Skan the structure if we're processing a regular #include. */ 1514169695Skan bool have_once_only; 1515169695Skan 1516169695Skan struct pchf_entry entries[1]; 1517169695Skan}; 1518169695Skan 1519169695Skanstatic struct pchf_data *pchf; 1520169695Skan 1521169695Skan/* A qsort ordering function for pchf_entry structures. */ 1522169695Skan 1523169695Skanstatic int 1524169695Skanpchf_save_compare (const void *e1, const void *e2) 1525169695Skan{ 1526169695Skan return memcmp (e1, e2, sizeof (struct pchf_entry)); 1527169695Skan} 1528169695Skan 1529169695Skan/* Create and write to F a pchf_data structure. */ 1530169695Skan 1531169695Skanbool 1532169695Skan_cpp_save_file_entries (cpp_reader *pfile, FILE *fp) 1533169695Skan{ 1534169695Skan size_t count = 0; 1535169695Skan struct pchf_data *result; 1536169695Skan size_t result_size; 1537169695Skan _cpp_file *f; 1538169695Skan 1539169695Skan for (f = pfile->all_files; f; f = f->next_file) 1540169695Skan ++count; 1541169695Skan 1542169695Skan result_size = (sizeof (struct pchf_data) 1543169695Skan + sizeof (struct pchf_entry) * (count - 1)); 1544169695Skan result = XCNEWVAR (struct pchf_data, result_size); 1545169695Skan 1546169695Skan result->count = 0; 1547169695Skan result->have_once_only = false; 1548169695Skan 1549169695Skan for (f = pfile->all_files; f; f = f->next_file) 1550169695Skan { 1551169695Skan size_t count; 1552169695Skan 1553169695Skan /* This should probably never happen, since if a read error occurred 1554169695Skan the PCH file shouldn't be written... */ 1555169695Skan if (f->dont_read || f->err_no) 1556169695Skan continue; 1557169695Skan 1558169695Skan if (f->stack_count == 0) 1559169695Skan continue; 1560169695Skan 1561169695Skan count = result->count++; 1562169695Skan 1563169695Skan result->entries[count].once_only = f->once_only; 1564169695Skan /* |= is avoided in the next line because of an HP C compiler bug */ 1565169695Skan result->have_once_only = result->have_once_only | f->once_only; 1566169695Skan if (f->buffer_valid) 1567169695Skan md5_buffer ((const char *)f->buffer, 1568169695Skan f->st.st_size, result->entries[count].sum); 1569169695Skan else 1570169695Skan { 1571169695Skan FILE *ff; 1572169695Skan int oldfd = f->fd; 1573169695Skan 1574169695Skan if (!open_file (f)) 1575169695Skan { 1576169695Skan open_file_failed (pfile, f, 0); 1577169695Skan return false; 1578169695Skan } 1579169695Skan ff = fdopen (f->fd, "rb"); 1580169695Skan md5_stream (ff, result->entries[count].sum); 1581169695Skan fclose (ff); 1582169695Skan f->fd = oldfd; 1583169695Skan } 1584169695Skan result->entries[count].size = f->st.st_size; 1585169695Skan } 1586169695Skan 1587169695Skan result_size = (sizeof (struct pchf_data) 1588169695Skan + sizeof (struct pchf_entry) * (result->count - 1)); 1589169695Skan 1590169695Skan qsort (result->entries, result->count, sizeof (struct pchf_entry), 1591169695Skan pchf_save_compare); 1592169695Skan 1593169695Skan return fwrite (result, result_size, 1, fp) == 1; 1594169695Skan} 1595169695Skan 1596169695Skan/* Read the pchf_data structure from F. */ 1597169695Skan 1598169695Skanbool 1599169695Skan_cpp_read_file_entries (cpp_reader *pfile ATTRIBUTE_UNUSED, FILE *f) 1600169695Skan{ 1601169695Skan struct pchf_data d; 1602169695Skan 1603169695Skan if (fread (&d, sizeof (struct pchf_data) - sizeof (struct pchf_entry), 1, f) 1604169695Skan != 1) 1605169695Skan return false; 1606169695Skan 1607169695Skan pchf = XNEWVAR (struct pchf_data, sizeof (struct pchf_data) 1608169695Skan + sizeof (struct pchf_entry) * (d.count - 1)); 1609169695Skan memcpy (pchf, &d, sizeof (struct pchf_data) - sizeof (struct pchf_entry)); 1610169695Skan if (fread (pchf->entries, sizeof (struct pchf_entry), d.count, f) 1611169695Skan != d.count) 1612169695Skan return false; 1613169695Skan return true; 1614169695Skan} 1615169695Skan 1616169695Skan/* The parameters for pchf_compare. */ 1617169695Skan 1618169695Skanstruct pchf_compare_data 1619169695Skan{ 1620169695Skan /* The size of the file we're looking for. */ 1621169695Skan off_t size; 1622169695Skan 1623169695Skan /* The MD5 checksum of the file, if it's been computed. */ 1624169695Skan unsigned char sum[16]; 1625169695Skan 1626169695Skan /* Is SUM valid? */ 1627169695Skan bool sum_computed; 1628169695Skan 1629169695Skan /* Do we need to worry about entries that don't have ONCE_ONLY set? */ 1630169695Skan bool check_included; 1631169695Skan 1632169695Skan /* The file that we're searching for. */ 1633169695Skan _cpp_file *f; 1634169695Skan}; 1635169695Skan 1636169695Skan/* bsearch comparison function; look for D_P in E_P. */ 1637169695Skan 1638169695Skanstatic int 1639169695Skanpchf_compare (const void *d_p, const void *e_p) 1640169695Skan{ 1641169695Skan const struct pchf_entry *e = (const struct pchf_entry *)e_p; 1642169695Skan struct pchf_compare_data *d = (struct pchf_compare_data *)d_p; 1643169695Skan int result; 1644169695Skan 1645169695Skan result = memcmp (&d->size, &e->size, sizeof (off_t)); 1646169695Skan if (result != 0) 1647169695Skan return result; 1648169695Skan 1649169695Skan if (! d->sum_computed) 1650169695Skan { 1651169695Skan _cpp_file *const f = d->f; 1652169695Skan 1653169695Skan md5_buffer ((const char *)f->buffer, f->st.st_size, d->sum); 1654169695Skan d->sum_computed = true; 1655169695Skan } 1656169695Skan 1657169695Skan result = memcmp (d->sum, e->sum, 16); 1658169695Skan if (result != 0) 1659169695Skan return result; 1660169695Skan 1661169695Skan if (d->check_included || e->once_only) 1662169695Skan return 0; 1663169695Skan else 1664169695Skan return 1; 1665169695Skan} 1666169695Skan 1667169695Skan/* Check that F is not in a list read from a PCH file (if any). 1668169695Skan Assumes that f->buffer_valid is true. Return TRUE if the file 1669169695Skan should not be read. */ 1670169695Skan 1671169695Skanstatic bool 1672169695Skancheck_file_against_entries (cpp_reader *pfile ATTRIBUTE_UNUSED, 1673169695Skan _cpp_file *f, 1674169695Skan bool check_included) 1675169695Skan{ 1676169695Skan struct pchf_compare_data d; 1677169695Skan 1678169695Skan if (pchf == NULL 1679169695Skan || (! check_included && ! pchf->have_once_only)) 1680169695Skan return false; 1681169695Skan 1682169695Skan d.size = f->st.st_size; 1683169695Skan d.sum_computed = false; 1684169695Skan d.f = f; 1685169695Skan d.check_included = check_included; 1686169695Skan return bsearch (&d, pchf->entries, pchf->count, sizeof (struct pchf_entry), 1687169695Skan pchf_compare) != NULL; 1688169695Skan} 1689