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

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

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