1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright 2007 Jon Loeliger, Freescale Semiconductor, Inc.
4 */
5
6 #ifndef _GNU_SOURCE
7 #define _GNU_SOURCE
8 #endif
9
10 #include <stdio.h>
11
12 #include "dtc.h"
13 #include "srcpos.h"
14
15 /* A node in our list of directories to search for source/include files */
16 struct search_path {
17 struct search_path *next; /* next node in list, NULL for end */
18 const char *dirname; /* name of directory to search */
19 };
20
21 /* This is the list of directories that we search for source files */
22 static struct search_path *search_path_head, **search_path_tail;
23
24 /* Detect infinite include recursion. */
25 #define MAX_SRCFILE_DEPTH (200)
26 static int srcfile_depth; /* = 0 */
27
get_dirname(const char * path)28 static char *get_dirname(const char *path)
29 {
30 const char *slash = strrchr(path, '/');
31
32 if (slash) {
33 int len = slash - path;
34 char *dir = xmalloc(len + 1);
35
36 memcpy(dir, path, len);
37 dir[len] = '\0';
38 return dir;
39 }
40 return NULL;
41 }
42
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;
48
set_initial_path(char * fname)49 static void set_initial_path(char *fname)
50 {
51 int i, len = strlen(fname);
52
53 xasprintf(&initial_path, "%s", fname);
54 initial_pathlen = 0;
55 for (i = 0; i != len; i++)
56 if (initial_path[i] == '/')
57 initial_pathlen++;
58 }
59
shorten_to_initial_path(char * fname)60 static char *shorten_to_initial_path(char *fname)
61 {
62 char *p1, *p2, *prevslash1 = NULL;
63 int slashes = 0;
64
65 for (p1 = fname, p2 = initial_path; *p1 && *p2; p1++, p2++) {
66 if (*p1 != *p2)
67 break;
68 if (*p1 == '/') {
69 prevslash1 = p1;
70 slashes++;
71 }
72 }
73 p1 = prevslash1 + 1;
74 if (prevslash1) {
75 int diff = initial_pathlen - slashes, i, j;
76 int restlen = strlen(fname) - (p1 - fname);
77 char *res;
78
79 res = xmalloc((3 * diff) + restlen + 1);
80 for (i = 0, j = 0; i != diff; i++) {
81 res[j++] = '.';
82 res[j++] = '.';
83 res[j++] = '/';
84 }
85 strcpy(res + j, p1);
86 return res;
87 }
88 return NULL;
89 }
90
91 /**
92 * Try to open a file in a given directory.
93 *
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.
96 *
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
101 */
try_open(const char * dirname,const char * fname,FILE ** fp)102 static char *try_open(const char *dirname, const char *fname, FILE **fp)
103 {
104 char *fullname;
105
106 if (!dirname || fname[0] == '/')
107 fullname = xstrdup(fname);
108 else
109 fullname = join_path(dirname, fname);
110
111 *fp = fopen(fullname, "rb");
112 if (!*fp) {
113 free(fullname);
114 fullname = NULL;
115 }
116
117 return fullname;
118 }
119
120 /**
121 * Open a file for read access
122 *
123 * If it is a relative filename, we search the full search path for it.
124 *
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
128 */
fopen_any_on_path(const char * fname,FILE ** fp)129 static char *fopen_any_on_path(const char *fname, FILE **fp)
130 {
131 const char *cur_dir = NULL;
132 struct search_path *node;
133 char *fullname;
134
135 /* Try current directory first */
136 assert(fp);
137 if (current_srcfile)
138 cur_dir = current_srcfile->dir;
139 fullname = try_open(cur_dir, fname, fp);
140
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);
144
145 return fullname;
146 }
147
srcfile_relative_open(const char * fname,char ** fullnamep)148 FILE *srcfile_relative_open(const char *fname, char **fullnamep)
149 {
150 FILE *f;
151 char *fullname;
152
153 if (streq(fname, "-")) {
154 f = stdin;
155 fullname = xstrdup("<stdin>");
156 } else {
157 fullname = fopen_any_on_path(fname, &f);
158 if (!f)
159 die("Couldn't open \"%s\": %s\n", fname,
160 strerror(errno));
161 }
162
163 if (depfile) {
164 fputc(' ', depfile);
165 fprint_path_escaped(depfile, fullname);
166 }
167
168 if (fullnamep)
169 *fullnamep = fullname;
170 else
171 free(fullname);
172
173 return f;
174 }
175
srcfile_push(const char * fname)176 void srcfile_push(const char *fname)
177 {
178 struct srcfile_state *srcfile;
179
180 if (srcfile_depth++ >= MAX_SRCFILE_DEPTH)
181 die("Includes nested too deeply");
182
183 srcfile = xmalloc(sizeof(*srcfile));
184
185 srcfile->f = srcfile_relative_open(fname, &srcfile->name);
186 srcfile->dir = get_dirname(srcfile->name);
187 srcfile->prev = current_srcfile;
188
189 srcfile->lineno = 1;
190 srcfile->colno = 1;
191
192 current_srcfile = srcfile;
193
194 if (srcfile_depth == 1)
195 set_initial_path(srcfile->name);
196 }
197
srcfile_pop(void)198 bool srcfile_pop(void)
199 {
200 struct srcfile_state *srcfile = current_srcfile;
201
202 assert(srcfile);
203
204 current_srcfile = srcfile->prev;
205
206 if (fclose(srcfile->f))
207 die("Error closing \"%s\": %s\n", srcfile->name,
208 strerror(errno));
209
210 /* FIXME: We allow the srcfile_state structure to leak,
211 * because it could still be referenced from a location
212 * variable being carried through the parser somewhere. To
213 * fix this we could either allocate all the files from a
214 * table, or use a pool allocator. */
215
216 return current_srcfile ? true : false;
217 }
218
srcfile_add_search_path(const char * dirname)219 void srcfile_add_search_path(const char *dirname)
220 {
221 struct search_path *node;
222
223 /* Create the node */
224 node = xmalloc(sizeof(*node));
225 node->next = NULL;
226 node->dirname = xstrdup(dirname);
227
228 /* Add to the end of our list */
229 if (search_path_tail)
230 *search_path_tail = node;
231 else
232 search_path_head = node;
233 search_path_tail = &node->next;
234 }
235
srcpos_update(struct srcpos * pos,const char * text,int len)236 void srcpos_update(struct srcpos *pos, const char *text, int len)
237 {
238 int i;
239
240 pos->file = current_srcfile;
241
242 pos->first_line = current_srcfile->lineno;
243 pos->first_column = current_srcfile->colno;
244
245 for (i = 0; i < len; i++)
246 if (text[i] == '\n') {
247 current_srcfile->lineno++;
248 current_srcfile->colno = 1;
249 } else {
250 current_srcfile->colno++;
251 }
252
253 pos->last_line = current_srcfile->lineno;
254 pos->last_column = current_srcfile->colno;
255 }
256
257 struct srcpos *
srcpos_copy(struct srcpos * pos)258 srcpos_copy(struct srcpos *pos)
259 {
260 struct srcpos *pos_new;
261 struct srcfile_state *srcfile_state;
262
263 if (!pos)
264 return NULL;
265
266 pos_new = xmalloc(sizeof(struct srcpos));
267 assert(pos->next == NULL);
268 memcpy(pos_new, pos, sizeof(struct srcpos));
269
270 /* allocate without free */
271 srcfile_state = xmalloc(sizeof(struct srcfile_state));
272 memcpy(srcfile_state, pos->file, sizeof(struct srcfile_state));
273 pos_new->file = srcfile_state;
274
275 return pos_new;
276 }
277
srcpos_extend(struct srcpos * pos,struct srcpos * newtail)278 struct srcpos *srcpos_extend(struct srcpos *pos, struct srcpos *newtail)
279 {
280 struct srcpos *p;
281
282 if (!pos)
283 return newtail;
284
285 for (p = pos; p->next != NULL; p = p->next);
286 p->next = newtail;
287 return pos;
288 }
289
srcpos_free(struct srcpos * pos)290 void srcpos_free(struct srcpos *pos)
291 {
292 struct srcpos *p_next;
293
294 while (pos) {
295 p_next = pos->next;
296 free(pos);
297 pos = p_next;
298 }
299 }
300
301 char *
srcpos_string(struct srcpos * pos)302 srcpos_string(struct srcpos *pos)
303 {
304 const char *fname = "<no-file>";
305 char *pos_str;
306
307 if (pos->file && pos->file->name)
308 fname = pos->file->name;
309
310
311 if (pos->first_line != pos->last_line)
312 xasprintf(&pos_str, "%s:%d.%d-%d.%d", fname,
313 pos->first_line, pos->first_column,
314 pos->last_line, pos->last_column);
315 else if (pos->first_column != pos->last_column)
316 xasprintf(&pos_str, "%s:%d.%d-%d", fname,
317 pos->first_line, pos->first_column,
318 pos->last_column);
319 else
320 xasprintf(&pos_str, "%s:%d.%d", fname,
321 pos->first_line, pos->first_column);
322
323 return pos_str;
324 }
325
326 static char *
srcpos_string_comment(struct srcpos * pos,bool first_line,int level)327 srcpos_string_comment(struct srcpos *pos, bool first_line, int level)
328 {
329 char *pos_str, *fresh_fname = NULL, *first, *rest;
330 const char *fname;
331
332 if (!pos) {
333 if (level > 1) {
334 xasprintf(&pos_str, "<no-file>:<no-line>");
335 return pos_str;
336 } else {
337 return NULL;
338 }
339 }
340
341 if (!pos->file)
342 fname = "<no-file>";
343 else if (!pos->file->name)
344 fname = "<no-filename>";
345 else if (level > 1)
346 fname = pos->file->name;
347 else {
348 fresh_fname = shorten_to_initial_path(pos->file->name);
349 if (fresh_fname)
350 fname = fresh_fname;
351 else
352 fname = pos->file->name;
353 }
354
355 if (level > 1)
356 xasprintf(&first, "%s:%d:%d-%d:%d", fname,
357 pos->first_line, pos->first_column,
358 pos->last_line, pos->last_column);
359 else
360 xasprintf(&first, "%s:%d", fname,
361 first_line ? pos->first_line : pos->last_line);
362
363 if (fresh_fname)
364 free(fresh_fname);
365
366 if (pos->next != NULL) {
367 rest = srcpos_string_comment(pos->next, first_line, level);
368 xasprintf(&pos_str, "%s, %s", first, rest);
369 free(first);
370 free(rest);
371 } else {
372 pos_str = first;
373 }
374
375 return pos_str;
376 }
377
srcpos_string_first(struct srcpos * pos,int level)378 char *srcpos_string_first(struct srcpos *pos, int level)
379 {
380 return srcpos_string_comment(pos, true, level);
381 }
382
srcpos_string_last(struct srcpos * pos,int level)383 char *srcpos_string_last(struct srcpos *pos, int level)
384 {
385 return srcpos_string_comment(pos, false, level);
386 }
387
srcpos_verror(struct srcpos * pos,const char * prefix,const char * fmt,va_list va)388 void srcpos_verror(struct srcpos *pos, const char *prefix,
389 const char *fmt, va_list va)
390 {
391 char *srcstr;
392
393 srcstr = srcpos_string(pos);
394
395 fprintf(stderr, "%s: %s ", prefix, srcstr);
396 vfprintf(stderr, fmt, va);
397 fprintf(stderr, "\n");
398
399 free(srcstr);
400 }
401
srcpos_error(struct srcpos * pos,const char * prefix,const char * fmt,...)402 void srcpos_error(struct srcpos *pos, const char *prefix,
403 const char *fmt, ...)
404 {
405 va_list va;
406
407 va_start(va, fmt);
408 srcpos_verror(pos, prefix, fmt, va);
409 va_end(va);
410 }
411
srcpos_set_line(char * f,int l)412 void srcpos_set_line(char *f, int l)
413 {
414 current_srcfile->name = f;
415 current_srcfile->lineno = l;
416
417 if (initial_cpp) {
418 initial_cpp = false;
419 set_initial_path(f);
420 }
421 }
422