2 * Copyright 2007 Jon Loeliger, Freescale Semiconductor, Inc.
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.
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.
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
28 static char *dirname(const char *path
)
30 const char *slash
= strrchr(path
, '/');
33 int len
= slash
- path
;
34 char *dir
= xmalloc(len
+ 1);
36 memcpy(dir
, path
, len
);
43 FILE *depfile
; /* = NULL */
44 struct srcfile_state
*current_srcfile
; /* = NULL */
46 /* Detect infinite include recursion. */
47 #define MAX_SRCFILE_DEPTH (100)
48 static int srcfile_depth
; /* = 0 */
50 FILE *srcfile_relative_open(const char *fname
, char **fullnamep
)
55 if (streq(fname
, "-")) {
57 fullname
= xstrdup("<stdin>");
59 if (!current_srcfile
|| !current_srcfile
->dir
61 fullname
= xstrdup(fname
);
63 fullname
= join_path(current_srcfile
->dir
, fname
);
65 f
= fopen(fullname
, "r");
67 die("Couldn't open \"%s\": %s\n", fname
,
72 fprintf(depfile
, " %s", fullname
);
75 *fullnamep
= fullname
;
82 void srcfile_push(const char *fname
)
84 struct srcfile_state
*srcfile
;
86 if (srcfile_depth
++ >= MAX_SRCFILE_DEPTH
)
87 die("Includes nested too deeply");
89 srcfile
= xmalloc(sizeof(*srcfile
));
91 srcfile
->f
= srcfile_relative_open(fname
, &srcfile
->name
);
92 srcfile
->dir
= dirname(srcfile
->name
);
93 srcfile
->prev
= current_srcfile
;
98 current_srcfile
= srcfile
;
101 int srcfile_pop(void)
103 struct srcfile_state
*srcfile
= current_srcfile
;
107 current_srcfile
= srcfile
->prev
;
109 if (fclose(srcfile
->f
))
110 die("Error closing \"%s\": %s\n", srcfile
->name
,
113 /* FIXME: We allow the srcfile_state structure to leak,
114 * because it could still be referenced from a location
115 * variable being carried through the parser somewhere. To
116 * fix this we could either allocate all the files from a
117 * table, or use a pool allocator. */
119 return current_srcfile
? 1 : 0;
123 * The empty source position.
126 struct srcpos srcpos_empty
= {
136 void srcpos_update(struct srcpos
*pos
, const char *text
, int len
)
140 pos
->file
= current_srcfile
;
142 pos
->first_line
= current_srcfile
->lineno
;
143 pos
->first_column
= current_srcfile
->colno
;
145 for (i
= 0; i
< len
; i
++)
146 if (text
[i
] == '\n') {
147 current_srcfile
->lineno
++;
148 current_srcfile
->colno
= 1;
149 } else if (text
[i
] == '\t') {
150 current_srcfile
->colno
=
151 ALIGN(current_srcfile
->colno
, TAB_SIZE
);
153 current_srcfile
->colno
++;
156 pos
->last_line
= current_srcfile
->lineno
;
157 pos
->last_column
= current_srcfile
->colno
;
161 srcpos_copy(struct srcpos
*pos
)
163 struct srcpos
*pos_new
;
165 pos_new
= xmalloc(sizeof(struct srcpos
));
166 memcpy(pos_new
, pos
, sizeof(struct srcpos
));
174 srcpos_dump(struct srcpos
*pos
)
176 printf("file : \"%s\"\n",
177 pos
->file
? (char *) pos
->file
: "<no file>");
178 printf("first_line : %d\n", pos
->first_line
);
179 printf("first_column: %d\n", pos
->first_column
);
180 printf("last_line : %d\n", pos
->last_line
);
181 printf("last_column : %d\n", pos
->last_column
);
182 printf("file : %s\n", pos
->file
->name
);
187 srcpos_string(struct srcpos
*pos
)
189 const char *fname
= "<no-file>";
194 fname
= pos
->file
->name
;
197 if (pos
->first_line
!= pos
->last_line
)
198 rc
= asprintf(&pos_str
, "%s:%d.%d-%d.%d", fname
,
199 pos
->first_line
, pos
->first_column
,
200 pos
->last_line
, pos
->last_column
);
201 else if (pos
->first_column
!= pos
->last_column
)
202 rc
= asprintf(&pos_str
, "%s:%d.%d-%d", fname
,
203 pos
->first_line
, pos
->first_column
,
206 rc
= asprintf(&pos_str
, "%s:%d.%d", fname
,
207 pos
->first_line
, pos
->first_column
);
210 die("Couldn't allocate in srcpos string");
216 srcpos_verror(struct srcpos
*pos
, char const *fmt
, va_list va
)
220 srcstr
= srcpos_string(pos
);
222 fprintf(stdout
, "Error: %s ", srcstr
);
223 vfprintf(stdout
, fmt
, va
);
224 fprintf(stdout
, "\n");
228 srcpos_error(struct srcpos
*pos
, char const *fmt
, ...)
233 srcpos_verror(pos
, fmt
, va
);
239 srcpos_warn(struct srcpos
*pos
, char const *fmt
, ...)
245 srcstr
= srcpos_string(pos
);
247 fprintf(stderr
, "Warning: %s ", srcstr
);
248 vfprintf(stderr
, fmt
, va
);
249 fprintf(stderr
, "\n");