annotate lwasm/input.c @ 370:6b33faa21a0a

Debugging output and bugfixing pass 0
author lost@starbug
date Tue, 20 Apr 2010 21:59:58 -0600
parents 7166254491ed
children 38b50ce6967a
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
356
7166254491ed Finished pseudo ops
lost@starbug
parents: 332
diff changeset
216 FILE *input_open_standalone(asmstate_t *as, char *s)
7166254491ed Finished pseudo ops
lost@starbug
parents: 332
diff changeset
217 {
7166254491ed Finished pseudo ops
lost@starbug
parents: 332
diff changeset
218 char *s2;
7166254491ed Finished pseudo ops
lost@starbug
parents: 332
diff changeset
219 FILE *fp;
7166254491ed Finished pseudo ops
lost@starbug
parents: 332
diff changeset
220 char *p, *p2;
7166254491ed Finished pseudo ops
lost@starbug
parents: 332
diff changeset
221
7166254491ed Finished pseudo ops
lost@starbug
parents: 332
diff changeset
222 /* first check for absolute path and if so, skip path */
7166254491ed Finished pseudo ops
lost@starbug
parents: 332
diff changeset
223 if (*s == '/')
7166254491ed Finished pseudo ops
lost@starbug
parents: 332
diff changeset
224 {
7166254491ed Finished pseudo ops
lost@starbug
parents: 332
diff changeset
225 /* absolute path */
7166254491ed Finished pseudo ops
lost@starbug
parents: 332
diff changeset
226 fp = fopen(s, "rb");
7166254491ed Finished pseudo ops
lost@starbug
parents: 332
diff changeset
227 if (!fp)
7166254491ed Finished pseudo ops
lost@starbug
parents: 332
diff changeset
228 {
7166254491ed Finished pseudo ops
lost@starbug
parents: 332
diff changeset
229 return NULL;
7166254491ed Finished pseudo ops
lost@starbug
parents: 332
diff changeset
230 }
7166254491ed Finished pseudo ops
lost@starbug
parents: 332
diff changeset
231 return fp;
7166254491ed Finished pseudo ops
lost@starbug
parents: 332
diff changeset
232 }
7166254491ed Finished pseudo ops
lost@starbug
parents: 332
diff changeset
233
7166254491ed Finished pseudo ops
lost@starbug
parents: 332
diff changeset
234 /* relative path, check relative to "current file" directory */
7166254491ed Finished pseudo ops
lost@starbug
parents: 332
diff changeset
235 p = lw_stack_top(as -> file_dir);
7166254491ed Finished pseudo ops
lost@starbug
parents: 332
diff changeset
236 0 == asprintf(&p2, "%s/%s", p, s);
7166254491ed Finished pseudo ops
lost@starbug
parents: 332
diff changeset
237 fp = fopen(p2, "rb");
7166254491ed Finished pseudo ops
lost@starbug
parents: 332
diff changeset
238 if (fp)
7166254491ed Finished pseudo ops
lost@starbug
parents: 332
diff changeset
239 {
7166254491ed Finished pseudo ops
lost@starbug
parents: 332
diff changeset
240 lw_free(p2);
7166254491ed Finished pseudo ops
lost@starbug
parents: 332
diff changeset
241 return fp;
7166254491ed Finished pseudo ops
lost@starbug
parents: 332
diff changeset
242 }
7166254491ed Finished pseudo ops
lost@starbug
parents: 332
diff changeset
243 lw_free(p2);
7166254491ed Finished pseudo ops
lost@starbug
parents: 332
diff changeset
244
7166254491ed Finished pseudo ops
lost@starbug
parents: 332
diff changeset
245 /* now check relative to entries in the search path */
7166254491ed Finished pseudo ops
lost@starbug
parents: 332
diff changeset
246 lw_stringlist_reset(as -> include_list);
7166254491ed Finished pseudo ops
lost@starbug
parents: 332
diff changeset
247 while (p = lw_stringlist_current(as -> include_list))
7166254491ed Finished pseudo ops
lost@starbug
parents: 332
diff changeset
248 {
7166254491ed Finished pseudo ops
lost@starbug
parents: 332
diff changeset
249 0 == asprintf(&p2, "%s/%s", p, s);
7166254491ed Finished pseudo ops
lost@starbug
parents: 332
diff changeset
250 fp = fopen(p2, "rb");
7166254491ed Finished pseudo ops
lost@starbug
parents: 332
diff changeset
251 if (fp)
7166254491ed Finished pseudo ops
lost@starbug
parents: 332
diff changeset
252 {
7166254491ed Finished pseudo ops
lost@starbug
parents: 332
diff changeset
253 lw_free(p2);
7166254491ed Finished pseudo ops
lost@starbug
parents: 332
diff changeset
254 return fp;
7166254491ed Finished pseudo ops
lost@starbug
parents: 332
diff changeset
255 }
7166254491ed Finished pseudo ops
lost@starbug
parents: 332
diff changeset
256 lw_free(p2);
7166254491ed Finished pseudo ops
lost@starbug
parents: 332
diff changeset
257 lw_stringlist_next(as -> include_list);
7166254491ed Finished pseudo ops
lost@starbug
parents: 332
diff changeset
258 }
7166254491ed Finished pseudo ops
lost@starbug
parents: 332
diff changeset
259
7166254491ed Finished pseudo ops
lost@starbug
parents: 332
diff changeset
260 return NULL;
7166254491ed Finished pseudo ops
lost@starbug
parents: 332
diff changeset
261 }
7166254491ed Finished pseudo ops
lost@starbug
parents: 332
diff changeset
262
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
263 char *input_readline(asmstate_t *as)
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
264 {
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
265 char *s;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
266 char linebuff[2049];
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
267 int lbloc;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
268 int eol = 0;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
269
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
270 /* if no file is open, open one */
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
271 nextfile:
330
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
272 if (!IS) {
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
273 s = lw_stringlist_current(as -> input_files);
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
274 if (!s)
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
275 return NULL;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
276 lw_stringlist_next(as -> input_files);
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
277 input_open(as, s);
328
591d01b343b9 checkpoint - input layer
lost
parents:
diff changeset
278 }
591d01b343b9 checkpoint - input layer
lost
parents:
diff changeset
279
330
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
280 switch (IS -> type)
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
281 {
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
282 case input_type_file:
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
283 case input_type_include:
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
284 /* read from a file */
332
67224d8d1024 Basic input layer works
lost
parents: 330
diff changeset
285 lbloc = 0;
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
286 for (;;)
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 int c, c2;
330
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
289 c = fgetc(IS -> data);
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
290 if (c == EOF)
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 if (lbloc == 0)
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
293 {
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
294 struct input_stack *t;
330
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
295 fclose(IS -> data);
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
296 lw_free(lw_stack_pop(as -> file_dir));
330
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 }
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
303 linebuff[lbloc] = '\0';
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
304 eol = 1;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
305 }
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
306 else if (c == '\r')
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
307 {
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
308 linebuff[lbloc] = '\0';
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
309 eol = 1;
330
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
310 c2 = fgetc(IS -> data);
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
311 if (c2 == EOF)
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
312 c = EOF;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
313 else if (c2 != '\n')
330
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
314 ungetc(c2, IS -> data);
329
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 == '\n')
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 c2 = fgetc(IS -> data);
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
321 if (c2 == EOF)
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
322 c = EOF;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
323 else if (c2 != '\r')
330
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
324 ungetc(c2, IS -> data);
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
325 }
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
326 else
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
327 {
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
328 if (lbloc < 2048)
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
329 linebuff[lbloc++] = c;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
330 }
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
331 if (eol)
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
332 {
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
333 s = lw_strdup(linebuff);
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
334 return s;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
335 }
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
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
338 case input_type_string:
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
339 /* read from a string */
330
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
340 if (((char *)(IS -> data))[IS -> data2] == '\0')
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
341 {
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
342 struct input_stack *t;
330
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
343 lw_free(IS -> data);
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
344 lw_free(IS -> filespec);
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
345 t = IS -> next;
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
346 lw_free(IS);
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
347 as -> input_data = t;
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
348 goto nextfile;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
349 }
330
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
350 s = (char *)(IS -> data);
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
351 lbloc = 0;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
352 for (;;)
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
353 {
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
354 int c;
330
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
355 c = s[IS -> data2];
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
356 if (c)
330
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
357 IS -> data2++;
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
358 if (c == '\0')
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
359 {
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
360 linebuff[lbloc] = '\0';
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
361 eol = 1;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
362 }
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
363 else if (c == '\r')
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
364 {
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
365 linebuff[lbloc] = '\0';
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
366 eol = 1;
330
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
367 if (s[IS -> data2] == '\n')
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
368 IS -> data2++;
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
369 }
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
370 else if (c == '\n')
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
371 {
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
372 linebuff[lbloc] = '\0';
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
373 eol = 1;
330
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
374 if (s[IS -> data2] == '\r')
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
375 IS -> data2++;
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
376 }
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
377 else
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
378 {
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
379 if (lbloc < 2048)
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
380 linebuff[lbloc++] = c;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
381 }
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
382 if (eol)
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
383 {
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
384 s = lw_strdup(linebuff);
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
385 return s;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
386 }
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
387 }
328
591d01b343b9 checkpoint - input layer
lost
parents:
diff changeset
388
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
389 default:
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
390 lw_error("Problem reading from unknown input type");
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
391 }
328
591d01b343b9 checkpoint - input layer
lost
parents:
diff changeset
392 }
330
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
393
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
394 char *input_curspec(asmstate_t *as)
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
395 {
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
396 if (IS)
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
397 return IS -> filespec;
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
398 return NULL;
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
399 }