Jon Loeliger | e45e6fd | 2007-03-23 15:18:41 -0500 | [diff] [blame] | 1 | /* |
| 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 Loeliger | e45e6fd | 2007-03-23 15:18:41 -0500 | [diff] [blame] | 23 | /* |
| 24 | * Like yylineno, this is the current open file pos. |
| 25 | */ |
| 26 | |
Scott Wood | 910efac | 2008-01-03 17:43:31 -0600 | [diff] [blame^] | 27 | struct dtc_file *srcpos_file; |
Jon Loeliger | e45e6fd | 2007-03-23 15:18:41 -0500 | [diff] [blame] | 28 | |
Scott Wood | 910efac | 2008-01-03 17:43:31 -0600 | [diff] [blame^] | 29 | static int dtc_open_one(struct dtc_file *file, |
| 30 | const char *search, |
| 31 | const char *fname) |
Jon Loeliger | e45e6fd | 2007-03-23 15:18:41 -0500 | [diff] [blame] | 32 | { |
Scott Wood | 910efac | 2008-01-03 17:43:31 -0600 | [diff] [blame^] | 33 | char *fullname; |
Jon Loeliger | e45e6fd | 2007-03-23 15:18:41 -0500 | [diff] [blame] | 34 | |
Scott Wood | 910efac | 2008-01-03 17:43:31 -0600 | [diff] [blame^] | 35 | if (search) { |
| 36 | fullname = malloc(strlen(search) + strlen(fname) + 2); |
| 37 | if (!fullname) |
| 38 | die("Out of memory\n"); |
Jon Loeliger | e45e6fd | 2007-03-23 15:18:41 -0500 | [diff] [blame] | 39 | |
Scott Wood | 910efac | 2008-01-03 17:43:31 -0600 | [diff] [blame^] | 40 | strcpy(fullname, search); |
| 41 | strcat(fullname, "/"); |
| 42 | strcat(fullname, fname); |
| 43 | } else { |
| 44 | fullname = strdup(fname); |
| 45 | } |
Jon Loeliger | e45e6fd | 2007-03-23 15:18:41 -0500 | [diff] [blame] | 46 | |
Scott Wood | 910efac | 2008-01-03 17:43:31 -0600 | [diff] [blame^] | 47 | file->file = fopen(fullname, "r"); |
| 48 | if (!file->file) { |
| 49 | free(fullname); |
| 50 | return 0; |
| 51 | } |
Jon Loeliger | e45e6fd | 2007-03-23 15:18:41 -0500 | [diff] [blame] | 52 | |
Scott Wood | 910efac | 2008-01-03 17:43:31 -0600 | [diff] [blame^] | 53 | file->name = fullname; |
| 54 | return 1; |
Jon Loeliger | e45e6fd | 2007-03-23 15:18:41 -0500 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | |
Scott Wood | 910efac | 2008-01-03 17:43:31 -0600 | [diff] [blame^] | 58 | struct dtc_file *dtc_open_file(const char *fname, |
| 59 | const struct search_path *search) |
Jon Loeliger | e45e6fd | 2007-03-23 15:18:41 -0500 | [diff] [blame] | 60 | { |
Scott Wood | 910efac | 2008-01-03 17:43:31 -0600 | [diff] [blame^] | 61 | static const struct search_path default_search = { NULL, NULL, NULL }; |
Jon Loeliger | e45e6fd | 2007-03-23 15:18:41 -0500 | [diff] [blame] | 62 | |
Scott Wood | 910efac | 2008-01-03 17:43:31 -0600 | [diff] [blame^] | 63 | 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 Loeliger | e45e6fd | 2007-03-23 15:18:41 -0500 | [diff] [blame] | 81 | } |
| 82 | |
Scott Wood | 910efac | 2008-01-03 17:43:31 -0600 | [diff] [blame^] | 83 | if (streq(fname, "-")) { |
| 84 | file->name = "stdin"; |
| 85 | file->file = stdin; |
| 86 | return file; |
Jon Loeliger | e45e6fd | 2007-03-23 15:18:41 -0500 | [diff] [blame] | 87 | } |
| 88 | |
Scott Wood | 910efac | 2008-01-03 17:43:31 -0600 | [diff] [blame^] | 89 | 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 | |
| 102 | out: |
| 103 | free(file); |
| 104 | return NULL; |
Jon Loeliger | e45e6fd | 2007-03-23 15:18:41 -0500 | [diff] [blame] | 105 | } |
| 106 | |
Scott Wood | 910efac | 2008-01-03 17:43:31 -0600 | [diff] [blame^] | 107 | void dtc_close_file(struct dtc_file *file) |
Jon Loeliger | e45e6fd | 2007-03-23 15:18:41 -0500 | [diff] [blame] | 108 | { |
Scott Wood | 910efac | 2008-01-03 17:43:31 -0600 | [diff] [blame^] | 109 | if (fclose(file->file)) |
| 110 | die("Error closing \"%s\": %s\n", file->name, strerror(errno)); |
Jon Loeliger | e45e6fd | 2007-03-23 15:18:41 -0500 | [diff] [blame] | 111 | |
Scott Wood | 910efac | 2008-01-03 17:43:31 -0600 | [diff] [blame^] | 112 | free(file); |
Jon Loeliger | e45e6fd | 2007-03-23 15:18:41 -0500 | [diff] [blame] | 113 | } |