xref: /openbmc/u-boot/scripts/dtc/srcpos.c (revision db405d1980e0e3bfdd629399d1dfe457f1f32646)
1*c0e032e0STom Rini /*
2*c0e032e0STom Rini  * Copyright 2007 Jon Loeliger, Freescale Semiconductor, Inc.
3*c0e032e0STom Rini  *
4*c0e032e0STom Rini  * This program is free software; you can redistribute it and/or
5*c0e032e0STom Rini  * modify it under the terms of the GNU General Public License as
6*c0e032e0STom Rini  * published by the Free Software Foundation; either version 2 of the
7*c0e032e0STom Rini  * License, or (at your option) any later version.
8*c0e032e0STom Rini  *
9*c0e032e0STom Rini  *  This program is distributed in the hope that it will be useful,
10*c0e032e0STom Rini  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11*c0e032e0STom Rini  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12*c0e032e0STom Rini  *  General Public License for more details.
13*c0e032e0STom Rini  *
14*c0e032e0STom Rini  *  You should have received a copy of the GNU General Public License
15*c0e032e0STom Rini  *  along with this program; if not, write to the Free Software
16*c0e032e0STom Rini  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
17*c0e032e0STom Rini  *                                                                   USA
18*c0e032e0STom Rini  */
19*c0e032e0STom Rini 
20*c0e032e0STom Rini #define _GNU_SOURCE
21*c0e032e0STom Rini 
22*c0e032e0STom Rini #include <stdio.h>
23*c0e032e0STom Rini 
24*c0e032e0STom Rini #include "dtc.h"
25*c0e032e0STom Rini #include "srcpos.h"
26*c0e032e0STom Rini 
27*c0e032e0STom Rini /* A node in our list of directories to search for source/include files */
28*c0e032e0STom Rini struct search_path {
29*c0e032e0STom Rini 	struct search_path *next;	/* next node in list, NULL for end */
30*c0e032e0STom Rini 	const char *dirname;		/* name of directory to search */
31*c0e032e0STom Rini };
32*c0e032e0STom Rini 
33*c0e032e0STom Rini /* This is the list of directories that we search for source files */
34*c0e032e0STom Rini static struct search_path *search_path_head, **search_path_tail;
35*c0e032e0STom Rini 
36*c0e032e0STom Rini 
get_dirname(const char * path)37*c0e032e0STom Rini static char *get_dirname(const char *path)
38*c0e032e0STom Rini {
39*c0e032e0STom Rini 	const char *slash = strrchr(path, '/');
40*c0e032e0STom Rini 
41*c0e032e0STom Rini 	if (slash) {
42*c0e032e0STom Rini 		int len = slash - path;
43*c0e032e0STom Rini 		char *dir = xmalloc(len + 1);
44*c0e032e0STom Rini 
45*c0e032e0STom Rini 		memcpy(dir, path, len);
46*c0e032e0STom Rini 		dir[len] = '\0';
47*c0e032e0STom Rini 		return dir;
48*c0e032e0STom Rini 	}
49*c0e032e0STom Rini 	return NULL;
50*c0e032e0STom Rini }
51*c0e032e0STom Rini 
52*c0e032e0STom Rini FILE *depfile; /* = NULL */
53*c0e032e0STom Rini struct srcfile_state *current_srcfile; /* = NULL */
54*c0e032e0STom Rini 
55*c0e032e0STom Rini /* Detect infinite include recursion. */
56*c0e032e0STom Rini #define MAX_SRCFILE_DEPTH     (100)
57*c0e032e0STom Rini static int srcfile_depth; /* = 0 */
58*c0e032e0STom Rini 
59*c0e032e0STom Rini 
60*c0e032e0STom Rini /**
61*c0e032e0STom Rini  * Try to open a file in a given directory.
62*c0e032e0STom Rini  *
63*c0e032e0STom Rini  * If the filename is an absolute path, then dirname is ignored. If it is a
64*c0e032e0STom Rini  * relative path, then we look in that directory for the file.
65*c0e032e0STom Rini  *
66*c0e032e0STom Rini  * @param dirname	Directory to look in, or NULL for none
67*c0e032e0STom Rini  * @param fname		Filename to look for
68*c0e032e0STom Rini  * @param fp		Set to NULL if file did not open
69*c0e032e0STom Rini  * @return allocated filename on success (caller must free), NULL on failure
70*c0e032e0STom Rini  */
try_open(const char * dirname,const char * fname,FILE ** fp)71*c0e032e0STom Rini static char *try_open(const char *dirname, const char *fname, FILE **fp)
72*c0e032e0STom Rini {
73*c0e032e0STom Rini 	char *fullname;
74*c0e032e0STom Rini 
75*c0e032e0STom Rini 	if (!dirname || fname[0] == '/')
76*c0e032e0STom Rini 		fullname = xstrdup(fname);
77*c0e032e0STom Rini 	else
78*c0e032e0STom Rini 		fullname = join_path(dirname, fname);
79*c0e032e0STom Rini 
80*c0e032e0STom Rini 	*fp = fopen(fullname, "rb");
81*c0e032e0STom Rini 	if (!*fp) {
82*c0e032e0STom Rini 		free(fullname);
83*c0e032e0STom Rini 		fullname = NULL;
84*c0e032e0STom Rini 	}
85*c0e032e0STom Rini 
86*c0e032e0STom Rini 	return fullname;
87*c0e032e0STom Rini }
88*c0e032e0STom Rini 
89*c0e032e0STom Rini /**
90*c0e032e0STom Rini  * Open a file for read access
91*c0e032e0STom Rini  *
92*c0e032e0STom Rini  * If it is a relative filename, we search the full search path for it.
93*c0e032e0STom Rini  *
94*c0e032e0STom Rini  * @param fname	Filename to open
95*c0e032e0STom Rini  * @param fp	Returns pointer to opened FILE, or NULL on failure
96*c0e032e0STom Rini  * @return pointer to allocated filename, which caller must free
97*c0e032e0STom Rini  */
fopen_any_on_path(const char * fname,FILE ** fp)98*c0e032e0STom Rini static char *fopen_any_on_path(const char *fname, FILE **fp)
99*c0e032e0STom Rini {
100*c0e032e0STom Rini 	const char *cur_dir = NULL;
101*c0e032e0STom Rini 	struct search_path *node;
102*c0e032e0STom Rini 	char *fullname;
103*c0e032e0STom Rini 
104*c0e032e0STom Rini 	/* Try current directory first */
105*c0e032e0STom Rini 	assert(fp);
106*c0e032e0STom Rini 	if (current_srcfile)
107*c0e032e0STom Rini 		cur_dir = current_srcfile->dir;
108*c0e032e0STom Rini 	fullname = try_open(cur_dir, fname, fp);
109*c0e032e0STom Rini 
110*c0e032e0STom Rini 	/* Failing that, try each search path in turn */
111*c0e032e0STom Rini 	for (node = search_path_head; !*fp && node; node = node->next)
112*c0e032e0STom Rini 		fullname = try_open(node->dirname, fname, fp);
113*c0e032e0STom Rini 
114*c0e032e0STom Rini 	return fullname;
115*c0e032e0STom Rini }
116*c0e032e0STom Rini 
srcfile_relative_open(const char * fname,char ** fullnamep)117*c0e032e0STom Rini FILE *srcfile_relative_open(const char *fname, char **fullnamep)
118*c0e032e0STom Rini {
119*c0e032e0STom Rini 	FILE *f;
120*c0e032e0STom Rini 	char *fullname;
121*c0e032e0STom Rini 
122*c0e032e0STom Rini 	if (streq(fname, "-")) {
123*c0e032e0STom Rini 		f = stdin;
124*c0e032e0STom Rini 		fullname = xstrdup("<stdin>");
125*c0e032e0STom Rini 	} else {
126*c0e032e0STom Rini 		fullname = fopen_any_on_path(fname, &f);
127*c0e032e0STom Rini 		if (!f)
128*c0e032e0STom Rini 			die("Couldn't open \"%s\": %s\n", fname,
129*c0e032e0STom Rini 			    strerror(errno));
130*c0e032e0STom Rini 	}
131*c0e032e0STom Rini 
132*c0e032e0STom Rini 	if (depfile)
133*c0e032e0STom Rini 		fprintf(depfile, " %s", fullname);
134*c0e032e0STom Rini 
135*c0e032e0STom Rini 	if (fullnamep)
136*c0e032e0STom Rini 		*fullnamep = fullname;
137*c0e032e0STom Rini 	else
138*c0e032e0STom Rini 		free(fullname);
139*c0e032e0STom Rini 
140*c0e032e0STom Rini 	return f;
141*c0e032e0STom Rini }
142*c0e032e0STom Rini 
srcfile_push(const char * fname)143*c0e032e0STom Rini void srcfile_push(const char *fname)
144*c0e032e0STom Rini {
145*c0e032e0STom Rini 	struct srcfile_state *srcfile;
146*c0e032e0STom Rini 
147*c0e032e0STom Rini 	if (srcfile_depth++ >= MAX_SRCFILE_DEPTH)
148*c0e032e0STom Rini 		die("Includes nested too deeply");
149*c0e032e0STom Rini 
150*c0e032e0STom Rini 	srcfile = xmalloc(sizeof(*srcfile));
151*c0e032e0STom Rini 
152*c0e032e0STom Rini 	srcfile->f = srcfile_relative_open(fname, &srcfile->name);
153*c0e032e0STom Rini 	srcfile->dir = get_dirname(srcfile->name);
154*c0e032e0STom Rini 	srcfile->prev = current_srcfile;
155*c0e032e0STom Rini 
156*c0e032e0STom Rini 	srcfile->lineno = 1;
157*c0e032e0STom Rini 	srcfile->colno = 1;
158*c0e032e0STom Rini 
159*c0e032e0STom Rini 	current_srcfile = srcfile;
160*c0e032e0STom Rini }
161*c0e032e0STom Rini 
srcfile_pop(void)162*c0e032e0STom Rini bool srcfile_pop(void)
163*c0e032e0STom Rini {
164*c0e032e0STom Rini 	struct srcfile_state *srcfile = current_srcfile;
165*c0e032e0STom Rini 
166*c0e032e0STom Rini 	assert(srcfile);
167*c0e032e0STom Rini 
168*c0e032e0STom Rini 	current_srcfile = srcfile->prev;
169*c0e032e0STom Rini 
170*c0e032e0STom Rini 	if (fclose(srcfile->f))
171*c0e032e0STom Rini 		die("Error closing \"%s\": %s\n", srcfile->name,
172*c0e032e0STom Rini 		    strerror(errno));
173*c0e032e0STom Rini 
174*c0e032e0STom Rini 	/* FIXME: We allow the srcfile_state structure to leak,
175*c0e032e0STom Rini 	 * because it could still be referenced from a location
176*c0e032e0STom Rini 	 * variable being carried through the parser somewhere.  To
177*c0e032e0STom Rini 	 * fix this we could either allocate all the files from a
178*c0e032e0STom Rini 	 * table, or use a pool allocator. */
179*c0e032e0STom Rini 
180*c0e032e0STom Rini 	return current_srcfile ? true : false;
181*c0e032e0STom Rini }
182*c0e032e0STom Rini 
srcfile_add_search_path(const char * dirname)183*c0e032e0STom Rini void srcfile_add_search_path(const char *dirname)
184*c0e032e0STom Rini {
185*c0e032e0STom Rini 	struct search_path *node;
186*c0e032e0STom Rini 
187*c0e032e0STom Rini 	/* Create the node */
188*c0e032e0STom Rini 	node = xmalloc(sizeof(*node));
189*c0e032e0STom Rini 	node->next = NULL;
190*c0e032e0STom Rini 	node->dirname = xstrdup(dirname);
191*c0e032e0STom Rini 
192*c0e032e0STom Rini 	/* Add to the end of our list */
193*c0e032e0STom Rini 	if (search_path_tail)
194*c0e032e0STom Rini 		*search_path_tail = node;
195*c0e032e0STom Rini 	else
196*c0e032e0STom Rini 		search_path_head = node;
197*c0e032e0STom Rini 	search_path_tail = &node->next;
198*c0e032e0STom Rini }
199*c0e032e0STom Rini 
200*c0e032e0STom Rini /*
201*c0e032e0STom Rini  * The empty source position.
202*c0e032e0STom Rini  */
203*c0e032e0STom Rini 
204*c0e032e0STom Rini struct srcpos srcpos_empty = {
205*c0e032e0STom Rini 	.first_line = 0,
206*c0e032e0STom Rini 	.first_column = 0,
207*c0e032e0STom Rini 	.last_line = 0,
208*c0e032e0STom Rini 	.last_column = 0,
209*c0e032e0STom Rini 	.file = NULL,
210*c0e032e0STom Rini };
211*c0e032e0STom Rini 
srcpos_update(struct srcpos * pos,const char * text,int len)212*c0e032e0STom Rini void srcpos_update(struct srcpos *pos, const char *text, int len)
213*c0e032e0STom Rini {
214*c0e032e0STom Rini 	int i;
215*c0e032e0STom Rini 
216*c0e032e0STom Rini 	pos->file = current_srcfile;
217*c0e032e0STom Rini 
218*c0e032e0STom Rini 	pos->first_line = current_srcfile->lineno;
219*c0e032e0STom Rini 	pos->first_column = current_srcfile->colno;
220*c0e032e0STom Rini 
221*c0e032e0STom Rini 	for (i = 0; i < len; i++)
222*c0e032e0STom Rini 		if (text[i] == '\n') {
223*c0e032e0STom Rini 			current_srcfile->lineno++;
224*c0e032e0STom Rini 			current_srcfile->colno = 1;
225*c0e032e0STom Rini 		} else {
226*c0e032e0STom Rini 			current_srcfile->colno++;
227*c0e032e0STom Rini 		}
228*c0e032e0STom Rini 
229*c0e032e0STom Rini 	pos->last_line = current_srcfile->lineno;
230*c0e032e0STom Rini 	pos->last_column = current_srcfile->colno;
231*c0e032e0STom Rini }
232*c0e032e0STom Rini 
233*c0e032e0STom Rini struct srcpos *
srcpos_copy(struct srcpos * pos)234*c0e032e0STom Rini srcpos_copy(struct srcpos *pos)
235*c0e032e0STom Rini {
236*c0e032e0STom Rini 	struct srcpos *pos_new;
237*c0e032e0STom Rini 
238*c0e032e0STom Rini 	pos_new = xmalloc(sizeof(struct srcpos));
239*c0e032e0STom Rini 	memcpy(pos_new, pos, sizeof(struct srcpos));
240*c0e032e0STom Rini 
241*c0e032e0STom Rini 	return pos_new;
242*c0e032e0STom Rini }
243*c0e032e0STom Rini 
244*c0e032e0STom Rini char *
srcpos_string(struct srcpos * pos)245*c0e032e0STom Rini srcpos_string(struct srcpos *pos)
246*c0e032e0STom Rini {
247*c0e032e0STom Rini 	const char *fname = "<no-file>";
248*c0e032e0STom Rini 	char *pos_str;
249*c0e032e0STom Rini 
250*c0e032e0STom Rini 	if (pos->file && pos->file->name)
251*c0e032e0STom Rini 		fname = pos->file->name;
252*c0e032e0STom Rini 
253*c0e032e0STom Rini 
254*c0e032e0STom Rini 	if (pos->first_line != pos->last_line)
255*c0e032e0STom Rini 		xasprintf(&pos_str, "%s:%d.%d-%d.%d", fname,
256*c0e032e0STom Rini 			  pos->first_line, pos->first_column,
257*c0e032e0STom Rini 			  pos->last_line, pos->last_column);
258*c0e032e0STom Rini 	else if (pos->first_column != pos->last_column)
259*c0e032e0STom Rini 		xasprintf(&pos_str, "%s:%d.%d-%d", fname,
260*c0e032e0STom Rini 			  pos->first_line, pos->first_column,
261*c0e032e0STom Rini 			  pos->last_column);
262*c0e032e0STom Rini 	else
263*c0e032e0STom Rini 		xasprintf(&pos_str, "%s:%d.%d", fname,
264*c0e032e0STom Rini 			  pos->first_line, pos->first_column);
265*c0e032e0STom Rini 
266*c0e032e0STom Rini 	return pos_str;
267*c0e032e0STom Rini }
268*c0e032e0STom Rini 
srcpos_verror(struct srcpos * pos,const char * prefix,const char * fmt,va_list va)269*c0e032e0STom Rini void srcpos_verror(struct srcpos *pos, const char *prefix,
270*c0e032e0STom Rini 		   const char *fmt, va_list va)
271*c0e032e0STom Rini {
272*c0e032e0STom Rini 	char *srcstr;
273*c0e032e0STom Rini 
274*c0e032e0STom Rini 	srcstr = srcpos_string(pos);
275*c0e032e0STom Rini 
276*c0e032e0STom Rini 	fprintf(stderr, "%s: %s ", prefix, srcstr);
277*c0e032e0STom Rini 	vfprintf(stderr, fmt, va);
278*c0e032e0STom Rini 	fprintf(stderr, "\n");
279*c0e032e0STom Rini 
280*c0e032e0STom Rini 	free(srcstr);
281*c0e032e0STom Rini }
282*c0e032e0STom Rini 
srcpos_error(struct srcpos * pos,const char * prefix,const char * fmt,...)283*c0e032e0STom Rini void srcpos_error(struct srcpos *pos, const char *prefix,
284*c0e032e0STom Rini 		  const char *fmt, ...)
285*c0e032e0STom Rini {
286*c0e032e0STom Rini 	va_list va;
287*c0e032e0STom Rini 
288*c0e032e0STom Rini 	va_start(va, fmt);
289*c0e032e0STom Rini 	srcpos_verror(pos, prefix, fmt, va);
290*c0e032e0STom Rini 	va_end(va);
291*c0e032e0STom Rini }
292*c0e032e0STom Rini 
srcpos_set_line(char * f,int l)293*c0e032e0STom Rini void srcpos_set_line(char *f, int l)
294*c0e032e0STom Rini {
295*c0e032e0STom Rini 	current_srcfile->name = f;
296*c0e032e0STom Rini 	current_srcfile->lineno = l;
297*c0e032e0STom Rini }
298