annotate lwasm/input.c @ 329:c15cca3ae6a2

Created first pass of input layer
author lost
date Sun, 28 Feb 2010 05:01:31 +0000
parents 591d01b343b9
children 81c005b82775
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
328
591d01b343b9 checkpoint - input layer
lost
parents:
diff changeset
1 /*
591d01b343b9 checkpoint - input layer
lost
parents:
diff changeset
2 input.c
591d01b343b9 checkpoint - input layer
lost
parents:
diff changeset
3
591d01b343b9 checkpoint - input layer
lost
parents:
diff changeset
4 Copyright © 2010 William Astle
591d01b343b9 checkpoint - input layer
lost
parents:
diff changeset
5
591d01b343b9 checkpoint - input layer
lost
parents:
diff changeset
6 This file is part of LWTOOLS.
591d01b343b9 checkpoint - input layer
lost
parents:
diff changeset
7
591d01b343b9 checkpoint - input layer
lost
parents:
diff changeset
8 LWTOOLS is free software: you can redistribute it and/or modify it under the
591d01b343b9 checkpoint - input layer
lost
parents:
diff changeset
9 terms of the GNU General Public License as published by the Free Software
591d01b343b9 checkpoint - input layer
lost
parents:
diff changeset
10 Foundation, either version 3 of the License, or (at your option) any later
591d01b343b9 checkpoint - input layer
lost
parents:
diff changeset
11 version.
591d01b343b9 checkpoint - input layer
lost
parents:
diff changeset
12
591d01b343b9 checkpoint - input layer
lost
parents:
diff changeset
13 This program is distributed in the hope that it will be useful, but WITHOUT
591d01b343b9 checkpoint - input layer
lost
parents:
diff changeset
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
591d01b343b9 checkpoint - input layer
lost
parents:
diff changeset
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
591d01b343b9 checkpoint - input layer
lost
parents:
diff changeset
16 more details.
591d01b343b9 checkpoint - input layer
lost
parents:
diff changeset
17
591d01b343b9 checkpoint - input layer
lost
parents:
diff changeset
18 You should have received a copy of the GNU General Public License along with
591d01b343b9 checkpoint - input layer
lost
parents:
diff changeset
19 this program. If not, see <http://www.gnu.org/licenses/>.
591d01b343b9 checkpoint - input layer
lost
parents:
diff changeset
20 */
591d01b343b9 checkpoint - input layer
lost
parents:
diff changeset
21
591d01b343b9 checkpoint - input layer
lost
parents:
diff changeset
22 #include <config.h>
591d01b343b9 checkpoint - input layer
lost
parents:
diff changeset
23
591d01b343b9 checkpoint - input layer
lost
parents:
diff changeset
24 /*
591d01b343b9 checkpoint - input layer
lost
parents:
diff changeset
25 This file is used to handle reading input files. It serves to encapsulate
591d01b343b9 checkpoint - input layer
lost
parents:
diff changeset
26 the entire input system to make porting to different media and systems
591d01b343b9 checkpoint - input layer
lost
parents:
diff changeset
27 less difficult.
591d01b343b9 checkpoint - input layer
lost
parents:
diff changeset
28 */
591d01b343b9 checkpoint - input layer
lost
parents:
diff changeset
29
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
30 #include <errno.h>
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
31 #include <stdio.h>
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
32 #include <string.h>
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
33
328
591d01b343b9 checkpoint - input layer
lost
parents:
diff changeset
34 #include <lw_alloc.h>
591d01b343b9 checkpoint - input layer
lost
parents:
diff changeset
35 #include <lw_stringlist.h>
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
36 #include <lw_string.h>
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
37 #include "lwasm.h"
328
591d01b343b9 checkpoint - input layer
lost
parents:
diff changeset
38
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
39 /*
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
40 Data type for storing input buffers
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
41 */
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
42
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
43 enum input_types_e
328
591d01b343b9 checkpoint - input layer
lost
parents:
diff changeset
44 {
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
45 input_type_file, // regular file, no search path
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
46 input_type_include, // include path, start from "local"
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
47 input_type_string, // input from a string
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
48
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
49 input_type_error // invalid input type
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
50 };
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
51
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
52 struct input_stack
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
53 {
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
54 struct input_stack *next;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
55 int type;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
56 void *data;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
57 int data2;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
58 char *filespec;
328
591d01b343b9 checkpoint - input layer
lost
parents:
diff changeset
59 };
591d01b343b9 checkpoint - input layer
lost
parents:
diff changeset
60
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
61 static struct input_stack *is = NULL;
328
591d01b343b9 checkpoint - input layer
lost
parents:
diff changeset
62
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
63 void input_init(asmstate_t *as)
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
64 {
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
65 struct input_stack *t;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
66
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
67 lw_stringlist_reset(as -> input_files);
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
68 while (is)
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
69 {
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
70 t = is;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
71 is = is -> next;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
72 lw_free(t);
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
73 }
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
74 }
328
591d01b343b9 checkpoint - input layer
lost
parents:
diff changeset
75
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
76 void input_pushpath(asmstate_t *as, char *fn)
328
591d01b343b9 checkpoint - input layer
lost
parents:
diff changeset
77 {
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
78 /* take apart fn into path and filename then push the path */
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
79 /* onto the current file path stack */
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
80 char *dn, *dp;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
81 int o;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
82
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
83 dn = lw_strdup(fn);
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
84 dp = dn + strlen(dp);
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
85
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
86 while (--dp != dn)
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
87 {
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
88 if (*dp == '/')
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
89 break;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
90 }
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
91 if (*dp == '/')
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
92 *dp = '\0';
328
591d01b343b9 checkpoint - input layer
lost
parents:
diff changeset
93
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
94 if (dp == dn)
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
95 {
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
96 lw_free(dn);
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
97 dn = lw_strdup(".");
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
98 lw_stack_push(as -> file_dir, dn);
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
99 return;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
100 }
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
101 dp = lw_strdup(dn);
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
102 lw_free(dn);
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
103 lw_stack_push(as -> file_dir, dp);
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
104 }
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
105
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
106 void input_openstring(asmstate_t *as, char *s, char *str)
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
107 {
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
108 struct input_stack *t;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
109
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
110 t = lw_alloc(sizeof(struct input_stack));
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
111 t -> filespec = lw_strdup(s);
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
112
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
113 t -> type = input_type_string;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
114 t -> data = lw_strdup(str);
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
115 t -> data2 = 0;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
116 t -> next = is;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
117 is = t;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
118 t -> filespec = lw_strdup(s);
328
591d01b343b9 checkpoint - input layer
lost
parents:
diff changeset
119 }
591d01b343b9 checkpoint - input layer
lost
parents:
diff changeset
120
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
121 void input_open(asmstate_t *as, char *s)
328
591d01b343b9 checkpoint - input layer
lost
parents:
diff changeset
122 {
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
123 struct input_stack *t;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
124 char *s2;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
125 char *p, *p2;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
126
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
127 t = lw_alloc(sizeof(struct input_stack));
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
128 t -> filespec = lw_strdup(s);
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
129
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
130 for (s2 = s; *s2 && *s2 != ':'; s2++)
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
131 /* do nothing */ ;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
132 if (!s2)
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
133 {
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
134 t -> type = input_type_file;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
135 }
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
136 else
328
591d01b343b9 checkpoint - input layer
lost
parents:
diff changeset
137 {
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
138 char *ts;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
139
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
140 ts = lw_strndup(s, s2 - s);
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
141 s = s2 + 1;
328
591d01b343b9 checkpoint - input layer
lost
parents:
diff changeset
142
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
143 if (!strcmp(ts, "include"))
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
144 t -> type = input_type_include;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
145 else if (!strcmp(ts, "file"))
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
146 t -> type = input_type_file;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
147 else
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
148 t -> type = input_type_error;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
149 }
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
150
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
151 t -> next = is;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
152 is = t;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
153
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
154 switch (is -> type)
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
155 {
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
156 case input_type_include:
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
157 /* first check for absolute path and if so, skip path */
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
158 if (*s == '/')
328
591d01b343b9 checkpoint - input layer
lost
parents:
diff changeset
159 {
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
160 /* absolute path */
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
161 is -> data = fopen(s, "rb");
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
162 if (!is -> data)
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
163 {
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
164 lw_error("Cannot open file '%s': %s", s, strerror(errno));
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
165 }
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
166 input_pushpath(as, s);
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
167 break;
328
591d01b343b9 checkpoint - input layer
lost
parents:
diff changeset
168 }
591d01b343b9 checkpoint - input layer
lost
parents:
diff changeset
169
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
170 /* relative path, check relative to "current file" directory */
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
171 p = lw_stack_top(as -> file_dir);
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
172 0 == asprintf(&p2, "%s/%s", p, s);
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
173 is -> data = fopen(p2, "rb");
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
174 if (is -> data)
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
175 {
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
176 input_pushpath(as, p2);
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
177 lw_free(p2);
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
178 break;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
179 }
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
180 lw_free(p2);
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
181
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
182 /* now check relative to entries in the search path */
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
183 lw_stringlist_reset(as -> include_list);
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
184 while (p = lw_stringlist_current(as -> include_list))
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
185 {
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
186 0 == asprintf(&p2, "%s/%s", p, s);
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
187 is -> data = fopen(p2, "rb");
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
188 if (is -> data)
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
189 {
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
190 input_pushpath(as, p2);
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
191 lw_free(p2);
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
192 return;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
193 }
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
194 lw_free(p2);
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
195 lw_stringlist_next(as -> include_list);
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
196 }
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
197 lw_error("Cannot open include file '%s': %s", s, strerror(errno));
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
198
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
199 case input_type_file:
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
200 is -> data = fopen(s, "rb");
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
201
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
202 if (!is -> data)
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
203 {
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
204 lw_error("Cannot open file '%s': %s", s, strerror(errno));
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
205 }
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
206 input_pushpath(as, s);
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
207 return;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
208 }
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
209
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
210 lw_error("Cannot figure out how to open '%s'.", s);
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
211 }
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
212
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
213 char *input_readline(asmstate_t *as)
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
214 {
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
215 char *s;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
216 char linebuff[2049];
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
217 int lbloc;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
218 int eol = 0;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
219
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
220 /* if no file is open, open one */
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
221 nextfile:
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
222 if (!is) {
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
223 s = lw_stringlist_current(as -> input_files);
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
224 if (!s)
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
225 return NULL;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
226 lw_stringlist_next(as -> input_files);
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
227 input_open(as, s);
328
591d01b343b9 checkpoint - input layer
lost
parents:
diff changeset
228 }
591d01b343b9 checkpoint - input layer
lost
parents:
diff changeset
229
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
230 switch (is -> type)
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
231 {
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
232 case input_type_file:
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
233 case input_type_include:
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
234 /* read from a file */
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
235 for (;;)
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
236 {
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
237 int c, c2;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
238 c = fgetc(is -> data);
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
239 lbloc = 0;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
240 if (c == EOF)
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
241 {
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
242 if (lbloc == 0)
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
243 {
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
244 struct input_stack *t;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
245 fclose(is -> data);
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
246 lw_free(lw_stack_pop(as -> file_dir));
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
247 lw_free(is -> filespec);
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
248 t = is -> next;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
249 lw_free(is);
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
250 is = t;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
251 goto nextfile;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
252 }
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
253 linebuff[lbloc] = '\0';
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
254 eol = 1;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
255 }
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
256 else if (c == '\r')
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
257 {
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
258 linebuff[lbloc] = '\0';
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
259 eol = 1;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
260 c2 = fgetc(is -> data);
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
261 if (c2 == EOF)
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
262 c = EOF;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
263 else if (c2 != '\n')
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
264 ungetc(c2, is -> data);
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
265 }
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
266 else if (c == '\n')
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
267 {
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
268 linebuff[lbloc] = '\0';
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
269 eol = 1;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
270 c2 = fgetc(is -> data);
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
271 if (c2 == EOF)
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
272 c = EOF;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
273 else if (c2 != '\r')
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
274 ungetc(c2, is -> data);
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
275 }
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
276 else
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
277 {
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
278 if (lbloc < 2048)
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
279 linebuff[lbloc++] = c;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
280 }
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
281 if (eol)
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
282 {
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
283 s = lw_strdup(linebuff);
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
284 return s;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
285 }
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
286 }
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
287
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
288 case input_type_string:
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
289 /* read from a string */
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
290 if (((char *)(is -> data))[is -> data2] == '\0')
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
291 {
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
292 struct input_stack *t;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
293 lw_free(is -> data);
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
294 lw_free(is -> filespec);
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
295 t = is -> next;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
296 lw_free(is);
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
297 is = t;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
298 goto nextfile;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
299 }
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
300 s = (char *)(is -> data);
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
301 lbloc = 0;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
302 for (;;)
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
303 {
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
304 int c;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
305 c = s[is -> data2];
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
306 if (c)
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
307 is -> data2++;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
308 if (c == '\0')
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
309 {
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
310 linebuff[lbloc] = '\0';
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
311 eol = 1;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
312 }
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
313 else if (c == '\r')
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
314 {
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
315 linebuff[lbloc] = '\0';
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
316 eol = 1;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
317 if (s[is -> data2] == '\n')
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
318 is -> data2++;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
319 }
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
320 else if (c == '\n')
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
321 {
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
322 linebuff[lbloc] = '\0';
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
323 eol = 1;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
324 if (s[is -> data2] == '\r')
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
325 is -> data2++;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
326 }
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
327 else
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
328 {
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
329 if (lbloc < 2048)
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
330 linebuff[lbloc++] = c;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
331 }
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
332 if (eol)
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
333 {
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
334 s = lw_strdup(linebuff);
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
335 return s;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
336 }
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
337 }
328
591d01b343b9 checkpoint - input layer
lost
parents:
diff changeset
338
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
339 default:
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
340 lw_error("Problem reading from unknown input type");
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
341 }
328
591d01b343b9 checkpoint - input layer
lost
parents:
diff changeset
342 }