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

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

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