blob: 9d38459902f3c3c1fbb4ca5d3151a10e92fbf73d [file] [log] [blame]
Jon Loeligere45e6fd2007-03-23 15:18:41 -05001/*
2 * Copyright 2007 Jon Loeliger, Freescale Semiconductor, Inc.
3 *
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.
8 *
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.
13 *
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
17 * USA
18 */
19
Jon Loeligere5c8e1d2008-09-12 13:39:49 -050020#define _GNU_SOURCE
21
22#include <stdio.h>
23
Jon Loeligere45e6fd2007-03-23 15:18:41 -050024#include "dtc.h"
25#include "srcpos.h"
26
Simon Glassde6b7622012-03-14 20:04:13 -070027/* A node in our list of directories to search for source/include files */
28struct search_path {
29 struct search_path *next; /* next node in list, NULL for end */
30 const char *dirname; /* name of directory to search */
31};
32
33/* This is the list of directories that we search for source files */
34static struct search_path *search_path_head, **search_path_tail;
35
Jon Loeligere5c8e1d2008-09-12 13:39:49 -050036
Florian Fainelli6d889342014-01-24 17:19:10 -080037static char *get_dirname(const char *path)
David Gibsond68cb362009-12-08 14:24:42 +110038{
39 const char *slash = strrchr(path, '/');
40
41 if (slash) {
42 int len = slash - path;
43 char *dir = xmalloc(len + 1);
44
45 memcpy(dir, path, len);
46 dir[len] = '\0';
47 return dir;
48 }
49 return NULL;
50}
51
Stephen Warren69df9f02012-01-12 11:31:00 -070052FILE *depfile; /* = NULL */
David Gibsond68cb362009-12-08 14:24:42 +110053struct srcfile_state *current_srcfile; /* = NULL */
54
55/* Detect infinite include recursion. */
56#define MAX_SRCFILE_DEPTH (100)
57static int srcfile_depth; /* = 0 */
58
Simon Glassde6b7622012-03-14 20:04:13 -070059
60/**
61 * Try to open a file in a given directory.
62 *
63 * If the filename is an absolute path, then dirname is ignored. If it is a
64 * relative path, then we look in that directory for the file.
65 *
66 * @param dirname Directory to look in, or NULL for none
67 * @param fname Filename to look for
68 * @param fp Set to NULL if file did not open
69 * @return allocated filename on success (caller must free), NULL on failure
70 */
71static char *try_open(const char *dirname, const char *fname, FILE **fp)
72{
73 char *fullname;
74
75 if (!dirname || fname[0] == '/')
76 fullname = xstrdup(fname);
77 else
78 fullname = join_path(dirname, fname);
79
Andrei Errapart83e606a2014-06-19 21:12:27 +100080 *fp = fopen(fullname, "rb");
Simon Glassde6b7622012-03-14 20:04:13 -070081 if (!*fp) {
82 free(fullname);
83 fullname = NULL;
84 }
85
86 return fullname;
87}
88
89/**
90 * Open a file for read access
91 *
92 * If it is a relative filename, we search the full search path for it.
93 *
94 * @param fname Filename to open
95 * @param fp Returns pointer to opened FILE, or NULL on failure
96 * @return pointer to allocated filename, which caller must free
97 */
98static char *fopen_any_on_path(const char *fname, FILE **fp)
99{
100 const char *cur_dir = NULL;
101 struct search_path *node;
102 char *fullname;
103
104 /* Try current directory first */
105 assert(fp);
106 if (current_srcfile)
107 cur_dir = current_srcfile->dir;
108 fullname = try_open(cur_dir, fname, fp);
109
110 /* Failing that, try each search path in turn */
111 for (node = search_path_head; !*fp && node; node = node->next)
112 fullname = try_open(node->dirname, fname, fp);
113
114 return fullname;
115}
116
David Gibsond68cb362009-12-08 14:24:42 +1100117FILE *srcfile_relative_open(const char *fname, char **fullnamep)
118{
119 FILE *f;
120 char *fullname;
121
122 if (streq(fname, "-")) {
123 f = stdin;
124 fullname = xstrdup("<stdin>");
125 } else {
Simon Glassde6b7622012-03-14 20:04:13 -0700126 fullname = fopen_any_on_path(fname, &f);
David Gibsond68cb362009-12-08 14:24:42 +1100127 if (!f)
128 die("Couldn't open \"%s\": %s\n", fname,
129 strerror(errno));
130 }
131
Stephen Warren69df9f02012-01-12 11:31:00 -0700132 if (depfile)
133 fprintf(depfile, " %s", fullname);
134
David Gibsond68cb362009-12-08 14:24:42 +1100135 if (fullnamep)
136 *fullnamep = fullname;
137 else
138 free(fullname);
139
140 return f;
141}
142
143void srcfile_push(const char *fname)
144{
145 struct srcfile_state *srcfile;
146
147 if (srcfile_depth++ >= MAX_SRCFILE_DEPTH)
148 die("Includes nested too deeply");
149
150 srcfile = xmalloc(sizeof(*srcfile));
151
152 srcfile->f = srcfile_relative_open(fname, &srcfile->name);
Florian Fainelli6d889342014-01-24 17:19:10 -0800153 srcfile->dir = get_dirname(srcfile->name);
David Gibsond68cb362009-12-08 14:24:42 +1100154 srcfile->prev = current_srcfile;
David Gibson728c5e82009-12-08 14:24:42 +1100155
156 srcfile->lineno = 1;
157 srcfile->colno = 1;
158
David Gibsond68cb362009-12-08 14:24:42 +1100159 current_srcfile = srcfile;
160}
161
David Gibson17625372013-10-28 21:06:53 +1100162bool srcfile_pop(void)
David Gibsond68cb362009-12-08 14:24:42 +1100163{
164 struct srcfile_state *srcfile = current_srcfile;
165
166 assert(srcfile);
167
168 current_srcfile = srcfile->prev;
169
170 if (fclose(srcfile->f))
David Gibson728c5e82009-12-08 14:24:42 +1100171 die("Error closing \"%s\": %s\n", srcfile->name,
172 strerror(errno));
David Gibsond68cb362009-12-08 14:24:42 +1100173
174 /* FIXME: We allow the srcfile_state structure to leak,
175 * because it could still be referenced from a location
176 * variable being carried through the parser somewhere. To
177 * fix this we could either allocate all the files from a
178 * table, or use a pool allocator. */
179
David Gibson17625372013-10-28 21:06:53 +1100180 return current_srcfile ? true : false;
David Gibsond68cb362009-12-08 14:24:42 +1100181}
Jon Loeligere45e6fd2007-03-23 15:18:41 -0500182
Simon Glassde6b7622012-03-14 20:04:13 -0700183void srcfile_add_search_path(const char *dirname)
184{
185 struct search_path *node;
186
187 /* Create the node */
188 node = xmalloc(sizeof(*node));
189 node->next = NULL;
190 node->dirname = xstrdup(dirname);
191
192 /* Add to the end of our list */
193 if (search_path_tail)
194 *search_path_tail = node;
195 else
196 search_path_head = node;
197 search_path_tail = &node->next;
198}
199
Jon Loeligere5c8e1d2008-09-12 13:39:49 -0500200/*
201 * The empty source position.
202 */
203
David Gibsonc6225f82009-12-08 14:24:42 +1100204struct srcpos srcpos_empty = {
Jon Loeligere5c8e1d2008-09-12 13:39:49 -0500205 .first_line = 0,
206 .first_column = 0,
207 .last_line = 0,
208 .last_column = 0,
David Gibsond68cb362009-12-08 14:24:42 +1100209 .file = NULL,
Jon Loeligere5c8e1d2008-09-12 13:39:49 -0500210};
211
David Gibson728c5e82009-12-08 14:24:42 +1100212#define TAB_SIZE 8
213
David Gibsonc6225f82009-12-08 14:24:42 +1100214void srcpos_update(struct srcpos *pos, const char *text, int len)
David Gibson728c5e82009-12-08 14:24:42 +1100215{
216 int i;
217
218 pos->file = current_srcfile;
219
220 pos->first_line = current_srcfile->lineno;
221 pos->first_column = current_srcfile->colno;
222
223 for (i = 0; i < len; i++)
224 if (text[i] == '\n') {
225 current_srcfile->lineno++;
226 current_srcfile->colno = 1;
227 } else if (text[i] == '\t') {
228 current_srcfile->colno =
229 ALIGN(current_srcfile->colno, TAB_SIZE);
230 } else {
231 current_srcfile->colno++;
232 }
233
234 pos->last_line = current_srcfile->lineno;
235 pos->last_column = current_srcfile->colno;
236}
237
David Gibsonc6225f82009-12-08 14:24:42 +1100238struct srcpos *
239srcpos_copy(struct srcpos *pos)
Jon Loeligere5c8e1d2008-09-12 13:39:49 -0500240{
David Gibsonc6225f82009-12-08 14:24:42 +1100241 struct srcpos *pos_new;
Jon Loeligere5c8e1d2008-09-12 13:39:49 -0500242
David Gibsonc6225f82009-12-08 14:24:42 +1100243 pos_new = xmalloc(sizeof(struct srcpos));
244 memcpy(pos_new, pos, sizeof(struct srcpos));
Jon Loeligere5c8e1d2008-09-12 13:39:49 -0500245
246 return pos_new;
247}
248
Jon Loeligere5c8e1d2008-09-12 13:39:49 -0500249char *
David Gibsonc6225f82009-12-08 14:24:42 +1100250srcpos_string(struct srcpos *pos)
Jon Loeligere5c8e1d2008-09-12 13:39:49 -0500251{
David Gibsone1fee322009-12-08 14:24:42 +1100252 const char *fname = "<no-file>";
Jon Loeligere5c8e1d2008-09-12 13:39:49 -0500253 char *pos_str;
254
David Gibson00d7bb12017-02-08 17:39:36 +1100255 if (pos->file && pos->file->name)
Jon Loeligere5c8e1d2008-09-12 13:39:49 -0500256 fname = pos->file->name;
Jon Loeligere5c8e1d2008-09-12 13:39:49 -0500257
Jon Loeligere5c8e1d2008-09-12 13:39:49 -0500258
David Gibsone1fee322009-12-08 14:24:42 +1100259 if (pos->first_line != pos->last_line)
David Gibsond71d25d2016-05-25 15:15:36 +1000260 xasprintf(&pos_str, "%s:%d.%d-%d.%d", fname,
261 pos->first_line, pos->first_column,
262 pos->last_line, pos->last_column);
David Gibsone1fee322009-12-08 14:24:42 +1100263 else if (pos->first_column != pos->last_column)
David Gibsond71d25d2016-05-25 15:15:36 +1000264 xasprintf(&pos_str, "%s:%d.%d-%d", fname,
265 pos->first_line, pos->first_column,
266 pos->last_column);
David Gibsone1fee322009-12-08 14:24:42 +1100267 else
David Gibsond71d25d2016-05-25 15:15:36 +1000268 xasprintf(&pos_str, "%s:%d.%d", fname,
269 pos->first_line, pos->first_column);
Jon Loeligere5c8e1d2008-09-12 13:39:49 -0500270
271 return pos_str;
272}
273
David Gibson0e2d3992014-01-01 23:27:31 +1100274void srcpos_verror(struct srcpos *pos, const char *prefix,
275 const char *fmt, va_list va)
John Bonesioc0fa2e62010-10-20 14:44:58 -0700276{
David Gibson0c0bf852014-01-01 23:23:54 +1100277 char *srcstr;
John Bonesioc0fa2e62010-10-20 14:44:58 -0700278
David Gibsone19d3b12014-01-01 23:17:39 +1100279 srcstr = srcpos_string(pos);
John Bonesioc0fa2e62010-10-20 14:44:58 -0700280
David Gibson0e2d3992014-01-01 23:27:31 +1100281 fprintf(stderr, "%s: %s ", prefix, srcstr);
David Gibsone19d3b12014-01-01 23:17:39 +1100282 vfprintf(stderr, fmt, va);
283 fprintf(stderr, "\n");
David Gibson0c0bf852014-01-01 23:23:54 +1100284
285 free(srcstr);
John Bonesioc0fa2e62010-10-20 14:44:58 -0700286}
Jon Loeligere5c8e1d2008-09-12 13:39:49 -0500287
David Gibson0e2d3992014-01-01 23:27:31 +1100288void srcpos_error(struct srcpos *pos, const char *prefix,
289 const char *fmt, ...)
Jon Loeligere5c8e1d2008-09-12 13:39:49 -0500290{
Jon Loeligere5c8e1d2008-09-12 13:39:49 -0500291 va_list va;
John Bonesioc0fa2e62010-10-20 14:44:58 -0700292
Jon Loeligere5c8e1d2008-09-12 13:39:49 -0500293 va_start(va, fmt);
David Gibson0e2d3992014-01-01 23:27:31 +1100294 srcpos_verror(pos, prefix, fmt, va);
Jon Loeligere5c8e1d2008-09-12 13:39:49 -0500295 va_end(va);
296}
297
Stephen Warren1b6d1942012-09-27 17:11:05 -0600298void srcpos_set_line(char *f, int l)
299{
300 current_srcfile->name = f;
301 current_srcfile->lineno = l;
302}