11a59d1b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
29fffb55fSDavid Gibson /*
39fffb55fSDavid Gibson * Copyright 2007 Jon Loeliger, Freescale Semiconductor, Inc.
49fffb55fSDavid Gibson */
59fffb55fSDavid Gibson
6*12d638f4SRob Herring (Arm) #ifndef _GNU_SOURCE
7658f29a5SJohn Bonesio #define _GNU_SOURCE
8*12d638f4SRob Herring (Arm) #endif
9658f29a5SJohn Bonesio
10658f29a5SJohn Bonesio #include <stdio.h>
11658f29a5SJohn Bonesio
129fffb55fSDavid Gibson #include "dtc.h"
139fffb55fSDavid Gibson #include "srcpos.h"
149fffb55fSDavid Gibson
15cd296721SStephen Warren /* A node in our list of directories to search for source/include files */
16cd296721SStephen Warren struct search_path {
17cd296721SStephen Warren struct search_path *next; /* next node in list, NULL for end */
18cd296721SStephen Warren const char *dirname; /* name of directory to search */
19cd296721SStephen Warren };
20cd296721SStephen Warren
21cd296721SStephen Warren /* This is the list of directories that we search for source files */
22cd296721SStephen Warren static struct search_path *search_path_head, **search_path_tail;
23cd296721SStephen Warren
24c2e7075cSRob Herring /* Detect infinite include recursion. */
2579edff12SRob Herring #define MAX_SRCFILE_DEPTH (200)
26c2e7075cSRob Herring static int srcfile_depth; /* = 0 */
279fffb55fSDavid Gibson
get_dirname(const char * path)2847605971SRob Herring static char *get_dirname(const char *path)
299fffb55fSDavid Gibson {
30658f29a5SJohn Bonesio const char *slash = strrchr(path, '/');
31658f29a5SJohn Bonesio
32658f29a5SJohn Bonesio if (slash) {
33658f29a5SJohn Bonesio int len = slash - path;
34658f29a5SJohn Bonesio char *dir = xmalloc(len + 1);
35658f29a5SJohn Bonesio
36658f29a5SJohn Bonesio memcpy(dir, path, len);
37658f29a5SJohn Bonesio dir[len] = '\0';
38658f29a5SJohn Bonesio return dir;
39658f29a5SJohn Bonesio }
40658f29a5SJohn Bonesio return NULL;
41658f29a5SJohn Bonesio }
42658f29a5SJohn Bonesio
43136ec204SStephen Warren FILE *depfile; /* = NULL */
44658f29a5SJohn Bonesio struct srcfile_state *current_srcfile; /* = NULL */
45c2e7075cSRob Herring static char *initial_path; /* = NULL */
46c2e7075cSRob Herring static int initial_pathlen; /* = 0 */
47c2e7075cSRob Herring static bool initial_cpp = true;
48658f29a5SJohn Bonesio
set_initial_path(char * fname)49c2e7075cSRob Herring static void set_initial_path(char *fname)
50c2e7075cSRob Herring {
51c2e7075cSRob Herring int i, len = strlen(fname);
52658f29a5SJohn Bonesio
53c2e7075cSRob Herring xasprintf(&initial_path, "%s", fname);
54c2e7075cSRob Herring initial_pathlen = 0;
55c2e7075cSRob Herring for (i = 0; i != len; i++)
56c2e7075cSRob Herring if (initial_path[i] == '/')
57c2e7075cSRob Herring initial_pathlen++;
58c2e7075cSRob Herring }
59c2e7075cSRob Herring
shorten_to_initial_path(char * fname)60c2e7075cSRob Herring static char *shorten_to_initial_path(char *fname)
61c2e7075cSRob Herring {
62c2e7075cSRob Herring char *p1, *p2, *prevslash1 = NULL;
63c2e7075cSRob Herring int slashes = 0;
64c2e7075cSRob Herring
65c2e7075cSRob Herring for (p1 = fname, p2 = initial_path; *p1 && *p2; p1++, p2++) {
66c2e7075cSRob Herring if (*p1 != *p2)
67c2e7075cSRob Herring break;
68c2e7075cSRob Herring if (*p1 == '/') {
69c2e7075cSRob Herring prevslash1 = p1;
70c2e7075cSRob Herring slashes++;
71c2e7075cSRob Herring }
72c2e7075cSRob Herring }
73c2e7075cSRob Herring p1 = prevslash1 + 1;
74c2e7075cSRob Herring if (prevslash1) {
75c2e7075cSRob Herring int diff = initial_pathlen - slashes, i, j;
76c2e7075cSRob Herring int restlen = strlen(fname) - (p1 - fname);
77c2e7075cSRob Herring char *res;
78c2e7075cSRob Herring
79c2e7075cSRob Herring res = xmalloc((3 * diff) + restlen + 1);
80c2e7075cSRob Herring for (i = 0, j = 0; i != diff; i++) {
81c2e7075cSRob Herring res[j++] = '.';
82c2e7075cSRob Herring res[j++] = '.';
83c2e7075cSRob Herring res[j++] = '/';
84c2e7075cSRob Herring }
85c2e7075cSRob Herring strcpy(res + j, p1);
86c2e7075cSRob Herring return res;
87c2e7075cSRob Herring }
88c2e7075cSRob Herring return NULL;
89c2e7075cSRob Herring }
90cd296721SStephen Warren
91cd296721SStephen Warren /**
92cd296721SStephen Warren * Try to open a file in a given directory.
93cd296721SStephen Warren *
94cd296721SStephen Warren * If the filename is an absolute path, then dirname is ignored. If it is a
95cd296721SStephen Warren * relative path, then we look in that directory for the file.
96cd296721SStephen Warren *
97cd296721SStephen Warren * @param dirname Directory to look in, or NULL for none
98cd296721SStephen Warren * @param fname Filename to look for
99cd296721SStephen Warren * @param fp Set to NULL if file did not open
100cd296721SStephen Warren * @return allocated filename on success (caller must free), NULL on failure
101cd296721SStephen Warren */
try_open(const char * dirname,const char * fname,FILE ** fp)102cd296721SStephen Warren static char *try_open(const char *dirname, const char *fname, FILE **fp)
103cd296721SStephen Warren {
104cd296721SStephen Warren char *fullname;
105cd296721SStephen Warren
106cd296721SStephen Warren if (!dirname || fname[0] == '/')
107cd296721SStephen Warren fullname = xstrdup(fname);
108cd296721SStephen Warren else
109cd296721SStephen Warren fullname = join_path(dirname, fname);
110cd296721SStephen Warren
11147605971SRob Herring *fp = fopen(fullname, "rb");
112cd296721SStephen Warren if (!*fp) {
113cd296721SStephen Warren free(fullname);
114cd296721SStephen Warren fullname = NULL;
115cd296721SStephen Warren }
116cd296721SStephen Warren
117cd296721SStephen Warren return fullname;
118cd296721SStephen Warren }
119cd296721SStephen Warren
120cd296721SStephen Warren /**
121cd296721SStephen Warren * Open a file for read access
122cd296721SStephen Warren *
123cd296721SStephen Warren * If it is a relative filename, we search the full search path for it.
124cd296721SStephen Warren *
125cd296721SStephen Warren * @param fname Filename to open
126cd296721SStephen Warren * @param fp Returns pointer to opened FILE, or NULL on failure
127cd296721SStephen Warren * @return pointer to allocated filename, which caller must free
128cd296721SStephen Warren */
fopen_any_on_path(const char * fname,FILE ** fp)129cd296721SStephen Warren static char *fopen_any_on_path(const char *fname, FILE **fp)
130cd296721SStephen Warren {
131cd296721SStephen Warren const char *cur_dir = NULL;
132cd296721SStephen Warren struct search_path *node;
133cd296721SStephen Warren char *fullname;
134cd296721SStephen Warren
135cd296721SStephen Warren /* Try current directory first */
136cd296721SStephen Warren assert(fp);
137cd296721SStephen Warren if (current_srcfile)
138cd296721SStephen Warren cur_dir = current_srcfile->dir;
139cd296721SStephen Warren fullname = try_open(cur_dir, fname, fp);
140cd296721SStephen Warren
141cd296721SStephen Warren /* Failing that, try each search path in turn */
142cd296721SStephen Warren for (node = search_path_head; !*fp && node; node = node->next)
143cd296721SStephen Warren fullname = try_open(node->dirname, fname, fp);
144cd296721SStephen Warren
145cd296721SStephen Warren return fullname;
146cd296721SStephen Warren }
147cd296721SStephen Warren
srcfile_relative_open(const char * fname,char ** fullnamep)148658f29a5SJohn Bonesio FILE *srcfile_relative_open(const char *fname, char **fullnamep)
149658f29a5SJohn Bonesio {
150658f29a5SJohn Bonesio FILE *f;
1519fffb55fSDavid Gibson char *fullname;
1529fffb55fSDavid Gibson
1539fffb55fSDavid Gibson if (streq(fname, "-")) {
154658f29a5SJohn Bonesio f = stdin;
155658f29a5SJohn Bonesio fullname = xstrdup("<stdin>");
156658f29a5SJohn Bonesio } else {
157cd296721SStephen Warren fullname = fopen_any_on_path(fname, &f);
158658f29a5SJohn Bonesio if (!f)
159658f29a5SJohn Bonesio die("Couldn't open \"%s\": %s\n", fname,
160658f29a5SJohn Bonesio strerror(errno));
1619fffb55fSDavid Gibson }
1629fffb55fSDavid Gibson
163136ec204SStephen Warren if (depfile)
164136ec204SStephen Warren fprintf(depfile, " %s", fullname);
165136ec204SStephen Warren
166658f29a5SJohn Bonesio if (fullnamep)
167658f29a5SJohn Bonesio *fullnamep = fullname;
168658f29a5SJohn Bonesio else
169658f29a5SJohn Bonesio free(fullname);
1709fffb55fSDavid Gibson
171658f29a5SJohn Bonesio return f;
1729fffb55fSDavid Gibson }
1739fffb55fSDavid Gibson
srcfile_push(const char * fname)174658f29a5SJohn Bonesio void srcfile_push(const char *fname)
1759fffb55fSDavid Gibson {
176658f29a5SJohn Bonesio struct srcfile_state *srcfile;
1779fffb55fSDavid Gibson
178658f29a5SJohn Bonesio if (srcfile_depth++ >= MAX_SRCFILE_DEPTH)
179658f29a5SJohn Bonesio die("Includes nested too deeply");
180658f29a5SJohn Bonesio
181658f29a5SJohn Bonesio srcfile = xmalloc(sizeof(*srcfile));
182658f29a5SJohn Bonesio
183658f29a5SJohn Bonesio srcfile->f = srcfile_relative_open(fname, &srcfile->name);
18447605971SRob Herring srcfile->dir = get_dirname(srcfile->name);
185658f29a5SJohn Bonesio srcfile->prev = current_srcfile;
186658f29a5SJohn Bonesio
187658f29a5SJohn Bonesio srcfile->lineno = 1;
188658f29a5SJohn Bonesio srcfile->colno = 1;
189658f29a5SJohn Bonesio
190658f29a5SJohn Bonesio current_srcfile = srcfile;
191c2e7075cSRob Herring
192c2e7075cSRob Herring if (srcfile_depth == 1)
193c2e7075cSRob Herring set_initial_path(srcfile->name);
194658f29a5SJohn Bonesio }
195658f29a5SJohn Bonesio
srcfile_pop(void)19647605971SRob Herring bool srcfile_pop(void)
197658f29a5SJohn Bonesio {
198658f29a5SJohn Bonesio struct srcfile_state *srcfile = current_srcfile;
199658f29a5SJohn Bonesio
200658f29a5SJohn Bonesio assert(srcfile);
201658f29a5SJohn Bonesio
202658f29a5SJohn Bonesio current_srcfile = srcfile->prev;
203658f29a5SJohn Bonesio
204658f29a5SJohn Bonesio if (fclose(srcfile->f))
205658f29a5SJohn Bonesio die("Error closing \"%s\": %s\n", srcfile->name,
206658f29a5SJohn Bonesio strerror(errno));
207658f29a5SJohn Bonesio
208658f29a5SJohn Bonesio /* FIXME: We allow the srcfile_state structure to leak,
209658f29a5SJohn Bonesio * because it could still be referenced from a location
210658f29a5SJohn Bonesio * variable being carried through the parser somewhere. To
211658f29a5SJohn Bonesio * fix this we could either allocate all the files from a
212658f29a5SJohn Bonesio * table, or use a pool allocator. */
213658f29a5SJohn Bonesio
21447605971SRob Herring return current_srcfile ? true : false;
215658f29a5SJohn Bonesio }
216658f29a5SJohn Bonesio
srcfile_add_search_path(const char * dirname)217cd296721SStephen Warren void srcfile_add_search_path(const char *dirname)
218cd296721SStephen Warren {
219cd296721SStephen Warren struct search_path *node;
220cd296721SStephen Warren
221cd296721SStephen Warren /* Create the node */
222cd296721SStephen Warren node = xmalloc(sizeof(*node));
223cd296721SStephen Warren node->next = NULL;
224cd296721SStephen Warren node->dirname = xstrdup(dirname);
225cd296721SStephen Warren
226cd296721SStephen Warren /* Add to the end of our list */
227cd296721SStephen Warren if (search_path_tail)
228cd296721SStephen Warren *search_path_tail = node;
229cd296721SStephen Warren else
230cd296721SStephen Warren search_path_head = node;
231cd296721SStephen Warren search_path_tail = &node->next;
232cd296721SStephen Warren }
233cd296721SStephen Warren
srcpos_update(struct srcpos * pos,const char * text,int len)234658f29a5SJohn Bonesio void srcpos_update(struct srcpos *pos, const char *text, int len)
235658f29a5SJohn Bonesio {
236658f29a5SJohn Bonesio int i;
237658f29a5SJohn Bonesio
238658f29a5SJohn Bonesio pos->file = current_srcfile;
239658f29a5SJohn Bonesio
240658f29a5SJohn Bonesio pos->first_line = current_srcfile->lineno;
241658f29a5SJohn Bonesio pos->first_column = current_srcfile->colno;
242658f29a5SJohn Bonesio
243658f29a5SJohn Bonesio for (i = 0; i < len; i++)
244658f29a5SJohn Bonesio if (text[i] == '\n') {
245658f29a5SJohn Bonesio current_srcfile->lineno++;
246658f29a5SJohn Bonesio current_srcfile->colno = 1;
247658f29a5SJohn Bonesio } else {
248658f29a5SJohn Bonesio current_srcfile->colno++;
249658f29a5SJohn Bonesio }
250658f29a5SJohn Bonesio
251658f29a5SJohn Bonesio pos->last_line = current_srcfile->lineno;
252658f29a5SJohn Bonesio pos->last_column = current_srcfile->colno;
253658f29a5SJohn Bonesio }
254658f29a5SJohn Bonesio
255658f29a5SJohn Bonesio struct srcpos *
srcpos_copy(struct srcpos * pos)256658f29a5SJohn Bonesio srcpos_copy(struct srcpos *pos)
257658f29a5SJohn Bonesio {
258658f29a5SJohn Bonesio struct srcpos *pos_new;
259c2e7075cSRob Herring struct srcfile_state *srcfile_state;
260c2e7075cSRob Herring
261c2e7075cSRob Herring if (!pos)
262c2e7075cSRob Herring return NULL;
263658f29a5SJohn Bonesio
264658f29a5SJohn Bonesio pos_new = xmalloc(sizeof(struct srcpos));
265c2e7075cSRob Herring assert(pos->next == NULL);
266658f29a5SJohn Bonesio memcpy(pos_new, pos, sizeof(struct srcpos));
267658f29a5SJohn Bonesio
268c2e7075cSRob Herring /* allocate without free */
269c2e7075cSRob Herring srcfile_state = xmalloc(sizeof(struct srcfile_state));
270c2e7075cSRob Herring memcpy(srcfile_state, pos->file, sizeof(struct srcfile_state));
271c2e7075cSRob Herring pos_new->file = srcfile_state;
272c2e7075cSRob Herring
273658f29a5SJohn Bonesio return pos_new;
274658f29a5SJohn Bonesio }
275658f29a5SJohn Bonesio
srcpos_extend(struct srcpos * pos,struct srcpos * newtail)276c2e7075cSRob Herring struct srcpos *srcpos_extend(struct srcpos *pos, struct srcpos *newtail)
277c2e7075cSRob Herring {
278c2e7075cSRob Herring struct srcpos *p;
279c2e7075cSRob Herring
280c2e7075cSRob Herring if (!pos)
281c2e7075cSRob Herring return newtail;
282c2e7075cSRob Herring
283c2e7075cSRob Herring for (p = pos; p->next != NULL; p = p->next);
284c2e7075cSRob Herring p->next = newtail;
285c2e7075cSRob Herring return pos;
286c2e7075cSRob Herring }
287c2e7075cSRob Herring
288658f29a5SJohn Bonesio char *
srcpos_string(struct srcpos * pos)289658f29a5SJohn Bonesio srcpos_string(struct srcpos *pos)
290658f29a5SJohn Bonesio {
291658f29a5SJohn Bonesio const char *fname = "<no-file>";
292658f29a5SJohn Bonesio char *pos_str;
293658f29a5SJohn Bonesio
29489d12310SRob Herring if (pos->file && pos->file->name)
295658f29a5SJohn Bonesio fname = pos->file->name;
296658f29a5SJohn Bonesio
297658f29a5SJohn Bonesio
298658f29a5SJohn Bonesio if (pos->first_line != pos->last_line)
2996f05afcbSRob Herring xasprintf(&pos_str, "%s:%d.%d-%d.%d", fname,
300658f29a5SJohn Bonesio pos->first_line, pos->first_column,
301658f29a5SJohn Bonesio pos->last_line, pos->last_column);
302658f29a5SJohn Bonesio else if (pos->first_column != pos->last_column)
3036f05afcbSRob Herring xasprintf(&pos_str, "%s:%d.%d-%d", fname,
304658f29a5SJohn Bonesio pos->first_line, pos->first_column,
305658f29a5SJohn Bonesio pos->last_column);
306658f29a5SJohn Bonesio else
3076f05afcbSRob Herring xasprintf(&pos_str, "%s:%d.%d", fname,
308658f29a5SJohn Bonesio pos->first_line, pos->first_column);
309658f29a5SJohn Bonesio
310658f29a5SJohn Bonesio return pos_str;
311658f29a5SJohn Bonesio }
312658f29a5SJohn Bonesio
313c2e7075cSRob Herring static char *
srcpos_string_comment(struct srcpos * pos,bool first_line,int level)314c2e7075cSRob Herring srcpos_string_comment(struct srcpos *pos, bool first_line, int level)
315c2e7075cSRob Herring {
316*12d638f4SRob Herring (Arm) char *pos_str, *fresh_fname = NULL, *first, *rest;
317*12d638f4SRob Herring (Arm) const char *fname;
318c2e7075cSRob Herring
319c2e7075cSRob Herring if (!pos) {
320c2e7075cSRob Herring if (level > 1) {
321c2e7075cSRob Herring xasprintf(&pos_str, "<no-file>:<no-line>");
322c2e7075cSRob Herring return pos_str;
323c2e7075cSRob Herring } else {
324c2e7075cSRob Herring return NULL;
325c2e7075cSRob Herring }
326c2e7075cSRob Herring }
327c2e7075cSRob Herring
328c2e7075cSRob Herring if (!pos->file)
329c2e7075cSRob Herring fname = "<no-file>";
330c2e7075cSRob Herring else if (!pos->file->name)
331c2e7075cSRob Herring fname = "<no-filename>";
332c2e7075cSRob Herring else if (level > 1)
333c2e7075cSRob Herring fname = pos->file->name;
334c2e7075cSRob Herring else {
335*12d638f4SRob Herring (Arm) fresh_fname = shorten_to_initial_path(pos->file->name);
336*12d638f4SRob Herring (Arm) if (fresh_fname)
337*12d638f4SRob Herring (Arm) fname = fresh_fname;
338c2e7075cSRob Herring else
339c2e7075cSRob Herring fname = pos->file->name;
340c2e7075cSRob Herring }
341c2e7075cSRob Herring
342c2e7075cSRob Herring if (level > 1)
343c2e7075cSRob Herring xasprintf(&first, "%s:%d:%d-%d:%d", fname,
344c2e7075cSRob Herring pos->first_line, pos->first_column,
345c2e7075cSRob Herring pos->last_line, pos->last_column);
346c2e7075cSRob Herring else
347c2e7075cSRob Herring xasprintf(&first, "%s:%d", fname,
348c2e7075cSRob Herring first_line ? pos->first_line : pos->last_line);
349c2e7075cSRob Herring
350c2e7075cSRob Herring if (fresh_fname)
351*12d638f4SRob Herring (Arm) free(fresh_fname);
352c2e7075cSRob Herring
353c2e7075cSRob Herring if (pos->next != NULL) {
354c2e7075cSRob Herring rest = srcpos_string_comment(pos->next, first_line, level);
355c2e7075cSRob Herring xasprintf(&pos_str, "%s, %s", first, rest);
356c2e7075cSRob Herring free(first);
357c2e7075cSRob Herring free(rest);
358c2e7075cSRob Herring } else {
359c2e7075cSRob Herring pos_str = first;
360c2e7075cSRob Herring }
361c2e7075cSRob Herring
362c2e7075cSRob Herring return pos_str;
363c2e7075cSRob Herring }
364c2e7075cSRob Herring
srcpos_string_first(struct srcpos * pos,int level)365c2e7075cSRob Herring char *srcpos_string_first(struct srcpos *pos, int level)
366c2e7075cSRob Herring {
367c2e7075cSRob Herring return srcpos_string_comment(pos, true, level);
368c2e7075cSRob Herring }
369c2e7075cSRob Herring
srcpos_string_last(struct srcpos * pos,int level)370c2e7075cSRob Herring char *srcpos_string_last(struct srcpos *pos, int level)
371c2e7075cSRob Herring {
372c2e7075cSRob Herring return srcpos_string_comment(pos, false, level);
373c2e7075cSRob Herring }
374c2e7075cSRob Herring
srcpos_verror(struct srcpos * pos,const char * prefix,const char * fmt,va_list va)37547605971SRob Herring void srcpos_verror(struct srcpos *pos, const char *prefix,
37647605971SRob Herring const char *fmt, va_list va)
377658f29a5SJohn Bonesio {
37847605971SRob Herring char *srcstr;
379658f29a5SJohn Bonesio
380658f29a5SJohn Bonesio srcstr = srcpos_string(pos);
381658f29a5SJohn Bonesio
38247605971SRob Herring fprintf(stderr, "%s: %s ", prefix, srcstr);
38373ab39b1SGrant Likely vfprintf(stderr, fmt, va);
38473ab39b1SGrant Likely fprintf(stderr, "\n");
38547605971SRob Herring
38647605971SRob Herring free(srcstr);
387658f29a5SJohn Bonesio }
388658f29a5SJohn Bonesio
srcpos_error(struct srcpos * pos,const char * prefix,const char * fmt,...)38947605971SRob Herring void srcpos_error(struct srcpos *pos, const char *prefix,
39047605971SRob Herring const char *fmt, ...)
391658f29a5SJohn Bonesio {
392658f29a5SJohn Bonesio va_list va;
393658f29a5SJohn Bonesio
394658f29a5SJohn Bonesio va_start(va, fmt);
39547605971SRob Herring srcpos_verror(pos, prefix, fmt, va);
396658f29a5SJohn Bonesio va_end(va);
3979fffb55fSDavid Gibson }
398cd296721SStephen Warren
srcpos_set_line(char * f,int l)399cd296721SStephen Warren void srcpos_set_line(char *f, int l)
400cd296721SStephen Warren {
401cd296721SStephen Warren current_srcfile->name = f;
402cd296721SStephen Warren current_srcfile->lineno = l;
403c2e7075cSRob Herring
404c2e7075cSRob Herring if (initial_cpp) {
405c2e7075cSRob Herring initial_cpp = false;
406c2e7075cSRob Herring set_initial_path(f);
407c2e7075cSRob Herring }
408cd296721SStephen Warren }
409