#include #include # include #include "system.h" # include "stdio.h" # include "object.h" # include "operator.h" # include "defines.h" # include "class.h" #include "dict.h" void equalsOP(void) { int length, i ; unsigned char *p ; p = cvs(pop(),&length) ; for ( i=0 ; i NEL(buf) ){ fprintf(stderr,"file filename too long %d\n", filename.value.v_string.length); pserror("limitcheck", "file") ; } memmove(buf,filename.value.v_string.chars,filename.value.v_string.length) ; buf[filename.value.v_string.length] = '\0' ; switch(access.value.v_string.chars[0]){ case 'r' : fp = fopen(buf,"r") ; if ( fp == NULL ){ fprintf(stderr,"name %s ",buf); pserror("undefinedfilename", "file read") ; } push(makefile(fp,AC_READONLY,XA_LITERAL)) ; return; case 'w' : fp = fopen(buf,"r+") ; if ( fp == NULL ){ fp = fopen(buf,"w+") ; if ( fp == NULL ){ fprintf(stderr,"name %s ",buf); pserror("undefinedfilename", "file write") ; } } push(makefile(fp,AC_UNLIMITED,XA_LITERAL)) ; return; default : pserror("invalidfileaccess", "file") ; } } void flushfileOP(void) { struct object file ; int i; file = pop() ; if ( file.type != OB_FILE ) pserror("typecheck", "flushfile") ; if ( file.value.v_file.access == AC_READONLY ){ if ( (i=fseek(file.value.v_file.fp,0,2)) != 0 ){ fprintf(stderr,"i %d\n",i); pserror("ioerror", "flushfile1") ; } } else if ( fflush(file.value.v_file.fp) == EOF ) pserror("ioerror", "flushfile2") ; } void closefileOP(void) { struct object file ; file = pop() ; push(file) ; flushfileOP() ; if(decryptf){ decryptf=0; return; } if ( fclose(file.value.v_file.fp) == EOF ) pserror("ioerror", "closefile") ; } void readOP(void) { int c ; struct object file ; file = pop() ; if ( file.type != OB_FILE ){ fprintf(stderr,"type %o\n",file.type); pserror("typecheck", "read") ; } if ( file.value.v_file.access == AC_NONE) pserror("invalidaccess", "read") ; if(decryptf)pserror("decrypt", "read"); c = fgetc(file.value.v_file.fp) ; if ( c == EOF ) if ( ferror(file.value.v_file.fp) ) pserror("ioerror", "read") ; else push(false) ; else { push(makeint(c)) ; push(true) ; } } void writeOP(void) { int c ; struct object file, object ; object = integer("write"); c = object.value.v_int ; file = pop() ; if ( file.type != OB_FILE ) pserror("typecheck", "write") ; if ( file.value.v_file.access != AC_UNLIMITED ) pserror("invalidaccess", "write") ; if ( fputc(c,file.value.v_file.fp) == EOF ) pserror("ioerror", "write") ; } void readlineOP(void) { int i, c ; struct object string, file ; string = pop() ; if ( string.type != OB_STRING ) pserror("typecheck", "readline") ; file = pop() ; if ( file.type != OB_FILE ) pserror("typecheck", "readline") ; fseek(file.value.v_file.fp,0,1) ; if(decryptf)pserror("decrypt", "readline"); for ( i=0 ; iwp - file.value.v_file.fp->rp ; push(makeint(count)) ; } void statusOP(void) { struct object status, file ; file = pop() ; if ( file.type != OB_FILE ) pserror("typecheck", "status") ; if ( ferror(file.value.v_file.fp) != 0 ) status = false ; else status = true ; push(status) ; } void tokenOP(void) { int length ; struct object object, source ; FILE *fp ; source = pop() ; switch(source.type){ case OB_FILE : object = get_token(source.value.v_file.fp,&length) ; if ( object.type == OB_NONE ){ push(source) ; closefileOP() ; push(false) ; } else { push(object) ; push(true) ; } return; case OB_STRING : fp = sopen(source.value.v_string) ; object = scanner(fp) ; source.value.v_string.chars += fp->rp - fp->buf ; /*plan9*/ source.value.v_string.length -= fp->rp - fp->buf ; /*plan9*/ f_close(fp); if ( object.type == OB_NONE ) push(false) ; else { push(source) ; push(object) ; push(true) ; } return; default : pserror("typecheck", "token") ; } } void runOP(void) { char buf[BUFSIZ] ; FILE *fp ; filename = pop() ; if ( filename.type != OB_STRING ) pserror("typecheck", "run") ; if ( filename.value.v_string.length + 1 > NEL(buf) ) pserror("limitcheck", "run") ; memmove(buf,filename.value.v_string.chars,filename.value.v_string.length) ; buf[filename.value.v_string.length] = '\0' ; fp = fopen(buf,"r") ; if ( fp == NULL ){ fprintf(stderr,"name %s ",buf); pserror("undefinedfilename", "run") ; } currentfile = fp; execpush(makefile(fp,AC_EXECUTEONLY,XA_EXECUTABLE)) ; }