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

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

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