blob: 164b0bf344183ae0e36b3a3431ccf977c2c21ea4 [file] [log] [blame]
Nicholas Flintham1e3d3112013-04-10 10:48:38 +01001/*
2 * Copyright 2007 Jon Loeliger, Freescale Semiconductor, Inc.
3 *
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.
8 *
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.
13 *
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
17 * USA
18 */
19
20#define _GNU_SOURCE
21
22#include <stdio.h>
23
24#include "dtc.h"
25#include "srcpos.h"
26
27
28static char *dirname(const char *path)
29{
30 const char *slash = strrchr(path, '/');
31
32 if (slash) {
33 int len = slash - path;
34 char *dir = xmalloc(len + 1);
35
36 memcpy(dir, path, len);
37 dir[len] = '\0';
38 return dir;
39 }
40 return NULL;
41}
42
43FILE *depfile;
44struct srcfile_state *current_srcfile;
45
46#define MAX_SRCFILE_DEPTH (100)
47static int srcfile_depth;
48
49FILE *srcfile_relative_open(const char *fname, char **fullnamep)
50{
51 FILE *f;
52 char *fullname;
53
54 if (streq(fname, "-")) {
55 f = stdin;
56 fullname = xstrdup("<stdin>");
57 } else {
58 if (!current_srcfile || !current_srcfile->dir
59 || (fname[0] == '/'))
60 fullname = xstrdup(fname);
61 else
62 fullname = join_path(current_srcfile->dir, fname);
63
64 f = fopen(fullname, "r");
65 if (!f)
66 die("Couldn't open \"%s\": %s\n", fname,
67 strerror(errno));
68 }
69
70 if (depfile)
71 fprintf(depfile, " %s", fullname);
72
73 if (fullnamep)
74 *fullnamep = fullname;
75 else
76 free(fullname);
77
78 return f;
79}
80
81void srcfile_push(const char *fname)
82{
83 struct srcfile_state *srcfile;
84
85 if (srcfile_depth++ >= MAX_SRCFILE_DEPTH)
86 die("Includes nested too deeply");
87
88 srcfile = xmalloc(sizeof(*srcfile));
89
90 srcfile->f = srcfile_relative_open(fname, &srcfile->name);
91 srcfile->dir = dirname(srcfile->name);
92 srcfile->prev = current_srcfile;
93
94 srcfile->lineno = 1;
95 srcfile->colno = 1;
96
97 current_srcfile = srcfile;
98}
99
100int srcfile_pop(void)
101{
102 struct srcfile_state *srcfile = current_srcfile;
103
104 assert(srcfile);
105
106 current_srcfile = srcfile->prev;
107
108 if (fclose(srcfile->f))
109 die("Error closing \"%s\": %s\n", srcfile->name,
110 strerror(errno));
111
112
113 return current_srcfile ? 1 : 0;
114}
115
116
117struct srcpos srcpos_empty = {
118 .first_line = 0,
119 .first_column = 0,
120 .last_line = 0,
121 .last_column = 0,
122 .file = NULL,
123};
124
125#define TAB_SIZE 8
126
127void srcpos_update(struct srcpos *pos, const char *text, int len)
128{
129 int i;
130
131 pos->file = current_srcfile;
132
133 pos->first_line = current_srcfile->lineno;
134 pos->first_column = current_srcfile->colno;
135
136 for (i = 0; i < len; i++)
137 if (text[i] == '\n') {
138 current_srcfile->lineno++;
139 current_srcfile->colno = 1;
140 } else if (text[i] == '\t') {
141 current_srcfile->colno =
142 ALIGN(current_srcfile->colno, TAB_SIZE);
143 } else {
144 current_srcfile->colno++;
145 }
146
147 pos->last_line = current_srcfile->lineno;
148 pos->last_column = current_srcfile->colno;
149}
150
151struct srcpos *
152srcpos_copy(struct srcpos *pos)
153{
154 struct srcpos *pos_new;
155
156 pos_new = xmalloc(sizeof(struct srcpos));
157 memcpy(pos_new, pos, sizeof(struct srcpos));
158
159 return pos_new;
160}
161
162
163
164void
165srcpos_dump(struct srcpos *pos)
166{
167 printf("file : \"%s\"\n",
168 pos->file ? (char *) pos->file : "<no file>");
169 printf("first_line : %d\n", pos->first_line);
170 printf("first_column: %d\n", pos->first_column);
171 printf("last_line : %d\n", pos->last_line);
172 printf("last_column : %d\n", pos->last_column);
173 printf("file : %s\n", pos->file->name);
174}
175
176
177char *
178srcpos_string(struct srcpos *pos)
179{
180 const char *fname = "<no-file>";
181 char *pos_str;
182 int rc;
183
184 if (pos)
185 fname = pos->file->name;
186
187
188 if (pos->first_line != pos->last_line)
189 rc = asprintf(&pos_str, "%s:%d.%d-%d.%d", fname,
190 pos->first_line, pos->first_column,
191 pos->last_line, pos->last_column);
192 else if (pos->first_column != pos->last_column)
193 rc = asprintf(&pos_str, "%s:%d.%d-%d", fname,
194 pos->first_line, pos->first_column,
195 pos->last_column);
196 else
197 rc = asprintf(&pos_str, "%s:%d.%d", fname,
198 pos->first_line, pos->first_column);
199
200 if (rc == -1)
201 die("Couldn't allocate in srcpos string");
202
203 return pos_str;
204}
205
206void
207srcpos_verror(struct srcpos *pos, char const *fmt, va_list va)
208{
209 const char *srcstr;
210
211 srcstr = srcpos_string(pos);
212
213 fprintf(stdout, "Error: %s ", srcstr);
214 vfprintf(stdout, fmt, va);
215 fprintf(stdout, "\n");
216}
217
218void
219srcpos_error(struct srcpos *pos, char const *fmt, ...)
220{
221 va_list va;
222
223 va_start(va, fmt);
224 srcpos_verror(pos, fmt, va);
225 va_end(va);
226}
227
228
229void
230srcpos_warn(struct srcpos *pos, char const *fmt, ...)
231{
232 const char *srcstr;
233 va_list va;
234 va_start(va, fmt);
235
236 srcstr = srcpos_string(pos);
237
238 fprintf(stderr, "Warning: %s ", srcstr);
239 vfprintf(stderr, fmt, va);
240 fprintf(stderr, "\n");
241
242 va_end(va);
243}