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

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

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