1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright 2007 Jon Loeliger, Freescale Semiconductor, Inc.
15 /* A node in our list of directories to search for source/include files */
17 struct search_path *next; /* next node in list, NULL for end */
18 const char *dirname; /* name of directory to search */
21 /* This is the list of directories that we search for source files */
22 static struct search_path *search_path_head, **search_path_tail;
24 /* Detect infinite include recursion. */
25 #define MAX_SRCFILE_DEPTH (200)
26 static int srcfile_depth; /* = 0 */
28 static char *get_dirname(const char *path)
30 const char *slash = strrchr(path, '/');
33 int len = slash - path;
34 char *dir = xmalloc(len + 1);
36 memcpy(dir, path, len);
43 FILE *depfile; /* = NULL */
44 struct srcfile_state *current_srcfile; /* = NULL */
45 static char *initial_path; /* = NULL */
46 static int initial_pathlen; /* = 0 */
47 static bool initial_cpp = true;
49 static void set_initial_path(char *fname)
51 int i, len = strlen(fname);
53 xasprintf(&initial_path, "%s", fname);
55 for (i = 0; i != len; i++)
56 if (initial_path[i] == '/')
60 static char *shorten_to_initial_path(char *fname)
62 char *p1, *p2, *prevslash1 = NULL;
65 for (p1 = fname, p2 = initial_path; *p1 && *p2; p1++, p2++) {
75 int diff = initial_pathlen - slashes, i, j;
76 int restlen = strlen(fname) - (p1 - fname);
79 res = xmalloc((3 * diff) + restlen + 1);
80 for (i = 0, j = 0; i != diff; i++) {
92 * Try to open a file in a given directory.
94 * If the filename is an absolute path, then dirname is ignored. If it is a
95 * relative path, then we look in that directory for the file.
97 * @param dirname Directory to look in, or NULL for none
98 * @param fname Filename to look for
99 * @param fp Set to NULL if file did not open
100 * @return allocated filename on success (caller must free), NULL on failure
102 static char *try_open(const char *dirname, const char *fname, FILE **fp)
106 if (!dirname || fname[0] == '/')
107 fullname = xstrdup(fname);
109 fullname = join_path(dirname, fname);
111 *fp = fopen(fullname, "rb");
121 * Open a file for read access
123 * If it is a relative filename, we search the full search path for it.
125 * @param fname Filename to open
126 * @param fp Returns pointer to opened FILE, or NULL on failure
127 * @return pointer to allocated filename, which caller must free
129 static char *fopen_any_on_path(const char *fname, FILE **fp)
131 const char *cur_dir = NULL;
132 struct search_path *node;
135 /* Try current directory first */
138 cur_dir = current_srcfile->dir;
139 fullname = try_open(cur_dir, fname, fp);
141 /* Failing that, try each search path in turn */
142 for (node = search_path_head; !*fp && node; node = node->next)
143 fullname = try_open(node->dirname, fname, fp);
148 FILE *srcfile_relative_open(const char *fname, char **fullnamep)
153 if (streq(fname, "-")) {
155 fullname = xstrdup("<stdin>");
157 fullname = fopen_any_on_path(fname, &f);
159 die("Couldn't open \"%s\": %s\n", fname,
164 fprintf(depfile, " %s", fullname);
167 *fullnamep = fullname;
174 void srcfile_push(const char *fname)
176 struct srcfile_state *srcfile;
178 if (srcfile_depth++ >= MAX_SRCFILE_DEPTH)
179 die("Includes nested too deeply");
181 srcfile = xmalloc(sizeof(*srcfile));
183 srcfile->f = srcfile_relative_open(fname, &srcfile->name);
184 srcfile->dir = get_dirname(srcfile->name);
185 srcfile->prev = current_srcfile;
190 current_srcfile = srcfile;
192 if (srcfile_depth == 1)
193 set_initial_path(srcfile->name);
196 bool srcfile_pop(void)
198 struct srcfile_state *srcfile = current_srcfile;
202 current_srcfile = srcfile->prev;
204 if (fclose(srcfile->f))
205 die("Error closing \"%s\": %s\n", srcfile->name,
208 /* FIXME: We allow the srcfile_state structure to leak,
209 * because it could still be referenced from a location
210 * variable being carried through the parser somewhere. To
211 * fix this we could either allocate all the files from a
212 * table, or use a pool allocator. */
214 return current_srcfile ? true : false;
217 void srcfile_add_search_path(const char *dirname)
219 struct search_path *node;
221 /* Create the node */
222 node = xmalloc(sizeof(*node));
224 node->dirname = xstrdup(dirname);
226 /* Add to the end of our list */
227 if (search_path_tail)
228 *search_path_tail = node;
230 search_path_head = node;
231 search_path_tail = &node->next;
234 void srcpos_update(struct srcpos *pos, const char *text, int len)
238 pos->file = current_srcfile;
240 pos->first_line = current_srcfile->lineno;
241 pos->first_column = current_srcfile->colno;
243 for (i = 0; i < len; i++)
244 if (text[i] == '\n') {
245 current_srcfile->lineno++;
246 current_srcfile->colno = 1;
248 current_srcfile->colno++;
251 pos->last_line = current_srcfile->lineno;
252 pos->last_column = current_srcfile->colno;
256 srcpos_copy(struct srcpos *pos)
258 struct srcpos *pos_new;
259 struct srcfile_state *srcfile_state;
264 pos_new = xmalloc(sizeof(struct srcpos));
265 assert(pos->next == NULL);
266 memcpy(pos_new, pos, sizeof(struct srcpos));
268 /* allocate without free */
269 srcfile_state = xmalloc(sizeof(struct srcfile_state));
270 memcpy(srcfile_state, pos->file, sizeof(struct srcfile_state));
271 pos_new->file = srcfile_state;
276 struct srcpos *srcpos_extend(struct srcpos *pos, struct srcpos *newtail)
283 for (p = pos; p->next != NULL; p = p->next);
289 srcpos_string(struct srcpos *pos)
291 const char *fname = "<no-file>";
294 if (pos->file && pos->file->name)
295 fname = pos->file->name;
298 if (pos->first_line != pos->last_line)
299 xasprintf(&pos_str, "%s:%d.%d-%d.%d", fname,
300 pos->first_line, pos->first_column,
301 pos->last_line, pos->last_column);
302 else if (pos->first_column != pos->last_column)
303 xasprintf(&pos_str, "%s:%d.%d-%d", fname,
304 pos->first_line, pos->first_column,
307 xasprintf(&pos_str, "%s:%d.%d", fname,
308 pos->first_line, pos->first_column);
314 srcpos_string_comment(struct srcpos *pos, bool first_line, int level)
316 char *pos_str, *fresh_fname = NULL, *first, *rest;
321 xasprintf(&pos_str, "<no-file>:<no-line>");
330 else if (!pos->file->name)
331 fname = "<no-filename>";
333 fname = pos->file->name;
335 fresh_fname = shorten_to_initial_path(pos->file->name);
339 fname = pos->file->name;
343 xasprintf(&first, "%s:%d:%d-%d:%d", fname,
344 pos->first_line, pos->first_column,
345 pos->last_line, pos->last_column);
347 xasprintf(&first, "%s:%d", fname,
348 first_line ? pos->first_line : pos->last_line);
353 if (pos->next != NULL) {
354 rest = srcpos_string_comment(pos->next, first_line, level);
355 xasprintf(&pos_str, "%s, %s", first, rest);
365 char *srcpos_string_first(struct srcpos *pos, int level)
367 return srcpos_string_comment(pos, true, level);
370 char *srcpos_string_last(struct srcpos *pos, int level)
372 return srcpos_string_comment(pos, false, level);
375 void srcpos_verror(struct srcpos *pos, const char *prefix,
376 const char *fmt, va_list va)
380 srcstr = srcpos_string(pos);
382 fprintf(stderr, "%s: %s ", prefix, srcstr);
383 vfprintf(stderr, fmt, va);
384 fprintf(stderr, "\n");
389 void srcpos_error(struct srcpos *pos, const char *prefix,
390 const char *fmt, ...)
395 srcpos_verror(pos, prefix, fmt, va);
399 void srcpos_set_line(char *f, int l)
401 current_srcfile->name = f;
402 current_srcfile->lineno = l;