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) { |
Scott Wood | 42107f8 | 2008-01-07 14:27:36 -0600 | [diff] [blame^] | 36 | fullname = xmalloc(strlen(search) + strlen(fname) + 2); |
Jon Loeliger | e45e6fd | 2007-03-23 15:18:41 -0500 | [diff] [blame] | 37 | |
Scott Wood | 910efac | 2008-01-03 17:43:31 -0600 | [diff] [blame] | 38 | strcpy(fullname, search); |
| 39 | strcat(fullname, "/"); |
| 40 | strcat(fullname, fname); |
| 41 | } else { |
| 42 | fullname = strdup(fname); |
| 43 | } |
Jon Loeliger | e45e6fd | 2007-03-23 15:18:41 -0500 | [diff] [blame] | 44 | |
Scott Wood | 910efac | 2008-01-03 17:43:31 -0600 | [diff] [blame] | 45 | file->file = fopen(fullname, "r"); |
| 46 | if (!file->file) { |
| 47 | free(fullname); |
| 48 | return 0; |
| 49 | } |
Jon Loeliger | e45e6fd | 2007-03-23 15:18:41 -0500 | [diff] [blame] | 50 | |
Scott Wood | 910efac | 2008-01-03 17:43:31 -0600 | [diff] [blame] | 51 | file->name = fullname; |
| 52 | return 1; |
Jon Loeliger | e45e6fd | 2007-03-23 15:18:41 -0500 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | |
Scott Wood | 910efac | 2008-01-03 17:43:31 -0600 | [diff] [blame] | 56 | struct dtc_file *dtc_open_file(const char *fname, |
| 57 | const struct search_path *search) |
Jon Loeliger | e45e6fd | 2007-03-23 15:18:41 -0500 | [diff] [blame] | 58 | { |
Scott Wood | 910efac | 2008-01-03 17:43:31 -0600 | [diff] [blame] | 59 | static const struct search_path default_search = { NULL, NULL, NULL }; |
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 | struct dtc_file *file; |
| 62 | const char *slash; |
| 63 | |
Scott Wood | 42107f8 | 2008-01-07 14:27:36 -0600 | [diff] [blame^] | 64 | file = xmalloc(sizeof(struct dtc_file)); |
Scott Wood | 910efac | 2008-01-03 17:43:31 -0600 | [diff] [blame] | 65 | |
| 66 | slash = strrchr(fname, '/'); |
| 67 | if (slash) { |
Scott Wood | 42107f8 | 2008-01-07 14:27:36 -0600 | [diff] [blame^] | 68 | char *dir = xmalloc(slash - fname + 1); |
Scott Wood | 910efac | 2008-01-03 17:43:31 -0600 | [diff] [blame] | 69 | |
| 70 | memcpy(dir, fname, slash - fname); |
| 71 | dir[slash - fname] = 0; |
| 72 | file->dir = dir; |
| 73 | } else { |
| 74 | file->dir = NULL; |
Jon Loeliger | e45e6fd | 2007-03-23 15:18:41 -0500 | [diff] [blame] | 75 | } |
| 76 | |
Scott Wood | 910efac | 2008-01-03 17:43:31 -0600 | [diff] [blame] | 77 | if (streq(fname, "-")) { |
| 78 | file->name = "stdin"; |
| 79 | file->file = stdin; |
| 80 | return file; |
Jon Loeliger | e45e6fd | 2007-03-23 15:18:41 -0500 | [diff] [blame] | 81 | } |
| 82 | |
Scott Wood | 5695e99 | 2008-01-04 15:10:45 -0600 | [diff] [blame] | 83 | 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 Wood | 910efac | 2008-01-03 17:43:31 -0600 | [diff] [blame] | 93 | 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 | |
| 106 | out: |
Scott Wood | 5695e99 | 2008-01-04 15:10:45 -0600 | [diff] [blame] | 107 | free((void *)file->dir); |
Scott Wood | 910efac | 2008-01-03 17:43:31 -0600 | [diff] [blame] | 108 | free(file); |
| 109 | return NULL; |
Jon Loeliger | e45e6fd | 2007-03-23 15:18:41 -0500 | [diff] [blame] | 110 | } |
| 111 | |
Scott Wood | 910efac | 2008-01-03 17:43:31 -0600 | [diff] [blame] | 112 | void dtc_close_file(struct dtc_file *file) |
Jon Loeliger | e45e6fd | 2007-03-23 15:18:41 -0500 | [diff] [blame] | 113 | { |
Scott Wood | 910efac | 2008-01-03 17:43:31 -0600 | [diff] [blame] | 114 | if (fclose(file->file)) |
| 115 | die("Error closing \"%s\": %s\n", file->name, strerror(errno)); |
Jon Loeliger | e45e6fd | 2007-03-23 15:18:41 -0500 | [diff] [blame] | 116 | |
Scott Wood | 5695e99 | 2008-01-04 15:10:45 -0600 | [diff] [blame] | 117 | free((void *)file->dir); |
Scott Wood | 910efac | 2008-01-03 17:43:31 -0600 | [diff] [blame] | 118 | free(file); |
Jon Loeliger | e45e6fd | 2007-03-23 15:18:41 -0500 | [diff] [blame] | 119 | } |