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

1.14    ! takayama    1: /* $OpenXM: OpenXM/src/kan96xx/plugin/oxmisc2.c,v 1.13 2002/02/24 10:27: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));
                    385:   return(0);
                    386: }
                    387:
                    388: int oxObjectToClient(struct object ob,oxclientp cp)
                    389: {
                    390:   struct object ob1;
                    391:   struct object *obp;
                    392:   if (cp == NULL) {
                    393:     errorOxmisc2("oxObjectToClient(): the second argument is NULL");
                    394:     return(-1);
                    395:   }
                    396:   if (!isItClientObject(ob)) {
                    397:     errorOxmisc2("oxObjectToClient(): the first argument is not client object.");
                    398:     oxInitClient(cp);
                    399:     return(-1);
                    400:   }
                    401:
                    402:   ob1 = getoa(ob,1);
                    403:   cp->datafp2 = (FILE2 *) (ob1.rc.voidp);
                    404:
                    405:   ob1 = getoa(ob,2);
                    406:   cp->dataport = KopInteger(ob1);
                    407:
                    408:   ob1 = getoa(ob,3);
                    409:   cp->controlfd = KopInteger(ob1);
                    410:
                    411:   ob1 = getoa(ob,4);
                    412:   cp->controlport = KopInteger(ob1);
                    413:
                    414:   ob1 = getoa(ob,5);
                    415:   cp->dstate = KopInteger(ob1);
                    416:
                    417:   ob1 = getoa(ob,6);
                    418:   cp->cstate = KopInteger(ob1);
                    419:
                    420:   ob1 = getoa(ob,7);
                    421:   cp->humanio = KopInteger(ob1);
                    422:
                    423:   ob1 = getoa(ob,8);
                    424:   cp->id = KopInteger(ob1);
                    425:
                    426:   ob1 = getoa(ob,9);
                    427:   cp->type = KopInteger(ob1);
                    428:
                    429:   ob1 = getoa(ob,10);
                    430:   if (ob1.tag == Snull) {
                    431:     cp->mathcapObjp = NULL;
                    432:   }else{
                    433:     obp = (struct object *) sGC_malloc(sizeof(struct object));
                    434:     *obp = ob1;
                    435:     cp->mathcapObjp = (void *)obp;
                    436:   }
                    437:   ob1 = getoa(ob,11);
                    438:   cp->engineByteOrder = KopInteger(ob1);
                    439:   ob1 = getoa(ob,12);
                    440:   cp->controlByteOrder = KopInteger(ob1);
                    441:
                    442:
                    443:   return(0);
                    444: }
                    445:
                    446: struct object KoxReq(struct object client,
1.12      takayama  447:                      struct object func,
                    448:                      struct object ob1)
1.1       maekawa   449: {
                    450:   int ans;
                    451:   static oxclientp cc1 = NULL;
                    452:   struct object rob;
                    453:   rob.tag = Snull;
                    454:   if (cc1 == NULL) {
                    455:     cc1 = (oxclientp) mymalloc(sizeof(oxclient));
                    456:     if (cc1 == NULL) {
                    457:       errorOxmisc2("KoxReq(): no more memory.");
                    458:       return(rob);
                    459:     }
                    460:     oxInitClient(cc1);
                    461:   }
                    462:
                    463:   if (oxObjectToClient(client,cc1) == -1) return(rob);
                    464:   if (cc1 == NULL) {
                    465:     errorOxmisc2("KoxReq(): the first argument must be a client object.");
                    466:     return(rob);
                    467:   }
                    468:   if (func.tag != Sinteger) {
                    469:     errorOxmisc2("KoxReq(): the second argument must be an integer.");
                    470:     return(rob);
                    471:   }
                    472:   ans = oxReq(cc1,KopInteger(func),ob1);
                    473:   /* synchronize cc1 and client. */
                    474:   oxClientToObject(cc1,client);
                    475:
                    476:   return(KpoInteger(ans));
                    477: }
                    478:
                    479: struct object KoxGet(struct object client)
                    480: {
                    481:   int ans,k;
                    482:   static oxclientp cc1 = NULL;
                    483:   struct object rob;
                    484:   rob.tag = Snull;
                    485:   if (cc1 == NULL) {
                    486:     cc1 = (oxclientp) mymalloc(sizeof(oxclient));
                    487:     if (cc1 == NULL) {
                    488:       errorOxmisc2("KoxGet(): no more memory.");
                    489:       return(rob);
                    490:     }
                    491:     oxInitClient(cc1);
                    492:   }
                    493:
                    494:   if (oxObjectToClient(client,cc1) == -1) return(rob);
                    495:   if (cc1 == NULL) {
                    496:     errorOxmisc2("KoxGet(): the first argument must be a client object.");
                    497:     return(rob);
                    498:   }
                    499:
                    500:   ans = oxGet(cc1,&rob,&k);
                    501:   /* synchronize cc1 and client. */
                    502:   oxClientToObject(cc1,client);
                    503:
                    504:   if (k) return(rob);
                    505:   else {
                    506:     return(KpoInteger(ans));
                    507:   }
                    508: }
                    509:
                    510: struct object KoxGetFromControl(struct object client)
                    511: {
                    512:   int ans;
                    513:   static oxclientp cc1 = NULL;
                    514:   struct object rob;
                    515:   rob.tag = Snull;
                    516:   if (cc1 == NULL) {
                    517:     cc1 = (oxclientp) mymalloc(sizeof(oxclient));
                    518:     if (cc1 == NULL) {
                    519:       errorOxmisc2("KoxGetFromControl(): no more memory.");
                    520:       return(rob);
                    521:     }
                    522:     oxInitClient(cc1);
                    523:   }
                    524:
                    525:   if (oxObjectToClient(client,cc1) == -1) return(rob);
                    526:   if (cc1 == NULL) {
                    527:     errorOxmisc2("KoxGetFromControl(): the first argument must be a client object.");
                    528:     return(rob);
                    529:   }
                    530:
                    531:   ans = oxGetFromControl(cc1);
                    532:   /* synchronize cc1 and client. */
                    533:   oxClientToObject(cc1,client);
                    534:
                    535:   return(KpoInteger(ans));
                    536: }
                    537:
                    538: struct object KoxMultiSelect(struct object oclients,struct object t)
                    539: {
                    540:   static int first = 1;
                    541:   static int csize = 0;
                    542:   static oxclientp *clients = NULL;
                    543:   oxclientp cc1;
                    544:   struct object rob;
                    545:   int i;
                    546:   int tt;
                    547:   struct object ob1;
                    548:   struct object ob2;
                    549:   struct object ob0;
                    550:   int size;
                    551:   int ans;
                    552:   int dataready[1024];
                    553:   int controlready[1024];
                    554:
                    555:   rob.tag = Snull;
                    556:   if (oclients.tag != Sarray) {
                    557:     errorOxmisc2("KoxMultiSelect(): the first argument must be an array.");
                    558:     return(rob);
                    559:   }
                    560:   size = getoaSize(oclients);
                    561:   if (first) {
                    562:     first = 0; csize = size;
                    563:     clients = (oxclientp *)mymalloc(sizeof(oxclientp)*(size+1));
1.12      takayama  564:     if (clients == NULL) {
1.1       maekawa   565:       errorOxmisc2("KoxMultiSelect(): no more memory.");
                    566:       return(rob);
                    567:     }
                    568:     for (i=0; i<size; i++) {
                    569:       clients[i] =  (oxclientp) mymalloc(sizeof(oxclient));
                    570:       if (clients[i] == NULL) {
1.12      takayama  571:         errorOxmisc2("KoxMultiSelect(): no more memory.");
                    572:         return(rob);
1.1       maekawa   573:       }
                    574:       oxInitClient(clients[i]);
                    575:     }
                    576:   }
                    577:   if (csize < size)  {
                    578:     first = 1;
                    579:     return(KoxMultiSelect(oclients,t));
                    580:   }
                    581:   for (i=0; i<size; i++) {
                    582:     ob0 = getoa(oclients,i);
                    583:     if (oxObjectToClient(ob0,clients[i]) == -1) return(rob);
                    584:   }
                    585:   if (t.tag != Sinteger) {
                    586:     errorOxmisc2("KoxMultiSelect(): the second argument must be an integer.");
                    587:   }
                    588:   tt = KopInteger(t);
                    589:   ans = oxclientMultiSelect(clients,dataready,controlready,size,tt);
                    590:   /* synchronize oclients and clients. */
                    591:   for (i=0; i<size; i++) {
                    592:     ob0 = getoa(oclients,i);
                    593:     oxClientToObject(clients[i],ob0);
                    594:     putoa(oclients,i,ob0);
                    595:   }
                    596:   rob = newObjectArray(3);
                    597:   putoa(rob,0,KpoInteger(ans));
                    598:   ob1 = newObjectArray(size);
                    599:   ob2 = newObjectArray(size);
                    600:   for (i=0; i<size; i++) {
                    601:     putoa(ob1,i,KpoInteger(dataready[i]));
                    602:     putoa(ob2,i,KpoInteger(controlready[i]));
                    603:   }
                    604:   putoa(rob,1,ob1);
                    605:   putoa(rob,2,ob2);
                    606:   return(rob);
                    607: }
                    608:
                    609: struct object KoxWatch(struct object client,struct object f)
1.12      takayama  610:      /* f is not used for now. It should be log file. */
1.1       maekawa   611: {
                    612:   int ans,k;
                    613:   static oxclientp cc1 = NULL;
                    614:   struct object rob;
1.3       takayama  615:   extern int WatchStream;
1.1       maekawa   616:   rob.tag = Snull;
1.3       takayama  617:   if (client.tag == Sinteger) {
1.12      takayama  618:     if (KopInteger(client)) {
                    619:       WatchStream = 1;
                    620:     }else{
                    621:       WatchStream = 0;
                    622:     }
                    623:     return;
1.3       takayama  624:   }
1.1       maekawa   625:   if (cc1 == NULL) {
                    626:     cc1 = (oxclientp) mymalloc(sizeof(oxclient));
                    627:     if (cc1 == NULL) {
                    628:       errorOxmisc2("KoxWatch(): no more memory.");
                    629:       return(rob);
                    630:     }
                    631:     oxInitClient(cc1);
                    632:   }
                    633:
                    634:   if (oxObjectToClient(client,cc1) == -1) return(rob);
                    635:   if (cc1 == NULL) {
                    636:     errorOxmisc2("KoxWatch(): the first argument must be a client object.");
                    637:     return(rob);
                    638:   }
                    639:
                    640:   k = fp2watch(cc1->datafp2,stdout);
                    641:   /* synchronize cc1 and client. */
                    642:   oxClientToObject(cc1,client);
                    643:
                    644:   return(KpoInteger(ans));
                    645: }
                    646:
                    647:
                    648: struct object KoxCloseClient(struct object client) {
                    649:   oxclientp cc1 = NULL;
                    650:   oxclient cc;
                    651:   struct object rob;
                    652:   rob.tag = Snull;
                    653:   cc1  = &cc;
                    654:   if (oxObjectToClient(client,cc1) == -1) return(rob);
                    655:   if (cc1 == NULL) {
                    656:     errorOxmisc2("KoxCloseClient(): the first argument must be a client object.");
                    657:     return(rob);
                    658:   }
                    659:
                    660:   fp2fflush(cc1->datafp2);
                    661:   if (cc1->humanio) {
                    662:     /* Do not close the file. */
                    663:     return(KpoInteger(0));
                    664:   }
                    665:   switch (cc1->type) {
                    666:   case CLIENT_SOCKET:
                    667:     fp2fclose(cc1->datafp2);
                    668:     close(cc1->controlfd);
                    669:     break;
                    670:   case CLIENT_FILE:
                    671:     fp2fclose(cc1->datafp2);
                    672:     close(cc1->controlfd);
                    673:     break;
                    674:   default:
                    675:     errorOxmisc2("Unknown client->type\n");
                    676:     break;
                    677:   }
1.14    ! takayama  678:   oxClientListRemove(client);
1.1       maekawa   679:   return(KpoInteger(0));
                    680:
                    681: }
                    682:
                    683: static int cmoCheck00(struct object obj,int cmo[], int n) {
                    684:   int i,j,m;
                    685:   int ttt;
                    686: #define CHECK00_N  4098      /* look up stackm.h and kclass.h */
                    687:   static int typeTrans[CHECK00_N];
                    688:   static int init = 0;
1.5       takayama  689:   /* if n == 0, report the cmo tag of the object obj.
1.12      takayama  690:      If it cannot be translated to cmo, then return -1. */
1.5       takayama  691:
1.1       maekawa   692:   if (!init) {
                    693:     for (i=0; i<CHECK00_N; i++) {
                    694:       typeTrans[i] = 0;  /* unknown cmo number */
                    695:     }
                    696:     typeTrans[Snull] = CMO_NULL;
                    697:     typeTrans[Sinteger] = CMO_INT32;
                    698:     typeTrans[Sdollar] = CMO_STRING;
                    699:     if (OxVersion >= 199907170) {
                    700:       typeTrans[SuniversalNumber] = CMO_ZZ;
                    701:     }else{
                    702:       typeTrans[SuniversalNumber] = CMO_ZZ_OLD;
                    703:     }
                    704:     typeTrans[Sarray] = CMO_LIST;
                    705:     /* typeTrans[Spoly] = CMO_DMS;  */
                    706:     typeTrans[Spoly] = CMO_DISTRIBUTED_POLYNOMIAL;
                    707:     typeTrans[Sdouble] = CMO_64BIT_MACHINE_DOUBLE;
                    708:     typeTrans[CLASSNAME_ERROR_PACKET]   = CMO_ERROR2;
                    709:     typeTrans[CLASSNAME_mathcap] = CMO_MATHCAP;
                    710:     typeTrans[CLASSNAME_indeterminate] = CMO_INDETERMINATE;
                    711:     typeTrans[CLASSNAME_tree] = CMO_TREE;
                    712:     typeTrans[CLASSNAME_recursivePolynomial] = CMO_RECURSIVE_POLYNOMIAL;
                    713:     typeTrans[CLASSNAME_polynomialInOneVariable] = CMO_POLYNOMIAL_IN_ONE_VARIABLE;
                    714:     init = 1;
                    715:   }
                    716:   ttt = typeTrans[obj.tag];
                    717:   if (obj.tag == Sclass) {
                    718:     ttt = typeTrans[ectag(obj)];
                    719:   }
1.5       takayama  720:   /* Only report the cmo tag. */
                    721:   if (n == 0) {
1.12      takayama  722:     if (ttt == 0) return(-1);
                    723:     else return(ttt);
1.5       takayama  724:   }
1.1       maekawa   725:
                    726:   for (i=0; i<n; i++) {
                    727:     if (ttt == cmo[i]) {
                    728:       if (ttt != CMO_LIST) return(1);
                    729:       else {
1.12      takayama  730:         m = getoaSize(obj);
                    731:         for (j=0; j<m; j++) {
                    732:           if (!cmoCheck00(getoa(obj,j),cmo,n)) return(0);
                    733:         }
                    734:         return(1);
1.1       maekawa   735:       }
                    736:     }
                    737:   }
                    738:   if (DebugMathCap) {
                    739:     if (DebugMathCap && 1) {
                    740:       fprintf(stderr,"Type translation table (internal object tag --> CMO tag)\n");
                    741:       for (i=0; i<20; i++) {
1.12      takayama  742:         printf("%d ", typeTrans[i]);
1.1       maekawa   743:       }
                    744:       printf("\n");
                    745:     }
                    746:     fprintf(stderr,"The type of the argument object in sm1 is %d.\n",obj.tag);
                    747:     fprintf(stderr,"The type of the argument object in CMO is %d.\n",ttt);
                    748:     fprintf(stderr,"Available CMO tags in mathcap= %d elements : [ ",n);
                    749:     for (i=0; i<n; i++) {
                    750:       fprintf(stderr," %d ",cmo[i]);
                    751:     }
                    752:     fprintf(stderr," ] \n");
                    753:   }
                    754:   return(0);
                    755: }
                    756:
                    757: int cmoCheckMathCap(struct object obj, struct object *obp)
                    758: {
                    759:   struct object mathcap;
                    760:   struct object cmolist;
1.2       takayama  761:   struct object mathcapMain;
1.4       takayama  762:   struct object mathcapThird;
                    763:   struct object ox;
                    764:   struct object oxtag;
1.2       takayama  765:   struct object ob0;
1.4       takayama  766:   int oxsize;
1.1       maekawa   767:   int n;
                    768:   int i;
                    769: #define CMO_CHECK_MATH_CAP_LIST_SIZE 1024
                    770:   int cmo[CMO_CHECK_MATH_CAP_LIST_SIZE];
                    771:   if (obp == NULL) return(1);
1.2       takayama  772:   /* printObject(*obp,0,stderr); for debug*/
1.1       maekawa   773:   if (obp->tag != Sarray) {
                    774:     fprintf(stderr,"cmoCheckMathCap: the mathcap obj is \n");
                    775:     printObject(*obp,0,stderr);
                    776:     fprintf(stderr,"\n");
                    777:     errorOxmisc2("cmoCheckMathCap: format error in the client->mathcapObjp field.\n");
                    778:   }
                    779:   mathcap = *obp;
1.2       takayama  780:   /* Example of mathcap
1.12      takayama  781:      [    $mathcap-object$ ,
                    782:      [    [    199909080 , $Ox_system=ox_sm1.plain$ , $Version=2.991106$ ,
                    783:      $HOSTTYPE=i386$ ]  ,
                    784:      [    262 , 263 , 264 , 265 , 266 , 268 , 269 , 272 , 273 , 275 ,
                    785:      276 ]  ,
                    786:      [    [    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  787:   */
                    788:
1.1       maekawa   789:   n = getoaSize(mathcap);
                    790:   if (n < 2) {
                    791:     fprintf(stderr,"cmoCheckMathCap: the mathcap obj is \n");
                    792:     printObject(*obp,0,stderr);
                    793:     fprintf(stderr,"\n");
                    794:     errorOxmisc2("cmoCheckMathCap: length of mathcap is wrong in the client->mathcapObjp field.\n");
                    795:   }
1.2       takayama  796:   ob0 = getoa(mathcap,0);
                    797:   if (ob0.tag != Sdollar) {
                    798:     fprintf(stderr,"cmoCheckMathCap: the mathcap obj is \n");
                    799:     printObject(*obp,0,stderr);
                    800:     fprintf(stderr,"\n");
                    801:     errorOxmisc2("cmoCheckMathCap: The first field must be the string mathcap-object.\n");
                    802:   }
                    803:   if (strcmp(KopString(ob0),"mathcap-object") != 0) {
                    804:     fprintf(stderr,"cmoCheckMathCap: the mathcap obj is \n");
                    805:     printObject(*obp,0,stderr);
                    806:     fprintf(stderr,"\n");
                    807:     errorOxmisc2("cmoCheckMathCap: The mathcap must be of the form [(mathcap-object) [...]]\n");
                    808:   }
                    809:
1.1       maekawa   810:   /* I should check
1.12      takayama  811:      getoa(getoa(mathcap,1),2)
1.1       maekawa   812:      contains OX_DATA.
                    813:      It has not yet implemented.
                    814:   */
1.2       takayama  815:   mathcapMain = getoa(mathcap,1);
                    816:   if (mathcapMain.tag != Sarray) {
                    817:     fprintf(stderr,"cmoCheckMathCap: mathcap[1] is \n");
                    818:     printObject(mathcapMain,0,stderr);
                    819:     fprintf(stderr,"\n");
                    820:     errorOxmisc2("cmoCheckMathCap: format error in the (client->mathcapObjp)[1] field. It should be an array.\n");
                    821:   }
                    822:   if (getoaSize(mathcapMain) < 3) {
                    823:     fprintf(stderr,"cmoCheckMathCap: mathcap[1] is \n");
                    824:     printObject(mathcapMain,0,stderr);
                    825:     fprintf(stderr,"\n");
                    826:     errorOxmisc2("cmoCheckMathCap: format error in the (client->mathcapObjp)[1] field. It should be an array of which length is more than 2.\n");
                    827:   }
1.4       takayama  828:   mathcapThird = getoa(mathcapMain,2);
                    829:   oxsize = getoaSize(mathcapThird);
                    830:   for (i=0; i<oxsize; i++) {
                    831:     ox = getoa(mathcapThird,i);
                    832:     if (ox.tag != Sarray) {
                    833:       fprintf(stderr,"cmoCheckMathCap: the mathcap obj is \n");
                    834:       printObject(*obp,0,stderr);
                    835:       fprintf(stderr,"\n");
                    836:       errorOxmisc2("cmoCheckMathCap: the third element of mathcap is a list of lists.");
                    837:     }
                    838:     if (getoaSize(ox) != 0) {
                    839:       oxtag = getoa(ox,0);
                    840:       if (oxtag.tag != Sinteger) {
1.12      takayama  841:         fprintf(stderr,"cmoCheckMathCap: the mathcap obj is \n");
                    842:         printObject(*obp,0,stderr);
                    843:         fprintf(stderr,"\n");
                    844:         errorOxmisc2("cmoCheckMathCap: the third element of mathcap must be [OX_DATA_xxx, [  ]].");
1.4       takayama  845:       }
                    846:       if (KopInteger(oxtag) == OX_DATA) {
1.12      takayama  847:         if (getoaSize(ox) > 1) {
                    848:           cmolist = getoa(ox,1);
                    849:           if (cmolist.tag != Sarray) {
                    850:             fprintf(stderr,"cmoCheckMathCap: the mathcap obj is \n");
                    851:             printObject(*obp,0,stderr);
                    852:             fprintf(stderr,"\n");
                    853:             errorOxmisc2("cmoCheckMathCap: mathcap[1] must be an array of integers.\n");
                    854:           }
                    855:           n = getoaSize(cmolist);
                    856:           if (n > CMO_CHECK_MATH_CAP_LIST_SIZE) {
                    857:             errorOxmisc2("cmoCheckMathCap: Too big cmo list.\n");
                    858:           }
                    859:           for (i=0; i<n; i++) {
                    860:             cmo[i] = KopInteger(getoa(cmolist,i));
                    861:           }
                    862:         }else{
                    863:           fprintf(stderr,"cmoCheckMathCap: the mathcap obj is \n");
                    864:           printObject(*obp,0,stderr);
                    865:           fprintf(stderr,"\nox=");
                    866:           printObject(ox,0,stderr);
                    867:           errorOxmisc2("cmoCheckMathCap: [OX_DATA, cmolist]");
                    868:         }
1.4       takayama  869:       }
                    870:     }
1.1       maekawa   871:   }
                    872:   return(cmoCheck00(obj,cmo,n));
                    873: }
                    874:
1.12      takayama  875:
1.1       maekawa   876: struct object KoxGenPortFile(void) {
                    877:   struct object ob;
                    878:   ob = KpoString(oxGenPortFile());
                    879:   return(ob);
                    880: }
                    881: void KoxRemovePortFile(void) {
                    882:   oxRemovePortFile();
                    883: }
                    884:
                    885: void oxPushMathCap(struct mathCap *mathcap)
                    886: {
                    887:   struct object rob;
                    888:   rob = newMathCap(mathcap);
                    889:   Kpush(rob);
                    890: }
                    891:
                    892: struct object KoxGenPass(void) {
                    893:   struct object rob;
                    894:   rob = KpoString(oxGenPass());
                    895:   return(rob);
                    896: }
                    897:
                    898: struct object KoxGetPort(struct object host)
                    899: {
                    900:   struct object rob;
                    901:   int fdStream, fdControl;
                    902:   int portStream, portControl;
                    903:   extern int OpenedSocket;
                    904:   char *sname;
                    905:   rob = NullObject;
                    906:   if (host.tag != Sdollar) {
                    907:     errorOxmisc2("KoxGetPort: argument is not a string.");
                    908:     return(rob);
                    909:   }
                    910:   sname = KopString(host);
                    911:   fdControl = socketOpen(sname,0);
                    912:   portControl = OpenedSocket;
                    913:   fdStream = socketOpen(sname,0);
                    914:   portStream = OpenedSocket;
                    915:   rob = newObjectArray(4);
                    916:   putoa(rob,0,KpoInteger(fdStream));
                    917:   putoa(rob,1,KpoInteger(portStream));
                    918:   putoa(rob,2,KpoInteger(fdControl));
                    919:   putoa(rob,3,KpoInteger(portControl));
1.10      takayama  920:   return(rob);
                    921: }
                    922: struct object KoxGetPort1(struct object host)
                    923: {
                    924:   struct object rob;
                    925:   int fdStream;
                    926:   int portStream;
                    927:   extern int OpenedSocket;
                    928:   char *sname;
                    929:   rob = NullObject;
                    930:   if (host.tag != Sdollar) {
                    931:     errorOxmisc2("KoxGetPort1: argument is not a string.");
                    932:     return(rob);
                    933:   }
                    934:   sname = KopString(host);
                    935:   fdStream = socketOpen(sname,0);
                    936:   portStream = OpenedSocket;
                    937:   rob = newObjectArray(2);
                    938:   putoa(rob,0,KpoInteger(fdStream));
                    939:   putoa(rob,1,KpoInteger(portStream));
1.1       maekawa   940:   return(rob);
                    941: }
                    942:
                    943: struct object KoxCreateClient2(struct object peer,
1.12      takayama  944:                                struct object ipmask,
                    945:                                struct object pass)
1.1       maekawa   946: {
                    947:   struct object rob;
                    948:   oxclientp client;
                    949:   int fdStream, portStream, fdControl, portControl;
                    950:   int i;
                    951:   struct object ob1;
                    952:   rob.tag = Snull;
                    953:   if (peer.tag != Sarray) {
                    954:     errorOxmisc2("KoxCreateClient2(): The first argument must be an array [fdStream, portStream, fdControl, portControl]");
                    955:     return(rob);
                    956:   }
                    957:   if (getoaSize(peer) != 4) {
                    958:     errorOxmisc2("KoxCreateClient2(): The first argument must be an array [fdStream, portStream, fdControl, portControl] of size 4.");
                    959:     return(rob);
                    960:   }
                    961:   for (i=0; i<4; i++) {
                    962:     ob1 = getoa(peer,i);
                    963:     if (ob1.tag != Sinteger) {
                    964:       errorOxmisc2("KoxCreateClient2(): The element of the first argument must be an integer.");
                    965:     }
                    966:   }
                    967:   fdStream = KopInteger(getoa(peer,0));
                    968:   portStream = KopInteger(getoa(peer,1));
                    969:   fdControl = KopInteger(getoa(peer,2));
                    970:   portControl = KopInteger(getoa(peer,3));
                    971:
                    972:   if (ipmask.tag != Sinteger) {
                    973:     errorOxmisc2("KoxCreateClient2(): ipmask must be an integer.");
                    974:   }
                    975:   if (pass.tag != Sdollar) {
                    976:     errorOxmisc2("KoxCreateClient2(): pass must be a string.");
                    977:   }
                    978:
                    979:   client = oxCreateClient2(fdStream, portStream, fdControl, portControl,
1.12      takayama  980:                            KopInteger(ipmask), KopString(pass));
1.1       maekawa   981:   if (client == NULL) {
                    982:     errorOxmisc2("KoxCreateClient2(): Open error.");
                    983:     return(rob);
                    984:   }
                    985:   rob = newObjectArray(N_OF_CLIENT_FIELDS);
                    986:   oxClientToObject(client,rob);
1.14    ! takayama  987:   oxClientListUpdate(rob);
1.1       maekawa   988:   return(rob);
1.5       takayama  989: }
                    990:
                    991: int KgetCmoTagOfObject(struct object obj) {
                    992:   int k;
                    993:   k=cmoCheck00(obj,(int *)NULL,0);
                    994:   return(k);
1.1       maekawa   995: }
                    996:
                    997: errorOxmisc2(char *s) {
1.6       takayama  998:   SET_MYERROROUT;
1.1       maekawa   999:   fprintf(MyErrorOut,"error in oxmisc2.c: %s\n",s);
                   1000:   errorKan1("%s\n","  ");
1.7       takayama 1001: }
                   1002:
                   1003: struct object KoxPushCMD(struct object client,struct object cmd) {
                   1004:   int ans;
                   1005:   static oxclientp cc1 = NULL;
                   1006:   struct object rob;
                   1007:   rob.tag = Snull;
                   1008:   if (cc1 == NULL) {
                   1009:     cc1 = (oxclientp) mymalloc(sizeof(oxclient));
                   1010:     if (cc1 == NULL) {
                   1011:       errorOxmisc2("KoxReq(): no more memory.");
                   1012:       return(rob);
                   1013:     }
                   1014:     oxInitClient(cc1);  /* BUG: is it fine? */
                   1015:   }
                   1016:
                   1017:   if (oxObjectToClient(client,cc1) == -1) return(rob);
                   1018:   if (cc1 == NULL) {
                   1019:     errorOxmisc2("KoxReq(): the first argument must be a client object.");
                   1020:     return(rob);
                   1021:   }
                   1022:   if (cmd.tag != Sinteger) {
                   1023:     errorOxmisc2("KoxReq(): the second argument must be an integer.");
                   1024:     return(rob);
                   1025:   }
                   1026:   /* BUG: check the mathcap */
                   1027:   oxSendOXheader(cc1->datafp2,OX_COMMAND,SerialOX++);
                   1028:   oxSendInt32(cc1->datafp2,KopInteger(cmd));
                   1029:   /* synchronize cc1 and client. */
                   1030:   oxClientToObject(cc1,client);
                   1031:   return(cmd);
                   1032: }
                   1033:
                   1034: struct object KoxPushCMO(struct object client,struct object ob) {
                   1035:   int ans;
                   1036:   static oxclientp cc1 = NULL;
                   1037:   struct object rob;
                   1038:   rob.tag = Snull;
                   1039:   if (cc1 == NULL) {
                   1040:     cc1 = (oxclientp) mymalloc(sizeof(oxclient));
                   1041:     if (cc1 == NULL) {
                   1042:       errorOxmisc2("KoxReq(): no more memory.");
                   1043:       return(rob);
                   1044:     }
                   1045:     oxInitClient(cc1);  /* BUG: is it fine? */
                   1046:   }
                   1047:
                   1048:   if (oxObjectToClient(client,cc1) == -1) return(rob);
                   1049:   if (cc1 == NULL) {
                   1050:     errorOxmisc2("KoxReq(): the first argument must be a client object.");
                   1051:     return(rob);
                   1052:   }
                   1053:
                   1054:   /* request to the data channel */
                   1055:   if (cc1->dstate != DSTATE_ANY) {
                   1056:     errorOxmisc2("oxPushCMO: cc1->dstate != DSTATE_ANY, data channel is not ready to send data.\n");
                   1057:     return(rob);
                   1058:   }
                   1059:
                   1060:   if (!cmoCheckMathCap(ob,(struct object *)cc1->mathcapObjp)) {
1.12      takayama 1061:     errorOxmisc2("oxPushCMO: your peer does not understand this cmo.\n");
                   1062:     return(rob);
1.7       takayama 1063:   }
                   1064:   oxSendOXheader(cc1->datafp2,OX_DATA,SerialOX++);
                   1065:   cmoObjectToStream2(ob,cc1->datafp2);
                   1066:   /* synchronize cc1 and client. */
                   1067:   oxClientToObject(cc1,client);
                   1068:   return(ob);
1.8       takayama 1069: }
                   1070:
                   1071: oxclientp oxCreateControl_RFC_101(int fdstream,int portStream,
1.12      takayama 1072:                                   int ipmask,char *pass);
1.8       takayama 1073: struct object KoxCreateControl_RFC_101(struct object peer,struct object ipmask,struct object pass)
                   1074: {
                   1075:   struct object rob;
                   1076:   oxclientp client;
                   1077:   int fdStream, portStream;
                   1078:   int i;
                   1079:   struct object ob1;
                   1080:   rob.tag = Snull;
                   1081:   if (peer.tag != Sarray) {
                   1082:     errorOxmisc2("KoxCreateControl_RFC_101(): The first argument must be an array [fdStream, portStream]");
                   1083:     return(rob);
                   1084:   }
                   1085:   if (getoaSize(peer) != 2 ) {
                   1086:     errorOxmisc2("KoxCreateControl_RFC_101(): The first argument must be an array [fdStream, portStream] of size 2.");
                   1087:     return(rob);
                   1088:   }
                   1089:   for (i=0; i<getoaSize(peer); i++) {
                   1090:     ob1 = getoa(peer,i);
                   1091:     if (ob1.tag != Sinteger) {
                   1092:       errorOxmisc2("KoxCreateControl_RFC_101(): The element of the first argument must be an integer.");
                   1093:     }
                   1094:   }
                   1095:   fdStream = KopInteger(getoa(peer,0));
                   1096:   portStream = KopInteger(getoa(peer,1));
                   1097:
                   1098:   if (ipmask.tag != Sinteger) {
                   1099:     errorOxmisc2("KoxCreateControl_RFC_101(): ipmask must be an integer.");
                   1100:   }
                   1101:   if (pass.tag != Sdollar) {
                   1102:     errorOxmisc2("KoxCreateControl_RFC_101(): pass must be a string.");
                   1103:   }
                   1104:
                   1105:   client = oxCreateControl_RFC_101(fdStream, portStream,
1.12      takayama 1106:                                    KopInteger(ipmask), KopString(pass));
1.8       takayama 1107:   if (client == NULL) {
                   1108:     errorOxmisc2("KoxCreateControl_RFC_101(): Open error.");
                   1109:     return(rob);
                   1110:   }
                   1111:   rob = newObjectArray(N_OF_CLIENT_FIELDS);
                   1112:   oxClientToObject(client,rob);
                   1113:   return(rob);
                   1114: }
                   1115:
                   1116: oxclientp oxCreateControl_RFC_101(int fdstream,int portStream,
1.12      takayama 1117:                                   int ipmask,char *pass)
1.8       takayama 1118: {
                   1119:   int v = 0;
                   1120:   int fdControl = -1;
                   1121:   int fdStream = -1;
                   1122:   int m;
                   1123:
                   1124:   char *s;
                   1125:   oxclientp client;
1.13      takayama 1126: #if defined(__CYGWIN__)
                   1127:   extern sigjmp_buf MyEnv_oxmisc;
                   1128: #else
                   1129:   extern jmp_buf MyEnv_oxmisc;
                   1130: #endif
1.8       takayama 1131:   int engineByteOrder;
                   1132:   extern int Quiet;
                   1133:
                   1134:   v = !Quiet;
                   1135:
                   1136:   switch(ipmask) {
                   1137:   case 0:/* only local */
                   1138:     fdStream  = socketAcceptLocal(fdstream);
                   1139:     break;
                   1140:   default:/* any */
                   1141:     fdStream  = socketAccept(fdstream);
                   1142:     break;
                   1143:   }
                   1144:   if (v) fprintf(stderr,"\nControl port %d : Connected.\n",portStream);
                   1145:
                   1146:   if (fdStream == -1 ) {
                   1147:     fprintf(stderr,"\nOpen error in oxCreateControl_RFC_101.\n");
                   1148:     return(NULL);
                   1149:   }
                   1150:
                   1151:   /* Authentication by password. */
                   1152:   m = strlen(pass);
                   1153:   s = (char *)mymalloc(sizeof(char)*(m+1));
                   1154:   read(fdStream,s,m+1); s[m] = '\0';
                   1155:   if (strcmp(s,pass) != 0) {
                   1156:     fprintf(stderr,"oxCreateControl_RFC_101(): password authentication failed for control channel.\n");
                   1157:     close(fdStream);
                   1158:     return(NULL);
                   1159:   }
                   1160:
                   1161:
                   1162:   engineByteOrder = oxSetByteOrder(fdStream);
                   1163:   if (v) fprintf(stderr,"Byte order for control stackmacine is %s.\n",
1.12      takayama 1164:                  (engineByteOrder == 0? "network byte order":
                   1165:                   (engineByteOrder == 1? "little indican":
                   1166:                    "big indian")));
1.8       takayama 1167:
                   1168:
                   1169:   client = (oxclientp) mymalloc(sizeof(oxclient));
                   1170:   oxInitClient(client);
                   1171:   client->datafp2 = fp2open(fdStream);
                   1172:   if (client->datafp2 == NULL) {
                   1173:     fprintf(stderr,"oxCreateControl_RFC_101(): fp2open(fd) failed.\n");
                   1174:     return(NULL);
                   1175:   }
                   1176:   client->dataport = portStream;
                   1177:   client->controlport = -1;
                   1178:   client->controlfd = -1;
1.14    ! takayama 1179:   client->id = oxGetClientID();
1.11      takayama 1180:   client->type = CLIENT_SOCKET; /* socket */
                   1181:   client->engineByteOrder = engineByteOrder;
                   1182:   client->controlByteOrder = -1;
                   1183:   return(client);
                   1184: }
                   1185:
                   1186: oxclientp oxCreateEngine_RFC_101(int fdstream,int portStream,
1.12      takayama 1187:                                  int ipmask,char *pass, int engineID);
1.11      takayama 1188: struct object KoxCreateEngine_RFC_101(struct object peer,struct object ipmask,struct object pass, struct object engineID)
                   1189: {
                   1190:   struct object rob;
                   1191:   oxclientp client;
                   1192:   int fdStream, portStream;
                   1193:   int i;
                   1194:   struct object ob1;
                   1195:   rob.tag = Snull;
                   1196:   if (peer.tag != Sarray) {
                   1197:     errorOxmisc2("KoxCreateEngine_RFC_101(): The first argument must be an array [fdStream, portStream]");
                   1198:     return(rob);
                   1199:   }
                   1200:   if (getoaSize(peer) != 2 ) {
                   1201:     errorOxmisc2("KoxCreateEngine_RFC_101(): The first argument must be an array [fdStream, portStream] of size 2.");
                   1202:     return(rob);
                   1203:   }
                   1204:   for (i=0; i<getoaSize(peer); i++) {
                   1205:     ob1 = getoa(peer,i);
                   1206:     if (ob1.tag != Sinteger) {
                   1207:       errorOxmisc2("KoxCreateEngine_RFC_101(): The element of the first argument must be an integer.");
                   1208:     }
                   1209:   }
                   1210:   fdStream = KopInteger(getoa(peer,0));
                   1211:   portStream = KopInteger(getoa(peer,1));
                   1212:
                   1213:   if (ipmask.tag != Sinteger) {
                   1214:     errorOxmisc2("KoxCreateEngine_RFC_101(): ipmask must be an integer.");
                   1215:   }
                   1216:   if (pass.tag != Sdollar) {
                   1217:     errorOxmisc2("KoxCreateEngine_RFC_101(): pass must be a string.");
                   1218:   }
                   1219:   if (engineID.tag != Sinteger) {
                   1220:     errorOxmisc2("KoxCreateEngine_RFC_101(): engineID must be an integer.");
                   1221:   }
                   1222:
                   1223:   client = oxCreateEngine_RFC_101(fdStream, portStream,
1.12      takayama 1224:                                   KopInteger(ipmask), KopString(pass),KopInteger(engineID));
1.11      takayama 1225:   if (client == NULL) {
                   1226:     errorOxmisc2("KoxCreateEngine_RFC_101(): Open error.");
                   1227:     return(rob);
                   1228:   }
                   1229:   rob = newObjectArray(N_OF_CLIENT_FIELDS);
                   1230:   oxClientToObject(client,rob);
1.14    ! takayama 1231:   oxClientListUpdate(rob);
1.11      takayama 1232:   return(rob);
                   1233: }
                   1234:
                   1235: oxclientp oxCreateEngine_RFC_101(int fdstream,int portStream,
1.12      takayama 1236:                                  int ipmask,char *pass,int engineID)
1.11      takayama 1237: {
                   1238:   int v = 0;
                   1239:   int fdControl = -1;
                   1240:   int fdStream = -1;
                   1241:   int m;
                   1242:
                   1243:   char *s;
                   1244:   oxclientp client;
1.13      takayama 1245: #if defined(__CYGWIN__)
                   1246:   extern sigjmp_buf MyEnv_oxmisc;
                   1247: #else
                   1248:   extern jmp_buf MyEnv_oxmisc;
                   1249: #endif
1.11      takayama 1250:   int engineByteOrder;
                   1251:   extern int Quiet;
                   1252:
                   1253:   v = !Quiet;
                   1254:
                   1255:   switch(ipmask) {
                   1256:   case 0:/* only local */
                   1257:     fdStream  = socketAcceptLocal(fdstream);
                   1258:     break;
                   1259:   default:/* any */
                   1260:     fdStream  = socketAccept(fdstream);
                   1261:     break;
                   1262:   }
                   1263:   if (v) fprintf(stderr,"\nEngine port %d : Connected.\n",portStream);
                   1264:
                   1265:   if (fdStream == -1 ) {
                   1266:     fprintf(stderr,"\nOpen error in oxCreateEngine_RFC_101.\n");
                   1267:     return(NULL);
                   1268:   }
                   1269:
                   1270:   /* Authentication by password. */
                   1271:   /*  skip password check for now. BUG.
1.12      takayama 1272:       m = strlen(pass);
                   1273:       s = (char *)mymalloc(sizeof(char)*(m+1));
                   1274:       read(fdStream,s,m+1); s[m] = '\0';
                   1275:       if (strcmp(s,pass) != 0) {
                   1276:       fprintf(stderr,"oxCreateEngine_RFC_101(): password authentication failed for control channel.\n");
                   1277:       close(fdStream);
                   1278:       return(NULL);
                   1279:       }
1.11      takayama 1280:   */
                   1281:
                   1282:   engineByteOrder = oxSetByteOrder(fdStream);
                   1283:   if (v) fprintf(stderr,"Byte order for engine stackmacine is %s.\n",
1.12      takayama 1284:                  (engineByteOrder == 0? "network byte order":
                   1285:                   (engineByteOrder == 1? "little indican":
                   1286:                    "big indian")));
1.11      takayama 1287:
                   1288:
                   1289:   client = (oxclientp) mymalloc(sizeof(oxclient));
                   1290:   oxInitClient(client);
                   1291:   client->datafp2 = fp2open(fdStream);
                   1292:   if (client->datafp2 == NULL) {
                   1293:     fprintf(stderr,"oxCreateEngine_RFC_101(): fp2open(fd) failed.\n");
                   1294:     return(NULL);
                   1295:   }
                   1296:   client->dataport = portStream;
                   1297:   client->controlport = -1;
                   1298:   client->controlfd = -1;
                   1299:   client->id = engineID;
1.8       takayama 1300:   client->type = CLIENT_SOCKET; /* socket */
                   1301:   client->engineByteOrder = engineByteOrder;
                   1302:   client->controlByteOrder = -1;
                   1303:   return(client);
1.1       maekawa  1304: }
1.14    ! takayama 1305:
        !          1306: #define MAX_N_OF_CLIENT 1024
        !          1307: static struct object OxClientList[MAX_N_OF_CLIENT];
        !          1308: static int OxClientListn = 0;
        !          1309: int oxGetClientID() {
        !          1310:   extern struct object OxClientList[];
        !          1311:   extern int OxClientListn;
        !          1312:   extern struct object Nobj;
        !          1313:   int i;
        !          1314:   for (i=0; i<OxClientListn; i++) {
        !          1315:        if ((OxClientList[i]).tag == Snull) {
        !          1316:          return i;
        !          1317:        }
        !          1318:   }
        !          1319:   i = OxClientListn;
        !          1320:   (OxClientList[i]).tag = Snull;
        !          1321:   if (OxClientListn < MAX_N_OF_CLIENT-1) {
        !          1322:        OxClientListn++;
        !          1323:        return i;
        !          1324:   }else{
        !          1325:        errorOxmisc2("oxGetClientID(): the client table is full.\n");
        !          1326:        return 0;
        !          1327:   }
        !          1328: }
        !          1329: void oxClientListUpdate(struct object ob) {
        !          1330:   int id;
        !          1331:   extern struct object OxClientList[];
        !          1332:   id = KopInteger(getoa(ob,8));
        !          1333:   /* printf("id=%d\n",id); */
        !          1334:   if ((id <MAX_N_OF_CLIENT) && (id >= 0)) {
        !          1335:        OxClientList[id] = ob;
        !          1336:   }else{
        !          1337:        errorOxmisc2("oxClientListUpdate(): the client table is full.\n");
        !          1338:   }
        !          1339: }
        !          1340: void oxClientListRemove(struct object ob) {
        !          1341:   int id;
        !          1342:   extern struct object OxClientList[];
        !          1343:   id = KopInteger(getoa(ob,8));
        !          1344:   if ((id <MAX_N_OF_CLIENT) && (id >= 0)) {
        !          1345:        (OxClientList[id]).tag = Snull;
        !          1346:   }else{
        !          1347:        /* errorOxmisc2("oxClientListRemove(): the client table is full.\n");*/
        !          1348:   }
        !          1349: }
        !          1350: struct object KoxGetClientList() {
        !          1351:   extern int OxClientListn;
        !          1352:   extern struct object OxClientList[];
        !          1353:   int i,j,n;
        !          1354:   struct object rob;
        !          1355:   n = 0;
        !          1356:   for (i=0; i<OxClientListn; i++) {
        !          1357:        if ((OxClientList[i]).tag != Snull) n++;
        !          1358:   }
        !          1359:   rob = newObjectArray(n);
        !          1360:   for (i=0, j=0; i<OxClientListn; i++) {
        !          1361:        if ((OxClientList[i]).tag != Snull) {
        !          1362:          if (j >= n) {
        !          1363:                j=0;
        !          1364:                errorOxmisc2("oxGetClientList(): the client table is broken.\n");
        !          1365:          }
        !          1366:          putoa(rob,j,OxClientList[i]);
        !          1367:          j++;
        !          1368:        }
        !          1369:   }
        !          1370:   return rob;
        !          1371: }
        !          1372:
        !          1373:

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