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;
37 static char *get_dirname(const char *path)
39 const char *slash = strrchr(path, '/');
42 int len = slash - path;
43 char *dir = xmalloc(len + 1);
45 memcpy(dir, path, len);
52 FILE *depfile; /* = NULL */
53 struct srcfile_state *current_srcfile; /* = NULL */
55 /* Detect infinite include recursion. */
56 #define MAX_SRCFILE_DEPTH (100)
57 static int srcfile_depth; /* = 0 */
61 * Try to open a file in a given directory.
63 * If the filename is an absolute path, then dirname is ignored. If it is a
64 * relative path, then we look in that directory for the file.
66 * @param dirname Directory to look in, or NULL for none
67 * @param fname Filename to look for
68 * @param fp Set to NULL if file did not open
69 * @return allocated filename on success (caller must free), NULL on failure
71 static char *try_open(const char *dirname, const char *fname, FILE **fp)
75 if (!dirname || fname[0] == '/')
76 fullname = xstrdup(fname);
78 fullname = join_path(dirname, fname);
80 *fp = fopen(fullname, "rb");
90 * Open a file for read access
92 * If it is a relative filename, we search the full search path for it.
94 * @param fname Filename to open
95 * @param fp Returns pointer to opened FILE, or NULL on failure
96 * @return pointer to allocated filename, which caller must free
98 static char *fopen_any_on_path(const char *fname, FILE **fp)
100 const char *cur_dir = NULL;
101 struct search_path *node;
104 /* Try current directory first */
107 cur_dir = current_srcfile->dir;
108 fullname = try_open(cur_dir, fname, fp);
110 /* Failing that, try each search path in turn */
111 for (node = search_path_head; !*fp && node; node = node->next)
112 fullname = try_open(node->dirname, fname, fp);
117 FILE *srcfile_relative_open(const char *fname, char **fullnamep)
122 if (streq(fname, "-")) {
124 fullname = xstrdup("<stdin>");
126 fullname = fopen_any_on_path(fname, &f);
128 die("Couldn't open \"%s\": %s\n", fname,
133 fprintf(depfile, " %s", fullname);
136 *fullnamep = fullname;
143 void srcfile_push(const char *fname)
145 struct srcfile_state *srcfile;
147 if (srcfile_depth++ >= MAX_SRCFILE_DEPTH)
148 die("Includes nested too deeply");
150 srcfile = xmalloc(sizeof(*srcfile));
152 srcfile->f = srcfile_relative_open(fname, &srcfile->name);
153 srcfile->dir = get_dirname(srcfile->name);
154 srcfile->prev = current_srcfile;
159 current_srcfile = srcfile;
162 bool srcfile_pop(void)
164 struct srcfile_state *srcfile = current_srcfile;
168 current_srcfile = srcfile->prev;
170 if (fclose(srcfile->f))
171 die("Error closing \"%s\": %s\n", srcfile->name,
174 /* FIXME: We allow the srcfile_state structure to leak,
175 * because it could still be referenced from a location
176 * variable being carried through the parser somewhere. To
177 * fix this we could either allocate all the files from a
178 * table, or use a pool allocator. */
180 return current_srcfile ? true : false;
183 void srcfile_add_search_path(const char *dirname)
185 struct search_path *node;
187 /* Create the node */
188 node = xmalloc(sizeof(*node));
190 node->dirname = xstrdup(dirname);
192 /* Add to the end of our list */
193 if (search_path_tail)
194 *search_path_tail = node;
196 search_path_head = node;
197 search_path_tail = &node->next;
201 * The empty source position.
204 struct srcpos srcpos_empty = {
214 void srcpos_update(struct srcpos *pos, const char *text, int len)
218 pos->file = current_srcfile;
220 pos->first_line = current_srcfile->lineno;
221 pos->first_column = current_srcfile->colno;
223 for (i = 0; i < len; i++)
224 if (text[i] == '\n') {
225 current_srcfile->lineno++;
226 current_srcfile->colno = 1;
227 } else if (text[i] == '\t') {
228 current_srcfile->colno =
229 ALIGN(current_srcfile->colno, TAB_SIZE);
231 current_srcfile->colno++;
234 pos->last_line = current_srcfile->lineno;
235 pos->last_column = current_srcfile->colno;
239 srcpos_copy(struct srcpos *pos)
241 struct srcpos *pos_new;
243 pos_new = xmalloc(sizeof(struct srcpos));
244 memcpy(pos_new, pos, sizeof(struct srcpos));
250 srcpos_string(struct srcpos *pos)
252 const char *fname = "<no-file>";
255 if (pos->file && pos->file->name)
256 fname = pos->file->name;
259 if (pos->first_line != pos->last_line)
260 xasprintf(&pos_str, "%s:%d.%d-%d.%d", fname,
261 pos->first_line, pos->first_column,
262 pos->last_line, pos->last_column);
263 else if (pos->first_column != pos->last_column)
264 xasprintf(&pos_str, "%s:%d.%d-%d", fname,
265 pos->first_line, pos->first_column,
268 xasprintf(&pos_str, "%s:%d.%d", fname,
269 pos->first_line, pos->first_column);
274 void srcpos_verror(struct srcpos *pos, const char *prefix,
275 const char *fmt, va_list va)
279 srcstr = srcpos_string(pos);
281 fprintf(stderr, "%s: %s ", prefix, srcstr);
282 vfprintf(stderr, fmt, va);
283 fprintf(stderr, "\n");
288 void srcpos_error(struct srcpos *pos, const char *prefix,
289 const char *fmt, ...)
294 srcpos_verror(pos, prefix, fmt, va);
298 void srcpos_set_line(char *f, int l)
300 current_srcfile->name = f;
301 current_srcfile->lineno = l;