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