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

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

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