=================================================================== RCS file: /home/cvs/OpenXM/src/kxx/oxserver00.c,v retrieving revision 1.1.1.1 retrieving revision 1.23 diff -u -p -r1.1.1.1 -r1.23 --- OpenXM/src/kxx/oxserver00.c 1999/10/08 02:12:13 1.1.1.1 +++ OpenXM/src/kxx/oxserver00.c 2016/03/31 05:27:34 1.23 @@ -1,3 +1,4 @@ +/* $OpenXM: OpenXM/src/kxx/oxserver00.c,v 1.22 2015/10/10 11:29:46 takayama Exp $ */ /* nullserver01 */ #include #include @@ -7,6 +8,7 @@ #include #include #include +#include /* -lnsl -lsocket /usr/ucblib/libucb.a */ #include "ox_kan.h" #include "serversm.h" @@ -14,14 +16,22 @@ int OxCritical = 0; int OxInterruptFlag = 0; int PacketMonitor = 0; +int NoExecution = 0; extern int SerialOX; /* Serial number of the packets sent. */ extern int SerialCurrent; /* Current Serial number of the recieved packet. */ extern int OXprintMessage; /* print oxmessages? */ +extern int Calling_ctrlC_hook; +extern int RestrictedMode, RestrictedMode_saved; +#if defined(__CYGWIN__) || defined(__MSYS__) +sigjmp_buf EnvOfChildServer; +#else jmp_buf EnvOfChildServer; +#endif int JmpMessage = 0; +extern int Lisplike; static char *getSuffix(char *s); main(int argc, char *argv[]) { @@ -32,12 +42,18 @@ main(int argc, char *argv[]) { char *tigers[] = {"callsm1.sm1","tigers.sm1"}; char *basicCD[] = {"basicCD.sm1"}; /* If you change the above, you need to change the argc of Sm1_start below. - */ + */ if (argc > 1) { if (strcmp(argv[1],"-monitor")==0) { fprintf(stderr,"Taking the packet monitor.\n"); PacketMonitor = 1; + }else if (strcmp(argv[1],"-lispLike")==0) { + fprintf(stderr,"Output lispLike expression.\n"); + Lisplike = 1; + }else if (strcmp(argv[1],"-noexec")==0) { + fprintf(stderr,"I do not execute commands.\n"); + NoExecution = 1; }else{ fprintf(stderr,"Unknown option. Possible options are -monitor\n"); } @@ -58,14 +74,18 @@ main(int argc, char *argv[]) { Sm1_start(0, NULL, "ox_sm1"); } - nullserver(3); + nullserver(3,4); } static char *getSuffix(char *s) { /* getSuffix("ox_sm1_forAsir") returns "forAsir" */ + /* getSuffix("ox_sm1_forAsir.exe") returns "forAsir" */ /* getSuffix("ox_sm1_gnuplot") returns "gnuplot" */ int n,i,k; n = strlen(s); + if (n > 5 && (strcmp(".exe",&(s[n-4]))==0 || strcmp(".EXE",&(s[n-4]))==0)) { + s[n-4] = '\0'; + } for (i=n-1; i>=0; i--) { if (s[i] == '_') { return( s+i+1 ); @@ -73,79 +93,120 @@ static char *getSuffix(char *s) { } return(s); } -nullserver(int fdStream) { +nullserver(int fdStreamIn,int fdStreamOut) { int mtag; int message = 1; - ox_stream ostream; + ox_stream ostreamIn; + ox_stream ostreamOut; char sreason[1024]; extern void controlResetHandler(); +#if defined(__CYGWIN__) || defined(__MSYS__) + extern sigjmp_buf EnvOfStackMachine; +#else extern jmp_buf EnvOfStackMachine; +#endif int engineByteOrder; - engineByteOrder = oxTellMyByteOrder(fdStream); + /* for debug, use -monitor + PacketMonitor = 1; */ + + fflush(NULL); + engineByteOrder = oxTellMyByteOrder(fdStreamOut,fdStreamIn); /* Set the network byte order. */ fprintf(stderr,"engineByteOrder=%x\n",engineByteOrder); - if (fdStream != -1) { - ostream = fp2open(fdStream); - if (ostream == NULL) { - fprintf(stderr,"fp2open(fdStream) failed.\n"); - fdStream = -1; + if (fdStreamIn != -1) { + ostreamIn = fp2open(fdStreamIn); + if (ostreamIn == NULL) { + fprintf(stderr,"fp2open(fdStreamIn) failed.\n"); + fdStreamIn = -1; } - if (PacketMonitor) fp2watch(ostream,stdout); + if (PacketMonitor) fp2watch(ostreamIn,stdout); } + if (fdStreamOut != -1) { + ostreamOut = fp2open(fdStreamOut); + if (ostreamOut == NULL) { + fprintf(stderr,"fp2open(fdStreamOut) failed.\n"); + fdStreamOut = -1; + } + if (PacketMonitor) fp2watch(ostreamOut,stdout); + } + + /* fprintf(stderr,"Hello world.\n"); OXprintMessage = 1; + JmpMessage = 1; */ + aaa : ; - if (setjmp(EnvOfChildServer)) { +#if defined(__CYGWIN__) + if (MYSIGSETJMP(EnvOfChildServer,1)) { +#else + if (MYSETJMP(EnvOfChildServer)) { +#endif fprintf(stderr,"childServerMain: jump here.\n"); if (OxInterruptFlag == 0) { fprintf(stderr," ?! \n"); fflush(NULL); } - signal(SIGUSR1,controlResetHandler); goto aaa; + if (!Calling_ctrlC_hook) { + Calling_ctrlC_hook = 1; RestrictedMode = 0; + KSexecuteString(" ctrlC-hook "); /* Execute User Defined functions. */ + RestrictedMode = RestrictedMode_saved; + } + Calling_ctrlC_hook = 0; + KSexecuteString(" (Computation is interrupted.) "); + mysignal(SIGUSR1,controlResetHandler); goto aaa; } else { if (JmpMessage) fprintf(stderr,"Set EnvOfChildServer.\n"); - signal(SIGUSR1,controlResetHandler); + mysignal(SIGUSR1,controlResetHandler); } - - if (setjmp(EnvOfStackMachine)) { - fprintf(stderr,"childServerMain: jump here by EnvOfStackMachine.\n"); +#if defined(__CYGWIN__) + if (MYSIGSETJMP(EnvOfStackMachine,1)) { +#else + if (MYSETJMP(EnvOfStackMachine)) { +#endif + fprintf(stderr,"childServerMain: jump here by EnvOfStackMachine or timeout.\n"); if (OxInterruptFlag == 0) { fprintf(stderr," ?! \n"); fflush(NULL); } /* In case of error in the stack machine, pop the error info and send the error packet. */ /* oxSendOXheader(ostream,OX_DATA,SerialOX++); - oxSendCmoError(ostream); - oxSendOXheader(ostream,OX_DATA,SerialOX++); - sprintf(sreason,"Jump here by sm1 error."); - oxSendCmoError2(ostream,sreason); + oxSendCmoError(ostream); + oxSendOXheader(ostream,OX_DATA,SerialOX++); + sprintf(sreason,"Jump here by sm1 error."); + oxSendCmoError2(ostream,sreason); */ Sm1_pushError2(SerialCurrent,-1,"Global jump by sm1 error"); - signal(SIGUSR1,controlResetHandler); goto aaa ; + if (!Calling_ctrlC_hook) { + Calling_ctrlC_hook = 1; RestrictedMode = 0; + KSexecuteString(" ctrlC-hook "); /* Execute User Defined functions. */ + RestrictedMode = RestrictedMode_saved; + } + Calling_ctrlC_hook = 0; + mysignal(SIGUSR1,controlResetHandler); goto aaa ; } else { if (JmpMessage) fprintf(stderr,"Set EnvOfStackMachine.\n"); - if (signal(SIGUSR1,SIG_IGN) != SIG_IGN) { - signal(SIGUSR1,controlResetHandler); + if (mysignal(SIGUSR1,SIG_IGN) != SIG_IGN) { + mysignal(SIGUSR1,controlResetHandler); } } while (1) { - message = OXprintMessage; + message = OXprintMessage; if (OxInterruptFlag) { OxCritical = 1; if (message) {fprintf(stderr,"Clearing the read buffer.\n");fflush(NULL); } - fp2clearReadBuf(ostream); /* clear the read buffer */ + fp2clearReadBuf(ostreamIn); /* clear the read buffer */ if (message) {fprintf(stderr,"Throwing OX_SYNC_BALL\n"); fflush(NULL);} - oxSendSyncBall(ostream); + oxSendSyncBall(ostreamOut); if (message) {fprintf(stderr,"Waiting for OX_SYNC_BALL\n");fflush(NULL);} - oxWaitSyncBall(ostream); + oxWaitSyncBall(ostreamIn); if (message) {fprintf(stderr,"Done changing OX_SYNC_BALL\n"); fflush(NULL);} OxInterruptFlag = 0; OxCritical = 0; goto aaa ; } OxCritical = 0; - if (fp2select(ostream,-1)) { + if (fp2select(ostreamIn,-1)) { /* If there is an data in the ostream, then read data in the buffer and read data in the communication stream. */ OxCritical = 1; @@ -154,7 +215,7 @@ nullserver(int fdStream) { /* This part is never reached. */ } OxCritical = 1; - mtag = oxGetOXheader(ostream,&SerialCurrent); /* get the message_tag */ + mtag = oxGetOXheader(ostreamIn,&SerialCurrent); /* get the message_tag */ if (message) { fprintf(stderr,"\nmtag is %d (serial=%d) : ",mtag,SerialCurrent); switch(mtag) { @@ -162,18 +223,18 @@ nullserver(int fdStream) { case OX_DATA: fprintf(stderr," OX_DATA \n"); break; case OX_SYNC_BALL: fprintf(stderr," OX_SYNC_BALL \n"); break; case -1: fprintf(stderr," End of file. Exiting the server child.\n"); - exit(); break; + exit(0); break; default: fprintf(stderr," ?! \n"); break; } } /*sleep(2); /* for dubug OX_SYNC_BALL */ switch(mtag) { case OX_COMMAND: - nullserverCommand(ostream); + nullserverCommand(ostreamIn,ostreamOut); goto aaa ; /* We always reset the jump vector. cf. memo1.txt 1998 2/13*/ break; case OX_DATA: - Sm1_pushCMO(ostream); + Sm1_pushCMO(ostreamIn); break; case OX_SYNC_BALL: /* if (OxInterruptFlag) think about it later. */ @@ -185,7 +246,7 @@ nullserver(int fdStream) { } } -nullserverCommand(ox_stream ostream) { +nullserverCommand(ox_stream ostreamIn,ox_stream ostreamOut) { int id; int mtag; int n; @@ -197,51 +258,50 @@ nullserverCommand(ox_stream ostream) { char *emsg; extern void controlResetHandler(); - message = OXprintMessage; + message = OXprintMessage; /* message_body */ - id = oxGetInt32(ostream); /* get the function_id */ - if (message) {fprintf(stderr,"\nfunction_id is %d\n",id);} + id = oxGetInt32(ostreamIn); /* get the function_id */ + if (message) {fprintf(stderr,"\nfunction_id is %d; %s\n",id,oxFIDtoStr(id));} switch( id ) { case SM_mathcap: - if (message) fprintf(stderr," mathcap\n"); mathresult = (struct mathCap *)Sm1_mathcap(); oxPushMathCap(mathresult); break; case SM_setMathCap: - if (message) fprintf(stderr," setMathCap\n"); - Sm1_setMathCap(ostream); + Sm1_setMathCap(ostreamOut); break; case SM_pops: - if (message) fprintf(stderr," pops \n"); Sm1_pops(); break; case SM_getsp: - if (message) fprintf(stderr," getsp \n"); Sm1_getsp(); break; case SM_dupErrors: - if (message) fprintf(stderr," dupErrors \n"); Sm1_dupErrors(); break; + case SM_pushCMOtag: + Sm1_pushCMOtag(SerialCurrent); + break; case SM_setName: - if (message) fprintf(stderr," setName \n"); iresult = Sm1_setName(); if (iresult < 0) { Sm1_pushError2(SerialCurrent,-1,"setName"); } break; case SM_evalName: - if (message) fprintf(stderr," evalName \n"); iresult = Sm1_evalName(); if (iresult < 0) { Sm1_pushError2(SerialCurrent,-1,"evalName"); } break; case SM_executeStringByLocalParser: - if (message) fprintf(stderr," executeStringByLocalParser\n"); OxCritical = 0; - iresult = Sm1_executeStringByLocalParser(); - OxCritical = 1; signal(SIGUSR1,controlResetHandler); + if (NoExecution) { + iresult = 0; + }else{ + iresult = Sm1_executeStringByLocalParser(); + } + OxCritical = 1; mysignal(SIGUSR1,controlResetHandler); if (iresult < 0) { emsg = Sm1_popErrorMessage("executeString: "); Sm1_pushError2(SerialCurrent,-1,emsg); @@ -249,29 +309,47 @@ nullserverCommand(ox_stream ostream) { } break; case SM_executeFunction: - if (message) fprintf(stderr," executeFunction\n"); OxCritical = 0; - iresult = Sm1_executeStringByLocalParser(); - OxCritical = 1; signal(SIGUSR1,controlResetHandler); + if (NoExecution) { + iresult = 0; + }else{ + iresult = Sm1_executeStringByLocalParser(); + } + OxCritical = 1; mysignal(SIGUSR1,controlResetHandler); if (iresult < 0) { emsg = Sm1_popErrorMessage("executeFunction: "); Sm1_pushError2(SerialCurrent,-1,emsg); return(-1); } break; + case SM_executeFunctionWithOptionalArgument: + OxCritical = 0; + if (NoExecution) { + iresult = 0; + }else{ + iresult = Sm1_executeStringByLocalParser(); + } + OxCritical = 1; mysignal(SIGUSR1,controlResetHandler); + if (iresult < 0) { + emsg = Sm1_popErrorMessage("executeFunctionWithOptionalArgument: "); + Sm1_pushError2(SerialCurrent,-1,emsg); + return(-1); + } + break; case SM_popCMO: - if (message) fprintf(stderr,"popCMO. Start to sending data.\n",n); - oxSendOXheader(ostream,OX_DATA,SerialOX++); - n=Sm1_popCMO(ostream,SerialCurrent); + oxSendOXheader(ostreamOut,OX_DATA,SerialOX++); + n=Sm1_popCMO(ostreamOut,SerialCurrent); if (message) fprintf(stderr,"Done.\n"); break; case SM_popString: - if (message) fprintf(stderr,"popString. send data from the stack.\n",n); - oxSendOXheader(ostream,OX_DATA,SerialOX++); - oxSendCmoString(ostream,Sm1_popString()); + oxSendOXheader(ostreamOut,OX_DATA,SerialOX++); + oxSendCmoString(ostreamOut,Sm1_popString()); if (message) fprintf(stderr,"Done.\n"); break; case SM_shutdown: + fprintf(stderr,"Shutting down the engine.\n"); + exit(0); + break; case SM_beginBlock: case SM_endBlock: fprintf(stderr,"This command has not yet been implemented.\n"); @@ -290,21 +368,27 @@ nullserver_simplest(int fd) { int c; while(1) { c = readOneByte(fd); - if (c == '@') { return; } + if (c == '@') { return 0; } } } void controlResetHandler(sig) -int sig; + int sig; { - signal(sig,SIG_IGN); + mysignal(sig,SIG_IGN); + cancelAlarm(); fprintf(stderr,"From controlResetHandler. OxCritical = %d\n",OxCritical); OxInterruptFlag = 1; if (OxCritical) { return; }else{ - longjmp(EnvOfChildServer,2); /* returns 2 for ctrl-C */ + (void) traceShowStack(); traceClearStack(); +#if defined(__CYGWIN__) || defined(__MSYS__) + MYSIGLONGJMP(EnvOfChildServer,2); /* returns 2 for ctrl-C */ +#else + MYLONGJMP(EnvOfChildServer,2); /* returns 2 for ctrl-C */ +#endif } }