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 /* Detect infinite include recursion. */
37 #define MAX_SRCFILE_DEPTH (100)
38 static int srcfile_depth
; /* = 0 */
40 static char *get_dirname(const char *path
)
42 const char *slash
= strrchr(path
, '/');
45 int len
= slash
- path
;
46 char *dir
= xmalloc(len
+ 1);
48 memcpy(dir
, path
, len
);
55 FILE *depfile
; /* = NULL */
56 struct srcfile_state
*current_srcfile
; /* = NULL */
57 static char *initial_path
; /* = NULL */
58 static int initial_pathlen
; /* = 0 */
59 static bool initial_cpp
= true;
61 static void set_initial_path(char *fname
)
63 int i
, len
= strlen(fname
);
65 xasprintf(&initial_path
, "%s", fname
);
67 for (i
= 0; i
!= len
; i
++)
68 if (initial_path
[i
] == '/')
72 static char *shorten_to_initial_path(char *fname
)
74 char *p1
, *p2
, *prevslash1
= NULL
;
77 for (p1
= fname
, p2
= initial_path
; *p1
&& *p2
; p1
++, p2
++) {
87 int diff
= initial_pathlen
- slashes
, i
, j
;
88 int restlen
= strlen(fname
) - (p1
- fname
);
91 res
= xmalloc((3 * diff
) + restlen
+ 1);
92 for (i
= 0, j
= 0; i
!= diff
; i
++) {
104 * Try to open a file in a given directory.
106 * If the filename is an absolute path, then dirname is ignored. If it is a
107 * relative path, then we look in that directory for the file.
109 * @param dirname Directory to look in, or NULL for none
110 * @param fname Filename to look for
111 * @param fp Set to NULL if file did not open
112 * @return allocated filename on success (caller must free), NULL on failure
114 static char *try_open(const char *dirname
, const char *fname
, FILE **fp
)
118 if (!dirname
|| fname
[0] == '/')
119 fullname
= xstrdup(fname
);
121 fullname
= join_path(dirname
, fname
);
123 *fp
= fopen(fullname
, "rb");
133 * Open a file for read access
135 * If it is a relative filename, we search the full search path for it.
137 * @param fname Filename to open
138 * @param fp Returns pointer to opened FILE, or NULL on failure
139 * @return pointer to allocated filename, which caller must free
141 static char *fopen_any_on_path(const char *fname
, FILE **fp
)
143 const char *cur_dir
= NULL
;
144 struct search_path
*node
;
147 /* Try current directory first */
150 cur_dir
= current_srcfile
->dir
;
151 fullname
= try_open(cur_dir
, fname
, fp
);
153 /* Failing that, try each search path in turn */
154 for (node
= search_path_head
; !*fp
&& node
; node
= node
->next
)
155 fullname
= try_open(node
->dirname
, fname
, fp
);
160 FILE *srcfile_relative_open(const char *fname
, char **fullnamep
)
165 if (streq(fname
, "-")) {
167 fullname
= xstrdup("<stdin>");
169 fullname
= fopen_any_on_path(fname
, &f
);
171 die("Couldn't open \"%s\": %s\n", fname
,
176 fprintf(depfile
, " %s", fullname
);
179 *fullnamep
= fullname
;
186 void srcfile_push(const char *fname
)
188 struct srcfile_state
*srcfile
;
190 if (srcfile_depth
++ >= MAX_SRCFILE_DEPTH
)
191 die("Includes nested too deeply");
193 srcfile
= xmalloc(sizeof(*srcfile
));
195 srcfile
->f
= srcfile_relative_open(fname
, &srcfile
->name
);
196 srcfile
->dir
= get_dirname(srcfile
->name
);
197 srcfile
->prev
= current_srcfile
;
202 current_srcfile
= srcfile
;
204 if (srcfile_depth
== 1)
205 set_initial_path(srcfile
->name
);
208 bool srcfile_pop(void)
210 struct srcfile_state
*srcfile
= current_srcfile
;
214 current_srcfile
= srcfile
->prev
;
216 if (fclose(srcfile
->f
))
217 die("Error closing \"%s\": %s\n", srcfile
->name
,
220 /* FIXME: We allow the srcfile_state structure to leak,
221 * because it could still be referenced from a location
222 * variable being carried through the parser somewhere. To
223 * fix this we could either allocate all the files from a
224 * table, or use a pool allocator. */
226 return current_srcfile
? true : false;
229 void srcfile_add_search_path(const char *dirname
)
231 struct search_path
*node
;
233 /* Create the node */
234 node
= xmalloc(sizeof(*node
));
236 node
->dirname
= xstrdup(dirname
);
238 /* Add to the end of our list */
239 if (search_path_tail
)
240 *search_path_tail
= node
;
242 search_path_head
= node
;
243 search_path_tail
= &node
->next
;
246 void srcpos_update(struct srcpos
*pos
, const char *text
, int len
)
250 pos
->file
= current_srcfile
;
252 pos
->first_line
= current_srcfile
->lineno
;
253 pos
->first_column
= current_srcfile
->colno
;
255 for (i
= 0; i
< len
; i
++)
256 if (text
[i
] == '\n') {
257 current_srcfile
->lineno
++;
258 current_srcfile
->colno
= 1;
260 current_srcfile
->colno
++;
263 pos
->last_line
= current_srcfile
->lineno
;
264 pos
->last_column
= current_srcfile
->colno
;
268 srcpos_copy(struct srcpos
*pos
)
270 struct srcpos
*pos_new
;
271 struct srcfile_state
*srcfile_state
;
276 pos_new
= xmalloc(sizeof(struct srcpos
));
277 assert(pos
->next
== NULL
);
278 memcpy(pos_new
, pos
, sizeof(struct srcpos
));
280 /* allocate without free */
281 srcfile_state
= xmalloc(sizeof(struct srcfile_state
));
282 memcpy(srcfile_state
, pos
->file
, sizeof(struct srcfile_state
));
283 pos_new
->file
= srcfile_state
;
288 struct srcpos
*srcpos_extend(struct srcpos
*pos
, struct srcpos
*newtail
)
295 for (p
= pos
; p
->next
!= NULL
; p
= p
->next
);
301 srcpos_string(struct srcpos
*pos
)
303 const char *fname
= "<no-file>";
306 if (pos
->file
&& pos
->file
->name
)
307 fname
= pos
->file
->name
;
310 if (pos
->first_line
!= pos
->last_line
)
311 xasprintf(&pos_str
, "%s:%d.%d-%d.%d", fname
,
312 pos
->first_line
, pos
->first_column
,
313 pos
->last_line
, pos
->last_column
);
314 else if (pos
->first_column
!= pos
->last_column
)
315 xasprintf(&pos_str
, "%s:%d.%d-%d", fname
,
316 pos
->first_line
, pos
->first_column
,
319 xasprintf(&pos_str
, "%s:%d.%d", fname
,
320 pos
->first_line
, pos
->first_column
);
326 srcpos_string_comment(struct srcpos
*pos
, bool first_line
, int level
)
328 char *pos_str
, *fname
, *first
, *rest
;
329 bool fresh_fname
= false;
333 xasprintf(&pos_str
, "<no-file>:<no-line>");
342 else if (!pos
->file
->name
)
343 fname
= "<no-filename>";
345 fname
= pos
->file
->name
;
347 fname
= shorten_to_initial_path(pos
->file
->name
);
351 fname
= pos
->file
->name
;
355 xasprintf(&first
, "%s:%d:%d-%d:%d", fname
,
356 pos
->first_line
, pos
->first_column
,
357 pos
->last_line
, pos
->last_column
);
359 xasprintf(&first
, "%s:%d", fname
,
360 first_line
? pos
->first_line
: pos
->last_line
);
365 if (pos
->next
!= NULL
) {
366 rest
= srcpos_string_comment(pos
->next
, first_line
, level
);
367 xasprintf(&pos_str
, "%s, %s", first
, rest
);
377 char *srcpos_string_first(struct srcpos
*pos
, int level
)
379 return srcpos_string_comment(pos
, true, level
);
382 char *srcpos_string_last(struct srcpos
*pos
, int level
)
384 return srcpos_string_comment(pos
, false, level
);
387 void srcpos_verror(struct srcpos
*pos
, const char *prefix
,
388 const char *fmt
, va_list va
)
392 srcstr
= srcpos_string(pos
);
394 fprintf(stderr
, "%s: %s ", prefix
, srcstr
);
395 vfprintf(stderr
, fmt
, va
);
396 fprintf(stderr
, "\n");
401 void srcpos_error(struct srcpos
*pos
, const char *prefix
,
402 const char *fmt
, ...)
407 srcpos_verror(pos
, prefix
, fmt
, va
);
411 void srcpos_set_line(char *f
, int l
)
413 current_srcfile
->name
= f
;
414 current_srcfile
->lineno
= l
;