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