annotate lwasm/input.c @ 386:af5f2c51db76

Bugfixing on includes
author lost@starbug
date Sun, 16 May 2010 13:03:17 -0600
parents 38b50ce6967a
children e3567a9f0d54
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);
384
38b50ce6967a Made --list and --depend work
lost@starbug
parents: 356
diff changeset
70 as -> includelist = lw_stack_create(lw_free);
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
71 lw_stringlist_reset(as -> input_files);
330
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
72 while (IS)
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
73 {
330
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
74 t = IS;
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
75 as -> input_data = IS -> next;
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
76 lw_free(t);
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
77 }
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
78 }
328
591d01b343b9 checkpoint - input layer
lost
parents:
diff changeset
79
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
80 void input_pushpath(asmstate_t *as, char *fn)
328
591d01b343b9 checkpoint - input layer
lost
parents:
diff changeset
81 {
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
82 /* take apart fn into path and filename then push the path */
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
83 /* onto the current file path stack */
384
38b50ce6967a Made --list and --depend work
lost@starbug
parents: 356
diff changeset
84
38b50ce6967a Made --list and --depend work
lost@starbug
parents: 356
diff changeset
85 /* also add it to the list of files included */
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
86 char *dn, *dp;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
87 int o;
384
38b50ce6967a Made --list and --depend work
lost@starbug
parents: 356
diff changeset
88
38b50ce6967a Made --list and --depend work
lost@starbug
parents: 356
diff changeset
89 dn = lw_strdup(fn);
38b50ce6967a Made --list and --depend work
lost@starbug
parents: 356
diff changeset
90 lw_stack_push(as -> includelist, dn);
38b50ce6967a Made --list and --depend work
lost@starbug
parents: 356
diff changeset
91
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
92 dn = lw_strdup(fn);
332
67224d8d1024 Basic input layer works
lost
parents: 330
diff changeset
93 dp = dn + strlen(dn);
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
94
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
95 while (--dp != dn)
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
96 {
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
97 if (*dp == '/')
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
98 break;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
99 }
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
100 if (*dp == '/')
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
101 *dp = '\0';
328
591d01b343b9 checkpoint - input layer
lost
parents:
diff changeset
102
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
103 if (dp == dn)
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 lw_free(dn);
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
106 dn = lw_strdup(".");
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
107 lw_stack_push(as -> file_dir, dn);
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
108 return;
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 dp = lw_strdup(dn);
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
111 lw_free(dn);
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
112 lw_stack_push(as -> file_dir, dp);
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
113 }
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
114
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
115 void input_openstring(asmstate_t *as, char *s, char *str)
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
116 {
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
117 struct input_stack *t;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
118
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
119 t = lw_alloc(sizeof(struct input_stack));
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
120 t -> filespec = lw_strdup(s);
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
121
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
122 t -> type = input_type_string;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
123 t -> data = lw_strdup(str);
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
124 t -> data2 = 0;
330
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
125 t -> next = IS;
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
126 as -> input_data = t;
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
127 t -> filespec = lw_strdup(s);
328
591d01b343b9 checkpoint - input layer
lost
parents:
diff changeset
128 }
591d01b343b9 checkpoint - input layer
lost
parents:
diff changeset
129
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
130 void input_open(asmstate_t *as, char *s)
328
591d01b343b9 checkpoint - input layer
lost
parents:
diff changeset
131 {
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
132 struct input_stack *t;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
133 char *s2;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
134 char *p, *p2;
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 t = lw_alloc(sizeof(struct input_stack));
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
137 t -> filespec = lw_strdup(s);
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
138
332
67224d8d1024 Basic input layer works
lost
parents: 330
diff changeset
139 for (s2 = s; *s2 && (*s2 != ':'); s2++)
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
140 /* do nothing */ ;
332
67224d8d1024 Basic input layer works
lost
parents: 330
diff changeset
141 if (!*s2)
329
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 t -> type = input_type_file;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
144 }
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
145 else
328
591d01b343b9 checkpoint - input layer
lost
parents:
diff changeset
146 {
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
147 char *ts;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
148
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
149 ts = lw_strndup(s, s2 - s);
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
150 s = s2 + 1;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
151 if (!strcmp(ts, "include"))
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
152 t -> type = input_type_include;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
153 else if (!strcmp(ts, "file"))
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
154 t -> type = input_type_file;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
155 else
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
156 t -> type = input_type_error;
332
67224d8d1024 Basic input layer works
lost
parents: 330
diff changeset
157
67224d8d1024 Basic input layer works
lost
parents: 330
diff changeset
158 lw_free(ts);
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
159 }
332
67224d8d1024 Basic input layer works
lost
parents: 330
diff changeset
160 t -> next = as -> input_data;
330
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
161 as -> input_data = t;
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
162
330
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
163 switch (IS -> type)
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
164 {
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
165 case input_type_include:
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
166 /* first check for absolute path and if so, skip path */
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
167 if (*s == '/')
328
591d01b343b9 checkpoint - input layer
lost
parents:
diff changeset
168 {
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
169 /* absolute path */
330
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
170 IS -> data = fopen(s, "rb");
386
af5f2c51db76 Bugfixing on includes
lost@starbug
parents: 384
diff changeset
171 debug_message(as, 1, "Opening (abs) %s", s);
330
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
172 if (!IS -> data)
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
173 {
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
174 lw_error("Cannot open file '%s': %s", s, strerror(errno));
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, s);
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
177 break;
328
591d01b343b9 checkpoint - input layer
lost
parents:
diff changeset
178 }
591d01b343b9 checkpoint - input layer
lost
parents:
diff changeset
179
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
180 /* relative path, check relative to "current file" directory */
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
181 p = lw_stack_top(as -> file_dir);
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
182 0 == asprintf(&p2, "%s/%s", p, s);
386
af5f2c51db76 Bugfixing on includes
lost@starbug
parents: 384
diff changeset
183 debug_message(as, 1, "Open: (cd) %s\n", p2);
330
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
184 IS -> data = fopen(p2, "rb");
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
185 if (IS -> data)
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
186 {
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
187 input_pushpath(as, p2);
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
188 lw_free(p2);
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
189 break;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
190 }
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
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
193 /* now check relative to entries in the search path */
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
194 lw_stringlist_reset(as -> include_list);
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
195 while (p = lw_stringlist_current(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 0 == asprintf(&p2, "%s/%s", p, s);
386
af5f2c51db76 Bugfixing on includes
lost@starbug
parents: 384
diff changeset
198 debug_message(as, 1, "Open (sp): %s\n", p2);
330
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
199 IS -> data = fopen(p2, "rb");
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
200 if (IS -> data)
329
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 input_pushpath(as, p2);
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
203 lw_free(p2);
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
204 return;
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 lw_free(p2);
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
207 lw_stringlist_next(as -> include_list);
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 lw_error("Cannot open include file '%s': %s", s, strerror(errno));
332
67224d8d1024 Basic input layer works
lost
parents: 330
diff changeset
210
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
211 case input_type_file:
386
af5f2c51db76 Bugfixing on includes
lost@starbug
parents: 384
diff changeset
212 debug_message(as, 1, "Opening (reg): %s\n", s);
330
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
213 IS -> data = fopen(s, "rb");
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
214
330
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
215 if (!IS -> data)
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
216 {
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
217 lw_error("Cannot open file '%s': %s", s, strerror(errno));
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
218 }
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
219 input_pushpath(as, s);
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
220 return;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
221 }
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
222
332
67224d8d1024 Basic input layer works
lost
parents: 330
diff changeset
223 lw_error("Cannot figure out how to open '%s'.", t -> filespec);
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
224 }
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
225
356
7166254491ed Finished pseudo ops
lost@starbug
parents: 332
diff changeset
226 FILE *input_open_standalone(asmstate_t *as, char *s)
7166254491ed Finished pseudo ops
lost@starbug
parents: 332
diff changeset
227 {
7166254491ed Finished pseudo ops
lost@starbug
parents: 332
diff changeset
228 char *s2;
7166254491ed Finished pseudo ops
lost@starbug
parents: 332
diff changeset
229 FILE *fp;
7166254491ed Finished pseudo ops
lost@starbug
parents: 332
diff changeset
230 char *p, *p2;
7166254491ed Finished pseudo ops
lost@starbug
parents: 332
diff changeset
231
7166254491ed Finished pseudo ops
lost@starbug
parents: 332
diff changeset
232 /* first check for absolute path and if so, skip path */
7166254491ed Finished pseudo ops
lost@starbug
parents: 332
diff changeset
233 if (*s == '/')
7166254491ed Finished pseudo ops
lost@starbug
parents: 332
diff changeset
234 {
7166254491ed Finished pseudo ops
lost@starbug
parents: 332
diff changeset
235 /* absolute path */
386
af5f2c51db76 Bugfixing on includes
lost@starbug
parents: 384
diff changeset
236 debug_message(as, 2, "Open file (st abs) %s", s);
356
7166254491ed Finished pseudo ops
lost@starbug
parents: 332
diff changeset
237 fp = fopen(s, "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 return NULL;
7166254491ed Finished pseudo ops
lost@starbug
parents: 332
diff changeset
241 }
7166254491ed Finished pseudo ops
lost@starbug
parents: 332
diff changeset
242 return fp;
7166254491ed Finished pseudo ops
lost@starbug
parents: 332
diff changeset
243 }
7166254491ed Finished pseudo ops
lost@starbug
parents: 332
diff changeset
244
7166254491ed Finished pseudo ops
lost@starbug
parents: 332
diff changeset
245 /* relative path, check relative to "current file" directory */
7166254491ed Finished pseudo ops
lost@starbug
parents: 332
diff changeset
246 p = lw_stack_top(as -> file_dir);
7166254491ed Finished pseudo ops
lost@starbug
parents: 332
diff changeset
247 0 == asprintf(&p2, "%s/%s", p, s);
386
af5f2c51db76 Bugfixing on includes
lost@starbug
parents: 384
diff changeset
248 debug_message(as, 2, "Open file (st cd) %s", p2);
356
7166254491ed Finished pseudo ops
lost@starbug
parents: 332
diff changeset
249 fp = fopen(p2, "rb");
7166254491ed Finished pseudo ops
lost@starbug
parents: 332
diff changeset
250 if (fp)
7166254491ed Finished pseudo ops
lost@starbug
parents: 332
diff changeset
251 {
7166254491ed Finished pseudo ops
lost@starbug
parents: 332
diff changeset
252 lw_free(p2);
7166254491ed Finished pseudo ops
lost@starbug
parents: 332
diff changeset
253 return fp;
7166254491ed Finished pseudo ops
lost@starbug
parents: 332
diff changeset
254 }
7166254491ed Finished pseudo ops
lost@starbug
parents: 332
diff changeset
255 lw_free(p2);
7166254491ed Finished pseudo ops
lost@starbug
parents: 332
diff changeset
256
7166254491ed Finished pseudo ops
lost@starbug
parents: 332
diff changeset
257 /* now check relative to entries in the search path */
7166254491ed Finished pseudo ops
lost@starbug
parents: 332
diff changeset
258 lw_stringlist_reset(as -> include_list);
7166254491ed Finished pseudo ops
lost@starbug
parents: 332
diff changeset
259 while (p = lw_stringlist_current(as -> include_list))
7166254491ed Finished pseudo ops
lost@starbug
parents: 332
diff changeset
260 {
7166254491ed Finished pseudo ops
lost@starbug
parents: 332
diff changeset
261 0 == asprintf(&p2, "%s/%s", p, s);
386
af5f2c51db76 Bugfixing on includes
lost@starbug
parents: 384
diff changeset
262 debug_message(as, 2, "Open file (st ip) %s", p2);
356
7166254491ed Finished pseudo ops
lost@starbug
parents: 332
diff changeset
263 fp = fopen(p2, "rb");
7166254491ed Finished pseudo ops
lost@starbug
parents: 332
diff changeset
264 if (fp)
7166254491ed Finished pseudo ops
lost@starbug
parents: 332
diff changeset
265 {
7166254491ed Finished pseudo ops
lost@starbug
parents: 332
diff changeset
266 lw_free(p2);
7166254491ed Finished pseudo ops
lost@starbug
parents: 332
diff changeset
267 return fp;
7166254491ed Finished pseudo ops
lost@starbug
parents: 332
diff changeset
268 }
7166254491ed Finished pseudo ops
lost@starbug
parents: 332
diff changeset
269 lw_free(p2);
7166254491ed Finished pseudo ops
lost@starbug
parents: 332
diff changeset
270 lw_stringlist_next(as -> include_list);
7166254491ed Finished pseudo ops
lost@starbug
parents: 332
diff changeset
271 }
7166254491ed Finished pseudo ops
lost@starbug
parents: 332
diff changeset
272
7166254491ed Finished pseudo ops
lost@starbug
parents: 332
diff changeset
273 return NULL;
7166254491ed Finished pseudo ops
lost@starbug
parents: 332
diff changeset
274 }
7166254491ed Finished pseudo ops
lost@starbug
parents: 332
diff changeset
275
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
276 char *input_readline(asmstate_t *as)
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 char *s;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
279 char linebuff[2049];
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
280 int lbloc;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
281 int eol = 0;
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 /* if no file is open, open one */
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
284 nextfile:
330
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
285 if (!IS) {
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
286 s = lw_stringlist_current(as -> input_files);
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
287 if (!s)
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
288 return NULL;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
289 lw_stringlist_next(as -> input_files);
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
290 input_open(as, s);
328
591d01b343b9 checkpoint - input layer
lost
parents:
diff changeset
291 }
591d01b343b9 checkpoint - input layer
lost
parents:
diff changeset
292
330
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
293 switch (IS -> type)
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 case input_type_file:
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
296 case input_type_include:
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
297 /* read from a file */
332
67224d8d1024 Basic input layer works
lost
parents: 330
diff changeset
298 lbloc = 0;
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
299 for (;;)
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
300 {
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
301 int c, c2;
330
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
302 c = fgetc(IS -> data);
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
303 if (c == EOF)
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
304 {
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
305 if (lbloc == 0)
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 struct input_stack *t;
330
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
308 fclose(IS -> data);
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
309 lw_free(lw_stack_pop(as -> file_dir));
330
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
310 lw_free(IS -> filespec);
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
311 t = IS -> next;
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
312 lw_free(IS);
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
313 as -> input_data = t;
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
314 goto nextfile;
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 linebuff[lbloc] = '\0';
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
317 eol = 1;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
318 }
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
319 else if (c == '\r')
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
320 {
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
321 linebuff[lbloc] = '\0';
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
322 eol = 1;
330
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
323 c2 = fgetc(IS -> data);
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
324 if (c2 == EOF)
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
325 c = EOF;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
326 else if (c2 != '\n')
330
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
327 ungetc(c2, IS -> data);
329
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 else if (c == '\n')
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 linebuff[lbloc] = '\0';
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
332 eol = 1;
330
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
333 c2 = fgetc(IS -> data);
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
334 if (c2 == EOF)
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
335 c = EOF;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
336 else if (c2 != '\r')
330
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
337 ungetc(c2, IS -> data);
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
338 }
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
339 else
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
340 {
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
341 if (lbloc < 2048)
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
342 linebuff[lbloc++] = c;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
343 }
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
344 if (eol)
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
345 {
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
346 s = lw_strdup(linebuff);
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
347 return s;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
348 }
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
349 }
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
350
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
351 case input_type_string:
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
352 /* read from a string */
330
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
353 if (((char *)(IS -> data))[IS -> data2] == '\0')
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
354 {
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
355 struct input_stack *t;
330
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
356 lw_free(IS -> data);
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
357 lw_free(IS -> filespec);
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
358 t = IS -> next;
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
359 lw_free(IS);
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
360 as -> input_data = t;
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
361 goto nextfile;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
362 }
330
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
363 s = (char *)(IS -> data);
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
364 lbloc = 0;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
365 for (;;)
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
366 {
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
367 int c;
330
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
368 c = s[IS -> data2];
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
369 if (c)
330
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
370 IS -> data2++;
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
371 if (c == '\0')
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
372 {
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
373 linebuff[lbloc] = '\0';
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
374 eol = 1;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
375 }
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
376 else if (c == '\r')
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
377 {
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
378 linebuff[lbloc] = '\0';
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
379 eol = 1;
330
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
380 if (s[IS -> data2] == '\n')
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
381 IS -> data2++;
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
382 }
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
383 else if (c == '\n')
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
384 {
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
385 linebuff[lbloc] = '\0';
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
386 eol = 1;
330
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
387 if (s[IS -> data2] == '\r')
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
388 IS -> data2++;
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
389 }
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
390 else
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
391 {
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
392 if (lbloc < 2048)
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
393 linebuff[lbloc++] = c;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
394 }
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
395 if (eol)
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
396 {
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
397 s = lw_strdup(linebuff);
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
398 return s;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
399 }
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
400 }
328
591d01b343b9 checkpoint - input layer
lost
parents:
diff changeset
401
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
402 default:
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
403 lw_error("Problem reading from unknown input type");
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
404 }
328
591d01b343b9 checkpoint - input layer
lost
parents:
diff changeset
405 }
330
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
406
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
407 char *input_curspec(asmstate_t *as)
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
408 {
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
409 if (IS)
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
410 return IS -> filespec;
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
411 return NULL;
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
412 }