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

Annotation of OpenXM/src/ox_math/ox.c, Revision 1.4

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

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