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>
52 table_open(char *file_name
,
60 /* create a file descriptor */
63 file
->nr_fields
= nr_fields
;
64 file
->nr_model_fields
= nr_model_fields
;
66 /* save the file name */
67 file
->file_name
= (char*)zalloc(strlen(file_name
) + 1);
68 ASSERT(file
->file_name
!= NULL
);
69 strcpy(file
->file_name
, file_name
);
72 fd
= open(file
->file_name
, O_RDONLY
, 0);
75 /* determine the size */
76 if (fstat(fd
, &stat_buf
) < 0) {
77 perror("table_open.fstat");
80 file
->size
= stat_buf
.st_size
;
82 /* allocate this much memory */
83 file
->buffer
= (char*)zalloc(file
->size
+1);
84 if(file
->buffer
== NULL
) {
85 perror("table_open.calloc.file->size+1");
88 file
->pos
= file
->buffer
;
91 if (read(fd
, file
->buffer
, file
->size
) < file
->size
) {
92 perror("table_open.read");
95 file
->buffer
[file
->size
] = '\0';
104 table_entry_read(table
*file
)
109 /* skip comments/blanks */
111 /* leading white space */
112 while (*file
->pos
!= '\0'
113 && *file
->pos
!= '\n'
114 && isspace(*file
->pos
))
117 if (*file
->pos
== '#') {
120 } while (*file
->pos
!= '\0' && *file
->pos
!= '\n');
123 if (*file
->pos
== '\n') {
130 if (*file
->pos
== '\0')
133 /* create this new entry */
134 entry
= (table_entry
*)zalloc(sizeof(table_entry
)
135 + (file
->nr_fields
+ 1) * sizeof(char*));
136 ASSERT(entry
!= NULL
);
137 entry
->file_name
= file
->file_name
;
138 entry
->nr_fields
= file
->nr_fields
;
140 /* break the line into its colon delimitered fields */
141 for (field
= 0; field
< file
->nr_fields
-1; field
++) {
142 entry
->fields
[field
] = file
->pos
;
143 while(*file
->pos
&& *file
->pos
!= ':' && *file
->pos
!= '\n')
145 if (*file
->pos
== ':') {
151 /* any trailing stuff not the last field */
152 ASSERT(field
== file
->nr_fields
-1);
153 entry
->fields
[field
] = file
->pos
;
154 while (*file
->pos
&& *file
->pos
!= '\n') {
157 if (*file
->pos
== '\n') {
162 entry
->line_nr
= file
->line_nr
;
164 /* if following lines begin with a star, add them to the model
166 while ((file
->nr_model_fields
> 0) && (*file
->pos
== '*')) {
167 table_model_entry
*model
= (table_model_entry
*)zalloc(sizeof(table_model_entry
)
168 + (file
->nr_model_fields
+ 1) * sizeof(char*));
169 if (entry
->model_last
)
170 entry
->model_last
->next
= model
;
172 entry
->model_first
= model
;
173 entry
->model_last
= model
;
175 /* break the line into its colon delimitered fields */
177 for (field
= 0; field
< file
->nr_model_fields
-1; field
++) {
178 model
->fields
[field
] = file
->pos
;
179 while(*file
->pos
&& *file
->pos
!= ':' && *file
->pos
!= '\n')
181 if (*file
->pos
== ':') {
187 /* any trailing stuff not the last field */
188 ASSERT(field
== file
->nr_model_fields
-1);
189 model
->fields
[field
] = file
->pos
;
190 while (*file
->pos
&& *file
->pos
!= '\n') {
193 if (*file
->pos
== '\n') {
199 model
->line_nr
= file
->line_nr
;
202 /* if following lines are tab indented, put in the annex */
203 if (*file
->pos
== '\t') {
204 entry
->annex
= file
->pos
;
208 } while (*file
->pos
!= '\0' && *file
->pos
!= '\n');
209 if (*file
->pos
== '\n') {
210 char *save_pos
= ++file
->pos
;
213 /* Allow tab indented to have blank lines */
214 while (*save_pos
== '\n') {
218 if (*save_pos
== '\t') {
219 file
->pos
= save_pos
;
220 file
->line_nr
+= extra_lines
;
223 } while (*file
->pos
!= '\0' && *file
->pos
== '\t');
224 if (file
->pos
[-1] == '\n')
225 file
->pos
[-1] = '\0';
237 dump_table_entry(table_entry
*entry
,
240 printf("(table_entry*)%p\n", entry
);
247 dumpf(indent
, "(fields");
248 for (field
= 0; field
< entry
->nr_fields
; field
++) {
249 printf("%c%s", sep
, entry
->fields
[field
]);
254 dumpf(indent
, "(line_nr %d)\n", entry
->line_nr
);
256 dumpf(indent
, "(file_name %s)\n", entry
->file_name
);
258 dumpf(indent
, "(annex\n%s\n", entry
->annex
);
259 dumpf(indent
, " )\n");
266 table_entry_lf_c_line_nr(lf
*file
,
269 lf_print_c_line_nr(file
, entry
->line_nr
, entry
->file_name
);
This page took 0.035401 seconds and 4 git commands to generate.