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