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

Annotation of OpenXM/src/ox_toolkit/ox.c, Revision 1.13

1.1       ohara       1: /* -*- mode: C; coding: euc-japan -*- */
1.13    ! ohara       2: /* $OpenXM: OpenXM/src/ox_toolkit/ox.c,v 1.12 2000/03/10 12:24:38 ohara Exp $ */
1.1       ohara       3:
1.8       ohara       4: /*
                      5:    This module includes functions for sending/receiveng CMO's.
                      6:    Some commnets is written in Japanese by the EUC-JP coded
                      7:    character set.
1.1       ohara       8: */
                      9:
                     10: #include <stdio.h>
                     11: #include <stdlib.h>
                     12: #include <string.h>
                     13: #include <unistd.h>
                     14: #include <errno.h>
                     15: #include <fcntl.h>
1.5       ohara      16: #include <sys/file.h>
1.13    ! ohara      17: #include <time.h>
1.1       ohara      18:
                     19: #include "mysocket.h"
1.12      ohara      20: #include "ox_toolkit.h"
1.1       ohara      21: #include "parse.h"
                     22:
1.3       ohara      23: /* CMO_xxx の値の順にならべること(デバッグのため) */
1.13    ! ohara      24: static cmo_null*         receive_cmo_null(OXFILE *oxfp);
        !            25: static cmo_int32*        receive_cmo_int32(OXFILE *oxfp);
        !            26: static cmo_string*       receive_cmo_string(OXFILE *oxfp);
        !            27: static cmo_mathcap*      receive_cmo_mathcap(OXFILE *oxfp);
        !            28: static cmo_list*         receive_cmo_list(OXFILE *oxfp);
        !            29: static cmo_monomial32*   receive_cmo_monomial32(OXFILE *oxfp);
        !            30: static cmo_zz*           receive_cmo_zz(OXFILE *oxfp);
        !            31: static cmo_zero*         receive_cmo_zero(OXFILE *oxfp);
        !            32: static cmo_dms_generic*  receive_cmo_dms_generic(OXFILE *oxfp);
        !            33: static cmo_ring_by_name* receive_cmo_ring_by_name(OXFILE *oxfp);
        !            34: static cmo_distributed_polynomial* receive_cmo_distributed_polynomial(OXFILE *oxfp);
        !            35:
        !            36: static cmo_error2*       receive_cmo_error2(OXFILE *oxfp);
        !            37: static void              receive_mpz(OXFILE *oxfp, mpz_ptr mpz);
        !            38:
        !            39: static int          send_cmo_null(OXFILE *oxfp, cmo_null* c);
        !            40: static int          send_cmo_int32(OXFILE *oxfp, cmo_int32* m);
        !            41: static int          send_cmo_string(OXFILE *oxfp, cmo_string* m);
        !            42: static int          send_cmo_mathcap(OXFILE *oxfp, cmo_mathcap* c);
        !            43: static int          send_cmo_list(OXFILE *oxfp, cmo_list* c);
        !            44: static int          send_cmo_monomial32(OXFILE *oxfp, cmo_monomial32* c);
        !            45: static int          send_cmo_zz(OXFILE *oxfp, cmo_zz* c);
        !            46: static int          send_cmo_error2(OXFILE *oxfp, cmo_error2* c);
        !            47: static int          send_mpz(OXFILE *oxfp, mpz_ptr mpz);
        !            48: static int          send_cmo_distributed_polynomial(OXFILE *oxfp, cmo_distributed_polynomial* c);
1.3       ohara      49:
1.11      ohara      50: int ssh_ox_server(char *, char *, char *, short, short);
                     51:
1.13    ! ohara      52: OXFILE *current_fd = NULL;
        !            53: void set_current_fd(OXFILE *oxfp)
1.2       ohara      54: {
1.13    ! ohara      55:     current_fd = oxfp;
1.2       ohara      56: }
                     57:
1.8       ohara      58: /* hook functions. (yet not implemented) */
1.2       ohara      59: static hook_t hook_before_send_cmo = NULL;
                     60: static hook_t hook_after_send_cmo  = NULL;
                     61:
                     62: int add_hook_before_send_cmo(hook_t func)
                     63: {
1.13    ! ohara      64:     hook_before_send_cmo = func;
        !            65:     return 0;
1.2       ohara      66: }
                     67:
                     68: int add_hook_after_send_cmo(hook_t func)
                     69: {
1.13    ! ohara      70:     hook_after_send_cmo = func;
        !            71:     return 0;
1.2       ohara      72: }
                     73:
1.13    ! ohara      74: static cmo *call_hook_before_send_cmo(OXFILE *oxfp, cmo *c)
1.2       ohara      75: {
1.13    ! ohara      76:     if (hook_before_send_cmo != NULL) {
        !            77:         return hook_before_send_cmo(oxfp, c);
        !            78:     }
        !            79:     return c;
1.2       ohara      80: }
                     81:
1.13    ! ohara      82: static cmo *call_hook_after_send_cmo(OXFILE *oxfp, cmo *c)
1.2       ohara      83: {
1.13    ! ohara      84:     if (hook_after_send_cmo != NULL) {
        !            85:         return hook_after_send_cmo(oxfp, c);
        !            86:     }
        !            87:     return c;
        !            88: }
        !            89:
        !            90: int oxf_read(void *buffer, size_t size, size_t num, OXFILE *oxfp)
        !            91: {
        !            92:     return read(oxfp->fd, buffer, size*num);
        !            93: /*  return fread(buffer, size, num, oxfp->fp); */
        !            94: }
        !            95:
        !            96: int oxf_write(void *buffer, size_t size, size_t num, OXFILE *oxfp)
        !            97: {
        !            98:     return write(oxfp->fd, buffer, size*num);
        !            99: /*  return fwrite(buffer, size, num, oxfp->fp); */
1.2       ohara     100: }
1.1       ohara     101:
1.7       ohara     102: /* Handling an error. */
1.1       ohara     103: static int current_received_serial = 0;
                    104:
1.7       ohara     105: /* If an error object be needed, then a server call the following function. */
1.1       ohara     106: cmo_error2* make_error_object(int err_code, cmo *ob)
                    107: {
                    108:     cmo_list* li = new_cmo_list();
1.13    ! ohara     109:     list_append(li, (cmo *)new_cmo_int32(current_received_serial));
        !           110:     list_append(li, (cmo *)new_cmo_int32(err_code));
        !           111:     list_append(li, ob);
1.1       ohara     112:     /* 他の情報を加えるならココ */
                    113:     return new_cmo_error2((cmo *)li);
                    114: }
                    115:
                    116: #define DEFAULT_SERIAL_NUMBER 0x0000ffff
1.13    ! ohara     117: #define receive_serial_number(x)   (receive_int32((x)))
1.1       ohara     118:
1.7       ohara     119: /* getting a next serial number. */
1.13    ! ohara     120: int next_serial(OXFILE *oxfp)
1.1       ohara     121: {
1.13    ! ohara     122: /*
1.1       ohara     123:     static int serial_number = DEFAULT_SERIAL_NUMBER;
1.13    ! ohara     124: */
        !           125:     return oxfp->serial_number++;
1.1       ohara     126: }
                    127:
1.7       ohara     128: /* sending an object of int32 type. (not equal to cmo_int32 type)  */
1.13    ! ohara     129: int send_int32(OXFILE *oxfp, int int32)
1.1       ohara     130: {
1.13    ! ohara     131:     return oxfp->send_int32(oxfp, int32);
1.1       ohara     132: }
                    133:
1.13    ! ohara     134: /* sending an object of int32 type with Network Byte Order.
        !           135:    (not equal to cmo_int32 type)  */
        !           136: int send_int32_nbo(OXFILE *oxfp, int int32)
1.1       ohara     137: {
1.13    ! ohara     138:     int32 = htonl(int32);
        !           139:     return oxf_write(&int32, sizeof(int), 1, oxfp);
1.1       ohara     140: }
                    141:
1.13    ! ohara     142: /* sending an object of int32 type with Local Byte Order.
        !           143:    (not equal to cmo_int32 type)  */
        !           144: int send_int32_lbo(OXFILE *oxfp, int int32)
1.1       ohara     145: {
1.13    ! ohara     146:     return oxf_write(&int32, sizeof(int), 1, oxfp);
1.1       ohara     147: }
                    148:
1.13    ! ohara     149: /* receiving an object of int32 type. (not equal to cmo_int32 type)  */
        !           150: int receive_int32(OXFILE *oxfp)
1.1       ohara     151: {
1.13    ! ohara     152:     return oxfp->receive_int32(oxfp);
1.1       ohara     153: }
                    154:
1.13    ! ohara     155: /* receiving an object of int32 type with Network Byte Order.
        !           156:    (not equal to cmo_int32 type)  */
        !           157: int receive_int32_nbo(OXFILE *oxfp)
1.1       ohara     158: {
1.13    ! ohara     159:     int tag;
        !           160:     oxf_read(&tag, sizeof(int), 1, oxfp);
        !           161:     return ntohl(tag);
1.1       ohara     162: }
                    163:
1.13    ! ohara     164: /* receiving an object of int32 type with Local Byte Order.
        !           165:    (not equal to cmo_int32 type)  */
        !           166: int receive_int32_lbo(OXFILE *oxfp)
1.1       ohara     167: {
1.13    ! ohara     168:     int tag;
        !           169:     oxf_read(&tag, sizeof(int), 1, oxfp);
        !           170:     return tag;
1.1       ohara     171: }
                    172:
1.13    ! ohara     173: /* socket システムコールなどで socket を開いたのち、
        !           174:    fdopen(sd, "a+") でバッファリングする。("w+" ではない)
        !           175:    バッファリングの後、バイトオーダを決定し、
        !           176:    oxf_setopt() で関数ポインタを設定し直す。*/
1.1       ohara     177:
1.13    ! ohara     178: /* receiving an (OX_tag, serial number)  */
        !           179: int receive_ox_tag(OXFILE *oxfp)
1.1       ohara     180: {
1.13    ! ohara     181:     int tag = receive_int32(oxfp);
        !           182:     current_received_serial = receive_serial_number(oxfp);
        !           183:     return tag;
1.1       ohara     184: }
                    185:
1.13    ! ohara     186: /* sending an (OX_tag, serial number)  */
        !           187: int send_ox_tag(OXFILE *oxfp, int tag)
1.3       ohara     188: {
1.13    ! ohara     189:     send_int32(oxfp, tag);
        !           190:     return send_int32(oxfp, next_serial(oxfp));
1.1       ohara     191: }
                    192:
1.7       ohara     193: /* functions named receive_cmo_*. */
1.13    ! ohara     194: static cmo_null* receive_cmo_null(OXFILE *oxfp)
1.1       ohara     195: {
                    196:     return new_cmo_null();
                    197: }
                    198:
1.13    ! ohara     199: static cmo_int32* receive_cmo_int32(OXFILE *oxfp)
1.1       ohara     200: {
1.13    ! ohara     201:     int i = receive_int32(oxfp);
1.1       ohara     202:     return new_cmo_int32(i);
                    203: }
                    204:
1.13    ! ohara     205: static cmo_string* receive_cmo_string(OXFILE *oxfp)
1.1       ohara     206: {
1.13    ! ohara     207:     int len = receive_int32(oxfp);
1.1       ohara     208:     char* s = malloc(len+1);
                    209:     memset(s, '\0', len+1);
                    210:     if (len > 0) {
1.13    ! ohara     211:         oxf_read(s, 1, len, oxfp);
1.1       ohara     212:     }
                    213:     return new_cmo_string(s);
                    214: }
                    215:
1.13    ! ohara     216: static cmo_mathcap* receive_cmo_mathcap(OXFILE *oxfp)
1.1       ohara     217: {
1.13    ! ohara     218:     cmo* ob = receive_cmo(oxfp);
1.1       ohara     219:     return new_cmo_mathcap(ob);
                    220: }
                    221:
1.13    ! ohara     222: static cmo_list* receive_cmo_list(OXFILE *oxfp)
1.1       ohara     223: {
                    224:     cmo* ob;
                    225:     cmo_list* c = new_cmo_list();
1.13    ! ohara     226:     int len = receive_int32(oxfp);
1.1       ohara     227:
                    228:     while (len>0) {
1.13    ! ohara     229:         ob = receive_cmo(oxfp);
        !           230:         list_append(c, ob);
1.1       ohara     231:         len--;
                    232:     }
                    233:     return c;
                    234: }
                    235:
1.13    ! ohara     236: static cmo_monomial32* receive_cmo_monomial32(OXFILE *oxfp)
1.1       ohara     237: {
                    238:     int i;
1.13    ! ohara     239:     int len = receive_int32(oxfp);
1.1       ohara     240:     cmo_monomial32* c = new_cmo_monomial32(len);
                    241:
                    242:     for(i=0; i<len; i++) {
1.13    ! ohara     243:         c->exps[i] = receive_int32(oxfp);
1.1       ohara     244:     }
1.13    ! ohara     245:     c->coef = receive_cmo(oxfp);
1.1       ohara     246:     return c;
                    247: }
                    248:
1.13    ! ohara     249: static cmo_zz* receive_cmo_zz(OXFILE *oxfp)
1.1       ohara     250: {
                    251:     cmo_zz* c = new_cmo_zz();
1.13    ! ohara     252:     receive_mpz(oxfp, c->mpz);
1.1       ohara     253:     return c;
                    254: }
                    255:
1.13    ! ohara     256: static cmo_zero* receive_cmo_zero(OXFILE *oxfp)
1.1       ohara     257: {
                    258:     return new_cmo_zero();
                    259: }
                    260:
1.13    ! ohara     261: static cmo_dms_generic* receive_cmo_dms_generic(OXFILE *oxfp)
1.1       ohara     262: {
                    263:     return new_cmo_dms_generic();
                    264: }
                    265:
1.13    ! ohara     266: static cmo_ring_by_name* receive_cmo_ring_by_name(OXFILE *oxfp)
1.1       ohara     267: {
1.13    ! ohara     268:     cmo* ob = receive_cmo(oxfp);
        !           269:     /* We need to check semantics but yet ... */
1.1       ohara     270:     return new_cmo_ring_by_name(ob);
                    271: }
                    272:
1.13    ! ohara     273: static cmo_distributed_polynomial* receive_cmo_distributed_polynomial(OXFILE *oxfp)
1.1       ohara     274: {
                    275:     cmo* ob;
                    276:     cmo_distributed_polynomial* c = new_cmo_distributed_polynomial();
1.13    ! ohara     277:     int len = receive_int32(oxfp);
        !           278:     c->ringdef = receive_cmo(oxfp);
1.1       ohara     279:
                    280:     while (len>0) {
1.13    ! ohara     281:         ob = receive_cmo(oxfp);
        !           282:         list_append((cmo_list *)c, ob);
1.1       ohara     283:         len--;
                    284:     }
                    285:     return c;
                    286: }
                    287:
1.13    ! ohara     288: static cmo_error2* receive_cmo_error2(OXFILE *oxfp)
1.1       ohara     289: {
1.13    ! ohara     290:     cmo* ob = receive_cmo(oxfp);
1.1       ohara     291:     return new_cmo_error2(ob);
                    292: }
                    293:
                    294: /* receive_ox_tag() == OX_DATA の後に呼び出される */
                    295: /* 関数ポインタを使った方がきれいに書けるような気がする.  */
1.13    ! ohara     296: /* if (foo[tag] != NULL) foo[tag](oxfp); とか */
1.1       ohara     297:
1.13    ! ohara     298: cmo* receive_cmo(OXFILE *oxfp)
1.1       ohara     299: {
                    300:     cmo* m;
1.13    ! ohara     301:     int tag = receive_int32(oxfp);
1.1       ohara     302:     switch(tag) {
                    303:     case CMO_NULL:
1.13    ! ohara     304:         m = receive_cmo_null(oxfp);
1.1       ohara     305:         break;
                    306:     case CMO_INT32:
1.13    ! ohara     307:         m = (cmo *)receive_cmo_int32(oxfp);
1.1       ohara     308:         break;
                    309:     case CMO_STRING:
1.13    ! ohara     310:         m = (cmo *)receive_cmo_string(oxfp);
1.1       ohara     311:         break;
                    312:     case CMO_MATHCAP:
1.13    ! ohara     313:         m = (cmo *)receive_cmo_mathcap(oxfp);
1.1       ohara     314:         break;
                    315:     case CMO_LIST:
1.13    ! ohara     316:         m = (cmo *)receive_cmo_list(oxfp);
1.1       ohara     317:         break;
                    318:     case CMO_MONOMIAL32:
1.13    ! ohara     319:         m = (cmo *)receive_cmo_monomial32(oxfp);
1.1       ohara     320:         break;
                    321:     case CMO_ZZ:
1.13    ! ohara     322:         m = (cmo *)receive_cmo_zz(oxfp);
1.1       ohara     323:         break;
                    324:     case CMO_ZERO:
1.13    ! ohara     325:         m = (cmo *)receive_cmo_zero(oxfp);
1.1       ohara     326:         break;
                    327:     case CMO_DMS_GENERIC:
1.13    ! ohara     328:         m = (cmo *)receive_cmo_dms_generic(oxfp);
1.1       ohara     329:         break;
                    330:     case CMO_RING_BY_NAME:
1.13    ! ohara     331:         m = (cmo *)receive_cmo_ring_by_name(oxfp);
1.1       ohara     332:         break;
                    333:     case CMO_DISTRIBUTED_POLYNOMIAL:
1.13    ! ohara     334:         m = (cmo *)receive_cmo_distributed_polynomial(oxfp);
1.1       ohara     335:         break;
                    336:     case CMO_ERROR2:
1.13    ! ohara     337:         m = (cmo *)receive_cmo_error2(oxfp);
1.1       ohara     338:         break;
                    339:     case CMO_DATUM:
                    340:     case CMO_QQ:
                    341:     default:
1.13    ! ohara     342:         m = NULL;
        !           343:         fprintf(stderr, "the CMO (%d) is not implemented.\n", tag);
1.1       ohara     344:     }
                    345:     return m;
                    346: }
                    347:
1.13    ! ohara     348: static void receive_mpz(OXFILE *oxfp, mpz_ptr mpz)
1.1       ohara     349: {
                    350:     int i;
1.13    ! ohara     351:     int size  = receive_int32(oxfp);
1.1       ohara     352:     int len   = abs(size);
                    353:     resize_mpz(mpz, size);
                    354:
                    355:     for(i=0; i<len; i++) {
1.13    ! ohara     356:         mpz->_mp_d[i] = receive_int32(oxfp);
1.1       ohara     357:     }
                    358: }
                    359:
1.13    ! ohara     360: void send_ox_command(OXFILE *oxfp, int sm_command)
1.1       ohara     361: {
1.13    ! ohara     362:     send_ox_tag(oxfp, OX_COMMAND);
        !           363:     send_int32(oxfp, sm_command);
        !           364:     oxf_flush(oxfp);
1.1       ohara     365: }
                    366:
1.13    ! ohara     367: void ox_close(OXFILE *sv)
1.1       ohara     368: {
1.13    ! ohara     369:     send_ox_command(oxf_control(sv), SM_control_kill);
1.1       ohara     370: #ifdef DEBUG
1.7       ohara     371:     sleep(2);
1.13    ! ohara     372:     /* We wait thar an OpenXM server terminates. */
1.1       ohara     373:     fprintf(stderr, "I have closed the connection to an Open XM server.\n");
                    374: #endif
                    375: }
                    376:
1.13    ! ohara     377: void ox_shutdown(OXFILE *sv)
1.3       ohara     378: {
1.13    ! ohara     379:     /* We need to use SM_shutdown but yet ... */
        !           380:     ox_close(sv);
1.3       ohara     381: }
                    382:
1.13    ! ohara     383: void ox_cmo_rpc(OXFILE *sv, char *function, int argc, cmo *argv[])
1.3       ohara     384: {
1.13    ! ohara     385:     int i = argc;
        !           386:     while(i-- > 0) {
        !           387:         send_ox_cmo(sv, argv[i]);
        !           388:     }
        !           389:     send_ox_cmo(sv, (cmo *)new_cmo_int32(argc));
        !           390:     send_ox_cmo(sv, (cmo *)new_cmo_string(function));
        !           391:     send_ox_command(sv, SM_executeFunction);
1.3       ohara     392: }
                    393:
1.13    ! ohara     394: void ox_execute_string(OXFILE *sv, char* s)
1.3       ohara     395: {
1.13    ! ohara     396:     send_ox_cmo(sv, (cmo *)new_cmo_string(s));
        !           397:     send_ox_command(sv, SM_executeStringByLocalParser);
1.3       ohara     398: }
                    399:
1.13    ! ohara     400: void ox_push_cmd(OXFILE *sv, int sm_code)
1.1       ohara     401: {
1.13    ! ohara     402:     send_ox_command(sv, sm_code);
1.1       ohara     403: }
                    404:
1.13    ! ohara     405: cmo_mathcap* ox_mathcap(OXFILE *sv)
1.1       ohara     406: {
1.13    ! ohara     407:     send_ox_command(sv, SM_mathcap);
        !           408:     send_ox_command(sv, SM_popCMO);
        !           409:     receive_ox_tag(sv);          /* OX_DATA */
        !           410:     return (cmo_mathcap *)receive_cmo(sv);
1.1       ohara     411: }
                    412:
1.13    ! ohara     413: char* ox_popString(OXFILE *sv)
1.1       ohara     414: {
                    415:     cmo_string* m = NULL;
                    416:
1.13    ! ohara     417:     send_ox_command(sv, SM_popString);
        !           418:     receive_ox_tag(sv); /* OX_DATA */
        !           419:     m = (cmo_string *)receive_cmo(sv);
1.1       ohara     420:     return m->s;
                    421: }
                    422:
1.13    ! ohara     423: void ox_pops(OXFILE *sv, int num)
1.3       ohara     424: {
1.13    ! ohara     425:     send_ox_cmo(sv, (cmo *)new_cmo_int32(num));
        !           426:     send_ox_command(sv, SM_pops);
1.3       ohara     427: }
                    428:
1.13    ! ohara     429: cmo* ox_pop_cmo(OXFILE *sv)
1.3       ohara     430: {
1.13    ! ohara     431:     send_ox_command(sv, SM_popCMO);
        !           432:     receive_ox_tag(sv); /* OX_DATA */
        !           433:     return receive_cmo(sv);
1.3       ohara     434: }
                    435:
1.13    ! ohara     436: void ox_push_cmo(OXFILE *sv, cmo *c)
1.3       ohara     437: {
1.13    ! ohara     438:     send_ox_cmo(sv, c);
1.3       ohara     439: }
                    440:
1.7       ohara     441: /* a dummy function for flushing a connection. */
1.13    ! ohara     442: int ox_flush(OXFILE *sv)
1.5       ohara     443: {
1.13    ! ohara     444:     return 1;
1.5       ohara     445: }
                    446:
1.13    ! ohara     447: void ox_reset(OXFILE *sv)
1.1       ohara     448: {
1.13    ! ohara     449:     send_ox_command(oxf_control(sv), SM_control_reset_connection);
1.1       ohara     450:
1.13    ! ohara     451:     receive_ox_tag(oxf_control(sv));      /* OX_DATA */
        !           452:     receive_cmo(oxf_control(sv));         /* (CMO_INT32, 0) */
1.1       ohara     453:
1.13    ! ohara     454:     while(receive_ox_tag(sv) != OX_SYNC_BALL) {
        !           455:         receive_cmo(sv); /* skipping a message. */
1.1       ohara     456:     }
                    457:
1.13    ! ohara     458:     send_ox_tag(sv, OX_SYNC_BALL);
1.1       ohara     459: #ifdef DEBUG
                    460:     fprintf(stderr, "I have reset an Open XM server.\n");
                    461: #endif
                    462: }
                    463:
1.13    ! ohara     464: void send_ox(OXFILE *oxfp, ox *m)
1.1       ohara     465: {
                    466:     switch(m->tag) {
1.13    ! ohara     467:     case OX_DATA:
        !           468:         send_ox_cmo(oxfp, ((ox_data *)m)->cmo);
1.1       ohara     469:         break;
1.13    ! ohara     470:     case OX_COMMAND:
        !           471:         send_ox_command(oxfp, ((ox_command *)m)->command);
1.1       ohara     472:         break;
                    473:     default:
                    474:         /* CMO?? */
1.13    ! ohara     475:         send_ox_cmo(oxfp, (cmo *)m);
1.1       ohara     476:     }
                    477: }
                    478:
1.13    ! ohara     479: void send_ox_cmo(OXFILE *oxfp, cmo* m)
1.1       ohara     480: {
1.13    ! ohara     481:     send_ox_tag(oxfp, OX_DATA);
        !           482:     send_cmo(oxfp, m);
        !           483:     oxf_flush(oxfp);
1.1       ohara     484: }
                    485:
1.8       ohara     486: /* send_cmo_* functions */
1.13    ! ohara     487: static int send_cmo_null(OXFILE *oxfp, cmo_null* c)
1.1       ohara     488: {
                    489:     return 0;
                    490: }
                    491:
1.13    ! ohara     492: static int send_cmo_int32(OXFILE *oxfp, cmo_int32* m)
1.1       ohara     493: {
1.13    ! ohara     494:     return send_int32(oxfp, m->i);
1.1       ohara     495: }
                    496:
1.13    ! ohara     497: static int send_cmo_string(OXFILE *oxfp, cmo_string* m)
1.1       ohara     498: {
                    499:     int len = (m->s != NULL)? strlen(m->s): 0;
1.13    ! ohara     500:     send_int32(oxfp, len);
1.1       ohara     501:     if (len > 0) {
1.13    ! ohara     502:         oxf_write(m->s, 1, len, oxfp);
1.1       ohara     503:     }
                    504:     return 0;
                    505: }
                    506:
1.13    ! ohara     507: static int send_cmo_mathcap(OXFILE *oxfp, cmo_mathcap* c)
1.1       ohara     508: {
1.13    ! ohara     509:     send_cmo(oxfp, c->ob);
1.1       ohara     510:     return 0;
                    511: }
                    512:
1.13    ! ohara     513: static int send_cmo_list(OXFILE *oxfp, cmo_list* c)
1.1       ohara     514: {
1.13    ! ohara     515:     cell* el = list_first(c);
        !           516:     int len = list_length(c);
        !           517:     send_int32(oxfp, len);
        !           518:
        !           519:     while(!list_endof(c, el)) {
        !           520:         send_cmo(oxfp, el->cmo);
        !           521:         el = list_next(el);
1.1       ohara     522:     }
                    523:     return 0;
                    524: }
                    525:
1.13    ! ohara     526: static int send_cmo_distributed_polynomial(OXFILE *oxfp, cmo_distributed_polynomial* c)
1.1       ohara     527: {
1.13    ! ohara     528:     cell* el = list_first((cmo_list *)c);
        !           529:     int len = list_length((cmo_list *)c);
        !           530:     send_int32(oxfp, len);
        !           531:     send_cmo(oxfp, c->ringdef);
        !           532:     while(!list_endof((cmo_list *)c, el)) {
        !           533:         send_cmo(oxfp, el->cmo);
        !           534:         el = list_next(el);
1.1       ohara     535:     }
                    536:     return 0;
                    537: }
                    538:
1.13    ! ohara     539: static int send_cmo_monomial32(OXFILE *oxfp, cmo_monomial32* c)
1.1       ohara     540: {
                    541:     int i;
                    542:     int len = c->length;
1.13    ! ohara     543:     send_int32(oxfp, len);
1.1       ohara     544:     for(i=0; i<len; i++) {
1.13    ! ohara     545:         send_int32(oxfp, c->exps[i]);
1.1       ohara     546:     }
1.13    ! ohara     547:     send_cmo(oxfp, c->coef);
1.1       ohara     548:     return 0;
                    549: }
                    550:
1.13    ! ohara     551: static int send_cmo_zz(OXFILE *oxfp, cmo_zz* c)
1.1       ohara     552: {
1.13    ! ohara     553:     send_mpz(oxfp, c->mpz);
1.1       ohara     554:     return 0;
                    555: }
                    556:
1.13    ! ohara     557: static int send_cmo_error2(OXFILE *oxfp, cmo_error2* c)
1.1       ohara     558: {
1.13    ! ohara     559:     send_cmo(oxfp, c->ob);
1.1       ohara     560:     return 0;
                    561: }
                    562:
1.8       ohara     563: /* sending a CMO.  (Remarks: OX_tag is already sent.) */
1.13    ! ohara     564: void send_cmo(OXFILE *oxfp, cmo* c)
1.1       ohara     565: {
                    566:     int tag = c->tag;
                    567:
1.13    ! ohara     568:     c = call_hook_before_send_cmo(oxfp, c);
1.2       ohara     569:
1.13    ! ohara     570:     send_int32(oxfp, tag);
1.1       ohara     571:     switch(tag) {
                    572:     case CMO_NULL:
                    573:     case CMO_ZERO:
                    574:     case CMO_DMS_GENERIC:
1.13    ! ohara     575:         send_cmo_null(oxfp, c);  /* empty function. */
1.1       ohara     576:         break;
                    577:     case CMO_INT32:
1.13    ! ohara     578:         send_cmo_int32(oxfp, (cmo_int32 *)c);
1.1       ohara     579:         break;
                    580:     case CMO_STRING:
1.13    ! ohara     581:         send_cmo_string(oxfp, (cmo_string *)c);
1.1       ohara     582:         break;
                    583:     case CMO_MATHCAP:
                    584:     case CMO_ERROR2:
                    585:     case CMO_RING_BY_NAME:
                    586:     case CMO_INDETERMINATE:
1.13    ! ohara     587:         send_cmo_mathcap(oxfp, (cmo_mathcap *)c);
1.1       ohara     588:         break;
                    589:     case CMO_LIST:
1.13    ! ohara     590:         send_cmo_list(oxfp, (cmo_list *)c);
1.1       ohara     591:         break;
                    592:     case CMO_MONOMIAL32:
1.13    ! ohara     593:         send_cmo_monomial32(oxfp, (cmo_monomial32 *)c);
1.1       ohara     594:         break;
                    595:     case CMO_ZZ:
1.13    ! ohara     596:         send_cmo_zz(oxfp, (cmo_zz *)c);
1.1       ohara     597:         break;
                    598:     case CMO_DISTRIBUTED_POLYNOMIAL:
1.13    ! ohara     599:         send_cmo_distributed_polynomial(oxfp, (cmo_distributed_polynomial *)c);
1.1       ohara     600:         break;
                    601:     default:
1.13    ! ohara     602:         call_hook_after_send_cmo(oxfp, c);
1.1       ohara     603:     }
                    604: }
                    605:
1.13    ! ohara     606: static int send_mpz(OXFILE *oxfp, mpz_ptr mpz)
1.1       ohara     607: {
                    608:     int i;
                    609:     int len = abs(mpz->_mp_size);
1.13    ! ohara     610:     send_int32(oxfp, mpz->_mp_size);
1.1       ohara     611:     for(i=0; i<len; i++) {
1.13    ! ohara     612:         send_int32(oxfp, mpz->_mp_d[i]);
1.1       ohara     613:     }
                    614:     return 0;
                    615: }
                    616:
                    617: ox_data* new_ox_data(cmo* c)
                    618: {
                    619:     ox_data* m = malloc(sizeof(ox_data));
                    620:     m->tag = OX_DATA;
                    621:     m->cmo = c;
                    622:     return m;
                    623: }
                    624:
                    625: ox_command* new_ox_command(int sm_code)
                    626: {
                    627:     ox_command* m = malloc(sizeof(ox_command));
                    628:     m->tag = OX_COMMAND;
                    629:     m->command = sm_code;
                    630:     return m;
                    631: }
                    632:
                    633: ox_sync_ball* new_ox_sync_ball()
                    634: {
                    635:     ox_sync_ball *m = malloc(sizeof(ox_sync_ball));
                    636:     m->tag = OX_SYNC_BALL;
                    637:     return m;
                    638: }

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