1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright 2007 Jon Loeliger, Freescale Semiconductor, Inc.
13 /* A node in our list of directories to search for source/include files */
15 struct search_path
*next
; /* next node in list, NULL for end */
16 const char *dirname
; /* name of directory to search */
19 /* This is the list of directories that we search for source files */
20 static struct search_path
*search_path_head
, **search_path_tail
;
22 /* Detect infinite include recursion. */
23 #define MAX_SRCFILE_DEPTH (100)
24 static int srcfile_depth
; /* = 0 */
26 static char *get_dirname(const char *path
)
28 const char *slash
= strrchr(path
, '/');
31 int len
= slash
- path
;
32 char *dir
= xmalloc(len
+ 1);
34 memcpy(dir
, path
, len
);
41 FILE *depfile
; /* = NULL */
42 struct srcfile_state
*current_srcfile
; /* = NULL */
43 static char *initial_path
; /* = NULL */
44 static int initial_pathlen
; /* = 0 */
45 static bool initial_cpp
= true;
47 static void set_initial_path(char *fname
)
49 int i
, len
= strlen(fname
);
51 xasprintf(&initial_path
, "%s", fname
);
53 for (i
= 0; i
!= len
; i
++)
54 if (initial_path
[i
] == '/')
58 static char *shorten_to_initial_path(char *fname
)
60 char *p1
, *p2
, *prevslash1
= NULL
;
63 for (p1
= fname
, p2
= initial_path
; *p1
&& *p2
; p1
++, p2
++) {
73 int diff
= initial_pathlen
- slashes
, i
, j
;
74 int restlen
= strlen(fname
) - (p1
- fname
);
77 res
= xmalloc((3 * diff
) + restlen
+ 1);
78 for (i
= 0, j
= 0; i
!= diff
; i
++) {
90 * Try to open a file in a given directory.
92 * If the filename is an absolute path, then dirname is ignored. If it is a
93 * relative path, then we look in that directory for the file.
95 * @param dirname Directory to look in, or NULL for none
96 * @param fname Filename to look for
97 * @param fp Set to NULL if file did not open
98 * @return allocated filename on success (caller must free), NULL on failure
100 static char *try_open(const char *dirname
, const char *fname
, FILE **fp
)
104 if (!dirname
|| fname
[0] == '/')
105 fullname
= xstrdup(fname
);
107 fullname
= join_path(dirname
, fname
);
109 *fp
= fopen(fullname
, "rb");
119 * Open a file for read access
121 * If it is a relative filename, we search the full search path for it.
123 * @param fname Filename to open
124 * @param fp Returns pointer to opened FILE, or NULL on failure
125 * @return pointer to allocated filename, which caller must free
127 static char *fopen_any_on_path(const char *fname
, FILE **fp
)
129 const char *cur_dir
= NULL
;
130 struct search_path
*node
;
133 /* Try current directory first */
136 cur_dir
= current_srcfile
->dir
;
137 fullname
= try_open(cur_dir
, fname
, fp
);
139 /* Failing that, try each search path in turn */
140 for (node
= search_path_head
; !*fp
&& node
; node
= node
->next
)
141 fullname
= try_open(node
->dirname
, fname
, fp
);
146 FILE *srcfile_relative_open(const char *fname
, char **fullnamep
)
151 if (streq(fname
, "-")) {
153 fullname
= xstrdup("<stdin>");
155 fullname
= fopen_any_on_path(fname
, &f
);
157 die("Couldn't open \"%s\": %s\n", fname
,
162 fprintf(depfile
, " %s", fullname
);
165 *fullnamep
= fullname
;
172 void srcfile_push(const char *fname
)
174 struct srcfile_state
*srcfile
;
176 if (srcfile_depth
++ >= MAX_SRCFILE_DEPTH
)
177 die("Includes nested too deeply");
179 srcfile
= xmalloc(sizeof(*srcfile
));
181 srcfile
->f
= srcfile_relative_open(fname
, &srcfile
->name
);
182 srcfile
->dir
= get_dirname(srcfile
->name
);
183 srcfile
->prev
= current_srcfile
;
188 current_srcfile
= srcfile
;
190 if (srcfile_depth
== 1)
191 set_initial_path(srcfile
->name
);
194 bool srcfile_pop(void)
196 struct srcfile_state
*srcfile
= current_srcfile
;
200 current_srcfile
= srcfile
->prev
;
202 if (fclose(srcfile
->f
))
203 die("Error closing \"%s\": %s\n", srcfile
->name
,
206 /* FIXME: We allow the srcfile_state structure to leak,
207 * because it could still be referenced from a location
208 * variable being carried through the parser somewhere. To
209 * fix this we could either allocate all the files from a
210 * table, or use a pool allocator. */
212 return current_srcfile
? true : false;
215 void srcfile_add_search_path(const char *dirname
)
217 struct search_path
*node
;
219 /* Create the node */
220 node
= xmalloc(sizeof(*node
));
222 node
->dirname
= xstrdup(dirname
);
224 /* Add to the end of our list */
225 if (search_path_tail
)
226 *search_path_tail
= node
;
228 search_path_head
= node
;
229 search_path_tail
= &node
->next
;
232 void srcpos_update(struct srcpos
*pos
, const char *text
, int len
)
236 pos
->file
= current_srcfile
;
238 pos
->first_line
= current_srcfile
->lineno
;
239 pos
->first_column
= current_srcfile
->colno
;
241 for (i
= 0; i
< len
; i
++)
242 if (text
[i
] == '\n') {
243 current_srcfile
->lineno
++;
244 current_srcfile
->colno
= 1;
246 current_srcfile
->colno
++;
249 pos
->last_line
= current_srcfile
->lineno
;
250 pos
->last_column
= current_srcfile
->colno
;
254 srcpos_copy(struct srcpos
*pos
)
256 struct srcpos
*pos_new
;
257 struct srcfile_state
*srcfile_state
;
262 pos_new
= xmalloc(sizeof(struct srcpos
));
263 assert(pos
->next
== NULL
);
264 memcpy(pos_new
, pos
, sizeof(struct srcpos
));
266 /* allocate without free */
267 srcfile_state
= xmalloc(sizeof(struct srcfile_state
));
268 memcpy(srcfile_state
, pos
->file
, sizeof(struct srcfile_state
));
269 pos_new
->file
= srcfile_state
;
274 struct srcpos
*srcpos_extend(struct srcpos
*pos
, struct srcpos
*newtail
)
281 for (p
= pos
; p
->next
!= NULL
; p
= p
->next
);
287 srcpos_string(struct srcpos
*pos
)
289 const char *fname
= "<no-file>";
292 if (pos
->file
&& pos
->file
->name
)
293 fname
= pos
->file
->name
;
296 if (pos
->first_line
!= pos
->last_line
)
297 xasprintf(&pos_str
, "%s:%d.%d-%d.%d", fname
,
298 pos
->first_line
, pos
->first_column
,
299 pos
->last_line
, pos
->last_column
);
300 else if (pos
->first_column
!= pos
->last_column
)
301 xasprintf(&pos_str
, "%s:%d.%d-%d", fname
,
302 pos
->first_line
, pos
->first_column
,
305 xasprintf(&pos_str
, "%s:%d.%d", fname
,
306 pos
->first_line
, pos
->first_column
);
312 srcpos_string_comment(struct srcpos
*pos
, bool first_line
, int level
)
314 char *pos_str
, *fname
, *first
, *rest
;
315 bool fresh_fname
= false;
319 xasprintf(&pos_str
, "<no-file>:<no-line>");
328 else if (!pos
->file
->name
)
329 fname
= "<no-filename>";
331 fname
= pos
->file
->name
;
333 fname
= shorten_to_initial_path(pos
->file
->name
);
337 fname
= pos
->file
->name
;
341 xasprintf(&first
, "%s:%d:%d-%d:%d", fname
,
342 pos
->first_line
, pos
->first_column
,
343 pos
->last_line
, pos
->last_column
);
345 xasprintf(&first
, "%s:%d", fname
,
346 first_line
? pos
->first_line
: pos
->last_line
);
351 if (pos
->next
!= NULL
) {
352 rest
= srcpos_string_comment(pos
->next
, first_line
, level
);
353 xasprintf(&pos_str
, "%s, %s", first
, rest
);
363 char *srcpos_string_first(struct srcpos
*pos
, int level
)
365 return srcpos_string_comment(pos
, true, level
);
368 char *srcpos_string_last(struct srcpos
*pos
, int level
)
370 return srcpos_string_comment(pos
, false, level
);
373 void srcpos_verror(struct srcpos
*pos
, const char *prefix
,
374 const char *fmt
, va_list va
)
378 srcstr
= srcpos_string(pos
);
380 fprintf(stderr
, "%s: %s ", prefix
, srcstr
);
381 vfprintf(stderr
, fmt
, va
);
382 fprintf(stderr
, "\n");
387 void srcpos_error(struct srcpos
*pos
, const char *prefix
,
388 const char *fmt
, ...)
393 srcpos_verror(pos
, prefix
, fmt
, va
);
397 void srcpos_set_line(char *f
, int l
)
399 current_srcfile
->name
= f
;
400 current_srcfile
->lineno
= l
;