Annotation of OpenXM/src/kxx/oxserver00.c, Revision 1.14
1.14 ! takayama 1: /* $OpenXM: OpenXM/src/kxx/oxserver00.c,v 1.13 2003/11/20 11:41:44 takayama Exp $ */
1.1 maekawa 2: /* nullserver01 */
3: #include <stdio.h>
4: #include <sys/types.h>
5: #include <sys/socket.h>
6: #include <sys/time.h>
7: #include <netinet/in.h>
8: #include <netdb.h>
9: #include <signal.h>
10: #include <setjmp.h>
11: /* -lnsl -lsocket /usr/ucblib/libucb.a */
12: #include "ox_kan.h"
13: #include "serversm.h"
14:
15: int OxCritical = 0;
16: int OxInterruptFlag = 0;
17: int PacketMonitor = 0;
1.13 takayama 18: int NoExecution = 0;
1.1 maekawa 19:
20: extern int SerialOX; /* Serial number of the packets sent. */
21: extern int SerialCurrent; /* Current Serial number of the recieved packet. */
22: extern int OXprintMessage; /* print oxmessages? */
23:
1.7 takayama 24: #if defined(__CYGWIN__)
25: sigjmp_buf EnvOfChildServer;
26: #else
1.1 maekawa 27: jmp_buf EnvOfChildServer;
1.7 takayama 28: #endif
1.1 maekawa 29:
30: int JmpMessage = 0;
1.12 takayama 31: extern int Lisplike;
1.1 maekawa 32:
33: static char *getSuffix(char *s);
34: main(int argc, char *argv[]) {
35: char *s;
36: char *forAsir[] = {"callsm1.sm1","callsm1b.sm1"};
37: char *gnuplot[] = {"callsm1.sm1","gnuplot.sm1"};
38: char *phc[] = {"callsm1.sm1","phc.sm1"};
39: char *tigers[] = {"callsm1.sm1","tigers.sm1"};
40: char *basicCD[] = {"basicCD.sm1"};
41: /* If you change the above, you need to change the argc of Sm1_start below.
1.5 takayama 42: */
1.1 maekawa 43:
44: if (argc > 1) {
45: if (strcmp(argv[1],"-monitor")==0) {
46: fprintf(stderr,"Taking the packet monitor.\n");
47: PacketMonitor = 1;
1.12 takayama 48: }else if (strcmp(argv[1],"-lispLike")==0) {
49: fprintf(stderr,"Output lispLike expression.\n");
50: Lisplike = 1;
1.13 takayama 51: }else if (strcmp(argv[1],"-noexec")==0) {
52: fprintf(stderr,"I do not execute commands.\n");
53: NoExecution = 1;
1.1 maekawa 54: }else{
55: fprintf(stderr,"Unknown option. Possible options are -monitor\n");
56: }
57: }
58: /* Load initialization files if necessary. */
59: s = getSuffix(argv[0]);
60: if (strcmp(s,"forAsir") == 0) {
61: Sm1_start(2,forAsir,"ox_sm1_forAsir");
62: }else if (strcmp(s,"gnuplot") == 0) {
63: Sm1_start(2,gnuplot,"ox_sm1_gnuplot");
64: }else if (strcmp(s,"phc") == 0) {
65: Sm1_start(2,phc,"ox_sm1_phc");
66: }else if (strcmp(s,"tigers") == 0) {
67: Sm1_start(2,tigers,"ox_sm1_tigers");
68: }else if (strcmp(s,"basicCD") == 0) {
69: Sm1_start(1,basicCD,"basicCD");
70: }else { /* Default */
71: Sm1_start(0, NULL, "ox_sm1");
72: }
73:
1.11 takayama 74: nullserver(3,4);
1.1 maekawa 75: }
76:
77: static char *getSuffix(char *s) {
78: /* getSuffix("ox_sm1_forAsir") returns "forAsir" */
1.6 takayama 79: /* getSuffix("ox_sm1_forAsir.exe") returns "forAsir" */
1.1 maekawa 80: /* getSuffix("ox_sm1_gnuplot") returns "gnuplot" */
81: int n,i,k;
82: n = strlen(s);
1.6 takayama 83: if (n > 5 && (strcmp(".exe",&(s[n-4]))==0 || strcmp(".EXE",&(s[n-4]))==0)) {
84: s[n-4] = '\0';
85: }
1.1 maekawa 86: for (i=n-1; i>=0; i--) {
87: if (s[i] == '_') {
88: return( s+i+1 );
89: }
90: }
91: return(s);
92: }
1.11 takayama 93: nullserver(int fdStreamIn,int fdStreamOut) {
1.1 maekawa 94: int mtag;
95: int message = 1;
1.11 takayama 96: ox_stream ostreamIn;
97: ox_stream ostreamOut;
1.1 maekawa 98: char sreason[1024];
99: extern void controlResetHandler();
1.7 takayama 100: #if defined(__CYGWIN__)
101: extern sigjmp_buf EnvOfStackMachine;
102: #else
1.1 maekawa 103: extern jmp_buf EnvOfStackMachine;
1.7 takayama 104: #endif
1.1 maekawa 105: int engineByteOrder;
106:
1.12 takayama 107: /* for debug, use -monitor
108: PacketMonitor = 1; */
1.11 takayama 109:
1.4 takayama 110: fflush(NULL);
1.11 takayama 111: engineByteOrder = oxTellMyByteOrder(fdStreamOut,fdStreamIn);
1.1 maekawa 112: /* Set the network byte order. */
113: fprintf(stderr,"engineByteOrder=%x\n",engineByteOrder);
114:
1.11 takayama 115: if (fdStreamIn != -1) {
116: ostreamIn = fp2open(fdStreamIn);
117: if (ostreamIn == NULL) {
118: fprintf(stderr,"fp2open(fdStreamIn) failed.\n");
119: fdStreamIn = -1;
120: }
121: if (PacketMonitor) fp2watch(ostreamIn,stdout);
122: }
123: if (fdStreamOut != -1) {
124: ostreamOut = fp2open(fdStreamOut);
125: if (ostreamOut == NULL) {
126: fprintf(stderr,"fp2open(fdStreamOut) failed.\n");
127: fdStreamOut = -1;
1.1 maekawa 128: }
1.11 takayama 129: if (PacketMonitor) fp2watch(ostreamOut,stdout);
1.1 maekawa 130: }
1.11 takayama 131:
1.1 maekawa 132: aaa : ;
1.7 takayama 133: #if defined(__CYGWIN__)
134: if (sigsetjmp(EnvOfChildServer,1)) {
135: #else
1.1 maekawa 136: if (setjmp(EnvOfChildServer)) {
1.7 takayama 137: #endif
1.1 maekawa 138: fprintf(stderr,"childServerMain: jump here.\n");
139: if (OxInterruptFlag == 0) {
140: fprintf(stderr," ?! \n"); fflush(NULL);
141: }
1.9 takayama 142: KSexecuteString(" ctrlC-hook "); /* Execute User Defined functions. */
143: KSexecuteString(" (Computation is interrupted.) ");
1.1 maekawa 144: signal(SIGUSR1,controlResetHandler); goto aaa;
145: } else {
146: if (JmpMessage) fprintf(stderr,"Set EnvOfChildServer.\n");
147: signal(SIGUSR1,controlResetHandler);
148: }
1.7 takayama 149: #if defined(__CYGWIN__)
150: if (sigsetjmp(EnvOfStackMachine,1)) {
151: #else
1.1 maekawa 152: if (setjmp(EnvOfStackMachine)) {
1.7 takayama 153: #endif
1.9 takayama 154: fprintf(stderr,"childServerMain: jump here by EnvOfStackMachine or timeout.\n");
1.1 maekawa 155: if (OxInterruptFlag == 0) {
156: fprintf(stderr," ?! \n"); fflush(NULL);
157: }
158: /* In case of error in the stack machine, pop the error info
159: and send the error packet. */
160: /* oxSendOXheader(ostream,OX_DATA,SerialOX++);
1.5 takayama 161: oxSendCmoError(ostream);
162: oxSendOXheader(ostream,OX_DATA,SerialOX++);
163: sprintf(sreason,"Jump here by sm1 error.");
164: oxSendCmoError2(ostream,sreason);
1.1 maekawa 165: */
166: Sm1_pushError2(SerialCurrent,-1,"Global jump by sm1 error");
167:
1.9 takayama 168: KSexecuteString(" ctrlC-hook "); /* Execute User Defined functions. */
1.1 maekawa 169: signal(SIGUSR1,controlResetHandler); goto aaa ;
170: } else {
171: if (JmpMessage) fprintf(stderr,"Set EnvOfStackMachine.\n");
172: if (signal(SIGUSR1,SIG_IGN) != SIG_IGN) {
173: signal(SIGUSR1,controlResetHandler);
174: }
175: }
176:
177: while (1) {
1.11 takayama 178: message = OXprintMessage;
1.1 maekawa 179: if (OxInterruptFlag) {
180: OxCritical = 1;
181: if (message) {fprintf(stderr,"Clearing the read buffer.\n");fflush(NULL); }
1.11 takayama 182: fp2clearReadBuf(ostreamIn); /* clear the read buffer */
1.1 maekawa 183: if (message) {fprintf(stderr,"Throwing OX_SYNC_BALL\n"); fflush(NULL);}
1.11 takayama 184: oxSendSyncBall(ostreamOut);
1.1 maekawa 185: if (message) {fprintf(stderr,"Waiting for OX_SYNC_BALL\n");fflush(NULL);}
1.11 takayama 186: oxWaitSyncBall(ostreamIn);
1.1 maekawa 187: if (message) {fprintf(stderr,"Done changing OX_SYNC_BALL\n"); fflush(NULL);}
188: OxInterruptFlag = 0;
189: OxCritical = 0;
190: goto aaa ;
191: }
192: OxCritical = 0;
1.11 takayama 193: if (fp2select(ostreamIn,-1)) {
1.1 maekawa 194: /* If there is an data in the ostream, then read data in the buffer and
195: read data in the communication stream. */
196: OxCritical = 1;
197: }else{
198: /* interrupted system call */
199: /* This part is never reached. */
200: }
201: OxCritical = 1;
1.11 takayama 202: mtag = oxGetOXheader(ostreamIn,&SerialCurrent); /* get the message_tag */
1.1 maekawa 203: if (message) {
204: fprintf(stderr,"\nmtag is %d (serial=%d) : ",mtag,SerialCurrent);
205: switch(mtag) {
206: case OX_COMMAND: fprintf(stderr," OX_COMMAND \n"); break;
207: case OX_DATA: fprintf(stderr," OX_DATA \n"); break;
208: case OX_SYNC_BALL: fprintf(stderr," OX_SYNC_BALL \n"); break;
209: case -1: fprintf(stderr," End of file. Exiting the server child.\n");
1.10 ohara 210: exit(0); break;
1.1 maekawa 211: default: fprintf(stderr," ?! \n"); break;
212: }
213: }
214: /*sleep(2); /* for dubug OX_SYNC_BALL */
215: switch(mtag) {
216: case OX_COMMAND:
1.11 takayama 217: nullserverCommand(ostreamIn,ostreamOut);
1.1 maekawa 218: goto aaa ; /* We always reset the jump vector. cf. memo1.txt 1998 2/13*/
219: break;
220: case OX_DATA:
1.11 takayama 221: Sm1_pushCMO(ostreamIn);
1.1 maekawa 222: break;
223: case OX_SYNC_BALL:
224: /* if (OxInterruptFlag) think about it later. */
225: break;
226: default:
227: fprintf(stderr,"Fatal error in server.\n");
228: break;
229: }
230: }
231: }
232:
1.11 takayama 233: nullserverCommand(ox_stream ostreamIn,ox_stream ostreamOut) {
1.1 maekawa 234: int id;
235: int mtag;
236: int n;
237: char *name;
238: char *sresult;
239: struct mathCap *mathresult;
240: int iresult;
241: int message = 1;
242: char *emsg;
243: extern void controlResetHandler();
244:
1.11 takayama 245: message = OXprintMessage;
1.1 maekawa 246: /* message_body */
1.11 takayama 247: id = oxGetInt32(ostreamIn); /* get the function_id */
1.12 takayama 248: if (message) {fprintf(stderr,"\nfunction_id is %d; %s\n",id,oxFIDtoStr(id));}
1.1 maekawa 249: switch( id ) {
250: case SM_mathcap:
251: mathresult = (struct mathCap *)Sm1_mathcap();
252: oxPushMathCap(mathresult);
253: break;
254: case SM_setMathCap:
1.11 takayama 255: Sm1_setMathCap(ostreamOut);
1.1 maekawa 256: break;
257: case SM_pops:
258: Sm1_pops();
259: break;
260: case SM_getsp:
261: Sm1_getsp();
262: break;
263: case SM_dupErrors:
264: Sm1_dupErrors();
265: break;
1.3 takayama 266: case SM_pushCMOtag:
267: Sm1_pushCMOtag(SerialCurrent);
1.5 takayama 268: break;
1.1 maekawa 269: case SM_setName:
270: iresult = Sm1_setName();
271: if (iresult < 0) {
272: Sm1_pushError2(SerialCurrent,-1,"setName");
273: }
274: break;
275: case SM_evalName:
276: iresult = Sm1_evalName();
277: if (iresult < 0) {
278: Sm1_pushError2(SerialCurrent,-1,"evalName");
279: }
280: break;
281: case SM_executeStringByLocalParser:
282: OxCritical = 0;
1.13 takayama 283: if (NoExecution) {
284: iresult = 0;
285: }else{
286: iresult = Sm1_executeStringByLocalParser();
287: }
1.1 maekawa 288: OxCritical = 1; signal(SIGUSR1,controlResetHandler);
289: if (iresult < 0) {
290: emsg = Sm1_popErrorMessage("executeString: ");
291: Sm1_pushError2(SerialCurrent,-1,emsg);
292: return(-1);
293: }
294: break;
295: case SM_executeFunction:
296: OxCritical = 0;
1.13 takayama 297: if (NoExecution) {
298: iresult = 0;
299: }else{
300: iresult = Sm1_executeStringByLocalParser();
301: }
1.1 maekawa 302: OxCritical = 1; signal(SIGUSR1,controlResetHandler);
303: if (iresult < 0) {
304: emsg = Sm1_popErrorMessage("executeFunction: ");
1.14 ! takayama 305: Sm1_pushError2(SerialCurrent,-1,emsg);
! 306: return(-1);
! 307: }
! 308: break;
! 309: case SM_executeFunctionWithOptionalArgument:
! 310: OxCritical = 0;
! 311: if (NoExecution) {
! 312: iresult = 0;
! 313: }else{
! 314: iresult = Sm1_executeStringByLocalParser();
! 315: }
! 316: OxCritical = 1; signal(SIGUSR1,controlResetHandler);
! 317: if (iresult < 0) {
! 318: emsg = Sm1_popErrorMessage("executeFunctionWithOptionalArgument: ");
1.1 maekawa 319: Sm1_pushError2(SerialCurrent,-1,emsg);
320: return(-1);
321: }
322: break;
323: case SM_popCMO:
1.11 takayama 324: oxSendOXheader(ostreamOut,OX_DATA,SerialOX++);
325: n=Sm1_popCMO(ostreamOut,SerialCurrent);
1.1 maekawa 326: if (message) fprintf(stderr,"Done.\n");
327: break;
328: case SM_popString:
1.11 takayama 329: oxSendOXheader(ostreamOut,OX_DATA,SerialOX++);
330: oxSendCmoString(ostreamOut,Sm1_popString());
1.1 maekawa 331: if (message) fprintf(stderr,"Done.\n");
332: break;
333: case SM_shutdown:
1.5 takayama 334: fprintf(stderr,"Shutting down the engine.\n");
335: exit(0);
336: break;
1.1 maekawa 337: case SM_beginBlock:
338: case SM_endBlock:
339: fprintf(stderr,"This command has not yet been implemented.\n");
340: return(-1);
341: break;
342: default:
343: fprintf(stderr,"Fatal error. Unknown function_id %d\n",id);
344: return(-1);
345: break;
346: }
347: return(0);
348: }
349:
350:
351: nullserver_simplest(int fd) {
352: int c;
353: while(1) {
354: c = readOneByte(fd);
355: if (c == '@') { return; }
356: }
357: }
358:
359:
360: void controlResetHandler(sig)
1.5 takayama 361: int sig;
1.1 maekawa 362: {
363: signal(sig,SIG_IGN);
1.8 takayama 364: cancelAlarm();
1.1 maekawa 365: fprintf(stderr,"From controlResetHandler. OxCritical = %d\n",OxCritical);
366: OxInterruptFlag = 1;
367: if (OxCritical) {
368: return;
369: }else{
1.7 takayama 370: #if defined(__CYGWIN__)
371: siglongjmp(EnvOfChildServer,2); /* returns 2 for ctrl-C */
372: #else
1.1 maekawa 373: longjmp(EnvOfChildServer,2); /* returns 2 for ctrl-C */
1.7 takayama 374: #endif
1.1 maekawa 375: }
376: }
377:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>