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

1.1       ohara       1: /* -*- mode: C; coding: euc-japan -*- */
1.7     ! ohara       2: /* $OpenXM: OpenXM/src/ox_math/ox.c,v 1.6 1999/11/04 03:05:50 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: /* エラーハンドリングのため */
                     92: static int current_received_serial = 0;
                     93:
                     94: /* エラーを起こしたときはサーバは次のようにすればよい.  */
1.2       ohara      95: cmo_error2* gen_error_object(int err_code)
1.1       ohara      96: {
                     97:     cmo_list* li = new_cmo_list();
                     98:     append_cmo_list(li, (cmo *)new_cmo_int32(current_received_serial));
                     99:     append_cmo_list(li, (cmo *)new_cmo_int32(err_code));
                    100:     /* 他の情報を加えるならココ */
1.2       ohara     101:     return new_cmo_error2((cmo *)li);
1.1       ohara     102: }
                    103:
                    104: /* add at Mon Sep  7 15:51:28 JST 1998 */
                    105: #define DEFAULT_SERIAL_NUMBER 0x0000ffff
                    106: #define receive_serial_number(x)   (receive_int32(x))
                    107:
                    108: /* 新しいシリアル番号を得る */
                    109: int next_serial()
                    110: {
                    111:     static int serial_number = DEFAULT_SERIAL_NUMBER;
                    112:     return serial_number++;
                    113: }
                    114:
                    115: /* int32 型のオブジェクトを送信する.  */
                    116: int send_int32(int fd, int int32)
                    117: {
                    118:     int32 = htonl(int32);
                    119:     return write(fd, &int32, sizeof(int));
                    120: }
                    121:
                    122: /* int32 型のオブジェクトを受信する.  */
                    123: int receive_int32(int fd)
                    124: {
                    125:     int tag;
                    126:     read(fd, &tag, sizeof(int));
                    127:     return ntohl(tag);
                    128: }
                    129:
                    130: /* (OX_tag, serial number) を受信する.  */
                    131: int receive_ox_tag(int fd)
                    132: {
                    133:     int serial;
                    134:     int tag = receive_int32(fd);
                    135:     current_received_serial = receive_serial_number(fd);
                    136:     return tag;
                    137: }
                    138:
                    139: /* (OX_tag, serial number) を送信する.   */
                    140: int send_ox_tag(int fd, int tag)
                    141: {
                    142:     send_int32(fd, tag);
                    143:     return send_int32(fd, next_serial());
                    144: }
                    145:
                    146:
                    147: /* CMO_LIST 関係の関数群 */
                    148: cell* new_cell(cmo* newcmo)
                    149: {
                    150:     cell* h = malloc(sizeof(cell));
                    151:     h->next = NULL;
                    152:     h->cmo  = newcmo;
                    153:     return h;
                    154: }
                    155:
1.2       ohara     156: cell* next_cell(cell* this)
                    157: {
                    158:     return this->next;
                    159: }
                    160:
1.1       ohara     161: static cell* *tailp(cmo_list* this) {
                    162:     cell **cp = &this->head;
                    163:     while (*cp != NULL) {
                    164:         cp = &((*cp)->next);
                    165:     }
                    166:     return cp;
                    167: }
                    168:
                    169: int length_cmo_list(cmo_list* this)
                    170: {
                    171:     return this->length;
                    172: }
                    173:
                    174: int append_cmo_list(cmo_list* this, cmo* newcmo)
                    175: {
                    176:     /* リストの最後尾のNULLを保持している変数へのポインタ */
                    177:     cell **cp = tailp(this);
                    178:     *cp = new_cell(newcmo);
                    179:     this->length++;
                    180:     return 0;
                    181: }
                    182:
                    183: /** receive_cmo_XXX 関数群 **/
1.2       ohara     184: static cmo_null* receive_cmo_null(int fd)
1.1       ohara     185: {
1.2       ohara     186:     return new_cmo_null();
1.1       ohara     187: }
                    188:
                    189: static cmo_int32* receive_cmo_int32(int fd)
                    190: {
                    191:     int i = receive_int32(fd);
                    192:     return new_cmo_int32(i);
                    193: }
                    194:
1.2       ohara     195: static cmo_string* receive_cmo_string(int fd)
                    196: {
                    197:     int len = receive_int32(fd);
                    198:     char* s = malloc(len+1);
                    199:     memset(s, '\0', len+1);
                    200:     if (len > 0) {
                    201:         read(fd, s, len);
                    202:     }
                    203:     return new_cmo_string(s);
                    204: }
                    205:
                    206: static cmo_mathcap* receive_cmo_mathcap(int fd)
                    207: {
                    208:     cmo* ob = receive_cmo(fd);
                    209:     return new_cmo_mathcap(ob);
                    210: }
                    211:
1.1       ohara     212: static cmo_list* receive_cmo_list(int fd)
                    213: {
1.7     ! ohara     214:     cmo* ob;
1.1       ohara     215:     cmo_list* c = new_cmo_list();
                    216:     int len = receive_int32(fd);
                    217:
                    218:     while (len>0) {
1.7     ! ohara     219:         ob = receive_cmo(fd);
        !           220:         append_cmo_list(c, ob);
1.1       ohara     221:         len--;
                    222:     }
                    223:     return c;
                    224: }
                    225:
1.7     ! ohara     226: static cmo_monomial32* receive_cmo_monomial32(int fd)
        !           227: {
        !           228:        int i;
        !           229:        int len = receive_int32(fd);
        !           230:     cmo_monomial32* c = new_cmo_monomial32(len);
        !           231:
        !           232:        for(i=0; i<len; i++) {
        !           233:                c->exps[i] = receive_int32(fd);
        !           234:        }
        !           235:        c->coef = receive_cmo(fd);
        !           236:     return c;
        !           237: }
        !           238:
1.2       ohara     239: static cmo_zz* receive_cmo_zz(int fd)
                    240: {
                    241:     cmo_zz* c = new_cmo_zz();
                    242:     receive_mpz(fd, c->mpz);
                    243:     return c;
                    244: }
                    245:
                    246: static cmo_zero* receive_cmo_zero(int fd)
1.1       ohara     247: {
1.2       ohara     248:     return new_cmo_zero();
1.1       ohara     249: }
                    250:
1.2       ohara     251: static cmo_dms_generic* receive_cmo_dms_generic(int fd)
1.1       ohara     252: {
1.2       ohara     253:     return new_cmo_dms_generic();
1.1       ohara     254: }
                    255:
1.2       ohara     256: static cmo_ring_by_name* receive_cmo_ring_by_name(int fd)
1.1       ohara     257: {
1.2       ohara     258:     cmo* ob = receive_cmo(fd);
                    259:        /* 意味的チェックが必要 */
                    260:     return new_cmo_ring_by_name(ob);
1.1       ohara     261: }
                    262:
1.7     ! ohara     263: static cmo_distributed_polynomial* receive_cmo_distributed_polynomial(int fd)
        !           264: {
        !           265:     cmo* ob;
        !           266:     cmo_distributed_polynomial* c = new_cmo_distributed_polynomial();
        !           267:     int len = receive_int32(fd);
        !           268:        c->ringdef = receive_cmo(fd);
        !           269:
        !           270:     while (len>0) {
        !           271:         ob = receive_cmo(fd);
        !           272:         append_cmo_list(c, ob);
        !           273:         len--;
        !           274:     }
        !           275:     return c;
        !           276: }
        !           277:
1.2       ohara     278: static cmo_error2* receive_cmo_error2(int fd)
1.1       ohara     279: {
1.2       ohara     280:     cmo* ob = receive_cmo(fd);
                    281:     return new_cmo_error2(ob);
1.1       ohara     282: }
                    283:
1.7     ! ohara     284: /* receive_ox_tag() == OX_DATA の後に呼び出される */
        !           285: /* 関数ポインタを使った方がきれいに書けるような気がする.  */
        !           286: /* if (foo[tag] != NULL) foo[tag](fd); とか */
        !           287:
        !           288: cmo* receive_cmo(int fd)
        !           289: {
        !           290:     cmo* m;
        !           291:     int tag;
        !           292:     tag = receive_int32(fd);
        !           293:
        !           294:     switch(tag) {
        !           295:     case CMO_NULL:
        !           296:         m = receive_cmo_null(fd);
        !           297:         break;
        !           298:     case CMO_INT32:
        !           299:         m = (cmo *)receive_cmo_int32(fd);
        !           300:         break;
        !           301:     case CMO_STRING:
        !           302:         m = (cmo *)receive_cmo_string(fd);
        !           303:         break;
        !           304:     case CMO_MATHCAP:
        !           305:         m = (cmo *)receive_cmo_mathcap(fd);
        !           306:         break;
        !           307:     case CMO_LIST:
        !           308:         m = (cmo *)receive_cmo_list(fd);
        !           309:         break;
        !           310:     case CMO_MONOMIAL32:
        !           311:         m = (cmo *)receive_cmo_monomial32(fd);
        !           312:         break;
        !           313:     case CMO_ZZ:
        !           314:         m = (cmo *)receive_cmo_zz(fd);
        !           315:         break;
        !           316:     case CMO_ZERO:
        !           317:         m = (cmo *)receive_cmo_zero(fd);
        !           318:         break;
        !           319:     case CMO_DMS_GENERIC:
        !           320:                m = (cmo *)receive_cmo_dms_generic(fd);
        !           321:                break;
        !           322:        case CMO_RING_BY_NAME:
        !           323:                m = (cmo *)receive_cmo_ring_by_name(fd);
        !           324:                break;
        !           325:        case CMO_DISTRIBUTED_POLYNOMIAL:
        !           326:                m = (cmo *)receive_cmo_distributed_polynomial(fd);
        !           327:                break;
        !           328:     case CMO_ERROR2:
        !           329:         m = (cmo *)receive_cmo_error2(fd);
        !           330:         break;
        !           331:     case CMO_DATUM:
        !           332:     case CMO_QQ:
        !           333:     default:
        !           334:         fprintf(stderr, "unknown cmo-type: tag = (%d)\n", m->tag);
        !           335:     }
        !           336:     return m;
        !           337: }
        !           338:
1.1       ohara     339: static void receive_mpz(int fd, mpz_ptr mpz)
                    340: {
                    341:     int i;
                    342:     int size  = receive_int32(fd);
                    343:     int len   = abs(size);
                    344:     resize_mpz(mpz, size);
                    345:
                    346:     for(i=0; i<len; i++) {
                    347:         mpz->_mp_d[i] = receive_int32(fd);
                    348:     }
                    349: }
                    350:
                    351: void resize_mpz(mpz_ptr mpz, int size)
                    352: {
                    353:     _mpz_realloc(mpz, abs(size));
                    354:     mpz->_mp_size = size;
                    355: }
                    356:
                    357: /** new_cmo_XXX 関数群 **/
1.2       ohara     358: cmo_null* new_cmo_null()
1.1       ohara     359: {
1.2       ohara     360:     cmo_null* m = malloc(sizeof(cmo_null));
                    361:     m->tag = CMO_NULL;
                    362:     return m;
1.1       ohara     363: }
                    364:
                    365: cmo_int32* new_cmo_int32(int i)
                    366: {
                    367:     cmo_int32* c;
                    368:     c = malloc(sizeof(cmo_int32));
                    369:     c->tag     = CMO_INT32;
                    370:     c->i = i;
                    371:     return c;
                    372: }
                    373:
1.2       ohara     374: cmo_string* new_cmo_string(char* s)
                    375: {
                    376:     cmo_string* c = malloc(sizeof(cmo_string));
                    377:     c->tag = CMO_STRING;
1.6       ohara     378:        if (s != NULL) {
                    379:                c->s = malloc(strlen(s)+1);
                    380:                strcpy(c->s, s);
                    381:        }else {
                    382:                c->s = NULL;
                    383:        }
1.2       ohara     384:     return c;
                    385: }
                    386:
                    387: cmo_mathcap* new_cmo_mathcap(cmo* ob)
                    388: {
                    389:     cmo_mathcap* c = malloc(sizeof(cmo_mathcap));
                    390:     c->tag = CMO_MATHCAP;
                    391:     c->ob  = ob;
                    392:     return c;
                    393: }
                    394:
1.1       ohara     395: cmo_list* new_cmo_list()
                    396: {
                    397:     cmo_list* c = malloc(sizeof(cmo_list));
                    398:     c->tag    = CMO_LIST;
                    399:     c->length = 0;
                    400:     c->head   = NULL;
                    401:     return c;
                    402: }
                    403:
1.2       ohara     404: cmo_monomial32* new_cmo_monomial32()
1.1       ohara     405: {
1.2       ohara     406:     cmo_monomial32* c = malloc(sizeof(cmo_monomial32));
                    407:     c->tag  = CMO_MONOMIAL32;
1.1       ohara     408:     return c;
                    409: }
                    410:
1.2       ohara     411: cmo_monomial32* new_cmo_monomial32_size(int size)
1.1       ohara     412: {
1.2       ohara     413:     cmo_monomial32* c = new_cmo_monomial32();
                    414:        if (size>0) {
                    415:                c->length = size;
                    416:                c->exps = malloc(sizeof(int)*size);
                    417:        }
1.1       ohara     418:     return c;
                    419: }
                    420:
                    421: cmo_zz* new_cmo_zz()
                    422: {
                    423:     cmo_zz* c = malloc(sizeof(cmo_zz));
                    424:     c->tag  = CMO_ZZ;
                    425:     mpz_init(c->mpz);
                    426:     return c;
                    427: }
                    428:
                    429: cmo_zz* new_cmo_zz_noinit()
                    430: {
                    431:     cmo_zz* c = malloc(sizeof(cmo_zz));
                    432:     c->tag  = CMO_ZZ;
                    433:     return c;
                    434: }
                    435:
                    436: cmo_zz* new_cmo_zz_set_si(int i)
                    437: {
                    438:     cmo_zz* c = new_cmo_zz();
                    439:     mpz_set_si(c->mpz, i);
                    440:     return c;
                    441: }
                    442:
1.5       ohara     443: cmo_zz *new_cmo_zz_set_string(char *s)
                    444: {
                    445:     cmo_zz* c = new_cmo_zz_noinit();
                    446:     mpz_init_set_str(c->mpz, s, 10);
                    447:     return c;
                    448: }
                    449:
1.1       ohara     450: cmo_zz* new_cmo_zz_size(int size)
                    451: {
                    452:     cmo_zz* c = new_cmo_zz();
                    453:     resize_mpz(c->mpz, size);
                    454:     return c;
                    455: }
                    456:
1.2       ohara     457: cmo_zero* new_cmo_zero()
                    458: {
                    459:     cmo_zero* m = malloc(sizeof(cmo_zero));
                    460:     m->tag = CMO_ZERO;
                    461:     return m;
                    462: }
                    463:
                    464: cmo_dms_generic* new_cmo_dms_generic()
                    465: {
                    466:     cmo_dms_generic* m = malloc(sizeof(cmo_dms_generic));
                    467:     m->tag = CMO_DMS_GENERIC;
                    468:     return m;
                    469: }
                    470:
                    471: cmo_ring_by_name* new_cmo_ring_by_name(cmo* ob)
                    472: {
                    473:     cmo_ring_by_name* c = malloc(sizeof(cmo_ring_by_name));
                    474:     c->tag = CMO_RING_BY_NAME;
                    475:     c->ob  = ob;
                    476:     return c;
                    477: }
                    478:
                    479: cmo_indeterminate* new_cmo_indeterminate(cmo* ob)
                    480: {
                    481:     cmo_indeterminate* c = malloc(sizeof(cmo_indeterminate));
                    482:     c->tag = CMO_INDETERMINATE;
                    483:     c->ob  = ob;
                    484:     return c;
                    485: }
                    486:
1.3       ohara     487: cmo_distributed_polynomial* new_cmo_distributed_polynomial()
                    488: {
                    489:     cmo_distributed_polynomial* c = malloc(sizeof(cmo_distributed_polynomial));
                    490:     c->tag     = CMO_DISTRIBUTED_POLYNOMIAL;
                    491:     c->length  = 0;
                    492:     c->head    = NULL;
                    493:        c->ringdef = NULL;
                    494:     return c;
                    495: }
                    496:
1.2       ohara     497: cmo_error2* new_cmo_error2(cmo* ob)
                    498: {
                    499:     cmo_error2* c = malloc(sizeof(cmo_error2));
                    500:     c->tag = CMO_ERROR2;
                    501:     c->ob  = ob;
                    502:     return c;
                    503: }
                    504:
1.1       ohara     505: void send_ox_command(int fd, int sm_command)
                    506: {
                    507:     send_ox_tag(fd, OX_COMMAND);
                    508:     send_int32(fd, sm_command);
                    509: }
                    510:
                    511: int print_cmo(cmo* c)
                    512: {
                    513:     int tag = c->tag;
                    514:     fprintf(stderr, "local::tag = (%d): ", tag);
                    515:     switch(tag) {
                    516:     case CMO_LIST:
                    517:         print_cmo_list((cmo_list *)c);
                    518:         break;
                    519:     case CMO_INT32:
                    520:         print_cmo_int32((cmo_int32 *)c);
                    521:         break;
                    522:     case CMO_MATHCAP:
                    523:         print_cmo_mathcap((cmo_mathcap *)c);
                    524:         break;
                    525:     case CMO_STRING:
                    526:         print_cmo_string((cmo_string *)c);
                    527:         break;
                    528:     case CMO_NULL:
                    529:         fprintf(stderr, "\n");
                    530:         break;
                    531:     default:
                    532:         fprintf(stderr, "print_cmo() does not know how to print.\n");
                    533:     }
                    534: }
                    535:
                    536: int print_cmo_int32(cmo_int32* c)
                    537: {
                    538:     fprintf(stderr, "cmo_int32 = (%d)\n", c->i);
                    539: }
                    540:
                    541: int print_cmo_list(cmo_list* li)
                    542: {
                    543:     cell* cp = li->head;
                    544:     fprintf(stderr, "length = (%d)\nlist:\n", li->length);
                    545:     while(cp != NULL) {
                    546:         print_cmo(cp->cmo);
                    547:         cp=cp->next;
                    548:     }
                    549:     fprintf(stderr, "end of list\n");
                    550: }
                    551:
                    552: int print_cmo_mathcap(cmo_mathcap* c)
                    553: {
                    554:     fprintf(stderr, "\n");
1.2       ohara     555:     print_cmo(c->ob);
1.1       ohara     556: }
                    557:
                    558: int print_cmo_string(cmo_string* c)
                    559: {
                    560:     fprintf(stderr, "cmo_string = (%s)\n", c->s);
                    561: }
                    562:
                    563: void ox_close(ox_file_t sv)
                    564: {
                    565:     send_ox_command(sv->control, SM_control_kill);
                    566: #if DEBUG
                    567:     sleep(2); /* OpenXM server の終了を待つ. あまり意味はない. */
                    568:     fprintf(stderr, "I have closed an Open XM server.\n");
                    569: #endif
                    570: }
                    571:
                    572: void ox_executeStringByLocalParser(ox_file_t sv, char* s)
                    573: {
                    574:     /* 文字列ををスタックにプッシュ. */
                    575:     send_ox_cmo(sv->stream, (cmo *)new_cmo_string(s));
                    576:
                    577:     /* サーバに実行させる. */
                    578:     send_ox_command(sv->stream, SM_executeStringByLocalParser);
                    579: }
                    580:
                    581: /* ox_mathcap() をコールする.  */
                    582: cmo_mathcap* ox_mathcap(ox_file_t sv)
                    583: {
                    584:     send_ox_command(sv->stream, SM_mathcap);
                    585:     send_ox_command(sv->stream, SM_popCMO);
                    586:     receive_ox_tag(sv->stream);          /* OX_DATA */
                    587:     return (cmo_mathcap *)receive_cmo(sv->stream);
                    588: }
                    589:
                    590: char* ox_popString(ox_file_t sv, int fd)
                    591: {
                    592:     cmo_string* m = NULL;
                    593:
                    594:     send_ox_command(fd, SM_popString);
                    595:     receive_ox_tag(fd); /* OX_DATA */
                    596:     m = (cmo_string *)receive_cmo(fd);
                    597:     return m->s;
                    598: }
                    599:
                    600: cmo* ox_pop_cmo(ox_file_t sv, int fd)
                    601: {
                    602:     send_ox_command(fd, SM_popCMO);
                    603:     receive_ox_tag(fd); /* OX_DATA */
                    604:     return receive_cmo(fd);
                    605: }
                    606:
1.5       ohara     607: /* 手抜き. (後で改善しよう...) */
                    608: static char *create_otp()
                    609: {
                    610:        static char otp[] = "otpasswd";
                    611:        return otp;
                    612: }
                    613:
                    614: /* OneTimePassword の処理 */
                    615: static int login_with_otp(int fd, char* passwd)
                    616: {
                    617:     char buff[1024];
                    618:     int n = read(fd, buff, 1024);
                    619:        int len = strlen(passwd)+1;
                    620:     if (n != len) {
                    621:         fprintf(stderr, "Socket#%d: Login incorrect.\n", fd);
                    622:         fprintf(stderr, "password = (%s), length = (%d).\n", passwd, len);
                    623:         fprintf(stderr, "received = (%d), length = (%d).\n", buff, n);
                    624:                fflush(stderr);
                    625:     }
                    626: }
                    627:
1.1       ohara     628: /*
1.5       ohara     629:    (-reverse 版の ox_start)
1.1       ohara     630:    ox_start は クライアントが呼び出すための関数である.
                    631:    サーバでは使われない.  prog1 は コントロールサーバであり,
                    632:    -ox, -reverse, -data, -control, -pass, -host
                    633:    というオプションを理解することを仮定する. prog2 は計算サーバである.
                    634:    接続時には, sv->control を先にオープンする.
                    635: */
                    636:
                    637: ox_file_t ox_start(char* host, char* prog1, char* prog2)
                    638: {
1.5       ohara     639:        char *pass = create_otp();
1.1       ohara     640:     char ctl[16], dat[16];
                    641:     short portControl = 0; /* short であることに注意 */
                    642:     short portStream  = 0;
                    643:     ox_file_t sv = malloc(sizeof(__ox_file_struct));
                    644:
                    645:     sv->control = mysocketListen(host, &portControl);
                    646:     sv->stream  = mysocketListen(host, &portStream);
                    647:     sprintf(ctl, "%d", portControl);
                    648:     sprintf(dat, "%d", portStream);
                    649:
                    650:     if (fork() == 0) {
                    651:         dup2(2, 1);
                    652:         dup2(open(DEFAULT_LOGFILE, O_RDWR|O_CREAT|O_TRUNC, 0644), 2);
                    653:         execl(prog1, prog1, "-reverse", "-ox", prog2,
                    654:               "-data", dat, "-control", ctl, "-pass", pass,
                    655:               "-host", host, NULL);
                    656:     }
                    657:
                    658:     sv->control = mysocketAccept(sv->control);
1.5       ohara     659:        login_with_otp(sv->control, pass);
                    660:     decideByteOrderClient(sv->control, 0);
                    661:        /* 10マイクロ秒, 時間稼ぎする. */
1.1       ohara     662:     usleep(10);
                    663:     sv->stream  = mysocketAccept(sv->stream);
1.5       ohara     664:        login_with_otp(sv->stream, pass);
                    665:     decideByteOrderClient(sv->stream, 0);
1.1       ohara     666:
                    667:     return sv;
                    668: }
                    669:
1.5       ohara     670: /*
                    671:    (-insecure 版の ox_start)  まだ、中身はありません。
                    672:    ox_start_insecure_nonreverse は クライアントが呼び出すための関数である.
                    673:    接続時には, sv->control を先にオープンする.
                    674:    既定値:
                    675:    portControl = 1200
                    676:    portStream  = 1300
                    677: */
                    678:
                    679: ox_file_t ox_start_insecure_nonreverse(char* host, short portControl, short portStream)
                    680: {
                    681:     ox_file_t sv = malloc(sizeof(__ox_file_struct));
                    682:
                    683:        sv->control = mysocketOpen(host, portControl);
                    684:        /* 10マイクロ秒, 時間稼ぎする. */
                    685:        usleep(10);
                    686:        sv->stream  = mysocketOpen(host, portStream);
                    687:        return sv;
                    688: }
                    689:
1.1       ohara     690: void ox_reset(ox_file_t sv)
                    691: {
                    692:     send_ox_command(sv->control, SM_control_reset_connection);
                    693:
                    694:     receive_ox_tag(sv->control);      /* OX_DATA */
                    695:     receive_cmo(sv->control);         /* (CMO_INT32, 0) */
                    696:
                    697:     while(receive_ox_tag(sv->stream) != OX_SYNC_BALL) {
                    698:         receive_cmo(sv->stream); /* skipping a message. */
                    699:     }
                    700:
                    701:     send_ox_tag(sv->stream, OX_SYNC_BALL);
                    702: #if DEBUG
                    703:     fprintf(stderr, "I have reset an Open XM server.\n");
                    704: #endif
                    705: }
                    706:
1.2       ohara     707: /* 以下は bconv.c で必要とする関数群である. */
                    708:
                    709: /* cmolen 関数は cmo の(送信時の)バイト長を返す. */
                    710: /* cmolen_XXX 関数は cmo_XXX の tag を除いたバイト長を返す. */
                    711:
                    712: static int cmolen_cmo_null(cmo_null* c)
                    713: {
                    714:     return 0;
                    715: }
1.1       ohara     716:
                    717: static int cmolen_cmo_int32(cmo_int32* c)
                    718: {
                    719:     return sizeof(int);
                    720: }
                    721:
1.2       ohara     722: static int cmolen_cmo_string(cmo_string* c)
                    723: {
                    724:     return sizeof(int)+strlen(c->s);
                    725: }
                    726:
                    727: static int cmolen_cmo_mathcap(cmo_mathcap* c)
                    728: {
                    729:     return cmolen_cmo(c->ob);
                    730: }
                    731:
1.1       ohara     732: static int cmolen_cmo_list(cmo_list* c)
                    733: {
                    734:     int size = sizeof(c->head);
                    735:     cell* cp = c->head;
                    736:
                    737:     while(cp != NULL) {
                    738:         size += cmolen_cmo(cp->cmo);
                    739:         cp = cp->next;
                    740:     }
                    741:     return size;
                    742: }
                    743:
1.2       ohara     744: static int cmolen_cmo_monomial32(cmo_monomial32* c)
1.1       ohara     745: {
1.2       ohara     746:        int len = (c->length + 1)*sizeof(int);
                    747:        return len + cmolen_cmo(c->coef);
1.1       ohara     748: }
                    749:
                    750: static int cmolen_cmo_zz(cmo_zz* c)
                    751: {
                    752:     int len = abs(c->mpz->_mp_size);
                    753:     return sizeof(len) + len*sizeof(int);
                    754: }
                    755:
1.3       ohara     756: static int cmolen_cmo_distributed_polynomial(cmo_distributed_polynomial* c)
                    757: {
                    758:        return cmolen_cmo_list((cmo_list *)c) + cmolen_cmo(c->ringdef);
                    759: }
                    760:
1.1       ohara     761: /* CMO がバイトエンコードされた場合のバイト列の長さを求める */
                    762: int cmolen_cmo(cmo* c)
                    763: {
                    764:     int size = sizeof(int);
                    765:
                    766:     switch(c->tag) {
                    767:     case CMO_NULL:
1.2       ohara     768:     case CMO_ZERO:
                    769:     case CMO_DMS_GENERIC:
1.1       ohara     770:         size += cmolen_cmo_null(c);
                    771:         break;
                    772:     case CMO_INT32:
                    773:         size += cmolen_cmo_int32((cmo_int32 *)c);
                    774:         break;
                    775:     case CMO_STRING:
                    776:         size += cmolen_cmo_string((cmo_string *)c);
                    777:         break;
1.2       ohara     778:     case CMO_MATHCAP:
                    779:     case CMO_RING_BY_NAME:
                    780:     case CMO_ERROR2:
                    781:         size += cmolen_cmo_mathcap((cmo_mathcap *)c);
                    782:         break;
1.1       ohara     783:     case CMO_LIST:
                    784:         size += cmolen_cmo_list((cmo_list *)c);
                    785:         break;
1.2       ohara     786:     case CMO_MONOMIAL32:
                    787:         size += cmolen_cmo_monomial32((cmo_monomial32 *)c);
                    788:         break;
1.1       ohara     789:     case CMO_ZZ:
                    790:         size += cmolen_cmo_zz((cmo_zz *)c);
                    791:         break;
1.3       ohara     792:        case CMO_DISTRIBUTED_POLYNOMIAL:
                    793:                size += cmolen_cmo_distributed_polynomial((cmo_distributed_polynomial *)c);
                    794:                break;
1.1       ohara     795:     default:
                    796:     }
                    797:     return size;
                    798: }
                    799:
1.4       ohara     800: static int  d_ptr;
                    801: static char *d_buf;
                    802:
                    803: int init_dump_buffer(char *s)
1.2       ohara     804: {
1.4       ohara     805:        d_buf = s;
                    806:        d_ptr = 0;
1.2       ohara     807: }
                    808:
1.4       ohara     809: static int dump_cmo_null(cmo_null* m)
1.1       ohara     810: {
1.4       ohara     811:        return 0;
1.1       ohara     812: }
                    813:
1.4       ohara     814: static int dump_cmo_int32(cmo_int32* m)
                    815: {
                    816:     dump_integer(m->i);
                    817: }
                    818:
                    819: static int dump_cmo_string(cmo_string* m)
1.2       ohara     820: {
                    821:     int len = strlen(m->s);
1.4       ohara     822:     dump_integer(len);
                    823:        dump_string(m->s, len);
1.2       ohara     824: }
                    825:
1.4       ohara     826: static int dump_cmo_mathcap(cmo_mathcap* c)
1.2       ohara     827: {
1.4       ohara     828:     dump_cmo(c->ob);
1.2       ohara     829: }
                    830:
1.4       ohara     831: static int dump_cmo_list(cmo_list* m)
1.1       ohara     832: {
                    833:     cell* cp = m->head;
                    834:     int len = length_cmo_list(m);
1.4       ohara     835:     dump_integer(len);
1.1       ohara     836:
                    837:     while(cp != NULL) {
1.4       ohara     838:         dump_cmo(cp->cmo);
1.1       ohara     839:         cp = cp->next;
                    840:     }
                    841: }
                    842:
1.4       ohara     843: static int dump_cmo_monomial32(cmo_monomial32* c)
1.1       ohara     844: {
1.2       ohara     845:        int i;
                    846:        int length = c->length;
1.4       ohara     847:        dump_integer(c->length);
1.2       ohara     848:        for(i=0; i<length; i++) {
1.4       ohara     849:                dump_integer(c->exps[i]);
1.2       ohara     850:        }
1.4       ohara     851:        dump_cmo(c->coef);
1.1       ohara     852: }
                    853:
1.4       ohara     854: static int dump_cmo_zz(cmo_zz* c)
1.1       ohara     855: {
1.4       ohara     856:     dump_mpz(c->mpz);
1.1       ohara     857: }
                    858:
1.4       ohara     859: static int dump_cmo_distributed_polynomial(cmo_distributed_polynomial* m)
1.3       ohara     860: {
                    861:     cell* cp = m->head;
1.4       ohara     862:     int len = length_cmo_list((cmo_list *)m);
                    863:     dump_integer(len);
                    864:        dump_cmo(m->ringdef);
1.3       ohara     865:     while(cp != NULL) {
1.4       ohara     866:         dump_cmo(cp->cmo);
1.3       ohara     867:         cp = cp->next;
                    868:     }
                    869: }
                    870:
1.1       ohara     871: /* タグを書き出してから、各関数を呼び出す */
1.4       ohara     872: int dump_cmo(cmo* m)
1.1       ohara     873: {
1.4       ohara     874:     dump_integer(m->tag);
1.1       ohara     875:     switch(m->tag) {
                    876:     case CMO_NULL:
1.2       ohara     877:     case CMO_ZERO:
                    878:     case CMO_DMS_GENERIC:
1.4       ohara     879:         dump_cmo_null(m);
                    880:                break;
1.1       ohara     881:     case CMO_INT32:
1.4       ohara     882:         dump_cmo_int32((cmo_int32 *)m);
                    883:                break;
1.1       ohara     884:     case CMO_STRING:
1.4       ohara     885:         dump_cmo_string((cmo_string *)m);
                    886:                break;
1.2       ohara     887:     case CMO_MATHCAP:
                    888:     case CMO_RING_BY_NAME:
                    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:
                   1004:     while(cp != NULL) {
                   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:
        !          1018:     while(cp != NULL) {
        !          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: }
                   1183: #if 0
                   1184: cmo* (*received_funcs[])(int fd) = {
                   1185:     NULL,  /* gate keeper */
                   1186:     receive_cmo_null,
                   1187:     receive_cmo_int32,
                   1188:     receive_cmo_string,
                   1189:     receive_cmo_mathcap,
                   1190:     receive_cmo_list,
                   1191:     receive_cmo_zz,
1.2       ohara    1192:     receive_cmo_error2
1.1       ohara    1193: };
                   1194:
                   1195: cmo* receive_cmo2(int fd)
                   1196: {
                   1197:     int tag;
                   1198:     cmo* (*foo)() = received_funcs[funcs(tag)];
                   1199:     if (foo != NULL) {
                   1200:         return foo(fd);
                   1201:     }
                   1202: }
1.3       ohara    1203: #endif
                   1204:
                   1205: /* ファイルディスクリプタ fd の通信路での integer の byte order を決定する */
                   1206: /* 実際には order (0,1,or 0xFF)をみてはいない */
1.5       ohara    1207: int decideByteOrderClient(oxfd fd, int order)
1.1       ohara    1208: {
1.3       ohara    1209:     char zero = OX_BYTE_NETWORK_BYTE_ORDER;
1.1       ohara    1210:     char dest;
1.5       ohara    1211:     read(fd, &dest, sizeof(char));
                   1212:     write(fd, &zero, sizeof(char));
1.1       ohara    1213:     return 0;
                   1214: }
                   1215:
                   1216: /* Server 側ではこちらを用いる */
1.5       ohara    1217: /* いまの実装は dup されていることが前提になっている */
                   1218: int decideByteOrderServer(oxfd fd, int order)
1.1       ohara    1219: {
1.3       ohara    1220:     char zero = OX_BYTE_NETWORK_BYTE_ORDER;
1.1       ohara    1221:     char dest;
1.5       ohara    1222:     write(fd, &zero, sizeof(char));
                   1223:     read(fd, &dest, sizeof(char));
1.1       ohara    1224:     return 0;
                   1225: }
                   1226:
1.5       ohara    1227: /* cmo と string (ここではC言語のstring) の変換関数群 */
                   1228: char *convert_zz_to_string(cmo_zz *c)
1.1       ohara    1229: {
                   1230:     return mpz_get_str(NULL, 10, c->mpz);
                   1231: }
                   1232:
1.5       ohara    1233: char *convert_cmo_to_string(cmo *m)
1.1       ohara    1234: {
                   1235:     switch(m->tag) {
                   1236:     case CMO_ZZ:
1.5       ohara    1237:         return convert_zz_to_string((cmo_zz *)m);
1.1       ohara    1238:     case CMO_INT32:
1.5       ohara    1239:         return convert_int_to_string(((cmo_int32 *)m)->i);
1.1       ohara    1240:     case CMO_STRING:
                   1241:         return ((cmo_string *)m)->s;
                   1242:     case CMO_NULL:
1.5       ohara    1243:         return convert_null_to_string();
1.1       ohara    1244:     default:
                   1245:         fprintf(stderr, "sorry, not implemented CMO\n");
                   1246:         /* まだ実装していません. */
                   1247:         return NULL;
                   1248:     }
                   1249: }
                   1250:
1.5       ohara    1251: char *convert_null_to_string()
1.1       ohara    1252: {
                   1253:     static char* null_string = "";
                   1254:     return null_string;
                   1255: }
                   1256:
1.5       ohara    1257: char *convert_int_to_string(int integer)
1.1       ohara    1258: {
                   1259:     char buff[1024];
                   1260:     char *s;
                   1261:
                   1262:     sprintf(buff, "%d", integer);
                   1263:     s = malloc(strlen(buff)+1);
                   1264:     strcpy(s, buff);
                   1265:
                   1266:     return s;
                   1267: }

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