Verified Commit efdae0b6 authored by Camil Staps's avatar Camil Staps 🙂

Avoid explicit mention of stdin, stdout, and stderr in instructions for file...

Avoid explicit mention of stdin, stdout, and stderr in instructions for file I/O when building with LINK_CLEAN_RUNTIME; this avoids __acrt_iob_func which has changed in recent Windows versions
parent c5bcb136
Pipeline #42571 passed with stages
in 16 minutes and 30 seconds
......@@ -28,7 +28,6 @@ longjmp
_setjmp
strerror
__acrt_iob_func
fclose
ferror
fflush
......
#ifdef LINK_CLEAN_RUNTIME
extern int w_get_char (void);
extern int w_get_int (BC_WORD_S*);
extern int w_get_real (BC_REAL*);
#endif
#ifdef MACH_O64
# define NEWLINE_CHAR '\r'
#else
......@@ -18,7 +24,8 @@ static char *file_modes[]={"r","w","a","rb","wb","ab"};
struct file {
FILE *file_handle;
int file_mode;
int file_mode:4;
int file_error:1;
};
static struct file clean_stdinout;
......
......@@ -138,7 +138,7 @@ instr_closeF t = foldl (flip append) t
, "\tif (!stdio_open)"
, "\t\tIO_error(\"fclose: file not open (stdio)\");"
, "\tstdio_open=0;"
, "\tbsp[0]=(ferror(stdin) || ferror(stdout)) ? 0 : 1;"
, "\tbsp[0]=f->file_error==0;"
, "} else if (f==&clean_stderr) {"
, "\tbsp[0]=1;"
, "} else {"
......@@ -155,10 +155,8 @@ instr_endF t = foldl (flip append) t
, "pc++;"
, "struct file *f=(struct file*)bsp[1];"
, "FILE *h;"
, "if (f==&clean_stdinout)"
, "\th=stdin;"
, "else if (f==&clean_stderr)"
, "\tIO_error(\"endF stderr not implemented\");"
, "if (f==&clean_stdinout || f==&clean_stderr)"
, "\tIO_error(\"FEnd: not allowed for StdIO and StdErr\");"
, "else"
, "\th=f->file_handle;"
, "char c=getc(h);"
......@@ -177,27 +175,34 @@ instr_errorF t = foldl (flip append) t
, "CHECK_FILE_IO;"
, "pc++;"
, "struct file *f=(struct file*)bsp[1];"
, "if (f==&clean_stdinout)"
, "\t*--bsp=(ferror(stdin) || ferror(stdout)) ? 1 : 0;"
, "else if (f==&clean_stderr)"
, "\t*--bsp=ferror(stderr) ? 1 : 0;"
, "else"
, "\t*--bsp=ferror(f->file_handle) ? 1 : 0;"
, "*--bsp=f->file_error!=0;"
, "}"
]
// TODO: implement for stdout and stderr on Windows
instr_flushF :: !Target -> Target
instr_flushF t = foldl (flip append) t
[ "{"
, "CHECK_FILE_IO;"
, "pc++;"
, "struct file *f=(struct file*)bsp[1];"
, "int error=0;"
, "if (f==&clean_stdinout)"
, "\t*--bsp=fflush(stdout) ? 0 : 1;"
, "#ifdef WINDOWS"
, "\tEPRINTF (\"flushF not implemented on Windows\\n\");"
, "#else"
, "\terror=fflush (stdout);"
, "#endif"
, "else if (f==&clean_stderr)"
, "\t*--bsp=fflush(stdin) ? 0 : 1;"
, "#ifdef WINDOWS"
, "\tEPRINTF (\"flushF not implemented on Windows\\n\");"
, "#else"
, "\terror=fflush (stdin);"
, "#endif"
, "else"
, "\t*--bsp=fflush(f->file_handle) ? 0 : 1;"
, "\terror=fflush (f->file_handle);"
, "f->file_error|=error;"
, "*--bsp=error==0;"
, "}"
]
......@@ -219,6 +224,7 @@ instr_openF t = foldl (flip append) t
, "\tIO_error(\"openF: unimplemented mode\");"
, "f->file_handle=fopen(file_name,mode);"
, "f->file_mode=bsp[0];"
, "f->file_error=0;"
, "bsp-=2;"
, "bsp[1]=0;"
, "if (f->file_handle==NULL) {"
......@@ -254,13 +260,12 @@ instr_readFC t = foldl (flip append) t
, "bsp-=2;"
, "if (f==&clean_stdinout) {"
, "\tbsp[1]=getchar();"
, "\tbsp[0]=ferror(stdin) ? 0 : 1;"
, "} else if (f==&clean_stderr) {"
, "\tIO_error(\"FReadC: can't read from StdErr\");"
, "} else {"
, "\tbsp[1]=getc(f->file_handle);"
, "\tbsp[0]=ferror(f->file_handle) ? 0 : 1;"
, "}"
, "bsp[0]=bsp[1]!=EOF;"
, "}"
]
......@@ -272,7 +277,11 @@ instr_readFI t = foldl (flip append) t
, "struct file *f=(struct file*)bsp[1];"
, "bsp-=2;"
, "if (f==&clean_stdinout) {"
, "#ifdef LINK_CLEAN_RUNTIME"
, "\tbsp[0]=w_get_int ((BC_WORD_S*)&bsp[1]);"
, "#else"
, "\tbsp[0]=fscanf(stdin,BC_WORD_S_FMT,(BC_WORD_S*)&bsp[1])==1;"
, "#endif"
, "} else if (f==&clean_stderr) {"
, "\tIO_error(\"FReadI: can't read from StdErr\");"
, "} else if (F_IS_TEXT_MODE(f->file_mode)) {"
......@@ -282,7 +291,7 @@ instr_readFI t = foldl (flip append) t
, "\tbsp[0]=1;"
, "\tbsp[1]=0;"
, "\tfor (int n=0; n<4; n++) {"
, "\t\tif ((i=fgetc(f->file_handle))==EOF) break;"
, "\t\tif ((i=fgetc(f->file_handle))==EOF) { bsp[0]=0; break; }"
, "\t\t((char*)&bsp[1])[n]=i;"
, "\t}"
, "#if WORD_WIDTH == 64"
......@@ -300,7 +309,11 @@ instr_readFR t = foldl (flip append) t
, "struct file *f=(struct file*)bsp[1];"
, "bsp-=2;"
, "if (f==&clean_stdinout) {"
, "#ifdef LINK_CLEAN_RUNTIME"
, "\tbsp[0]=w_get_real ((BC_REAL*)&bsp[1]);"
, "#else"
, "\tbsp[0]=fscanf(stdin,BC_REAL_SCAN_FMT,(BC_REAL*)&bsp[1])==1;"
, "#endif"
, "} else if (f==&clean_stderr) {"
, "\tIO_error(\"FReadI: can't read from StdErr\");"
, "} else if (F_IS_TEXT_MODE(f->file_mode)) {"
......@@ -310,7 +323,7 @@ instr_readFR t = foldl (flip append) t
, "\tbsp[0]=1;"
, "\t*(BC_REAL*)&bsp[1]=0.0;"
, "\tfor (int n=0; n<8; n++) {"
, "\t\tif ((i=fgetc(f->file_handle))==EOF) break;"
, "\t\tif ((i=fgetc(f->file_handle))==EOF) { bsp[0]=0; break; }"
, "\t\t((char*)&bsp[1])[n]=i;"
, "\t}"
, "}"
......@@ -325,7 +338,11 @@ instr_readFR_32 t = foldl (flip append) t
, "struct file *f=(struct file*)bsp[1];"
, "bsp-=3;"
, "if (f==&clean_stdinout) {"
, "#ifdef LINK_CLEAN_RUNTIME"
, "\tbsp[0]=w_get_real ((BC_REAL*)&bsp[1]);"
, "#else"
, "\tbsp[0]=fscanf(stdin,BC_DREAL_SCAN_FMT,(BC_DREAL*)&bsp[1])==1;"
, "#endif"
, "} else if (f==&clean_stderr) {"
, "\tIO_error(\"FReadI: can't read from StdErr\");"
, "} else if (F_IS_TEXT_MODE(f->file_mode)) {"
......@@ -343,17 +360,17 @@ instr_readFR_32 t = foldl (flip append) t
, "#if WORD_WIDTH==32"
, "\t*(BC_DREAL*)&bsp[1]=0.0;"
, "\tfor (int n=0; n<8; n++){"
, "\t\tif ((i=fgetc(f->file_handle))==EOF) break;"
, "\t\tif ((i=fgetc(f->file_handle))==EOF) { bsp[0]=0; break; }"
, "\t\t((char*)&bsp[1])[n]=i;"
, "\t}"
, "#else"
, "\tbsp[2]=bsp[1]=0;"
, "\tfor (int n=0; n<4; n++){"
, "\t\tif ((i=fgetc(f->file_handle))==EOF) break;"
, "\t\tif ((i=fgetc(f->file_handle))==EOF) { bsp[0]=0; break; }"
, "\t\t((char*)&bsp[1])[n]=i;"
, "\t}"
, "\tfor (int n=0; n<4; n++){"
, "\t\tif ((i=fgetc(f->file_handle))==EOF) break;"
, "\t\tif ((i=fgetc(f->file_handle))==EOF) { bsp[0]=0; break; }"
, "\t\t((char*)&bsp[2])[n]=i;"
, "\t}"
, "#endif"
......@@ -375,7 +392,18 @@ instr_readFS t = foldl (flip append) t
, "*++asp=(BC_WORD)hp;"
, "hp[0]=(BC_WORD)&__STRING__+2;"
, "if (f==&clean_stdinout) {"
, "#if defined (WINDOWS) && defined (LINK_CLEAN_RUNTIME)"
, "\tint n=0;"
, "\tint c;"
, "\tchar *s=(char*)&hp[2];"
, "\twhile (n!=len && (c=w_get_char (),c!=-1)){"
, "\t\t*s++=c;"
, "\t\tn++;"
, "\t}"
, "\thp[1]=n;"
, "#else"
, "\thp[1]=fread((char*)&hp[2],1,len,stdin);"
, "#endif"
, "} else if (f==&clean_stderr) {"
, "\tIO_error(\"FReadS: can't read from StdErr\");"
, "} else {"
......@@ -441,6 +469,7 @@ instr_seekF t = foldl (flip append) t
, "\tIO_error(\"FSeek: can't seek on StdIO and StdErr\");"
, "else"
, "\tbsp[0]=fseek(f->file_handle,position,seek_mode) ? 0 : 1;"
, "f->file_error|=bsp[0]!=1;"
, "}"
]
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment