blob: 7340c33aa37005556d983a45d68f22857317d4b4 [file] [log] [blame]
Jon Loeligere45e6fd2007-03-23 15:18:41 -05001/*
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#include "dtc.h"
21#include "srcpos.h"
22
Jon Loeligere45e6fd2007-03-23 15:18:41 -050023/*
24 * Like yylineno, this is the current open file pos.
25 */
26
Scott Wood910efac2008-01-03 17:43:31 -060027struct dtc_file *srcpos_file;
Jon Loeligere45e6fd2007-03-23 15:18:41 -050028
Scott Wood910efac2008-01-03 17:43:31 -060029static int dtc_open_one(struct dtc_file *file,
30 const char *search,
31 const char *fname)
Jon Loeligere45e6fd2007-03-23 15:18:41 -050032{
Scott Wood910efac2008-01-03 17:43:31 -060033 char *fullname;
Jon Loeligere45e6fd2007-03-23 15:18:41 -050034
Scott Wood910efac2008-01-03 17:43:31 -060035 if (search) {
36 fullname = malloc(strlen(search) + strlen(fname) + 2);
37 if (!fullname)
38 die("Out of memory\n");
Jon Loeligere45e6fd2007-03-23 15:18:41 -050039
Scott Wood910efac2008-01-03 17:43:31 -060040 strcpy(fullname, search);
41 strcat(fullname, "/");
42 strcat(fullname, fname);
43 } else {
44 fullname = strdup(fname);
45 }
Jon Loeligere45e6fd2007-03-23 15:18:41 -050046
Scott Wood910efac2008-01-03 17:43:31 -060047 file->file = fopen(fullname, "r");
48 if (!file->file) {
49 free(fullname);
50 return 0;
51 }
Jon Loeligere45e6fd2007-03-23 15:18:41 -050052
Scott Wood910efac2008-01-03 17:43:31 -060053 file->name = fullname;
54 return 1;
Jon Loeligere45e6fd2007-03-23 15:18:41 -050055}
56
57
Scott Wood910efac2008-01-03 17:43:31 -060058struct dtc_file *dtc_open_file(const char *fname,
59 const struct search_path *search)
Jon Loeligere45e6fd2007-03-23 15:18:41 -050060{
Scott Wood910efac2008-01-03 17:43:31 -060061 static const struct search_path default_search = { NULL, NULL, NULL };
Jon Loeligere45e6fd2007-03-23 15:18:41 -050062
Scott Wood910efac2008-01-03 17:43:31 -060063 struct dtc_file *file;
64 const char *slash;
65
66 file = malloc(sizeof(struct dtc_file));
67 if (!file)
68 die("Out of memory\n");
69
70 slash = strrchr(fname, '/');
71 if (slash) {
72 char *dir = malloc(slash - fname + 1);
73 if (!dir)
74 die("Out of memory\n");
75
76 memcpy(dir, fname, slash - fname);
77 dir[slash - fname] = 0;
78 file->dir = dir;
79 } else {
80 file->dir = NULL;
Jon Loeligere45e6fd2007-03-23 15:18:41 -050081 }
82
Scott Wood910efac2008-01-03 17:43:31 -060083 if (streq(fname, "-")) {
84 file->name = "stdin";
85 file->file = stdin;
86 return file;
Jon Loeligere45e6fd2007-03-23 15:18:41 -050087 }
88
Scott Wood910efac2008-01-03 17:43:31 -060089 if (!search)
90 search = &default_search;
91
92 while (search) {
93 if (dtc_open_one(file, search->dir, fname))
94 return file;
95
96 if (errno != ENOENT)
97 goto out;
98
99 search = search->next;
100 }
101
102out:
103 free(file);
104 return NULL;
Jon Loeligere45e6fd2007-03-23 15:18:41 -0500105}
106
Scott Wood910efac2008-01-03 17:43:31 -0600107void dtc_close_file(struct dtc_file *file)
Jon Loeligere45e6fd2007-03-23 15:18:41 -0500108{
Scott Wood910efac2008-01-03 17:43:31 -0600109 if (fclose(file->file))
110 die("Error closing \"%s\": %s\n", file->name, strerror(errno));
Jon Loeligere45e6fd2007-03-23 15:18:41 -0500111
Scott Wood910efac2008-01-03 17:43:31 -0600112 free(file);
Jon Loeligere45e6fd2007-03-23 15:18:41 -0500113}