=================================================================== RCS file: /home/cvs/OpenXM_contrib2/asir2000/builtin/file.c,v retrieving revision 1.14 retrieving revision 1.23 diff -u -p -r1.14 -r1.23 --- OpenXM_contrib2/asir2000/builtin/file.c 2001/08/20 09:03:24 1.14 +++ OpenXM_contrib2/asir2000/builtin/file.c 2006/09/13 02:26:13 1.23 @@ -45,19 +45,23 @@ * 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.13 2001/03/21 23:49:34 noro Exp $ + * $OpenXM: OpenXM_contrib2/asir2000/builtin/file.c,v 1.22 2006/02/08 02:11:19 noro Exp $ */ #include "ca.h" #include "parse.h" +#include "ox.h" #include "base.h" +#if !defined(VISUAL) #include "unistd.h" -#if PARI +#endif +#if defined(PARI) #include "genpari.h" #endif #if defined(VISUAL) #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" @@ -73,7 +77,7 @@ void Pbsave_compat(), Pbload_compat(); void Pbsave_cmo(), Pbload_cmo(); void Popen_file(), Pclose_file(), Pget_line(), Pget_byte(), Pput_byte(); void Ppurge_stdin(); -void Pload_exec(); +void Pimport(); extern int des_encryption; extern char *asir_libdir; @@ -88,7 +92,7 @@ struct ftab file_tab[] = { {"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}, @@ -109,16 +113,13 @@ struct ftab file_tab[] = { static FILE *file_ptrs[BUFSIZ]; -void Ppurge_stdin(rp) -Q *rp; +void Ppurge_stdin(Q *rp) { purge_stdin(stdin); *rp = 0; } -void Popen_file(arg,rp) -NODE arg; -Q *rp; +void Popen_file(NODE arg,Q *rp) { char *name; FILE *fp; @@ -130,11 +131,19 @@ 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 + } else fp = fopen(name,"r"); + } if ( !fp ) { sprintf(errbuf,"open_file : cannot open \"%s\"",name); error(errbuf); @@ -143,9 +152,7 @@ Q *rp; STOQ(i,*rp); } -void Pclose_file(arg,rp) -NODE arg; -Q *rp; +void Pclose_file(NODE arg,Q *rp) { int i; @@ -159,9 +166,7 @@ Q *rp; *rp = ONE; } -void Pget_line(arg,rp) -NODE arg; -STRING *rp; +void Pget_line(NODE arg,STRING *rp) { int i,j,c; FILE *fp; @@ -212,9 +217,7 @@ STRING *rp; error("get_line : invalid argument"); } -void Pget_byte(arg,rp) -NODE arg; -Q *rp; +void Pget_byte(NODE arg,Q *rp) { int i,c; FILE *fp; @@ -232,32 +235,39 @@ Q *rp; error("get_byte : invalid argument"); } -void Pput_byte(arg,rp) -NODE arg; -Q *rp; +void Pput_byte(NODE arg,Obj *rp) { - int i,c; + int i,j,c; FILE *fp; + Obj obj; + TB tb; asir_assert(ARG0(arg),O_N,"put_byte"); - asir_assert(ARG1(arg),O_N,"put_byte"); i = QTOS((Q)ARG0(arg)); - c = QTOS((Q)ARG1(arg)); - if ( fp = file_ptrs[i] ) { - putc(c,fp); - STOQ(c,*rp); - } else + 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 Pload(arg,rp) -NODE arg; -Q *rp; +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: @@ -267,11 +277,7 @@ 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"); @@ -281,25 +287,34 @@ Q *rp; STOQ(ret,*rp); } -void Pload_exec(arg,rp) -NODE arg; -Q *rp; +NODE imported_files; + +void Pimport(NODE arg,Q *rp) { - int ret = 0; - char *name0,*name; + char *name; + NODE t,p,opt; - name0 = BDY((STRING)ARG0(arg)); - searchasirpath(name0,&name); - if ( !name ) - ret = -1; - else - ret = load_and_execfile(name); - STOQ(ret,*rp); + 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; + } + } + } + *rp = 0; } -void Pwhich(arg,rp) -NODE arg; -STRING *rp; +void Pwhich(NODE arg,STRING *rp) { char *name; STRING str; @@ -318,9 +333,7 @@ STRING *rp; *rp = 0; } -void Ploadfiles(arg,rp) -NODE arg; -Q *rp; +void Ploadfiles(NODE arg,Q *rp) { int ret; @@ -334,11 +347,9 @@ Q *rp; STOQ(ret,*rp); } -void Poutput(arg,rp) -NODE arg; -Q *rp; +void Poutput(NODE arg,Q *rp) { -#if PARI +#if defined(PARI) extern FILE *outfile; #endif FILE *fp; @@ -356,7 +367,7 @@ Q *rp; error("output : invalid filename"); break; } -#if PARI +#if defined(PARI) pari_outfile = #endif asir_out = fp; @@ -365,9 +376,7 @@ Q *rp; extern int ox_file_io; -void Pbsave(arg,rp) -NODE arg; -Q *rp; +void Pbsave(NODE arg,Q *rp) { FILE *fp; VL vl,t; @@ -388,9 +397,7 @@ Q *rp; *rp = ONE; } -void Pbload(arg,rp) -NODE arg; -Obj *rp; +void Pbload(NODE arg,Obj *rp) { FILE *fp; @@ -405,12 +412,9 @@ Obj *rp; ox_file_io = 0; } -void Pbsave_cmo(arg,rp) -NODE arg; -Q *rp; +void Pbsave_cmo(NODE arg,Q *rp) { FILE *fp; - VL vl,t; asir_assert(ARG1(arg),O_STR,"bsave_cmo"); fp = fopen(BDY((STRING)ARG1(arg)),"wb"); @@ -423,9 +427,7 @@ Q *rp; *rp = ONE; } -void Pbload_cmo(arg,rp) -NODE arg; -Obj *rp; +void Pbload_cmo(NODE arg,Obj *rp) { FILE *fp; @@ -442,9 +444,7 @@ Obj *rp; static struct oSTRING rootdir; #if defined(VISUAL) -void get_rootdir(name,len) -char *name; -int len; +void get_rootdir(char *name,int len) { LONG ret; HKEY hOpenKey; @@ -480,8 +480,7 @@ int len; } } -void set_rootdir(name) -char *name; +void set_rootdir(char *name) { static char DirName[BUFSIZ]; @@ -494,15 +493,12 @@ char *name; } #else -void get_rootdir(name,len) -char *name; -int len; +void get_rootdir(char *name,int len) { strcpy(name,asir_libdir); } -void set_rootdir(name) -char *name; +void set_rootdir(char *name) { static char DirName[BUFSIZ]; @@ -514,11 +510,9 @@ char *name; #endif -void Pget_rootdir(rp) -STRING *rp; +void Pget_rootdir(STRING *rp) { static char DirName[BUFSIZ]; - int DirNameLen; if ( !rootdir.body ) { get_rootdir(DirName,sizeof(DirName)); @@ -529,9 +523,7 @@ STRING *rp; } #if defined(VISUAL) && defined(DES_ENC) -void Pbsave_enc(arg,rp) -NODE arg; -Obj *rp; +void Pbsave_enc(NODE arg,Obj *rp) { init_deskey(); des_encryption = 1; @@ -539,9 +531,7 @@ Obj *rp; des_encryption = 0; } -void Pbload_enc(arg,rp) -NODE arg; -Obj *rp; +void Pbload_enc(NODE arg,Obj *rp) { init_deskey(); des_encryption = 1; @@ -550,9 +540,7 @@ Obj *rp; } #endif -void Pbload27(arg,rp) -NODE arg; -Obj *rp; +void Pbload27(NODE arg,Obj *rp) { FILE *fp; Obj r; @@ -567,9 +555,7 @@ Obj *rp; bobjtoobj(BASE27,r,rp); } -void Pbsave_compat(arg,rp) -NODE arg; -Q *rp; +void Pbsave_compat(NODE arg,Q *rp) { FILE *fp; VL vl,t; @@ -590,9 +576,7 @@ Q *rp; *rp = ONE; } -void Pbload_compat(arg,rp) -NODE arg; -Obj *rp; +void Pbload_compat(NODE arg,Obj *rp) { FILE *fp; unsigned int hdr[2]; @@ -626,21 +610,14 @@ Obj *rp; fclose(fp); } -void Premove_file(arg,rp) -NODE arg; -Q *rp; +void Premove_file(NODE arg,Q *rp) { unlink((char *)BDY((STRING)ARG0(arg))); *rp = ONE; } -void Paccess(arg,rp) -NODE arg; -Q *rp; +void Paccess(NODE arg,Q *rp) { - char *name; - STRING str; - #if defined(VISUAL) if ( access(BDY((STRING)ARG0(arg)),04) >= 0 ) #else @@ -657,9 +634,7 @@ int process_id() return GetCurrentProcessId(); } -void call_exe(name,av) -char *name; -char **av; +void call_exe(char *name,char **av) { _spawnv(_P_WAIT,name,av); }