...
 
Commits (2)
......@@ -3,6 +3,7 @@
#include <ctype.h>
#include <string.h>
#include <math.h>
#include <unistd.h>
#define ALLOCATED 1
#define UNALLOCATED 0
......@@ -26,7 +27,7 @@ const char xlat2[] =
"5z]&gqtyfr$(we4{WP)H-Zn,[%\\3dL+Q;>U!pJS72FhOA1C"
"B6v^=I_0/8|jsb9m<.TVac`uY*MK'X~xDl}REokN:#?G\"i@";
FILE *f;
FILE *fperror;
int main( int argc, char **argv )
{
......@@ -36,18 +37,43 @@ int main( int argc, char **argv )
unsigned int i,ii = 0;
int x;
int opt, outf=0;
char *usage="Usage: malbolge20 [-oh] prog-file\n";
while ((opt = getopt(argc, argv, "o")) != -1) {
switch (opt){
case 'o':
outf=1;
break;
case 'h':
default:
fputs(usage, stderr);
return (1);
}
}
for(i=0;i<59049;i++){// フラグ初期化
flag[i]=UNALLOCATED;
}
if ( argc != 2 )
if ( outf ) {
FILE *tmpf;
if ( ( tmpf = fopen( "info", "w" ) ) != NULL )
{
fperror = tmpf;
}
} else fperror = stdout;
if ( (argc - optind) != 1 )
{
fputs( "Usage: malbolge20 prog-file\n", stderr );
fputs(usage, stderr );
return ( 1 );
}
if ( ( f = fopen( argv[1], "r" ) ) == NULL )
if ( ( f = fopen( argv[optind], "r" ) ) == NULL )
{
fputs( "can't open file\n", stderr );
fputs( "can't open input file\n", stderr );
return ( 1 );
}
......@@ -55,7 +81,8 @@ int main( int argc, char **argv )
if ( mem == NULL )
{
fclose( f );
fputs( "can't allocate memory\n", stderr );
fprintf(fperror, "Error: can't allocate memory\n" );
fclose(fperror);
return ( 1 );
}
......@@ -65,7 +92,8 @@ int main( int argc, char **argv )
if ( mem[id] == NULL )
{
fclose( f );
fputs( "can't allocate memory\n", stderr );
fprintf(fperror, "Error: can't allocate memory\n" );
fclose(fperror);
return ( 1 );
}
flag[id]=ALLOCATED;
......@@ -79,15 +107,17 @@ int main( int argc, char **argv )
{
if ( strchr( "ji*p</vo", xlat1[( x - 33 + ii ) % 94] ) == NULL )
{
fputs( "invalid character in source file\n", stderr );
fprintf(fperror, "Error: invalid character (ascii=%d) in source file\n",x);
free( mem );
fclose( f );
fclose(fperror);
return ( 1 );
}
}else{
fputs( "invalid character in source file\n", stderr );
fprintf(fperror, "Error: invalid character (ascii=%d) in source file\n", x);
free( mem );
fclose( f );
fclose(fperror);
return ( 1 );
}
mem[id][i++] = x;
......@@ -99,6 +129,7 @@ int main( int argc, char **argv )
fputs( "input file too long\n", stderr );
free( mem );
fclose( f );
fclose(fperror);
return ( 1 );
}
//次ブロックのメモリ領域を確保
......@@ -106,7 +137,8 @@ int main( int argc, char **argv )
if ( mem[id] == NULL )
{
fclose( f );
fputs( "can't allocate memory\n", stderr );
fprintf(fperror, "Error: can't allocate memory\n" );
fclose(fperror);
return ( 1 );
}
flag[id]=ALLOCATED;//第idブロックを使用済みに変更
......@@ -115,6 +147,10 @@ int main( int argc, char **argv )
}
fclose( f );//入力の読み込み終了
//以下は使用中ブロックの余ったメモリの初期化処理
if(id==0 && i<=1) { // avoiding errors when null inputs
fprintf( fperror, "Exited due to an empty input\n" );
return(1);
}
if(i==0){
mem[id][0] = op( mem[id-1][59048], mem[id-1][59047] );
mem[id][1] = op( mem[id][0], mem[id-1][59048] );
......@@ -132,6 +168,8 @@ int main( int argc, char **argv )
exec( flag, mem, init );//実行
free( mem );
if(outf) fprintf(fperror,"Execution completed.\n");
fclose(fperror);
return ( 0 );
}
......@@ -186,7 +224,8 @@ void mem_manage(unsigned int *reg_addr, unsigned int num, int *id, unsigned int
if ( mem[new_id] == NULL ){
fclose( f );
free(mem);
fputs( "can't allocate memory\n", stderr );
fprintf(fperror, "Error: can't allocate memory\n");
fclose(fperror);
exit ( 1 );
}
mem[new_id][0] = init[new_id][0];
......@@ -213,7 +252,8 @@ void increment(unsigned int *reg_addr, int *id, int flag[59049], unsigned int **
mem[*id] = (unsigned int *)malloc( sizeof(unsigned int) * 59049 );
if ( mem[*id] == NULL ){
fclose( f );
fputs( "can't allocate memory\n", stderr );
fprintf(fperror, "Error: can't allocate memory\n");
fclose(fperror);
exit ( 1 );
}
mem[*id][0] = init[*id][0];
......@@ -238,7 +278,8 @@ void exec( int flag[59049], unsigned int **mem, unsigned int init[59049][2] )
for (;;)
{
if ( mem[c_id][c] < 33 || mem[c_id][c] > 126 ) {
printf("Enter into an infinite loop. exit(1). ");
fprintf(fperror,"Error: enterring into an infinite loop.\n");
fclose(fperror);
exit(1);
//continue; // もともとの処理
}
......