first stage in function unit support; add new switches & latest code from andrew
[binutils-gdb.git] / sim / ppc / table.c
1 /* This file is part of the program psim.
2
3 Copyright (C) 1994-1995, Andrew Cagney <cagney@highland.com.au>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18
19 */
20
21
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <stdio.h>
25 #include <fcntl.h>
26 #include <ctype.h>
27
28 #include "config.h"
29 #include "misc.h"
30 #include "lf.h"
31 #include "table.h"
32
33 #ifdef HAVE_UNISTD_H
34 #include <unistd.h>
35 #endif
36
37 #ifdef HAVE_STDLIB_H
38 #include <stdlib.h>
39 #endif
40
41 struct _table {
42 size_t size;
43 char *buffer;
44 char *pos;
45 int line_nr;
46 int nr_fields;
47 char *file_name;
48 };
49
50 extern table *
51 table_open(char *file_name,
52 int nr_fields)
53 {
54 int fd;
55 struct stat stat_buf;
56 table *file;
57
58 /* create a file descriptor */
59 file = ZALLOC(table);
60 ASSERT(file != NULL);
61 file->nr_fields = nr_fields;
62
63 /* save the file name */
64 file->file_name = (char*)zalloc(strlen(file_name) + 1);
65 ASSERT(file->file_name != NULL);
66 strcpy(file->file_name, file_name);
67
68 /* open the file */
69 fd = open(file->file_name, O_RDONLY, 0);
70 ASSERT(fd >= 0);
71
72 /* determine the size */
73 if (fstat(fd, &stat_buf) < 0) {
74 perror("table_open.fstat");
75 exit(1);
76 }
77 file->size = stat_buf.st_size;
78
79 /* allocate this much memory */
80 file->buffer = (char*)zalloc(file->size+1);
81 if(file->buffer == NULL) {
82 perror("table_open.calloc.file->size+1");
83 exit(1);
84 }
85 file->pos = file->buffer;
86
87 /* read it in */
88 if (read(fd, file->buffer, file->size) < file->size) {
89 perror("table_open.read");
90 exit(1);
91 }
92 file->buffer[file->size] = '\0';
93
94 /* done */
95 close(fd);
96 return file;
97 }
98
99
100 extern table_entry *
101 table_entry_read(table *file)
102 {
103 int field;
104 table_entry *entry;
105
106 /* skip comments/blanks */
107 while(1) {
108 /* leading white space */
109 while (*file->pos != '\0'
110 && *file->pos != '\n'
111 && isspace(*file->pos))
112 file->pos++;
113 /* comment */
114 if (*file->pos == '#') {
115 do {
116 file->pos++;
117 } while (*file->pos != '\0' && *file->pos != '\n');
118 }
119 /* end of line? */
120 if (*file->pos == '\n') {
121 file->pos++;
122 file->line_nr++;
123 }
124 else
125 break;
126 }
127 if (*file->pos == '\0')
128 return NULL;
129
130 /* create this new entry */
131 entry = (table_entry*)zalloc(sizeof(table_entry)
132 + (file->nr_fields + 1) * sizeof(char*));
133 ASSERT(entry != NULL);
134 entry->file_name = file->file_name;
135 entry->nr_fields = file->nr_fields;
136
137 /* break the line into its colon delimitered fields */
138 for (field = 0; field < file->nr_fields-1; field++) {
139 entry->fields[field] = file->pos;
140 while(*file->pos && *file->pos != ':' && *file->pos != '\n')
141 file->pos++;
142 if (*file->pos == ':') {
143 *file->pos = '\0';
144 file->pos++;
145 }
146 }
147
148 /* any trailing stuff not the last field */
149 ASSERT(field == file->nr_fields-1);
150 entry->fields[field] = file->pos;
151 while (*file->pos && *file->pos != '\n') {
152 file->pos++;
153 }
154 if (*file->pos == '\n') {
155 *file->pos = '\0';
156 file->pos++;
157 }
158 file->line_nr++;
159 entry->line_nr = file->line_nr;
160
161 /* if following lines tab indented, put in the annex */
162 if (*file->pos == '\t') {
163 entry->annex = file->pos;
164 do {
165 do {
166 file->pos++;
167 } while (*file->pos != '\0' && *file->pos != '\n');
168 if (*file->pos == '\n') {
169 file->pos++;
170 file->line_nr++;
171 }
172 } while (*file->pos != '\0' && *file->pos == '\t');
173 if (file->pos[-1] == '\n')
174 file->pos[-1] = '\0';
175 }
176 else
177 entry->annex = NULL;
178
179 /* return it */
180 return entry;
181
182 }
183
184
185 extern void
186 dump_table_entry(table_entry *entry,
187 int indent)
188 {
189 printf("(table_entry*)%p\n", entry);
190
191 if (entry != NULL) {
192 int field;
193 char sep;
194
195 sep = ' ';
196 dumpf(indent, "(fields");
197 for (field = 0; field < entry->nr_fields; field++) {
198 printf("%c%s", sep, entry->fields[field]);
199 sep = ':';
200 }
201 printf(")\n");
202
203 dumpf(indent, "(line_nr %d)\n", entry->line_nr);
204
205 dumpf(indent, "(file_name %s)\n", entry->file_name);
206
207 dumpf(indent, "(annex\n%s\n", entry->annex);
208 dumpf(indent, " )\n");
209
210 }
211 }
212
213
214 extern void
215 table_entry_lf_c_line_nr(lf *file,
216 table_entry *entry)
217 {
218 lf_print_c_line_nr(file, entry->line_nr, entry->file_name);
219 }
220
221