git @ Cat's Eye Technologies stringie / 275b01d
Add ability to load Underload program from a file. Chris Pressey 6 years ago
2 changed file(s) with 50 addition(s) and 1 deletion(s). Raw diff Collapse all Expand all
3434 From this we can see that the Underload program to be interpreted is passed
3535 directly in the first command-line argument to the executable.
3636
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
3742 Included materials
3843 ------------------
3944
171171 free(program);
172172 }
173173
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
174205 int main(int argc, char **argv)
175206 {
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 }
177221 root = NULL;
178222 run(program);
179223 exit(0);