0 | 0 |
/* ******************************************************************
|
1 | 1 |
|
2 | 2 |
bef.c - The Original Befunge-93 Interpreter/Debugger in ANSI C
|
3 | |
v2.21 Sep 20 2004 Chris Pressey, Cat's-Eye Technologies
|
4 | |
|
5 | |
Copyright (c)1993-2004, Cat's Eye Technologies.
|
|
3 |
v2.22 Mar 18 2011 Chris Pressey, Cat's Eye Technologies
|
|
4 |
|
|
5 |
Copyright (c)1993-2011, Cat's Eye Technologies.
|
6 | 6 |
All rights reserved.
|
7 | 7 |
|
8 | 8 |
Redistribution and use in source and binary forms, with or without
|
|
10 | 10 |
are met:
|
11 | 11 |
|
12 | 12 |
Redistributions of source code must retain the above copyright
|
13 | |
notice, this list of conditions and the following disclaimer.
|
|
13 |
notices, this list of conditions and the following disclaimer.
|
14 | 14 |
|
15 | 15 |
Redistributions in binary form must reproduce the above copyright
|
16 | |
notice, this list of conditions and the following disclaimer in
|
|
16 |
notices, this list of conditions, and the following disclaimer in
|
17 | 17 |
the documentation and/or other materials provided with the
|
18 | 18 |
distribution.
|
19 | 19 |
|
20 | |
Neither the name of Cat's Eye Technologies nor the names of its
|
|
20 |
Neither the names of the copyright holders nor the names of their
|
21 | 21 |
contributors may be used to endorse or promote products derived
|
22 | 22 |
from this software without specific prior written permission.
|
23 | 23 |
|
24 | |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
|
25 | |
CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
26 | |
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
27 | |
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
28 | |
DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
|
29 | |
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
|
30 | |
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
31 | |
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
|
32 | |
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
33 | |
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
34 | |
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
35 | |
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
36 | |
POSSIBILITY OF SUCH DAMAGE.
|
|
24 |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
25 |
``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES INCLUDING, BUT NOT
|
|
26 |
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
27 |
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
28 |
COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
29 |
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
30 |
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
31 |
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
32 |
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
33 |
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
|
34 |
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
35 |
POSSIBILITY OF SUCH DAMAGE.
|
37 | 36 |
|
38 | 37 |
******************************************************************
|
39 | 38 |
|
40 | 39 |
Usage :
|
41 | 40 |
|
42 | |
bef [-d] [-o] [-q] [-i] [-=]
|
|
41 |
bef [-d] [-o] [-q] [-i] [-=] [-l] [-t]
|
43 | 42 |
[-r input-file] [-w output-file]
|
44 | 43 |
[-s stack-file] [-y delay] <befunge-source>
|
45 | 44 |
|
|
48 | 47 |
-q: produce no output except Befunge program output ('quiet')
|
49 | 48 |
-i: ignore unsupported instructions
|
50 | 49 |
-=: use b97-ish = directives
|
|
50 |
-l: wrap instead of truncating long program lines (compat w/2.21)
|
|
51 |
-t: make # at edge of playfield wrap inconsistently (compat w/2.21)
|
51 | 52 |
-r: redirect input from a specified file instead of stdin
|
52 | 53 |
-w: redirect output to a specified file instead of stdout
|
53 | 54 |
-s: write contents of stack to log file
|
|
60 | 61 |
Mingw v2.
|
61 | 62 |
|
62 | 63 |
******************************************************************
|
|
64 |
|
|
65 |
v2.22: Mar 2011, Chris Pressey
|
|
66 |
don't insert bogus EOF/directive characters into
|
|
67 |
playfield when loading otherwise blank source file
|
|
68 |
(thanks to whoever sent me this patch 6 years ago)
|
|
69 |
truncate long lines instead of wrapping them, so that
|
|
70 |
programs that use the full 80 characters on adjacent
|
|
71 |
lines (like mycology) can load correctly; -l disables
|
|
72 |
make # at edge of playfield wrap and consistently skip
|
|
73 |
a space, regardless of which edge it's on; -t disables
|
|
74 |
unknown command-line options are reported as such
|
|
75 |
use corrected verbiage in license (I am not a Regent)
|
63 | 76 |
|
64 | 77 |
v2.21: Sep 2004, Chris Pressey
|
65 | 78 |
display correct version number
|
|
174 | 187 |
int deldur = 25; /* debugging delay in milliseconds */
|
175 | 188 |
int ignore_unsupported = 0; /* flag : ignore unsupported instructions? */
|
176 | 189 |
int use_b97directives = 0; /* flag : use b97-esque directives? */
|
|
190 |
int wrap_long_program_lines = 0; /* flag : long program lines wrap? */
|
|
191 |
int inconsistent_trampoline = 0; /* flag : should # wrap inconsistently? */
|
177 | 192 |
|
178 | 193 |
/********************************************************* PROTOTYPES */
|
179 | 194 |
|
180 | 195 |
void push (signed long val);
|
181 | 196 |
signed long pop (void);
|
|
197 |
void usage (void);
|
182 | 198 |
|
183 | 199 |
/******************************************************* MAIN PROGRAM */
|
184 | 200 |
|
|
201 | 217 |
|
202 | 218 |
if (argc < 2)
|
203 | 219 |
{
|
204 | |
printf ("USAGE: bef [-d] [-o] [-q] [-i] [-=]\n");
|
205 | |
printf (" [-r input] [-w output] [-s stack] [-y delay] foo.bf\n");
|
206 | |
exit (0);
|
|
220 |
usage();
|
207 | 221 |
}
|
208 | 222 |
for (i = 1; i < argc; i++)
|
209 | 223 |
{
|
210 | |
if (!strcmp(argv[i], "-o")) { v10err_compat = 1; }
|
211 | |
if (!strcmp(argv[i], "-d")) { debug = 1; }
|
212 | |
if (!strcmp(argv[i], "-r")) { infile = 1; ia = i + 1; }
|
213 | |
if (!strcmp(argv[i], "-w")) { outfile = 1; oa = i + 1; }
|
214 | |
if (!strcmp(argv[i], "-s")) { stackfile = 1; sa = i + 1; }
|
215 | |
if (!strcmp(argv[i], "-y")) { deldur = atoi(argv[i + 1]); }
|
216 | |
if (!strcmp(argv[i], "-q")) { quiet = 1; }
|
217 | |
if (!strcmp(argv[i], "-i")) { ignore_unsupported = 1; }
|
218 | |
if (!strcmp(argv[i], "-=")) { use_b97directives = 1; }
|
|
224 |
if (argv[i][0] == '-') {
|
|
225 |
if (!strcmp(argv[i], "-o")) { v10err_compat = 1; }
|
|
226 |
else if (!strcmp(argv[i], "-d")) { debug = 1; }
|
|
227 |
else if (!strcmp(argv[i], "-r")) { infile = 1; ia = i + 1; }
|
|
228 |
else if (!strcmp(argv[i], "-w")) { outfile = 1; oa = i + 1; }
|
|
229 |
else if (!strcmp(argv[i], "-s")) { stackfile = 1; sa = i + 1; }
|
|
230 |
else if (!strcmp(argv[i], "-y")) { deldur = atoi(argv[i + 1]); }
|
|
231 |
else if (!strcmp(argv[i], "-q")) { quiet = 1; }
|
|
232 |
else if (!strcmp(argv[i], "-i")) { ignore_unsupported = 1; }
|
|
233 |
else if (!strcmp(argv[i], "-=")) { use_b97directives = 1; }
|
|
234 |
else if (!strcmp(argv[i], "-l")) { wrap_long_program_lines = 1; }
|
|
235 |
else if (!strcmp(argv[i], "-t")) { inconsistent_trampoline = 1; }
|
|
236 |
else
|
|
237 |
{
|
|
238 |
printf("Unknown option: %s\n", argv[i]);
|
|
239 |
usage();
|
|
240 |
}
|
|
241 |
}
|
219 | 242 |
}
|
220 | 243 |
if (!quiet)
|
221 | 244 |
{
|
222 | |
printf ("Befunge-93 Interpreter/Debugger v2.21\n");
|
|
245 |
printf ("Befunge-93 Interpreter/Debugger v2.22\n");
|
223 | 246 |
}
|
224 | 247 |
|
225 | 248 |
memset(pg, ' ', LINEWIDTH * PAGEHEIGHT);
|
|
239 | 262 |
|
240 | 263 |
while (!feof (f))
|
241 | 264 |
{
|
242 | |
cur = fgetc (f);
|
243 | |
if (use_b97directives && (x == 0) && ((cur == dc) || ((accept_pound) && (cur == '#'))))
|
|
265 |
tc = fgetc (f);
|
|
266 |
if (feof (f))
|
|
267 |
break;
|
|
268 |
if (use_b97directives && (x == 0) &&
|
|
269 |
((tc == dc) || (accept_pound && (tc == '#'))))
|
244 | 270 |
{
|
245 | |
if (cur != '#') accept_pound = 0;
|
|
271 |
if (tc != '#') accept_pound = 0;
|
246 | 272 |
tc = fgetc (f);
|
247 | 273 |
if (tc == 'l')
|
248 | 274 |
{
|
|
257 | 283 |
}
|
258 | 284 |
if (strcmp(s, "b93"))
|
259 | 285 |
{
|
260 | |
fprintf(stderr, "Error: only Befunge-93 (not %s) sources are supported by BEF.\n", s);
|
|
286 |
fprintf(stderr, "Error: only Befunge-93 (not %s) sources are supported by bef.\n", s);
|
261 | 287 |
exit(10);
|
262 | 288 |
}
|
263 | 289 |
}
|
|
268 | 294 |
} else
|
269 | 295 |
{
|
270 | 296 |
accept_pound = 0;
|
271 | |
if (cur == '\n')
|
272 | |
{
|
273 | |
cur = ' ';
|
|
297 |
if (tc == '\n')
|
|
298 |
{
|
274 | 299 |
x = 0;
|
275 | 300 |
y++;
|
276 | 301 |
if (y >= PAGEHEIGHT) break;
|
277 | 302 |
} else
|
278 | 303 |
{
|
|
304 |
cur = tc;
|
279 | 305 |
x++;
|
280 | 306 |
if (x >= LINEWIDTH)
|
281 | 307 |
{
|
|
308 |
if (!wrap_long_program_lines)
|
|
309 |
{
|
|
310 |
while (tc != '\n')
|
|
311 |
{
|
|
312 |
tc = fgetc (f);
|
|
313 |
if (feof(f)) { y = PAGEHEIGHT; break; }
|
|
314 |
}
|
|
315 |
}
|
282 | 316 |
x = 0;
|
283 | 317 |
y++;
|
284 | 318 |
if (y >= PAGEHEIGHT) break;
|
|
750 | 784 |
if (v10err_compat)
|
751 | 785 |
{
|
752 | 786 |
x = LINEWIDTH;
|
|
787 |
} else if (inconsistent_trampoline)
|
|
788 |
{
|
|
789 |
x = LINEWIDTH - 1;
|
753 | 790 |
} else
|
754 | |
{
|
755 | |
x = LINEWIDTH - 1;
|
756 | |
}
|
|
791 |
{
|
|
792 |
x += LINEWIDTH;
|
|
793 |
}
|
757 | 794 |
else
|
758 | 795 |
x = x % LINEWIDTH;
|
759 | 796 |
if (y < 0)
|
760 | 797 |
if (v10err_compat)
|
761 | 798 |
{
|
762 | 799 |
y = PAGEHEIGHT;
|
|
800 |
} else if (inconsistent_trampoline)
|
|
801 |
{
|
|
802 |
y = PAGEHEIGHT - 1;
|
763 | 803 |
} else
|
764 | |
{
|
765 | |
y = PAGEHEIGHT - 1;
|
766 | |
}
|
|
804 |
{
|
|
805 |
y += PAGEHEIGHT;
|
|
806 |
}
|
767 | 807 |
else
|
768 | 808 |
y = y % PAGEHEIGHT;
|
769 | 809 |
if (stackfile)
|
|
832 | 872 |
return 0;
|
833 | 873 |
}
|
834 | 874 |
}
|
|
875 |
|
|
876 |
void usage ()
|
|
877 |
{
|
|
878 |
printf ("USAGE: bef [-d] [-o] [-q] [-i] [-=] [-l] [-t]\n");
|
|
879 |
printf (" [-r input] [-w output] [-s stack] [-y delay] foo.bf\n");
|
|
880 |
exit (1);
|
|
881 |
}
|