Use autoconf correctly; provide more stats with -I
[deliverable/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 "misc.h"
29 #include "lf.h"
30 #include "table.h"
31
32 #ifdef HAVE_UNISTD_H
33 #include <unistd.h>
34 #endif
35
36 #ifdef HAVE_STDLIB_H
37 #include <stdlib.h>
38 #endif
39
40 struct _table {
41 size_t size;
42 char *buffer;
43 char *pos;
44 int line_nr;
45 int nr_fields;
46 char *file_name;
47 };
48
49 extern table *
50 table_open(char *file_name,
51 int nr_fields)
52 {
53 int fd;
54 struct stat stat_buf;
55 table *file;
56
57 /* create a file descriptor */
58 file = ZALLOC(table);
59 ASSERT(file != NULL);
60 file->nr_fields = nr_fields;
61
62 /* save the file name */
63 file->file_name = (char*)zalloc(strlen(file_name) + 1);
64 ASSERT(file->file_name != NULL);
65 strcpy(file->file_name, file_name);
66
67 /* open the file */
68 fd = open(file->file_name, O_RDONLY, 0);
69 ASSERT(fd >= 0);
70
71 /* determine the size */
72 if (fstat(fd, &stat_buf) < 0) {
73 perror("table_open.fstat");
74 exit(1);
75 }
76 file->size = stat_buf.st_size;
77
78 /* allocate this much memory */
79 file->buffer = (char*)zalloc(file->size+1);
80 if(file->buffer == NULL) {
81 perror("table_open.calloc.file->size+1");
82 exit(1);
83 }
84 file->pos = file->buffer;
85
86 /* read it in */
87 if (read(fd, file->buffer, file->size) < file->size) {
88 perror("table_open.read");
89 exit(1);
90 }
91 file->buffer[file->size] = '\0';
92
93 /* done */
94 close(fd);
95 return file;
96 }
97
98
99 extern table_entry *
100 table_entry_read(table *file)
101 {
102 int field;
103 table_entry *entry;
104
105 /* skip comments/blanks */
106 while(1) {
107 /* leading white space */
108 while (*file->pos != '\0'
109 && *file->pos != '\n'
110 && isspace(*file->pos))
111 file->pos++;
112 /* comment */
113 if (*file->pos == '#') {
114 do {
115 file->pos++;
116 } while (*file->pos != '\0' && *file->pos != '\n');
117 }
118 /* end of line? */
119 if (*file->pos == '\n') {
120 file->pos++;
121 file->line_nr++;
122 }
123 else
124 break;
125 }
126 if (*file->pos == '\0')
127 return NULL;
128
129 /* create this new entry */
130 entry = (table_entry*)zalloc(sizeof(table_entry)
131 + (file->nr_fields + 1) * sizeof(char*));
132 ASSERT(entry != NULL);
133 entry->file_name = file->file_name;
134 entry->nr_fields = file->nr_fields;
135
136 /* break the line into its colon delimitered fields */
137 for (field = 0; field < file->nr_fields-1; field++) {
138 entry->fields[field] = file->pos;
139 while(*file->pos && *file->pos != ':' && *file->pos != '\n')
140 file->pos++;
141 if (*file->pos == ':') {
142 *file->pos = '\0';
143 file->pos++;
144 }
145 }
146
147 /* any trailing stuff not the last field */
148 ASSERT(field == file->nr_fields-1);
149 entry->fields[field] = file->pos;
150 while (*file->pos && *file->pos != '\n') {
151 file->pos++;
152 }
153 if (*file->pos == '\n') {
154 *file->pos = '\0';
155 file->pos++;
156 }
157 file->line_nr++;
158 entry->line_nr = file->line_nr;
159
160 /* if following lines tab indented, put in the annex */
161 if (*file->pos == '\t') {
162 entry->annex = file->pos;
163 do {
164 do {
165 file->pos++;
166 } while (*file->pos != '\0' && *file->pos != '\n');
167 if (*file->pos == '\n') {
168 file->pos++;
169 file->line_nr++;
170 }
171 } while (*file->pos != '\0' && *file->pos == '\t');
172 if (file->pos[-1] == '\n')
173 file->pos[-1] = '\0';
174 }
175 else
176 entry->annex = NULL;
177
178 /* return it */
179 return entry;
180
181 }
182
183
184 extern void
185 dump_table_entry(table_entry *entry,
186 int indent)
187 {
188 printf("(table_entry*)%p\n", entry);
189
190 if (entry != NULL) {
191 int field;
192 char sep;
193
194 sep = ' ';
195 dumpf(indent, "(fields");
196 for (field = 0; field < entry->nr_fields; field++) {
197 printf("%c%s", sep, entry->fields[field]);
198 sep = ':';
199 }
200 printf(")\n");
201
202 dumpf(indent, "(line_nr %d)\n", entry->line_nr);
203
204 dumpf(indent, "(file_name %s)\n", entry->file_name);
205
206 dumpf(indent, "(annex\n%s\n", entry->annex);
207 dumpf(indent, " )\n");
208
209 }
210 }
211
212
213 extern void
214 table_entry_lf_c_line_nr(lf *file,
215 table_entry *entry)
216 {
217 lf_print_c_line_nr(file, entry->line_nr, entry->file_name);
218 }
219
220
This page took 0.04054 seconds and 5 git commands to generate.