=================================================================== RCS file: /home/cvs/OpenXM_contrib2/asir2000/io/ox_asir.c,v retrieving revision 1.2 retrieving revision 1.13 diff -u -p -r1.2 -r1.13 --- OpenXM_contrib2/asir2000/io/ox_asir.c 1999/12/22 07:01:39 1.2 +++ OpenXM_contrib2/asir2000/io/ox_asir.c 2000/03/19 12:35:20 1.13 @@ -1,10 +1,15 @@ -/* $OpenXM: OpenXM_contrib2/asir2000/io/ox_asir.c,v 1.1.1.1 1999/12/03 07:39:11 noro Exp $ */ +/* $OpenXM: OpenXM_contrib2/asir2000/io/ox_asir.c,v 1.12 2000/03/16 08:23:16 noro Exp $ */ #include "ca.h" #include "parse.h" +#include "signal.h" #include "ox.h" #include "version.h" +#if PARI +#include "genpari.h" +#endif void ox_usr1_handler(); +int asir_ox_init(); extern jmp_buf environnement; @@ -13,7 +18,10 @@ extern int ox_flushing; extern jmp_buf ox_env; extern MATHCAP my_mathcap; +extern int little_endian,ox_sock_id; + int ox_sock_id; +int lib_ox_need_conv; static int asir_OperandStackSize; static Obj *asir_OperandStack; @@ -34,9 +42,9 @@ static void asir_popString(); static void asir_popCMO(unsigned int); static void asir_popSerializedLocalObject(); static LIST asir_GetErrorList(); -static char *name_of_cmd(unsigned int); +static char *name_of_cmd(int); static char *name_of_id(int); -static void asir_do_cmd(unsigned int,unsigned int); +static void asir_do_cmd(int,unsigned int); #if MPI extern int mpi_nprocs,mpi_myid; @@ -82,7 +90,7 @@ static void create_error(ERR *err,unsigned int serial, void ox_main(int argc,char **argv) { int id; - unsigned int cmd; + int cmd; Obj obj; USINT ui; ERR err; @@ -152,7 +160,7 @@ void ox_main(int argc,char **argv) { } } -static void asir_do_cmd(unsigned int cmd,unsigned int serial) +static void asir_do_cmd(int cmd,unsigned int serial) { MATHCAP client_mathcap; Q q; @@ -234,7 +242,7 @@ static char *name_of_id(int id) } } -static char *name_of_cmd(unsigned cmd) +static char *name_of_cmd(int cmd) { switch ( cmd ) { case SM_popSerializedLocalObject: @@ -456,11 +464,23 @@ static void asir_executeFunction(int serial) char *path; USINT ui; ERR err; + Obj arg; static char buf[BUFSIZ]; - func = ((STRING)asir_pop_one())->body; - argc = (int)(((USINT)asir_pop_one())->body); + arg = asir_pop_one(); + if ( !arg || OID(arg) != O_STR ) { + sprintf(buf,"executeFunction : invalid function name"); + goto error; + } else + func = ((STRING)arg)->body; + arg = asir_pop_one(); + if ( !arg || OID(arg) != O_USINT ) { + sprintf(buf,"executeFunction : invalid argc"); + goto error; + } else + argc = (int)(((USINT)arg)->body); + for ( n = 0; argc; argc-- ) { NEXTNODE(n,n1); BDY(n1) = (pointer)asir_pop_one(); @@ -491,13 +511,18 @@ static void asir_executeFunction(int serial) searchf(usrf,func,&f); if ( !f ) { sprintf(buf,"executeFunction : the function %s not found",func); - create_error(&err,serial,buf); - result = (Obj)err; + goto error; } else { result = (Obj)bevalf(f,n); } } asir_push_one(result); + return; + +error: + create_error(&err,serial,buf); + result = (Obj)err; + asir_push_one(result); } static void asir_end_flush() @@ -549,6 +574,8 @@ static void ox_asir_init(int argc,char **argv) char *getenv(); static ox_asir_initialized = 0; FILE *ifp; + char *homedir; + char *ptr; #if !defined(VISUAL) && !MPI do_server_in_X11 = 1; /* XXX */ @@ -587,11 +614,23 @@ static void ox_asir_init(int argc,char **argv) #if defined(UINIT) reg_sysf(); #endif +/* if ASIR_CONFIG is set, execute it; else execute .asirrc */ + if ( ptr = getenv("ASIR_CONFIG") ) + strcpy(ifname,ptr); + else { #if defined(THINK_C) - sprintf(ifname,"asirrc"); + sprintf(ifname,"asirrc"); #else - sprintf(ifname,"%s/.asirrc",getenv("HOME")); + homedir = getenv("HOME"); + if ( !homedir ) { + char rootname[BUFSIZ]; + + get_rootdir(rootname,sizeof(rootname)); + homedir = rootname; + } + sprintf(ifname,"%s/.asirrc",homedir); #endif + } if ( do_asirrc && (ifp = fopen(ifname,"r")) ) { input_init(ifp,ifname); if ( !setjmp(env) ) { @@ -610,7 +649,6 @@ static void ox_asir_init(int argc,char **argv) static void ox_io_init() { unsigned char c,rc; - extern int little_endian,ox_sock_id; endian_init(); #if defined(VISUAL) @@ -637,4 +675,203 @@ static void ox_io_init() { write_char(iofp[0].out,&c); ox_flush_stream_force(0); read_char(iofp[0].in,&rc); iofp[0].conv = c == rc ? 0 : 1; +} + +/* + * Library mode functions + */ + +/* + * Converts a binary encoded CMO into a risa object + * and pushes it onto the stack. + */ + +void asir_ox_push_cmo(void *cmo) +{ + Obj obj; + + ox_copy_init(cmo); + ox_buf_to_obj_as_cmo(&obj); + asir_push_one(obj); +} + +/* + * Pop an object from the stack and converts it + * int a binary encoded CMO. + */ + +int asir_ox_pop_cmo(void *cmo, int limit) +{ + Obj obj; + int len; + ERR err; + + obj = asir_pop_one(); + if ( !valid_as_cmo(obj) ) { + asir_push_one(obj); + create_error(&err,0,"The object at the stack top is invalid as a CMO."); + obj = (Obj)err; + } + len = count_as_cmo(obj); + if ( len <= limit ) { + ox_copy_init(cmo); + ox_obj_to_buf_as_cmo(obj); + return len; + } else + return -1; +} + +/* + * Executes an SM command. + */ + +void asir_ox_push_cmd(int cmd) +{ + int ret; + ERR err; + extern char LastError[]; + + if ( ret = setjmp(env) ) { + asir_reset_handler(); + if ( ret == 1 ) { + create_error(&err,0,LastError); /* XXX */ + asir_push_one((Obj)err); + } + } else { + asir_save_handler(); + asir_set_handler(); + asir_do_cmd(cmd,0); + asir_reset_handler(); + } +} + +/* + * Executes a string written in Asir. + */ + +void asir_ox_execute_string(char *s) +{ + STRING str; + int ret; + ERR err; + extern char LastError[]; + + MKSTR(str,s); + asir_push_one((Obj)str); + if ( ret = setjmp(env) ) { + asir_reset_handler(); + if ( ret == 1 ) { + create_error(&err,0,LastError); /* XXX */ + asir_push_one((Obj)err); + } + } else { + asir_save_handler(); + asir_set_handler(); + asir_executeString(); + asir_reset_handler(); + } +} + +/* + * Returns the size as a CMO of the object + * at the top of the stack. + */ + +int asir_ox_peek_cmo_size() +{ + Obj obj; + int len; + + obj = asir_pop_one(); + asir_push_one(obj); + if ( !valid_as_cmo(obj) ) { + fprintf(stderr,"The object at the stack top is invalid as a CMO.\n"); + return 0; + } + len = count_as_cmo(obj); + return len; +} + +/* + * Initialization. + * byteorder=0 => native + * =1 => network byte order + */ + +int asir_ox_init(int byteorder) +{ + int tmp; + char ifname[BUFSIZ]; + extern int GC_dont_gc; + extern int read_exec_file; + extern int do_asirrc; + extern int do_server_in_X11; + char *getenv(); + static ox_asir_initialized = 0; + FILE *ifp; + +#if !defined(VISUAL) && !MPI + do_server_in_X11 = 0; /* XXX */ +#endif + asir_save_handler(); +#if PARI + risa_pari_init(); +#endif + srandom((int)get_current_time()); + +#if defined(THINK_C) + param_init(); +#endif + StackBottom = &tmp + 1; /* XXX */ + rtime_init(); + env_init(); + endian_init(); +#if !defined(VISUAL) && !defined(THINK_C) +/* check_key(); */ +#endif + GC_init(); +/* process_args(argc,argv); */ + output_init(); + arf_init(); + nglob_init(); + glob_init(); + sig_init(); + tty_init(); + debug_init(); + pf_init(); + sysf_init(); + parif_init(); +#if defined(VISUAL) + init_socket(); +#endif +#if defined(UINIT) + reg_sysf(); +#endif +#if defined(THINK_C) + sprintf(ifname,"asirrc"); +#else + sprintf(ifname,"%s/.asirrc",getenv("HOME")); +#endif + if ( do_asirrc && (ifp = fopen(ifname,"r")) ) { + input_init(ifp,ifname); + if ( !setjmp(env) ) { + read_exec_file = 1; + read_eval_loop(); + read_exec_file = 0; + } + fclose(ifp); + } + input_init(0,"string"); + + asir_OperandStackSize = BUFSIZ; + asir_OperandStack = (Obj *)CALLOC(asir_OperandStackSize,sizeof(Obj)); + asir_OperandStackPtr = -1; + if ( little_endian && byteorder ) + lib_ox_need_conv = 1; + else + lib_ox_need_conv = 0; + do_message = 0; + create_my_mathcap("ox_asir"); + asir_reset_handler(); + return 0; }