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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
|
/* The IGEN simulator generator for GDB, the GNU Debugger.
Copyright 2002-2015 Free Software Foundation, Inc.
Contributed by Andrew Cagney.
This file is part of GDB.
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 3 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, see <http://www.gnu.org/licenses/>. */
#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;
line_ref pseudo_line;
line_ref real_line;
open_table *parent;
table *root;
};
struct _table
{
open_table *current;
};
static line_ref *
current_line (open_table * file)
{
line_ref *entry = ZALLOC (line_ref);
*entry = file->pseudo_line;
return entry;
}
static table_entry *
new_table_entry (open_table * file, table_entry_type type)
{
table_entry *entry;
entry = ZALLOC (table_entry);
entry->file = file->root;
entry->line = current_line (file);
entry->type = type;
return entry;
}
static void
set_nr_table_entry_fields (table_entry *entry, int nr_fields)
{
entry->field = NZALLOC (char *, nr_fields + 1);
entry->nr_fields = nr_fields;
}
void
table_push (table *root,
line_ref *line, table_include *includes, const char *file_name)
{
FILE *ff;
open_table *file;
table_include dummy;
table_include *include = &dummy;
/* dummy up a search of this directory */
dummy.next = includes;
dummy.dir = "";
/* create a file descriptor */
file = ZALLOC (open_table);
if (file == NULL)
{
perror (file_name);
exit (1);
}
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->real_line.file_name = dup_name;
file->pseudo_line.file_name = dup_name;
/* open the file */
ff = fopen (dup_name, "rb");
if (ff)
break;
/* free (dup_name); */
if (include->next == NULL)
{
if (line != NULL)
error (line, "Problem opening file `%s'\n", file_name);
perror (file_name);
exit (1);
}
include = include->next;
}
/* determine the size */
fseek (ff, 0, SEEK_END);
file->size = ftell (ff);
fseek (ff, 0, SEEK_SET);
/* allocate this much memory */
file->buffer = (char *) zalloc (file->size + 1);
if (file->buffer == NULL)
{
perror (file_name);
exit (1);
}
file->pos = file->buffer;
/* read it all in */
if (fread (file->buffer, 1, file->size, ff) < file->size)
{
perror (file_name);
exit (1);
}
file->buffer[file->size] = '\0';
/* set the initial line numbering */
file->real_line.line_nr = 1; /* specifies current line */
file->pseudo_line.line_nr = 1; /* specifies current line */
/* done */
fclose (ff);
}
table *
table_open (const char *file_name)
{
table *root;
/* create a file descriptor */
root = ZALLOC (table);
if (root == NULL)
{
perror (file_name);
exit (1);
}
table_push (root, NULL, NULL, file_name);
return root;
}
char *
skip_spaces (char *chp)
{
while (1)
{
if (*chp == '\0' || *chp == '\n' || !isspace (*chp))
return chp;
chp++;
}
}
char *
back_spaces (char *start, char *chp)
{
while (1)
{
if (chp <= start || !isspace (chp[-1]))
return chp;
chp--;
}
}
char *
skip_digits (char *chp)
{
while (1)
{
if (*chp == '\0' || *chp == '\n' || !isdigit (*chp))
return chp;
chp++;
}
}
char *
skip_to_separator (char *chp, char *separators)
{
while (1)
{
char *sep = separators;
while (1)
{
if (*chp == *sep)
return chp;
if (*sep == '\0')
break;
sep++;
}
chp++;
}
}
static char *
skip_to_null (char *chp)
{
return skip_to_separator (chp, "");
}
static char *
skip_to_nl (char *chp)
{
return skip_to_separator (chp, "\n");
}
static void
next_line (open_table * file)
{
file->pos = skip_to_nl (file->pos);
if (*file->pos == '0')
error (&file->pseudo_line, "Missing <nl> at end of line\n");
*file->pos = '\0';
file->pos += 1;
file->real_line.line_nr += 1;
file->pseudo_line.line_nr += 1;
}
extern table_entry *
table_read (table *root)
{
open_table *file = root->current;
table_entry *entry = NULL;
while (1)
{
/* end-of-file? */
while (*file->pos == '\0')
{
if (file->parent != NULL)
{
file = file->parent;
root->current = file;
}
else
return NULL;
}
/* code_block? */
if (*file->pos == '{')
{
char *chp;
next_line (file); /* discard leading brace */
entry = new_table_entry (file, table_code_entry);
chp = file->pos;
/* determine how many lines are involved - look for <nl> "}" */
{
int nr_lines = 0;
while (*file->pos != '}')
{
next_line (file);
nr_lines++;
}
set_nr_table_entry_fields (entry, nr_lines);
}
/* now enter each line */
{
int line_nr;
for (line_nr = 0; line_nr < entry->nr_fields; line_nr++)
{
if (strncmp (chp, " ", 2) == 0)
entry->field[line_nr] = chp + 2;
else
entry->field[line_nr] = chp;
chp = skip_to_null (chp) + 1;
}
/* skip trailing brace */
ASSERT (*file->pos == '}');
next_line (file);
}
break;
}
/* tab block? */
if (*file->pos == '\t')
{
char *chp = file->pos;
entry = new_table_entry (file, table_code_entry);
/* determine how many lines are involved - look for <nl> !<tab> */
{
int nr_lines = 0;
int nr_blank_lines = 0;
while (1)
{
if (*file->pos == '\t')
{
nr_lines = nr_lines + nr_blank_lines + 1;
nr_blank_lines = 0;
next_line (file);
}
else
{
file->pos = skip_spaces (file->pos);
if (*file->pos != '\n')
break;
nr_blank_lines++;
next_line (file);
}
}
set_nr_table_entry_fields (entry, nr_lines);
}
/* now enter each line */
{
int line_nr;
for (line_nr = 0; line_nr < entry->nr_fields; line_nr++)
{
if (*chp == '\t')
entry->field[line_nr] = chp + 1;
else
entry->field[line_nr] = ""; /* blank */
chp = skip_to_null (chp) + 1;
}
}
break;
}
/* cpp directive? */
if (file->pos[0] == '#')
{
char *chp = skip_spaces (file->pos + 1);
/* cpp line-nr directive - # <line-nr> "<file>" */
if (isdigit (*chp)
&& *skip_digits (chp) == ' '
&& *skip_spaces (skip_digits (chp)) == '"')
{
int line_nr;
char *file_name;
file->pos = chp;
/* parse the number */
line_nr = atoi (file->pos) - 1;
/* skip to the file name */
while (file->pos[0] != '0'
&& file->pos[0] != '"' && file->pos[0] != '\0')
file->pos++;
if (file->pos[0] != '"')
error (&file->real_line,
"Missing opening quote in cpp directive\n");
/* parse the file name */
file->pos++;
file_name = file->pos;
while (file->pos[0] != '"' && file->pos[0] != '\0')
file->pos++;
if (file->pos[0] != '"')
error (&file->real_line,
"Missing closing quote in cpp directive\n");
file->pos[0] = '\0';
file->pos++;
file->pos = skip_to_nl (file->pos);
if (file->pos[0] != '\n')
error (&file->real_line,
"Missing newline in cpp directive\n");
file->pseudo_line.file_name = file_name;
file->pseudo_line.line_nr = line_nr;
next_line (file);
continue;
}
/* #define and #undef - not implemented yet */
/* Old style # comment */
next_line (file);
continue;
}
/* blank line or end-of-file? */
file->pos = skip_spaces (file->pos);
if (*file->pos == '\0')
error (&file->pseudo_line, "Missing <nl> at end of file\n");
if (*file->pos == '\n')
{
next_line (file);
continue;
}
/* comment - leading // or # - skip */
if ((file->pos[0] == '/' && file->pos[1] == '/')
|| (file->pos[0] == '#'))
{
next_line (file);
continue;
}
/* colon field */
{
char *chp = file->pos;
entry = new_table_entry (file, table_colon_entry);
next_line (file);
/* figure out how many fields */
{
int nr_fields = 1;
char *tmpch = chp;
while (1)
{
tmpch = skip_to_separator (tmpch, "\\:");
if (*tmpch == '\\')
{
/* eat the escaped character */
char *cp = tmpch;
while (cp[1] != '\0')
{
cp[0] = cp[1];
cp++;
}
cp[0] = '\0';
tmpch++;
}
else if (*tmpch != ':')
break;
else
{
*tmpch = '\0';
tmpch++;
nr_fields++;
}
}
set_nr_table_entry_fields (entry, nr_fields);
}
/* now parse them */
{
int field_nr;
for (field_nr = 0; field_nr < entry->nr_fields; field_nr++)
{
chp = skip_spaces (chp);
entry->field[field_nr] = chp;
chp = skip_to_null (chp);
*back_spaces (entry->field[field_nr], chp) = '\0';
chp++;
}
}
break;
}
}
ASSERT (entry == NULL || entry->field[entry->nr_fields] == NULL);
return entry;
}
extern void
table_print_code (lf *file, table_entry *entry)
{
int field_nr;
int nr = 0;
for (field_nr = 0; field_nr < entry->nr_fields; field_nr++)
{
char *chp = entry->field[field_nr];
int in_bit_field = 0;
if (*chp == '#')
lf_indent_suppress (file);
while (*chp != '\0')
{
if (chp[0] == '{' && !isspace (chp[1]) && chp[1] != '\0')
{
in_bit_field = 1;
nr += lf_putchr (file, '_');
}
else if (in_bit_field && chp[0] == ':')
{
nr += lf_putchr (file, '_');
}
else if (in_bit_field && *chp == '}')
{
nr += lf_putchr (file, '_');
in_bit_field = 0;
}
else
{
nr += lf_putchr (file, *chp);
}
chp++;
}
if (in_bit_field)
{
line_ref line = *entry->line;
line.line_nr += field_nr;
error (&line, "Bit field brace miss match\n");
}
nr += lf_putchr (file, '\n');
}
}
void
dump_line_ref (lf *file, char *prefix, const line_ref *line, char *suffix)
{
lf_printf (file, "%s(line_ref*) 0x%lx", prefix, (long) line);
if (line != NULL)
{
lf_indent (file, +1);
lf_printf (file, "\n(line_nr %d)", line->line_nr);
lf_printf (file, "\n(file_name %s)", line->file_name);
lf_indent (file, -1);
}
lf_printf (file, "%s", suffix);
}
static const char *
table_entry_type_to_str (table_entry_type type)
{
switch (type)
{
case table_code_entry:
return "code-entry";
case table_colon_entry:
return "colon-entry";
}
return "*invalid*";
}
void
dump_table_entry (lf *file,
char *prefix, const table_entry *entry, char *suffix)
{
lf_printf (file, "%s(table_entry*) 0x%lx", prefix, (long) entry);
if (entry != NULL)
{
int field;
lf_indent (file, +1);
dump_line_ref (file, "\n(line ", entry->line, ")");
lf_printf (file, "\n(type %s)", table_entry_type_to_str (entry->type));
lf_printf (file, "\n(nr_fields %d)", entry->nr_fields);
lf_printf (file, "\n(fields");
lf_indent (file, +1);
for (field = 0; field < entry->nr_fields; field++)
lf_printf (file, "\n\"%s\"", entry->field[field]);
lf_indent (file, -1);
lf_printf (file, ")");
lf_indent (file, -1);
}
lf_printf (file, "%s", suffix);
}
#ifdef MAIN
int
main (int argc, char **argv)
{
table *t;
table_entry *entry;
lf *l;
int line_nr;
if (argc != 2)
{
printf ("Usage: table <file>\n");
exit (1);
}
t = table_open (argv[1]);
l = lf_open ("-", "stdout", lf_omit_references, lf_is_text, "tmp-table");
line_nr = 0;
do
{
char line[10];
entry = table_read (t);
line_nr++;
sprintf (line, "(%d ", line_nr);
dump_table_entry (l, line, entry, ")\n");
}
while (entry != NULL);
return 0;
}
#endif
|