Add ability to load Underload program from a file.
Chris Pressey
6 years ago
34 | 34 |
From this we can see that the Underload program to be interpreted is passed
|
35 | 35 |
directly in the first command-line argument to the executable.
|
36 | 36 |
|
|
37 |
However, there is also a form by which the Underload program can be read from
|
|
38 |
a file:
|
|
39 |
|
|
40 |
./bin/stringie from example.ul
|
|
41 |
|
37 | 42 |
Included materials
|
38 | 43 |
------------------
|
39 | 44 |
|
171 | 171 |
free(program);
|
172 | 172 |
}
|
173 | 173 |
|
|
174 |
char *readfile(FILE *f)
|
|
175 |
{
|
|
176 |
char chunk[256];
|
|
177 |
char *buffer;
|
|
178 |
int size = 256;
|
|
179 |
int len = 0;
|
|
180 |
int n;
|
|
181 |
|
|
182 |
buffer = malloc(size);
|
|
183 |
|
|
184 |
n = fread(chunk, 1, 256, f);
|
|
185 |
while (n > 0) {
|
|
186 |
if (len + n > size) {
|
|
187 |
size *= 2;
|
|
188 |
buffer = realloc(buffer, size);
|
|
189 |
}
|
|
190 |
memcpy(buffer + len, chunk, n);
|
|
191 |
len += n;
|
|
192 |
n = fread(chunk, 256, 1, f);
|
|
193 |
}
|
|
194 |
|
|
195 |
/* NUL-terminate the buffer; but first, make sure the NUL can fit! */
|
|
196 |
if (len + 1 > size) {
|
|
197 |
size *= 2;
|
|
198 |
buffer = realloc(buffer, size);
|
|
199 |
}
|
|
200 |
buffer[len + 1] = '\0';
|
|
201 |
|
|
202 |
return buffer;
|
|
203 |
}
|
|
204 |
|
174 | 205 |
int main(int argc, char **argv)
|
175 | 206 |
{
|
176 | |
char *program = strdupe(argv[1]);
|
|
207 |
char *program;
|
|
208 |
FILE *f;
|
|
209 |
|
|
210 |
if (argc >= 1) {
|
|
211 |
if (!strcmp(argv[1], "from") && argc >= 2) {
|
|
212 |
if ((f = fopen(argv[2], "r")) == NULL) {
|
|
213 |
exit(1);
|
|
214 |
}
|
|
215 |
program = readfile(f);
|
|
216 |
fclose(f);
|
|
217 |
} else {
|
|
218 |
program = strdupe(argv[1]);
|
|
219 |
}
|
|
220 |
}
|
177 | 221 |
root = NULL;
|
178 | 222 |
run(program);
|
179 | 223 |
exit(0);
|