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

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

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