24 char *progname
= NULL
;
26 char mem
[MEMSIZE
] = {0};
28 int jump
[MAXJUMP
] = {0};
34 FILE *fd
= ret
? stderr
: stdout
;
35 fprintf (fd
, "usage: %s [-i file] [-h] [-m memory] [-o file] [-v]\n", progname
);
36 fprintf (fd
, " -i : input file\n");
37 fprintf (fd
, " -h : help message\n");
38 fprintf (fd
, " -m : memory [0..0]\n");
39 fprintf (fd
, " -o : output file\n");
40 fprintf (fd
, " -v : verbose level (%d)\n", verbose
);
47 int process (char *buffer
, int nb
, FILE *out
) {
50 for (i
= 0; i
< nb
; i
++) {
55 VERBOSE (DEBUG
, fprintf (stderr
, "%s: p=%d, buffer='%s', memory=[ ", progname
, p
, buffer
+ i
); int _i
; for (_i
= 0; _i
< MEMSIZE
; _i
++) fprintf (stderr
," %d", mem
[_i
]); fprintf (stderr
," ]\n"));
58 case '>': /* increase pointer */
61 case '<': /* decrease pointer */
64 case '+': /* increase pointer value */
65 if ((p
>= 0) && (p
< MEMSIZE
)) {
68 VERBOSE (ERROR
, fprintf (stderr
, "%s: invalid address (%d)\n", progname
, p
));
72 case '-': /* decrease pointer value */
73 if ((p
>= 0) && (p
< MEMSIZE
)) {
76 VERBOSE (ERROR
, fprintf (stderr
, "%s: invalid address (%d)\n", progname
, p
));
80 case '.': /* output pointer value */
81 if ((p
>= 0) && (p
< MEMSIZE
)) {
82 fprintf (out
, "%c", mem
[p
]);
84 VERBOSE (ERROR
, fprintf (stderr
, "%s: invalid address (%d)\n", progname
, p
));
88 case ',': /* read a byte and store it in memory */
89 if ((p
>= 0) && (p
< MEMSIZE
)) {
91 mem
[p
] = (c
> 0) ? c
: 0;
93 VERBOSE (ERROR
, fprintf (stderr
, "%s: invalid address (%d)\n", progname
, p
));
97 case '[': /* jump to right bracket if pointer is set to 0 */
98 if ((p
< 0) || (p
>= MEMSIZE
)) {
99 VERBOSE (ERROR
, fprintf (stderr
, "%s: invalid address (%d)\n", progname
, p
));
104 while ((++i
< nb
) && (bracket
> 0)) {
105 bracket
+= (buffer
[i
] == '[') ? +1 : (buffer
[i
] == ']') ? -1 :0;
108 VERBOSE (ERROR
, fprintf (stderr
, "%s: brace not closed\n", progname
));
114 VERBOSE (ERROR
, fprintf (stderr
, "%s: too many jump\n", progname
));
120 case ']': /* jump to left bracket if pointer is different to 0 */
122 VERBOSE (ERROR
, fprintf (stderr
, "%s: can't jump back\n", progname
));
133 VERBOSE (WARNING
, fprintf (stderr
, "%s: can't understand '%c'\n", progname
, buffer
[i
]));
136 //VERBOSE (DEBUG, int _i; fprintf (stderr, "%s: p: %d mem:", progname, p); for (_i = 0; _i < MEMSIZE; _i++) fprintf (stderr, " %d", mem[_i]); fprintf (stderr, "\n"));
145 int main (int argc
, char *argv
[])
156 char *pt
= progname
= argv
[0];
158 if ((*pt
== '/') || (*pt
== '\\')) {
165 while ((c
= getopt(argc
, argv
, "e:i:m:o:hv:")) != EOF
) {
171 buffer
= strdup (optarg
);
172 if (buffer
== NULL
) {
173 VERBOSE (ERROR
, fprintf (stderr
, "%s: can't allocate memory\n", progname
));
176 size
= strlen (buffer
) + 1;
182 for (i
= 0; i
< MEMSIZE
; i
++) {
184 mem
[i
] = strtol (optarg
, &optarg
, 10);
185 VERBOSE (DEBUG
, fprintf (stderr
, "%s: mem[%d] = %d\n", progname
, i
, mem
[i
]));
189 VERBOSE (WARNING
, fprintf (stderr
, "%s: too many memory values or incorrect value\n", progname
));
197 verbose
= atoi (optarg
);
201 return usage (c
!= 'h');
204 if (argc
- optind
!= 0) {
205 VERBOSE (ERROR
, fprintf (stderr
, "%s: invalid option -- %s\n", progname
, argv
[optind
]));
209 /* check input file */
211 fid
= fopen (input
, "r");
213 VERBOSE (ERROR
, fprintf (stderr
, "%s: can't open file '%s' for reading\n", progname
, input
));
216 } else if (buffer
== NULL
) {
220 /* read input file */
222 while (!feof (fid
)) {
223 buffer
= (char *) realloc (buffer
, size
+ BUFSIZE
);
224 if (buffer
== NULL
) {
225 VERBOSE (ERROR
, fprintf (stderr
, "%s: can't allocate memory\n", progname
));
228 memset (buffer
+ size
, 0, BUFSIZE
);
229 n
= fread (buffer
+ size
, 1, BUFSIZE
, fid
);
231 VERBOSE (ERROR
, fprintf (stderr
, "%s: can't read data from file '%s'\n", progname
, input
));
237 /* close input file */
241 VERBOSE (DEBUG
, fprintf (stderr
, "%s: read %d bytes\n", progname
, size
+ n
- BUFSIZE
));
243 VERBOSE (DEBUG
, fprintf (stderr
, "%s: prog %d bytes\n", progname
, size
-1));
246 /* check output file */
248 fid
= fopen (output
, "w");
250 VERBOSE (ERROR
, fprintf (stderr
, "%s: can't open file '%s' for writing\n", progname
, output
));
251 if (buffer
) free (buffer
);
259 int rc
= process (buffer
, size
, fid
);
261 /* close output file */
271 VERBOSE (INFO
, fprintf (stdout
, "\nmemory:"); int _i
; for (_i
= 0; _i
< MEMSIZE
; _i
++) fprintf (stdout
," %d", mem
[_i
]); fprintf (stdout
,"\n"));
277 // test: bf.exe -h | awk '/usage:/ { rc=1 } END { exit (1-rc) }'
279 // test: bf.exe -_ 2> /dev/null | awk 'END { if (NR == 0) { exit(0) } else exit (1) }'
280 // test: bf.exe -_ 2>&1 | awk '/usage:/ { rc=1 } END { exit (1-rc) }'
281 // test: bf.exe error 2>&1 | awk '/usage:/ { rc=1 } END { exit (1-rc) }'
283 // test: bf.exe -i error.b 2>&1 | grep "can't open" | grep -q "reading"
284 // test: echo ">>." | bf.exe -o error/error.b 2>&1 | grep "can't open" | grep -q "writing"
285 // test: echo "error" | bf.exe -v1 2>&1 | grep -q "can't understand"
286 // test: echo "<+" | bf.exe -v1 2>&1 | grep -q "invalid address"
287 // test: echo "<-" | bf.exe -v1 2>&1 | grep -q "invalid address"
288 // test: echo "<." | bf.exe -v1 2>&1 | grep -q "invalid address"
289 // test: echo "<," | bf.exe -v1 2>&1 | grep -q "invalid address"
290 // test: echo "<[" | bf.exe -v1 2>&1 | grep -q "invalid address"
291 // test: echo "[" | bf.exe -v1 2>&1 | grep -q "brace not closed"
292 // test: echo "+[[[[[]]]]]" | bf.exe -v1 2>&1 | grep -q "too many jump"
293 // test: bf.exe -m "0 1 2 3 4 5 6 7 8 9 10" -e '.' -v1 2>&1 | grep -q "too many memory values"
294 // test: bf.exe -m "0 error" -e '.' -v1 2>&1 | grep -q "incorrect value"
295 // test: echo "]" | bf.exe -v1 2>&1 | grep -q "can't jump back"
297 // test: echo '+++>++>>-<--' | bf.exe -v2 | grep -q "memory: 3 2 -2 -1 0"
298 // test: bf.exe -e '+++' -e '+++>++>>-<--' -v2 | grep -q "memory: 3 2 -2 -1 0"
299 // test: bf.exe -e '+++>++>>-<--' -v2 | grep -q "memory: 3 2 -2 -1 0"
300 // test: bf.exe -m "51 50 49" -e '.>.>.' |grep -q "321"
301 // test: bf.exe -m "51 50 49" -e '.>.>.' -o test.log && grep -q "321" test.log; rc=$?; rm test.log; test $rc -eq 0
302 // test: echo '+++>++>>-<--' > test.b; bf.exe -i test.b | grep -q "memory: 3 2 -2 -1 0"; rc=$?; rm test.b; test $rc -eq 0
304 // test: echo '++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.' | bf.exe -v1 | grep -q "Hello World!"
305 // test: echo '++++++++++[>+>+++>+++++++>++++++++++<<<<-]>>>++++++++++++++.>+++++++++++++++++.<<++.>+++++++++++++.>--.<<.>+++.+.--.>----.++++++.+.<++.>----.++.<<.>>+.-------.<<.>>++.<.>+++++.<<.>-.+.<.>---.>---.<-.++++++++.>----.<---.>+++++++.<---.++++++++.' | bf.exe -v1 | grep -q "Tu as decouvert un peu de brainfuck"
307 // test: echo -e "123\0" | bf.exe -e ',[>,]' -v2 | grep -q "memory: 49 50 51 0"
308 // test: echo -e "4+3\0" | bf.exe -e ',>++++++[<-------->-],,[<+>-]<.' -v1 | grep -q 7
309 // test: echo -e "1+7\0" | bf.exe -e ',>++++++[<-------->-],,[<+>-]<.' -v1 | grep -q 8
310 // test: echo -e "3*2\0" | bf.exe -e ',>,,>++++++++[<------<------>>-]<<[>[>+>+<<-]>>[<<+>>-]<<<-]>>>++++++[<++++++++>-]<.' -v1 | grep -q 6
311 // test: echo -e "1*7\0" | bf.exe -e ',>,,>++++++++[<------<------>>-]<<[>[>+>+<<-]>>[<<+>>-]<<<-]>>>++++++[<++++++++>-]<.' -v1 | grep -q 7
313 /* vim: set ts=4 sw=4 et: */