1 /* This file is part of the program psim.
3 Copyright (C) 1994-1995,1997 Andrew Cagney <cagney@highland.com.au>
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.
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.
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.
22 #include <sys/types.h>
41 typedef struct _open_table open_table
;
57 current_line (open_table
*file
)
59 line_ref
*entry
= ZALLOC (line_ref
);
60 *entry
= file
->pseudo_line
;
65 new_table_entry (open_table
*file
,
66 table_entry_type type
)
69 entry
= ZALLOC (table_entry
);
70 entry
->file
= file
->root
;
71 entry
->line
= current_line (file
);
77 set_nr_table_entry_fields (table_entry
*entry
,
80 entry
->field
= NZALLOC (char*, nr_fields
+ 1);
81 entry
->nr_fields
= nr_fields
;
86 table_push (table
*root
,
88 table_include
*includes
,
89 const char *file_name
)
95 table_include
*include
= &dummy
;
97 /* dummy up a search of this directory */
98 dummy
.next
= includes
;
101 /* create a file descriptor */
102 file
= ZALLOC (open_table
);
109 file
->parent
= root
->current
;
110 root
->current
= file
;
114 /* save the file name */
115 char *dup_name
= NZALLOC (char, strlen (include
->dir
) + strlen (file_name
) + 2);
116 if (dup_name
== NULL
)
121 if (include
->dir
[0] != '\0')
123 strcat (dup_name
, include
->dir
);
124 strcat (dup_name
, "/");
126 strcat (dup_name
, file_name
);
127 file
->real_line
.file_name
= dup_name
;
128 file
->pseudo_line
.file_name
= dup_name
;
129 printf ("Trying `%s'\n", dup_name
);
131 fd
= open (dup_name
, O_RDONLY
, 0);
134 /* zfree (dup_name); */
135 if (include
->next
== NULL
)
138 error (line
, "Problem opening file `%s'\n", file_name
);
142 include
= include
->next
;
146 /* determine the size */
147 if (fstat (fd
, &stat_buf
) < 0) {
151 file
->size
= stat_buf
.st_size
;
153 /* allocate this much memory */
154 file
->buffer
= (char*) zalloc (file
->size
+ 1);
155 if (file
->buffer
== NULL
)
160 file
->pos
= file
->buffer
;
163 if (read (fd
, file
->buffer
, file
->size
) < file
->size
) {
167 file
->buffer
[file
->size
] = '\0';
169 /* set the initial line numbering */
170 file
->real_line
.line_nr
= 1; /* specifies current line */
171 file
->pseudo_line
.line_nr
= 1; /* specifies current line */
178 table_open (const char *file_name
)
182 /* create a file descriptor */
183 root
= ZALLOC (table
);
190 table_push (root
, NULL
, NULL
, file_name
);
195 skip_spaces (char *chp
)
209 back_spaces (char *start
, char *chp
)
214 || !isspace (chp
[-1]))
221 skip_digits (char *chp
)
234 skip_to_separator (char *chp
,
239 char *sep
= separators
;
253 skip_to_null (char *chp
)
255 return skip_to_separator (chp
, "");
260 skip_to_nl (char * chp
)
262 return skip_to_separator (chp
, "\n");
267 next_line (open_table
*file
)
269 file
->pos
= skip_to_nl (file
->pos
);
270 if (*file
->pos
== '0')
271 error (&file
->pseudo_line
, "Missing <nl> at end of line\n");
274 file
->real_line
.line_nr
+= 1;
275 file
->pseudo_line
.line_nr
+= 1;
280 table_read (table
*root
)
282 open_table
*file
= root
->current
;
283 table_entry
*entry
= NULL
;
288 while (*file
->pos
== '\0')
290 if (file
->parent
!= NULL
)
293 root
->current
= file
;
300 if (*file
->pos
== '{')
303 next_line (file
); /* discard leading brace */
304 entry
= new_table_entry (file
, table_code_entry
);
306 /* determine how many lines are involved - look for <nl> "}" */
309 while (*file
->pos
!= '}')
314 set_nr_table_entry_fields (entry
, nr_lines
);
316 /* now enter each line */
319 for (line_nr
= 0; line_nr
< entry
->nr_fields
; line_nr
++)
321 if (strncmp (chp
, " ", 2) == 0)
322 entry
->field
[line_nr
] = chp
+ 2;
324 entry
->field
[line_nr
] = chp
;
325 chp
= skip_to_null (chp
) + 1;
327 /* skip trailing brace */
328 ASSERT (*file
->pos
== '}');
335 if (*file
->pos
== '\t')
337 char *chp
= file
->pos
;
338 entry
= new_table_entry (file
, table_code_entry
);
339 /* determine how many lines are involved - look for <nl> !<tab> */
342 int nr_blank_lines
= 0;
345 if (*file
->pos
== '\t')
347 nr_lines
= nr_lines
+ nr_blank_lines
+ 1;
353 file
->pos
= skip_spaces (file
->pos
);
354 if (*file
->pos
!= '\n')
360 set_nr_table_entry_fields (entry
, nr_lines
);
362 /* now enter each line */
365 for (line_nr
= 0; line_nr
< entry
->nr_fields
; line_nr
++)
368 entry
->field
[line_nr
] = chp
+ 1;
370 entry
->field
[line_nr
] = ""; /* blank */
371 chp
= skip_to_null (chp
) + 1;
378 if (file
->pos
[0] == '#')
380 char *chp
= skip_spaces (file
->pos
+ 1);
382 /* cpp line-nr directive - # <line-nr> "<file>" */
384 && *skip_digits (chp
) == ' '
385 && *skip_spaces (skip_digits (chp
)) == '"')
390 /* parse the number */
391 line_nr
= atoi(file
->pos
) - 1;
392 /* skip to the file name */
393 while (file
->pos
[0] != '0'
394 && file
->pos
[0] != '"'
395 && file
->pos
[0] != '\0')
397 if (file
->pos
[0] != '"')
398 error (&file
->real_line
, "Missing opening quote in cpp directive\n");
399 /* parse the file name */
401 file_name
= file
->pos
;
402 while (file
->pos
[0] != '"'
403 && file
->pos
[0] != '\0')
405 if (file
->pos
[0] != '"')
406 error (&file
->real_line
, "Missing closing quote in cpp directive\n");
409 file
->pos
= skip_to_nl (file
->pos
);
410 if (file
->pos
[0] != '\n')
411 error (&file
->real_line
, "Missing newline in cpp directive\n");
412 file
->pseudo_line
.file_name
= file_name
;
413 file
->pseudo_line
.line_nr
= line_nr
;
418 /* #define and #undef - not implemented yet */
420 /* Old style # comment */
425 /* blank line or end-of-file? */
426 file
->pos
= skip_spaces (file
->pos
);
427 if (*file
->pos
== '\0')
428 error (&file
->pseudo_line
, "Missing <nl> at end of file\n");
429 if (*file
->pos
== '\n')
435 /* comment - leading // or # - skip */
436 if ((file
->pos
[0] == '/' && file
->pos
[1] == '/')
437 || (file
->pos
[0] == '#'))
445 char *chp
= file
->pos
;
446 entry
= new_table_entry (file
, table_colon_entry
);
448 /* figure out how many fields */
454 tmpch
= skip_to_separator (tmpch
, "\\:");
457 /* eat the escaped character */
459 while (cp
[1] != '\0')
467 else if (*tmpch
!= ':')
476 set_nr_table_entry_fields (entry
, nr_fields
);
481 for (field_nr
= 0; field_nr
< entry
->nr_fields
; field_nr
++)
483 chp
= skip_spaces (chp
);
484 entry
->field
[field_nr
] = chp
;
485 chp
= skip_to_null (chp
);
486 *back_spaces (entry
->field
[field_nr
], chp
) = '\0';
495 ASSERT (entry
== NULL
|| entry
->field
[entry
->nr_fields
] == NULL
);
500 table_print_code (lf
*file
,
506 field_nr
< entry
->nr_fields
;
509 char *chp
= entry
->field
[field_nr
];
510 int in_bit_field
= 0;
512 lf_indent_suppress(file
);
520 nr
+= lf_putchr(file
, '_');
522 else if (in_bit_field
&& chp
[0] == ':')
524 nr
+= lf_putchr(file
, '_');
526 else if (in_bit_field
&& *chp
== '}')
528 nr
+= lf_putchr(file
, '_');
533 nr
+= lf_putchr(file
, *chp
);
539 line_ref line
= *entry
->line
;
540 line
.line_nr
+= field_nr
;
541 error (&line
, "Bit field brace miss match\n");
543 nr
+= lf_putchr(file
, '\n');
550 dump_line_ref (lf
*file
,
552 const line_ref
*line
,
555 lf_printf (file
, "%s(line_ref*) 0x%lx", prefix
, (long) line
);
558 lf_indent (file
, +1);
559 lf_printf (file
, "\n(line_nr %d)", line
->line_nr
);
560 lf_printf (file
, "\n(file_name %s)", line
->file_name
);
561 lf_indent (file
, -1);
563 lf_printf (file
, "%s", suffix
);
568 table_entry_type_to_str (table_entry_type type
)
572 case table_code_entry
: return "code-entry";
573 case table_colon_entry
: return "colon-entry";
579 dump_table_entry(lf
*file
,
581 const table_entry
*entry
,
584 lf_printf (file
, "%s(table_entry*) 0x%lx", prefix
, (long) entry
);
588 lf_indent (file
, +1);
589 dump_line_ref (file
, "\n(line ", entry
->line
, ")");
590 lf_printf (file
, "\n(type %s)", table_entry_type_to_str (entry
->type
));
591 lf_printf (file
, "\n(nr_fields %d)", entry
->nr_fields
);
592 lf_printf (file
, "\n(fields");
593 lf_indent (file
, +1);
594 for (field
= 0; field
< entry
->nr_fields
; field
++)
595 lf_printf (file
, "\n\"%s\"", entry
->field
[field
]);
596 lf_indent (file
, -1);
597 lf_printf (file
, ")");
598 lf_indent (file
, -1);
600 lf_printf (file
, "%s", suffix
);
606 main(int argc
, char **argv
)
615 printf("Usage: table <file>\n");
619 t
= table_open (argv
[1]);
620 l
= lf_open ("-", "stdout", lf_omit_references
, lf_is_text
, "tmp-table");
626 entry
= table_read (t
);
628 sprintf (line
, "(%d ", line_nr
);
629 dump_table_entry (l
, line
, entry
, ")\n");
631 while (entry
!= NULL
);
This page took 0.064962 seconds and 4 git commands to generate.