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

Annotation of OpenXM/src/ox_toolkit/ox.c, Revision 1.2

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

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