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

Annotation of OpenXM/src/kxx/oxserver00.c, Revision 1.15

1.15    ! takayama    1: /* $OpenXM: OpenXM/src/kxx/oxserver00.c,v 1.14 2004/03/08 08:24:42 takayama Exp $ */
1.1       maekawa     2: /* nullserver01 */
                      3: #include <stdio.h>
                      4: #include <sys/types.h>
                      5: #include <sys/socket.h>
                      6: #include <sys/time.h>
                      7: #include <netinet/in.h>
                      8: #include <netdb.h>
                      9: #include <signal.h>
                     10: #include <setjmp.h>
                     11: /* -lnsl -lsocket /usr/ucblib/libucb.a */
                     12: #include "ox_kan.h"
                     13: #include "serversm.h"
                     14:
                     15: int OxCritical = 0;
                     16: int OxInterruptFlag = 0;
                     17: int PacketMonitor = 0;
1.13      takayama   18: int NoExecution = 0;
1.1       maekawa    19:
                     20: extern int SerialOX;  /* Serial number of the packets sent. */
                     21: extern int SerialCurrent;  /* Current Serial number of the recieved packet. */
                     22: extern int OXprintMessage; /* print oxmessages? */
1.15    ! takayama   23: extern int Calling_ctrlC_hook;
1.1       maekawa    24:
1.7       takayama   25: #if defined(__CYGWIN__)
                     26: sigjmp_buf EnvOfChildServer;
                     27: #else
1.1       maekawa    28: jmp_buf EnvOfChildServer;
1.7       takayama   29: #endif
1.1       maekawa    30:
                     31: int JmpMessage = 0;
1.12      takayama   32: extern int Lisplike;
1.1       maekawa    33:
                     34: static char *getSuffix(char *s);
                     35: main(int argc, char *argv[]) {
                     36:   char *s;
                     37:   char *forAsir[] = {"callsm1.sm1","callsm1b.sm1"};
                     38:   char *gnuplot[] = {"callsm1.sm1","gnuplot.sm1"};
                     39:   char *phc[] = {"callsm1.sm1","phc.sm1"};
                     40:   char *tigers[] = {"callsm1.sm1","tigers.sm1"};
                     41:   char *basicCD[] = {"basicCD.sm1"};
                     42:   /*  If you change the above, you need to change the argc of Sm1_start below.
1.5       takayama   43:    */
1.1       maekawa    44:
                     45:   if (argc > 1) {
                     46:     if (strcmp(argv[1],"-monitor")==0) {
                     47:       fprintf(stderr,"Taking the packet monitor.\n");
                     48:       PacketMonitor = 1;
1.12      takayama   49:     }else if (strcmp(argv[1],"-lispLike")==0) {
                     50:       fprintf(stderr,"Output lispLike expression.\n");
                     51:       Lisplike = 1;
1.13      takayama   52:     }else if (strcmp(argv[1],"-noexec")==0) {
                     53:       fprintf(stderr,"I do not execute commands.\n");
                     54:       NoExecution = 1;
1.1       maekawa    55:     }else{
                     56:       fprintf(stderr,"Unknown option. Possible options are -monitor\n");
                     57:     }
                     58:   }
                     59:   /* Load initialization files if necessary. */
                     60:   s = getSuffix(argv[0]);
                     61:   if (strcmp(s,"forAsir") == 0) {
                     62:     Sm1_start(2,forAsir,"ox_sm1_forAsir");
                     63:   }else if (strcmp(s,"gnuplot") == 0) {
                     64:     Sm1_start(2,gnuplot,"ox_sm1_gnuplot");
                     65:   }else if (strcmp(s,"phc") == 0) {
                     66:     Sm1_start(2,phc,"ox_sm1_phc");
                     67:   }else if (strcmp(s,"tigers") == 0) {
                     68:     Sm1_start(2,tigers,"ox_sm1_tigers");
                     69:   }else if (strcmp(s,"basicCD") == 0) {
                     70:     Sm1_start(1,basicCD,"basicCD");
                     71:   }else {  /* Default */
                     72:     Sm1_start(0, NULL, "ox_sm1");
                     73:   }
                     74:
1.11      takayama   75:   nullserver(3,4);
1.1       maekawa    76: }
                     77:
                     78: static char *getSuffix(char *s) {
                     79:   /* getSuffix("ox_sm1_forAsir") returns "forAsir" */
1.6       takayama   80:   /* getSuffix("ox_sm1_forAsir.exe") returns "forAsir" */
1.1       maekawa    81:   /* getSuffix("ox_sm1_gnuplot") returns "gnuplot" */
                     82:   int n,i,k;
                     83:   n = strlen(s);
1.6       takayama   84:   if (n > 5 && (strcmp(".exe",&(s[n-4]))==0 || strcmp(".EXE",&(s[n-4]))==0)) {
                     85:     s[n-4] = '\0';
                     86:   }
1.1       maekawa    87:   for (i=n-1; i>=0; i--) {
                     88:     if (s[i] == '_') {
                     89:       return( s+i+1 );
                     90:     }
                     91:   }
                     92:   return(s);
                     93: }
1.11      takayama   94: nullserver(int fdStreamIn,int fdStreamOut) {
1.1       maekawa    95:   int mtag;
                     96:   int message = 1;
1.11      takayama   97:   ox_stream ostreamIn;
                     98:   ox_stream ostreamOut;
1.1       maekawa    99:   char sreason[1024];
                    100:   extern void controlResetHandler();
1.7       takayama  101: #if defined(__CYGWIN__)
                    102:   extern sigjmp_buf EnvOfStackMachine;
                    103: #else
1.1       maekawa   104:   extern jmp_buf EnvOfStackMachine;
1.7       takayama  105: #endif
1.1       maekawa   106:   int engineByteOrder;
                    107:
1.12      takayama  108:   /* for debug,  use -monitor
                    109:         PacketMonitor = 1;  */
1.11      takayama  110:
1.4       takayama  111:   fflush(NULL);
1.11      takayama  112:   engineByteOrder = oxTellMyByteOrder(fdStreamOut,fdStreamIn);
1.1       maekawa   113:   /* Set the network byte order. */
                    114:   fprintf(stderr,"engineByteOrder=%x\n",engineByteOrder);
                    115:
1.11      takayama  116:   if (fdStreamIn != -1) {
                    117:     ostreamIn = fp2open(fdStreamIn);
                    118:     if (ostreamIn == NULL) {
                    119:       fprintf(stderr,"fp2open(fdStreamIn) failed.\n");
                    120:       fdStreamIn = -1;
                    121:     }
                    122:     if (PacketMonitor) fp2watch(ostreamIn,stdout);
                    123:   }
                    124:   if (fdStreamOut != -1) {
                    125:     ostreamOut = fp2open(fdStreamOut);
                    126:     if (ostreamOut == NULL) {
                    127:       fprintf(stderr,"fp2open(fdStreamOut) failed.\n");
                    128:       fdStreamOut = -1;
1.1       maekawa   129:     }
1.11      takayama  130:     if (PacketMonitor) fp2watch(ostreamOut,stdout);
1.1       maekawa   131:   }
1.11      takayama  132:
1.1       maekawa   133:   aaa : ;
1.7       takayama  134: #if defined(__CYGWIN__)
                    135:   if (sigsetjmp(EnvOfChildServer,1)) {
                    136: #else
1.1       maekawa   137:   if (setjmp(EnvOfChildServer)) {
1.7       takayama  138: #endif
1.1       maekawa   139:     fprintf(stderr,"childServerMain: jump here.\n");
                    140:     if (OxInterruptFlag == 0) {
                    141:       fprintf(stderr," ?! \n"); fflush(NULL);
                    142:     }
1.15    ! takayama  143:     if (!Calling_ctrlC_hook) {
        !           144:       Calling_ctrlC_hook = 1;
        !           145:       KSexecuteString(" ctrlC-hook "); /* Execute User Defined functions. */
        !           146:     }
        !           147:     Calling_ctrlC_hook = 0;
1.9       takayama  148:        KSexecuteString(" (Computation is interrupted.) ");
1.1       maekawa   149:     signal(SIGUSR1,controlResetHandler); goto aaa;
                    150:   } else {
                    151:     if (JmpMessage) fprintf(stderr,"Set EnvOfChildServer.\n");
                    152:     signal(SIGUSR1,controlResetHandler);
                    153:   }
1.7       takayama  154: #if defined(__CYGWIN__)
                    155:   if (sigsetjmp(EnvOfStackMachine,1)) {
                    156: #else
1.1       maekawa   157:   if (setjmp(EnvOfStackMachine)) {
1.7       takayama  158: #endif
1.9       takayama  159:     fprintf(stderr,"childServerMain: jump here by EnvOfStackMachine or timeout.\n");
1.1       maekawa   160:     if (OxInterruptFlag == 0) {
                    161:       fprintf(stderr," ?! \n"); fflush(NULL);
                    162:     }
                    163:     /* In case of error in the stack machine, pop the error info
                    164:        and send the error packet. */
                    165:     /* oxSendOXheader(ostream,OX_DATA,SerialOX++);
1.5       takayama  166:        oxSendCmoError(ostream);
                    167:        oxSendOXheader(ostream,OX_DATA,SerialOX++);
                    168:        sprintf(sreason,"Jump here by sm1 error.");
                    169:        oxSendCmoError2(ostream,sreason);
1.1       maekawa   170:     */
                    171:     Sm1_pushError2(SerialCurrent,-1,"Global jump by sm1 error");
                    172:
1.15    ! takayama  173:     if (!Calling_ctrlC_hook) {
        !           174:       Calling_ctrlC_hook = 1;
        !           175:       KSexecuteString(" ctrlC-hook "); /* Execute User Defined functions. */
        !           176:     }
        !           177:     Calling_ctrlC_hook = 0;
1.1       maekawa   178:     signal(SIGUSR1,controlResetHandler); goto aaa ;
                    179:   } else {
                    180:     if (JmpMessage) fprintf(stderr,"Set EnvOfStackMachine.\n");
                    181:     if (signal(SIGUSR1,SIG_IGN) != SIG_IGN) {
                    182:       signal(SIGUSR1,controlResetHandler);
                    183:     }
                    184:   }
                    185:
                    186:   while (1) {
1.11      takayama  187:     message = OXprintMessage;
1.1       maekawa   188:     if (OxInterruptFlag) {
                    189:       OxCritical = 1;
                    190:       if (message) {fprintf(stderr,"Clearing the read buffer.\n");fflush(NULL); }
1.11      takayama  191:       fp2clearReadBuf(ostreamIn); /* clear the read buffer */
1.1       maekawa   192:       if (message) {fprintf(stderr,"Throwing OX_SYNC_BALL\n"); fflush(NULL);}
1.11      takayama  193:       oxSendSyncBall(ostreamOut);
1.1       maekawa   194:       if (message) {fprintf(stderr,"Waiting for OX_SYNC_BALL\n");fflush(NULL);}
1.11      takayama  195:       oxWaitSyncBall(ostreamIn);
1.1       maekawa   196:       if (message) {fprintf(stderr,"Done changing OX_SYNC_BALL\n"); fflush(NULL);}
                    197:       OxInterruptFlag = 0;
                    198:       OxCritical = 0;
                    199:       goto aaa ;
                    200:     }
                    201:     OxCritical = 0;
1.11      takayama  202:     if (fp2select(ostreamIn,-1)) {
1.1       maekawa   203:       /* If there is an data in the ostream, then read data in the buffer and
                    204:          read data in the communication stream. */
                    205:       OxCritical = 1;
                    206:     }else{
                    207:       /* interrupted system call */
                    208:       /* This part is never reached. */
                    209:     }
                    210:     OxCritical = 1;
1.11      takayama  211:     mtag = oxGetOXheader(ostreamIn,&SerialCurrent); /* get the message_tag */
1.1       maekawa   212:     if (message) {
                    213:       fprintf(stderr,"\nmtag is %d (serial=%d) : ",mtag,SerialCurrent);
                    214:       switch(mtag) {
                    215:       case OX_COMMAND: fprintf(stderr," OX_COMMAND \n"); break;
                    216:       case OX_DATA:    fprintf(stderr," OX_DATA \n"); break;
                    217:       case OX_SYNC_BALL: fprintf(stderr," OX_SYNC_BALL \n"); break;
                    218:       case -1: fprintf(stderr," End of file. Exiting the server child.\n");
1.10      ohara     219:         exit(0); break;
1.1       maekawa   220:       default: fprintf(stderr," ?! \n"); break;
                    221:       }
                    222:     }
                    223:     /*sleep(2);  /* for dubug OX_SYNC_BALL */
                    224:     switch(mtag) {
                    225:     case OX_COMMAND:
1.11      takayama  226:       nullserverCommand(ostreamIn,ostreamOut);
1.1       maekawa   227:       goto aaa ;  /* We always reset the jump vector. cf. memo1.txt 1998 2/13*/
                    228:       break;
                    229:     case OX_DATA:
1.11      takayama  230:       Sm1_pushCMO(ostreamIn);
1.1       maekawa   231:       break;
                    232:     case OX_SYNC_BALL:
                    233:       /* if (OxInterruptFlag)  think about it later. */
                    234:       break;
                    235:     default:
                    236:       fprintf(stderr,"Fatal error in server.\n");
                    237:       break;
                    238:     }
                    239:   }
                    240: }
                    241:
1.11      takayama  242: nullserverCommand(ox_stream ostreamIn,ox_stream ostreamOut) {
1.1       maekawa   243:   int id;
                    244:   int mtag;
                    245:   int n;
                    246:   char *name;
                    247:   char *sresult;
                    248:   struct mathCap *mathresult;
                    249:   int iresult;
                    250:   int message = 1;
                    251:   char *emsg;
                    252:   extern void controlResetHandler();
                    253:
1.11      takayama  254:   message = OXprintMessage;
1.1       maekawa   255:   /* message_body */
1.11      takayama  256:   id = oxGetInt32(ostreamIn);   /* get the function_id */
1.12      takayama  257:   if (message) {fprintf(stderr,"\nfunction_id is %d; %s\n",id,oxFIDtoStr(id));}
1.1       maekawa   258:   switch( id ) {
                    259:   case SM_mathcap:
                    260:     mathresult = (struct mathCap *)Sm1_mathcap();
                    261:     oxPushMathCap(mathresult);
                    262:     break;
                    263:   case SM_setMathCap:
1.11      takayama  264:     Sm1_setMathCap(ostreamOut);
1.1       maekawa   265:     break;
                    266:   case SM_pops:
                    267:     Sm1_pops();
                    268:     break;
                    269:   case SM_getsp:
                    270:     Sm1_getsp();
                    271:     break;
                    272:   case SM_dupErrors:
                    273:     Sm1_dupErrors();
                    274:     break;
1.3       takayama  275:   case SM_pushCMOtag:
                    276:     Sm1_pushCMOtag(SerialCurrent);
1.5       takayama  277:     break;
1.1       maekawa   278:   case SM_setName:
                    279:     iresult = Sm1_setName();
                    280:     if (iresult < 0) {
                    281:       Sm1_pushError2(SerialCurrent,-1,"setName");
                    282:     }
                    283:     break;
                    284:   case SM_evalName:
                    285:     iresult = Sm1_evalName();
                    286:     if (iresult < 0) {
                    287:       Sm1_pushError2(SerialCurrent,-1,"evalName");
                    288:     }
                    289:     break;
                    290:   case SM_executeStringByLocalParser:
                    291:     OxCritical = 0;
1.13      takayama  292:        if (NoExecution) {
                    293:          iresult = 0;
                    294:        }else{
                    295:          iresult = Sm1_executeStringByLocalParser();
                    296:        }
1.1       maekawa   297:     OxCritical = 1; signal(SIGUSR1,controlResetHandler);
                    298:     if (iresult < 0) {
                    299:       emsg = Sm1_popErrorMessage("executeString: ");
                    300:       Sm1_pushError2(SerialCurrent,-1,emsg);
                    301:       return(-1);
                    302:     }
                    303:     break;
                    304:   case SM_executeFunction:
                    305:     OxCritical = 0;
1.13      takayama  306:        if (NoExecution) {
                    307:          iresult = 0;
                    308:        }else{
                    309:          iresult = Sm1_executeStringByLocalParser();
                    310:        }
1.1       maekawa   311:     OxCritical = 1; signal(SIGUSR1,controlResetHandler);
                    312:     if (iresult < 0) {
                    313:       emsg = Sm1_popErrorMessage("executeFunction: ");
1.14      takayama  314:       Sm1_pushError2(SerialCurrent,-1,emsg);
                    315:       return(-1);
                    316:     }
                    317:     break;
                    318:   case SM_executeFunctionWithOptionalArgument:
                    319:     OxCritical = 0;
                    320:        if (NoExecution) {
                    321:          iresult = 0;
                    322:        }else{
                    323:          iresult = Sm1_executeStringByLocalParser();
                    324:        }
                    325:     OxCritical = 1; signal(SIGUSR1,controlResetHandler);
                    326:     if (iresult < 0) {
                    327:       emsg = Sm1_popErrorMessage("executeFunctionWithOptionalArgument: ");
1.1       maekawa   328:       Sm1_pushError2(SerialCurrent,-1,emsg);
                    329:       return(-1);
                    330:     }
                    331:     break;
                    332:   case SM_popCMO:
1.11      takayama  333:     oxSendOXheader(ostreamOut,OX_DATA,SerialOX++);
                    334:     n=Sm1_popCMO(ostreamOut,SerialCurrent);
1.1       maekawa   335:     if (message) fprintf(stderr,"Done.\n");
                    336:     break;
                    337:   case SM_popString:
1.11      takayama  338:     oxSendOXheader(ostreamOut,OX_DATA,SerialOX++);
                    339:     oxSendCmoString(ostreamOut,Sm1_popString());
1.1       maekawa   340:     if (message) fprintf(stderr,"Done.\n");
                    341:     break;
                    342:   case SM_shutdown:
1.5       takayama  343:     fprintf(stderr,"Shutting down the engine.\n");
                    344:     exit(0);
                    345:     break;
1.1       maekawa   346:   case SM_beginBlock:
                    347:   case SM_endBlock:
                    348:     fprintf(stderr,"This command has not yet been implemented.\n");
                    349:     return(-1);
                    350:     break;
                    351:   default:
                    352:     fprintf(stderr,"Fatal error. Unknown function_id %d\n",id);
                    353:     return(-1);
                    354:     break;
                    355:   }
                    356:   return(0);
                    357: }
                    358:
                    359:
                    360: nullserver_simplest(int fd) {
                    361:   int c;
                    362:   while(1) {
                    363:     c = readOneByte(fd);
                    364:     if (c == '@') { return; }
                    365:   }
                    366: }
                    367:
                    368:
                    369: void controlResetHandler(sig)
1.5       takayama  370:      int sig;
1.1       maekawa   371: {
                    372:   signal(sig,SIG_IGN);
1.8       takayama  373:   cancelAlarm();
1.1       maekawa   374:   fprintf(stderr,"From controlResetHandler. OxCritical = %d\n",OxCritical);
                    375:   OxInterruptFlag = 1;
                    376:   if (OxCritical) {
                    377:     return;
                    378:   }else{
1.15    ! takayama  379:     (void) traceShowStack(); traceClearStack();
1.7       takayama  380: #if defined(__CYGWIN__)
                    381:     siglongjmp(EnvOfChildServer,2); /* returns 2 for ctrl-C */
                    382: #else
1.1       maekawa   383:     longjmp(EnvOfChildServer,2); /* returns 2 for ctrl-C */
1.7       takayama  384: #endif
1.1       maekawa   385:   }
                    386: }
                    387:

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