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