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 struct srcfile_state *current_srcfile; /* = NULL */
45 /* Detect infinite include recursion. */
46 #define MAX_SRCFILE_DEPTH (100)
47 static int srcfile_depth; /* = 0 */
49 FILE *srcfile_relative_open(const char *fname, char **fullnamep)
54 if (streq(fname, "-")) {
56 fullname = xstrdup("<stdin>");
58 if (!current_srcfile || !current_srcfile->dir
60 fullname = xstrdup(fname);
62 fullname = join_path(current_srcfile->dir, fname);
64 f = fopen(fullname, "r");
66 die("Couldn't open \"%s\": %s\n", fname,
71 *fullnamep = fullname;
78 void srcfile_push(const char *fname)
80 struct srcfile_state *srcfile;
82 if (srcfile_depth++ >= MAX_SRCFILE_DEPTH)
83 die("Includes nested too deeply");
85 srcfile = xmalloc(sizeof(*srcfile));
87 srcfile->f = srcfile_relative_open(fname, &srcfile->name);
88 srcfile->dir = dirname(srcfile->name);
89 srcfile->prev = current_srcfile;
94 current_srcfile = srcfile;
99 struct srcfile_state *srcfile = current_srcfile;
103 current_srcfile = srcfile->prev;
105 if (fclose(srcfile->f))
106 die("Error closing \"%s\": %s\n", srcfile->name,
109 /* FIXME: We allow the srcfile_state structure to leak,
110 * because it could still be referenced from a location
111 * variable being carried through the parser somewhere. To
112 * fix this we could either allocate all the files from a
113 * table, or use a pool allocator. */
115 return current_srcfile ? 1 : 0;
119 * The empty source position.
122 struct srcpos srcpos_empty = {
132 void srcpos_update(struct srcpos *pos, const char *text, int len)
136 pos->file = current_srcfile;
138 pos->first_line = current_srcfile->lineno;
139 pos->first_column = current_srcfile->colno;
141 for (i = 0; i < len; i++)
142 if (text[i] == '\n') {
143 current_srcfile->lineno++;
144 current_srcfile->colno = 1;
145 } else if (text[i] == '\t') {
146 current_srcfile->colno =
147 ALIGN(current_srcfile->colno, TAB_SIZE);
149 current_srcfile->colno++;
152 pos->last_line = current_srcfile->lineno;
153 pos->last_column = current_srcfile->colno;
157 srcpos_copy(struct srcpos *pos)
159 struct srcpos *pos_new;
161 pos_new = xmalloc(sizeof(struct srcpos));
162 memcpy(pos_new, pos, sizeof(struct srcpos));
170 srcpos_dump(struct srcpos *pos)
172 printf("file : \"%s\"\n",
173 pos->file ? (char *) pos->file : "<no file>");
174 printf("first_line : %d\n", pos->first_line);
175 printf("first_column: %d\n", pos->first_column);
176 printf("last_line : %d\n", pos->last_line);
177 printf("last_column : %d\n", pos->last_column);
178 printf("file : %s\n", pos->file->name);
183 srcpos_string(struct srcpos *pos)
185 const char *fname = "<no-file>";
190 fname = pos->file->name;
193 if (pos->first_line != pos->last_line)
194 rc = asprintf(&pos_str, "%s:%d.%d-%d.%d", fname,
195 pos->first_line, pos->first_column,
196 pos->last_line, pos->last_column);
197 else if (pos->first_column != pos->last_column)
198 rc = asprintf(&pos_str, "%s:%d.%d-%d", fname,
199 pos->first_line, pos->first_column,
202 rc = asprintf(&pos_str, "%s:%d.%d", fname,
203 pos->first_line, pos->first_column);
206 die("Couldn't allocate in srcpos string");
212 srcpos_verror(struct srcpos *pos, char const *fmt, va_list va)
216 srcstr = srcpos_string(pos);
218 fprintf(stdout, "Error: %s ", srcstr);
219 vfprintf(stdout, fmt, va);
220 fprintf(stdout, "\n");
224 srcpos_error(struct srcpos *pos, char const *fmt, ...)
229 srcpos_verror(pos, fmt, va);
235 srcpos_warn(struct srcpos *pos, char const *fmt, ...)
241 srcstr = srcpos_string(pos);
243 fprintf(stderr, "Warning: %s ", srcstr);
244 vfprintf(stderr, fmt, va);
245 fprintf(stderr, "\n");