annotate lwasm/input.c @ 392:e3567a9f0d54

Fixed problem with include path handling
author lost@l-w.ca
date Wed, 21 Jul 2010 22:21:35 -0600
parents af5f2c51db76
children
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);
392
e3567a9f0d54 Fixed problem with include path handling
lost@l-w.ca
parents: 386
diff changeset
177 return;
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);
392
e3567a9f0d54 Fixed problem with include path handling
lost@l-w.ca
parents: 386
diff changeset
189 return;
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
190 }
392
e3567a9f0d54 Fixed problem with include path handling
lost@l-w.ca
parents: 386
diff changeset
191 debug_message(as, 2, "Failed to open: (cd) %s (%s)\n", p2, strerror(errno));
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
192 lw_free(p2);
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
193
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
194 /* now check relative to entries in the search path */
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
195 lw_stringlist_reset(as -> include_list);
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
196 while (p = lw_stringlist_current(as -> include_list))
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
197 {
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
198 0 == asprintf(&p2, "%s/%s", p, s);
386
af5f2c51db76 Bugfixing on includes
lost@starbug
parents: 384
diff changeset
199 debug_message(as, 1, "Open (sp): %s\n", p2);
330
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
200 IS -> data = fopen(p2, "rb");
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
201 if (IS -> data)
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
202 {
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
203 input_pushpath(as, p2);
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
204 lw_free(p2);
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
205 return;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
206 }
392
e3567a9f0d54 Fixed problem with include path handling
lost@l-w.ca
parents: 386
diff changeset
207 debug_message(as, 2, "Failed to open: (sp) %s (%s)\n", p2, strerror(errno));
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
208 lw_free(p2);
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
209 lw_stringlist_next(as -> include_list);
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
210 }
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
211 lw_error("Cannot open include file '%s': %s", s, strerror(errno));
392
e3567a9f0d54 Fixed problem with include path handling
lost@l-w.ca
parents: 386
diff changeset
212 break;
332
67224d8d1024 Basic input layer works
lost
parents: 330
diff changeset
213
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
214 case input_type_file:
386
af5f2c51db76 Bugfixing on includes
lost@starbug
parents: 384
diff changeset
215 debug_message(as, 1, "Opening (reg): %s\n", s);
330
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
216 IS -> data = fopen(s, "rb");
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
217
330
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
218 if (!IS -> data)
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
219 {
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
220 lw_error("Cannot open file '%s': %s", s, strerror(errno));
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 input_pushpath(as, s);
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
223 return;
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
332
67224d8d1024 Basic input layer works
lost
parents: 330
diff changeset
226 lw_error("Cannot figure out how to open '%s'.", t -> filespec);
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
227 }
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
228
356
7166254491ed Finished pseudo ops
lost@starbug
parents: 332
diff changeset
229 FILE *input_open_standalone(asmstate_t *as, char *s)
7166254491ed Finished pseudo ops
lost@starbug
parents: 332
diff changeset
230 {
7166254491ed Finished pseudo ops
lost@starbug
parents: 332
diff changeset
231 char *s2;
7166254491ed Finished pseudo ops
lost@starbug
parents: 332
diff changeset
232 FILE *fp;
7166254491ed Finished pseudo ops
lost@starbug
parents: 332
diff changeset
233 char *p, *p2;
7166254491ed Finished pseudo ops
lost@starbug
parents: 332
diff changeset
234
7166254491ed Finished pseudo ops
lost@starbug
parents: 332
diff changeset
235 /* first check for absolute path and if so, skip path */
7166254491ed Finished pseudo ops
lost@starbug
parents: 332
diff changeset
236 if (*s == '/')
7166254491ed Finished pseudo ops
lost@starbug
parents: 332
diff changeset
237 {
7166254491ed Finished pseudo ops
lost@starbug
parents: 332
diff changeset
238 /* absolute path */
386
af5f2c51db76 Bugfixing on includes
lost@starbug
parents: 384
diff changeset
239 debug_message(as, 2, "Open file (st abs) %s", s);
356
7166254491ed Finished pseudo ops
lost@starbug
parents: 332
diff changeset
240 fp = fopen(s, "rb");
7166254491ed Finished pseudo ops
lost@starbug
parents: 332
diff changeset
241 if (!fp)
7166254491ed Finished pseudo ops
lost@starbug
parents: 332
diff changeset
242 {
7166254491ed Finished pseudo ops
lost@starbug
parents: 332
diff changeset
243 return NULL;
7166254491ed Finished pseudo ops
lost@starbug
parents: 332
diff changeset
244 }
7166254491ed Finished pseudo ops
lost@starbug
parents: 332
diff changeset
245 return fp;
7166254491ed Finished pseudo ops
lost@starbug
parents: 332
diff changeset
246 }
7166254491ed Finished pseudo ops
lost@starbug
parents: 332
diff changeset
247
7166254491ed Finished pseudo ops
lost@starbug
parents: 332
diff changeset
248 /* relative path, check relative to "current file" directory */
7166254491ed Finished pseudo ops
lost@starbug
parents: 332
diff changeset
249 p = lw_stack_top(as -> file_dir);
7166254491ed Finished pseudo ops
lost@starbug
parents: 332
diff changeset
250 0 == asprintf(&p2, "%s/%s", p, s);
386
af5f2c51db76 Bugfixing on includes
lost@starbug
parents: 384
diff changeset
251 debug_message(as, 2, "Open file (st cd) %s", p2);
356
7166254491ed Finished pseudo ops
lost@starbug
parents: 332
diff changeset
252 fp = fopen(p2, "rb");
7166254491ed Finished pseudo ops
lost@starbug
parents: 332
diff changeset
253 if (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 return fp;
7166254491ed Finished pseudo ops
lost@starbug
parents: 332
diff changeset
257 }
7166254491ed Finished pseudo ops
lost@starbug
parents: 332
diff changeset
258 lw_free(p2);
7166254491ed Finished pseudo ops
lost@starbug
parents: 332
diff changeset
259
7166254491ed Finished pseudo ops
lost@starbug
parents: 332
diff changeset
260 /* now check relative to entries in the search path */
7166254491ed Finished pseudo ops
lost@starbug
parents: 332
diff changeset
261 lw_stringlist_reset(as -> include_list);
7166254491ed Finished pseudo ops
lost@starbug
parents: 332
diff changeset
262 while (p = lw_stringlist_current(as -> include_list))
7166254491ed Finished pseudo ops
lost@starbug
parents: 332
diff changeset
263 {
7166254491ed Finished pseudo ops
lost@starbug
parents: 332
diff changeset
264 0 == asprintf(&p2, "%s/%s", p, s);
386
af5f2c51db76 Bugfixing on includes
lost@starbug
parents: 384
diff changeset
265 debug_message(as, 2, "Open file (st ip) %s", p2);
356
7166254491ed Finished pseudo ops
lost@starbug
parents: 332
diff changeset
266 fp = fopen(p2, "rb");
7166254491ed Finished pseudo ops
lost@starbug
parents: 332
diff changeset
267 if (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 return fp;
7166254491ed Finished pseudo ops
lost@starbug
parents: 332
diff changeset
271 }
7166254491ed Finished pseudo ops
lost@starbug
parents: 332
diff changeset
272 lw_free(p2);
7166254491ed Finished pseudo ops
lost@starbug
parents: 332
diff changeset
273 lw_stringlist_next(as -> include_list);
7166254491ed Finished pseudo ops
lost@starbug
parents: 332
diff changeset
274 }
7166254491ed Finished pseudo ops
lost@starbug
parents: 332
diff changeset
275
7166254491ed Finished pseudo ops
lost@starbug
parents: 332
diff changeset
276 return NULL;
7166254491ed Finished pseudo ops
lost@starbug
parents: 332
diff changeset
277 }
7166254491ed Finished pseudo ops
lost@starbug
parents: 332
diff changeset
278
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
279 char *input_readline(asmstate_t *as)
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 char *s;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
282 char linebuff[2049];
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
283 int lbloc;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
284 int eol = 0;
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 /* if no file is open, open one */
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
287 nextfile:
330
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
288 if (!IS) {
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
289 s = lw_stringlist_current(as -> input_files);
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
290 if (!s)
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
291 return NULL;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
292 lw_stringlist_next(as -> input_files);
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
293 input_open(as, s);
328
591d01b343b9 checkpoint - input layer
lost
parents:
diff changeset
294 }
591d01b343b9 checkpoint - input layer
lost
parents:
diff changeset
295
330
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
296 switch (IS -> type)
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
297 {
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
298 case input_type_file:
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
299 case input_type_include:
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
300 /* read from a file */
332
67224d8d1024 Basic input layer works
lost
parents: 330
diff changeset
301 lbloc = 0;
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
302 for (;;)
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
303 {
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
304 int c, c2;
330
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
305 c = fgetc(IS -> data);
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
306 if (c == EOF)
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 if (lbloc == 0)
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
309 {
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
310 struct input_stack *t;
330
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
311 fclose(IS -> data);
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
312 lw_free(lw_stack_pop(as -> file_dir));
330
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
313 lw_free(IS -> filespec);
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
314 t = IS -> next;
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
315 lw_free(IS);
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
316 as -> input_data = t;
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
317 goto nextfile;
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 linebuff[lbloc] = '\0';
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
320 eol = 1;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
321 }
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
322 else if (c == '\r')
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
323 {
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
324 linebuff[lbloc] = '\0';
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
325 eol = 1;
330
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
326 c2 = fgetc(IS -> data);
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
327 if (c2 == EOF)
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
328 c = EOF;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
329 else if (c2 != '\n')
330
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
330 ungetc(c2, IS -> data);
329
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 else if (c == '\n')
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
333 {
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
334 linebuff[lbloc] = '\0';
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
335 eol = 1;
330
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
336 c2 = fgetc(IS -> data);
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
337 if (c2 == EOF)
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
338 c = EOF;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
339 else if (c2 != '\r')
330
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
340 ungetc(c2, IS -> data);
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 else
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 (lbloc < 2048)
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
345 linebuff[lbloc++] = c;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
346 }
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
347 if (eol)
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 s = lw_strdup(linebuff);
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
350 return s;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
351 }
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
352 }
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 case input_type_string:
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
355 /* read from a string */
330
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
356 if (((char *)(IS -> data))[IS -> data2] == '\0')
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
357 {
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
358 struct input_stack *t;
330
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
359 lw_free(IS -> data);
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
360 lw_free(IS -> filespec);
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
361 t = IS -> next;
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
362 lw_free(IS);
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
363 as -> input_data = t;
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
364 goto nextfile;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
365 }
330
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
366 s = (char *)(IS -> data);
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
367 lbloc = 0;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
368 for (;;)
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 int c;
330
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
371 c = s[IS -> data2];
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
372 if (c)
330
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
373 IS -> data2++;
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
374 if (c == '\0')
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 linebuff[lbloc] = '\0';
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
377 eol = 1;
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 else if (c == '\r')
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
380 {
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
381 linebuff[lbloc] = '\0';
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
382 eol = 1;
330
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
383 if (s[IS -> data2] == '\n')
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
384 IS -> data2++;
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
385 }
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
386 else if (c == '\n')
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
387 {
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
388 linebuff[lbloc] = '\0';
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
389 eol = 1;
330
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
390 if (s[IS -> data2] == '\r')
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
391 IS -> data2++;
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
392 }
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
393 else
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 (lbloc < 2048)
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
396 linebuff[lbloc++] = c;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
397 }
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
398 if (eol)
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 s = lw_strdup(linebuff);
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
401 return s;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
402 }
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
403 }
328
591d01b343b9 checkpoint - input layer
lost
parents:
diff changeset
404
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
405 default:
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
406 lw_error("Problem reading from unknown input type");
c15cca3ae6a2 Created first pass of input layer
lost
parents: 328
diff changeset
407 }
328
591d01b343b9 checkpoint - input layer
lost
parents:
diff changeset
408 }
330
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
409
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
410 char *input_curspec(asmstate_t *as)
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
411 {
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
412 if (IS)
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
413 return IS -> filespec;
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
414 return NULL;
81c005b82775 More tinkering with input subsystem
lost
parents: 329
diff changeset
415 }