comparison lwasm/pseudo.c @ 225:058f18119025

Fixed filename parsing bug in include directive and added includebin directive
author lost
date Thu, 11 Jun 2009 23:29:15 +0000
parents 271c0ef9ea60
children 59a138df0401
comparison
equal deleted inserted replaced
224:2e5a32d56e60 225:058f18119025
19 19
20 20
21 This file implements the various pseudo operations. 21 This file implements the various pseudo operations.
22 */ 22 */
23 #include <config.h> 23 #include <config.h>
24 #include <errno.h>
25 #include <stdio.h>
24 #include <stdlib.h> 26 #include <stdlib.h>
25 #include <string.h> 27 #include <string.h>
26 #include "lwasm.h" 28 #include "lwasm.h"
27 #include "instab.h" 29 #include "instab.h"
28 #include "expr.h" 30 #include "expr.h"
115 117
116 fn = lwasm_alloc(v1 + 1); 118 fn = lwasm_alloc(v1 + 1);
117 memcpy(fn, *p, v1); 119 memcpy(fn, *p, v1);
118 fn[v1] = '\0'; 120 fn[v1] = '\0';
119 121
122 (*p) += v1;
123 if (**p == '"')
124 (*p)++;
125
120 // end local label context on include 126 // end local label context on include
121 as -> context = lwasm_next_context(as); 127 as -> context = lwasm_next_context(as);
122 if (lwasm_read_file(as, fn) < 0) 128 if (lwasm_read_file(as, fn) < 0)
123 { 129 {
124 register_error(as, l, 1, "File include error (%s)", fn); 130 register_error(as, l, 1, "File include error (%s)", fn);
125 } 131 }
126 lwasm_free(fn); 132 lwasm_free(fn);
133 }
134
135 /*
136 The operand for includebin is a string optionally enclosed in "
137 */
138 OPFUNC(pseudo_includebin)
139 {
140 int v1;
141 char *fn;
142 FILE *f;
143
144 // only include files on pass 1
145 while (**p && isspace(**p))
146 (*p)++;
147
148 if (!**p)
149 {
150 register_error(as, l, 1, "Bad file name");
151 return;
152 }
153
154 if (**p == '"')
155 {
156 // search for ending "
157 (*p)++;
158 for (v1 = 0; *((*p)+v1) && *((*p)+v1) != '"'; v1++)
159 /* do nothing */ ;
160 if (*((*p)+v1) != '"')
161 {
162 register_error(as, l, 1, "Bad file name");
163 return;
164 }
165 }
166 else
167 {
168 // search for a space type character
169 for (v1 = 0; *((*p)+v1) && !isspace(*((*p)+v1)); v1++)
170 ;
171 }
172
173 fn = lwasm_alloc(v1 + 1);
174 memcpy(fn, *p, v1);
175 fn[v1] = '\0';
176
177 (*p) += v1;
178 if (**p == '"')
179 (*p)++;
180
181 // open the file
182 f = fopen(fn, "rb");
183 if (!f)
184 {
185 register_error(as, l, 1, "Cannot open file: %s", strerror(errno));
186 register_error(as, l, 2, "Cannot open file: %s", strerror(errno));
187 }
188
189 // don't need fn any more
190 lwasm_free(fn);
191
192 // read the contents of the file and "emit()" it
193 while (!feof(f) && !ferror(f))
194 {
195 v1 = fgetc(f);
196 if (v1 == EOF)
197 break;
198 lwasm_emit(as, l, v1);
199 }
200 // close file
201 fclose(f);
127 } 202 }
128 203
129 OPFUNC(pseudo_rmb) 204 OPFUNC(pseudo_rmb)
130 { 205 {
131 int r, v; 206 int r, v;