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

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

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