Annotation of OpenXM/src/k097/ox_k0.c, Revision 1.6
1.6 ! takayama 1: /* $OpenXM: OpenXM/src/k097/ox_k0.c,v 1.5 2003/12/05 14:02:23 takayama Exp $ */
1.1 takayama 2: #include <stdio.h>
3: #include <sys/types.h>
4: #include <sys/socket.h>
5: #include <sys/time.h>
6: #include <netinet/in.h>
7: #include <netdb.h>
8: #include <signal.h>
9: #include <setjmp.h>
10: /* -lnsl -lsocket /usr/ucblib/libucb.a */
11: #include "../kxx/ox_kan.h"
12: #include "../kxx/serversm.h"
13:
14: int OxCritical = 0;
15: int OxInterruptFlag = 0;
16: int PacketMonitor = 0;
17:
18: extern int SerialOX; /* Serial number of the packets sent. */
19: extern int SerialCurrent; /* Current Serial number of the recieved packet. */
20: extern int OXprintMessage; /* print oxmessages? */
1.6 ! takayama 21: extern int Calling_ctrlC_hook;
1.1 takayama 22:
23: #if defined(__CYGWIN__)
24: sigjmp_buf EnvOfChildServer;
25: #else
26: jmp_buf EnvOfChildServer;
27: #endif
28:
29: int JmpMessage = 0;
30:
31: static char *getSuffix(char *s);
32: main(int argc, char *argv[]) {
33: char *s;
34:
35: if (argc > 1) {
36: if (strcmp(argv[1],"-monitor")==0) {
37: fprintf(stderr,"Taking the packet monitor.\n");
38: PacketMonitor = 1;
39: }else{
40: fprintf(stderr,"Unknown option. Possible options are -monitor\n");
41: }
42: }
43: /* Load initialization files if necessary. */
44: /* Sm1_start(0, NULL, "ox_sm1"); */
45: K0_start();
46:
1.4 takayama 47: nullserver(3,4);
1.1 takayama 48: }
49:
50: static char *getSuffix(char *s) {
51: /* getSuffix("ox_sm1_forAsir") returns "forAsir" */
52: /* getSuffix("ox_sm1_forAsir.exe") returns "forAsir" */
53: /* getSuffix("ox_sm1_gnuplot") returns "gnuplot" */
54: int n,i,k;
55: n = strlen(s);
56: if (n > 5 && (strcmp(".exe",&(s[n-4]))==0 || strcmp(".EXE",&(s[n-4]))==0)) {
57: s[n-4] = '\0';
58: }
59: for (i=n-1; i>=0; i--) {
60: if (s[i] == '_') {
61: return( s+i+1 );
62: }
63: }
64: return(s);
65: }
1.4 takayama 66: nullserver(int fdStreamIn,int fdStreamOut) {
1.1 takayama 67: int mtag;
68: int message = 1;
1.4 takayama 69: ox_stream ostreamIn;
70: ox_stream ostreamOut;
1.1 takayama 71: char sreason[1024];
72: extern void controlResetHandler();
73: #if defined(__CYGWIN__)
74: extern sigjmp_buf EnvOfStackMachine;
75: #else
76: extern jmp_buf EnvOfStackMachine;
77: #endif
78: int engineByteOrder;
1.5 takayama 79: extern int InSendmsg2;
1.1 takayama 80:
81: fflush(NULL);
1.4 takayama 82: engineByteOrder = oxTellMyByteOrder(fdStreamOut,fdStreamIn);
1.1 takayama 83: /* Set the network byte order. */
84: fprintf(stderr,"engineByteOrder=%x\n",engineByteOrder);
85:
1.4 takayama 86: if (fdStreamIn != -1) {
87: ostreamIn = fp2open(fdStreamIn);
88: if (ostreamIn == NULL) {
89: fprintf(stderr,"fp2open(fdStreamIn) failed.\n");
90: fdStreamIn = -1;
1.1 takayama 91: }
1.4 takayama 92: if (PacketMonitor) fp2watch(ostreamIn,stdout);
93: }
94: if (fdStreamOut != -1) {
95: ostreamOut = fp2open(fdStreamOut);
96: if (ostreamOut == NULL) {
97: fprintf(stderr,"fp2open(fdStreamOut) failed.\n");
98: fdStreamOut = -1;
99: }
100: if (PacketMonitor) fp2watch(ostreamOut,stdout);
1.1 takayama 101: }
102: aaa : ;
103: clearInop();
104: #if defined(__CYGWIN__)
105: if (sigsetjmp(EnvOfChildServer,1)) {
106: #else
107: if (setjmp(EnvOfChildServer)) {
108: #endif
109: fprintf(stderr,"childServerMain: jump here.\n");
110:
111: {
112: extern int Kpt;
113: extern char * Kbuff;
114: fprintf(stderr,"Resetting...\n"); fflush(NULL);
115: Kpt = 0; Kbuff[0] = '\0';
116: parseAfile(stdin);
117: }
118:
119: if (OxInterruptFlag == 0) {
120: fprintf(stderr," ?! \n"); fflush(NULL);
121: }
1.6 ! takayama 122: if (!Calling_ctrlC_hook) {
! 123: Calling_ctrlC_hook = 1;
! 124: KSexecuteString(" ctrlC-hook "); /* Execute User Defined functions. */
! 125: }
! 126: Calling_ctrlC_hook = 0;
1.1 takayama 127: KSexecuteString(" (Computation is interrupted.) ");
1.5 takayama 128: InSendmsg2 = 0;
1.1 takayama 129: signal(SIGUSR1,controlResetHandler); goto aaa;
130: } else {
131: if (JmpMessage) fprintf(stderr,"Set EnvOfChildServer.\n");
132: signal(SIGUSR1,controlResetHandler);
133: }
134: #if defined(__CYGWIN__)
135: if (sigsetjmp(EnvOfStackMachine,1)) {
136: #else
137: if (setjmp(EnvOfStackMachine)) {
138: #endif
139: fprintf(stderr,"childServerMain: jump here by EnvOfStackMachine or timeout.\n");
140: if (OxInterruptFlag == 0) {
141: fprintf(stderr," ?! \n"); fflush(NULL);
142: }
143: {
144: extern int Kpt;
145: extern char * Kbuff;
146: fprintf(stderr,"Resetting...\n"); fflush(NULL);
147: Kpt = 0; Kbuff[0] = '\0';
148: parseAfile(stdin);
149: }
150: /* In case of error in the stack machine, pop the error info
151: and send the error packet. */
152: /* oxSendOXheader(ostream,OX_DATA,SerialOX++);
153: oxSendCmoError(ostream);
154: oxSendOXheader(ostream,OX_DATA,SerialOX++);
155: sprintf(sreason,"Jump here by sm1 error.");
156: oxSendCmoError2(ostream,sreason);
157: */
158: Sm1_pushError2(SerialCurrent,-1,"Global jump by sm1 error");
159:
160: KSexecuteString(" ctrlC-hook "); /* Execute User Defined functions. */
1.5 takayama 161: InSendmsg2=0;
1.1 takayama 162: signal(SIGUSR1,controlResetHandler); goto aaa ;
163: } else {
164: if (JmpMessage) fprintf(stderr,"Set EnvOfStackMachine.\n");
165: if (signal(SIGUSR1,SIG_IGN) != SIG_IGN) {
166: signal(SIGUSR1,controlResetHandler);
167: }
168: }
169:
170: while (1) {
171: message = OXprintMessage;
172: if (OxInterruptFlag) {
173: OxCritical = 1;
174: if (message) {fprintf(stderr,"Clearing the read buffer.\n");fflush(NULL); }
1.4 takayama 175: fp2clearReadBuf(ostreamIn); /* clear the read buffer */
1.1 takayama 176: if (message) {fprintf(stderr,"Throwing OX_SYNC_BALL\n"); fflush(NULL);}
1.4 takayama 177: oxSendSyncBall(ostreamOut);
1.1 takayama 178: if (message) {fprintf(stderr,"Waiting for OX_SYNC_BALL\n");fflush(NULL);}
1.4 takayama 179: oxWaitSyncBall(ostreamIn);
1.1 takayama 180: if (message) {fprintf(stderr,"Done changing OX_SYNC_BALL\n"); fflush(NULL);}
181: OxInterruptFlag = 0;
182: OxCritical = 0;
183: goto aaa ;
184: }
185: OxCritical = 0;
1.4 takayama 186: if (fp2select(ostreamIn,-1)) {
1.1 takayama 187: /* If there is an data in the ostream, then read data in the buffer and
188: read data in the communication stream. */
189: OxCritical = 1;
190: }else{
191: /* interrupted system call */
192: /* This part is never reached. */
193: }
194: OxCritical = 1;
1.4 takayama 195: mtag = oxGetOXheader(ostreamIn,&SerialCurrent); /* get the message_tag */
1.1 takayama 196: if (message) {
197: fprintf(stderr,"\nmtag is %d (serial=%d) : ",mtag,SerialCurrent);
198: switch(mtag) {
199: case OX_COMMAND: fprintf(stderr," OX_COMMAND \n"); break;
200: case OX_DATA: fprintf(stderr," OX_DATA \n"); break;
201: case OX_SYNC_BALL: fprintf(stderr," OX_SYNC_BALL \n"); break;
202: case -1: fprintf(stderr," End of file. Exiting the server child.\n");
1.2 ohara 203: exit(0); break;
1.1 takayama 204: default: fprintf(stderr," ?! \n"); break;
205: }
206: }
207: /*sleep(2); /* for dubug OX_SYNC_BALL */
208: switch(mtag) {
209: case OX_COMMAND:
1.4 takayama 210: nullserverCommand(ostreamIn,ostreamOut);
1.1 takayama 211: goto aaa ; /* We always reset the jump vector. cf. memo1.txt 1998 2/13*/
212: break;
213: case OX_DATA:
1.4 takayama 214: Sm1_pushCMO(ostreamIn);
1.1 takayama 215: break;
216: case OX_SYNC_BALL:
217: /* if (OxInterruptFlag) think about it later. */
218: break;
219: default:
220: fprintf(stderr,"Fatal error in server.\n");
221: break;
222: }
223: }
224: }
225:
1.4 takayama 226: nullserverCommand(ox_stream ostreamIn,ox_stream ostreamOut) {
1.1 takayama 227: int id;
228: int mtag;
229: int n;
230: char *name;
231: char *sresult;
232: struct mathCap *mathresult;
233: int iresult;
234: int message = 1;
235: char *emsg;
236: extern void controlResetHandler();
237:
238: message = OXprintMessage;
239: /* message_body */
1.4 takayama 240: id = oxGetInt32(ostreamIn); /* get the function_id */
1.1 takayama 241: if (message) {fprintf(stderr,"\nfunction_id is %d\n",id);}
242: switch( id ) {
243: case SM_mathcap:
244: if (message) fprintf(stderr," mathcap\n");
245: mathresult = (struct mathCap *)Sm1_mathcap();
246: oxPushMathCap(mathresult);
247: break;
248: case SM_setMathCap:
249: if (message) fprintf(stderr," setMathCap\n");
1.4 takayama 250: Sm1_setMathCap(ostreamOut);
1.1 takayama 251: break;
252: case SM_pops:
253: if (message) fprintf(stderr," pops \n");
254: Sm1_pops();
255: break;
256: case SM_getsp:
257: if (message) fprintf(stderr," getsp \n");
258: Sm1_getsp();
259: break;
260: case SM_dupErrors:
261: if (message) fprintf(stderr," dupErrors \n");
262: Sm1_dupErrors();
263: break;
264: case SM_pushCMOtag:
265: if (message) fprintf(stderr," pushCMOtag \n");
266: Sm1_pushCMOtag(SerialCurrent);
267: break;
268: case SM_setName:
269: if (message) fprintf(stderr," setName \n");
270: iresult = Sm1_setName();
271: if (iresult < 0) {
272: Sm1_pushError2(SerialCurrent,-1,"setName");
273: }
274: break;
275: case SM_evalName:
276: if (message) fprintf(stderr," evalName \n");
277: iresult = Sm1_evalName();
278: if (iresult < 0) {
279: Sm1_pushError2(SerialCurrent,-1,"evalName");
280: }
281: break;
282: case SM_executeStringByLocalParser:
283: if (message) fprintf(stderr," executeStringByLocalParser\n");
284: OxCritical = 0;
285: iresult = K0_executeStringByLocalParser();
286: OxCritical = 1; signal(SIGUSR1,controlResetHandler);
287: if (iresult < 0) {
288: emsg = Sm1_popErrorMessage("executeString: ");
289: Sm1_pushError2(SerialCurrent,-1,emsg);
290: return(-1);
291: }
292: break;
293: case SM_executeFunction:
294: if (message) fprintf(stderr," executeFunction\n");
295: OxCritical = 0;
296: iresult = K0_executeStringByLocalParser();
297: OxCritical = 1; signal(SIGUSR1,controlResetHandler);
298: if (iresult < 0) {
299: emsg = Sm1_popErrorMessage("executeFunction: ");
300: Sm1_pushError2(SerialCurrent,-1,emsg);
301: return(-1);
302: }
303: break;
304: case SM_popCMO:
305: if (message) fprintf(stderr,"popCMO. Start to sending data.\n",n);
1.4 takayama 306: oxSendOXheader(ostreamOut,OX_DATA,SerialOX++);
307: n=Sm1_popCMO(ostreamOut,SerialCurrent);
1.1 takayama 308: if (message) fprintf(stderr,"Done.\n");
309: break;
310: case SM_popString:
311: if (message) fprintf(stderr,"popString. send data from the stack.\n",n);
1.4 takayama 312: oxSendOXheader(ostreamOut,OX_DATA,SerialOX++);
313: oxSendCmoString(ostreamOut,Sm1_popString());
1.1 takayama 314: if (message) fprintf(stderr,"Done.\n");
315: break;
316: case SM_shutdown:
317: fprintf(stderr,"Shutting down the engine.\n");
318: exit(0);
319: break;
320: case SM_beginBlock:
321: case SM_endBlock:
322: fprintf(stderr,"This command has not yet been implemented.\n");
323: return(-1);
324: break;
325: default:
326: fprintf(stderr,"Fatal error. Unknown function_id %d\n",id);
327: return(-1);
328: break;
329: }
330: return(0);
331: }
332:
333:
334: nullserver_simplest(int fd) {
335: int c;
336: while(1) {
337: c = readOneByte(fd);
338: if (c == '@') { return; }
339: }
340: }
341:
342:
343: void controlResetHandler(sig)
344: int sig;
345: {
346: signal(sig,SIG_IGN);
347: cancelAlarm();
348: fprintf(stderr,"From controlResetHandler. OxCritical = %d\n",OxCritical);
349: OxInterruptFlag = 1;
350: if (OxCritical) {
351: return;
352: }else{
1.6 ! takayama 353: (void) traceShowStack(); traceClearStack();
1.1 takayama 354: #if defined(__CYGWIN__)
355: siglongjmp(EnvOfChildServer,2); /* returns 2 for ctrl-C */
356: #else
357: longjmp(EnvOfChildServer,2); /* returns 2 for ctrl-C */
358: #endif
359: }
360: }
361:
362: /* From k2.c */
363:
364: #define DATE "1998,12/15"
365: #include <stdio.h>
366:
367: char *getLOAD_K_PATH(); /* from d.h */
368:
369:
370: extern int DebugCompiler; /* 0: , 1: Displays sendKan[ .... ] */
371: extern int DebugMode;
372: extern int K00_verbose;
373:
374: int Startupk2 = 1;
375:
376:
377: K0_start() {
378: extern int Saki;
379: extern int Interactive;
380: int i;
381: char *s;
382: char tname[1024];
383: FILE *fp;
384:
385: KSstart(); /******** initialize ***********/
386: /* Call setjmp(EnvOfStackMachine) for error handling. cf. scanner() in
387: Kan/stackmachine.c */
388:
389: fprintf(stderr,"This is kan/k0 Version %s",DATE);
390: fprintf(stderr,"\n");
391: fprintf(stderr,"WARNING: This is an EXPERIMENTAL version\n");
392: if (K00_verbose == 1) {
393: KSexecuteString(" /K00_verbose 1 def ");
394: }else if (K00_verbose == 2) {
395: KSexecuteString(" /K00_verbose 2 def ");
396: }else {
397: KSexecuteString(" /K00_verbose 0 def ");
398: }
399: execFile("var.sm1"); KSexecuteString(" strictMode ");
400: execFile("incmac.sm1");
401: execFile("slib.sm1");
402:
403: printf("\n\nIn(1)= ");
404: s = "startup.k";
405: if (Startupk2) {
406: strcpy(tname,s);
407: if ((fp = fopen(tname,"r")) == (FILE *) NULL) {
408: strcpy(tname,getLOAD_K_PATH());
409: strcat(tname,s);
410: if ((fp = fopen(tname,"r")) == (FILE *) NULL) {
411: strcpy(tname,LOAD_K_PATH);
412: strcat(tname,s);
413: if ((fp = fopen(tname,"r")) == (FILE *) NULL) {
414: fprintf(stderr,"No startup file.\n");
415: }
416: }
417: }
418: if (fp != (FILE *)NULL) {
419: if (K00_verbose) fprintf(stderr,"Reading startup.k... ");
420: Saki = 0;
421: parseAfile(fp);
422: KCparse();
423: if (K00_verbose) fprintf(stderr,"Done.\n");
424: }
425: }
426:
427: clearInop();
428: }
429:
430: /*
431: Interactive = 0;
432: parseAfile(stdin);
433: parseAstring(" sm1(\" 1 1 10 { message } for \" ) "); It works !
434: KCparse();
435:
436: KSstop(); closing
437: */
438:
439: K0_executeStringByLocalParser() {
440: extern int Interactive;
441: char *s;
442: s = Sm1_popString();
443: fprintf(stderr,"K0_executeStringByLocalParse(): %s\n",s);
444: /* fflush(NULL); getchar(); */
445: if (s != NULL) {
446: clearInop(); Interactive=0;
447: parseAstring(s);
448: KCparse();
449: }
450: return 0;
451: }
452:
453:
454:
455:
456:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>