[BACK]Return to oxmisc2.c CVS log [TXT][DIR] Up to [local] / OpenXM / src / kan96xx / plugin

Annotation of OpenXM/src/kan96xx/plugin/oxmisc2.c, Revision 1.19

1.19    ! takayama    1: /* $OpenXM: OpenXM/src/kan96xx/plugin/oxmisc2.c,v 1.18 2003/11/17 05:45:47 takayama Exp $ */
1.1       maekawa     2: #include <stdio.h>
                      3: #include "ox_kan.h"
                      4: #include "oxmisc2.h"   /* This file requires sm1 object description. */
                      5: #include "cmo.h"
1.8       takayama    6:
1.1       maekawa     7: extern FILE *MyErrorOut;
1.6       takayama    8: #define SET_MYERROROUT { if (MyErrorOut == NULL) MyErrorOut=stdout; }
                      9: /* It is also defined in oxmisc.c */
                     10:
1.1       maekawa    11: extern int SerialOX; /* defined in SerialOX */
                     12:
                     13: extern int OxVersion;
                     14:
                     15: int DebugMathCap = 1;
                     16:
                     17:
                     18:
                     19: int oxGet(oxclientp client, struct object *op,int *isObj)
1.12      takayama   20:      /* This method should be synchronized. */
                     21:      /* oxGet is a function for client. */
1.1       maekawa    22: {
                     23:   int ans;
                     24:   ox_stream os;
                     25:   int m;
                     26:   struct object rob;
                     27:   int sss;  /* Serial number of the recieved packet. */
                     28:   *isObj = 0;
                     29:   op->tag = Snull;
                     30:   os = client->datafp2;
                     31:   switch(client->dstate) {
                     32:   case DSTATE_ANY:
                     33:     m = oxGetOXheader(os,&sss);
                     34:     switch(m) {
                     35:     case OX_DATA:
                     36:       client->dstate = DSTATE_WAIT_OX_DATA;
                     37:       return(oxGet(client,op,isObj));
                     38:     case OX_SYNC_BALL:
                     39:       client->dstate = DSTATE_ANY;
                     40:       return(OX_SYNC_BALL);
                     41:     default:
                     42:       errorOxmisc2("oxGet: cannot handle this tag.\n");
                     43:       client->dstate = DSTATE_ERROR;
                     44:       return(-1);
                     45:     }
                     46:     break;
                     47:   case DSTATE_FIRST_SYNC:  /* waiting the first sync ball */
                     48:     /* I need to clear the buffer?? */
                     49:     oxWaitSyncBall(os);
                     50:     client->dstate = DSTATE_ANY;
                     51:     oxSendSyncBall(os);
                     52:     return(OX_SYNC_BALL);
                     53:     break;
                     54:   case DSTATE_WAIT_OX_DATA:  /* waiting a cmo data. */
                     55:     *op = cmoObjectFromStream2(client->datafp2);
                     56:     client->dstate = DSTATE_ANY;
                     57:     *isObj = 1;
                     58:     return(0);
                     59:     break;
                     60:   case DSTATE_ERROR:
                     61:     client->dstate = DSTATE_ERROR;
                     62:     errorOxmisc2("oxGet: dstate == DSTATE_ERROR (error state)\n");
                     63:     return(-1);
                     64:   default:
                     65:     client->dstate = DSTATE_ERROR;
                     66:     errorOxmisc2("oxGet: Unknown state number.");
                     67:   }
                     68:
                     69:   return(-1);
                     70: }
                     71:
                     72: int oxGetFromControl(oxclientp client)
                     73: {
                     74:   int ans = -1;
1.7       takayama   75:   AbortIfRFC_101(client);
1.1       maekawa    76:   if (client->cstate != -1) {
                     77:     ans = oxGetResultOfControlInt32(client->controlfd);
                     78:     if (ans != -1) {    client->cstate = 0; }
                     79:     else {client->cstate = -1; }
                     80:   }
                     81:   return(ans);
                     82: }
                     83:
                     84: int oxReq(oxclientp client,int func,struct object ob)
                     85: {
                     86:   struct object *ob1p;
1.6       takayama   87:   SET_MYERROROUT;
1.1       maekawa    88:   /* request to the control channel */
                     89:   if (func == SM_control_reset_connection ||
                     90:       func == SM_control_kill) {
1.12      takayama   91:     AbortIfRFC_101(client);
1.1       maekawa    92:     switch(func) {
                     93:     case SM_control_reset_connection:
                     94:       oxReqControlResetConnection(client->controlfd);
1.9       takayama   95:       client->cstate = 0;
1.1       maekawa    96:       client->dstate = DSTATE_FIRST_SYNC;
                     97:       break;
                     98:     case SM_control_kill:
                     99:       oxReqControlKill(client->controlfd);
                    100:       client->cstate = 0;
                    101:       client->dstate = DSTATE_ANY;
                    102:       break;
                    103:     }
                    104:     fflush(NULL);
                    105:     return(0);
                    106:   }
                    107:
                    108:   /* request to the data channel */
                    109:   if (client->dstate != DSTATE_ANY) {
                    110:     errorOxmisc2("oxReq: client->dstate != DSTATE_ANY, data channel is not ready to send data.\n");
                    111:     return(-1);
                    112:   }
                    113:   switch(func) {
                    114:   case SM_DUMMY_sendcmo:
                    115:     if (!cmoCheckMathCap(ob,(struct object *)client->mathcapObjp)) {
                    116:       errorOxmisc2("oxReq: your peer does not understand this cmo.\n");
                    117:       return(-1);
                    118:     }
                    119:     oxSendOXheader(client->datafp2,OX_DATA,SerialOX++);
                    120:     cmoObjectToStream2(ob,client->datafp2);
                    121:     client->dstate = DSTATE_ANY;
                    122:     break;
                    123:   case SM_sync_ball:
                    124:     oxSendSyncBall(client->datafp2);
                    125:     client->dstate = DSTATE_ANY; /*  We do not expect the sync ball.*/
                    126:     client->cstate = 0;          /* clear the cstate */
                    127:     break;
                    128:   case SM_popCMO:
                    129:     oxReqPopCMO(client->datafp2);
                    130:     client->dstate = DSTATE_ANY;
                    131:     break;
                    132:   case SM_mathcap:
                    133:     oxReqMathCap(client->datafp2);
                    134:     client->dstate = DSTATE_ANY;
                    135:     break;
                    136:   case SM_setMathCap:
                    137:     /* ob = [(mathcap-obj) [[version num, system name] [sm tags]
1.12      takayama  138:        ob1                        smtags
                    139:        oxtags      [[ox numbers, [cmo numbers]]]
                    140:        ob3         ob2 */
                    141:     /*     oxtags      [[OX_DATA, [cmo numbers]],[OX_DATA_LOCAL,[opt]],...]*/
1.1       maekawa   142:     {
                    143:       struct object ob1;
                    144:       struct object ob2;
                    145:       struct object ob3;
1.4       takayama  146:       struct object obm;
1.1       maekawa   147:       struct object smtags;
1.4       takayama  148:       struct object oxtags;
                    149:       struct object ox;
1.12      takayama  150:       int n,i;
                    151:       struct mathCap mathcap;
1.1       maekawa   152:
1.12      takayama  153:       if (strcmp(KopString(getoa(ob,0)),"mathcap-object") != 0) {
                    154:         errorOxmisc2("data format error in oxReqSetMathCap");
                    155:         client->dstate = DSTATE_ANY;
                    156:         break;
                    157:       }
1.4       takayama  158:       obm = getoa(ob,1);
                    159:       ob1 = getoa(obm,0);
                    160:       smtags = getoa(obm,1);
                    161:       oxtags = getoa(obm,2);
                    162:       if (smtags.tag != Sarray || oxtags.tag != Sarray) {
1.12      takayama  163:         errorOxmisc2("data format error in oxReqSetMathCap");
1.4       takayama  164:       }
1.1       maekawa   165:       ob1p = (struct object *) sGC_malloc(sizeof(struct object));
                    166:       *ob1p = ob1;
                    167:       mathcap.infop = ob1p;
1.4       takayama  168:
                    169:       n = getoaSize(oxtags);
1.1       maekawa   170:       if (n >= MATHCAP_SIZE) errorOxmisc2("Too big mathcap of your peer.");
                    171:       mathcap.oxSize = n;
                    172:       for (i=0; i<n; i++) {
1.12      takayama  173:         ox = getoa(oxtags,i);
                    174:         if (ox.tag != Sarray) {
                    175:           errorOxmisc2("Data format error of the third argument of mathcap.");
                    176:         }
                    177:         mathcap.ox[i] = KopInteger(getoa(ox,0));
                    178:         if (mathcap.ox[i] == OX_DATA) {
                    179:           if (getoaSize(ox) < 2) {
                    180:             errorOxmisc2("Data format error in an entry of the third argument of mathcap.");
                    181:           }
                    182:           ob2 = getoa(ox,1);
                    183:           if (ob2.tag != Sarray) {
                    184:             errorOxmisc2("Data format error in an entry of the third argument of mathcap.");
                    185:           }
                    186:           mathcap.n = getoaSize(ob2);
                    187:           if (n >= MATHCAP_SIZE) errorOxmisc2("Too big mathcap of your peer.");
                    188:           for (i=0; i<mathcap.n; i++) {
                    189:             mathcap.cmo[i] = KopInteger(getoa(ob2,i));
                    190:           }
                    191:         }
1.1       maekawa   192:       }
                    193:
                    194:       n = getoaSize(smtags);
                    195:       if (n >= MATHCAP_SIZE) errorOxmisc2("Too big mathcap of your peer.");
                    196:       mathcap.smSize = n;
                    197:       for (i=0; i<n; i++) {
1.12      takayama  198:         mathcap.sm[i] = KopInteger(getoa(smtags,i));
1.1       maekawa   199:       }
                    200:
                    201:       oxReqSetMathCap(client->datafp2,&mathcap);
                    202:       client->dstate = DSTATE_ANY;
                    203:     }
                    204:     break;
                    205:   case SM_pops:
                    206:     if (ob.tag != Sinteger) {
                    207:       errorOxmisc2("SM_pops : the argument must be an integer.");
                    208:       return(-1);
                    209:     }
                    210:     oxReqPops(client->datafp2, KopInteger(ob));
                    211:     client->dstate = DSTATE_ANY;
                    212:     break;
                    213:   case SM_executeStringByLocalParser:
                    214:     if (ob.tag != Sdollar) {
                    215:       errorOxmisc2("SM_executeStringByLocalParser : the argument must be a string.");
                    216:       return(-1);
                    217:     }
                    218:     oxReqExecuteStringByLocalParser(client->datafp2,KopString(ob));
                    219:     client->dstate = DSTATE_ANY;
                    220:     break;
                    221:   case SM_executeFunction:
                    222:     if (ob.tag != Sdollar) {
                    223:       errorOxmisc2("SM_executeFunction : the argument must be a string.");
                    224:       return(-1);
                    225:     }
                    226:     oxReqExecuteFunction(client->datafp2,KopString(ob));
                    227:     client->dstate = DSTATE_ANY;
                    228:     break;
                    229:   case SM_popString:
                    230:     oxReqPopString(client->datafp2);
                    231:     client->dstate = DSTATE_ANY;
                    232:     break;
                    233:   case SM_evalName:
                    234:     if (ob.tag != Sdollar) {
                    235:       errorOxmisc2("SM_evalName : the argument must be a string.");
                    236:       return(-1);
                    237:     }
                    238:     oxReqEvalName(client->datafp2,KopString(ob));
                    239:     client->dstate = DSTATE_ANY;
                    240:     break;
                    241:   case SM_setName:
                    242:     if (ob.tag != Sdollar) {
                    243:       errorOxmisc2("SM_setName : the argument must be a string.");
                    244:       return(-1);
                    245:     }
                    246:     oxReqSetName(client->datafp2,KopString(ob));
                    247:     client->dstate = DSTATE_ANY;
                    248:     break;
                    249:   case SM_getsp:
                    250:     oxReqSingleOperand(client->datafp2,SM_getsp);
                    251:     client->dstate = DSTATE_ANY;
                    252:     break;
                    253:   case SM_dupErrors:
                    254:     oxReqSingleOperand(client->datafp2,SM_dupErrors);
                    255:     client->dstate = DSTATE_ANY;
                    256:     break;
                    257:   default:
                    258:     fprintf(MyErrorOut,"func=%d ",func);
                    259:     errorOxmisc2("This function is not implemented.");
                    260:     break;
                    261:   }
                    262:   fp2fflush(client->datafp2);
                    263:   return(0);
                    264: }
                    265:
                    266: struct object KoxCreateClient(struct object ip,
1.12      takayama  267:                               struct object portStream,
                    268:                               struct object portControl)
1.1       maekawa   269: {
                    270:   struct object rob;
                    271:   oxclientp client;
                    272:   rob.tag = Snull;
                    273:   if (ip.tag != Sdollar) {
                    274:     errorOxmisc2("KoxCreateClient(): The first argument must be a hostname given by a string.");
                    275:     return(rob);
                    276:   }
                    277:   if (portStream.tag == Sdollar) {
                    278:     client = oxCreateClientFile(KopString(ip),KopString(portStream),
1.12      takayama  279:                                 "/dev/null","w");
1.1       maekawa   280:     if (client == NULL) {
                    281:       errorOxmisc2("KoxCreateClient(): Open error.");
                    282:       return(rob);
                    283:     }
                    284:     rob = newObjectArray(N_OF_CLIENT_FIELDS);
                    285:     oxClientToObject(client,rob);
                    286:     return(rob);
                    287:   }
                    288:
                    289:   if (portStream.tag != Sinteger) {
                    290:     errorOxmisc2("KoxCreateClient(): The second argument must be a port number given in an integer.");
                    291:     return(rob);
                    292:   }
                    293:   if (portControl.tag != Sinteger) {
                    294:     errorOxmisc2("KoxCreateClient(): The third argument must be a port number given in an integer.");
                    295:     return(rob);
                    296:   }
                    297:   client = oxCreateClient(KopString(ip),KopInteger(portStream),KopInteger(portControl));
                    298:   if (client == NULL) {
                    299:     errorOxmisc2("KoxCreateClient(): Open error.");
                    300:     return(rob);
                    301:   }
                    302:   rob = newObjectArray(N_OF_CLIENT_FIELDS);
                    303:   oxClientToObject(client,rob);
1.14      takayama  304:   oxClientListUpdate(rob);
1.1       maekawa   305:   return(rob);
                    306: }
                    307:
                    308: static int isItClientObject(struct object ob)
                    309: {
                    310:   int size,i;
                    311:   struct object ee[N_OF_CLIENT_FIELDS];
                    312:   if (ob.tag != Sarray) {
                    313:     return(0);
                    314:   }
                    315:   size = getoaSize(ob);
                    316:   if (size != N_OF_CLIENT_FIELDS) return(0);
                    317:   for (i=0; i<N_OF_CLIENT_FIELDS; i++) {
                    318:     ee[i] = getoa(ob,i);
                    319:   }
                    320:
                    321:   if (ee[0].tag != Sdollar) return(0);
                    322:   if (strcmp(KopString(ee[0]),"client")!=0) return(0);
                    323:
                    324:   if (ee[1].tag != Sfile) return(0);
                    325:   if (strcmp((ee[1]).lc.str,MAGIC2) != 0) return(0);
                    326:
                    327:   for (i=2; i<=9; i++) {
                    328:     if (ee[i].tag != Sinteger) return(0);
                    329:   }
                    330:   return(1);
                    331: }
                    332:
                    333:
                    334: struct object KoxIsThereErrorClient(struct object ob)
                    335: {
                    336:   struct object rob;
                    337:   int ans;
                    338:   int size;
                    339:   oxclient cc;
                    340:   rob.tag = Snull;
                    341:   if (!isItClientObject(ob)) {
                    342:     errorOxmisc2("KoxIsThereErrorClient(): the argument must be an array for client object.");
                    343:     return(rob);
                    344:   }
                    345:   if (oxObjectToClient(ob,&cc) == -1) return(KpoInteger(-1));
                    346:   ans = oxIsThereErrorClient(&cc);
                    347:   return(KpoInteger(ans));
                    348: }
                    349:
                    350: int oxClientToObject(oxclientp client,struct object rob)
                    351: {
                    352:   struct object ob;
                    353:   if (client == NULL) return;
                    354:   /* rob = newObjectArray(N_OF_CLIENT_FIELDS); */
                    355:   if (rob.tag != Sarray) {
                    356:     errorOxmisc2("oxClientToObject(): the second argument must be an array.");
                    357:     return(-1);
                    358:   }
                    359:   if (getoaSize(rob) != N_OF_CLIENT_FIELDS) {
                    360:     errorOxmisc2("oxClientToObject(): the second argument must be an array of size N_OF_CLIENT_FIELDS.");
                    361:     return(-1);
                    362:   }
                    363:
                    364:   ob = KpoString("client");
                    365:   putoa(rob,0,ob);
                    366:
                    367:   ob.tag = Sfile; ob.lc.str = MAGIC2; ob.rc.voidp = (void *)(client->datafp2);
                    368:   putoa(rob,1,ob);
                    369:
                    370:   putoa(rob,2,KpoInteger(client->dataport));
                    371:   putoa(rob,3,KpoInteger(client->controlfd));
                    372:   putoa(rob,4,KpoInteger(client->controlport));
                    373:   putoa(rob,5,KpoInteger(client->dstate));
                    374:   putoa(rob,6,KpoInteger(client->cstate));
                    375:   putoa(rob,7,KpoInteger(client->humanio));
                    376:   putoa(rob,8,KpoInteger(client->id));
                    377:   putoa(rob,9,KpoInteger(client->type));
                    378:   if (client->mathcapObjp == NULL) {
                    379:     putoa(rob,10,NullObject);
                    380:   }else{
                    381:     putoa(rob,10,*((struct object *)(client->mathcapObjp)));
                    382:   }
                    383:   putoa(rob,11,KpoInteger(client->engineByteOrder));
                    384:   putoa(rob,12,KpoInteger(client->controlByteOrder));
1.16      takayama  385:   putoa(rob,13,KpoInteger(client->engineID));
1.1       maekawa   386:   return(0);
                    387: }
                    388:
                    389: int oxObjectToClient(struct object ob,oxclientp cp)
                    390: {
                    391:   struct object ob1;
                    392:   struct object *obp;
                    393:   if (cp == NULL) {
                    394:     errorOxmisc2("oxObjectToClient(): the second argument is NULL");
                    395:     return(-1);
                    396:   }
                    397:   if (!isItClientObject(ob)) {
                    398:     errorOxmisc2("oxObjectToClient(): the first argument is not client object.");
                    399:     oxInitClient(cp);
                    400:     return(-1);
                    401:   }
                    402:
                    403:   ob1 = getoa(ob,1);
                    404:   cp->datafp2 = (FILE2 *) (ob1.rc.voidp);
                    405:
                    406:   ob1 = getoa(ob,2);
                    407:   cp->dataport = KopInteger(ob1);
                    408:
                    409:   ob1 = getoa(ob,3);
                    410:   cp->controlfd = KopInteger(ob1);
                    411:
                    412:   ob1 = getoa(ob,4);
                    413:   cp->controlport = KopInteger(ob1);
                    414:
                    415:   ob1 = getoa(ob,5);
                    416:   cp->dstate = KopInteger(ob1);
                    417:
                    418:   ob1 = getoa(ob,6);
                    419:   cp->cstate = KopInteger(ob1);
                    420:
                    421:   ob1 = getoa(ob,7);
                    422:   cp->humanio = KopInteger(ob1);
                    423:
                    424:   ob1 = getoa(ob,8);
                    425:   cp->id = KopInteger(ob1);
                    426:
                    427:   ob1 = getoa(ob,9);
                    428:   cp->type = KopInteger(ob1);
                    429:
                    430:   ob1 = getoa(ob,10);
                    431:   if (ob1.tag == Snull) {
                    432:     cp->mathcapObjp = NULL;
                    433:   }else{
                    434:     obp = (struct object *) sGC_malloc(sizeof(struct object));
                    435:     *obp = ob1;
                    436:     cp->mathcapObjp = (void *)obp;
                    437:   }
                    438:   ob1 = getoa(ob,11);
                    439:   cp->engineByteOrder = KopInteger(ob1);
                    440:   ob1 = getoa(ob,12);
                    441:   cp->controlByteOrder = KopInteger(ob1);
1.16      takayama  442:
                    443:   ob1 = getoa(ob,13);
                    444:   cp->engineID = KopInteger(ob1);
1.1       maekawa   445:
                    446:
                    447:   return(0);
                    448: }
                    449:
                    450: struct object KoxReq(struct object client,
1.12      takayama  451:                      struct object func,
                    452:                      struct object ob1)
1.1       maekawa   453: {
                    454:   int ans;
                    455:   static oxclientp cc1 = NULL;
                    456:   struct object rob;
                    457:   rob.tag = Snull;
                    458:   if (cc1 == NULL) {
                    459:     cc1 = (oxclientp) mymalloc(sizeof(oxclient));
                    460:     if (cc1 == NULL) {
                    461:       errorOxmisc2("KoxReq(): no more memory.");
                    462:       return(rob);
                    463:     }
                    464:     oxInitClient(cc1);
                    465:   }
                    466:
                    467:   if (oxObjectToClient(client,cc1) == -1) return(rob);
                    468:   if (cc1 == NULL) {
                    469:     errorOxmisc2("KoxReq(): the first argument must be a client object.");
                    470:     return(rob);
                    471:   }
                    472:   if (func.tag != Sinteger) {
                    473:     errorOxmisc2("KoxReq(): the second argument must be an integer.");
                    474:     return(rob);
                    475:   }
                    476:   ans = oxReq(cc1,KopInteger(func),ob1);
                    477:   /* synchronize cc1 and client. */
                    478:   oxClientToObject(cc1,client);
                    479:
                    480:   return(KpoInteger(ans));
                    481: }
                    482:
                    483: struct object KoxGet(struct object client)
                    484: {
                    485:   int ans,k;
                    486:   static oxclientp cc1 = NULL;
                    487:   struct object rob;
                    488:   rob.tag = Snull;
                    489:   if (cc1 == NULL) {
                    490:     cc1 = (oxclientp) mymalloc(sizeof(oxclient));
                    491:     if (cc1 == NULL) {
                    492:       errorOxmisc2("KoxGet(): no more memory.");
                    493:       return(rob);
                    494:     }
                    495:     oxInitClient(cc1);
                    496:   }
                    497:
                    498:   if (oxObjectToClient(client,cc1) == -1) return(rob);
                    499:   if (cc1 == NULL) {
                    500:     errorOxmisc2("KoxGet(): the first argument must be a client object.");
                    501:     return(rob);
                    502:   }
                    503:
                    504:   ans = oxGet(cc1,&rob,&k);
                    505:   /* synchronize cc1 and client. */
                    506:   oxClientToObject(cc1,client);
                    507:
                    508:   if (k) return(rob);
                    509:   else {
                    510:     return(KpoInteger(ans));
                    511:   }
                    512: }
                    513:
                    514: struct object KoxGetFromControl(struct object client)
                    515: {
                    516:   int ans;
                    517:   static oxclientp cc1 = NULL;
                    518:   struct object rob;
                    519:   rob.tag = Snull;
                    520:   if (cc1 == NULL) {
                    521:     cc1 = (oxclientp) mymalloc(sizeof(oxclient));
                    522:     if (cc1 == NULL) {
                    523:       errorOxmisc2("KoxGetFromControl(): no more memory.");
                    524:       return(rob);
                    525:     }
                    526:     oxInitClient(cc1);
                    527:   }
                    528:
                    529:   if (oxObjectToClient(client,cc1) == -1) return(rob);
                    530:   if (cc1 == NULL) {
                    531:     errorOxmisc2("KoxGetFromControl(): the first argument must be a client object.");
                    532:     return(rob);
                    533:   }
                    534:
                    535:   ans = oxGetFromControl(cc1);
                    536:   /* synchronize cc1 and client. */
                    537:   oxClientToObject(cc1,client);
                    538:
                    539:   return(KpoInteger(ans));
                    540: }
                    541:
                    542: struct object KoxMultiSelect(struct object oclients,struct object t)
                    543: {
                    544:   static int first = 1;
                    545:   static int csize = 0;
                    546:   static oxclientp *clients = NULL;
                    547:   oxclientp cc1;
                    548:   struct object rob;
                    549:   int i;
                    550:   int tt;
                    551:   struct object ob1;
                    552:   struct object ob2;
                    553:   struct object ob0;
                    554:   int size;
                    555:   int ans;
                    556:   int dataready[1024];
                    557:   int controlready[1024];
                    558:
                    559:   rob.tag = Snull;
                    560:   if (oclients.tag != Sarray) {
                    561:     errorOxmisc2("KoxMultiSelect(): the first argument must be an array.");
                    562:     return(rob);
                    563:   }
                    564:   size = getoaSize(oclients);
                    565:   if (first) {
                    566:     first = 0; csize = size;
                    567:     clients = (oxclientp *)mymalloc(sizeof(oxclientp)*(size+1));
1.12      takayama  568:     if (clients == NULL) {
1.1       maekawa   569:       errorOxmisc2("KoxMultiSelect(): no more memory.");
                    570:       return(rob);
                    571:     }
                    572:     for (i=0; i<size; i++) {
                    573:       clients[i] =  (oxclientp) mymalloc(sizeof(oxclient));
                    574:       if (clients[i] == NULL) {
1.12      takayama  575:         errorOxmisc2("KoxMultiSelect(): no more memory.");
                    576:         return(rob);
1.1       maekawa   577:       }
                    578:       oxInitClient(clients[i]);
                    579:     }
                    580:   }
                    581:   if (csize < size)  {
                    582:     first = 1;
                    583:     return(KoxMultiSelect(oclients,t));
                    584:   }
                    585:   for (i=0; i<size; i++) {
                    586:     ob0 = getoa(oclients,i);
                    587:     if (oxObjectToClient(ob0,clients[i]) == -1) return(rob);
                    588:   }
                    589:   if (t.tag != Sinteger) {
                    590:     errorOxmisc2("KoxMultiSelect(): the second argument must be an integer.");
                    591:   }
                    592:   tt = KopInteger(t);
                    593:   ans = oxclientMultiSelect(clients,dataready,controlready,size,tt);
                    594:   /* synchronize oclients and clients. */
                    595:   for (i=0; i<size; i++) {
                    596:     ob0 = getoa(oclients,i);
                    597:     oxClientToObject(clients[i],ob0);
                    598:     putoa(oclients,i,ob0);
                    599:   }
                    600:   rob = newObjectArray(3);
                    601:   putoa(rob,0,KpoInteger(ans));
                    602:   ob1 = newObjectArray(size);
                    603:   ob2 = newObjectArray(size);
                    604:   for (i=0; i<size; i++) {
                    605:     putoa(ob1,i,KpoInteger(dataready[i]));
                    606:     putoa(ob2,i,KpoInteger(controlready[i]));
                    607:   }
                    608:   putoa(rob,1,ob1);
                    609:   putoa(rob,2,ob2);
                    610:   return(rob);
                    611: }
                    612:
                    613: struct object KoxWatch(struct object client,struct object f)
1.12      takayama  614:      /* f is not used for now. It should be log file. */
1.1       maekawa   615: {
                    616:   int ans,k;
                    617:   static oxclientp cc1 = NULL;
                    618:   struct object rob;
1.3       takayama  619:   extern int WatchStream;
1.1       maekawa   620:   rob.tag = Snull;
1.3       takayama  621:   if (client.tag == Sinteger) {
1.12      takayama  622:     if (KopInteger(client)) {
                    623:       WatchStream = 1;
                    624:     }else{
                    625:       WatchStream = 0;
                    626:     }
                    627:     return;
1.3       takayama  628:   }
1.1       maekawa   629:   if (cc1 == NULL) {
                    630:     cc1 = (oxclientp) mymalloc(sizeof(oxclient));
                    631:     if (cc1 == NULL) {
                    632:       errorOxmisc2("KoxWatch(): no more memory.");
                    633:       return(rob);
                    634:     }
                    635:     oxInitClient(cc1);
                    636:   }
                    637:
                    638:   if (oxObjectToClient(client,cc1) == -1) return(rob);
                    639:   if (cc1 == NULL) {
                    640:     errorOxmisc2("KoxWatch(): the first argument must be a client object.");
                    641:     return(rob);
                    642:   }
                    643:
                    644:   k = fp2watch(cc1->datafp2,stdout);
                    645:   /* synchronize cc1 and client. */
                    646:   oxClientToObject(cc1,client);
                    647:
                    648:   return(KpoInteger(ans));
                    649: }
                    650:
1.18      takayama  651: struct object KoxLog(struct object client,struct object in,struct object out)
                    652: {
                    653:   int ans,k;
                    654:   static oxclientp cc1 = NULL;
                    655:   struct object rob;
                    656:   rob.tag = Snull;
                    657:   if (cc1 == NULL) {
                    658:     cc1 = (oxclientp) mymalloc(sizeof(oxclient));
                    659:     if (cc1 == NULL) {
                    660:       errorOxmisc2("KoxLog(): no more memory.");
                    661:       return(rob);
                    662:     }
                    663:     oxInitClient(cc1);
                    664:   }
                    665:
                    666:   if (oxObjectToClient(client,cc1) == -1) return(rob);
                    667:   if (cc1 == NULL) {
                    668:     errorOxmisc2("KoxLog(): the first argument must be a client object.");
                    669:     return(rob);
                    670:   }
                    671:
                    672:   if (in.tag != Sfile) {
                    673:        errorOxmisc2("KoxLog(): the second argument is not a file object.");
                    674:        return rob;
                    675:   }
                    676:   if (out.tag != Sfile) {
                    677:        errorOxmisc2("KoxLog(): the third argument is not a file object.");
                    678:        return rob;
                    679:   }
                    680:   k = fp2log(cc1->datafp2,in.rc.file,out.rc.file);
1.19    ! takayama  681:   fputc(cc1->engineByteOrder,out.rc.file); /* Output engineByteOrder. */
1.18      takayama  682:   /* synchronize cc1 and client. */
                    683:   oxClientToObject(cc1,client);
                    684:
                    685:   return(KpoInteger(ans));
                    686: }
                    687:
                    688: struct object KoxLogStop(struct object client) {
                    689:   static oxclientp cc1 = NULL;
                    690:   struct object rob;
                    691:   rob.tag = Snull;
                    692:   if (cc1 == NULL) {
                    693:     cc1 = (oxclientp) mymalloc(sizeof(oxclient));
                    694:     if (cc1 == NULL) {
                    695:       errorOxmisc2("KoxLog(): no more memory.");
                    696:       return(rob);
                    697:     }
                    698:     oxInitClient(cc1);
                    699:   }
                    700:
                    701:   if (oxObjectToClient(client,cc1) == -1) return(rob);
                    702:   if (cc1 == NULL) {
                    703:     errorOxmisc2("KoxLog(): the first argument must be a client object.");
                    704:     return(rob);
                    705:   }
                    706:   return(KpoInteger(fp2stopLog(cc1->datafp2)));
                    707: }
1.1       maekawa   708:
                    709: struct object KoxCloseClient(struct object client) {
                    710:   oxclientp cc1 = NULL;
                    711:   oxclient cc;
                    712:   struct object rob;
                    713:   rob.tag = Snull;
                    714:   cc1  = &cc;
                    715:   if (oxObjectToClient(client,cc1) == -1) return(rob);
                    716:   if (cc1 == NULL) {
                    717:     errorOxmisc2("KoxCloseClient(): the first argument must be a client object.");
                    718:     return(rob);
                    719:   }
                    720:
                    721:   fp2fflush(cc1->datafp2);
                    722:   if (cc1->humanio) {
                    723:     /* Do not close the file. */
                    724:     return(KpoInteger(0));
                    725:   }
                    726:   switch (cc1->type) {
                    727:   case CLIENT_SOCKET:
                    728:     fp2fclose(cc1->datafp2);
                    729:     close(cc1->controlfd);
                    730:     break;
                    731:   case CLIENT_FILE:
                    732:     fp2fclose(cc1->datafp2);
                    733:     close(cc1->controlfd);
                    734:     break;
                    735:   default:
                    736:     errorOxmisc2("Unknown client->type\n");
                    737:     break;
                    738:   }
1.14      takayama  739:   oxClientListRemove(client);
1.1       maekawa   740:   return(KpoInteger(0));
                    741:
                    742: }
                    743:
                    744: static int cmoCheck00(struct object obj,int cmo[], int n) {
                    745:   int i,j,m;
                    746:   int ttt;
                    747: #define CHECK00_N  4098      /* look up stackm.h and kclass.h */
                    748:   static int typeTrans[CHECK00_N];
                    749:   static int init = 0;
1.5       takayama  750:   /* if n == 0, report the cmo tag of the object obj.
1.12      takayama  751:      If it cannot be translated to cmo, then return -1. */
1.5       takayama  752:
1.1       maekawa   753:   if (!init) {
                    754:     for (i=0; i<CHECK00_N; i++) {
                    755:       typeTrans[i] = 0;  /* unknown cmo number */
                    756:     }
                    757:     typeTrans[Snull] = CMO_NULL;
                    758:     typeTrans[Sinteger] = CMO_INT32;
                    759:     typeTrans[Sdollar] = CMO_STRING;
                    760:     if (OxVersion >= 199907170) {
                    761:       typeTrans[SuniversalNumber] = CMO_ZZ;
                    762:     }else{
                    763:       typeTrans[SuniversalNumber] = CMO_ZZ_OLD;
                    764:     }
                    765:     typeTrans[Sarray] = CMO_LIST;
                    766:     /* typeTrans[Spoly] = CMO_DMS;  */
                    767:     typeTrans[Spoly] = CMO_DISTRIBUTED_POLYNOMIAL;
                    768:     typeTrans[Sdouble] = CMO_64BIT_MACHINE_DOUBLE;
                    769:     typeTrans[CLASSNAME_ERROR_PACKET]   = CMO_ERROR2;
                    770:     typeTrans[CLASSNAME_mathcap] = CMO_MATHCAP;
                    771:     typeTrans[CLASSNAME_indeterminate] = CMO_INDETERMINATE;
                    772:     typeTrans[CLASSNAME_tree] = CMO_TREE;
                    773:     typeTrans[CLASSNAME_recursivePolynomial] = CMO_RECURSIVE_POLYNOMIAL;
                    774:     typeTrans[CLASSNAME_polynomialInOneVariable] = CMO_POLYNOMIAL_IN_ONE_VARIABLE;
                    775:     init = 1;
                    776:   }
                    777:   ttt = typeTrans[obj.tag];
                    778:   if (obj.tag == Sclass) {
                    779:     ttt = typeTrans[ectag(obj)];
                    780:   }
1.5       takayama  781:   /* Only report the cmo tag. */
                    782:   if (n == 0) {
1.12      takayama  783:     if (ttt == 0) return(-1);
                    784:     else return(ttt);
1.5       takayama  785:   }
1.1       maekawa   786:
                    787:   for (i=0; i<n; i++) {
                    788:     if (ttt == cmo[i]) {
                    789:       if (ttt != CMO_LIST) return(1);
                    790:       else {
1.12      takayama  791:         m = getoaSize(obj);
                    792:         for (j=0; j<m; j++) {
                    793:           if (!cmoCheck00(getoa(obj,j),cmo,n)) return(0);
                    794:         }
                    795:         return(1);
1.1       maekawa   796:       }
                    797:     }
                    798:   }
                    799:   if (DebugMathCap) {
                    800:     if (DebugMathCap && 1) {
                    801:       fprintf(stderr,"Type translation table (internal object tag --> CMO tag)\n");
                    802:       for (i=0; i<20; i++) {
1.12      takayama  803:         printf("%d ", typeTrans[i]);
1.1       maekawa   804:       }
                    805:       printf("\n");
                    806:     }
                    807:     fprintf(stderr,"The type of the argument object in sm1 is %d.\n",obj.tag);
                    808:     fprintf(stderr,"The type of the argument object in CMO is %d.\n",ttt);
                    809:     fprintf(stderr,"Available CMO tags in mathcap= %d elements : [ ",n);
                    810:     for (i=0; i<n; i++) {
                    811:       fprintf(stderr," %d ",cmo[i]);
                    812:     }
                    813:     fprintf(stderr," ] \n");
                    814:   }
                    815:   return(0);
                    816: }
                    817:
                    818: int cmoCheckMathCap(struct object obj, struct object *obp)
                    819: {
                    820:   struct object mathcap;
                    821:   struct object cmolist;
1.2       takayama  822:   struct object mathcapMain;
1.4       takayama  823:   struct object mathcapThird;
                    824:   struct object ox;
                    825:   struct object oxtag;
1.2       takayama  826:   struct object ob0;
1.4       takayama  827:   int oxsize;
1.1       maekawa   828:   int n;
                    829:   int i;
                    830: #define CMO_CHECK_MATH_CAP_LIST_SIZE 1024
                    831:   int cmo[CMO_CHECK_MATH_CAP_LIST_SIZE];
                    832:   if (obp == NULL) return(1);
1.2       takayama  833:   /* printObject(*obp,0,stderr); for debug*/
1.1       maekawa   834:   if (obp->tag != Sarray) {
                    835:     fprintf(stderr,"cmoCheckMathCap: the mathcap obj is \n");
                    836:     printObject(*obp,0,stderr);
                    837:     fprintf(stderr,"\n");
                    838:     errorOxmisc2("cmoCheckMathCap: format error in the client->mathcapObjp field.\n");
                    839:   }
                    840:   mathcap = *obp;
1.2       takayama  841:   /* Example of mathcap
1.12      takayama  842:      [    $mathcap-object$ ,
                    843:      [    [    199909080 , $Ox_system=ox_sm1.plain$ , $Version=2.991106$ ,
                    844:      $HOSTTYPE=i386$ ]  ,
                    845:      [    262 , 263 , 264 , 265 , 266 , 268 , 269 , 272 , 273 , 275 ,
                    846:      276 ]  ,
                    847:      [    [    514  , [    2130706434 , 1 , 2 , 4 , 5 , 17 , 19 , 20 , 22 , 23 , 24 , 25 , 26 , 30 , 31 , 60 , 61 , 27 , 33 , 40 , 16 , 34 ] ] ]  ]  ]
1.2       takayama  848:   */
                    849:
1.1       maekawa   850:   n = getoaSize(mathcap);
                    851:   if (n < 2) {
                    852:     fprintf(stderr,"cmoCheckMathCap: the mathcap obj is \n");
                    853:     printObject(*obp,0,stderr);
                    854:     fprintf(stderr,"\n");
                    855:     errorOxmisc2("cmoCheckMathCap: length of mathcap is wrong in the client->mathcapObjp field.\n");
                    856:   }
1.2       takayama  857:   ob0 = getoa(mathcap,0);
                    858:   if (ob0.tag != Sdollar) {
                    859:     fprintf(stderr,"cmoCheckMathCap: the mathcap obj is \n");
                    860:     printObject(*obp,0,stderr);
                    861:     fprintf(stderr,"\n");
                    862:     errorOxmisc2("cmoCheckMathCap: The first field must be the string mathcap-object.\n");
                    863:   }
                    864:   if (strcmp(KopString(ob0),"mathcap-object") != 0) {
                    865:     fprintf(stderr,"cmoCheckMathCap: the mathcap obj is \n");
                    866:     printObject(*obp,0,stderr);
                    867:     fprintf(stderr,"\n");
                    868:     errorOxmisc2("cmoCheckMathCap: The mathcap must be of the form [(mathcap-object) [...]]\n");
                    869:   }
                    870:
1.1       maekawa   871:   /* I should check
1.12      takayama  872:      getoa(getoa(mathcap,1),2)
1.1       maekawa   873:      contains OX_DATA.
                    874:      It has not yet implemented.
                    875:   */
1.2       takayama  876:   mathcapMain = getoa(mathcap,1);
                    877:   if (mathcapMain.tag != Sarray) {
                    878:     fprintf(stderr,"cmoCheckMathCap: mathcap[1] is \n");
                    879:     printObject(mathcapMain,0,stderr);
                    880:     fprintf(stderr,"\n");
                    881:     errorOxmisc2("cmoCheckMathCap: format error in the (client->mathcapObjp)[1] field. It should be an array.\n");
                    882:   }
                    883:   if (getoaSize(mathcapMain) < 3) {
                    884:     fprintf(stderr,"cmoCheckMathCap: mathcap[1] is \n");
                    885:     printObject(mathcapMain,0,stderr);
                    886:     fprintf(stderr,"\n");
                    887:     errorOxmisc2("cmoCheckMathCap: format error in the (client->mathcapObjp)[1] field. It should be an array of which length is more than 2.\n");
                    888:   }
1.4       takayama  889:   mathcapThird = getoa(mathcapMain,2);
                    890:   oxsize = getoaSize(mathcapThird);
                    891:   for (i=0; i<oxsize; i++) {
                    892:     ox = getoa(mathcapThird,i);
                    893:     if (ox.tag != Sarray) {
                    894:       fprintf(stderr,"cmoCheckMathCap: the mathcap obj is \n");
                    895:       printObject(*obp,0,stderr);
                    896:       fprintf(stderr,"\n");
                    897:       errorOxmisc2("cmoCheckMathCap: the third element of mathcap is a list of lists.");
                    898:     }
                    899:     if (getoaSize(ox) != 0) {
                    900:       oxtag = getoa(ox,0);
                    901:       if (oxtag.tag != Sinteger) {
1.12      takayama  902:         fprintf(stderr,"cmoCheckMathCap: the mathcap obj is \n");
                    903:         printObject(*obp,0,stderr);
                    904:         fprintf(stderr,"\n");
                    905:         errorOxmisc2("cmoCheckMathCap: the third element of mathcap must be [OX_DATA_xxx, [  ]].");
1.4       takayama  906:       }
                    907:       if (KopInteger(oxtag) == OX_DATA) {
1.12      takayama  908:         if (getoaSize(ox) > 1) {
                    909:           cmolist = getoa(ox,1);
                    910:           if (cmolist.tag != Sarray) {
                    911:             fprintf(stderr,"cmoCheckMathCap: the mathcap obj is \n");
                    912:             printObject(*obp,0,stderr);
                    913:             fprintf(stderr,"\n");
                    914:             errorOxmisc2("cmoCheckMathCap: mathcap[1] must be an array of integers.\n");
                    915:           }
                    916:           n = getoaSize(cmolist);
                    917:           if (n > CMO_CHECK_MATH_CAP_LIST_SIZE) {
                    918:             errorOxmisc2("cmoCheckMathCap: Too big cmo list.\n");
                    919:           }
                    920:           for (i=0; i<n; i++) {
                    921:             cmo[i] = KopInteger(getoa(cmolist,i));
                    922:           }
                    923:         }else{
                    924:           fprintf(stderr,"cmoCheckMathCap: the mathcap obj is \n");
                    925:           printObject(*obp,0,stderr);
                    926:           fprintf(stderr,"\nox=");
                    927:           printObject(ox,0,stderr);
                    928:           errorOxmisc2("cmoCheckMathCap: [OX_DATA, cmolist]");
                    929:         }
1.4       takayama  930:       }
                    931:     }
1.1       maekawa   932:   }
                    933:   return(cmoCheck00(obj,cmo,n));
                    934: }
                    935:
1.12      takayama  936:
1.1       maekawa   937: struct object KoxGenPortFile(void) {
                    938:   struct object ob;
                    939:   ob = KpoString(oxGenPortFile());
                    940:   return(ob);
                    941: }
                    942: void KoxRemovePortFile(void) {
                    943:   oxRemovePortFile();
                    944: }
                    945:
                    946: void oxPushMathCap(struct mathCap *mathcap)
                    947: {
                    948:   struct object rob;
                    949:   rob = newMathCap(mathcap);
                    950:   Kpush(rob);
                    951: }
                    952:
                    953: struct object KoxGenPass(void) {
                    954:   struct object rob;
                    955:   rob = KpoString(oxGenPass());
                    956:   return(rob);
                    957: }
                    958:
                    959: struct object KoxGetPort(struct object host)
                    960: {
                    961:   struct object rob;
                    962:   int fdStream, fdControl;
                    963:   int portStream, portControl;
                    964:   extern int OpenedSocket;
                    965:   char *sname;
                    966:   rob = NullObject;
                    967:   if (host.tag != Sdollar) {
                    968:     errorOxmisc2("KoxGetPort: argument is not a string.");
                    969:     return(rob);
                    970:   }
                    971:   sname = KopString(host);
                    972:   fdControl = socketOpen(sname,0);
                    973:   portControl = OpenedSocket;
                    974:   fdStream = socketOpen(sname,0);
                    975:   portStream = OpenedSocket;
                    976:   rob = newObjectArray(4);
                    977:   putoa(rob,0,KpoInteger(fdStream));
                    978:   putoa(rob,1,KpoInteger(portStream));
                    979:   putoa(rob,2,KpoInteger(fdControl));
                    980:   putoa(rob,3,KpoInteger(portControl));
1.10      takayama  981:   return(rob);
                    982: }
                    983: struct object KoxGetPort1(struct object host)
                    984: {
                    985:   struct object rob;
                    986:   int fdStream;
                    987:   int portStream;
                    988:   extern int OpenedSocket;
                    989:   char *sname;
                    990:   rob = NullObject;
                    991:   if (host.tag != Sdollar) {
                    992:     errorOxmisc2("KoxGetPort1: argument is not a string.");
                    993:     return(rob);
                    994:   }
                    995:   sname = KopString(host);
                    996:   fdStream = socketOpen(sname,0);
                    997:   portStream = OpenedSocket;
                    998:   rob = newObjectArray(2);
                    999:   putoa(rob,0,KpoInteger(fdStream));
                   1000:   putoa(rob,1,KpoInteger(portStream));
1.1       maekawa  1001:   return(rob);
                   1002: }
                   1003:
                   1004: struct object KoxCreateClient2(struct object peer,
1.12      takayama 1005:                                struct object ipmask,
                   1006:                                struct object pass)
1.1       maekawa  1007: {
                   1008:   struct object rob;
                   1009:   oxclientp client;
                   1010:   int fdStream, portStream, fdControl, portControl;
                   1011:   int i;
                   1012:   struct object ob1;
                   1013:   rob.tag = Snull;
                   1014:   if (peer.tag != Sarray) {
                   1015:     errorOxmisc2("KoxCreateClient2(): The first argument must be an array [fdStream, portStream, fdControl, portControl]");
                   1016:     return(rob);
                   1017:   }
                   1018:   if (getoaSize(peer) != 4) {
                   1019:     errorOxmisc2("KoxCreateClient2(): The first argument must be an array [fdStream, portStream, fdControl, portControl] of size 4.");
                   1020:     return(rob);
                   1021:   }
                   1022:   for (i=0; i<4; i++) {
                   1023:     ob1 = getoa(peer,i);
                   1024:     if (ob1.tag != Sinteger) {
                   1025:       errorOxmisc2("KoxCreateClient2(): The element of the first argument must be an integer.");
                   1026:     }
                   1027:   }
                   1028:   fdStream = KopInteger(getoa(peer,0));
                   1029:   portStream = KopInteger(getoa(peer,1));
                   1030:   fdControl = KopInteger(getoa(peer,2));
                   1031:   portControl = KopInteger(getoa(peer,3));
                   1032:
                   1033:   if (ipmask.tag != Sinteger) {
                   1034:     errorOxmisc2("KoxCreateClient2(): ipmask must be an integer.");
                   1035:   }
                   1036:   if (pass.tag != Sdollar) {
                   1037:     errorOxmisc2("KoxCreateClient2(): pass must be a string.");
                   1038:   }
                   1039:
                   1040:   client = oxCreateClient2(fdStream, portStream, fdControl, portControl,
1.12      takayama 1041:                            KopInteger(ipmask), KopString(pass));
1.1       maekawa  1042:   if (client == NULL) {
                   1043:     errorOxmisc2("KoxCreateClient2(): Open error.");
                   1044:     return(rob);
                   1045:   }
                   1046:   rob = newObjectArray(N_OF_CLIENT_FIELDS);
                   1047:   oxClientToObject(client,rob);
1.14      takayama 1048:   oxClientListUpdate(rob);
1.1       maekawa  1049:   return(rob);
1.5       takayama 1050: }
                   1051:
                   1052: int KgetCmoTagOfObject(struct object obj) {
                   1053:   int k;
                   1054:   k=cmoCheck00(obj,(int *)NULL,0);
                   1055:   return(k);
1.1       maekawa  1056: }
                   1057:
                   1058: errorOxmisc2(char *s) {
1.6       takayama 1059:   SET_MYERROROUT;
1.1       maekawa  1060:   fprintf(MyErrorOut,"error in oxmisc2.c: %s\n",s);
                   1061:   errorKan1("%s\n","  ");
1.7       takayama 1062: }
                   1063:
                   1064: struct object KoxPushCMD(struct object client,struct object cmd) {
                   1065:   int ans;
                   1066:   static oxclientp cc1 = NULL;
                   1067:   struct object rob;
                   1068:   rob.tag = Snull;
                   1069:   if (cc1 == NULL) {
                   1070:     cc1 = (oxclientp) mymalloc(sizeof(oxclient));
                   1071:     if (cc1 == NULL) {
                   1072:       errorOxmisc2("KoxReq(): no more memory.");
                   1073:       return(rob);
                   1074:     }
                   1075:     oxInitClient(cc1);  /* BUG: is it fine? */
                   1076:   }
                   1077:
                   1078:   if (oxObjectToClient(client,cc1) == -1) return(rob);
                   1079:   if (cc1 == NULL) {
                   1080:     errorOxmisc2("KoxReq(): the first argument must be a client object.");
                   1081:     return(rob);
                   1082:   }
                   1083:   if (cmd.tag != Sinteger) {
                   1084:     errorOxmisc2("KoxReq(): the second argument must be an integer.");
                   1085:     return(rob);
                   1086:   }
                   1087:   /* BUG: check the mathcap */
                   1088:   oxSendOXheader(cc1->datafp2,OX_COMMAND,SerialOX++);
                   1089:   oxSendInt32(cc1->datafp2,KopInteger(cmd));
                   1090:   /* synchronize cc1 and client. */
                   1091:   oxClientToObject(cc1,client);
                   1092:   return(cmd);
                   1093: }
                   1094:
                   1095: struct object KoxPushCMO(struct object client,struct object ob) {
                   1096:   int ans;
                   1097:   static oxclientp cc1 = NULL;
                   1098:   struct object rob;
                   1099:   rob.tag = Snull;
                   1100:   if (cc1 == NULL) {
                   1101:     cc1 = (oxclientp) mymalloc(sizeof(oxclient));
                   1102:     if (cc1 == NULL) {
                   1103:       errorOxmisc2("KoxReq(): no more memory.");
                   1104:       return(rob);
                   1105:     }
                   1106:     oxInitClient(cc1);  /* BUG: is it fine? */
                   1107:   }
                   1108:
                   1109:   if (oxObjectToClient(client,cc1) == -1) return(rob);
                   1110:   if (cc1 == NULL) {
                   1111:     errorOxmisc2("KoxReq(): the first argument must be a client object.");
                   1112:     return(rob);
                   1113:   }
                   1114:
                   1115:   /* request to the data channel */
                   1116:   if (cc1->dstate != DSTATE_ANY) {
                   1117:     errorOxmisc2("oxPushCMO: cc1->dstate != DSTATE_ANY, data channel is not ready to send data.\n");
                   1118:     return(rob);
                   1119:   }
                   1120:
                   1121:   if (!cmoCheckMathCap(ob,(struct object *)cc1->mathcapObjp)) {
1.12      takayama 1122:     errorOxmisc2("oxPushCMO: your peer does not understand this cmo.\n");
                   1123:     return(rob);
1.7       takayama 1124:   }
                   1125:   oxSendOXheader(cc1->datafp2,OX_DATA,SerialOX++);
                   1126:   cmoObjectToStream2(ob,cc1->datafp2);
                   1127:   /* synchronize cc1 and client. */
                   1128:   oxClientToObject(cc1,client);
                   1129:   return(ob);
1.8       takayama 1130: }
                   1131:
                   1132: oxclientp oxCreateControl_RFC_101(int fdstream,int portStream,
1.12      takayama 1133:                                   int ipmask,char *pass);
1.8       takayama 1134: struct object KoxCreateControl_RFC_101(struct object peer,struct object ipmask,struct object pass)
                   1135: {
                   1136:   struct object rob;
                   1137:   oxclientp client;
                   1138:   int fdStream, portStream;
                   1139:   int i;
                   1140:   struct object ob1;
                   1141:   rob.tag = Snull;
                   1142:   if (peer.tag != Sarray) {
                   1143:     errorOxmisc2("KoxCreateControl_RFC_101(): The first argument must be an array [fdStream, portStream]");
                   1144:     return(rob);
                   1145:   }
                   1146:   if (getoaSize(peer) != 2 ) {
                   1147:     errorOxmisc2("KoxCreateControl_RFC_101(): The first argument must be an array [fdStream, portStream] of size 2.");
                   1148:     return(rob);
                   1149:   }
                   1150:   for (i=0; i<getoaSize(peer); i++) {
                   1151:     ob1 = getoa(peer,i);
                   1152:     if (ob1.tag != Sinteger) {
                   1153:       errorOxmisc2("KoxCreateControl_RFC_101(): The element of the first argument must be an integer.");
                   1154:     }
                   1155:   }
                   1156:   fdStream = KopInteger(getoa(peer,0));
                   1157:   portStream = KopInteger(getoa(peer,1));
                   1158:
                   1159:   if (ipmask.tag != Sinteger) {
                   1160:     errorOxmisc2("KoxCreateControl_RFC_101(): ipmask must be an integer.");
                   1161:   }
                   1162:   if (pass.tag != Sdollar) {
                   1163:     errorOxmisc2("KoxCreateControl_RFC_101(): pass must be a string.");
                   1164:   }
                   1165:
                   1166:   client = oxCreateControl_RFC_101(fdStream, portStream,
1.12      takayama 1167:                                    KopInteger(ipmask), KopString(pass));
1.8       takayama 1168:   if (client == NULL) {
                   1169:     errorOxmisc2("KoxCreateControl_RFC_101(): Open error.");
                   1170:     return(rob);
                   1171:   }
                   1172:   rob = newObjectArray(N_OF_CLIENT_FIELDS);
                   1173:   oxClientToObject(client,rob);
                   1174:   return(rob);
                   1175: }
                   1176:
                   1177: oxclientp oxCreateControl_RFC_101(int fdstream,int portStream,
1.12      takayama 1178:                                   int ipmask,char *pass)
1.8       takayama 1179: {
                   1180:   int v = 0;
                   1181:   int fdControl = -1;
                   1182:   int fdStream = -1;
                   1183:   int m;
                   1184:
                   1185:   char *s;
                   1186:   oxclientp client;
1.13      takayama 1187: #if defined(__CYGWIN__)
                   1188:   extern sigjmp_buf MyEnv_oxmisc;
                   1189: #else
                   1190:   extern jmp_buf MyEnv_oxmisc;
                   1191: #endif
1.8       takayama 1192:   int engineByteOrder;
                   1193:   extern int Quiet;
                   1194:
                   1195:   v = !Quiet;
                   1196:
                   1197:   switch(ipmask) {
                   1198:   case 0:/* only local */
                   1199:     fdStream  = socketAcceptLocal(fdstream);
                   1200:     break;
                   1201:   default:/* any */
                   1202:     fdStream  = socketAccept(fdstream);
                   1203:     break;
                   1204:   }
                   1205:   if (v) fprintf(stderr,"\nControl port %d : Connected.\n",portStream);
                   1206:
                   1207:   if (fdStream == -1 ) {
                   1208:     fprintf(stderr,"\nOpen error in oxCreateControl_RFC_101.\n");
                   1209:     return(NULL);
                   1210:   }
                   1211:
                   1212:   /* Authentication by password. */
                   1213:   m = strlen(pass);
                   1214:   s = (char *)mymalloc(sizeof(char)*(m+1));
                   1215:   read(fdStream,s,m+1); s[m] = '\0';
                   1216:   if (strcmp(s,pass) != 0) {
                   1217:     fprintf(stderr,"oxCreateControl_RFC_101(): password authentication failed for control channel.\n");
                   1218:     close(fdStream);
                   1219:     return(NULL);
                   1220:   }
                   1221:
                   1222:
                   1223:   engineByteOrder = oxSetByteOrder(fdStream);
                   1224:   if (v) fprintf(stderr,"Byte order for control stackmacine is %s.\n",
1.12      takayama 1225:                  (engineByteOrder == 0? "network byte order":
                   1226:                   (engineByteOrder == 1? "little indican":
                   1227:                    "big indian")));
1.8       takayama 1228:
                   1229:
                   1230:   client = (oxclientp) mymalloc(sizeof(oxclient));
                   1231:   oxInitClient(client);
                   1232:   client->datafp2 = fp2open(fdStream);
                   1233:   if (client->datafp2 == NULL) {
                   1234:     fprintf(stderr,"oxCreateControl_RFC_101(): fp2open(fd) failed.\n");
                   1235:     return(NULL);
                   1236:   }
                   1237:   client->dataport = portStream;
                   1238:   client->controlport = -1;
                   1239:   client->controlfd = -1;
1.14      takayama 1240:   client->id = oxGetClientID();
1.11      takayama 1241:   client->type = CLIENT_SOCKET; /* socket */
                   1242:   client->engineByteOrder = engineByteOrder;
                   1243:   client->controlByteOrder = -1;
                   1244:   return(client);
                   1245: }
                   1246:
                   1247: oxclientp oxCreateEngine_RFC_101(int fdstream,int portStream,
1.12      takayama 1248:                                  int ipmask,char *pass, int engineID);
1.11      takayama 1249: struct object KoxCreateEngine_RFC_101(struct object peer,struct object ipmask,struct object pass, struct object engineID)
                   1250: {
                   1251:   struct object rob;
                   1252:   oxclientp client;
                   1253:   int fdStream, portStream;
                   1254:   int i;
                   1255:   struct object ob1;
                   1256:   rob.tag = Snull;
                   1257:   if (peer.tag != Sarray) {
                   1258:     errorOxmisc2("KoxCreateEngine_RFC_101(): The first argument must be an array [fdStream, portStream]");
                   1259:     return(rob);
                   1260:   }
                   1261:   if (getoaSize(peer) != 2 ) {
                   1262:     errorOxmisc2("KoxCreateEngine_RFC_101(): The first argument must be an array [fdStream, portStream] of size 2.");
                   1263:     return(rob);
                   1264:   }
                   1265:   for (i=0; i<getoaSize(peer); i++) {
                   1266:     ob1 = getoa(peer,i);
                   1267:     if (ob1.tag != Sinteger) {
                   1268:       errorOxmisc2("KoxCreateEngine_RFC_101(): The element of the first argument must be an integer.");
                   1269:     }
                   1270:   }
                   1271:   fdStream = KopInteger(getoa(peer,0));
                   1272:   portStream = KopInteger(getoa(peer,1));
                   1273:
                   1274:   if (ipmask.tag != Sinteger) {
                   1275:     errorOxmisc2("KoxCreateEngine_RFC_101(): ipmask must be an integer.");
                   1276:   }
                   1277:   if (pass.tag != Sdollar) {
                   1278:     errorOxmisc2("KoxCreateEngine_RFC_101(): pass must be a string.");
                   1279:   }
                   1280:   if (engineID.tag != Sinteger) {
                   1281:     errorOxmisc2("KoxCreateEngine_RFC_101(): engineID must be an integer.");
                   1282:   }
                   1283:
                   1284:   client = oxCreateEngine_RFC_101(fdStream, portStream,
1.12      takayama 1285:                                   KopInteger(ipmask), KopString(pass),KopInteger(engineID));
1.11      takayama 1286:   if (client == NULL) {
                   1287:     errorOxmisc2("KoxCreateEngine_RFC_101(): Open error.");
                   1288:     return(rob);
                   1289:   }
                   1290:   rob = newObjectArray(N_OF_CLIENT_FIELDS);
                   1291:   oxClientToObject(client,rob);
1.14      takayama 1292:   oxClientListUpdate(rob);
1.11      takayama 1293:   return(rob);
                   1294: }
                   1295:
                   1296: oxclientp oxCreateEngine_RFC_101(int fdstream,int portStream,
1.12      takayama 1297:                                  int ipmask,char *pass,int engineID)
1.11      takayama 1298: {
                   1299:   int v = 0;
                   1300:   int fdControl = -1;
                   1301:   int fdStream = -1;
                   1302:   int m;
                   1303:
                   1304:   char *s;
                   1305:   oxclientp client;
1.13      takayama 1306: #if defined(__CYGWIN__)
                   1307:   extern sigjmp_buf MyEnv_oxmisc;
                   1308: #else
                   1309:   extern jmp_buf MyEnv_oxmisc;
                   1310: #endif
1.11      takayama 1311:   int engineByteOrder;
                   1312:   extern int Quiet;
                   1313:
                   1314:   v = !Quiet;
                   1315:
                   1316:   switch(ipmask) {
                   1317:   case 0:/* only local */
                   1318:     fdStream  = socketAcceptLocal(fdstream);
                   1319:     break;
                   1320:   default:/* any */
                   1321:     fdStream  = socketAccept(fdstream);
                   1322:     break;
                   1323:   }
                   1324:   if (v) fprintf(stderr,"\nEngine port %d : Connected.\n",portStream);
                   1325:
                   1326:   if (fdStream == -1 ) {
                   1327:     fprintf(stderr,"\nOpen error in oxCreateEngine_RFC_101.\n");
                   1328:     return(NULL);
                   1329:   }
                   1330:
                   1331:   /* Authentication by password. */
                   1332:   /*  skip password check for now. BUG.
1.12      takayama 1333:       m = strlen(pass);
                   1334:       s = (char *)mymalloc(sizeof(char)*(m+1));
                   1335:       read(fdStream,s,m+1); s[m] = '\0';
                   1336:       if (strcmp(s,pass) != 0) {
                   1337:       fprintf(stderr,"oxCreateEngine_RFC_101(): password authentication failed for control channel.\n");
                   1338:       close(fdStream);
                   1339:       return(NULL);
                   1340:       }
1.11      takayama 1341:   */
                   1342:
                   1343:   engineByteOrder = oxSetByteOrder(fdStream);
                   1344:   if (v) fprintf(stderr,"Byte order for engine stackmacine is %s.\n",
1.12      takayama 1345:                  (engineByteOrder == 0? "network byte order":
                   1346:                   (engineByteOrder == 1? "little indican":
                   1347:                    "big indian")));
1.11      takayama 1348:
                   1349:
                   1350:   client = (oxclientp) mymalloc(sizeof(oxclient));
                   1351:   oxInitClient(client);
                   1352:   client->datafp2 = fp2open(fdStream);
                   1353:   if (client->datafp2 == NULL) {
                   1354:     fprintf(stderr,"oxCreateEngine_RFC_101(): fp2open(fd) failed.\n");
                   1355:     return(NULL);
                   1356:   }
                   1357:   client->dataport = portStream;
                   1358:   client->controlport = -1;
                   1359:   client->controlfd = -1;
1.16      takayama 1360:   client->id = oxGetClientID();
                   1361:   client->engineID = engineID;
1.8       takayama 1362:   client->type = CLIENT_SOCKET; /* socket */
                   1363:   client->engineByteOrder = engineByteOrder;
                   1364:   client->controlByteOrder = -1;
                   1365:   return(client);
1.1       maekawa  1366: }
1.14      takayama 1367:
                   1368: void oxClientListUpdate(struct object ob) {
                   1369:   int id;
                   1370:   extern struct object OxClientList[];
                   1371:   id = KopInteger(getoa(ob,8));
                   1372:   /* printf("id=%d\n",id); */
                   1373:   if ((id <MAX_N_OF_CLIENT) && (id >= 0)) {
                   1374:        OxClientList[id] = ob;
                   1375:   }else{
                   1376:        errorOxmisc2("oxClientListUpdate(): the client table is full.\n");
                   1377:   }
                   1378: }
                   1379: void oxClientListRemove(struct object ob) {
                   1380:   int id;
                   1381:   extern struct object OxClientList[];
                   1382:   id = KopInteger(getoa(ob,8));
                   1383:   if ((id <MAX_N_OF_CLIENT) && (id >= 0)) {
                   1384:        (OxClientList[id]).tag = Snull;
                   1385:   }else{
                   1386:        /* errorOxmisc2("oxClientListRemove(): the client table is full.\n");*/
                   1387:   }
                   1388: }
1.17      takayama 1389: static void KoxCleanClientList() {
                   1390:   extern int OxClientListn;
                   1391:   extern struct object OxClientList[];
                   1392:   int i,j,n;
                   1393:   struct object ob;
                   1394:   n = 0;
                   1395:   for (i=0; i<OxClientListn; i++) {
                   1396:        if ((OxClientList[i]).tag != Snull) {
                   1397:          if (!isItClientObject(OxClientList[i])) {
                   1398:                (OxClientList[i]).tag = Snull;
                   1399:          }
                   1400:        }
                   1401:   }
                   1402: }
1.14      takayama 1403: struct object KoxGetClientList() {
                   1404:   extern int OxClientListn;
                   1405:   extern struct object OxClientList[];
                   1406:   int i,j,n;
                   1407:   struct object rob;
1.17      takayama 1408:   KoxCleanClientList();
1.14      takayama 1409:   n = 0;
                   1410:   for (i=0; i<OxClientListn; i++) {
                   1411:        if ((OxClientList[i]).tag != Snull) n++;
                   1412:   }
                   1413:   rob = newObjectArray(n);
                   1414:   for (i=0, j=0; i<OxClientListn; i++) {
                   1415:        if ((OxClientList[i]).tag != Snull) {
                   1416:          if (j >= n) {
                   1417:                j=0;
                   1418:                errorOxmisc2("oxGetClientList(): the client table is broken.\n");
                   1419:          }
                   1420:          putoa(rob,j,OxClientList[i]);
                   1421:          j++;
                   1422:        }
                   1423:   }
                   1424:   return rob;
                   1425: }
                   1426:
                   1427:

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