{
const char *name; /* actual path name of file */
const cpp_hashnode *cmacro; /* macro, if any, preventing reinclusion. */
- const struct file_name_list *foundhere;
+ const struct search_path *foundhere;
/* location in search path where file was
found, for #include_next and sysp. */
const unsigned char *buffer; /* pointer to cached file contents */
PARAMS ((cpp_reader *, const char *));
static char *read_filename_string PARAMS ((int, FILE *));
static char *remap_filename PARAMS ((cpp_reader *, char *,
- struct file_name_list *));
-static struct file_name_list *actual_directory
- PARAMS ((cpp_reader *, const char *));
+ struct search_path *));
+static struct search_path *search_from PARAMS ((cpp_reader *,
+ struct include_file *));
static struct include_file *find_include_file
PARAMS ((cpp_reader *, const char *,
- struct file_name_list *));
+ struct search_path *));
static struct include_file *open_file PARAMS ((cpp_reader *, const char *));
static void read_include_file PARAMS ((cpp_reader *, struct include_file *));
static void stack_include_file PARAMS ((cpp_reader *, struct include_file *));
fp->inc = inc;
fp->inc->refcnt++;
fp->sysp = sysp;
-
- /* The ->actual_dir field is only used when ignore_srcdir is not in effect;
- see do_include */
- if (!CPP_OPTION (pfile, ignore_srcdir))
- fp->actual_dir = actual_directory (pfile, inc->name);
+ fp->search_from = search_from (pfile, inc);
/* Initialise controlling macro state. */
pfile->mi_state = MI_OUTSIDE;
cpp_reader *pfile;
const char *fname;
{
- struct file_name_list *path;
+ struct search_path *path;
char *name;
splay_tree_node nd;
+ 2 + INCLUDE_LEN_FUDGE);
for (path = CPP_OPTION (pfile, quote_include); path; path = path->next)
{
- memcpy (name, path->name, path->nlen);
- name[path->nlen] = '/';
- strcpy (&name[path->nlen+1], fname);
+ memcpy (name, path->name, path->len);
+ name[path->len] = '/';
+ strcpy (&name[path->len + 1], fname);
_cpp_simplify_pathname (name);
if (CPP_OPTION (pfile, remap))
name = remap_filename (pfile, name, path);
find_include_file (pfile, fname, search_start)
cpp_reader *pfile;
const char *fname;
- struct file_name_list *search_start;
+ struct search_path *search_start;
{
- struct file_name_list *path;
+ struct search_path *path;
char *name;
struct include_file *file;
+ 2 + INCLUDE_LEN_FUDGE);
for (path = search_start; path; path = path->next)
{
- memcpy (name, path->name, path->nlen);
- name[path->nlen] = '/';
- strcpy (&name[path->nlen+1], fname);
+ memcpy (name, path->name, path->len);
+ name[path->len] = '/';
+ strcpy (&name[path->len + 1], fname);
_cpp_simplify_pathname (name);
if (CPP_OPTION (pfile, remap))
name = remap_filename (pfile, name, path);
return file;
}
}
+
return 0;
}
int no_reinclude;
int include_next;
{
- struct file_name_list *search_start = 0;
+ struct search_path *search_start = 0;
unsigned int len = header->val.str.len;
unsigned int angle_brackets = header->type == CPP_HEADER_NAME;
struct include_file *inc;
{
if (angle_brackets)
search_start = CPP_OPTION (pfile, bracket_include);
- else if (CPP_OPTION (pfile, ignore_srcdir))
- search_start = CPP_OPTION (pfile, quote_include);
else
- search_start = CPP_BUFFER (pfile)->actual_dir;
+ search_start = pfile->buffer->search_from;
if (!search_start)
{
else
{
char *p;
- struct file_name_list *ptr;
+ struct search_path *ptr;
int len;
/* If requested as a system header, assume it belongs in
- the first system header directory. */
+ the first system header directory. */
if (CPP_OPTION (pfile, bracket_include))
ptr = CPP_OPTION (pfile, bracket_include);
else
ptr = CPP_OPTION (pfile, quote_include);
- len = strlen (ptr->name);
+ /* FIXME: ptr can be null, no? */
+ len = ptr->len;
p = (char *) alloca (len + strlen (fname) + 2);
if (len)
{
{
unsigned int len = f->val.str.len;
char *fname;
- struct file_name_list *search_start;
+ struct search_path *search_start;
struct include_file *inc;
if (f->type == CPP_HEADER_NAME)
search_start = CPP_OPTION (pfile, bracket_include);
else if (CPP_OPTION (pfile, ignore_srcdir))
- search_start = CPP_OPTION (pfile, quote_include);
- else
- search_start = CPP_BUFFER (pfile)->actual_dir;
+ search_start = pfile->buffer->search_from;
fname = alloca (len + 1);
memcpy (fname, f->val.str.text, len);
purge_cache (inc);
}
+/* Returns the first place in the include chain to start searching for
+ "" includes. This involves stripping away the basename of the
+ current file, unless -I- was specified. */
+static struct search_path *
+search_from (pfile, inc)
+ cpp_reader *pfile;
+ struct include_file *inc;
+{
+ cpp_buffer *buffer = pfile->buffer;
+ unsigned int dlen;
+
+ /* Ignore the current file's directory if -I- was given. */
+ if (CPP_OPTION (pfile, ignore_srcdir))
+ return CPP_OPTION (pfile, quote_include);
+
+ dlen = basename (inc->name) - inc->name;
+ if (dlen)
+ {
+ /* We don't guarantee NAME is null-terminated. This saves
+ allocating and freeing memory, and duplicating it when faking
+ buffers in cpp_push_buffer. Drop a trailing '/'. */
+ buffer->dir.name = inc->name;
+ if (dlen > 1)
+ dlen--;
+ }
+ else
+ {
+ buffer->dir.name = ".";
+ dlen = 1;
+ }
+
+ if (dlen > pfile->max_include_len)
+ pfile->max_include_len = dlen;
+
+ buffer->dir.len = dlen;
+ buffer->dir.next = CPP_OPTION (pfile, quote_include);
+ buffer->dir.sysp = buffer->sysp;
+
+ return &buffer->dir;
+}
+
/* The file_name_map structure holds a mapping of file names for a
particular directory. This mapping is read from the file named
FILE_NAME_MAP_FILE in that directory. Such a file can be used to
remap_filename (pfile, name, loc)
cpp_reader *pfile;
char *name;
- struct file_name_list *loc;
+ struct search_path *loc;
{
struct file_name_map *map;
const char *from, *p;
- char *dir;
+ char *dir, *dname;
+
+ /* Get a null-terminated path. */
+ dname = alloca (loc->len + 1);
+ memcpy (dname, loc->name, loc->len);
+ dname[loc->len] = '\0';
if (! loc->name_map)
{
- loc->name_map = read_name_map (pfile, loc->name ? loc->name : ".");
+ loc->name_map = read_name_map (pfile, dname);
if (! loc->name_map)
return name;
}
- from = name + strlen (loc->name) + 1;
+ /* FIXME: this doesn't look right - NAME has been simplified. */
+ from = name + loc->len + 1;
for (map = loc->name_map; map; map = map->map_next)
if (!strcmp (map->map_from, from))
return name;
}
-/* Given a path FNAME, extract the directory component and place it
- onto the actual_dirs list. Return a pointer to the allocated
- file_name_list structure. These structures are used to implement
- current-directory "" include searching. */
-
-static struct file_name_list *
-actual_directory (pfile, fname)
- cpp_reader *pfile;
- const char *fname;
-{
- char *last_slash, *dir;
- size_t dlen;
- struct file_name_list *x;
-
- dir = xstrdup (fname);
- last_slash = strrchr (dir, '/');
- if (last_slash)
- {
- if (last_slash == dir)
- {
- dlen = 1;
- last_slash[1] = '\0';
- }
- else
- {
- dlen = last_slash - dir;
- *last_slash = '\0';
- }
- }
- else
- {
- free (dir);
- dir = xstrdup (".");
- dlen = 1;
- }
-
- if (dlen > pfile->max_include_len)
- pfile->max_include_len = dlen;
-
- for (x = pfile->actual_dirs; x; x = x->alloc)
- if (!strcmp (x->name, dir))
- {
- free (dir);
- return x;
- }
-
- /* Not found, make a new one. */
- x = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
- x->name = dir;
- x->nlen = dlen;
- x->next = CPP_OPTION (pfile, quote_include);
- x->alloc = pfile->actual_dirs;
- x->sysp = pfile->buffer->sysp;
- x->name_map = NULL;
-
- pfile->actual_dirs = x;
- return x;
-}
-
/* Simplify a path name in place, deleting redundant components. This
reduces OS overhead and guarantees that equivalent paths compare
the same (modulo symlinks).
unsigned int locks;
};
-/* List of directories to look for include files in. */
-struct file_name_list
+/* List of directories to look for include files in. */
+struct search_path
{
- struct file_name_list *next;
- struct file_name_list *alloc; /* for the cache of
- current directory entries */
- char *name;
- unsigned int nlen;
+ struct search_path *next;
+
+ /* NOTE: NAME may not be null terminated for the case of the current
+ file's directory! */
+ const char *name;
+ unsigned int len;
/* We use these to tell if the directory mentioned here is a duplicate
- of an earlier directory on the search path. */
+ of an earlier directory on the search path. */
ino_t ino;
dev_t dev;
- /* If the following is nonzero, it is a C-language system include
- directory. */
+ /* Non-zero if it is a system include directory. */
int sysp;
- /* Mapping of file names for this directory.
- Only used on MS-DOS and related platforms. */
+ /* Mapping of file names for this directory. Only used on MS-DOS
+ and related platforms. */
struct file_name_map *name_map;
};
/* Filename specified with #line command. */
const char *nominal_fname;
- /* Actual directory of this file, used only for "" includes */
- struct file_name_list *actual_dir;
-
/* Pointer into the include table. Used for include_next and
to record control macros. */
struct include_file *inc;
/* Buffer type. */
ENUM_BITFIELD (cpp_buffer_type) type : 8;
+
+ /* The directory of the this buffer's file. Its NAME member is not
+ allocated, so we don't need to worry about freeing it. */
+ struct search_path dir;
+
+ /* The directory to start searching for "" include files. Is either
+ "dir" above, or options.quote_include, depending upon whether -I-
+ was on the command line. */
+ struct search_path *search_from;
};
/* A cpp_reader encapsulates the "state" of a pre-processor run.
/* Tree of other included files. See cppfiles.c. */
struct splay_tree_s *all_include_files;
- /* Chain of `actual directory' file_name_list entries, for ""
- inclusion. */
- struct file_name_list *actual_dirs;
-
/* Current maximum length of directory names in the search path
for include files. (Altered as we get more of them.) */
unsigned int max_include_len;
{
struct pending_option *directive_head, *directive_tail;
- struct file_name_list *quote_head, *quote_tail;
- struct file_name_list *brack_head, *brack_tail;
- struct file_name_list *systm_head, *systm_tail;
- struct file_name_list *after_head, *after_tail;
+ struct search_path *quote_head, *quote_tail;
+ struct search_path *brack_head, *brack_tail;
+ struct search_path *systm_head, *systm_tail;
+ struct search_path *after_head, *after_tail;
struct pending_option *imacros_head, *imacros_tail;
struct pending_option *include_head, *include_tail;
static void init_builtins PARAMS ((cpp_reader *));
static void append_include_chain PARAMS ((cpp_reader *,
char *, int, int));
-struct file_name_list * remove_dup_dir PARAMS ((cpp_reader *,
- struct file_name_list *));
-struct file_name_list * remove_dup_dirs PARAMS ((cpp_reader *,
- struct file_name_list *));
+struct search_path * remove_dup_dir PARAMS ((cpp_reader *,
+ struct search_path *));
+struct search_path * remove_dup_dirs PARAMS ((cpp_reader *,
+ struct search_path *));
static void merge_include_chains PARAMS ((cpp_reader *));
static void do_includes PARAMS ((cpp_reader *,
struct pending_option *,
int cxx_aware ATTRIBUTE_UNUSED;
{
struct cpp_pending *pend = CPP_OPTION (pfile, pending);
- struct file_name_list *new;
+ struct search_path *new;
struct stat st;
unsigned int len;
if (len > pfile->max_include_len)
pfile->max_include_len = len;
- new = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
+ new = (struct search_path *) xmalloc (sizeof (struct search_path));
new->name = dir;
- new->nlen = len;
+ new->len = len;
new->ino = st.st_ino;
new->dev = st.st_dev;
if (path == SYSTEM)
new->sysp = 0;
new->name_map = NULL;
new->next = NULL;
- new->alloc = NULL;
switch (path)
{
/* Handle a duplicated include path. PREV is the link in the chain
before the duplicate. The duplicate is removed from the chain and
freed. Returns PREV. */
-struct file_name_list *
+struct search_path *
remove_dup_dir (pfile, prev)
cpp_reader *pfile;
- struct file_name_list *prev;
+ struct search_path *prev;
{
- struct file_name_list *cur = prev->next;
+ struct search_path *cur = prev->next;
if (CPP_OPTION (pfile, verbose))
fprintf (stderr, _("ignoring duplicate directory \"%s\"\n"), cur->name);
prev->next = cur->next;
- free (cur->name);
+ free ((PTR) cur->name);
free (cur);
return prev;
chain, or NULL if the chain is empty. This algorithm is quadratic
in the number of -I switches, which is acceptable since there
aren't usually that many of them. */
-struct file_name_list *
+struct search_path *
remove_dup_dirs (pfile, head)
cpp_reader *pfile;
- struct file_name_list *head;
+ struct search_path *head;
{
- struct file_name_list *prev = NULL, *cur, *other;
+ struct search_path *prev = NULL, *cur, *other;
for (cur = head; cur; cur = cur->next)
{
merge_include_chains (pfile)
cpp_reader *pfile;
{
- struct file_name_list *quote, *brack, *systm, *qtail;
+ struct search_path *quote, *brack, *systm, *qtail;
struct cpp_pending *pend = CPP_OPTION (pfile, pending);
cpp_reader *pfile;
{
int result;
- struct file_name_list *dir, *dirn;
+ struct search_path *dir, *dirn;
cpp_context *context, *contextn;
while (CPP_BUFFER (pfile) != NULL)
for (dir = CPP_OPTION (pfile, quote_include); dir; dir = dirn)
{
dirn = dir->next;
- free (dir->name);
+ free ((PTR) dir->name);
free (dir);
}
/* With -v, print the list of dirs to search. */
if (CPP_OPTION (pfile, verbose))
{
- struct file_name_list *l;
+ struct search_path *l;
fprintf (stderr, _("#include \"...\" search starts here:\n"));
for (l = CPP_OPTION (pfile, quote_include); l; l = l->next)
{