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