git @ Cat's Eye Technologies Befunge-93 / 9974ee7
Fixes Double free due to double fclose Trampoline at leftmost/topmost edges Don't print \0 into sourcecode Use %% instead of %c with '%' being passed in Avoid freeing fo/fi on failure to open Demur Rumed 6 years ago
2 changed file(s) with 36 addition(s) and 40 deletion(s). Raw diff Collapse all Expand all
7979 int pageheight = 1; /* 25 */
8080
8181 #define cur pg[y * 80 + x]
82 #define shrink(s) s[strlen(s)-1]=0
8382
8483 #define RIGHT "_%2.2d_%2.2d_R"
8584 #define LEFT "_%2.2d_%2.2d_L"
9291 #define x_left (x ? x-1 : linewidth-1)
9392
9493 #define y_2down (y+2)%pageheight
95 #define y_2up ((y>1) ? y-2 : ((pageheight-2)<0 ? 0 : (pageheight-2)))
94 #define y_2up ((y>1) ? y-2 : ((pageheight-2)<0 ? 0 : (y-2+pageheight)))
9695 #define x_2right (x+2)%linewidth
97 #define x_2left ((x>1) ? x-2 : ((linewidth-2)<0 ? 0 : (linewidth-2)))
96 #define x_2left ((x>1) ? x-2 : ((linewidth-2)<0 ? 0 : (x-2+linewidth)))
9897
9998 #define ALL RIGHT ": " LEFT ": " UP ": " DOWN ":\n"
10099
155154 int x = 0, y = 0;
156155 while (!feof (fi))
157156 {
158 cur = fgetc (fi);
159 if ((x+1)>linewidth) linewidth=x+1;
160 if ((y+1)>pageheight) pageheight=y+1;
161 if (cur == '\n')
162 {
163 cur = ' ';
157 int ch = fgetc (fi);
158 if (ch == -1) break;
159 if (ch == '\n')
160 {
164161 x = 0;
165162 y++;
166163 if (y >= 25) break;
164 else if (y > pageheight) pageheight=y;
167165 } else
168166 {
167 cur = ch;
169168 x++;
170169 if (x >= 80)
171170 {
172171 x = 0;
173172 y++;
174173 if (y >= 25) break;
174 else if (y > pageheight) pageheight=y;
175 } else if (x > linewidth)
176 {
177 linewidth = x;
175178 }
176179 }
177180 }
178 fclose (fi);
179181 } else
180182 {
181183 printf ("Error : couldn't open '%s' for input.\n", argv[argc - 1]);
208210 {
209211 for(x = 0; x < linewidth; x++)
210212 {
211 if (cur!='\\')
212 {
213 fprintf (fo, " pg[%d]='%c';\n", y * 80 + x, cur);
214 } else
215 {
216 fprintf (fo, " pg[%d]='%c%c';\n", y * 80 + x, cur, cur);
217 }
213 if (cur) fprintf (fo, " pg[%d]=%d;\n", y * 80 + x, cur);
218214 }
219215 }
220216
228224 {
229225 case '0': case '1': case '2': case '3': case '4':
230226 case '5': case '6': case '7': case '8': case '9':
231 fprintf(fo, RIGHT ": push(%c); goto " RIGHT ";\n", x, y, cur, x_right, y);
232 fprintf(fo, LEFT ": push(%c); goto " LEFT ";\n", x, y, cur, x_left, y);
233 fprintf(fo, UP ": push(%c); goto " UP ";\n", x, y, cur, x, y_up);
234 fprintf(fo, DOWN ": push(%c); goto " DOWN ";\n", x, y, cur, x, y_down);
227 fprintf(fo, RIGHT ": push(%d); goto " RIGHT ";\n", x, y, cur, x_right, y);
228 fprintf(fo, LEFT ": push(%d); goto " LEFT ";\n", x, y, cur, x_left, y);
229 fprintf(fo, UP ": push(%d); goto " UP ";\n", x, y, cur, x, y_up);
230 fprintf(fo, DOWN ": push(%d); goto " DOWN ";\n", x, y, cur, x, y_down);
235231 break;
236232 case '>': /* PC Right */
237233 fprintf(fo, ALL, x, y, x, y, x, y, x, y);
270266 ECHO("a=pop();b=pop();push(b/a);");
271267 break;
272268 case '%':
273 sprintf(t, "a=pop();b=pop();push(b%ca);", '%');
269 sprintf(t, "a=pop();b=pop();push(b%%a);");
274270 fprintf(fo, RIGHT ": %s goto " RIGHT ";\n", x, y, t, x_right, y);
275271 fprintf(fo, LEFT ": %s goto " LEFT ";\n", x, y, t, x_left, y);
276272 fprintf(fo, UP ": %s goto " UP ";\n", x, y, t, x, y_up);
283279 ECHO("a=pop();push(a);push(a);");
284280 break;
285281 case '.':
286 fprintf(fo, RIGHT ": fprintf(stdout,\"%cld \",pop());fflush(stdout); goto " RIGHT ";\n", x, y, '%', x_right, y);
287 fprintf(fo, LEFT ": fprintf(stdout,\"%cld \",pop());fflush(stdout); goto " LEFT ";\n", x, y, '%', x_left, y);
288 fprintf(fo, UP ": fprintf(stdout,\"%cld \",pop());fflush(stdout); goto " UP ";\n", x, y, '%', x, y_up);
289 fprintf(fo, DOWN ": fprintf(stdout,\"%cld \",pop());fflush(stdout); goto " DOWN ";\n", x, y, '%', x, y_down);
282 fprintf(fo, RIGHT ": fprintf(stdout,\"%%ld \",pop());fflush(stdout); goto " RIGHT ";\n", x, y, x_right, y);
283 fprintf(fo, LEFT ": fprintf(stdout,\"%%ld \",pop());fflush(stdout); goto " LEFT ";\n", x, y, x_left, y);
284 fprintf(fo, UP ": fprintf(stdout,\"%%ld \",pop());fflush(stdout); goto " UP ";\n", x, y, x, y_up);
285 fprintf(fo, DOWN ": fprintf(stdout,\"%%ld \",pop());fflush(stdout); goto " DOWN ";\n", x, y, x, y_down);
290286 break;
291287 case ',':
292 fprintf(fo, RIGHT ": fprintf(stdout,\"%cc\",pop());fflush(stdout); goto " RIGHT ";\n", x, y, '%', x_right, y);
293 fprintf(fo, LEFT ": fprintf(stdout,\"%cc\",pop());fflush(stdout); goto " LEFT ";\n", x, y, '%', x_left, y);
294 fprintf(fo, UP ": fprintf(stdout,\"%cc\",pop());fflush(stdout); goto " UP ";\n", x, y, '%', x, y_up);
295 fprintf(fo, DOWN ": fprintf(stdout,\"%cc\",pop());fflush(stdout); goto " DOWN ";\n", x, y, '%', x, y_down);
288 fprintf(fo, RIGHT ": fprintf(stdout,\"%%c\",pop());fflush(stdout); goto " RIGHT ";\n", x, y, x_right, y);
289 fprintf(fo, LEFT ": fprintf(stdout,\"%%c\",pop());fflush(stdout); goto " LEFT ";\n", x, y, x_left, y);
290 fprintf(fo, UP ": fprintf(stdout,\"%%c\",pop());fflush(stdout); goto " UP ";\n", x, y, x, y_up);
291 fprintf(fo, DOWN ": fprintf(stdout,\"%%c\",pop());fflush(stdout); goto " DOWN ";\n", x, y, x, y_down);
296292 break;
297293 case '&':
298 fprintf(fo, RIGHT ": fscanf(stdin,\"%cld\",&b);push(b); goto " RIGHT ";\n", x, y, '%', x_right, y);
299 fprintf(fo, LEFT ": fscanf(stdin,\"%cld\",&b);push(b); goto " LEFT ";\n", x, y, '%', x_left, y);
300 fprintf(fo, UP ": fscanf(stdin,\"%cld\",&b);push(b); goto " UP ";\n", x, y, '%', x, y_up);
301 fprintf(fo, DOWN ": fscanf(stdin,\"%cld\",&b);push(b); goto " DOWN ";\n", x, y, '%', x, y_down);
294 fprintf(fo, RIGHT ": fscanf(stdin,\"%%ld\",&b);push(b); goto " RIGHT ";\n", x, y, x_right, y);
295 fprintf(fo, LEFT ": fscanf(stdin,\"%%ld\",&b);push(b); goto " LEFT ";\n", x, y, x_left, y);
296 fprintf(fo, UP ": fscanf(stdin,\"%%ld\",&b);push(b); goto " UP ";\n", x, y, x, y_up);
297 fprintf(fo, DOWN ": fscanf(stdin,\"%%ld\",&b);push(b); goto " DOWN ";\n", x, y, x, y_down);
302298 break;
303299 case '~':
304300 fprintf(fo, RIGHT ": c=fgetc(stdin);push(c); goto " RIGHT ";\n", x, y, x_right, y);
320316 break;
321317 case '?':
322318 fprintf(fo, ALL, x, y, x, y, x, y, x, y);
323 fprintf(fo, " switch ((rand () / 32) %c 4) \n"
319 fprintf(fo, " switch ((rand () / 32) %% 4) \n"
324320 " { case 0: goto " RIGHT ";\n"
325321 " case 1: goto " LEFT ";\n"
326322 " case 2: goto " UP ";\n"
327323 " case 3: goto " DOWN "; }\n",
328 '%', x_right, y, x_left, y, x, y_up, x, y_down);
324 x_right, y, x_left, y, x, y_up, x, y_down);
329325 break;
330326 case '#':
331327 fprintf(fo, RIGHT ": goto " RIGHT ";\n", x, y, x_2right, y);
415411 }
416412 }
417413 }
418 fclose (fi);
419 fclose (fo);
414 if (fi) fclose (fi);
415 if (fo) fclose (fo);
420416 if ((fi = fopen ("temp.c", "r")) != NULL)
421417 {
422418 if ((fo = fopen (argv[argc - 1], "w")) != NULL)
556556 y += dy;
557557 if (x < 0)
558558 {
559 x = LINEWIDTH - 1;
559 x += LINEWIDTH;
560560 } else
561561 {
562562 x = x % LINEWIDTH;
563563 }
564564 if (y < 0)
565565 {
566 y = PAGEHEIGHT - 1;
566 y += PAGEHEIGHT;
567567 } else
568568 {
569569 y = y % PAGEHEIGHT;