23 char *progname
= NULL
;
25 char mem
[MEMSIZE
] = {0};
31 FILE *fd
= ret
? stderr
: stdout
;
32 fprintf (fd
, "usage: %s [-i file] [-h] [-m k&r|ansi|c99] [-o file] [-v]\n", progname
);
33 fprintf (fd
, " -i : input file\n");
34 fprintf (fd
, " -h : help message\n");
35 fprintf (fd
, " -m : indent mode\n");
36 fprintf (fd
, " -o : output file\n");
37 fprintf (fd
, " -v : verbose level (%d)\n", verbose
);
44 int main (int argc
, char *argv
[])
54 char *pt
= progname
= argv
[0];
56 if ((*pt
== '/') || (*pt
== '\\')) {
63 while ((c
= getopt(argc
, argv
, "i:o:hv:")) != EOF
) {
72 verbose
= atoi (optarg
);
76 return usage (c
!= 'h');
79 if (argc
- optind
!= 0) {
80 VERBOSE (ERROR
, fprintf (stderr
, "%s: invalid option -- %s\n", progname
, argv
[optind
]));
84 /* check input file */
86 fid
= fopen (input
, "r");
88 VERBOSE (ERROR
, fprintf (stderr
, "%s: can't open file '%s' for reading\n", progname
, input
));
97 buffer
= (char *) realloc (buffer
, size
+ BUFSIZE
);
99 VERBOSE (ERROR
, fprintf (stderr
, "%s: can't allocate memory\n", progname
));
102 memset (buffer
+ size
, 0, BUFSIZE
);
103 n
= fread (buffer
+ size
, 1, BUFSIZE
, fid
);
105 VERBOSE (ERROR
, fprintf (stderr
, "%s: can't read data from file '%s'\n", progname
, input
));
111 /* close input file */
113 VERBOSE (DEBUG
, fprintf (stderr
, "%s: read %d bytes\n", progname
, size
+ n
- BUFSIZE
));
115 /* check output file */
117 fid
= fopen (output
, "w");
119 VERBOSE (ERROR
, fprintf (stderr
, "%s: can't open file '%s' for writing\n", progname
, output
));
127 int rc
= process (buffer
, size
, fid
);
129 /* close output file */
136 int process (char *buffer
, int nb
, FILE *out
) {
139 for (i
= 0; (i
< nb
) && (buffer
[i
] != 0); i
++) {
141 VERBOSE (DEBUG
, fprintf (stderr
, "%s: read '%c' (%u)\n", progname
, buffer
[i
], buffer
[i
]));
143 case '>': /* increase pointer */
146 case '<': /* decrease pointer */
149 case '+': /* increase pointer value */
150 if ((p
>= 0) && (p
< MEMSIZE
)) {
153 VERBOSE (ERROR
, fprintf (stderr
, "%s: invalid address (%d)\n", progname
, p
));
157 case '-': /* decrease pointer value */
158 if ((p
>= 0) && (p
< MEMSIZE
)) {
161 VERBOSE (ERROR
, fprintf (stderr
, "%s: invalid address (%d)\n", progname
, p
));
165 case '.': /* output pointer value */
166 if ((p
>= 0) && (p
< MEMSIZE
)) {
167 fprintf (out
, "%c", mem
[p
]);
169 VERBOSE (ERROR
, fprintf (stderr
, "%s: invalid address (%d)\n", progname
, p
));
173 case ',': /* read a byte and store it in memory */
174 if ((p
>= 0) && (p
< MEMSIZE
)) {
175 mem
[p
] = buffer
[++i
];
177 VERBOSE (ERROR
, fprintf (stderr
, "%s: invalid address (%d)\n", progname
, p
));
181 case '[': /* jump to right bracket if pointer is set to 0 */
184 while ((++i
< nb
) && (bracket
> 0)) {
185 bracket
+= (buffer
[i
] == '[') ? +1 : (buffer
[i
] == ']') ? -1 :0;
188 VERBOSE (ERROR
, fprintf (stderr
, "%s: brace not closed\n", progname
));
192 return process (buffer
+ i
+ 1, nb
- i
- 1, out
);
195 case ']': /* jump to left bracket if pointer is different to 0 */
204 VERBOSE (WARNING
, fprintf (stderr
, "%s: can't understand '%c'\n", progname
, buffer
[i
]));
207 //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"));
215 // test: bf.exe -h | awk '/usage:/ { rc=1 } END { exit (1-rc) }'
216 // test: bf.exe -_ 2> /dev/null | awk 'END { if (NR == 0) { exit(0) } else exit (1) }'
217 // test: bf.exe -_ 2>&1 | awk '/usage:/ { rc=1 } END { exit (1-rc) }'
218 // test: echo '++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.' | bf.exe
219 // test: echo '++++++++++[>+>+++>+++++++>++++++++++<<<<-]>>>++++++++++++++.>+++++++++++++++++.<<++.>+++++++++++++.>--.<<.>+++.+.--.>----.++++++.+.<++.>----.++.<<.>>+.-------.<<.>>++.<.>+++++.<<.>-.+.<.>---.>---.<-.++++++++.>----.<---.>+++++++.<---.++++++++.' | bf.exe
220 // test: echo ',3>,2>>++++++++++++++++[-<+++<---<--->>>]<<[<[>>+>+<<<-]>>>[<<<+>>>-]<<-]>.' | bf.exe
222 /* vim: set ts=4 sw=4 et: */