=================================================================== RCS file: /home/cvs/OpenXM_contrib2/asir2000/builtin/file.c,v retrieving revision 1.16 retrieving revision 1.37 diff -u -p -r1.16 -r1.37 --- OpenXM_contrib2/asir2000/builtin/file.c 2002/10/01 09:58:48 1.16 +++ OpenXM_contrib2/asir2000/builtin/file.c 2017/09/02 04:04:41 1.37 @@ -45,28 +45,32 @@ * DEVELOPER SHALL HAVE NO LIABILITY IN CONNECTION WITH THE USE, * PERFORMANCE OR NON-PERFORMANCE OF THE SOFTWARE. * - * $OpenXM: OpenXM_contrib2/asir2000/builtin/file.c,v 1.15 2001/10/09 01:36:05 noro Exp $ + * $OpenXM: OpenXM_contrib2/asir2000/builtin/file.c,v 1.36 2017/06/12 06:22:50 noro Exp $ */ #include "ca.h" #include "parse.h" #include "ox.h" #include "base.h" -#if !defined(VISUAL) +#include +#if !defined(VISUAL) && !defined(__MINGW32__) #include "unistd.h" #endif -#if PARI +#if defined(PARI) #include "genpari.h" #endif -#if defined(VISUAL) +#if defined(VISUAL) || defined(__MINGW32__) #include #include #include /* #define ECGEN_KEYNAME "SoftWare\\Fujitsu\\WinECgen\\1.00.000" */ #define ECGEN_KEYNAME "SoftWare\\Fujitsu\\FSEcParamGen\\V1.0L10" #define ASIR_KEYNAME "SoftWare\\Fujitsu\\Asir\\1999.03.31" +#define R_OK 0x04 #endif +void Psprintf(NODE,STRING *); + void Pget_rootdir(); void Paccess(),Premove_file(); void Pbsave_enc(), Pbload_enc(); @@ -76,30 +80,36 @@ void Pbsave(), Pbload(), Pbload27(); void Pbsave_compat(), Pbload_compat(); void Pbsave_cmo(), Pbload_cmo(); void Popen_file(), Pclose_file(), Pget_line(), Pget_byte(), Pput_byte(); +void Pput_word(), Pget_word(); void Ppurge_stdin(); -void Pload_exec(); +void Pfprintf(); +void Pimport(); +void Pgetpid(); extern int des_encryption; extern char *asir_libdir; struct ftab file_tab[] = { + {"fprintf",Pfprintf,-99999999}, {"purge_stdin",Ppurge_stdin,0}, {"open_file",Popen_file,-2}, {"close_file",Pclose_file,1}, {"get_byte",Pget_byte,1}, {"put_byte",Pput_byte,2}, + {"get_word",Pget_word,1}, + {"put_word",Pput_word,2}, {"get_line",Pget_line,-1}, {"remove_file",Premove_file,1}, {"access",Paccess,1}, {"load",Pload,-1}, - {"load_exec",Pload_exec,1}, + {"import",Pimport,-1}, {"which",Pwhich,1}, {"loadfiles",Ploadfiles,1}, {"output",Poutput,-1}, {"bsave",Pbsave,2}, {"bload",Pbload,1}, {"get_rootdir",Pget_rootdir,0}, -#if defined(VISUAL) && defined(DES_ENC) +#if defined(DES_ENC) {"bsave_enc",Pbsave_enc,2}, {"bload_enc",Pbload_enc,1}, #endif @@ -108,11 +118,31 @@ struct ftab file_tab[] = { {"bload_compat",Pbload_compat,1}, {"bsave_cmo",Pbsave_cmo,2}, {"bload_cmo",Pbload_cmo,1}, + {"getpid",Pgetpid,0}, {0,0,0}, }; static FILE *file_ptrs[BUFSIZ]; +void Pfprintf(NODE arg,pointer *rp) +{ + FILE *fp; + STRING s; + asir_assert(ARG0(arg),O_N,"fprintf"); + fp = file_ptrs[QTOS((Q)ARG0(arg))]; + if ( !fp ) { + error("fprintf : invalid argument"); + } + arg = NEXT(arg); + if ( arg ) { + Psprintf(arg,&s); + fputs(BDY(s),fp); + /* fflush(fp); */ + } + *rp = 0; + return; +} + void Ppurge_stdin(Q *rp) { purge_stdin(stdin); @@ -131,11 +161,19 @@ void Popen_file(NODE arg,Q *rp) if ( i == BUFSIZ ) error("open_file : too many open files"); name = BDY((STRING)ARG0(arg)); - if ( argc(arg) == 2 ) { + if (strcmp(name,"unix://stdin") == 0) { + fp = stdin; + }else if (strcmp(name,"unix://stdout") == 0) { + fp = stdout; + }else if (strcmp(name,"unix://stderr") == 0) { + fp = stderr; + }else{ + if ( argc(arg) == 2 ) { asir_assert(ARG1(arg),O_STR,"open_file"); fp = fopen(name,BDY((STRING)ARG1(arg))); - } else - fp = fopen(name,"r"); + } else + fp = fopen(name,"rb"); + } if ( !fp ) { sprintf(errbuf,"open_file : cannot open \"%s\"",name); error(errbuf); @@ -204,6 +242,12 @@ void Pget_line(NODE arg,STRING *rp) fsetpos(fp,&head); str = (char *)MALLOC_ATOMIC(j+1); fgets(str,j+1,fp); + for ( i = 0; i < j; i++ ) + if ( str[i] == '\r' ) { + str[i] = '\n'; + str[i+1] = 0; + break; + } MKSTR(*rp,str); } else error("get_line : invalid argument"); @@ -227,28 +271,75 @@ void Pget_byte(NODE arg,Q *rp) error("get_byte : invalid argument"); } -void Pput_byte(NODE arg,Q *rp) +void Pget_word(NODE arg,Q *rp) { int i,c; FILE *fp; - asir_assert(ARG0(arg),O_N,"put_byte"); - asir_assert(ARG1(arg),O_N,"put_byte"); + asir_assert(ARG0(arg),O_N,"get_word"); i = QTOS((Q)ARG0(arg)); - c = QTOS((Q)ARG1(arg)); if ( fp = file_ptrs[i] ) { - putc(c,fp); + if ( feof(fp) ) { + error("get_word : end of file"); + return; + } + read_int(fp,&c); STOQ(c,*rp); } else + error("get_word : invalid argument"); +} + +void Pput_byte(NODE arg,Obj *rp) +{ + int i,j,c; + FILE *fp; + Obj obj; + TB tb; + + asir_assert(ARG0(arg),O_N,"put_byte"); + i = QTOS((Q)ARG0(arg)); + if ( !(fp = file_ptrs[i]) ) error("put_byte : invalid argument"); + + obj = (Obj)ARG1(arg); + if ( !obj || OID(obj) == O_N ) { + c = QTOS((Q)obj); + putc(c,fp); + } else if ( OID(obj) == O_STR ) + fputs(BDY((STRING)obj),fp); + else if ( OID(obj) == O_TB ) { + tb = (TB)obj; + for ( j = 0; j < tb->next; j++ ) + fputs(tb->body[j],fp); + } + *rp = obj; } +void Pput_word(NODE arg,Obj *rp) +{ + int i,c; + FILE *fp; + Obj obj; + + asir_assert(ARG0(arg),O_N,"put_word"); + asir_assert(ARG1(arg),O_N,"put_word"); + i = QTOS((Q)ARG0(arg)); + if ( !(fp = file_ptrs[i]) ) + error("put_word : invalid argument"); + + obj = (Q)ARG1(arg); + c = QTOS((Q)obj); + write_int(fp,&c); + *rp = obj; +} + void Pload(NODE arg,Q *rp) { int ret = 0; char *name,*name0; char errbuf[BUFSIZ]; + if ( !arg ) error("load : invalid argument"); if ( ARG0(arg) ) { switch (OID(ARG0(arg))) { case O_STR: @@ -258,11 +349,7 @@ void Pload(NODE arg,Q *rp) sprintf(errbuf,"load : \"%s\" not found in the search path",name0); error(errbuf); } - ret = loadfile(name); - if ( !ret ) { - sprintf(errbuf,"load : \"%s\" could not be loaded",name); - error(errbuf); - } + execasirfile(name); break; default: error("load : invalid argument"); @@ -272,20 +359,31 @@ void Pload(NODE arg,Q *rp) STOQ(ret,*rp); } -void Pload_exec(NODE arg,Q *rp) +NODE imported_files; + +void Pimport(NODE arg,Q *rp) { - int ret; - char *name0,*name; + char *name; + NODE t,p,opt; - name0 = BDY((STRING)ARG0(arg)); - searchasirpath(name0,&name); - if ( !name ) - ret = -1; - else { - load_and_execfile(name); - ret = 0; + name = BDY((STRING)ARG0(arg)); + for ( t = imported_files; t; t = NEXT(t) ) + if ( !strcmp((char *)BDY(t),name) ) break; + if ( !t ) { + Pload(arg,rp); + MKNODE(t,name,imported_files); + imported_files = t; + return; + } else if ( current_option ) { + for ( opt = current_option; opt; opt = NEXT(opt) ) { + p = BDY((LIST)BDY(opt)); + if ( !strcmp(BDY((STRING)BDY(p)),"reimport") && BDY(NEXT(p)) ) { + Pload(arg,rp); + return; + } + } } - STOQ(ret,*rp); + *rp = 0; } void Pwhich(NODE arg,STRING *rp) @@ -323,7 +421,7 @@ void Ploadfiles(NODE arg,Q *rp) void Poutput(NODE arg,Q *rp) { -#if PARI +#if defined(PARI) extern FILE *outfile; #endif FILE *fp; @@ -341,7 +439,7 @@ void Poutput(NODE arg,Q *rp) error("output : invalid filename"); break; } -#if PARI +#if defined(PARI) pari_outfile = #endif asir_out = fp; @@ -417,24 +515,34 @@ void Pbload_cmo(NODE arg,Obj *rp) static struct oSTRING rootdir; -#if defined(VISUAL) +#if defined(VISUAL) || defined(__MINGW32__) +static void chop_delim(char *s) +{ + int n=strlen(s); + if(n>1 && s[n-1]=='\\') { + s[n-1]=0; + } +} + void get_rootdir(char *name,int len) { LONG ret; HKEY hOpenKey; DWORD Type; - char *slash; + char drive[_MAX_DRIVE], dir0[_MAX_DIR], dir[_MAX_DIR]; + char fullname[_MAX_PATH]; if ( rootdir.body ) { - strcpy(name,rootdir.body); + strcpy_s(name,len,rootdir.body); return; } - - if ( access("UseCurrentDir",0) >= 0 ) { - GetCurrentDirectory(BUFSIZ,name); - slash = strrchr(name,'\\'); - if ( slash ) - *slash = 0; + if ( GetModuleFileName(NULL,fullname,_MAX_PATH) ) { + _splitpath(fullname,drive,dir0,NULL,NULL); + chop_delim(dir0); + _splitpath(dir0,NULL,dir,NULL,NULL); + chop_delim(dir); + strcpy_s(name,len,drive); + strcat_s(name,len,dir); return; } name[0] = 0; @@ -447,10 +555,11 @@ void get_rootdir(char *name,int len) RegQueryValueEx(hOpenKey, "Directory", NULL, &Type, name, &len); RegCloseKey(hOpenKey); } else { - GetCurrentDirectory(len,name); - slash = strrchr(name,'\\'); - if ( slash ) - *slash = 0; + GetCurrentDirectory(_MAX_PATH,fullname); + _splitpath(fullname,drive,dir,NULL,NULL); + chop_delim(dir); + strcpy_s(name,len,drive); + strcat_s(name,len,dir); } } @@ -496,7 +605,19 @@ void Pget_rootdir(STRING *rp) *rp = &rootdir; } -#if defined(VISUAL) && defined(DES_ENC) +void Pgetpid(Q *rp) +{ + int id; + +#if defined(VISUAL) || defined(__MINGW32__) + id = GetCurrentProcessId(); +#else + id = getpid(); +#endif + STOQ(id,*rp); +} + +#if defined(DES_ENC) void Pbsave_enc(NODE arg,Obj *rp) { init_deskey(); @@ -592,17 +713,13 @@ void Premove_file(NODE arg,Q *rp) void Paccess(NODE arg,Q *rp) { -#if defined(VISUAL) - if ( access(BDY((STRING)ARG0(arg)),04) >= 0 ) -#else if ( access(BDY((STRING)ARG0(arg)),R_OK) >= 0 ) -#endif *rp = ONE; else *rp = 0; } -#if defined(VISUAL) +#if defined(VISUAL) || defined(__MINGW32__) int process_id() { return GetCurrentProcessId();