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

1.3     ! takayama    1: /* $OpenXM: OpenXM/src/kan96xx/plugin/oxmisc2.c,v 1.2 1999/11/09 09:57:32 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"
                      6: extern FILE *MyErrorOut;
                      7: extern int SerialOX; /* defined in SerialOX */
                      8:
                      9: extern int OxVersion;
                     10:
                     11: int DebugMathCap = 1;
                     12:
                     13:
                     14:
                     15: int oxGet(oxclientp client, struct object *op,int *isObj)
                     16: /* This method should be synchronized. */
                     17: /* oxGet is a function for client. */
                     18: {
                     19:   int ans;
                     20:   ox_stream os;
                     21:   int m;
                     22:   struct object rob;
                     23:   int sss;  /* Serial number of the recieved packet. */
                     24:   *isObj = 0;
                     25:   op->tag = Snull;
                     26:   os = client->datafp2;
                     27:   switch(client->dstate) {
                     28:   case DSTATE_ANY:
                     29:     m = oxGetOXheader(os,&sss);
                     30:     switch(m) {
                     31:     case OX_DATA:
                     32:       client->dstate = DSTATE_WAIT_OX_DATA;
                     33:       return(oxGet(client,op,isObj));
                     34:     case OX_SYNC_BALL:
                     35:       client->dstate = DSTATE_ANY;
                     36:       return(OX_SYNC_BALL);
                     37:     default:
                     38:       errorOxmisc2("oxGet: cannot handle this tag.\n");
                     39:       client->dstate = DSTATE_ERROR;
                     40:       return(-1);
                     41:     }
                     42:     break;
                     43:   case DSTATE_FIRST_SYNC:  /* waiting the first sync ball */
                     44:     /* I need to clear the buffer?? */
                     45:     oxWaitSyncBall(os);
                     46:     client->dstate = DSTATE_ANY;
                     47:     oxSendSyncBall(os);
                     48:     return(OX_SYNC_BALL);
                     49:     break;
                     50:   case DSTATE_WAIT_OX_DATA:  /* waiting a cmo data. */
                     51:     *op = cmoObjectFromStream2(client->datafp2);
                     52:     client->dstate = DSTATE_ANY;
                     53:     *isObj = 1;
                     54:     return(0);
                     55:     break;
                     56:   case DSTATE_ERROR:
                     57:     client->dstate = DSTATE_ERROR;
                     58:     errorOxmisc2("oxGet: dstate == DSTATE_ERROR (error state)\n");
                     59:     return(-1);
                     60:   default:
                     61:     client->dstate = DSTATE_ERROR;
                     62:     errorOxmisc2("oxGet: Unknown state number.");
                     63:   }
                     64:
                     65:   return(-1);
                     66: }
                     67:
                     68: int oxGetFromControl(oxclientp client)
                     69: {
                     70:   int ans = -1;
                     71:   if (client->cstate != -1) {
                     72:     ans = oxGetResultOfControlInt32(client->controlfd);
                     73:     if (ans != -1) {    client->cstate = 0; }
                     74:     else {client->cstate = -1; }
                     75:   }
                     76:   return(ans);
                     77: }
                     78:
                     79: int oxReq(oxclientp client,int func,struct object ob)
                     80: {
                     81:   struct object *ob1p;
                     82:   /* request to the control channel */
                     83:   if (func == SM_control_reset_connection ||
                     84:       func == SM_control_kill) {
                     85:     switch(func) {
                     86:     case SM_control_reset_connection:
                     87:       oxReqControlResetConnection(client->controlfd);
                     88:       client->cstate = 1;
                     89:       client->dstate = DSTATE_FIRST_SYNC;
                     90:       break;
                     91:     case SM_control_kill:
                     92:       oxReqControlKill(client->controlfd);
                     93:       client->cstate = 0;
                     94:       client->dstate = DSTATE_ANY;
                     95:       break;
                     96:     }
                     97:     fflush(NULL);
                     98:     return(0);
                     99:   }
                    100:
                    101:   /* request to the data channel */
                    102:   if (client->dstate != DSTATE_ANY) {
                    103:     errorOxmisc2("oxReq: client->dstate != DSTATE_ANY, data channel is not ready to send data.\n");
                    104:     return(-1);
                    105:   }
                    106:   switch(func) {
                    107:   case SM_DUMMY_sendcmo:
                    108:     if (!cmoCheckMathCap(ob,(struct object *)client->mathcapObjp)) {
                    109:       errorOxmisc2("oxReq: your peer does not understand this cmo.\n");
                    110:       return(-1);
                    111:     }
                    112:     oxSendOXheader(client->datafp2,OX_DATA,SerialOX++);
                    113:     cmoObjectToStream2(ob,client->datafp2);
                    114:     client->dstate = DSTATE_ANY;
                    115:     break;
                    116:   case SM_sync_ball:
                    117:     oxSendSyncBall(client->datafp2);
                    118:     client->dstate = DSTATE_ANY; /*  We do not expect the sync ball.*/
                    119:     client->cstate = 0;          /* clear the cstate */
                    120:     break;
                    121:   case SM_popCMO:
                    122:     oxReqPopCMO(client->datafp2);
                    123:     client->dstate = DSTATE_ANY;
                    124:     break;
                    125:   case SM_mathcap:
                    126:     oxReqMathCap(client->datafp2);
                    127:     client->dstate = DSTATE_ANY;
                    128:     break;
                    129:   case SM_setMathCap:
                    130:     /* ob = [(mathcap-obj) [[version num, system name] [sm tags]
                    131:                              ob1                        smtags
                    132:                                                 [[ox numbers] [cmo numbers]]]
                    133:                                                     ob3         ob2 */
                    134:     {
                    135:       struct object ob1;
                    136:       struct object ob2;
                    137:       struct object ob3;
                    138:       struct object smtags;
                    139:     int n,i;
                    140:     struct mathCap mathcap;
                    141:
                    142:     if (strcmp(KopString(getoa(ob,0)),"mathcap-object") != 0) {
                    143:       errorOxmisc2("data format error in oxReqSetMathCap");
                    144:       client->dstate = DSTATE_ANY;
                    145:       break;
                    146:     }
                    147:       ob1 = getoa(getoa(ob,1),0);
                    148:       smtags = getoa(getoa(ob,1),1);
                    149:       ob2 = getoa(getoa(getoa(ob,1),2),1);
                    150:       ob3 = getoa(getoa(getoa(ob,1),2),0);
                    151:
                    152:       ob1p = (struct object *) sGC_malloc(sizeof(struct object));
                    153:       *ob1p = ob1;
                    154:       mathcap.infop = ob1p;
                    155:       n = getoaSize(ob2);
                    156:       mathcap.n = n;
                    157:       if (n >= MATHCAP_SIZE) errorOxmisc2("Too big mathcap of your peer.");
                    158:       for (i=0; i<n; i++) {
                    159:        mathcap.cmo[i] = KopInteger(getoa(ob2,i));
                    160:       }
                    161:       n = getoaSize(ob3);
                    162:       if (n >= MATHCAP_SIZE) errorOxmisc2("Too big mathcap of your peer.");
                    163:       mathcap.oxSize = n;
                    164:       for (i=0; i<n; i++) {
                    165:        mathcap.ox[i] = KopInteger(getoa(ob3,i));
                    166:       }
                    167:
                    168:       n = getoaSize(smtags);
                    169:       if (n >= MATHCAP_SIZE) errorOxmisc2("Too big mathcap of your peer.");
                    170:       mathcap.smSize = n;
                    171:       for (i=0; i<n; i++) {
                    172:        mathcap.sm[i] = KopInteger(getoa(smtags,i));
                    173:       }
                    174:
                    175:       oxReqSetMathCap(client->datafp2,&mathcap);
                    176:       client->dstate = DSTATE_ANY;
                    177:     }
                    178:     break;
                    179:   case SM_pops:
                    180:     if (ob.tag != Sinteger) {
                    181:       errorOxmisc2("SM_pops : the argument must be an integer.");
                    182:       return(-1);
                    183:     }
                    184:     oxReqPops(client->datafp2, KopInteger(ob));
                    185:     client->dstate = DSTATE_ANY;
                    186:     break;
                    187:   case SM_executeStringByLocalParser:
                    188:     if (ob.tag != Sdollar) {
                    189:       errorOxmisc2("SM_executeStringByLocalParser : the argument must be a string.");
                    190:       return(-1);
                    191:     }
                    192:     oxReqExecuteStringByLocalParser(client->datafp2,KopString(ob));
                    193:     client->dstate = DSTATE_ANY;
                    194:     break;
                    195:   case SM_executeFunction:
                    196:     if (ob.tag != Sdollar) {
                    197:       errorOxmisc2("SM_executeFunction : the argument must be a string.");
                    198:       return(-1);
                    199:     }
                    200:     oxReqExecuteFunction(client->datafp2,KopString(ob));
                    201:     client->dstate = DSTATE_ANY;
                    202:     break;
                    203:   case SM_popString:
                    204:     oxReqPopString(client->datafp2);
                    205:     client->dstate = DSTATE_ANY;
                    206:     break;
                    207:   case SM_evalName:
                    208:     if (ob.tag != Sdollar) {
                    209:       errorOxmisc2("SM_evalName : the argument must be a string.");
                    210:       return(-1);
                    211:     }
                    212:     oxReqEvalName(client->datafp2,KopString(ob));
                    213:     client->dstate = DSTATE_ANY;
                    214:     break;
                    215:   case SM_setName:
                    216:     if (ob.tag != Sdollar) {
                    217:       errorOxmisc2("SM_setName : the argument must be a string.");
                    218:       return(-1);
                    219:     }
                    220:     oxReqSetName(client->datafp2,KopString(ob));
                    221:     client->dstate = DSTATE_ANY;
                    222:     break;
                    223:   case SM_getsp:
                    224:     oxReqSingleOperand(client->datafp2,SM_getsp);
                    225:     client->dstate = DSTATE_ANY;
                    226:     break;
                    227:   case SM_dupErrors:
                    228:     oxReqSingleOperand(client->datafp2,SM_dupErrors);
                    229:     client->dstate = DSTATE_ANY;
                    230:     break;
                    231:   default:
                    232:     fprintf(MyErrorOut,"func=%d ",func);
                    233:     errorOxmisc2("This function is not implemented.");
                    234:     break;
                    235:   }
                    236:   fp2fflush(client->datafp2);
                    237:   return(0);
                    238: }
                    239:
                    240: struct object KoxCreateClient(struct object ip,
                    241:                              struct object portStream,
                    242:                              struct object portControl)
                    243: {
                    244:   struct object rob;
                    245:   oxclientp client;
                    246:   rob.tag = Snull;
                    247:   if (ip.tag != Sdollar) {
                    248:     errorOxmisc2("KoxCreateClient(): The first argument must be a hostname given by a string.");
                    249:     return(rob);
                    250:   }
                    251:   if (portStream.tag == Sdollar) {
                    252:     client = oxCreateClientFile(KopString(ip),KopString(portStream),
                    253:                                "/dev/null","w");
                    254:     if (client == NULL) {
                    255:       errorOxmisc2("KoxCreateClient(): Open error.");
                    256:       return(rob);
                    257:     }
                    258:     rob = newObjectArray(N_OF_CLIENT_FIELDS);
                    259:     oxClientToObject(client,rob);
                    260:     return(rob);
                    261:   }
                    262:
                    263:   if (portStream.tag != Sinteger) {
                    264:     errorOxmisc2("KoxCreateClient(): The second argument must be a port number given in an integer.");
                    265:     return(rob);
                    266:   }
                    267:   if (portControl.tag != Sinteger) {
                    268:     errorOxmisc2("KoxCreateClient(): The third argument must be a port number given in an integer.");
                    269:     return(rob);
                    270:   }
                    271:   client = oxCreateClient(KopString(ip),KopInteger(portStream),KopInteger(portControl));
                    272:   if (client == NULL) {
                    273:     errorOxmisc2("KoxCreateClient(): Open error.");
                    274:     return(rob);
                    275:   }
                    276:   rob = newObjectArray(N_OF_CLIENT_FIELDS);
                    277:   oxClientToObject(client,rob);
                    278:   return(rob);
                    279: }
                    280:
                    281: static int isItClientObject(struct object ob)
                    282: {
                    283:   int size,i;
                    284:   struct object ee[N_OF_CLIENT_FIELDS];
                    285:   if (ob.tag != Sarray) {
                    286:     return(0);
                    287:   }
                    288:   size = getoaSize(ob);
                    289:   if (size != N_OF_CLIENT_FIELDS) return(0);
                    290:   for (i=0; i<N_OF_CLIENT_FIELDS; i++) {
                    291:     ee[i] = getoa(ob,i);
                    292:   }
                    293:
                    294:   if (ee[0].tag != Sdollar) return(0);
                    295:   if (strcmp(KopString(ee[0]),"client")!=0) return(0);
                    296:
                    297:   if (ee[1].tag != Sfile) return(0);
                    298:   if (strcmp((ee[1]).lc.str,MAGIC2) != 0) return(0);
                    299:
                    300:   for (i=2; i<=9; i++) {
                    301:     if (ee[i].tag != Sinteger) return(0);
                    302:   }
                    303:   return(1);
                    304: }
                    305:
                    306:
                    307: struct object KoxIsThereErrorClient(struct object ob)
                    308: {
                    309:   struct object rob;
                    310:   int ans;
                    311:   int size;
                    312:   oxclient cc;
                    313:   rob.tag = Snull;
                    314:   if (!isItClientObject(ob)) {
                    315:     errorOxmisc2("KoxIsThereErrorClient(): the argument must be an array for client object.");
                    316:     return(rob);
                    317:   }
                    318:   if (oxObjectToClient(ob,&cc) == -1) return(KpoInteger(-1));
                    319:   ans = oxIsThereErrorClient(&cc);
                    320:   return(KpoInteger(ans));
                    321: }
                    322:
                    323: int oxClientToObject(oxclientp client,struct object rob)
                    324: {
                    325:   struct object ob;
                    326:   if (client == NULL) return;
                    327:   /* rob = newObjectArray(N_OF_CLIENT_FIELDS); */
                    328:   if (rob.tag != Sarray) {
                    329:     errorOxmisc2("oxClientToObject(): the second argument must be an array.");
                    330:     return(-1);
                    331:   }
                    332:   if (getoaSize(rob) != N_OF_CLIENT_FIELDS) {
                    333:     errorOxmisc2("oxClientToObject(): the second argument must be an array of size N_OF_CLIENT_FIELDS.");
                    334:     return(-1);
                    335:   }
                    336:
                    337:   ob = KpoString("client");
                    338:   putoa(rob,0,ob);
                    339:
                    340:   ob.tag = Sfile; ob.lc.str = MAGIC2; ob.rc.voidp = (void *)(client->datafp2);
                    341:   putoa(rob,1,ob);
                    342:
                    343:   putoa(rob,2,KpoInteger(client->dataport));
                    344:   putoa(rob,3,KpoInteger(client->controlfd));
                    345:   putoa(rob,4,KpoInteger(client->controlport));
                    346:   putoa(rob,5,KpoInteger(client->dstate));
                    347:   putoa(rob,6,KpoInteger(client->cstate));
                    348:   putoa(rob,7,KpoInteger(client->humanio));
                    349:   putoa(rob,8,KpoInteger(client->id));
                    350:   putoa(rob,9,KpoInteger(client->type));
                    351:   if (client->mathcapObjp == NULL) {
                    352:     putoa(rob,10,NullObject);
                    353:   }else{
                    354:     putoa(rob,10,*((struct object *)(client->mathcapObjp)));
                    355:   }
                    356:   putoa(rob,11,KpoInteger(client->engineByteOrder));
                    357:   putoa(rob,12,KpoInteger(client->controlByteOrder));
                    358:   return(0);
                    359: }
                    360:
                    361: int oxObjectToClient(struct object ob,oxclientp cp)
                    362: {
                    363:   struct object ob1;
                    364:   struct object *obp;
                    365:   if (cp == NULL) {
                    366:     errorOxmisc2("oxObjectToClient(): the second argument is NULL");
                    367:     return(-1);
                    368:   }
                    369:   if (!isItClientObject(ob)) {
                    370:     errorOxmisc2("oxObjectToClient(): the first argument is not client object.");
                    371:     oxInitClient(cp);
                    372:     return(-1);
                    373:   }
                    374:
                    375:   ob1 = getoa(ob,1);
                    376:   cp->datafp2 = (FILE2 *) (ob1.rc.voidp);
                    377:
                    378:   ob1 = getoa(ob,2);
                    379:   cp->dataport = KopInteger(ob1);
                    380:
                    381:   ob1 = getoa(ob,3);
                    382:   cp->controlfd = KopInteger(ob1);
                    383:
                    384:   ob1 = getoa(ob,4);
                    385:   cp->controlport = KopInteger(ob1);
                    386:
                    387:   ob1 = getoa(ob,5);
                    388:   cp->dstate = KopInteger(ob1);
                    389:
                    390:   ob1 = getoa(ob,6);
                    391:   cp->cstate = KopInteger(ob1);
                    392:
                    393:   ob1 = getoa(ob,7);
                    394:   cp->humanio = KopInteger(ob1);
                    395:
                    396:   ob1 = getoa(ob,8);
                    397:   cp->id = KopInteger(ob1);
                    398:
                    399:   ob1 = getoa(ob,9);
                    400:   cp->type = KopInteger(ob1);
                    401:
                    402:   ob1 = getoa(ob,10);
                    403:   if (ob1.tag == Snull) {
                    404:     cp->mathcapObjp = NULL;
                    405:   }else{
                    406:     obp = (struct object *) sGC_malloc(sizeof(struct object));
                    407:     *obp = ob1;
                    408:     cp->mathcapObjp = (void *)obp;
                    409:   }
                    410:   ob1 = getoa(ob,11);
                    411:   cp->engineByteOrder = KopInteger(ob1);
                    412:   ob1 = getoa(ob,12);
                    413:   cp->controlByteOrder = KopInteger(ob1);
                    414:
                    415:
                    416:   return(0);
                    417: }
                    418:
                    419: struct object KoxReq(struct object client,
                    420:                     struct object func,
                    421:                     struct object ob1)
                    422: {
                    423:   int ans;
                    424:   static oxclientp cc1 = NULL;
                    425:   struct object rob;
                    426:   rob.tag = Snull;
                    427:   if (cc1 == NULL) {
                    428:     cc1 = (oxclientp) mymalloc(sizeof(oxclient));
                    429:     if (cc1 == NULL) {
                    430:       errorOxmisc2("KoxReq(): no more memory.");
                    431:       return(rob);
                    432:     }
                    433:     oxInitClient(cc1);
                    434:   }
                    435:
                    436:   if (oxObjectToClient(client,cc1) == -1) return(rob);
                    437:   if (cc1 == NULL) {
                    438:     errorOxmisc2("KoxReq(): the first argument must be a client object.");
                    439:     return(rob);
                    440:   }
                    441:   if (func.tag != Sinteger) {
                    442:     errorOxmisc2("KoxReq(): the second argument must be an integer.");
                    443:     return(rob);
                    444:   }
                    445:   ans = oxReq(cc1,KopInteger(func),ob1);
                    446:   /* synchronize cc1 and client. */
                    447:   oxClientToObject(cc1,client);
                    448:
                    449:   return(KpoInteger(ans));
                    450: }
                    451:
                    452: struct object KoxGet(struct object client)
                    453: {
                    454:   int ans,k;
                    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("KoxGet(): 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("KoxGet(): the first argument must be a client object.");
                    470:     return(rob);
                    471:   }
                    472:
                    473:   ans = oxGet(cc1,&rob,&k);
                    474:   /* synchronize cc1 and client. */
                    475:   oxClientToObject(cc1,client);
                    476:
                    477:   if (k) return(rob);
                    478:   else {
                    479:     return(KpoInteger(ans));
                    480:   }
                    481: }
                    482:
                    483: struct object KoxGetFromControl(struct object client)
                    484: {
                    485:   int ans;
                    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("KoxGetFromControl(): 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("KoxGetFromControl(): the first argument must be a client object.");
                    501:     return(rob);
                    502:   }
                    503:
                    504:   ans = oxGetFromControl(cc1);
                    505:   /* synchronize cc1 and client. */
                    506:   oxClientToObject(cc1,client);
                    507:
                    508:   return(KpoInteger(ans));
                    509: }
                    510:
                    511: struct object KoxMultiSelect(struct object oclients,struct object t)
                    512: {
                    513:   static int first = 1;
                    514:   static int csize = 0;
                    515:   static oxclientp *clients = NULL;
                    516:   oxclientp cc1;
                    517:   struct object rob;
                    518:   int i;
                    519:   int tt;
                    520:   struct object ob1;
                    521:   struct object ob2;
                    522:   struct object ob0;
                    523:   int size;
                    524:   int ans;
                    525:   int dataready[1024];
                    526:   int controlready[1024];
                    527:
                    528:   rob.tag = Snull;
                    529:   if (oclients.tag != Sarray) {
                    530:     errorOxmisc2("KoxMultiSelect(): the first argument must be an array.");
                    531:     return(rob);
                    532:   }
                    533:   size = getoaSize(oclients);
                    534:   if (first) {
                    535:     first = 0; csize = size;
                    536:     clients = (oxclientp *)mymalloc(sizeof(oxclientp)*(size+1));
                    537:      if (clients == NULL) {
                    538:       errorOxmisc2("KoxMultiSelect(): no more memory.");
                    539:       return(rob);
                    540:     }
                    541:     for (i=0; i<size; i++) {
                    542:       clients[i] =  (oxclientp) mymalloc(sizeof(oxclient));
                    543:       if (clients[i] == NULL) {
                    544:        errorOxmisc2("KoxMultiSelect(): no more memory.");
                    545:        return(rob);
                    546:       }
                    547:       oxInitClient(clients[i]);
                    548:     }
                    549:   }
                    550:   if (csize < size)  {
                    551:     first = 1;
                    552:     return(KoxMultiSelect(oclients,t));
                    553:   }
                    554:   for (i=0; i<size; i++) {
                    555:     ob0 = getoa(oclients,i);
                    556:     if (oxObjectToClient(ob0,clients[i]) == -1) return(rob);
                    557:   }
                    558:   if (t.tag != Sinteger) {
                    559:     errorOxmisc2("KoxMultiSelect(): the second argument must be an integer.");
                    560:   }
                    561:   tt = KopInteger(t);
                    562:   ans = oxclientMultiSelect(clients,dataready,controlready,size,tt);
                    563:   /* synchronize oclients and clients. */
                    564:   for (i=0; i<size; i++) {
                    565:     ob0 = getoa(oclients,i);
                    566:     oxClientToObject(clients[i],ob0);
                    567:     putoa(oclients,i,ob0);
                    568:   }
                    569:   rob = newObjectArray(3);
                    570:   putoa(rob,0,KpoInteger(ans));
                    571:   ob1 = newObjectArray(size);
                    572:   ob2 = newObjectArray(size);
                    573:   for (i=0; i<size; i++) {
                    574:     putoa(ob1,i,KpoInteger(dataready[i]));
                    575:     putoa(ob2,i,KpoInteger(controlready[i]));
                    576:   }
                    577:   putoa(rob,1,ob1);
                    578:   putoa(rob,2,ob2);
                    579:   return(rob);
                    580: }
                    581:
                    582: struct object KoxWatch(struct object client,struct object f)
                    583: /* f is not used for now. It should be log file. */
                    584: {
                    585:   int ans,k;
                    586:   static oxclientp cc1 = NULL;
                    587:   struct object rob;
1.3     ! takayama  588:   extern int WatchStream;
1.1       maekawa   589:   rob.tag = Snull;
1.3     ! takayama  590:   if (client.tag == Sinteger) {
        !           591:        if (KopInteger(client)) {
        !           592:          WatchStream = 1;
        !           593:        }else{
        !           594:          WatchStream = 0;
        !           595:        }
        !           596:        return;
        !           597:   }
1.1       maekawa   598:   if (cc1 == NULL) {
                    599:     cc1 = (oxclientp) mymalloc(sizeof(oxclient));
                    600:     if (cc1 == NULL) {
                    601:       errorOxmisc2("KoxWatch(): no more memory.");
                    602:       return(rob);
                    603:     }
                    604:     oxInitClient(cc1);
                    605:   }
                    606:
                    607:   if (oxObjectToClient(client,cc1) == -1) return(rob);
                    608:   if (cc1 == NULL) {
                    609:     errorOxmisc2("KoxWatch(): the first argument must be a client object.");
                    610:     return(rob);
                    611:   }
                    612:
                    613:   k = fp2watch(cc1->datafp2,stdout);
                    614:   /* synchronize cc1 and client. */
                    615:   oxClientToObject(cc1,client);
                    616:
                    617:   return(KpoInteger(ans));
                    618: }
                    619:
                    620:
                    621: struct object KoxCloseClient(struct object client) {
                    622:   oxclientp cc1 = NULL;
                    623:   oxclient cc;
                    624:   struct object rob;
                    625:   rob.tag = Snull;
                    626:   cc1  = &cc;
                    627:   if (oxObjectToClient(client,cc1) == -1) return(rob);
                    628:   if (cc1 == NULL) {
                    629:     errorOxmisc2("KoxCloseClient(): the first argument must be a client object.");
                    630:     return(rob);
                    631:   }
                    632:
                    633:   fp2fflush(cc1->datafp2);
                    634:   if (cc1->humanio) {
                    635:     /* Do not close the file. */
                    636:     return(KpoInteger(0));
                    637:   }
                    638:   switch (cc1->type) {
                    639:   case CLIENT_SOCKET:
                    640:     fp2fclose(cc1->datafp2);
                    641:     close(cc1->controlfd);
                    642:     break;
                    643:   case CLIENT_FILE:
                    644:     fp2fclose(cc1->datafp2);
                    645:     close(cc1->controlfd);
                    646:     break;
                    647:   default:
                    648:     errorOxmisc2("Unknown client->type\n");
                    649:     break;
                    650:   }
                    651:   return(KpoInteger(0));
                    652:
                    653: }
                    654:
                    655: static int cmoCheck00(struct object obj,int cmo[], int n) {
                    656:   int i,j,m;
                    657:   int ttt;
                    658: #define CHECK00_N  4098      /* look up stackm.h and kclass.h */
                    659:   static int typeTrans[CHECK00_N];
                    660:   static int init = 0;
                    661:   if (n == 0) return(1);  /* For null cmolist, OK. */
                    662:   if (!init) {
                    663:     for (i=0; i<CHECK00_N; i++) {
                    664:       typeTrans[i] = 0;  /* unknown cmo number */
                    665:     }
                    666:     typeTrans[Snull] = CMO_NULL;
                    667:     typeTrans[Sinteger] = CMO_INT32;
                    668:     typeTrans[Sdollar] = CMO_STRING;
                    669:     if (OxVersion >= 199907170) {
                    670:       typeTrans[SuniversalNumber] = CMO_ZZ;
                    671:     }else{
                    672:       typeTrans[SuniversalNumber] = CMO_ZZ_OLD;
                    673:     }
                    674:     typeTrans[Sarray] = CMO_LIST;
                    675:     /* typeTrans[Spoly] = CMO_DMS;  */
                    676:     typeTrans[Spoly] = CMO_DISTRIBUTED_POLYNOMIAL;
                    677:     typeTrans[Sdouble] = CMO_64BIT_MACHINE_DOUBLE;
                    678:     typeTrans[CLASSNAME_ERROR_PACKET]   = CMO_ERROR2;
                    679:     typeTrans[CLASSNAME_mathcap] = CMO_MATHCAP;
                    680:     typeTrans[CLASSNAME_indeterminate] = CMO_INDETERMINATE;
                    681:     typeTrans[CLASSNAME_tree] = CMO_TREE;
                    682:     typeTrans[CLASSNAME_recursivePolynomial] = CMO_RECURSIVE_POLYNOMIAL;
                    683:     typeTrans[CLASSNAME_polynomialInOneVariable] = CMO_POLYNOMIAL_IN_ONE_VARIABLE;
                    684:     init = 1;
                    685:   }
                    686:   ttt = typeTrans[obj.tag];
                    687:   if (obj.tag == Sclass) {
                    688:     ttt = typeTrans[ectag(obj)];
                    689:   }
                    690:
                    691:   for (i=0; i<n; i++) {
                    692:     if (ttt == cmo[i]) {
                    693:       if (ttt != CMO_LIST) return(1);
                    694:       else {
                    695:        m = getoaSize(obj);
                    696:        for (j=0; j<m; j++) {
                    697:          if (!cmoCheck00(getoa(obj,j),cmo,n)) return(0);
                    698:        }
                    699:        return(1);
                    700:       }
                    701:     }
                    702:   }
                    703:   if (DebugMathCap) {
                    704:     if (DebugMathCap && 1) {
                    705:       fprintf(stderr,"Type translation table (internal object tag --> CMO tag)\n");
                    706:       for (i=0; i<20; i++) {
                    707:        printf("%d ", typeTrans[i]);
                    708:       }
                    709:       printf("\n");
                    710:     }
                    711:     fprintf(stderr,"The type of the argument object in sm1 is %d.\n",obj.tag);
                    712:     fprintf(stderr,"The type of the argument object in CMO is %d.\n",ttt);
                    713:     fprintf(stderr,"Available CMO tags in mathcap= %d elements : [ ",n);
                    714:     for (i=0; i<n; i++) {
                    715:       fprintf(stderr," %d ",cmo[i]);
                    716:     }
                    717:     fprintf(stderr," ] \n");
                    718:   }
                    719:   return(0);
                    720: }
                    721:
                    722: int cmoCheckMathCap(struct object obj, struct object *obp)
                    723: {
                    724:   struct object mathcap;
                    725:   struct object cmolist;
1.2       takayama  726:   struct object mathcapMain;
                    727:   struct object ob0;
1.1       maekawa   728:   int n;
                    729:   int i;
                    730: #define CMO_CHECK_MATH_CAP_LIST_SIZE 1024
                    731:   int cmo[CMO_CHECK_MATH_CAP_LIST_SIZE];
                    732:   if (obp == NULL) return(1);
1.2       takayama  733:   /* printObject(*obp,0,stderr); for debug*/
1.1       maekawa   734:   if (obp->tag != Sarray) {
                    735:     fprintf(stderr,"cmoCheckMathCap: the mathcap obj is \n");
                    736:     printObject(*obp,0,stderr);
                    737:     fprintf(stderr,"\n");
                    738:     errorOxmisc2("cmoCheckMathCap: format error in the client->mathcapObjp field.\n");
                    739:   }
                    740:   mathcap = *obp;
1.2       takayama  741:   /* Example of mathcap
                    742:     [    $mathcap-object$ ,
                    743:        [    [    199909080 , $Ox_system=ox_sm1.plain$ , $Version=2.991106$ ,
                    744:                  $HOSTTYPE=i386$ ]  ,
                    745:             [    262 , 263 , 264 , 265 , 266 , 268 , 269 , 272 , 273 , 275 ,
                    746:                  276 ]  ,
                    747:             [    [    514 ]  , [    2130706434 , 1 , 2 , 4 , 5 , 17 , 19 , 20 , 22 , 23 , 24 , 25 , 26 , 30 , 31 , 60 , 61 , 27 , 33 , 40 , 16 , 34 ]  ]  ]  ]
                    748:   */
                    749:
1.1       maekawa   750:   n = getoaSize(mathcap);
                    751:   if (n < 2) {
                    752:     fprintf(stderr,"cmoCheckMathCap: the mathcap obj is \n");
                    753:     printObject(*obp,0,stderr);
                    754:     fprintf(stderr,"\n");
                    755:     errorOxmisc2("cmoCheckMathCap: length of mathcap is wrong in the client->mathcapObjp field.\n");
                    756:   }
1.2       takayama  757:   ob0 = getoa(mathcap,0);
                    758:   if (ob0.tag != Sdollar) {
                    759:     fprintf(stderr,"cmoCheckMathCap: the mathcap obj is \n");
                    760:     printObject(*obp,0,stderr);
                    761:     fprintf(stderr,"\n");
                    762:     errorOxmisc2("cmoCheckMathCap: The first field must be the string mathcap-object.\n");
                    763:   }
                    764:   if (strcmp(KopString(ob0),"mathcap-object") != 0) {
                    765:     fprintf(stderr,"cmoCheckMathCap: the mathcap obj is \n");
                    766:     printObject(*obp,0,stderr);
                    767:     fprintf(stderr,"\n");
                    768:     errorOxmisc2("cmoCheckMathCap: The mathcap must be of the form [(mathcap-object) [...]]\n");
                    769:   }
                    770:
1.1       maekawa   771:   /* I should check
1.2       takayama  772:          getoa(getoa(mathcap,1),2)
1.1       maekawa   773:      contains OX_DATA.
                    774:      It has not yet implemented.
                    775:   */
1.2       takayama  776:   mathcapMain = getoa(mathcap,1);
                    777:   if (mathcapMain.tag != Sarray) {
                    778:     fprintf(stderr,"cmoCheckMathCap: mathcap[1] is \n");
                    779:     printObject(mathcapMain,0,stderr);
                    780:     fprintf(stderr,"\n");
                    781:     errorOxmisc2("cmoCheckMathCap: format error in the (client->mathcapObjp)[1] field. It should be an array.\n");
                    782:   }
                    783:   if (getoaSize(mathcapMain) < 3) {
                    784:     fprintf(stderr,"cmoCheckMathCap: mathcap[1] is \n");
                    785:     printObject(mathcapMain,0,stderr);
                    786:     fprintf(stderr,"\n");
                    787:     errorOxmisc2("cmoCheckMathCap: format error in the (client->mathcapObjp)[1] field. It should be an array of which length is more than 2.\n");
                    788:   }
                    789:   mathcap = getoa(mathcapMain,2);
1.1       maekawa   790:   n = getoaSize(mathcap);
                    791:   if (n < 2) {
                    792:     fprintf(stderr,"cmoCheckMathCap: the mathcap obj is \n");
                    793:     printObject(*obp,0,stderr);
                    794:     fprintf(stderr,"\n");
                    795:     errorOxmisc2("cmoCheckMathCap: length of mathcap is wrong in the client->mathcapObjp field.\n");
                    796:   }
                    797:   cmolist = getoa(mathcap,1);
                    798:   if (cmolist.tag != Sarray) {
                    799:     fprintf(stderr,"cmoCheckMathCap: the mathcap obj is \n");
                    800:     printObject(*obp,0,stderr);
                    801:     fprintf(stderr,"\n");
                    802:     errorOxmisc2("cmoCheckMathCap: mathcap[1] must be an array of integers.\n");
                    803:   }
                    804:   n = getoaSize(cmolist);
                    805:   if (n > CMO_CHECK_MATH_CAP_LIST_SIZE) {
                    806:     errorOxmisc2("cmoCheckMathCap: Too big cmo list.\n");
                    807:   }
                    808:   for (i=0; i<n; i++) {
                    809:     cmo[i] = KopInteger(getoa(cmolist,i));
                    810:   }
                    811:   return(cmoCheck00(obj,cmo,n));
                    812: }
                    813:
                    814:
                    815: struct object KoxGenPortFile(void) {
                    816:   struct object ob;
                    817:   ob = KpoString(oxGenPortFile());
                    818:   return(ob);
                    819: }
                    820: void KoxRemovePortFile(void) {
                    821:   oxRemovePortFile();
                    822: }
                    823:
                    824: void oxPushMathCap(struct mathCap *mathcap)
                    825: {
                    826:   struct object rob;
                    827:   rob = newMathCap(mathcap);
                    828:   Kpush(rob);
                    829: }
                    830:
                    831: struct object KoxGenPass(void) {
                    832:   struct object rob;
                    833:   rob = KpoString(oxGenPass());
                    834:   return(rob);
                    835: }
                    836:
                    837: struct object KoxGetPort(struct object host)
                    838: {
                    839:   struct object rob;
                    840:   int fdStream, fdControl;
                    841:   int portStream, portControl;
                    842:   extern int OpenedSocket;
                    843:   char *sname;
                    844:   rob = NullObject;
                    845:   if (host.tag != Sdollar) {
                    846:     errorOxmisc2("KoxGetPort: argument is not a string.");
                    847:     return(rob);
                    848:   }
                    849:   sname = KopString(host);
                    850:   fdControl = socketOpen(sname,0);
                    851:   portControl = OpenedSocket;
                    852:   fdStream = socketOpen(sname,0);
                    853:   portStream = OpenedSocket;
                    854:   rob = newObjectArray(4);
                    855:   putoa(rob,0,KpoInteger(fdStream));
                    856:   putoa(rob,1,KpoInteger(portStream));
                    857:   putoa(rob,2,KpoInteger(fdControl));
                    858:   putoa(rob,3,KpoInteger(portControl));
                    859:   return(rob);
                    860: }
                    861:
                    862: struct object KoxCreateClient2(struct object peer,
                    863:                               struct object ipmask,
                    864:                               struct object pass)
                    865: {
                    866:   struct object rob;
                    867:   oxclientp client;
                    868:   int fdStream, portStream, fdControl, portControl;
                    869:   int i;
                    870:   struct object ob1;
                    871:   rob.tag = Snull;
                    872:   if (peer.tag != Sarray) {
                    873:     errorOxmisc2("KoxCreateClient2(): The first argument must be an array [fdStream, portStream, fdControl, portControl]");
                    874:     return(rob);
                    875:   }
                    876:   if (getoaSize(peer) != 4) {
                    877:     errorOxmisc2("KoxCreateClient2(): The first argument must be an array [fdStream, portStream, fdControl, portControl] of size 4.");
                    878:     return(rob);
                    879:   }
                    880:   for (i=0; i<4; i++) {
                    881:     ob1 = getoa(peer,i);
                    882:     if (ob1.tag != Sinteger) {
                    883:       errorOxmisc2("KoxCreateClient2(): The element of the first argument must be an integer.");
                    884:     }
                    885:   }
                    886:   fdStream = KopInteger(getoa(peer,0));
                    887:   portStream = KopInteger(getoa(peer,1));
                    888:   fdControl = KopInteger(getoa(peer,2));
                    889:   portControl = KopInteger(getoa(peer,3));
                    890:
                    891:   if (ipmask.tag != Sinteger) {
                    892:     errorOxmisc2("KoxCreateClient2(): ipmask must be an integer.");
                    893:   }
                    894:   if (pass.tag != Sdollar) {
                    895:     errorOxmisc2("KoxCreateClient2(): pass must be a string.");
                    896:   }
                    897:
                    898:   client = oxCreateClient2(fdStream, portStream, fdControl, portControl,
                    899:                           KopInteger(ipmask), KopString(pass));
                    900:   if (client == NULL) {
                    901:     errorOxmisc2("KoxCreateClient2(): Open error.");
                    902:     return(rob);
                    903:   }
                    904:   rob = newObjectArray(N_OF_CLIENT_FIELDS);
                    905:   oxClientToObject(client,rob);
                    906:   return(rob);
                    907: }
                    908:
                    909: errorOxmisc2(char *s) {
                    910:   fprintf(MyErrorOut,"error in oxmisc2.c: %s\n",s);
                    911:   errorKan1("%s\n","  ");
                    912: }

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