2 * Copyright 2007 Jon Loeliger, Freescale Semiconductor, Inc.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
27 /* A node in our list of directories to search for source/include files */
29 struct search_path *next; /* next node in list, NULL for end */
30 const char *dirname; /* name of directory to search */
33 /* This is the list of directories that we search for source files */
34 static struct search_path *search_path_head, **search_path_tail;
36 static char *get_dirname(const char *path)
38 const char *slash = strrchr(path, '/');
41 int len = slash - path;
42 char *dir = xmalloc(len + 1);
44 memcpy(dir, path, len);
51 FILE *depfile; /* = NULL */
52 struct srcfile_state *current_srcfile; /* = NULL */
54 /* Detect infinite include recursion. */
55 #define MAX_SRCFILE_DEPTH (100)
56 static int srcfile_depth; /* = 0 */
59 * Try to open a file in a given directory.
61 * If the filename is an absolute path, then dirname is ignored. If it is a
62 * relative path, then we look in that directory for the file.
64 * @param dirname Directory to look in, or NULL for none
65 * @param fname Filename to look for
66 * @param fp Set to NULL if file did not open
67 * Return: allocated filename on success (caller must free), NULL on failure
69 static char *try_open(const char *dirname, const char *fname, FILE **fp)
73 if (!dirname || fname[0] == '/')
74 fullname = xstrdup(fname);
76 fullname = join_path(dirname, fname);
78 *fp = fopen(fullname, "rb");
88 * Open a file for read access
90 * If it is a relative filename, we search the full search path for it.
92 * @param fname Filename to open
93 * @param fp Returns pointer to opened FILE, or NULL on failure
94 * Return: pointer to allocated filename, which caller must free
96 static char *fopen_any_on_path(const char *fname, FILE **fp)
98 const char *cur_dir = NULL;
99 struct search_path *node;
102 /* Try current directory first */
105 cur_dir = current_srcfile->dir;
106 fullname = try_open(cur_dir, fname, fp);
108 /* Failing that, try each search path in turn */
109 for (node = search_path_head; !*fp && node; node = node->next)
110 fullname = try_open(node->dirname, fname, fp);
115 FILE *srcfile_relative_open(const char *fname, char **fullnamep)
120 if (streq(fname, "-")) {
122 fullname = xstrdup("<stdin>");
124 fullname = fopen_any_on_path(fname, &f);
126 die("Couldn't open \"%s\": %s\n", fname,
131 fprintf(depfile, " %s", fullname);
134 *fullnamep = fullname;
141 void srcfile_push(const char *fname)
143 struct srcfile_state *srcfile;
145 if (srcfile_depth++ >= MAX_SRCFILE_DEPTH)
146 die("Includes nested too deeply");
148 srcfile = xmalloc(sizeof(*srcfile));
150 srcfile->f = srcfile_relative_open(fname, &srcfile->name);
151 srcfile->dir = get_dirname(srcfile->name);
152 srcfile->prev = current_srcfile;
157 current_srcfile = srcfile;
160 bool srcfile_pop(void)
162 struct srcfile_state *srcfile = current_srcfile;
166 current_srcfile = srcfile->prev;
168 if (fclose(srcfile->f))
169 die("Error closing \"%s\": %s\n", srcfile->name,
172 /* FIXME: We allow the srcfile_state structure to leak,
173 * because it could still be referenced from a location
174 * variable being carried through the parser somewhere. To
175 * fix this we could either allocate all the files from a
176 * table, or use a pool allocator. */
178 return current_srcfile ? true : false;
181 void srcfile_add_search_path(const char *dirname)
183 struct search_path *node;
185 /* Create the node */
186 node = xmalloc(sizeof(*node));
188 node->dirname = xstrdup(dirname);
190 /* Add to the end of our list */
191 if (search_path_tail)
192 *search_path_tail = node;
194 search_path_head = node;
195 search_path_tail = &node->next;
199 * The empty source position.
202 struct srcpos srcpos_empty = {
210 void srcpos_update(struct srcpos *pos, const char *text, int len)
214 pos->file = current_srcfile;
216 pos->first_line = current_srcfile->lineno;
217 pos->first_column = current_srcfile->colno;
219 for (i = 0; i < len; i++)
220 if (text[i] == '\n') {
221 current_srcfile->lineno++;
222 current_srcfile->colno = 1;
224 current_srcfile->colno++;
227 pos->last_line = current_srcfile->lineno;
228 pos->last_column = current_srcfile->colno;
232 srcpos_copy(struct srcpos *pos)
234 struct srcpos *pos_new;
236 pos_new = xmalloc(sizeof(struct srcpos));
237 memcpy(pos_new, pos, sizeof(struct srcpos));
243 srcpos_string(struct srcpos *pos)
245 const char *fname = "<no-file>";
248 if (pos->file && pos->file->name)
249 fname = pos->file->name;
251 if (pos->first_line != pos->last_line)
252 xasprintf(&pos_str, "%s:%d.%d-%d.%d", fname,
253 pos->first_line, pos->first_column,
254 pos->last_line, pos->last_column);
255 else if (pos->first_column != pos->last_column)
256 xasprintf(&pos_str, "%s:%d.%d-%d", fname,
257 pos->first_line, pos->first_column,
260 xasprintf(&pos_str, "%s:%d.%d", fname,
261 pos->first_line, pos->first_column);
266 void srcpos_verror(struct srcpos *pos, const char *prefix,
267 const char *fmt, va_list va)
271 srcstr = srcpos_string(pos);
273 fprintf(stderr, "%s: %s ", prefix, srcstr);
274 vfprintf(stderr, fmt, va);
275 fprintf(stderr, "\n");
280 void srcpos_error(struct srcpos *pos, const char *prefix,
281 const char *fmt, ...)
286 srcpos_verror(pos, prefix, fmt, va);
290 void srcpos_set_line(char *f, int l)
292 current_srcfile->name = f;
293 current_srcfile->lineno = l;