1 /* This file is part of the program psim.
3 Copyright (C) 1994-1995, 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>
51 table_open(char *file_name
,
58 /* create a file descriptor */
61 file
->nr_fields
= nr_fields
;
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
);
69 fd
= open(file
->file_name
, O_RDONLY
, 0);
72 /* determine the size */
73 if (fstat(fd
, &stat_buf
) < 0) {
74 perror("table_open.fstat");
77 file
->size
= stat_buf
.st_size
;
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");
85 file
->pos
= file
->buffer
;
88 if (read(fd
, file
->buffer
, file
->size
) < file
->size
) {
89 perror("table_open.read");
92 file
->buffer
[file
->size
] = '\0';
101 table_entry_read(table
*file
)
106 /* skip comments/blanks */
108 /* leading white space */
109 while (*file
->pos
!= '\0'
110 && *file
->pos
!= '\n'
111 && isspace(*file
->pos
))
114 if (*file
->pos
== '#') {
117 } while (*file
->pos
!= '\0' && *file
->pos
!= '\n');
120 if (*file
->pos
== '\n') {
127 if (*file
->pos
== '\0')
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
;
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')
142 if (*file
->pos
== ':') {
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') {
154 if (*file
->pos
== '\n') {
159 entry
->line_nr
= file
->line_nr
;
161 /* if following lines tab indented, put in the annex */
162 if (*file
->pos
== '\t') {
163 entry
->annex
= file
->pos
;
167 } while (*file
->pos
!= '\0' && *file
->pos
!= '\n');
168 if (*file
->pos
== '\n') {
172 } while (*file
->pos
!= '\0' && *file
->pos
== '\t');
173 if (file
->pos
[-1] == '\n')
174 file
->pos
[-1] = '\0';
186 dump_table_entry(table_entry
*entry
,
189 printf("(table_entry*)%p\n", entry
);
196 dumpf(indent
, "(fields");
197 for (field
= 0; field
< entry
->nr_fields
; field
++) {
198 printf("%c%s", sep
, entry
->fields
[field
]);
203 dumpf(indent
, "(line_nr %d)\n", entry
->line_nr
);
205 dumpf(indent
, "(file_name %s)\n", entry
->file_name
);
207 dumpf(indent
, "(annex\n%s\n", entry
->annex
);
208 dumpf(indent
, " )\n");
215 table_entry_lf_c_line_nr(lf
*file
,
218 lf_print_c_line_nr(file
, entry
->line_nr
, entry
->file_name
);