blob: c8eaa1ef67a076e19035f48421e5cc528228d793 [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) {
Scott Wood42107f82008-01-07 14:27:36 -060036 fullname = xmalloc(strlen(search) + strlen(fname) + 2);
Jon Loeligere45e6fd2007-03-23 15:18:41 -050037
Scott Wood910efac2008-01-03 17:43:31 -060038 strcpy(fullname, search);
39 strcat(fullname, "/");
40 strcat(fullname, fname);
41 } else {
42 fullname = strdup(fname);
43 }
Jon Loeligere45e6fd2007-03-23 15:18:41 -050044
Scott Wood910efac2008-01-03 17:43:31 -060045 file->file = fopen(fullname, "r");
46 if (!file->file) {
47 free(fullname);
48 return 0;
49 }
Jon Loeligere45e6fd2007-03-23 15:18:41 -050050
Scott Wood910efac2008-01-03 17:43:31 -060051 file->name = fullname;
52 return 1;
Jon Loeligere45e6fd2007-03-23 15:18:41 -050053}
54
55
Scott Wood910efac2008-01-03 17:43:31 -060056struct dtc_file *dtc_open_file(const char *fname,
57 const struct search_path *search)
Jon Loeligere45e6fd2007-03-23 15:18:41 -050058{
Scott Wood910efac2008-01-03 17:43:31 -060059 static const struct search_path default_search = { NULL, NULL, NULL };
Jon Loeligere45e6fd2007-03-23 15:18:41 -050060
Scott Wood910efac2008-01-03 17:43:31 -060061 struct dtc_file *file;
62 const char *slash;
63
Scott Wood42107f82008-01-07 14:27:36 -060064 file = xmalloc(sizeof(struct dtc_file));
Scott Wood910efac2008-01-03 17:43:31 -060065
66 slash = strrchr(fname, '/');
67 if (slash) {
Scott Wood42107f82008-01-07 14:27:36 -060068 char *dir = xmalloc(slash - fname + 1);
Scott Wood910efac2008-01-03 17:43:31 -060069
70 memcpy(dir, fname, slash - fname);
71 dir[slash - fname] = 0;
72 file->dir = dir;
73 } else {
74 file->dir = NULL;
Jon Loeligere45e6fd2007-03-23 15:18:41 -050075 }
76
Scott Wood910efac2008-01-03 17:43:31 -060077 if (streq(fname, "-")) {
78 file->name = "stdin";
79 file->file = stdin;
80 return file;
Jon Loeligere45e6fd2007-03-23 15:18:41 -050081 }
82
Scott Wood5695e992008-01-04 15:10:45 -060083 if (fname[0] == '/') {
84 file->file = fopen(fname, "r");
85
86 if (!file->file)
87 goto out;
88
89 file->name = strdup(fname);
90 return file;
91 }
92
Scott Wood910efac2008-01-03 17:43:31 -060093 if (!search)
94 search = &default_search;
95
96 while (search) {
97 if (dtc_open_one(file, search->dir, fname))
98 return file;
99
100 if (errno != ENOENT)
101 goto out;
102
103 search = search->next;
104 }
105
106out:
Scott Wood0f635df2008-01-11 13:14:57 -0600107 free(file->dir);
Scott Wood910efac2008-01-03 17:43:31 -0600108 free(file);
109 return NULL;
Jon Loeligere45e6fd2007-03-23 15:18:41 -0500110}
111
Scott Wood910efac2008-01-03 17:43:31 -0600112void dtc_close_file(struct dtc_file *file)
Jon Loeligere45e6fd2007-03-23 15:18:41 -0500113{
Scott Wood910efac2008-01-03 17:43:31 -0600114 if (fclose(file->file))
115 die("Error closing \"%s\": %s\n", file->name, strerror(errno));
Jon Loeligere45e6fd2007-03-23 15:18:41 -0500116
Scott Wood0f635df2008-01-11 13:14:57 -0600117 free(file->dir);
Scott Wood910efac2008-01-03 17:43:31 -0600118 free(file);
Jon Loeligere45e6fd2007-03-23 15:18:41 -0500119}