[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.9

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

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