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