[BACK]Return to ox_k0.c CVS log [TXT][DIR] Up to [local] / OpenXM / src / k097

Annotation of OpenXM/src/k097/ox_k0.c, Revision 1.7

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

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>