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