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

Annotation of OpenXM/src/ox_toolkit/ox.c, Revision 1.8

1.1       ohara       1: /* -*- mode: C; coding: euc-japan -*- */
1.8     ! ohara       2: /* $OpenXM: OpenXM/src/ox_toolkit/ox.c,v 1.7 2000/01/17 19:55:55 ohara Exp $ */
1.1       ohara       3:
1.8     ! ohara       4: /*
        !             5:    This module includes functions for sending/receiveng CMO's.
        !             6:    Some commnets is written in Japanese by the EUC-JP coded
        !             7:    character set.
1.1       ohara       8: */
                      9:
1.8     ! ohara      10:
1.1       ohara      11: #include <stdio.h>
                     12: #include <stdlib.h>
                     13: #include <string.h>
                     14: #include <unistd.h>
                     15: #include <errno.h>
                     16: #include <fcntl.h>
1.5       ohara      17: #include <sys/file.h>
1.1       ohara      18: #include <gmp.h>
                     19:
                     20: #include "mysocket.h"
                     21: #include "ox.h"
                     22: #include "parse.h"
                     23:
                     24: static int          cmolen_cmo_int32(cmo_int32* c);
                     25: static int          cmolen_cmo_list(cmo_list* c);
                     26: static int          cmolen_cmo_mathcap(cmo_mathcap* c);
                     27: static int          cmolen_cmo_null(cmo_null* c);
                     28: static int          cmolen_cmo_string(cmo_string* c);
                     29: static int          cmolen_cmo_zz(cmo_zz* c);
                     30: static int          cmolen_cmo_monomial32(cmo_monomial32* c);
                     31:
                     32: static int          dump_cmo_int32(cmo_int32* m);
                     33: static int          dump_cmo_list(cmo_list* m);
                     34: static int          dump_cmo_mathcap(cmo_mathcap* m);
                     35: static int          dump_cmo_null(cmo_null* m);
                     36: static int          dump_cmo_string(cmo_string* m);
                     37: static int          dump_cmo_monomial32(cmo_monomial32* c);
                     38: static int          dump_cmo_zz(cmo_zz* c);
                     39: static int          dump_string(char *s, int len);
                     40: static int          dump_integer(int x);
                     41: static int          dump_mpz(mpz_ptr mpz);
                     42:
                     43: static int          login_with_otp(int fd, char* passwd);
                     44: static char         *create_otp();
                     45:
1.3       ohara      46: /* CMO_xxx の値の順にならべること(デバッグのため) */
1.1       ohara      47: static cmo_null*         receive_cmo_null(int fd);
                     48: static cmo_int32*        receive_cmo_int32(int fd);
                     49: static cmo_string*       receive_cmo_string(int fd);
                     50: static cmo_mathcap*      receive_cmo_mathcap(int fd);
                     51: static cmo_list*         receive_cmo_list(int fd);
                     52: static cmo_monomial32*   receive_cmo_monomial32(int fd);
                     53: static cmo_zz*           receive_cmo_zz(int fd);
                     54: static cmo_zero*         receive_cmo_zero(int fd);
                     55: static cmo_dms_generic*  receive_cmo_dms_generic(int fd);
                     56: static cmo_ring_by_name* receive_cmo_ring_by_name(int fd);
                     57: static cmo_distributed_polynomial* receive_cmo_distributed_polynomial(int fd);
                     58:
                     59: static cmo_error2*       receive_cmo_error2(int fd);
                     60: static void              receive_mpz(int fd, mpz_ptr mpz);
                     61:
                     62: static int          send_cmo_null(int fd, cmo_null* c);
                     63: static int          send_cmo_int32(int fd, cmo_int32* m);
                     64: static int          send_cmo_string(int fd, cmo_string* m);
                     65: static int          send_cmo_mathcap(int fd, cmo_mathcap* c);
                     66: static int          send_cmo_list(int fd, cmo_list* c);
                     67: static int          send_cmo_monomial32(int fd, cmo_monomial32* c);
                     68: static int          send_cmo_zz(int fd, cmo_zz* c);
                     69: static int          send_cmo_error2(int fd, cmo_error2* c);
                     70: static int          send_mpz(int fd, mpz_ptr mpz);
                     71: static int          send_cmo_distributed_polynomial(int fd, cmo_distributed_polynomial* c);
                     72:
                     73: static void         resize_mpz(mpz_ptr mpz, int size);
                     74:
1.3       ohara      75: static int          print_cmo_int32(cmo_int32* c);
                     76: static int          print_cmo_list(cmo_list* li);
                     77: static int          print_cmo_mathcap(cmo_mathcap* c);
                     78: static int          print_cmo_string(cmo_string* c);
                     79:
                     80: static char*        new_string_set_cmo_null();
                     81: static char*        new_string_set_cmo_int32(int integer);
                     82: static char*        new_string_set_cmo_list(cmo_list *c);
                     83: static char*        new_string_set_cmo_zz(cmo_zz *c);
                     84:
1.2       ohara      85: int current_fd = 0;
                     86: int set_current_fd(int fd)
                     87: {
                     88:        current_fd = fd;
                     89: }
                     90:
1.8     ! ohara      91: /* hook functions. (yet not implemented) */
1.2       ohara      92: static hook_t hook_before_send_cmo = NULL;
                     93: static hook_t hook_after_send_cmo  = NULL;
                     94:
                     95: int add_hook_before_send_cmo(hook_t func)
                     96: {
                     97:        hook_before_send_cmo = func;
                     98: }
                     99:
                    100: int add_hook_after_send_cmo(hook_t func)
                    101: {
                    102:        hook_after_send_cmo = func;
                    103: }
                    104:
                    105: static cmo *call_hook_before_send_cmo(int fd, cmo *c)
                    106: {
                    107:        if (hook_before_send_cmo != NULL) {
                    108:                return hook_before_send_cmo(fd, c);
                    109:        }
                    110:        return c;
                    111: }
                    112:
                    113: static cmo *call_hook_after_send_cmo(int fd, cmo *c)
                    114: {
                    115:        if (hook_after_send_cmo != NULL) {
                    116:                return hook_after_send_cmo(fd, c);
                    117:        }
                    118:        return c;
                    119: }
1.1       ohara     120:
1.7       ohara     121: /* Handling an error. */
1.1       ohara     122: static int current_received_serial = 0;
                    123:
1.7       ohara     124: /* If an error object be needed, then a server call the following function. */
1.1       ohara     125: cmo_error2* make_error_object(int err_code, cmo *ob)
                    126: {
                    127:     cmo_list* li = new_cmo_list();
                    128:     append_cmo_list(li, (cmo *)new_cmo_int32(current_received_serial));
                    129:     append_cmo_list(li, (cmo *)new_cmo_int32(err_code));
                    130:     append_cmo_list(li, ob);
                    131:     /* 他の情報を加えるならココ */
                    132:     return new_cmo_error2((cmo *)li);
                    133: }
                    134:
                    135: #define DEFAULT_SERIAL_NUMBER 0x0000ffff
                    136: #define receive_serial_number(x)   (receive_int32(x))
                    137:
1.7       ohara     138: /* getting a next serial number. */
1.1       ohara     139: int next_serial()
                    140: {
                    141:     static int serial_number = DEFAULT_SERIAL_NUMBER;
                    142:     return serial_number++;
                    143: }
                    144:
1.7       ohara     145: /* sending an object of int32 type. (not equal to cmo_int32 type)  */
1.1       ohara     146: int send_int32(int fd, int int32)
                    147: {
                    148:     int32 = htonl(int32);
                    149:     return write(fd, &int32, sizeof(int));
                    150: }
                    151:
1.7       ohara     152: /* receiving an object of int32 type. (not equal to cmo_int32 type)  */
1.1       ohara     153: int receive_int32(int fd)
                    154: {
                    155:     int tag;
                    156:     read(fd, &tag, sizeof(int));
                    157:     return ntohl(tag);
                    158: }
                    159:
1.7       ohara     160: /* receiving an (OX_tag, serial number)  */
1.1       ohara     161: int receive_ox_tag(int fd)
                    162: {
                    163:     int serial;
                    164:     int tag = receive_int32(fd);
                    165:     current_received_serial = receive_serial_number(fd);
                    166:     return tag;
                    167: }
                    168:
1.7       ohara     169: /* sending an (OX_tag, serial number)  */
1.1       ohara     170: int send_ox_tag(int fd, int tag)
                    171: {
                    172:     send_int32(fd, tag);
                    173:     return send_int32(fd, next_serial());
                    174: }
                    175:
1.7       ohara     176: /* functions for a cmo_list */
1.1       ohara     177: cell* new_cell()
                    178: {
                    179:     cell* h = malloc(sizeof(cell));
                    180:     h->next = NULL;
                    181:     h->cmo  = NULL;
                    182:     return h;
                    183: }
                    184:
                    185: cell* next_cell(cell* this)
                    186: {
                    187:     return this->next;
                    188: }
                    189:
                    190: static cell *tail(cmo_list* this) {
                    191:     cell *cp = this->head;
                    192:     while (cp->next != NULL) {
                    193:         cp = cp->next;
                    194:     }
                    195:     return cp;
                    196: }
                    197:
                    198: int append_cmo_list(cmo_list* this, cmo* newcmo)
                    199: {
                    200:     cell *cp = tail(this);
                    201:     cp->cmo  = newcmo;
                    202:     cp->next = new_cell();
                    203:     this->length++;
                    204:     return 0;
                    205: }
                    206:
1.3       ohara     207: cmo *nth_cmo_list(cmo_list* this, int n)
                    208: {
                    209:        cell *cp = this->head;
                    210:        if(this->length <= n) {
                    211:                return NULL;
                    212:        }
                    213:        while(n-- > 0) {
                    214:                cp = cp->next;
                    215:        }
                    216:        return cp->cmo;
                    217: }
                    218:
1.1       ohara     219: int length_cmo_list(cmo_list* this)
                    220: {
                    221:     return this->length;
                    222: }
                    223:
1.7       ohara     224: /* functions named receive_cmo_*. */
1.1       ohara     225: static cmo_null* receive_cmo_null(int fd)
                    226: {
                    227:     return new_cmo_null();
                    228: }
                    229:
                    230: static cmo_int32* receive_cmo_int32(int fd)
                    231: {
                    232:     int i = receive_int32(fd);
                    233:     return new_cmo_int32(i);
                    234: }
                    235:
                    236: static cmo_string* receive_cmo_string(int fd)
                    237: {
                    238:     int len = receive_int32(fd);
                    239:     char* s = malloc(len+1);
                    240:     memset(s, '\0', len+1);
                    241:     if (len > 0) {
                    242:         read(fd, s, len);
                    243:     }
                    244:     return new_cmo_string(s);
                    245: }
                    246:
                    247: static cmo_mathcap* receive_cmo_mathcap(int fd)
                    248: {
                    249:     cmo* ob = receive_cmo(fd);
                    250:     return new_cmo_mathcap(ob);
                    251: }
                    252:
                    253: static cmo_list* receive_cmo_list(int fd)
                    254: {
                    255:     cmo* ob;
                    256:     cmo_list* c = new_cmo_list();
                    257:     int len = receive_int32(fd);
                    258:
                    259:     while (len>0) {
                    260:         ob = receive_cmo(fd);
                    261:         append_cmo_list(c, ob);
                    262:         len--;
                    263:     }
                    264:     return c;
                    265: }
                    266:
                    267: static cmo_monomial32* receive_cmo_monomial32(int fd)
                    268: {
                    269:     int i;
                    270:     int len = receive_int32(fd);
                    271:     cmo_monomial32* c = new_cmo_monomial32(len);
                    272:
                    273:     for(i=0; i<len; i++) {
                    274:         c->exps[i] = receive_int32(fd);
                    275:     }
                    276:     c->coef = receive_cmo(fd);
                    277:     return c;
                    278: }
                    279:
                    280: static cmo_zz* receive_cmo_zz(int fd)
                    281: {
                    282:     cmo_zz* c = new_cmo_zz();
                    283:     receive_mpz(fd, c->mpz);
                    284:     return c;
                    285: }
                    286:
                    287: static cmo_zero* receive_cmo_zero(int fd)
                    288: {
                    289:     return new_cmo_zero();
                    290: }
                    291:
                    292: static cmo_dms_generic* receive_cmo_dms_generic(int fd)
                    293: {
                    294:     return new_cmo_dms_generic();
                    295: }
                    296:
                    297: static cmo_ring_by_name* receive_cmo_ring_by_name(int fd)
                    298: {
                    299:     cmo* ob = receive_cmo(fd);
1.7       ohara     300:        /* We need to check semantics but yet ... */
1.1       ohara     301:     return new_cmo_ring_by_name(ob);
                    302: }
                    303:
                    304: static cmo_distributed_polynomial* receive_cmo_distributed_polynomial(int fd)
                    305: {
                    306:     cmo* ob;
                    307:     cmo_distributed_polynomial* c = new_cmo_distributed_polynomial();
                    308:     int len = receive_int32(fd);
                    309:     c->ringdef = receive_cmo(fd);
                    310:
                    311:     while (len>0) {
                    312:         ob = receive_cmo(fd);
                    313:         append_cmo_list((cmo_list *)c, ob);
                    314:         len--;
                    315:     }
                    316:     return c;
                    317: }
                    318:
                    319: static cmo_error2* receive_cmo_error2(int fd)
                    320: {
                    321:     cmo* ob = receive_cmo(fd);
                    322:     return new_cmo_error2(ob);
                    323: }
                    324:
                    325: /* receive_ox_tag() == OX_DATA の後に呼び出される */
                    326: /* 関数ポインタを使った方がきれいに書けるような気がする.  */
                    327: /* if (foo[tag] != NULL) foo[tag](fd); とか */
                    328:
                    329: cmo* receive_cmo(int fd)
                    330: {
                    331:     cmo* m;
                    332:     int tag;
                    333:     tag = receive_int32(fd);
                    334:
                    335:     switch(tag) {
                    336:     case CMO_NULL:
                    337:         m = receive_cmo_null(fd);
                    338:         break;
                    339:     case CMO_INT32:
                    340:         m = (cmo *)receive_cmo_int32(fd);
                    341:         break;
                    342:     case CMO_STRING:
                    343:         m = (cmo *)receive_cmo_string(fd);
                    344:         break;
                    345:     case CMO_MATHCAP:
                    346:         m = (cmo *)receive_cmo_mathcap(fd);
                    347:         break;
                    348:     case CMO_LIST:
                    349:         m = (cmo *)receive_cmo_list(fd);
                    350:         break;
                    351:     case CMO_MONOMIAL32:
                    352:         m = (cmo *)receive_cmo_monomial32(fd);
                    353:         break;
                    354:     case CMO_ZZ:
                    355:         m = (cmo *)receive_cmo_zz(fd);
                    356:         break;
                    357:     case CMO_ZERO:
                    358:         m = (cmo *)receive_cmo_zero(fd);
                    359:         break;
                    360:     case CMO_DMS_GENERIC:
                    361:         m = (cmo *)receive_cmo_dms_generic(fd);
                    362:         break;
                    363:     case CMO_RING_BY_NAME:
                    364:         m = (cmo *)receive_cmo_ring_by_name(fd);
                    365:         break;
                    366:     case CMO_DISTRIBUTED_POLYNOMIAL:
                    367:         m = (cmo *)receive_cmo_distributed_polynomial(fd);
                    368:         break;
                    369:     case CMO_ERROR2:
                    370:         m = (cmo *)receive_cmo_error2(fd);
                    371:         break;
                    372:     case CMO_DATUM:
                    373:     case CMO_QQ:
                    374:     default:
                    375:         fprintf(stderr, "the CMO (%d) is not implemented.\n", m->tag);
                    376:     }
                    377:     return m;
                    378: }
                    379:
                    380: static void receive_mpz(int fd, mpz_ptr mpz)
                    381: {
                    382:     int i;
                    383:     int size  = receive_int32(fd);
                    384:     int len   = abs(size);
                    385:     resize_mpz(mpz, size);
                    386:
                    387:     for(i=0; i<len; i++) {
                    388:         mpz->_mp_d[i] = receive_int32(fd);
                    389:     }
                    390: }
                    391:
                    392: static void resize_mpz(mpz_ptr mpz, int size)
                    393: {
                    394:     _mpz_realloc(mpz, abs(size));
                    395:     mpz->_mp_size = size;
                    396: }
                    397:
1.7       ohara     398: /* functions named new_cmo_*. */
1.1       ohara     399: cmo_null* new_cmo_null()
                    400: {
                    401:     cmo_null* m = malloc(sizeof(cmo_null));
                    402:     m->tag = CMO_NULL;
                    403:     return m;
                    404: }
                    405:
                    406: cmo_int32* new_cmo_int32(int i)
                    407: {
                    408:     cmo_int32* c;
                    409:     c = malloc(sizeof(cmo_int32));
                    410:     c->tag     = CMO_INT32;
                    411:     c->i = i;
                    412:     return c;
                    413: }
                    414:
                    415: cmo_string* new_cmo_string(char* s)
                    416: {
                    417:     cmo_string* c = malloc(sizeof(cmo_string));
                    418:     c->tag = CMO_STRING;
                    419:     if (s != NULL) {
                    420:         c->s = malloc(strlen(s)+1);
                    421:         strcpy(c->s, s);
                    422:     }else {
                    423:         c->s = NULL;
                    424:     }
                    425:     return c;
                    426: }
                    427:
                    428: cmo_mathcap* new_cmo_mathcap(cmo* ob)
                    429: {
                    430:     cmo_mathcap* c = malloc(sizeof(cmo_mathcap));
                    431:     c->tag = CMO_MATHCAP;
                    432:     c->ob  = ob;
                    433:     return c;
                    434: }
                    435:
                    436: cmo_list* new_cmo_list()
                    437: {
                    438:     cmo_list* c = malloc(sizeof(cmo_list));
                    439:     c->tag    = CMO_LIST;
                    440:     c->length = 0;
                    441:     c->head->next = NULL;
                    442:     return c;
                    443: }
                    444:
                    445: cmo_monomial32* new_cmo_monomial32()
                    446: {
                    447:     cmo_monomial32* c = malloc(sizeof(cmo_monomial32));
                    448:     c->tag  = CMO_MONOMIAL32;
                    449:     return c;
                    450: }
                    451:
                    452: cmo_monomial32* new_cmo_monomial32_size(int size)
                    453: {
                    454:     cmo_monomial32* c = new_cmo_monomial32();
                    455:     if (size>0) {
                    456:         c->length = size;
                    457:         c->exps = malloc(sizeof(int)*size);
                    458:     }
                    459:     return c;
                    460: }
                    461:
                    462: cmo_zz* new_cmo_zz()
                    463: {
                    464:     cmo_zz* c = malloc(sizeof(cmo_zz));
                    465:     c->tag  = CMO_ZZ;
                    466:     mpz_init(c->mpz);
                    467:     return c;
                    468: }
                    469:
                    470: cmo_zz* new_cmo_zz_noinit()
                    471: {
                    472:     cmo_zz* c = malloc(sizeof(cmo_zz));
                    473:     c->tag  = CMO_ZZ;
                    474:     return c;
                    475: }
                    476:
                    477: cmo_zz* new_cmo_zz_set_si(int i)
                    478: {
                    479:     cmo_zz* c = new_cmo_zz();
                    480:     mpz_set_si(c->mpz, i);
                    481:     return c;
                    482: }
                    483:
1.7       ohara     484: cmo_zz* new_cmo_zz_set_mpz(mpz_ptr z)
                    485: {
                    486:     cmo_zz* c = new_cmo_zz();
                    487:     mpz_set(c->mpz, z);
                    488:     return c;
                    489: }
                    490:
1.1       ohara     491: cmo_zz *new_cmo_zz_set_string(char *s)
                    492: {
                    493:     cmo_zz* c = new_cmo_zz_noinit();
                    494:     mpz_init_set_str(c->mpz, s, 10);
                    495:     return c;
                    496: }
                    497:
                    498: cmo_zz* new_cmo_zz_size(int size)
                    499: {
                    500:     cmo_zz* c = new_cmo_zz();
                    501:     resize_mpz(c->mpz, size);
                    502:     return c;
                    503: }
                    504:
                    505: cmo_zero* new_cmo_zero()
                    506: {
                    507:     cmo_zero* m = malloc(sizeof(cmo_zero));
                    508:     m->tag = CMO_ZERO;
                    509:     return m;
                    510: }
                    511:
                    512: cmo_dms_generic* new_cmo_dms_generic()
                    513: {
                    514:     cmo_dms_generic* m = malloc(sizeof(cmo_dms_generic));
                    515:     m->tag = CMO_DMS_GENERIC;
                    516:     return m;
                    517: }
                    518:
                    519: cmo_ring_by_name* new_cmo_ring_by_name(cmo* ob)
                    520: {
                    521:     cmo_ring_by_name* c = malloc(sizeof(cmo_ring_by_name));
                    522:     c->tag = CMO_RING_BY_NAME;
                    523:     c->ob  = ob;
                    524:     return c;
                    525: }
                    526:
                    527: cmo_indeterminate* new_cmo_indeterminate(cmo* ob)
                    528: {
                    529:     cmo_indeterminate* c = malloc(sizeof(cmo_indeterminate));
                    530:     c->tag = CMO_INDETERMINATE;
                    531:     c->ob  = ob;
                    532:     return c;
                    533: }
                    534:
                    535: cmo_distributed_polynomial* new_cmo_distributed_polynomial()
                    536: {
                    537:     cmo_distributed_polynomial* c = malloc(sizeof(cmo_distributed_polynomial));
                    538:     c->tag     = CMO_DISTRIBUTED_POLYNOMIAL;
                    539:     c->length  = 0;
                    540:     c->head->next = NULL;
                    541:     c->ringdef = NULL;
                    542:     return c;
                    543: }
                    544:
                    545: cmo_error2* new_cmo_error2(cmo* ob)
                    546: {
                    547:     cmo_error2* c = malloc(sizeof(cmo_error2));
                    548:     c->tag = CMO_ERROR2;
                    549:     c->ob  = ob;
                    550:     return c;
                    551: }
                    552:
                    553: void send_ox_command(int fd, int sm_command)
                    554: {
                    555:     send_ox_tag(fd, OX_COMMAND);
                    556:     send_int32(fd, sm_command);
                    557: }
                    558:
                    559: int print_cmo(cmo* c)
                    560: {
                    561:     int tag = c->tag;
                    562:
                    563:     symbol* symp = lookup_by_tag(tag);
                    564:     if (symp != NULL) {
                    565:         fprintf(stderr, "(%s", symp->key);
                    566:     }else {
                    567:         fprintf(stderr, "(%d", tag);
                    568:     }
                    569:
                    570:     switch(tag) {
                    571:     case CMO_LIST:
                    572:         print_cmo_list((cmo_list *)c);
                    573:         break;
                    574:     case CMO_INT32:
                    575:         print_cmo_int32((cmo_int32 *)c);
                    576:         break;
                    577:     case CMO_MATHCAP:
                    578:     case CMO_INDETERMINATE:
                    579:     case CMO_RING_BY_NAME:
                    580:     case CMO_ERROR2:
                    581:         print_cmo_mathcap((cmo_mathcap *)c);
                    582:         break;
                    583:     case CMO_STRING:
                    584:         print_cmo_string((cmo_string *)c);
                    585:         break;
                    586:     case CMO_NULL:
                    587:     case CMO_ZERO:
                    588:     case CMO_DMS_GENERIC:
                    589:         fprintf(stderr, ")");
                    590:         break;
                    591:     default:
                    592:         fprintf(stderr, "print_cmo() does not know how to print.\n");
                    593:     }
                    594: }
                    595:
1.3       ohara     596: static int print_cmo_int32(cmo_int32* c)
1.1       ohara     597: {
                    598:     fprintf(stderr, ", %d)", c->i);
                    599: }
                    600:
1.3       ohara     601: static int print_cmo_list(cmo_list* li)
1.1       ohara     602: {
                    603:     cell* cp = li->head;
                    604:     while(cp->next != NULL) {
                    605:         fprintf(stderr, ", ");
                    606:         print_cmo(cp->cmo);
                    607:         cp=cp->next;
                    608:     }
                    609:     fprintf(stderr, ")");
                    610: }
                    611:
1.3       ohara     612: static int print_cmo_mathcap(cmo_mathcap* c)
1.1       ohara     613: {
                    614:     fprintf(stderr, ", ");
                    615:     print_cmo(c->ob);
                    616:     fprintf(stderr, ")");
                    617: }
                    618:
1.3       ohara     619: static int print_cmo_string(cmo_string* c)
1.1       ohara     620: {
                    621:     fprintf(stderr, ", \"%s\")", c->s);
                    622: }
                    623:
                    624: void ox_close(ox_file_t sv)
                    625: {
                    626:     send_ox_command(sv->control, SM_control_kill);
                    627: #ifdef DEBUG
1.7       ohara     628:     sleep(2);
                    629:        /* We wait thar an OpenXM server terminates. */
1.1       ohara     630:     fprintf(stderr, "I have closed the connection to an Open XM server.\n");
                    631: #endif
                    632: }
                    633:
1.3       ohara     634: void ox_shutdown(ox_file_t sv)
                    635: {
1.7       ohara     636:        /* We need to use SM_shutdown but yet ... */
1.3       ohara     637:        ox_close(sv);
                    638: }
                    639:
                    640: int ox_cmo_rpc(ox_file_t sv, char *function, int argc, cmo *argv[])
                    641: {
                    642:        int i = argc;
                    643:        while(i-- > 0) {
                    644:                send_ox_cmo(sv->stream, argv[i]);
                    645:        }
                    646:        send_ox_cmo(sv->stream, (cmo *)new_cmo_int32(argc));
                    647:        send_ox_cmo(sv->stream, (cmo *)new_cmo_string(function));
                    648:        send_ox_command(sv->stream, SM_executeFunction);
                    649: }
                    650:
                    651: void ox_execute_string(ox_file_t sv, char* s)
                    652: {
                    653:        send_ox_cmo(sv->stream, (cmo *)new_cmo_string(s));
                    654:        send_ox_command(sv->stream, SM_executeStringByLocalParser);
                    655: }
                    656:
                    657: void ox_push_cmd(ox_file_t sv, int sm_code)
1.1       ohara     658: {
1.3       ohara     659:     send_ox_command(sv->stream, sm_code);
1.1       ohara     660: }
                    661:
                    662: cmo_mathcap* ox_mathcap(ox_file_t sv)
                    663: {
                    664:     send_ox_command(sv->stream, SM_mathcap);
                    665:     send_ox_command(sv->stream, SM_popCMO);
                    666:     receive_ox_tag(sv->stream);          /* OX_DATA */
                    667:     return (cmo_mathcap *)receive_cmo(sv->stream);
                    668: }
                    669:
1.3       ohara     670: char* ox_popString(ox_file_t sv)
1.1       ohara     671: {
                    672:     cmo_string* m = NULL;
                    673:
1.3       ohara     674:     send_ox_command(sv->stream, SM_popString);
                    675:     receive_ox_tag(sv->stream); /* OX_DATA */
                    676:     m = (cmo_string *)receive_cmo(sv->stream);
1.1       ohara     677:     return m->s;
                    678: }
                    679:
1.3       ohara     680: int ox_pops(ox_file_t sv, int num)
                    681: {
                    682:        send_ox_cmo(sv->stream, (cmo *)new_cmo_int32(num));
                    683:     send_ox_command(sv->stream, SM_pops);
                    684: }
                    685:
                    686: cmo* ox_pop_cmo(ox_file_t sv)
                    687: {
                    688:     send_ox_command(sv->stream, SM_popCMO);
                    689:     receive_ox_tag(sv->stream); /* OX_DATA */
                    690:     return receive_cmo(sv->stream);
                    691: }
                    692:
                    693: void ox_push_cmo(ox_file_t sv, cmo *c)
                    694: {
                    695:        send_ox_cmo(sv->stream, c);
                    696: }
                    697:
1.7       ohara     698: /* a dummy function for flushing a connection. */
1.3       ohara     699: int ox_flush(ox_file_t sv)
1.1       ohara     700: {
1.3       ohara     701:        return 1;
1.1       ohara     702: }
                    703:
1.7       ohara     704: /* a dummy password function. */
1.1       ohara     705: static char *create_otp()
                    706: {
                    707:     static char otp[] = "otpasswd";
                    708:     return otp;
                    709: }
                    710:
1.7       ohara     711: /* proceeding an one time password. */
1.1       ohara     712: static int login_with_otp(int fd, char* passwd)
                    713: {
                    714:     int len   = strlen(passwd)+1;
                    715:     char *buf = alloca(len);
                    716:     int n     = read(fd, buf, len);
                    717:     int ret   = strcmp(passwd, buf);
                    718:
                    719: #ifdef DEBUG
                    720:     if (ret != 0) {
                    721:         fprintf(stderr, "Socket#%d: Login incorrect.\n", fd);
                    722:     }else {
                    723:         fprintf(stderr, "Socket#%d: login!.\n", fd);
                    724:     }
                    725:     fprintf(stderr, "password = (%s), %d bytes.\n", passwd, len);
                    726:     fprintf(stderr, "received = (%s), %d bytes.\n", buf, n);
                    727:     fflush(stderr);
                    728: #endif
                    729:
                    730:     return ret;
                    731: }
                    732:
1.8     ! ohara     733: /* The environment variable OpenXM_HOME must be defined. */
        !           734: static char *concat_openxm_home_bin(char *s)
1.1       ohara     735: {
1.8     ! ohara     736:     char *path;
        !           737:     char *base;
1.5       ohara     738:
1.8     ! ohara     739:     /* if s includes '/' then it is not concaticated. */
        !           740:        if (strchr(s, '/') != NULL) {
        !           741:                return s;
1.5       ohara     742:        }
1.8     ! ohara     743:
        !           744:        base = getenv("OpenXM_HOME");
        !           745:        path = malloc(strlen(base)+5+strlen(s));
        !           746:        sprintf(path, "%s/bin/%s", base, s);
        !           747:        return path;
1.1       ohara     748: }
                    749:
1.5       ohara     750: /* example: which("xterm", getenv("PATH")); */
                    751: static char *which(char *prog, char *path_env)
1.1       ohara     752: {
1.5       ohara     753:        char *tok;
                    754:        char *path;
                    755:        char delim[] = ":";
                    756:        char *e = alloca(strlen(path_env)+1);
                    757:        strcpy(e, path_env);
                    758:        tok = strtok(e, delim);
                    759:        while (tok != NULL) {
                    760:                char *path = malloc(strlen(tok)+strlen(prog)+2);
                    761:                sprintf(path, "%s/%s", tok, prog);
                    762:                if (access(path, X_OK&R_OK) == 0) {
                    763:                        return path;
                    764:                }
                    765:                free(path);
                    766:                tok = strtok(NULL, delim);
                    767:        }
                    768:        return NULL;
1.1       ohara     769: }
                    770:
                    771: static int mysocketAccept2(int fd, char *pass)
                    772: {
                    773:     fd = mysocketAccept(fd);
                    774:     if(login_with_otp(fd, pass)==0) {
                    775:         decideByteOrderClient(fd, 0);
                    776:         return fd;
                    777:     }
                    778:     close(fd);
                    779:     return -1;
                    780: }
                    781:
1.8     ! ohara     782: /* if it is not 0, then we use oxlog to execute ox. */
1.5       ohara     783: static int flag_ox_start_with_oxlog = 1;
                    784:
1.1       ohara     785: /*
                    786:    (-reverse 版の ox_start)
                    787:    ox_start は クライアントが呼び出すための関数である.
1.3       ohara     788:    サーバでは使われない.  ctl_prog はコントロールサーバであり,
1.1       ohara     789:    -ox, -reverse, -data, -control, -pass, -host
                    790:    というオプションを理解することを仮定する. dat_prog は計算サーバである.
                    791:    接続時には, sv->control を先にオープンする.
                    792: */
                    793:
                    794: ox_file_t ox_start(char* host, char* ctl_prog, char* dat_prog)
                    795: {
1.5       ohara     796:     ox_file_t sv = NULL;
1.1       ohara     797:     char *pass;
1.5       ohara     798:     char ctl[128], dat[128];
1.8     ! ohara     799:     short portControl = 0;  /* short! */
1.1       ohara     800:     short portStream  = 0;
1.8     ! ohara     801:        char *oxlog;
        !           802:
        !           803:        /* not overwrite */
        !           804:        setenv("OpenXM_HOME", "/usr/local/OpenXM", 0);
        !           805:
        !           806:        oxlog    = concat_openxm_home_bin("oxlog");
        !           807:        ctl_prog = concat_openxm_home_bin(ctl_prog);
        !           808:        dat_prog = concat_openxm_home_bin(dat_prog);
1.1       ohara     809:
                    810:     sv = malloc(sizeof(__ox_file_struct));
                    811:     sv->control = mysocketListen(host, &portControl);
                    812:     sv->stream  = mysocketListen(host, &portStream);
                    813:
                    814:     sprintf(ctl, "%d", portControl);
                    815:     sprintf(dat, "%d", portStream);
                    816:     pass = create_otp();
                    817:
                    818:     if (fork() == 0) {
1.5       ohara     819:                if (flag_ox_start_with_oxlog) {
                    820:                        execl(oxlog, oxlog, "xterm", "-icon", "-e", ctl_prog,
                    821:                                  "-reverse", "-ox", dat_prog,
                    822:                                  "-data", dat, "-control", ctl, "-pass", pass,
                    823:                                  "-host", host, NULL);
                    824:                }else {
                    825:                        dup2(2, 1);
1.6       ohara     826:                        dup2(open(tempnam("/tmp", "ox."), O_RDWR|O_CREAT|O_TRUNC, 0644),  2);
1.5       ohara     827:                        execl(ctl_prog, ctl_prog, "-reverse", "-ox", dat_prog,
                    828:                                  "-data", dat, "-control", ctl, "-pass", pass,
                    829:                                  "-host", host, NULL);
                    830:                }
                    831:                exit(1);
1.1       ohara     832:     }
                    833:
                    834:     if ((sv->control = mysocketAccept2(sv->control, pass)) == -1) {
                    835:         close(sv->stream);
                    836:         return NULL;
                    837:     }
1.8     ! ohara     838:     /* waiting 10 micro second. */
1.1       ohara     839:     usleep(10);
                    840:     if((sv->stream  = mysocketAccept2(sv->stream, pass)) == -1) {
                    841:         return NULL;
                    842:     }
                    843:     return sv;
                    844: }
                    845:
1.5       ohara     846: /* ssh -f host oxlog xterm -e ox -ox ox_asir ... */
                    847: int ssh_ox_server(char *host, char *ctl_prog, char *dat_prog, short portControl, short portStream)
                    848: {
1.8     ! ohara     849:        char *oxlog;
        !           850:        char *ssh;
        !           851:        oxlog    = concat_openxm_home_bin("oxlog");
        !           852:        ctl_prog = concat_openxm_home_bin(ctl_prog);
        !           853:        dat_prog = concat_openxm_home_bin(dat_prog);
        !           854:
        !           855:        ssh = which("ssh", getenv(PATH));
        !           856:
1.5       ohara     857:        if (fork() == 0) {
1.8     ! ohara     858:                execl(ssh, ssh, "-f", host, oxlog, "xterm", "-icon",
1.5       ohara     859:                          "-e", ctl_prog, "-insecure", "-ox", dat_prog,
                    860:                          "-data", portStream, "-control", portControl, "-host", host, NULL);
                    861:                exit(1);
                    862:        }
                    863: }
                    864:
1.1       ohara     865: /*
                    866:    (-insecure 版の ox_start)  まだ、中身はありません。
                    867:    ox_start_insecure_nonreverse は クライアントが呼び出すための関数である.
                    868:    接続時には, sv->control を先にオープンする.
                    869:    既定値:
                    870:    portControl = 1200
                    871:    portStream  = 1300
                    872: */
                    873:
                    874: ox_file_t ox_start_insecure_nonreverse(char* host, short portControl, short portStream)
                    875: {
                    876:     ox_file_t sv = malloc(sizeof(__ox_file_struct));
                    877:
                    878:     sv->control = mysocketOpen(host, portControl);
                    879: #if 0
                    880:     /* ox は insecure のとき byte order の決定が正しくできないようだ... */
                    881:     decideByteOrderClient(sv->control, 0);
                    882: #endif
1.8     ! ohara     883:     /* wainting 10 micro second. */
1.1       ohara     884:     usleep(10);
                    885:     sv->stream  = mysocketOpen(host, portStream);
                    886:     decideByteOrderClient(sv->stream, 0);
                    887:     return sv;
1.5       ohara     888: }
                    889:
                    890: ox_file_t ox_start_insecure_nonreverse2(char* host, char *ctl_prog, char *dat_prog)
                    891: {
1.8     ! ohara     892:        short portControl= 1200;  /* 自動生成させよう... */
1.5       ohara     893:        short portStream = 1300;
                    894:
                    895:        ssh_ox_server(host, ctl_prog, dat_prog, portControl, portStream);
                    896:        return ox_start_insecure_nonreverse(host, portControl, portStream);
1.1       ohara     897: }
                    898:
                    899: void ox_reset(ox_file_t sv)
                    900: {
                    901:     send_ox_command(sv->control, SM_control_reset_connection);
                    902:
                    903:     receive_ox_tag(sv->control);      /* OX_DATA */
                    904:     receive_cmo(sv->control);         /* (CMO_INT32, 0) */
                    905:
                    906:     while(receive_ox_tag(sv->stream) != OX_SYNC_BALL) {
                    907:         receive_cmo(sv->stream); /* skipping a message. */
                    908:     }
                    909:
                    910:     send_ox_tag(sv->stream, OX_SYNC_BALL);
                    911: #ifdef DEBUG
                    912:     fprintf(stderr, "I have reset an Open XM server.\n");
                    913: #endif
                    914: }
                    915:
1.8     ! ohara     916: /* the following functions are needed by bconv.c */
1.1       ohara     917:
                    918: /* cmolen 関数は cmo の(送信時の)バイト長を返す. */
                    919: /* cmolen_XXX 関数は cmo_XXX の tag を除いたバイト長を返す. */
                    920:
                    921: static int cmolen_cmo_null(cmo_null* c)
                    922: {
                    923:     return 0;
                    924: }
                    925:
                    926: static int cmolen_cmo_int32(cmo_int32* c)
                    927: {
                    928:     return sizeof(int);
                    929: }
                    930:
                    931: static int cmolen_cmo_string(cmo_string* c)
                    932: {
                    933:     return sizeof(int)+strlen(c->s);
                    934: }
                    935:
                    936: static int cmolen_cmo_mathcap(cmo_mathcap* c)
                    937: {
                    938:     return cmolen_cmo(c->ob);
                    939: }
                    940:
                    941: static int cmolen_cmo_list(cmo_list* c)
                    942: {
                    943:     int size = sizeof(int);
                    944:     cell* cp = c->head;
                    945:
                    946:     while(cp->next != NULL) {
                    947:         size += cmolen_cmo(cp->cmo);
                    948:         cp = cp->next;
                    949:     }
                    950:     return size;
                    951: }
                    952:
                    953: static int cmolen_cmo_monomial32(cmo_monomial32* c)
                    954: {
                    955:     int len = (c->length + 1)*sizeof(int);
                    956:     return len + cmolen_cmo(c->coef);
                    957: }
                    958:
                    959: static int cmolen_cmo_zz(cmo_zz* c)
                    960: {
                    961:     int len = abs(c->mpz->_mp_size);
                    962:     return sizeof(len) + len*sizeof(int);
                    963: }
                    964:
                    965: static int cmolen_cmo_distributed_polynomial(cmo_distributed_polynomial* c)
                    966: {
                    967:     return cmolen_cmo_list((cmo_list *)c) + cmolen_cmo(c->ringdef);
                    968: }
                    969:
1.8     ! ohara     970: /* calculating the length of the byte stream of given CMO.  */
1.1       ohara     971: int cmolen_cmo(cmo* c)
                    972: {
                    973:     int size = sizeof(int);
                    974:
                    975:     switch(c->tag) {
                    976:     case CMO_NULL:
                    977:     case CMO_ZERO:
                    978:     case CMO_DMS_GENERIC:
                    979:         size += cmolen_cmo_null(c);
                    980:         break;
                    981:     case CMO_INT32:
                    982:         size += cmolen_cmo_int32((cmo_int32 *)c);
                    983:         break;
                    984:     case CMO_STRING:
                    985:         size += cmolen_cmo_string((cmo_string *)c);
                    986:         break;
                    987:     case CMO_MATHCAP:
                    988:     case CMO_RING_BY_NAME:
                    989:     case CMO_INDETERMINATE:
                    990:     case CMO_ERROR2:
                    991:         size += cmolen_cmo_mathcap((cmo_mathcap *)c);
                    992:         break;
                    993:     case CMO_LIST:
                    994:         size += cmolen_cmo_list((cmo_list *)c);
                    995:         break;
                    996:     case CMO_MONOMIAL32:
                    997:         size += cmolen_cmo_monomial32((cmo_monomial32 *)c);
                    998:         break;
                    999:     case CMO_ZZ:
                   1000:         size += cmolen_cmo_zz((cmo_zz *)c);
                   1001:         break;
                   1002:     case CMO_DISTRIBUTED_POLYNOMIAL:
                   1003:         size += cmolen_cmo_distributed_polynomial((cmo_distributed_polynomial *)c);
                   1004:         break;
                   1005:     default:
                   1006:     }
                   1007:     return size;
                   1008: }
                   1009:
                   1010: static int  d_ptr;
                   1011: static char *d_buf;
                   1012:
                   1013: int init_dump_buffer(char *s)
                   1014: {
                   1015:     d_buf = s;
                   1016:     d_ptr = 0;
                   1017: }
                   1018:
                   1019: static int dump_cmo_null(cmo_null* m)
                   1020: {
                   1021:     return 0;
                   1022: }
                   1023:
                   1024: static int dump_cmo_int32(cmo_int32* m)
                   1025: {
                   1026:     dump_integer(m->i);
                   1027: }
                   1028:
                   1029: static int dump_cmo_string(cmo_string* m)
                   1030: {
                   1031:     int len = strlen(m->s);
                   1032:     dump_integer(len);
                   1033:     dump_string(m->s, len);
                   1034: }
                   1035:
                   1036: static int dump_cmo_mathcap(cmo_mathcap* c)
                   1037: {
                   1038:     dump_cmo(c->ob);
                   1039: }
                   1040:
                   1041: static int dump_cmo_list(cmo_list* m)
                   1042: {
                   1043:     cell* cp = m->head;
                   1044:     int len = length_cmo_list(m);
                   1045:     dump_integer(len);
                   1046:
                   1047:     while(cp->next != NULL) {
                   1048:         dump_cmo(cp->cmo);
                   1049:         cp = cp->next;
                   1050:     }
                   1051: }
                   1052:
                   1053: static int dump_cmo_monomial32(cmo_monomial32* c)
                   1054: {
                   1055:     int i;
                   1056:     int length = c->length;
                   1057:     dump_integer(c->length);
                   1058:     for(i=0; i<length; i++) {
                   1059:         dump_integer(c->exps[i]);
                   1060:     }
                   1061:     dump_cmo(c->coef);
                   1062: }
                   1063:
                   1064: static int dump_cmo_zz(cmo_zz* c)
                   1065: {
                   1066:     dump_mpz(c->mpz);
                   1067: }
                   1068:
                   1069: static int dump_cmo_distributed_polynomial(cmo_distributed_polynomial* m)
                   1070: {
                   1071:     cell* cp = m->head;
                   1072:     int len = length_cmo_list((cmo_list *)m);
                   1073:     dump_integer(len);
                   1074:     dump_cmo(m->ringdef);
                   1075:     while(cp != NULL) {
                   1076:         dump_cmo(cp->cmo);
                   1077:         cp = cp->next;
                   1078:     }
                   1079: }
                   1080:
1.8     ! ohara    1081: /* after its tag is sent, we invoke each functions. */
1.1       ohara    1082: int dump_cmo(cmo* m)
                   1083: {
                   1084:     dump_integer(m->tag);
                   1085:     switch(m->tag) {
                   1086:     case CMO_NULL:
                   1087:     case CMO_ZERO:
                   1088:     case CMO_DMS_GENERIC:
                   1089:         dump_cmo_null(m);
                   1090:         break;
                   1091:     case CMO_INT32:
                   1092:         dump_cmo_int32((cmo_int32 *)m);
                   1093:         break;
                   1094:     case CMO_STRING:
                   1095:         dump_cmo_string((cmo_string *)m);
                   1096:         break;
                   1097:     case CMO_MATHCAP:
                   1098:     case CMO_RING_BY_NAME:
                   1099:     case CMO_INDETERMINATE:
                   1100:     case CMO_ERROR2:
                   1101:         dump_cmo_mathcap((cmo_mathcap *)m);
                   1102:         break;
                   1103:     case CMO_LIST:
                   1104:         dump_cmo_list((cmo_list *)m);
                   1105:         break;
                   1106:     case CMO_MONOMIAL32:
                   1107:         dump_cmo_monomial32((cmo_monomial32 *)m);
                   1108:         break;
                   1109:     case CMO_ZZ:
                   1110:         dump_cmo_zz((cmo_zz *)m);
                   1111:         break;
                   1112:     case CMO_DISTRIBUTED_POLYNOMIAL:
                   1113:         dump_cmo_distributed_polynomial((cmo_distributed_polynomial *)m);
                   1114:         break;
                   1115:     default:
                   1116:     }
                   1117: }
                   1118:
                   1119: static int dump_mpz(mpz_ptr mpz)
                   1120: {
                   1121:     int i;
                   1122:     int len = abs(mpz->_mp_size);
                   1123:     dump_integer(mpz->_mp_size);
                   1124:     for(i=0; i<len; i++) {
                   1125:         dump_integer(mpz->_mp_d[i]);
                   1126:     }
                   1127:     return;
                   1128: }
                   1129:
                   1130: static int dump_string(char *s, int len)
                   1131: {
                   1132:     memcpy(&d_buf[d_ptr], s, len);
                   1133:     d_ptr += len;
                   1134: }
                   1135:
                   1136: static int dump_integer(int x)
                   1137: {
                   1138:     int nx = htonl(x);
                   1139:     dump_string((char *)&nx, sizeof(int));
                   1140: }
                   1141:
                   1142: int dump_ox_data(ox_data* m)
                   1143: {
                   1144:     dump_integer(OX_DATA);
                   1145:     dump_integer(-1);
                   1146:     dump_cmo(m->cmo);
                   1147: }
                   1148:
                   1149: int dump_ox_command(ox_command* m)
                   1150: {
                   1151:     dump_integer(OX_COMMAND);
                   1152:     dump_integer(-1);
                   1153:     dump_integer(m->command);
                   1154: }
                   1155:
1.3       ohara    1156: int send_ox(int fd, ox *m)
1.1       ohara    1157: {
                   1158:     int code;
1.3       ohara    1159:        switch(m->tag) {
                   1160:        case OX_DATA:
                   1161:         send_ox_cmo(fd, ((ox_data *)m)->cmo);
                   1162:                break;
                   1163:        case OX_COMMAND:
                   1164:                send_ox_command(fd, ((ox_command *)m)->command);
                   1165:                break;
                   1166:        default:
                   1167: #if 0
1.1       ohara    1168:         /* CMO?? */
                   1169:         send_ox_cmo(s->stream, (cmo *)m);
1.3       ohara    1170: #endif
1.1       ohara    1171:     }
                   1172: }
                   1173:
                   1174: int send_ox_cmo(int fd, cmo* m)
                   1175: {
                   1176:     send_ox_tag(fd, OX_DATA);
                   1177:     send_cmo(fd, m);
                   1178: }
                   1179:
1.8     ! ohara    1180: /* send_cmo_* functions */
1.1       ohara    1181: static int send_cmo_null(int fd, cmo_null* c)
                   1182: {
                   1183:     return 0;
                   1184: }
                   1185:
                   1186: static int send_cmo_int32(int fd, cmo_int32* m)
                   1187: {
                   1188:     send_int32(fd, m->i);
                   1189: }
                   1190:
                   1191: static int send_cmo_string(int fd, cmo_string* m)
                   1192: {
                   1193:     int len = (m->s != NULL)? strlen(m->s): 0;
                   1194:     send_int32(fd, len);
                   1195:     if (len > 0) {
                   1196:         write(fd, m->s, len);
                   1197:     }
                   1198:     return 0;
                   1199: }
                   1200:
                   1201: static int send_cmo_mathcap(int fd, cmo_mathcap* c)
                   1202: {
                   1203:     send_cmo(fd, c->ob);
                   1204:     return 0;
                   1205: }
                   1206:
                   1207: static int send_cmo_list(int fd, cmo_list* c)
                   1208: {
                   1209:     cell* cp = c->head;
                   1210:     int len = length_cmo_list(c);
                   1211:     send_int32(fd, len);
                   1212:
                   1213:     while(cp->next != NULL) {
                   1214:         send_cmo(fd, cp->cmo);
                   1215:         cp = cp->next;
                   1216:     }
                   1217:     return 0;
                   1218: }
                   1219:
                   1220: static int send_cmo_distributed_polynomial(int fd, cmo_distributed_polynomial* c)
                   1221: {
                   1222:     cell* cp = c->head;
                   1223:     int len = length_cmo_list((cmo_list *)c);
                   1224:     send_int32(fd, len);
                   1225:     send_cmo(fd, c->ringdef);
                   1226:
                   1227:     while(cp->next != NULL) {
                   1228:         send_cmo(fd, cp->cmo);
                   1229:         cp = cp->next;
                   1230:     }
                   1231:     return 0;
                   1232: }
                   1233:
                   1234: static int send_cmo_monomial32(int fd, cmo_monomial32* c)
                   1235: {
                   1236:     int i;
                   1237:     int len = c->length;
                   1238:     send_int32(fd, len);
                   1239:     for(i=0; i<len; i++) {
                   1240:         send_int32(fd, c->exps[i]);
                   1241:     }
                   1242:     send_cmo(fd, c->coef);
                   1243:     return 0;
                   1244: }
                   1245:
                   1246: static int send_cmo_zz(int fd, cmo_zz* c)
                   1247: {
                   1248:     send_mpz(fd, c->mpz);
                   1249:     return 0;
                   1250: }
                   1251:
                   1252: static int send_cmo_error2(int fd, cmo_error2* c)
                   1253: {
                   1254:     send_cmo(fd, c->ob);
                   1255:     return 0;
                   1256: }
                   1257:
1.8     ! ohara    1258: /* sending a CMO.  (Remarks: OX_tag is already sent.) */
1.1       ohara    1259: int send_cmo(int fd, cmo* c)
                   1260: {
                   1261:     int tag = c->tag;
                   1262:
1.2       ohara    1263:        c = call_hook_before_send_cmo(fd, c);
                   1264:
1.1       ohara    1265:     send_int32(fd, tag);
                   1266:     switch(tag) {
                   1267:     case CMO_NULL:
                   1268:     case CMO_ZERO:
                   1269:     case CMO_DMS_GENERIC:
1.8     ! ohara    1270:         send_cmo_null(fd, c);  /* empty function. */
1.1       ohara    1271:         break;
                   1272:     case CMO_INT32:
                   1273:         send_cmo_int32(fd, (cmo_int32 *)c);
                   1274:         break;
                   1275:     case CMO_STRING:
                   1276:         send_cmo_string(fd, (cmo_string *)c);
                   1277:         break;
                   1278:     case CMO_MATHCAP:
                   1279:     case CMO_ERROR2:
                   1280:     case CMO_RING_BY_NAME:
                   1281:     case CMO_INDETERMINATE:
                   1282:         send_cmo_mathcap(fd, (cmo_mathcap *)c);
                   1283:         break;
                   1284:     case CMO_LIST:
                   1285:         send_cmo_list(fd, (cmo_list *)c);
                   1286:         break;
                   1287:     case CMO_MONOMIAL32:
                   1288:         send_cmo_monomial32(fd, (cmo_monomial32 *)c);
                   1289:         break;
                   1290:     case CMO_ZZ:
                   1291:         send_cmo_zz(fd, (cmo_zz *)c);
                   1292:         break;
                   1293:     case CMO_DISTRIBUTED_POLYNOMIAL:
                   1294:         send_cmo_distributed_polynomial(fd, (cmo_distributed_polynomial *)c);
                   1295:         break;
                   1296:     default:
1.2       ohara    1297:                call_hook_after_send_cmo(fd, c);
1.1       ohara    1298:     }
                   1299: }
                   1300:
                   1301: static int send_mpz(int fd, mpz_ptr mpz)
                   1302: {
                   1303:     int i;
                   1304:     int len = abs(mpz->_mp_size);
                   1305:     send_int32(fd, mpz->_mp_size);
                   1306:     for(i=0; i<len; i++) {
                   1307:         send_int32(fd, mpz->_mp_d[i]);
                   1308:     }
                   1309:     return 0;
                   1310: }
                   1311:
                   1312: ox_data* new_ox_data(cmo* c)
                   1313: {
                   1314:     ox_data* m = malloc(sizeof(ox_data));
                   1315:     m->tag = OX_DATA;
                   1316:     m->cmo = c;
                   1317:     return m;
                   1318: }
                   1319:
                   1320: ox_command* new_ox_command(int sm_code)
                   1321: {
                   1322:     ox_command* m = malloc(sizeof(ox_command));
                   1323:     m->tag = OX_COMMAND;
                   1324:     m->command = sm_code;
                   1325:     return m;
                   1326: }
                   1327:
                   1328: ox_sync_ball* new_ox_sync_ball()
                   1329: {
                   1330:     ox_sync_ball *m = malloc(sizeof(ox_sync_ball));
                   1331:     m->tag = OX_SYNC_BALL;
                   1332:     return m;
                   1333: }
                   1334:
                   1335: #define ID_TEMP   "(CMO_LIST, (CMO_INT32, %d), (CMO_STRING, \"%s\"), (CMO_STRING, \"%s\"), (CMO_STRING, \"%s\"))"
                   1336:
                   1337: static cmo_list* make_list_of_id(int ver, char* ver_s, char* sysname)
                   1338: {
                   1339:     cmo_list *cap;
                   1340:     char buff[512];
                   1341:
                   1342:     sprintf(buff, ID_TEMP, ver, sysname, ver_s, getenv("HOSTTYPE"));
1.6       ohara    1343:     init_parser(buff);
1.1       ohara    1344:     cap = (cmo_list *)parse();
                   1345:
                   1346:     return cap;
                   1347: }
                   1348:
                   1349: static cmo_list *make_list_of_tag(int type)
                   1350: {
                   1351:     cmo_list *li = new_cmo_list();
                   1352:     symbol *symp;
                   1353:     int i = 0;
                   1354:     while((symp = lookup(i++))->key != NULL) {
                   1355:         if (symp->type == type) {
                   1356:             append_cmo_list(li, (cmo *)new_cmo_int32(symp->tag));
                   1357:         }
                   1358:     }
                   1359:     return li;
                   1360: }
                   1361:
                   1362: cmo* make_mathcap_object(int version, char *id_string)
                   1363: {
                   1364:     char *sysname    = "ox_math";
                   1365:     cmo_list *li     = new_cmo_list();
                   1366:     cmo_list *li_1st = make_list_of_id(version, id_string, sysname);
                   1367:     cmo_list *li_2nd = make_list_of_tag(IS_SM);
                   1368:     cmo_list *li_3rd = new_cmo_list();
                   1369:     cmo_list *li_cmo = make_list_of_tag(IS_CMO);
                   1370:
                   1371:     cmo_list *li_ox_data  = new_cmo_list();
                   1372:     append_cmo_list(li_ox_data,  (cmo *)new_cmo_int32(OX_DATA));
                   1373:     append_cmo_list(li_ox_data,  (cmo *)li_cmo);
                   1374:     append_cmo_list(li_3rd, (cmo *)li_ox_data);
                   1375:
                   1376:     append_cmo_list(li, (cmo *)li_1st);
                   1377:     append_cmo_list(li, (cmo *)li_2nd);
                   1378:     append_cmo_list(li, (cmo *)li_3rd);
                   1379:
                   1380:     return (cmo *)new_cmo_mathcap((cmo *)li);
                   1381: }
                   1382:
                   1383: /* ファイルディスクリプタ fd の通信路での integer の byte order を決定する */
                   1384: /* 実際には order (0,1,or 0xFF)をみてはいない */
                   1385: int decideByteOrderClient(oxfd fd, int order)
                   1386: {
                   1387:     char zero = OX_BYTE_NETWORK_BYTE_ORDER;
                   1388:     char dest;
                   1389:     read(fd, &dest, sizeof(char));
                   1390:     write(fd, &zero, sizeof(char));
                   1391:     return 0;
                   1392: }
                   1393:
                   1394: /* Server 側ではこちらを用いる */
                   1395: /* いまの実装は dup されていることが前提になっている */
                   1396: int decideByteOrderServer(oxfd fd, int order)
                   1397: {
                   1398:     char zero = OX_BYTE_NETWORK_BYTE_ORDER;
                   1399:     char dest;
                   1400:     write(fd, &zero, sizeof(char));
                   1401:     read(fd, &dest, sizeof(char));
                   1402:     return 0;
                   1403: }
                   1404:
                   1405: /* cmo と string (ここではC言語のstring) の変換関数群 */
1.3       ohara    1406: static char *new_string_set_cmo_zz(cmo_zz *c)
1.1       ohara    1407: {
                   1408:     return mpz_get_str(NULL, 10, c->mpz);
                   1409: }
                   1410:
1.3       ohara    1411: static char *new_string_set_cmo_null()
1.1       ohara    1412: {
                   1413:     static char* null_string = "";
                   1414:     return null_string;
                   1415: }
                   1416:
1.3       ohara    1417: static char *new_string_set_cmo_int32(int integer)
1.1       ohara    1418: {
                   1419:     char buff[1024];
                   1420:     char *s;
                   1421:
                   1422:     sprintf(buff, "%d", integer);
                   1423:     s = malloc(strlen(buff)+1);
                   1424:     strcpy(s, buff);
                   1425:
                   1426:     return s;
                   1427: }
                   1428:
1.3       ohara    1429: static char *new_string_set_cmo_list(cmo_list *m)
1.1       ohara    1430: {
                   1431:     char *s;
                   1432:     int i;
                   1433:     int size = 0;
                   1434:     int len = length_cmo_list(m);
                   1435:     char **sp = malloc(len*sizeof(cmo *));
                   1436:
                   1437:     cell *cp = m->head;
                   1438:     for(i = 0; i < len; i++) {
1.3       ohara    1439:         sp[i] = new_string_set_cmo(cp->cmo);
1.1       ohara    1440:         size += strlen(sp[i]) + 3;
                   1441:         cp = cp->next;
                   1442:     }
                   1443:     s = malloc(size+2);
                   1444:     strcpy(s, "[ ");
                   1445:     for(i = 0; i < len - 1; i++) {
                   1446:         strcat(s, sp[i]);
                   1447:         strcat(s, " , ");
                   1448:     }
                   1449:     strcat(s, sp[len-1]);
                   1450:     strcat(s, " ]");
                   1451:     free(sp);
                   1452:     return s;
                   1453: }
                   1454:
1.3       ohara    1455: char *new_string_set_cmo(cmo *m)
1.1       ohara    1456: {
                   1457:     symbol *symp;
                   1458:     switch(m->tag) {
                   1459:     case CMO_ZZ:
1.3       ohara    1460:         return new_string_set_cmo_zz((cmo_zz *)m);
1.1       ohara    1461:     case CMO_INT32:
1.3       ohara    1462:         return new_string_set_cmo_int32(((cmo_int32 *)m)->i);
1.1       ohara    1463:     case CMO_STRING:
                   1464:         return ((cmo_string *)m)->s;
                   1465:     case CMO_NULL:
1.3       ohara    1466:         return new_string_set_cmo_null();
1.1       ohara    1467:     case CMO_LIST:
1.3       ohara    1468:         return new_string_set_cmo_list((cmo_list *)m);
1.1       ohara    1469:     default:
                   1470: #ifdef DEBUG
                   1471:         symp = lookup_by_tag(m->tag);
                   1472:         fprintf(stderr, "I do not know how to convert %s to a string.\n", symp->key);
                   1473: #endif
1.8     ! ohara    1474:         /* yet not implemented. */
1.1       ohara    1475:         return NULL;
                   1476:     }
                   1477: }

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