annotate lwasm/input.c @ 340:1a6fc6ebb31c

Checkpoint
author lost
date Fri, 19 Mar 2010 02:54:43 +0000
parents 67224d8d1024
children 7166254491ed
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
330
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
61 #define IS ((struct input_stack *)(as -> input_data))
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;
330
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
66
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
67 if (as -> file_dir)
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
68 lw_stack_destroy(as -> file_dir);
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
69 as -> file_dir = lw_stack_create(lw_free);
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
70 lw_stringlist_reset(as -> input_files);
330
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
71 while (IS)
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
72 {
330
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
73 t = IS;
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
74 as -> input_data = IS -> next;
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
75 lw_free(t);
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
76 }
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
77 }
328
591d01b343b9 checkpoint - input layer
lost
parents:
diff changeset
78
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
79 void input_pushpath(asmstate_t *as, char *fn)
328
591d01b343b9 checkpoint - input layer
lost
parents:
diff changeset
80 {
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
81 /* take apart fn into path and filename then push the path */
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
82 /* onto the current file path stack */
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
83 char *dn, *dp;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
84 int o;
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 dn = lw_strdup(fn);
332
67224d8d1024 Basic input layer works
lost
parents: 330
diff changeset
87 dp = dn + strlen(dn);
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
88
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
89 while (--dp != dn)
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 break;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
93 }
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
94 if (*dp == '/')
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
95 *dp = '\0';
328
591d01b343b9 checkpoint - input layer
lost
parents:
diff changeset
96
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
97 if (dp == dn)
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
98 {
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
99 lw_free(dn);
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
100 dn = lw_strdup(".");
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
101 lw_stack_push(as -> file_dir, dn);
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
102 return;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
103 }
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
104 dp = lw_strdup(dn);
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
105 lw_free(dn);
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
106 lw_stack_push(as -> file_dir, dp);
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
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
109 void input_openstring(asmstate_t *as, char *s, char *str)
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
110 {
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
111 struct input_stack *t;
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 = lw_alloc(sizeof(struct input_stack));
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
114 t -> filespec = lw_strdup(s);
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
115
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
116 t -> type = input_type_string;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
117 t -> data = lw_strdup(str);
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
118 t -> data2 = 0;
330
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
119 t -> next = IS;
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
120 as -> input_data = t;
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
121 t -> filespec = lw_strdup(s);
328
591d01b343b9 checkpoint - input layer
lost
parents:
diff changeset
122 }
591d01b343b9 checkpoint - input layer
lost
parents:
diff changeset
123
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
124 void input_open(asmstate_t *as, char *s)
328
591d01b343b9 checkpoint - input layer
lost
parents:
diff changeset
125 {
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
126 struct input_stack *t;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
127 char *s2;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
128 char *p, *p2;
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 t = lw_alloc(sizeof(struct input_stack));
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
131 t -> filespec = lw_strdup(s);
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
132
332
67224d8d1024 Basic input layer works
lost
parents: 330
diff changeset
133 for (s2 = s; *s2 && (*s2 != ':'); s2++)
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
134 /* do nothing */ ;
332
67224d8d1024 Basic input layer works
lost
parents: 330
diff changeset
135 if (!*s2)
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
136 {
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
137 t -> type = input_type_file;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
138 }
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
139 else
328
591d01b343b9 checkpoint - input layer
lost
parents:
diff changeset
140 {
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
141 char *ts;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
142
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
143 ts = lw_strndup(s, s2 - s);
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
144 s = s2 + 1;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
145 if (!strcmp(ts, "include"))
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
146 t -> type = input_type_include;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
147 else if (!strcmp(ts, "file"))
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
148 t -> type = input_type_file;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
149 else
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
150 t -> type = input_type_error;
332
67224d8d1024 Basic input layer works
lost
parents: 330
diff changeset
151
67224d8d1024 Basic input layer works
lost
parents: 330
diff changeset
152 lw_free(ts);
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
153 }
332
67224d8d1024 Basic input layer works
lost
parents: 330
diff changeset
154 t -> next = as -> input_data;
330
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
155 as -> input_data = t;
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
156
330
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
157 switch (IS -> type)
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
158 {
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
159 case input_type_include:
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
160 /* first check for absolute path and if so, skip path */
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
161 if (*s == '/')
328
591d01b343b9 checkpoint - input layer
lost
parents:
diff changeset
162 {
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
163 /* absolute path */
330
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
164 IS -> data = fopen(s, "rb");
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
165 if (!IS -> data)
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
166 {
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
167 lw_error("Cannot open file '%s': %s", s, strerror(errno));
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
168 }
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
169 input_pushpath(as, s);
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
170 break;
328
591d01b343b9 checkpoint - input layer
lost
parents:
diff changeset
171 }
591d01b343b9 checkpoint - input layer
lost
parents:
diff changeset
172
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
173 /* relative path, check relative to "current file" directory */
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
174 p = lw_stack_top(as -> file_dir);
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
175 0 == asprintf(&p2, "%s/%s", p, s);
330
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
176 IS -> data = fopen(p2, "rb");
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
177 if (IS -> data)
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
178 {
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
179 input_pushpath(as, p2);
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 break;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
182 }
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
183 lw_free(p2);
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
184
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
185 /* now check relative to entries in the search path */
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
186 lw_stringlist_reset(as -> include_list);
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
187 while (p = lw_stringlist_current(as -> include_list))
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
188 {
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
189 0 == asprintf(&p2, "%s/%s", p, s);
330
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
190 IS -> data = fopen(p2, "rb");
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
191 if (IS -> data)
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
192 {
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
193 input_pushpath(as, p2);
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 return;
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_free(p2);
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
198 lw_stringlist_next(as -> include_list);
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
199 }
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
200 lw_error("Cannot open include file '%s': %s", s, strerror(errno));
332
67224d8d1024 Basic input layer works
lost
parents: 330
diff changeset
201
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
202 case input_type_file:
330
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
203 IS -> data = fopen(s, "rb");
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
204
330
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
205 if (!IS -> data)
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
206 {
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
207 lw_error("Cannot open file '%s': %s", s, strerror(errno));
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 input_pushpath(as, s);
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
210 return;
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
332
67224d8d1024 Basic input layer works
lost
parents: 330
diff changeset
213 lw_error("Cannot figure out how to open '%s'.", t -> filespec);
329
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
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
216 char *input_readline(asmstate_t *as)
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
217 {
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
218 char *s;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
219 char linebuff[2049];
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
220 int lbloc;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
221 int eol = 0;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
222
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
223 /* if no file is open, open one */
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
224 nextfile:
330
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
225 if (!IS) {
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
226 s = lw_stringlist_current(as -> input_files);
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
227 if (!s)
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
228 return NULL;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
229 lw_stringlist_next(as -> input_files);
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
230 input_open(as, s);
328
591d01b343b9 checkpoint - input layer
lost
parents:
diff changeset
231 }
591d01b343b9 checkpoint - input layer
lost
parents:
diff changeset
232
330
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
233 switch (IS -> type)
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
234 {
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
235 case input_type_file:
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
236 case input_type_include:
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
237 /* read from a file */
332
67224d8d1024 Basic input layer works
lost
parents: 330
diff changeset
238 lbloc = 0;
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
239 for (;;)
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
240 {
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
241 int c, c2;
330
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
242 c = fgetc(IS -> data);
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
243 if (c == EOF)
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
244 {
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
245 if (lbloc == 0)
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
246 {
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
247 struct input_stack *t;
330
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
248 fclose(IS -> data);
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
249 lw_free(lw_stack_pop(as -> file_dir));
330
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
250 lw_free(IS -> filespec);
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
251 t = IS -> next;
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
252 lw_free(IS);
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
253 as -> input_data = t;
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
254 goto nextfile;
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 linebuff[lbloc] = '\0';
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
257 eol = 1;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
258 }
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
259 else if (c == '\r')
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
260 {
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
261 linebuff[lbloc] = '\0';
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
262 eol = 1;
330
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
263 c2 = fgetc(IS -> data);
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
264 if (c2 == EOF)
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
265 c = EOF;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
266 else if (c2 != '\n')
330
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
267 ungetc(c2, IS -> data);
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
268 }
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
269 else if (c == '\n')
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
270 {
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
271 linebuff[lbloc] = '\0';
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
272 eol = 1;
330
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
273 c2 = fgetc(IS -> data);
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
274 if (c2 == EOF)
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
275 c = EOF;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
276 else if (c2 != '\r')
330
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
277 ungetc(c2, IS -> data);
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
278 }
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
279 else
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 (lbloc < 2048)
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
282 linebuff[lbloc++] = c;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
283 }
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
284 if (eol)
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 s = lw_strdup(linebuff);
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
287 return s;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
288 }
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
289 }
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
290
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
291 case input_type_string:
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
292 /* read from a string */
330
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
293 if (((char *)(IS -> data))[IS -> data2] == '\0')
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
294 {
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
295 struct input_stack *t;
330
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
296 lw_free(IS -> data);
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
297 lw_free(IS -> filespec);
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
298 t = IS -> next;
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
299 lw_free(IS);
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
300 as -> input_data = t;
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
301 goto nextfile;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
302 }
330
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
303 s = (char *)(IS -> data);
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
304 lbloc = 0;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
305 for (;;)
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
306 {
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
307 int c;
330
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
308 c = s[IS -> data2];
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
309 if (c)
330
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
310 IS -> data2++;
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
311 if (c == '\0')
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 linebuff[lbloc] = '\0';
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
314 eol = 1;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
315 }
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
316 else if (c == '\r')
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
317 {
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
318 linebuff[lbloc] = '\0';
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
319 eol = 1;
330
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
320 if (s[IS -> data2] == '\n')
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
321 IS -> data2++;
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
322 }
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
323 else if (c == '\n')
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
324 {
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
325 linebuff[lbloc] = '\0';
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
326 eol = 1;
330
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
327 if (s[IS -> data2] == '\r')
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
328 IS -> data2++;
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
329 }
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
330 else
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 (lbloc < 2048)
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
333 linebuff[lbloc++] = c;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
334 }
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
335 if (eol)
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 s = lw_strdup(linebuff);
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
338 return s;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
339 }
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
340 }
328
591d01b343b9 checkpoint - input layer
lost
parents:
diff changeset
341
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
342 default:
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
343 lw_error("Problem reading from unknown input type");
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
344 }
328
591d01b343b9 checkpoint - input layer
lost
parents:
diff changeset
345 }
330
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
346
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
347 char *input_curspec(asmstate_t *as)
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
348 {
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
349 if (IS)
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
350 return IS -> filespec;
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
351 return NULL;
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
352 }