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