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

1.1       ohara       1: /* -*- mode: C; coding: euc-japan -*- */
1.10    ! ohara       2: /* $OpenXM: OpenXM/src/ox_math/ox.c,v 1.9 1999/11/04 19:33:17 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 len = strlen(passwd)+1;
1.10    ! ohara     617:     int n = read(fd, buff, len);
        !           618:        if (strcmp(passwd, buff) != 0) {
1.5       ohara     619:         fprintf(stderr, "Socket#%d: Login incorrect.\n", fd);
1.10    ! ohara     620:         fprintf(stderr, "password = (%s), %d bytes.\n", passwd, len);
        !           621:         fprintf(stderr, "received = (%s), %d bytes.\n", buff, n);
1.5       ohara     622:                fflush(stderr);
1.10    ! ohara     623:                exit(1);
1.5       ohara     624:     }
1.10    ! ohara     625: #ifdef DEBUG
        !           626:        fprintf(stderr, "Socket#%d: login!.\n", fd);
        !           627:        fprintf(stderr, "password = (%s), %d bytes.\n", passwd, len);
        !           628:        fprintf(stderr, "received = (%s), %d bytes.\n", buff, n);
        !           629:        fflush(stderr);
        !           630: #endif
1.5       ohara     631: }
                    632:
1.1       ohara     633: /*
1.5       ohara     634:    (-reverse 版の ox_start)
1.1       ohara     635:    ox_start は クライアントが呼び出すための関数である.
1.10    ! ohara     636:    サーバでは使われない.  ctl_prog は コントロールサーバであり,
1.1       ohara     637:    -ox, -reverse, -data, -control, -pass, -host
1.10    ! ohara     638:    というオプションを理解することを仮定する. dat_prog は計算サーバである.
1.1       ohara     639:    接続時には, sv->control を先にオープンする.
                    640: */
                    641:
1.10    ! ohara     642: ox_file_t ox_start(char* host, char* ctl_prog, char* dat_prog)
1.1       ohara     643: {
1.5       ohara     644:        char *pass = create_otp();
1.1       ohara     645:     char ctl[16], dat[16];
                    646:     short portControl = 0; /* short であることに注意 */
                    647:     short portStream  = 0;
                    648:     ox_file_t sv = malloc(sizeof(__ox_file_struct));
                    649:
                    650:     sv->control = mysocketListen(host, &portControl);
                    651:     sv->stream  = mysocketListen(host, &portStream);
                    652:     sprintf(ctl, "%d", portControl);
                    653:     sprintf(dat, "%d", portStream);
                    654:
                    655:     if (fork() == 0) {
                    656:         dup2(2, 1);
                    657:         dup2(open(DEFAULT_LOGFILE, O_RDWR|O_CREAT|O_TRUNC, 0644), 2);
1.10    ! ohara     658:         execl(ctl_prog, ctl_prog, "-reverse", "-ox", dat_prog,
1.1       ohara     659:               "-data", dat, "-control", ctl, "-pass", pass,
                    660:               "-host", host, NULL);
                    661:     }
                    662:
                    663:     sv->control = mysocketAccept(sv->control);
1.5       ohara     664:        login_with_otp(sv->control, pass);
                    665:     decideByteOrderClient(sv->control, 0);
                    666:        /* 10マイクロ秒, 時間稼ぎする. */
1.1       ohara     667:     usleep(10);
                    668:     sv->stream  = mysocketAccept(sv->stream);
1.5       ohara     669:        login_with_otp(sv->stream, pass);
                    670:     decideByteOrderClient(sv->stream, 0);
1.1       ohara     671:
                    672:     return sv;
                    673: }
                    674:
1.5       ohara     675: /*
                    676:    (-insecure 版の ox_start)  まだ、中身はありません。
                    677:    ox_start_insecure_nonreverse は クライアントが呼び出すための関数である.
                    678:    接続時には, sv->control を先にオープンする.
                    679:    既定値:
                    680:    portControl = 1200
                    681:    portStream  = 1300
                    682: */
                    683:
                    684: ox_file_t ox_start_insecure_nonreverse(char* host, short portControl, short portStream)
                    685: {
                    686:     ox_file_t sv = malloc(sizeof(__ox_file_struct));
                    687:
                    688:        sv->control = mysocketOpen(host, portControl);
1.9       ohara     689: #if 0
                    690:        /* ox は insecure のとき byte order の決定が正しくできないようだ... */
                    691:     decideByteOrderClient(sv->control, 0);
                    692: #endif
1.5       ohara     693:        /* 10マイクロ秒, 時間稼ぎする. */
                    694:        usleep(10);
                    695:        sv->stream  = mysocketOpen(host, portStream);
1.9       ohara     696:     decideByteOrderClient(sv->stream, 0);
1.5       ohara     697:        return sv;
                    698: }
                    699:
1.1       ohara     700: void ox_reset(ox_file_t sv)
                    701: {
                    702:     send_ox_command(sv->control, SM_control_reset_connection);
                    703:
                    704:     receive_ox_tag(sv->control);      /* OX_DATA */
                    705:     receive_cmo(sv->control);         /* (CMO_INT32, 0) */
                    706:
                    707:     while(receive_ox_tag(sv->stream) != OX_SYNC_BALL) {
                    708:         receive_cmo(sv->stream); /* skipping a message. */
                    709:     }
                    710:
                    711:     send_ox_tag(sv->stream, OX_SYNC_BALL);
                    712: #if DEBUG
                    713:     fprintf(stderr, "I have reset an Open XM server.\n");
                    714: #endif
                    715: }
                    716:
1.2       ohara     717: /* 以下は bconv.c で必要とする関数群である. */
                    718:
                    719: /* cmolen 関数は cmo の(送信時の)バイト長を返す. */
                    720: /* cmolen_XXX 関数は cmo_XXX の tag を除いたバイト長を返す. */
                    721:
                    722: static int cmolen_cmo_null(cmo_null* c)
                    723: {
                    724:     return 0;
                    725: }
1.1       ohara     726:
                    727: static int cmolen_cmo_int32(cmo_int32* c)
                    728: {
                    729:     return sizeof(int);
                    730: }
                    731:
1.2       ohara     732: static int cmolen_cmo_string(cmo_string* c)
                    733: {
                    734:     return sizeof(int)+strlen(c->s);
                    735: }
                    736:
                    737: static int cmolen_cmo_mathcap(cmo_mathcap* c)
                    738: {
                    739:     return cmolen_cmo(c->ob);
                    740: }
                    741:
1.1       ohara     742: static int cmolen_cmo_list(cmo_list* c)
                    743: {
1.8       ohara     744:     int size = sizeof(int);
1.1       ohara     745:     cell* cp = c->head;
                    746:
1.8       ohara     747:     while(cp->next != NULL) {
1.1       ohara     748:         size += cmolen_cmo(cp->cmo);
                    749:         cp = cp->next;
                    750:     }
                    751:     return size;
                    752: }
                    753:
1.2       ohara     754: static int cmolen_cmo_monomial32(cmo_monomial32* c)
1.1       ohara     755: {
1.2       ohara     756:        int len = (c->length + 1)*sizeof(int);
                    757:        return len + cmolen_cmo(c->coef);
1.1       ohara     758: }
                    759:
                    760: static int cmolen_cmo_zz(cmo_zz* c)
                    761: {
                    762:     int len = abs(c->mpz->_mp_size);
                    763:     return sizeof(len) + len*sizeof(int);
                    764: }
                    765:
1.3       ohara     766: static int cmolen_cmo_distributed_polynomial(cmo_distributed_polynomial* c)
                    767: {
                    768:        return cmolen_cmo_list((cmo_list *)c) + cmolen_cmo(c->ringdef);
                    769: }
                    770:
1.1       ohara     771: /* CMO がバイトエンコードされた場合のバイト列の長さを求める */
                    772: int cmolen_cmo(cmo* c)
                    773: {
                    774:     int size = sizeof(int);
                    775:
                    776:     switch(c->tag) {
                    777:     case CMO_NULL:
1.2       ohara     778:     case CMO_ZERO:
                    779:     case CMO_DMS_GENERIC:
1.1       ohara     780:         size += cmolen_cmo_null(c);
                    781:         break;
                    782:     case CMO_INT32:
                    783:         size += cmolen_cmo_int32((cmo_int32 *)c);
                    784:         break;
                    785:     case CMO_STRING:
                    786:         size += cmolen_cmo_string((cmo_string *)c);
                    787:         break;
1.2       ohara     788:     case CMO_MATHCAP:
                    789:     case CMO_RING_BY_NAME:
1.8       ohara     790:     case CMO_INDETERMINATE:
1.2       ohara     791:     case CMO_ERROR2:
                    792:         size += cmolen_cmo_mathcap((cmo_mathcap *)c);
                    793:         break;
1.1       ohara     794:     case CMO_LIST:
                    795:         size += cmolen_cmo_list((cmo_list *)c);
                    796:         break;
1.2       ohara     797:     case CMO_MONOMIAL32:
                    798:         size += cmolen_cmo_monomial32((cmo_monomial32 *)c);
                    799:         break;
1.1       ohara     800:     case CMO_ZZ:
                    801:         size += cmolen_cmo_zz((cmo_zz *)c);
                    802:         break;
1.3       ohara     803:        case CMO_DISTRIBUTED_POLYNOMIAL:
                    804:                size += cmolen_cmo_distributed_polynomial((cmo_distributed_polynomial *)c);
                    805:                break;
1.1       ohara     806:     default:
                    807:     }
                    808:     return size;
                    809: }
                    810:
1.4       ohara     811: static int  d_ptr;
                    812: static char *d_buf;
                    813:
                    814: int init_dump_buffer(char *s)
1.2       ohara     815: {
1.4       ohara     816:        d_buf = s;
                    817:        d_ptr = 0;
1.2       ohara     818: }
                    819:
1.4       ohara     820: static int dump_cmo_null(cmo_null* m)
1.1       ohara     821: {
1.4       ohara     822:        return 0;
1.1       ohara     823: }
                    824:
1.4       ohara     825: static int dump_cmo_int32(cmo_int32* m)
                    826: {
                    827:     dump_integer(m->i);
                    828: }
                    829:
                    830: static int dump_cmo_string(cmo_string* m)
1.2       ohara     831: {
                    832:     int len = strlen(m->s);
1.4       ohara     833:     dump_integer(len);
                    834:        dump_string(m->s, len);
1.2       ohara     835: }
                    836:
1.4       ohara     837: static int dump_cmo_mathcap(cmo_mathcap* c)
1.2       ohara     838: {
1.4       ohara     839:     dump_cmo(c->ob);
1.2       ohara     840: }
                    841:
1.4       ohara     842: static int dump_cmo_list(cmo_list* m)
1.1       ohara     843: {
                    844:     cell* cp = m->head;
                    845:     int len = length_cmo_list(m);
1.4       ohara     846:     dump_integer(len);
1.1       ohara     847:
1.8       ohara     848:     while(cp->next != NULL) {
1.4       ohara     849:         dump_cmo(cp->cmo);
1.1       ohara     850:         cp = cp->next;
                    851:     }
                    852: }
                    853:
1.4       ohara     854: static int dump_cmo_monomial32(cmo_monomial32* c)
1.1       ohara     855: {
1.2       ohara     856:        int i;
                    857:        int length = c->length;
1.4       ohara     858:        dump_integer(c->length);
1.2       ohara     859:        for(i=0; i<length; i++) {
1.4       ohara     860:                dump_integer(c->exps[i]);
1.2       ohara     861:        }
1.4       ohara     862:        dump_cmo(c->coef);
1.1       ohara     863: }
                    864:
1.4       ohara     865: static int dump_cmo_zz(cmo_zz* c)
1.1       ohara     866: {
1.4       ohara     867:     dump_mpz(c->mpz);
1.1       ohara     868: }
                    869:
1.4       ohara     870: static int dump_cmo_distributed_polynomial(cmo_distributed_polynomial* m)
1.3       ohara     871: {
                    872:     cell* cp = m->head;
1.4       ohara     873:     int len = length_cmo_list((cmo_list *)m);
                    874:     dump_integer(len);
                    875:        dump_cmo(m->ringdef);
1.3       ohara     876:     while(cp != NULL) {
1.4       ohara     877:         dump_cmo(cp->cmo);
1.3       ohara     878:         cp = cp->next;
                    879:     }
                    880: }
                    881:
1.1       ohara     882: /* タグを書き出してから、各関数を呼び出す */
1.4       ohara     883: int dump_cmo(cmo* m)
1.1       ohara     884: {
1.4       ohara     885:     dump_integer(m->tag);
1.1       ohara     886:     switch(m->tag) {
                    887:     case CMO_NULL:
1.2       ohara     888:     case CMO_ZERO:
                    889:     case CMO_DMS_GENERIC:
1.4       ohara     890:         dump_cmo_null(m);
                    891:                break;
1.1       ohara     892:     case CMO_INT32:
1.4       ohara     893:         dump_cmo_int32((cmo_int32 *)m);
                    894:                break;
1.1       ohara     895:     case CMO_STRING:
1.4       ohara     896:         dump_cmo_string((cmo_string *)m);
                    897:                break;
1.2       ohara     898:     case CMO_MATHCAP:
                    899:     case CMO_RING_BY_NAME:
1.8       ohara     900:     case CMO_INDETERMINATE:
1.2       ohara     901:     case CMO_ERROR2:
1.4       ohara     902:         dump_cmo_mathcap((cmo_mathcap *)m);
                    903:                break;
1.1       ohara     904:     case CMO_LIST:
1.4       ohara     905:         dump_cmo_list((cmo_list *)m);
                    906:                break;
1.2       ohara     907:     case CMO_MONOMIAL32:
1.4       ohara     908:         dump_cmo_monomial32((cmo_monomial32 *)m);
                    909:                break;
1.1       ohara     910:     case CMO_ZZ:
1.4       ohara     911:         dump_cmo_zz((cmo_zz *)m);
                    912:                break;
1.3       ohara     913:     case CMO_DISTRIBUTED_POLYNOMIAL:
1.4       ohara     914:         dump_cmo_distributed_polynomial((cmo_distributed_polynomial *)m);
                    915:                break;
1.1       ohara     916:     default:
                    917:     }
                    918: }
                    919:
1.4       ohara     920: static int dump_mpz(mpz_ptr mpz)
                    921: {
                    922:     int i;
                    923:     int len = abs(mpz->_mp_size);
                    924:     dump_integer(mpz->_mp_size);
                    925:     for(i=0; i<len; i++) {
                    926:         dump_integer(mpz->_mp_d[i]);
                    927:     }
                    928:     return;
                    929: }
                    930:
                    931: static int dump_string(char *s, int len)
                    932: {
                    933:     memcpy(&d_buf[d_ptr], s, len);
                    934:     d_ptr += len;
                    935: }
                    936:
                    937: static int dump_integer(int x)
1.1       ohara     938: {
                    939:     int nx = htonl(x);
1.4       ohara     940:        dump_string((char *)&nx, sizeof(int));
1.1       ohara     941: }
                    942:
1.4       ohara     943: int dump_ox_data(ox_data* m)
1.1       ohara     944: {
1.4       ohara     945:     dump_integer(OX_DATA);
                    946:     dump_integer(-1);
                    947:     dump_cmo(m->cmo);
1.1       ohara     948: }
                    949:
1.4       ohara     950: int dump_ox_command(ox_command* m)
                    951: {
                    952:     dump_integer(OX_COMMAND);
                    953:     dump_integer(-1);
                    954:     dump_integer(m->command);
                    955: }
1.1       ohara     956:
                    957: int send_ox(ox_file_t s, ox *m)
                    958: {
                    959:     int tag = m->tag;
                    960:     int code;
                    961:     if (tag == OX_DATA) {
                    962:         send_ox_cmo(s->stream, ((ox_data *)m)->cmo);
                    963:     }else if (tag == OX_COMMAND) {
                    964:         code = ((ox_command *)m)->command;
                    965:         if (code >= 1024) {
                    966:             /* control command */
                    967:             send_ox_command(s->control, code);
                    968:         }else {
                    969:             send_ox_command(s->stream, code);
                    970:         }
                    971:     }else {
                    972:         /* CMO?? */
                    973:         send_ox_cmo(s->stream, (cmo *)m);
                    974:     }
                    975: }
                    976:
                    977: int send_ox_cmo(int fd, cmo* m)
                    978: {
                    979:     send_ox_tag(fd, OX_DATA);
                    980:     send_cmo(fd, m);
                    981: }
                    982:
1.2       ohara     983: /* send_cmo_xxx 関数群 */
                    984: static int send_cmo_null(int fd, cmo_null* c)
                    985: {
                    986:     return 0;
                    987: }
1.1       ohara     988:
1.2       ohara     989: static int send_cmo_int32(int fd, cmo_int32* m)
1.1       ohara     990: {
1.2       ohara     991:     send_int32(fd, m->i);
1.1       ohara     992: }
                    993:
1.2       ohara     994: static int send_cmo_string(int fd, cmo_string* m)
1.1       ohara     995: {
1.6       ohara     996:     int len = (m->s != NULL)? strlen(m->s): 0;
1.2       ohara     997:     send_int32(fd, len);
                    998:     if (len > 0) {
                    999:         write(fd, m->s, len);
                   1000:     }
1.1       ohara    1001:     return 0;
                   1002: }
                   1003:
1.2       ohara    1004: static int send_cmo_mathcap(int fd, cmo_mathcap* c)
1.1       ohara    1005: {
1.2       ohara    1006:     send_cmo(fd, c->ob);
                   1007:     return 0;
1.1       ohara    1008: }
                   1009:
                   1010: static int send_cmo_list(int fd, cmo_list* c)
                   1011: {
                   1012:     cell* cp = c->head;
                   1013:     int len = length_cmo_list(c);
                   1014:     send_int32(fd, len);
                   1015:
1.8       ohara    1016:     while(cp->next != NULL) {
1.1       ohara    1017:         send_cmo(fd, cp->cmo);
                   1018:         cp = cp->next;
                   1019:     }
                   1020:     return 0;
                   1021: }
                   1022:
1.7       ohara    1023: static int send_cmo_distributed_polynomial(int fd, cmo_distributed_polynomial* c)
                   1024: {
                   1025:     cell* cp = c->head;
                   1026:     int len = length_cmo_list((cmo_list *)c);
                   1027:     send_int32(fd, len);
                   1028:        send_cmo(fd, c->ringdef);
                   1029:
1.8       ohara    1030:     while(cp->next != NULL) {
1.7       ohara    1031:         send_cmo(fd, cp->cmo);
                   1032:         cp = cp->next;
                   1033:     }
                   1034:     return 0;
                   1035: }
                   1036:
                   1037: static int send_cmo_monomial32(int fd, cmo_monomial32* c)
                   1038: {
                   1039:        int i;
                   1040:        int len = c->length;
                   1041:        send_int32(fd, len);
                   1042:        for(i=0; i<len; i++) {
                   1043:                send_int32(fd, c->exps[i]);
                   1044:        }
                   1045:        send_cmo(fd, c->coef);
                   1046:        return 0;
                   1047: }
                   1048:
1.2       ohara    1049: static int send_cmo_zz(int fd, cmo_zz* c)
1.1       ohara    1050: {
1.2       ohara    1051:     send_mpz(fd, c->mpz);
1.7       ohara    1052:        return 0;
1.1       ohara    1053: }
                   1054:
1.2       ohara    1055: static int send_cmo_error2(int fd, cmo_error2* c)
1.1       ohara    1056: {
1.2       ohara    1057:     send_cmo(fd, c->ob);
1.1       ohara    1058:     return 0;
                   1059: }
                   1060:
                   1061: /* CMOを送る.  OX_tag は送信済*/
                   1062: int send_cmo(int fd, cmo* c)
                   1063: {
                   1064:     int tag = c->tag;
                   1065:
                   1066:     send_int32(fd, tag);
                   1067:     switch(tag) {
                   1068:     case CMO_NULL:
1.7       ohara    1069:     case CMO_ZERO:
                   1070:     case CMO_DMS_GENERIC:
1.1       ohara    1071:         send_cmo_null(fd, c);  /* 空の関数 */
                   1072:         break;
                   1073:     case CMO_INT32:
                   1074:         send_cmo_int32(fd, (cmo_int32 *)c);
                   1075:         break;
                   1076:     case CMO_STRING:
                   1077:         send_cmo_string(fd, (cmo_string *)c);
                   1078:         break;
                   1079:     case CMO_MATHCAP:
1.7       ohara    1080:     case CMO_ERROR2:
                   1081:     case CMO_RING_BY_NAME:
                   1082:     case CMO_INDETERMINATE:
1.1       ohara    1083:         send_cmo_mathcap(fd, (cmo_mathcap *)c);
                   1084:         break;
                   1085:     case CMO_LIST:
                   1086:         send_cmo_list(fd, (cmo_list *)c);
                   1087:         break;
1.7       ohara    1088:     case CMO_MONOMIAL32:
                   1089:         send_cmo_monomial32(fd, (cmo_monomial32 *)c);
                   1090:         break;
1.1       ohara    1091:     case CMO_ZZ:
                   1092:         send_cmo_zz(fd, (cmo_zz *)c);
                   1093:         break;
1.7       ohara    1094:     case CMO_DISTRIBUTED_POLYNOMIAL:
                   1095:         send_cmo_distributed_polynomial(fd, (cmo_distributed_polynomial *)c);
1.1       ohara    1096:         break;
                   1097:     default:
                   1098:     }
                   1099: }
                   1100:
                   1101: static int send_mpz(int fd, mpz_ptr mpz)
                   1102: {
                   1103:     int i;
                   1104:     int len = abs(mpz->_mp_size);
                   1105:     send_int32(fd, mpz->_mp_size);
                   1106:     for(i=0; i<len; i++) {
                   1107:         send_int32(fd, mpz->_mp_d[i]);
                   1108:     }
                   1109:     return 0;
                   1110: }
                   1111:
                   1112: ox_data* new_ox_data(cmo* c)
                   1113: {
                   1114:     ox_data* m = malloc(sizeof(ox_data));
                   1115:     m->tag = OX_DATA;
                   1116:     m->cmo = c;
                   1117:     return m;
                   1118: }
                   1119:
                   1120: ox_command* new_ox_command(int sm_code)
                   1121: {
                   1122:     ox_command* m = malloc(sizeof(ox_command));
                   1123:     m->tag = OX_COMMAND;
                   1124:     m->command = sm_code;
                   1125:     return m;
                   1126: }
                   1127:
                   1128: #define  MAX_TYPES  8
                   1129: static int known_types[] = {
                   1130:     -1,   /* gate keeper */
                   1131:     CMO_NULL,
                   1132:     CMO_INT32,
                   1133:     CMO_STRING,
                   1134:     CMO_MATHCAP,
                   1135:     CMO_LIST,
                   1136:     CMO_ZZ,
                   1137:     CMO_ERROR2,
                   1138: };
                   1139:
1.2       ohara    1140: #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    1141:
1.2       ohara    1142: cmo* make_mathcap_object2(int ver, char* ver_s, char* sysname)
1.1       ohara    1143: {
1.2       ohara    1144:     cmo *cap;
                   1145:     char buff[8192];
1.1       ohara    1146:
                   1147:     setgetc(mygetc);
1.2       ohara    1148:     sprintf(buff, ID_TEMP, ver, sysname, ver_s, getenv("HOSTTYPE"));
                   1149:     setmode_mygetc(buff, 8192);
                   1150:     cap = parse();
1.1       ohara    1151:     resetgetc();
                   1152:
1.2       ohara    1153:     return cap;
1.1       ohara    1154: }
                   1155:
                   1156: cmo* make_mathcap_object(int version, char* id_string)
                   1157: {
                   1158:     cmo_list *li_ver, *li_cmo, *li;
                   1159:
                   1160:     int i;
                   1161:     li_ver = new_cmo_list();
                   1162:     append_cmo_list(li_ver, (cmo *)new_cmo_int32(version));
                   1163:     append_cmo_list(li_ver, (cmo *)new_cmo_string(id_string));
                   1164:
                   1165:     li_cmo = new_cmo_list();
                   1166:     for(i=0; i<MAX_TYPES; i++) {
                   1167:         if (known_types[i] != -1) {
                   1168:             append_cmo_list(li_cmo, (cmo *)new_cmo_int32(known_types[i]));
                   1169:         }
                   1170:     }
                   1171:
                   1172:     li = new_cmo_list();
                   1173:     append_cmo_list(li, (cmo *)li_ver);
                   1174:     append_cmo_list(li, (cmo *)li_cmo);
                   1175:
1.2       ohara    1176:     return (cmo *)new_cmo_mathcap((cmo *)li);
1.1       ohara    1177: }
                   1178:
                   1179: static int funcs(int cmo_type)
                   1180: {
                   1181:     int i;
                   1182:     for(i=0; i<MAX_TYPES; i++) {
                   1183:         if (known_types[i] == cmo_type) {
                   1184:             return i;
                   1185:         }
                   1186:     }
                   1187:     return 0;
                   1188: }
                   1189:
                   1190: void setCmotypeDisable(int type)
                   1191: {
                   1192:     int i = funcs(type);
                   1193:     known_types[i] = -1;
                   1194: }
1.8       ohara    1195:
1.1       ohara    1196: #if 0
                   1197: cmo* (*received_funcs[])(int fd) = {
                   1198:     NULL,  /* gate keeper */
                   1199:     receive_cmo_null,
                   1200:     receive_cmo_int32,
                   1201:     receive_cmo_string,
                   1202:     receive_cmo_mathcap,
                   1203:     receive_cmo_list,
                   1204:     receive_cmo_zz,
1.2       ohara    1205:     receive_cmo_error2
1.1       ohara    1206: };
                   1207:
                   1208: cmo* receive_cmo2(int fd)
                   1209: {
                   1210:     int tag;
                   1211:     cmo* (*foo)() = received_funcs[funcs(tag)];
                   1212:     if (foo != NULL) {
                   1213:         return foo(fd);
                   1214:     }
                   1215: }
1.3       ohara    1216: #endif
                   1217:
                   1218: /* ファイルディスクリプタ fd の通信路での integer の byte order を決定する */
                   1219: /* 実際には order (0,1,or 0xFF)をみてはいない */
1.5       ohara    1220: int decideByteOrderClient(oxfd fd, int order)
1.1       ohara    1221: {
1.3       ohara    1222:     char zero = OX_BYTE_NETWORK_BYTE_ORDER;
1.1       ohara    1223:     char dest;
1.5       ohara    1224:     read(fd, &dest, sizeof(char));
                   1225:     write(fd, &zero, sizeof(char));
1.1       ohara    1226:     return 0;
                   1227: }
                   1228:
                   1229: /* Server 側ではこちらを用いる */
1.5       ohara    1230: /* いまの実装は dup されていることが前提になっている */
                   1231: int decideByteOrderServer(oxfd fd, int order)
1.1       ohara    1232: {
1.3       ohara    1233:     char zero = OX_BYTE_NETWORK_BYTE_ORDER;
1.1       ohara    1234:     char dest;
1.5       ohara    1235:     write(fd, &zero, sizeof(char));
                   1236:     read(fd, &dest, sizeof(char));
1.1       ohara    1237:     return 0;
                   1238: }
                   1239:
1.5       ohara    1240: /* cmo と string (ここではC言語のstring) の変換関数群 */
                   1241: char *convert_zz_to_string(cmo_zz *c)
1.1       ohara    1242: {
                   1243:     return mpz_get_str(NULL, 10, c->mpz);
                   1244: }
                   1245:
1.5       ohara    1246: char *convert_cmo_to_string(cmo *m)
1.1       ohara    1247: {
                   1248:     switch(m->tag) {
                   1249:     case CMO_ZZ:
1.5       ohara    1250:         return convert_zz_to_string((cmo_zz *)m);
1.1       ohara    1251:     case CMO_INT32:
1.5       ohara    1252:         return convert_int_to_string(((cmo_int32 *)m)->i);
1.1       ohara    1253:     case CMO_STRING:
                   1254:         return ((cmo_string *)m)->s;
                   1255:     case CMO_NULL:
1.5       ohara    1256:         return convert_null_to_string();
1.1       ohara    1257:     default:
                   1258:         fprintf(stderr, "sorry, not implemented CMO\n");
                   1259:         /* まだ実装していません. */
                   1260:         return NULL;
                   1261:     }
                   1262: }
                   1263:
1.5       ohara    1264: char *convert_null_to_string()
1.1       ohara    1265: {
                   1266:     static char* null_string = "";
                   1267:     return null_string;
                   1268: }
                   1269:
1.5       ohara    1270: char *convert_int_to_string(int integer)
1.1       ohara    1271: {
                   1272:     char buff[1024];
                   1273:     char *s;
                   1274:
                   1275:     sprintf(buff, "%d", integer);
                   1276:     s = malloc(strlen(buff)+1);
                   1277:     strcpy(s, buff);
                   1278:
                   1279:     return s;
                   1280: }

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