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

1.16    ! takayama    1: /* $OpenXM: OpenXM/src/kan96xx/plugin/oxmisc2.c,v 1.15 2002/11/07 23:52:20 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:
                    651:
                    652: struct object KoxCloseClient(struct object client) {
                    653:   oxclientp cc1 = NULL;
                    654:   oxclient cc;
                    655:   struct object rob;
                    656:   rob.tag = Snull;
                    657:   cc1  = &cc;
                    658:   if (oxObjectToClient(client,cc1) == -1) return(rob);
                    659:   if (cc1 == NULL) {
                    660:     errorOxmisc2("KoxCloseClient(): the first argument must be a client object.");
                    661:     return(rob);
                    662:   }
                    663:
                    664:   fp2fflush(cc1->datafp2);
                    665:   if (cc1->humanio) {
                    666:     /* Do not close the file. */
                    667:     return(KpoInteger(0));
                    668:   }
                    669:   switch (cc1->type) {
                    670:   case CLIENT_SOCKET:
                    671:     fp2fclose(cc1->datafp2);
                    672:     close(cc1->controlfd);
                    673:     break;
                    674:   case CLIENT_FILE:
                    675:     fp2fclose(cc1->datafp2);
                    676:     close(cc1->controlfd);
                    677:     break;
                    678:   default:
                    679:     errorOxmisc2("Unknown client->type\n");
                    680:     break;
                    681:   }
1.14      takayama  682:   oxClientListRemove(client);
1.1       maekawa   683:   return(KpoInteger(0));
                    684:
                    685: }
                    686:
                    687: static int cmoCheck00(struct object obj,int cmo[], int n) {
                    688:   int i,j,m;
                    689:   int ttt;
                    690: #define CHECK00_N  4098      /* look up stackm.h and kclass.h */
                    691:   static int typeTrans[CHECK00_N];
                    692:   static int init = 0;
1.5       takayama  693:   /* if n == 0, report the cmo tag of the object obj.
1.12      takayama  694:      If it cannot be translated to cmo, then return -1. */
1.5       takayama  695:
1.1       maekawa   696:   if (!init) {
                    697:     for (i=0; i<CHECK00_N; i++) {
                    698:       typeTrans[i] = 0;  /* unknown cmo number */
                    699:     }
                    700:     typeTrans[Snull] = CMO_NULL;
                    701:     typeTrans[Sinteger] = CMO_INT32;
                    702:     typeTrans[Sdollar] = CMO_STRING;
                    703:     if (OxVersion >= 199907170) {
                    704:       typeTrans[SuniversalNumber] = CMO_ZZ;
                    705:     }else{
                    706:       typeTrans[SuniversalNumber] = CMO_ZZ_OLD;
                    707:     }
                    708:     typeTrans[Sarray] = CMO_LIST;
                    709:     /* typeTrans[Spoly] = CMO_DMS;  */
                    710:     typeTrans[Spoly] = CMO_DISTRIBUTED_POLYNOMIAL;
                    711:     typeTrans[Sdouble] = CMO_64BIT_MACHINE_DOUBLE;
                    712:     typeTrans[CLASSNAME_ERROR_PACKET]   = CMO_ERROR2;
                    713:     typeTrans[CLASSNAME_mathcap] = CMO_MATHCAP;
                    714:     typeTrans[CLASSNAME_indeterminate] = CMO_INDETERMINATE;
                    715:     typeTrans[CLASSNAME_tree] = CMO_TREE;
                    716:     typeTrans[CLASSNAME_recursivePolynomial] = CMO_RECURSIVE_POLYNOMIAL;
                    717:     typeTrans[CLASSNAME_polynomialInOneVariable] = CMO_POLYNOMIAL_IN_ONE_VARIABLE;
                    718:     init = 1;
                    719:   }
                    720:   ttt = typeTrans[obj.tag];
                    721:   if (obj.tag == Sclass) {
                    722:     ttt = typeTrans[ectag(obj)];
                    723:   }
1.5       takayama  724:   /* Only report the cmo tag. */
                    725:   if (n == 0) {
1.12      takayama  726:     if (ttt == 0) return(-1);
                    727:     else return(ttt);
1.5       takayama  728:   }
1.1       maekawa   729:
                    730:   for (i=0; i<n; i++) {
                    731:     if (ttt == cmo[i]) {
                    732:       if (ttt != CMO_LIST) return(1);
                    733:       else {
1.12      takayama  734:         m = getoaSize(obj);
                    735:         for (j=0; j<m; j++) {
                    736:           if (!cmoCheck00(getoa(obj,j),cmo,n)) return(0);
                    737:         }
                    738:         return(1);
1.1       maekawa   739:       }
                    740:     }
                    741:   }
                    742:   if (DebugMathCap) {
                    743:     if (DebugMathCap && 1) {
                    744:       fprintf(stderr,"Type translation table (internal object tag --> CMO tag)\n");
                    745:       for (i=0; i<20; i++) {
1.12      takayama  746:         printf("%d ", typeTrans[i]);
1.1       maekawa   747:       }
                    748:       printf("\n");
                    749:     }
                    750:     fprintf(stderr,"The type of the argument object in sm1 is %d.\n",obj.tag);
                    751:     fprintf(stderr,"The type of the argument object in CMO is %d.\n",ttt);
                    752:     fprintf(stderr,"Available CMO tags in mathcap= %d elements : [ ",n);
                    753:     for (i=0; i<n; i++) {
                    754:       fprintf(stderr," %d ",cmo[i]);
                    755:     }
                    756:     fprintf(stderr," ] \n");
                    757:   }
                    758:   return(0);
                    759: }
                    760:
                    761: int cmoCheckMathCap(struct object obj, struct object *obp)
                    762: {
                    763:   struct object mathcap;
                    764:   struct object cmolist;
1.2       takayama  765:   struct object mathcapMain;
1.4       takayama  766:   struct object mathcapThird;
                    767:   struct object ox;
                    768:   struct object oxtag;
1.2       takayama  769:   struct object ob0;
1.4       takayama  770:   int oxsize;
1.1       maekawa   771:   int n;
                    772:   int i;
                    773: #define CMO_CHECK_MATH_CAP_LIST_SIZE 1024
                    774:   int cmo[CMO_CHECK_MATH_CAP_LIST_SIZE];
                    775:   if (obp == NULL) return(1);
1.2       takayama  776:   /* printObject(*obp,0,stderr); for debug*/
1.1       maekawa   777:   if (obp->tag != Sarray) {
                    778:     fprintf(stderr,"cmoCheckMathCap: the mathcap obj is \n");
                    779:     printObject(*obp,0,stderr);
                    780:     fprintf(stderr,"\n");
                    781:     errorOxmisc2("cmoCheckMathCap: format error in the client->mathcapObjp field.\n");
                    782:   }
                    783:   mathcap = *obp;
1.2       takayama  784:   /* Example of mathcap
1.12      takayama  785:      [    $mathcap-object$ ,
                    786:      [    [    199909080 , $Ox_system=ox_sm1.plain$ , $Version=2.991106$ ,
                    787:      $HOSTTYPE=i386$ ]  ,
                    788:      [    262 , 263 , 264 , 265 , 266 , 268 , 269 , 272 , 273 , 275 ,
                    789:      276 ]  ,
                    790:      [    [    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  791:   */
                    792:
1.1       maekawa   793:   n = getoaSize(mathcap);
                    794:   if (n < 2) {
                    795:     fprintf(stderr,"cmoCheckMathCap: the mathcap obj is \n");
                    796:     printObject(*obp,0,stderr);
                    797:     fprintf(stderr,"\n");
                    798:     errorOxmisc2("cmoCheckMathCap: length of mathcap is wrong in the client->mathcapObjp field.\n");
                    799:   }
1.2       takayama  800:   ob0 = getoa(mathcap,0);
                    801:   if (ob0.tag != Sdollar) {
                    802:     fprintf(stderr,"cmoCheckMathCap: the mathcap obj is \n");
                    803:     printObject(*obp,0,stderr);
                    804:     fprintf(stderr,"\n");
                    805:     errorOxmisc2("cmoCheckMathCap: The first field must be the string mathcap-object.\n");
                    806:   }
                    807:   if (strcmp(KopString(ob0),"mathcap-object") != 0) {
                    808:     fprintf(stderr,"cmoCheckMathCap: the mathcap obj is \n");
                    809:     printObject(*obp,0,stderr);
                    810:     fprintf(stderr,"\n");
                    811:     errorOxmisc2("cmoCheckMathCap: The mathcap must be of the form [(mathcap-object) [...]]\n");
                    812:   }
                    813:
1.1       maekawa   814:   /* I should check
1.12      takayama  815:      getoa(getoa(mathcap,1),2)
1.1       maekawa   816:      contains OX_DATA.
                    817:      It has not yet implemented.
                    818:   */
1.2       takayama  819:   mathcapMain = getoa(mathcap,1);
                    820:   if (mathcapMain.tag != Sarray) {
                    821:     fprintf(stderr,"cmoCheckMathCap: mathcap[1] is \n");
                    822:     printObject(mathcapMain,0,stderr);
                    823:     fprintf(stderr,"\n");
                    824:     errorOxmisc2("cmoCheckMathCap: format error in the (client->mathcapObjp)[1] field. It should be an array.\n");
                    825:   }
                    826:   if (getoaSize(mathcapMain) < 3) {
                    827:     fprintf(stderr,"cmoCheckMathCap: mathcap[1] is \n");
                    828:     printObject(mathcapMain,0,stderr);
                    829:     fprintf(stderr,"\n");
                    830:     errorOxmisc2("cmoCheckMathCap: format error in the (client->mathcapObjp)[1] field. It should be an array of which length is more than 2.\n");
                    831:   }
1.4       takayama  832:   mathcapThird = getoa(mathcapMain,2);
                    833:   oxsize = getoaSize(mathcapThird);
                    834:   for (i=0; i<oxsize; i++) {
                    835:     ox = getoa(mathcapThird,i);
                    836:     if (ox.tag != Sarray) {
                    837:       fprintf(stderr,"cmoCheckMathCap: the mathcap obj is \n");
                    838:       printObject(*obp,0,stderr);
                    839:       fprintf(stderr,"\n");
                    840:       errorOxmisc2("cmoCheckMathCap: the third element of mathcap is a list of lists.");
                    841:     }
                    842:     if (getoaSize(ox) != 0) {
                    843:       oxtag = getoa(ox,0);
                    844:       if (oxtag.tag != Sinteger) {
1.12      takayama  845:         fprintf(stderr,"cmoCheckMathCap: the mathcap obj is \n");
                    846:         printObject(*obp,0,stderr);
                    847:         fprintf(stderr,"\n");
                    848:         errorOxmisc2("cmoCheckMathCap: the third element of mathcap must be [OX_DATA_xxx, [  ]].");
1.4       takayama  849:       }
                    850:       if (KopInteger(oxtag) == OX_DATA) {
1.12      takayama  851:         if (getoaSize(ox) > 1) {
                    852:           cmolist = getoa(ox,1);
                    853:           if (cmolist.tag != Sarray) {
                    854:             fprintf(stderr,"cmoCheckMathCap: the mathcap obj is \n");
                    855:             printObject(*obp,0,stderr);
                    856:             fprintf(stderr,"\n");
                    857:             errorOxmisc2("cmoCheckMathCap: mathcap[1] must be an array of integers.\n");
                    858:           }
                    859:           n = getoaSize(cmolist);
                    860:           if (n > CMO_CHECK_MATH_CAP_LIST_SIZE) {
                    861:             errorOxmisc2("cmoCheckMathCap: Too big cmo list.\n");
                    862:           }
                    863:           for (i=0; i<n; i++) {
                    864:             cmo[i] = KopInteger(getoa(cmolist,i));
                    865:           }
                    866:         }else{
                    867:           fprintf(stderr,"cmoCheckMathCap: the mathcap obj is \n");
                    868:           printObject(*obp,0,stderr);
                    869:           fprintf(stderr,"\nox=");
                    870:           printObject(ox,0,stderr);
                    871:           errorOxmisc2("cmoCheckMathCap: [OX_DATA, cmolist]");
                    872:         }
1.4       takayama  873:       }
                    874:     }
1.1       maekawa   875:   }
                    876:   return(cmoCheck00(obj,cmo,n));
                    877: }
                    878:
1.12      takayama  879:
1.1       maekawa   880: struct object KoxGenPortFile(void) {
                    881:   struct object ob;
                    882:   ob = KpoString(oxGenPortFile());
                    883:   return(ob);
                    884: }
                    885: void KoxRemovePortFile(void) {
                    886:   oxRemovePortFile();
                    887: }
                    888:
                    889: void oxPushMathCap(struct mathCap *mathcap)
                    890: {
                    891:   struct object rob;
                    892:   rob = newMathCap(mathcap);
                    893:   Kpush(rob);
                    894: }
                    895:
                    896: struct object KoxGenPass(void) {
                    897:   struct object rob;
                    898:   rob = KpoString(oxGenPass());
                    899:   return(rob);
                    900: }
                    901:
                    902: struct object KoxGetPort(struct object host)
                    903: {
                    904:   struct object rob;
                    905:   int fdStream, fdControl;
                    906:   int portStream, portControl;
                    907:   extern int OpenedSocket;
                    908:   char *sname;
                    909:   rob = NullObject;
                    910:   if (host.tag != Sdollar) {
                    911:     errorOxmisc2("KoxGetPort: argument is not a string.");
                    912:     return(rob);
                    913:   }
                    914:   sname = KopString(host);
                    915:   fdControl = socketOpen(sname,0);
                    916:   portControl = OpenedSocket;
                    917:   fdStream = socketOpen(sname,0);
                    918:   portStream = OpenedSocket;
                    919:   rob = newObjectArray(4);
                    920:   putoa(rob,0,KpoInteger(fdStream));
                    921:   putoa(rob,1,KpoInteger(portStream));
                    922:   putoa(rob,2,KpoInteger(fdControl));
                    923:   putoa(rob,3,KpoInteger(portControl));
1.10      takayama  924:   return(rob);
                    925: }
                    926: struct object KoxGetPort1(struct object host)
                    927: {
                    928:   struct object rob;
                    929:   int fdStream;
                    930:   int portStream;
                    931:   extern int OpenedSocket;
                    932:   char *sname;
                    933:   rob = NullObject;
                    934:   if (host.tag != Sdollar) {
                    935:     errorOxmisc2("KoxGetPort1: argument is not a string.");
                    936:     return(rob);
                    937:   }
                    938:   sname = KopString(host);
                    939:   fdStream = socketOpen(sname,0);
                    940:   portStream = OpenedSocket;
                    941:   rob = newObjectArray(2);
                    942:   putoa(rob,0,KpoInteger(fdStream));
                    943:   putoa(rob,1,KpoInteger(portStream));
1.1       maekawa   944:   return(rob);
                    945: }
                    946:
                    947: struct object KoxCreateClient2(struct object peer,
1.12      takayama  948:                                struct object ipmask,
                    949:                                struct object pass)
1.1       maekawa   950: {
                    951:   struct object rob;
                    952:   oxclientp client;
                    953:   int fdStream, portStream, fdControl, portControl;
                    954:   int i;
                    955:   struct object ob1;
                    956:   rob.tag = Snull;
                    957:   if (peer.tag != Sarray) {
                    958:     errorOxmisc2("KoxCreateClient2(): The first argument must be an array [fdStream, portStream, fdControl, portControl]");
                    959:     return(rob);
                    960:   }
                    961:   if (getoaSize(peer) != 4) {
                    962:     errorOxmisc2("KoxCreateClient2(): The first argument must be an array [fdStream, portStream, fdControl, portControl] of size 4.");
                    963:     return(rob);
                    964:   }
                    965:   for (i=0; i<4; i++) {
                    966:     ob1 = getoa(peer,i);
                    967:     if (ob1.tag != Sinteger) {
                    968:       errorOxmisc2("KoxCreateClient2(): The element of the first argument must be an integer.");
                    969:     }
                    970:   }
                    971:   fdStream = KopInteger(getoa(peer,0));
                    972:   portStream = KopInteger(getoa(peer,1));
                    973:   fdControl = KopInteger(getoa(peer,2));
                    974:   portControl = KopInteger(getoa(peer,3));
                    975:
                    976:   if (ipmask.tag != Sinteger) {
                    977:     errorOxmisc2("KoxCreateClient2(): ipmask must be an integer.");
                    978:   }
                    979:   if (pass.tag != Sdollar) {
                    980:     errorOxmisc2("KoxCreateClient2(): pass must be a string.");
                    981:   }
                    982:
                    983:   client = oxCreateClient2(fdStream, portStream, fdControl, portControl,
1.12      takayama  984:                            KopInteger(ipmask), KopString(pass));
1.1       maekawa   985:   if (client == NULL) {
                    986:     errorOxmisc2("KoxCreateClient2(): Open error.");
                    987:     return(rob);
                    988:   }
                    989:   rob = newObjectArray(N_OF_CLIENT_FIELDS);
                    990:   oxClientToObject(client,rob);
1.14      takayama  991:   oxClientListUpdate(rob);
1.1       maekawa   992:   return(rob);
1.5       takayama  993: }
                    994:
                    995: int KgetCmoTagOfObject(struct object obj) {
                    996:   int k;
                    997:   k=cmoCheck00(obj,(int *)NULL,0);
                    998:   return(k);
1.1       maekawa   999: }
                   1000:
                   1001: errorOxmisc2(char *s) {
1.6       takayama 1002:   SET_MYERROROUT;
1.1       maekawa  1003:   fprintf(MyErrorOut,"error in oxmisc2.c: %s\n",s);
                   1004:   errorKan1("%s\n","  ");
1.7       takayama 1005: }
                   1006:
                   1007: struct object KoxPushCMD(struct object client,struct object cmd) {
                   1008:   int ans;
                   1009:   static oxclientp cc1 = NULL;
                   1010:   struct object rob;
                   1011:   rob.tag = Snull;
                   1012:   if (cc1 == NULL) {
                   1013:     cc1 = (oxclientp) mymalloc(sizeof(oxclient));
                   1014:     if (cc1 == NULL) {
                   1015:       errorOxmisc2("KoxReq(): no more memory.");
                   1016:       return(rob);
                   1017:     }
                   1018:     oxInitClient(cc1);  /* BUG: is it fine? */
                   1019:   }
                   1020:
                   1021:   if (oxObjectToClient(client,cc1) == -1) return(rob);
                   1022:   if (cc1 == NULL) {
                   1023:     errorOxmisc2("KoxReq(): the first argument must be a client object.");
                   1024:     return(rob);
                   1025:   }
                   1026:   if (cmd.tag != Sinteger) {
                   1027:     errorOxmisc2("KoxReq(): the second argument must be an integer.");
                   1028:     return(rob);
                   1029:   }
                   1030:   /* BUG: check the mathcap */
                   1031:   oxSendOXheader(cc1->datafp2,OX_COMMAND,SerialOX++);
                   1032:   oxSendInt32(cc1->datafp2,KopInteger(cmd));
                   1033:   /* synchronize cc1 and client. */
                   1034:   oxClientToObject(cc1,client);
                   1035:   return(cmd);
                   1036: }
                   1037:
                   1038: struct object KoxPushCMO(struct object client,struct object ob) {
                   1039:   int ans;
                   1040:   static oxclientp cc1 = NULL;
                   1041:   struct object rob;
                   1042:   rob.tag = Snull;
                   1043:   if (cc1 == NULL) {
                   1044:     cc1 = (oxclientp) mymalloc(sizeof(oxclient));
                   1045:     if (cc1 == NULL) {
                   1046:       errorOxmisc2("KoxReq(): no more memory.");
                   1047:       return(rob);
                   1048:     }
                   1049:     oxInitClient(cc1);  /* BUG: is it fine? */
                   1050:   }
                   1051:
                   1052:   if (oxObjectToClient(client,cc1) == -1) return(rob);
                   1053:   if (cc1 == NULL) {
                   1054:     errorOxmisc2("KoxReq(): the first argument must be a client object.");
                   1055:     return(rob);
                   1056:   }
                   1057:
                   1058:   /* request to the data channel */
                   1059:   if (cc1->dstate != DSTATE_ANY) {
                   1060:     errorOxmisc2("oxPushCMO: cc1->dstate != DSTATE_ANY, data channel is not ready to send data.\n");
                   1061:     return(rob);
                   1062:   }
                   1063:
                   1064:   if (!cmoCheckMathCap(ob,(struct object *)cc1->mathcapObjp)) {
1.12      takayama 1065:     errorOxmisc2("oxPushCMO: your peer does not understand this cmo.\n");
                   1066:     return(rob);
1.7       takayama 1067:   }
                   1068:   oxSendOXheader(cc1->datafp2,OX_DATA,SerialOX++);
                   1069:   cmoObjectToStream2(ob,cc1->datafp2);
                   1070:   /* synchronize cc1 and client. */
                   1071:   oxClientToObject(cc1,client);
                   1072:   return(ob);
1.8       takayama 1073: }
                   1074:
                   1075: oxclientp oxCreateControl_RFC_101(int fdstream,int portStream,
1.12      takayama 1076:                                   int ipmask,char *pass);
1.8       takayama 1077: struct object KoxCreateControl_RFC_101(struct object peer,struct object ipmask,struct object pass)
                   1078: {
                   1079:   struct object rob;
                   1080:   oxclientp client;
                   1081:   int fdStream, portStream;
                   1082:   int i;
                   1083:   struct object ob1;
                   1084:   rob.tag = Snull;
                   1085:   if (peer.tag != Sarray) {
                   1086:     errorOxmisc2("KoxCreateControl_RFC_101(): The first argument must be an array [fdStream, portStream]");
                   1087:     return(rob);
                   1088:   }
                   1089:   if (getoaSize(peer) != 2 ) {
                   1090:     errorOxmisc2("KoxCreateControl_RFC_101(): The first argument must be an array [fdStream, portStream] of size 2.");
                   1091:     return(rob);
                   1092:   }
                   1093:   for (i=0; i<getoaSize(peer); i++) {
                   1094:     ob1 = getoa(peer,i);
                   1095:     if (ob1.tag != Sinteger) {
                   1096:       errorOxmisc2("KoxCreateControl_RFC_101(): The element of the first argument must be an integer.");
                   1097:     }
                   1098:   }
                   1099:   fdStream = KopInteger(getoa(peer,0));
                   1100:   portStream = KopInteger(getoa(peer,1));
                   1101:
                   1102:   if (ipmask.tag != Sinteger) {
                   1103:     errorOxmisc2("KoxCreateControl_RFC_101(): ipmask must be an integer.");
                   1104:   }
                   1105:   if (pass.tag != Sdollar) {
                   1106:     errorOxmisc2("KoxCreateControl_RFC_101(): pass must be a string.");
                   1107:   }
                   1108:
                   1109:   client = oxCreateControl_RFC_101(fdStream, portStream,
1.12      takayama 1110:                                    KopInteger(ipmask), KopString(pass));
1.8       takayama 1111:   if (client == NULL) {
                   1112:     errorOxmisc2("KoxCreateControl_RFC_101(): Open error.");
                   1113:     return(rob);
                   1114:   }
                   1115:   rob = newObjectArray(N_OF_CLIENT_FIELDS);
                   1116:   oxClientToObject(client,rob);
                   1117:   return(rob);
                   1118: }
                   1119:
                   1120: oxclientp oxCreateControl_RFC_101(int fdstream,int portStream,
1.12      takayama 1121:                                   int ipmask,char *pass)
1.8       takayama 1122: {
                   1123:   int v = 0;
                   1124:   int fdControl = -1;
                   1125:   int fdStream = -1;
                   1126:   int m;
                   1127:
                   1128:   char *s;
                   1129:   oxclientp client;
1.13      takayama 1130: #if defined(__CYGWIN__)
                   1131:   extern sigjmp_buf MyEnv_oxmisc;
                   1132: #else
                   1133:   extern jmp_buf MyEnv_oxmisc;
                   1134: #endif
1.8       takayama 1135:   int engineByteOrder;
                   1136:   extern int Quiet;
                   1137:
                   1138:   v = !Quiet;
                   1139:
                   1140:   switch(ipmask) {
                   1141:   case 0:/* only local */
                   1142:     fdStream  = socketAcceptLocal(fdstream);
                   1143:     break;
                   1144:   default:/* any */
                   1145:     fdStream  = socketAccept(fdstream);
                   1146:     break;
                   1147:   }
                   1148:   if (v) fprintf(stderr,"\nControl port %d : Connected.\n",portStream);
                   1149:
                   1150:   if (fdStream == -1 ) {
                   1151:     fprintf(stderr,"\nOpen error in oxCreateControl_RFC_101.\n");
                   1152:     return(NULL);
                   1153:   }
                   1154:
                   1155:   /* Authentication by password. */
                   1156:   m = strlen(pass);
                   1157:   s = (char *)mymalloc(sizeof(char)*(m+1));
                   1158:   read(fdStream,s,m+1); s[m] = '\0';
                   1159:   if (strcmp(s,pass) != 0) {
                   1160:     fprintf(stderr,"oxCreateControl_RFC_101(): password authentication failed for control channel.\n");
                   1161:     close(fdStream);
                   1162:     return(NULL);
                   1163:   }
                   1164:
                   1165:
                   1166:   engineByteOrder = oxSetByteOrder(fdStream);
                   1167:   if (v) fprintf(stderr,"Byte order for control stackmacine is %s.\n",
1.12      takayama 1168:                  (engineByteOrder == 0? "network byte order":
                   1169:                   (engineByteOrder == 1? "little indican":
                   1170:                    "big indian")));
1.8       takayama 1171:
                   1172:
                   1173:   client = (oxclientp) mymalloc(sizeof(oxclient));
                   1174:   oxInitClient(client);
                   1175:   client->datafp2 = fp2open(fdStream);
                   1176:   if (client->datafp2 == NULL) {
                   1177:     fprintf(stderr,"oxCreateControl_RFC_101(): fp2open(fd) failed.\n");
                   1178:     return(NULL);
                   1179:   }
                   1180:   client->dataport = portStream;
                   1181:   client->controlport = -1;
                   1182:   client->controlfd = -1;
1.14      takayama 1183:   client->id = oxGetClientID();
1.11      takayama 1184:   client->type = CLIENT_SOCKET; /* socket */
                   1185:   client->engineByteOrder = engineByteOrder;
                   1186:   client->controlByteOrder = -1;
                   1187:   return(client);
                   1188: }
                   1189:
                   1190: oxclientp oxCreateEngine_RFC_101(int fdstream,int portStream,
1.12      takayama 1191:                                  int ipmask,char *pass, int engineID);
1.11      takayama 1192: struct object KoxCreateEngine_RFC_101(struct object peer,struct object ipmask,struct object pass, struct object engineID)
                   1193: {
                   1194:   struct object rob;
                   1195:   oxclientp client;
                   1196:   int fdStream, portStream;
                   1197:   int i;
                   1198:   struct object ob1;
                   1199:   rob.tag = Snull;
                   1200:   if (peer.tag != Sarray) {
                   1201:     errorOxmisc2("KoxCreateEngine_RFC_101(): The first argument must be an array [fdStream, portStream]");
                   1202:     return(rob);
                   1203:   }
                   1204:   if (getoaSize(peer) != 2 ) {
                   1205:     errorOxmisc2("KoxCreateEngine_RFC_101(): The first argument must be an array [fdStream, portStream] of size 2.");
                   1206:     return(rob);
                   1207:   }
                   1208:   for (i=0; i<getoaSize(peer); i++) {
                   1209:     ob1 = getoa(peer,i);
                   1210:     if (ob1.tag != Sinteger) {
                   1211:       errorOxmisc2("KoxCreateEngine_RFC_101(): The element of the first argument must be an integer.");
                   1212:     }
                   1213:   }
                   1214:   fdStream = KopInteger(getoa(peer,0));
                   1215:   portStream = KopInteger(getoa(peer,1));
                   1216:
                   1217:   if (ipmask.tag != Sinteger) {
                   1218:     errorOxmisc2("KoxCreateEngine_RFC_101(): ipmask must be an integer.");
                   1219:   }
                   1220:   if (pass.tag != Sdollar) {
                   1221:     errorOxmisc2("KoxCreateEngine_RFC_101(): pass must be a string.");
                   1222:   }
                   1223:   if (engineID.tag != Sinteger) {
                   1224:     errorOxmisc2("KoxCreateEngine_RFC_101(): engineID must be an integer.");
                   1225:   }
                   1226:
                   1227:   client = oxCreateEngine_RFC_101(fdStream, portStream,
1.12      takayama 1228:                                   KopInteger(ipmask), KopString(pass),KopInteger(engineID));
1.11      takayama 1229:   if (client == NULL) {
                   1230:     errorOxmisc2("KoxCreateEngine_RFC_101(): Open error.");
                   1231:     return(rob);
                   1232:   }
                   1233:   rob = newObjectArray(N_OF_CLIENT_FIELDS);
                   1234:   oxClientToObject(client,rob);
1.14      takayama 1235:   oxClientListUpdate(rob);
1.11      takayama 1236:   return(rob);
                   1237: }
                   1238:
                   1239: oxclientp oxCreateEngine_RFC_101(int fdstream,int portStream,
1.12      takayama 1240:                                  int ipmask,char *pass,int engineID)
1.11      takayama 1241: {
                   1242:   int v = 0;
                   1243:   int fdControl = -1;
                   1244:   int fdStream = -1;
                   1245:   int m;
                   1246:
                   1247:   char *s;
                   1248:   oxclientp client;
1.13      takayama 1249: #if defined(__CYGWIN__)
                   1250:   extern sigjmp_buf MyEnv_oxmisc;
                   1251: #else
                   1252:   extern jmp_buf MyEnv_oxmisc;
                   1253: #endif
1.11      takayama 1254:   int engineByteOrder;
                   1255:   extern int Quiet;
                   1256:
                   1257:   v = !Quiet;
                   1258:
                   1259:   switch(ipmask) {
                   1260:   case 0:/* only local */
                   1261:     fdStream  = socketAcceptLocal(fdstream);
                   1262:     break;
                   1263:   default:/* any */
                   1264:     fdStream  = socketAccept(fdstream);
                   1265:     break;
                   1266:   }
                   1267:   if (v) fprintf(stderr,"\nEngine port %d : Connected.\n",portStream);
                   1268:
                   1269:   if (fdStream == -1 ) {
                   1270:     fprintf(stderr,"\nOpen error in oxCreateEngine_RFC_101.\n");
                   1271:     return(NULL);
                   1272:   }
                   1273:
                   1274:   /* Authentication by password. */
                   1275:   /*  skip password check for now. BUG.
1.12      takayama 1276:       m = strlen(pass);
                   1277:       s = (char *)mymalloc(sizeof(char)*(m+1));
                   1278:       read(fdStream,s,m+1); s[m] = '\0';
                   1279:       if (strcmp(s,pass) != 0) {
                   1280:       fprintf(stderr,"oxCreateEngine_RFC_101(): password authentication failed for control channel.\n");
                   1281:       close(fdStream);
                   1282:       return(NULL);
                   1283:       }
1.11      takayama 1284:   */
                   1285:
                   1286:   engineByteOrder = oxSetByteOrder(fdStream);
                   1287:   if (v) fprintf(stderr,"Byte order for engine stackmacine is %s.\n",
1.12      takayama 1288:                  (engineByteOrder == 0? "network byte order":
                   1289:                   (engineByteOrder == 1? "little indican":
                   1290:                    "big indian")));
1.11      takayama 1291:
                   1292:
                   1293:   client = (oxclientp) mymalloc(sizeof(oxclient));
                   1294:   oxInitClient(client);
                   1295:   client->datafp2 = fp2open(fdStream);
                   1296:   if (client->datafp2 == NULL) {
                   1297:     fprintf(stderr,"oxCreateEngine_RFC_101(): fp2open(fd) failed.\n");
                   1298:     return(NULL);
                   1299:   }
                   1300:   client->dataport = portStream;
                   1301:   client->controlport = -1;
                   1302:   client->controlfd = -1;
1.16    ! takayama 1303:   client->id = oxGetClientID();
        !          1304:   client->engineID = engineID;
1.8       takayama 1305:   client->type = CLIENT_SOCKET; /* socket */
                   1306:   client->engineByteOrder = engineByteOrder;
                   1307:   client->controlByteOrder = -1;
                   1308:   return(client);
1.1       maekawa  1309: }
1.14      takayama 1310:
                   1311: void oxClientListUpdate(struct object ob) {
                   1312:   int id;
                   1313:   extern struct object OxClientList[];
                   1314:   id = KopInteger(getoa(ob,8));
                   1315:   /* printf("id=%d\n",id); */
                   1316:   if ((id <MAX_N_OF_CLIENT) && (id >= 0)) {
                   1317:        OxClientList[id] = ob;
                   1318:   }else{
                   1319:        errorOxmisc2("oxClientListUpdate(): the client table is full.\n");
                   1320:   }
                   1321: }
                   1322: void oxClientListRemove(struct object ob) {
                   1323:   int id;
                   1324:   extern struct object OxClientList[];
                   1325:   id = KopInteger(getoa(ob,8));
                   1326:   if ((id <MAX_N_OF_CLIENT) && (id >= 0)) {
                   1327:        (OxClientList[id]).tag = Snull;
                   1328:   }else{
                   1329:        /* errorOxmisc2("oxClientListRemove(): the client table is full.\n");*/
                   1330:   }
                   1331: }
                   1332: struct object KoxGetClientList() {
                   1333:   extern int OxClientListn;
                   1334:   extern struct object OxClientList[];
                   1335:   int i,j,n;
                   1336:   struct object rob;
                   1337:   n = 0;
                   1338:   for (i=0; i<OxClientListn; i++) {
                   1339:        if ((OxClientList[i]).tag != Snull) n++;
                   1340:   }
                   1341:   rob = newObjectArray(n);
                   1342:   for (i=0, j=0; i<OxClientListn; i++) {
                   1343:        if ((OxClientList[i]).tag != Snull) {
                   1344:          if (j >= n) {
                   1345:                j=0;
                   1346:                errorOxmisc2("oxGetClientList(): the client table is broken.\n");
                   1347:          }
                   1348:          putoa(rob,j,OxClientList[i]);
                   1349:          j++;
                   1350:        }
                   1351:   }
                   1352:   return rob;
                   1353: }
                   1354:
                   1355:

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