at v2.6.27 105 lines 2.4 kB view raw
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 23 24/* 25 * Record the complete unique set of opened file names. 26 * Primarily used to cache source position file names. 27 */ 28#define MAX_N_FILE_NAMES (100) 29 30const char *file_names[MAX_N_FILE_NAMES]; 31static int n_file_names = 0; 32 33/* 34 * Like yylineno, this is the current open file pos. 35 */ 36 37int srcpos_filenum = -1; 38 39 40 41FILE *dtc_open_file(const char *fname) 42{ 43 FILE *f; 44 45 if (lookup_file_name(fname, 1) < 0) 46 die("Too many files opened\n"); 47 48 if (streq(fname, "-")) 49 f = stdin; 50 else 51 f = fopen(fname, "r"); 52 53 if (! f) 54 die("Couldn't open \"%s\": %s\n", fname, strerror(errno)); 55 56 return f; 57} 58 59 60 61/* 62 * Locate and optionally add filename fname in the file_names[] array. 63 * 64 * If the filename is currently not in the array and the boolean 65 * add_it is non-zero, an attempt to add the filename will be made. 66 * 67 * Returns; 68 * Index [0..MAX_N_FILE_NAMES) where the filename is kept 69 * -1 if the name can not be recorded 70 */ 71 72int lookup_file_name(const char *fname, int add_it) 73{ 74 int i; 75 76 for (i = 0; i < n_file_names; i++) { 77 if (strcmp(file_names[i], fname) == 0) 78 return i; 79 } 80 81 if (add_it) { 82 if (n_file_names < MAX_N_FILE_NAMES) { 83 file_names[n_file_names] = strdup(fname); 84 return n_file_names++; 85 } 86 } 87 88 return -1; 89} 90 91 92const char *srcpos_filename_for_num(int filenum) 93{ 94 if (0 <= filenum && filenum < n_file_names) { 95 return file_names[filenum]; 96 } 97 98 return 0; 99} 100 101 102const char *srcpos_get_filename(void) 103{ 104 return srcpos_filename_for_num(srcpos_filenum); 105}