table.c
7.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/* This file is part of the program psim.
Copyright (C) 1994-1995, Andrew Cagney <cagney@highland.com.au>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <fcntl.h>
#include <ctype.h>
#include "config.h"
#include "misc.h"
#include "lf.h"
#include "table.h"
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
typedef struct _open_table open_table;
struct _open_table {
size_t size;
char *buffer;
char *pos;
int line_nr;
int nr_fields;
int nr_model_fields;
char *file_name;
open_table *parent;
table *root;
};
struct _table {
open_table *current;
};
void
table_push (table *root,
table_include *includes,
const char *file_name,
int nr_fields,
int nr_model_fields)
{
int fd;
struct stat stat_buf;
open_table *file;
table_include dummy;
table_include *include = &dummy;
int nr;
/* dummy up a search of this directory */
dummy.next = includes;
dummy.dir = "";
/* create a file descriptor */
file = ZALLOC (open_table);
ASSERT(file != NULL);
file->nr_fields = nr_fields;
file->nr_model_fields = nr_model_fields;
file->root = root;
file->parent = root->current;
root->current = file;
while (1)
{
/* save the file name */
char *dup_name = NZALLOC (char, strlen (include->dir) + strlen (file_name) + 2);
if (dup_name == NULL)
{
perror (file_name);
exit (1);
}
if (include->dir[0] != '\0')
{
strcat (dup_name, include->dir);
strcat (dup_name, "/");
}
strcat (dup_name, file_name);
file->file_name = dup_name;
/* open the file */
fd = open (dup_name, O_RDONLY, 0);
if (fd >= 0)
break;
/* zfree (dup_name); */
if (include->next == NULL)
{
error ("Problem opening file `%s'\n", file_name);
perror (file_name);
exit (1);
}
include = include->next;
}
/* determine the size */
if (fstat(fd, &stat_buf) < 0) {
perror("table_open.fstat");
exit(1);
}
file->size = stat_buf.st_size;
/* allocate this much memory */
file->buffer = (char*)zalloc(file->size+1);
if(file->buffer == NULL) {
perror("table_open.calloc.file->size+1");
exit(1);
}
file->pos = file->buffer;
/* read it in */
#ifdef __CYGWIN32__
if ((file->size) && ((nr = read(fd, file->buffer, file->size)) <= 0)) {
#else
if ((nr = read(fd, file->buffer, file->size)) < file->size) {
#endif
perror("table_open.read");
exit(1);
}
file->size = nr;
file->buffer[file->size] = '\0';
/* done */
close(fd);
}
extern table *
table_open(const char *file_name,
int nr_fields,
int nr_model_fields)
{
table *root;
/* create a file descriptor */
root = ZALLOC (table);
if (root == NULL)
{
perror (file_name);
exit (1);
}
table_push (root, NULL, file_name, nr_fields, nr_model_fields);
return root;
}
extern table_entry *
table_entry_read(table *root)
{
open_table *file = root->current;
int field;
table_entry *entry;
/* skip comments/blanks */
while(1) {
/* end-of-file? */
while (*file->pos == '\0')
{
if (file->parent != NULL)
{
file = file->parent;
root->current = file;
}
else
return NULL;
}
/* leading white space */
while (*file->pos != '\0'
&& *file->pos != '\n'
&& isspace(*file->pos))
file->pos++;
/* comment */
if (*file->pos == '#') {
do {
file->pos++;
} while (*file->pos != '\0' && *file->pos != '\n');
}
/* end of line? */
if (*file->pos == '\n') {
file->pos++;
file->line_nr++;
}
else
break;
}
/* create this new entry */
entry = (table_entry*)zalloc(sizeof(table_entry)
+ (file->nr_fields + 1) * sizeof(char*));
ASSERT(entry != NULL);
entry->file_name = file->file_name;
entry->nr_fields = file->nr_fields;
/* break the line into its colon delimitered fields */
for (field = 0; field < file->nr_fields-1; field++) {
entry->fields[field] = file->pos;
while(*file->pos && *file->pos != ':' && *file->pos != '\n')
file->pos++;
if (*file->pos == ':') {
*file->pos = '\0';
file->pos++;
}
}
/* any trailing stuff not the last field */
ASSERT(field == file->nr_fields-1);
entry->fields[field] = file->pos;
while (*file->pos && *file->pos != '\n') {
file->pos++;
}
if (*file->pos == '\n') {
*file->pos = '\0';
file->pos++;
}
file->line_nr++;
/* if following lines begin with a star, add them to the model
section. */
while ((file->nr_model_fields > 0) && (*file->pos == '*')) {
table_model_entry *model = (table_model_entry*)zalloc(sizeof(table_model_entry)
+ (file->nr_model_fields + 1) * sizeof(char*));
if (entry->model_last)
entry->model_last->next = model;
else
entry->model_first = model;
entry->model_last = model;
/* break the line into its colon delimitered fields */
file->pos++;
for (field = 0; field < file->nr_model_fields-1; field++) {
model->fields[field] = file->pos;
while(*file->pos && *file->pos != ':' && *file->pos != '\n')
file->pos++;
if (*file->pos == ':') {
*file->pos = '\0';
file->pos++;
}
}
/* any trailing stuff not the last field */
ASSERT(field == file->nr_model_fields-1);
model->fields[field] = file->pos;
while (*file->pos && *file->pos != '\n') {
file->pos++;
}
if (*file->pos == '\n') {
*file->pos = '\0';
file->pos++;
}
file->line_nr++;
model->line_nr = file->line_nr;
}
entry->line_nr = file->line_nr;
/* if following lines are tab indented, put in the annex */
if (*file->pos == '\t') {
entry->annex = file->pos;
do {
do {
file->pos++;
} while (*file->pos != '\0' && *file->pos != '\n');
if (*file->pos == '\n') {
char *save_pos = ++file->pos;
int extra_lines = 0;
file->line_nr++;
/* Allow tab indented to have blank lines */
while (*save_pos == '\n') {
save_pos++;
extra_lines++;
}
if (*save_pos == '\t') {
file->pos = save_pos;
file->line_nr += extra_lines;
}
}
} while (*file->pos != '\0' && *file->pos == '\t');
if (file->pos[-1] == '\n')
file->pos[-1] = '\0';
}
else
entry->annex = NULL;
/* return it */
return entry;
}
extern void
dump_table_entry(table_entry *entry,
int indent)
{
printf("(table_entry*)%p\n", entry);
if (entry != NULL) {
int field;
char sep;
sep = ' ';
dumpf(indent, "(fields");
for (field = 0; field < entry->nr_fields; field++) {
printf("%c%s", sep, entry->fields[field]);
sep = ':';
}
printf(")\n");
dumpf(indent, "(line_nr %d)\n", entry->line_nr);
dumpf(indent, "(file_name %s)\n", entry->file_name);
dumpf(indent, "(annex\n%s\n", entry->annex);
dumpf(indent, " )\n");
}
}
extern void
table_entry_print_cpp_line_nr(lf *file,
table_entry *entry)
{
lf_print__external_reference(file, entry->line_nr, entry->file_name);
}