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