2 * Copyright 2007 Jon Loeliger, Freescale Semiconductor, Inc.
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.
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.
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
25 * Record the complete unique set of opened file names.
26 * Primarily used to cache source position file names.
28 #define MAX_N_FILE_NAMES (100)
30 const char *file_names[MAX_N_FILE_NAMES];
31 static int n_file_names = 0;
34 * Like yylineno, this is the current open file pos.
37 int srcpos_filenum = -1;
41 FILE *dtc_open_file(const char *fname)
45 if (lookup_file_name(fname, 1) < 0)
46 die("Too many files opened\n");
48 if (streq(fname, "-"))
51 f = fopen(fname, "r");
54 die("Couldn't open \"%s\": %s\n", fname, strerror(errno));
62 * Locate and optionally add filename fname in the file_names[] array.
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.
68 * Index [0..MAX_N_FILE_NAMES) where the filename is kept
69 * -1 if the name can not be recorded
72 int lookup_file_name(const char *fname, int add_it)
76 for (i = 0; i < n_file_names; i++) {
77 if (strcmp(file_names[i], fname) == 0)
82 if (n_file_names < MAX_N_FILE_NAMES) {
83 file_names[n_file_names] = strdup(fname);
84 return n_file_names++;
92 const char *srcpos_filename_for_num(int filenum)
94 if (0 <= filenum && filenum < n_file_names) {
95 return file_names[filenum];
102 const char *srcpos_get_filename(void)
104 return srcpos_filename_for_num(srcpos_filenum);