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
29 * Like yylineno, this is the current open file pos.
31 struct dtc_file *srcpos_file;
34 * The empty source position.
37 struct dtc_file dtc_empty_file = {
43 srcpos srcpos_empty = {
48 .file = &dtc_empty_file
53 dtc_open_one(struct dtc_file *file, const char *search, const char *fname)
58 fullname = xmalloc(strlen(search) + strlen(fname) + 2);
60 strcpy(fullname, search);
61 strcat(fullname, "/");
62 strcat(fullname, fname);
64 fullname = xstrdup(fname);
67 file->file = fopen(fullname, "r");
73 file->name = fullname;
79 dtc_open_file(const char *fname, const struct search_path *search)
81 static const struct search_path default_search = { NULL, NULL, NULL };
83 struct dtc_file *file;
86 file = xmalloc(sizeof(struct dtc_file));
88 slash = strrchr(fname, '/');
90 char *dir = xmalloc(slash - fname + 1);
92 memcpy(dir, fname, slash - fname);
93 dir[slash - fname] = 0;
99 if (streq(fname, "-")) {
100 file->name = "stdin";
105 if (fname[0] == '/') {
106 file->file = fopen(fname, "r");
110 file->name = xstrdup(fname);
115 search = &default_search;
118 if (dtc_open_one(file, search->dir, fname))
124 search = search->next;
128 die("Couldn't open \"%s\": %s\n", fname, strerror(errno));
133 dtc_close_file(struct dtc_file *file)
135 if (fclose(file->file))
136 die("Error closing \"%s\": %s\n", file->name, strerror(errno));
141 srcpos_copy(srcpos *pos)
145 pos_new = xmalloc(sizeof(srcpos));
146 memcpy(pos_new, pos, sizeof(srcpos));
154 srcpos_dump(srcpos *pos)
156 printf("file : \"%s\"\n",
157 pos->file ? (char *) pos->file : "<no file>");
158 printf("first_line : %d\n", pos->first_line);
159 printf("first_column: %d\n", pos->first_column);
160 printf("last_line : %d\n", pos->last_line);
161 printf("last_column : %d\n", pos->last_column);
162 printf("file : %s\n", pos->file->name);
167 srcpos_string(srcpos *pos)
175 } else if (pos->file->name) {
176 fname = pos->file->name;
177 if (strcmp(fname, "-") == 0)
183 if (pos->first_line == pos->last_line) {
184 if (pos->first_column == pos->last_column) {
185 snprintf(col_buf, sizeof(col_buf),
187 pos->first_line, pos->first_column);
189 snprintf(col_buf, sizeof(col_buf),
192 pos->first_column, pos->last_column);
196 snprintf(col_buf, sizeof(col_buf),
198 pos->first_line, pos->first_column,
199 pos->last_line, pos->last_column);
202 if (asprintf(&pos_str, "%s %s", fname, col_buf) == -1)
203 return "<unknown source position?";
210 srcpos_error(srcpos *pos, char const *fmt, ...)
216 srcstr = srcpos_string(pos);
218 fprintf(stderr, "Error: %s ", srcstr);
219 vfprintf(stderr, fmt, va);
220 fprintf(stderr, "\n");
227 srcpos_warn(srcpos *pos, char const *fmt, ...)
233 srcstr = srcpos_string(pos);
235 fprintf(stderr, "Warning: %s ", srcstr);
236 vfprintf(stderr, fmt, va);
237 fprintf(stderr, "\n");