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

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

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

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