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

1.1       ohara       1: /* -*- mode: C; coding: euc-japan -*- */
1.30    ! iwane       2: /* $OpenXM: OpenXM/src/ox_toolkit/ox.c,v 1.29 2003/09/18 20:30:00 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: {
1.27      ohara     216:        cmo_list* ringdef = (cmo_list *)receive_cmo(oxfp);
                    217:        cmo* coef         = receive_cmo(oxfp);
1.26      ohara     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);
1.27      ohara     247:         list_append_monomial((cmo_list *)c, coef, exp);
1.26      ohara     248:         len--;
                    249:     }
                    250:     return c;
                    251: }
                    252:
1.29      ohara     253: static cmo_indeterminate* receive_cmo_indeterminate(OXFILE *oxfp)
                    254: {
                    255:     cmo* ob = receive_cmo(oxfp);
                    256:     return new_cmo_indeterminate(ob);
                    257: }
                    258:
1.28      ohara     259: static cmo_tree* receive_cmo_tree(OXFILE *oxfp)
                    260: {
                    261:     cmo_string* name = (cmo_string *)receive_cmo(oxfp);
                    262:     cmo_list* attrib = (cmo_list *)receive_cmo(oxfp);
                    263:     cmo_list* leaves = (cmo_list *)receive_cmo(oxfp);
                    264:     return new_cmo_tree(name, attrib, leaves);
                    265: }
                    266:
                    267: static cmo_lambda* receive_cmo_lambda(OXFILE *oxfp)
                    268: {
                    269:     cmo_list* args = (cmo_list *)receive_cmo(oxfp);
                    270:     cmo_tree* body = (cmo_tree *)receive_cmo(oxfp);
                    271:     return new_cmo_lambda(args, body);
                    272: }
                    273:
1.13      ohara     274: static cmo_error2* receive_cmo_error2(OXFILE *oxfp)
1.1       ohara     275: {
1.13      ohara     276:     cmo* ob = receive_cmo(oxfp);
1.1       ohara     277:     return new_cmo_error2(ob);
                    278: }
                    279:
1.17      ohara     280: /* receive_cmo() is called after receive_ox_tag(). */
1.13      ohara     281: cmo* receive_cmo(OXFILE *oxfp)
1.1       ohara     282: {
1.24      ohara     283:     int tag = receive_int32(oxfp);
                    284:     return receive_cmo_tag(oxfp, tag);
                    285: }
                    286:
                    287: cmo *receive_cmo_tag(OXFILE *oxfp, int tag)
                    288: {
1.1       ohara     289:     cmo* m;
                    290:     switch(tag) {
                    291:     case CMO_NULL:
1.13      ohara     292:         m = receive_cmo_null(oxfp);
1.1       ohara     293:         break;
                    294:     case CMO_INT32:
1.13      ohara     295:         m = (cmo *)receive_cmo_int32(oxfp);
1.1       ohara     296:         break;
                    297:     case CMO_STRING:
1.13      ohara     298:         m = (cmo *)receive_cmo_string(oxfp);
1.1       ohara     299:         break;
                    300:     case CMO_MATHCAP:
1.13      ohara     301:         m = (cmo *)receive_cmo_mathcap(oxfp);
1.1       ohara     302:         break;
                    303:     case CMO_LIST:
1.13      ohara     304:         m = (cmo *)receive_cmo_list(oxfp);
1.1       ohara     305:         break;
                    306:     case CMO_MONOMIAL32:
1.13      ohara     307:         m = (cmo *)receive_cmo_monomial32(oxfp);
1.1       ohara     308:         break;
                    309:     case CMO_ZZ:
1.13      ohara     310:         m = (cmo *)receive_cmo_zz(oxfp);
1.1       ohara     311:         break;
                    312:     case CMO_ZERO:
1.13      ohara     313:         m = (cmo *)receive_cmo_zero(oxfp);
1.1       ohara     314:         break;
                    315:     case CMO_DMS_GENERIC:
1.13      ohara     316:         m = (cmo *)receive_cmo_dms_generic(oxfp);
1.1       ohara     317:         break;
                    318:     case CMO_RING_BY_NAME:
1.13      ohara     319:         m = (cmo *)receive_cmo_ring_by_name(oxfp);
1.1       ohara     320:         break;
                    321:     case CMO_DISTRIBUTED_POLYNOMIAL:
1.13      ohara     322:         m = (cmo *)receive_cmo_distributed_polynomial(oxfp);
1.1       ohara     323:         break;
1.26      ohara     324:     case CMO_RECURSIVE_POLYNOMIAL:
                    325:         m = (cmo *)receive_cmo_recursive_polynomial(oxfp);
                    326:         break;
                    327:     case CMO_POLYNOMIAL_IN_ONE_VARIABLE:
                    328:         m = (cmo *)receive_cmo_polynomial_in_one_variable(oxfp);
1.29      ohara     329:         break;
                    330:     case CMO_INDETERMINATE:
                    331:         m = (cmo *)receive_cmo_indeterminate(oxfp);
1.28      ohara     332:         break;
                    333:     case CMO_TREE:
                    334:         m = (cmo *)receive_cmo_tree(oxfp);
                    335:         break;
                    336:     case CMO_LAMBDA:
                    337:         m = (cmo *)receive_cmo_lambda(oxfp);
                    338:         break;
1.1       ohara     339:     case CMO_ERROR2:
1.13      ohara     340:         m = (cmo *)receive_cmo_error2(oxfp);
1.1       ohara     341:         break;
                    342:     case CMO_DATUM:
                    343:     case CMO_QQ:
                    344:     default:
1.13      ohara     345:         m = NULL;
1.20      ohara     346:         ox_printf("the CMO (%d) is not implemented.\n", tag);
1.1       ohara     347:     }
                    348:     return m;
                    349: }
                    350:
1.13      ohara     351: static void receive_mpz(OXFILE *oxfp, mpz_ptr mpz)
1.1       ohara     352: {
                    353:     int i;
1.13      ohara     354:     int size  = receive_int32(oxfp);
1.1       ohara     355:     int len   = abs(size);
                    356:     resize_mpz(mpz, size);
                    357:
                    358:     for(i=0; i<len; i++) {
1.13      ohara     359:         mpz->_mp_d[i] = receive_int32(oxfp);
1.1       ohara     360:     }
                    361: }
                    362:
1.13      ohara     363: void send_ox_command(OXFILE *oxfp, int sm_command)
1.1       ohara     364: {
1.13      ohara     365:     send_ox_tag(oxfp, OX_COMMAND);
                    366:     send_int32(oxfp, sm_command);
                    367:     oxf_flush(oxfp);
1.1       ohara     368: }
                    369:
1.13      ohara     370: void ox_close(OXFILE *sv)
1.1       ohara     371: {
1.13      ohara     372:     send_ox_command(oxf_control(sv), SM_control_kill);
1.7       ohara     373:     sleep(2);
1.13      ohara     374:     /* We wait thar an OpenXM server terminates. */
1.20      ohara     375:     ox_printf("I have closed the connection to an Open XM server.\n");
1.1       ohara     376: }
                    377:
1.13      ohara     378: void ox_shutdown(OXFILE *sv)
1.3       ohara     379: {
1.13      ohara     380:     /* We need to use SM_shutdown but yet ... */
                    381:     ox_close(sv);
1.3       ohara     382: }
                    383:
1.13      ohara     384: void ox_cmo_rpc(OXFILE *sv, char *function, int argc, cmo *argv[])
1.3       ohara     385: {
1.13      ohara     386:     int i = argc;
                    387:     while(i-- > 0) {
                    388:         send_ox_cmo(sv, argv[i]);
                    389:     }
                    390:     send_ox_cmo(sv, (cmo *)new_cmo_int32(argc));
                    391:     send_ox_cmo(sv, (cmo *)new_cmo_string(function));
                    392:     send_ox_command(sv, SM_executeFunction);
1.3       ohara     393: }
                    394:
1.13      ohara     395: void ox_execute_string(OXFILE *sv, char* s)
1.3       ohara     396: {
1.13      ohara     397:     send_ox_cmo(sv, (cmo *)new_cmo_string(s));
                    398:     send_ox_command(sv, SM_executeStringByLocalParser);
1.3       ohara     399: }
                    400:
1.13      ohara     401: void ox_push_cmd(OXFILE *sv, int sm_code)
1.1       ohara     402: {
1.13      ohara     403:     send_ox_command(sv, sm_code);
1.1       ohara     404: }
                    405:
1.13      ohara     406: cmo_mathcap* ox_mathcap(OXFILE *sv)
1.1       ohara     407: {
1.13      ohara     408:     send_ox_command(sv, SM_mathcap);
                    409:     send_ox_command(sv, SM_popCMO);
                    410:     receive_ox_tag(sv);          /* OX_DATA */
                    411:     return (cmo_mathcap *)receive_cmo(sv);
1.1       ohara     412: }
                    413:
1.13      ohara     414: char* ox_popString(OXFILE *sv)
1.1       ohara     415: {
                    416:     cmo_string* m = NULL;
                    417:
1.13      ohara     418:     send_ox_command(sv, SM_popString);
                    419:     receive_ox_tag(sv); /* OX_DATA */
                    420:     m = (cmo_string *)receive_cmo(sv);
1.1       ohara     421:     return m->s;
                    422: }
                    423:
1.13      ohara     424: void ox_pops(OXFILE *sv, int num)
1.3       ohara     425: {
1.13      ohara     426:     send_ox_cmo(sv, (cmo *)new_cmo_int32(num));
                    427:     send_ox_command(sv, SM_pops);
1.3       ohara     428: }
                    429:
1.13      ohara     430: cmo* ox_pop_cmo(OXFILE *sv)
1.3       ohara     431: {
1.13      ohara     432:     send_ox_command(sv, SM_popCMO);
                    433:     receive_ox_tag(sv); /* OX_DATA */
                    434:     return receive_cmo(sv);
1.3       ohara     435: }
                    436:
1.13      ohara     437: void ox_push_cmo(OXFILE *sv, cmo *c)
1.3       ohara     438: {
1.13      ohara     439:     send_ox_cmo(sv, c);
1.3       ohara     440: }
                    441:
1.7       ohara     442: /* a dummy function for flushing a connection. */
1.13      ohara     443: int ox_flush(OXFILE *sv)
1.5       ohara     444: {
1.13      ohara     445:     return 1;
1.5       ohara     446: }
                    447:
1.13      ohara     448: void ox_reset(OXFILE *sv)
1.1       ohara     449: {
1.30    ! iwane     450:     int tag;
1.13      ohara     451:     send_ox_command(oxf_control(sv), SM_control_reset_connection);
1.30    ! iwane     452:     while((tag = receive_ox_tag(sv)) != OX_SYNC_BALL) {
        !           453:         if (tag == OX_DATA) {
        !           454:           receive_cmo(sv); /* skipping a message. */
        !           455:         }
1.1       ohara     456:     }
                    457:
1.13      ohara     458:     send_ox_tag(sv, OX_SYNC_BALL);
1.20      ohara     459:     ox_printf("I have reset an Open XM server.\n");
1.1       ohara     460: }
                    461:
1.13      ohara     462: void send_ox(OXFILE *oxfp, ox *m)
1.1       ohara     463: {
                    464:     switch(m->tag) {
1.13      ohara     465:     case OX_DATA:
                    466:         send_ox_cmo(oxfp, ((ox_data *)m)->cmo);
1.1       ohara     467:         break;
1.13      ohara     468:     case OX_COMMAND:
                    469:         send_ox_command(oxfp, ((ox_command *)m)->command);
1.1       ohara     470:         break;
                    471:     default:
                    472:         /* CMO?? */
1.13      ohara     473:         send_ox_cmo(oxfp, (cmo *)m);
1.1       ohara     474:     }
                    475: }
                    476:
1.13      ohara     477: void send_ox_cmo(OXFILE *oxfp, cmo* m)
1.1       ohara     478: {
1.13      ohara     479:     send_ox_tag(oxfp, OX_DATA);
                    480:     send_cmo(oxfp, m);
                    481:     oxf_flush(oxfp);
1.1       ohara     482: }
                    483:
1.8       ohara     484: /* send_cmo_* functions */
1.13      ohara     485: static int send_cmo_null(OXFILE *oxfp, cmo_null* c)
1.1       ohara     486: {
                    487:     return 0;
                    488: }
                    489:
1.13      ohara     490: static int send_cmo_int32(OXFILE *oxfp, cmo_int32* m)
1.1       ohara     491: {
1.13      ohara     492:     return send_int32(oxfp, m->i);
1.1       ohara     493: }
                    494:
1.13      ohara     495: static int send_cmo_string(OXFILE *oxfp, cmo_string* m)
1.1       ohara     496: {
                    497:     int len = (m->s != NULL)? strlen(m->s): 0;
1.13      ohara     498:     send_int32(oxfp, len);
1.1       ohara     499:     if (len > 0) {
1.13      ohara     500:         oxf_write(m->s, 1, len, oxfp);
1.1       ohara     501:     }
                    502:     return 0;
                    503: }
                    504:
1.13      ohara     505: static int send_cmo_mathcap(OXFILE *oxfp, cmo_mathcap* c)
1.1       ohara     506: {
1.13      ohara     507:     send_cmo(oxfp, c->ob);
1.1       ohara     508:     return 0;
                    509: }
                    510:
1.13      ohara     511: static int send_cmo_list(OXFILE *oxfp, cmo_list* c)
1.1       ohara     512: {
1.13      ohara     513:     cell* el = list_first(c);
                    514:     int len = list_length(c);
                    515:     send_int32(oxfp, len);
                    516:
                    517:     while(!list_endof(c, el)) {
                    518:         send_cmo(oxfp, el->cmo);
                    519:         el = list_next(el);
1.1       ohara     520:     }
                    521:     return 0;
                    522: }
                    523:
1.13      ohara     524: static int send_cmo_distributed_polynomial(OXFILE *oxfp, cmo_distributed_polynomial* c)
1.1       ohara     525: {
1.13      ohara     526:     cell* el = list_first((cmo_list *)c);
                    527:     int len = list_length((cmo_list *)c);
                    528:     send_int32(oxfp, len);
                    529:     send_cmo(oxfp, c->ringdef);
                    530:     while(!list_endof((cmo_list *)c, el)) {
                    531:         send_cmo(oxfp, el->cmo);
                    532:         el = list_next(el);
1.1       ohara     533:     }
                    534:     return 0;
                    535: }
                    536:
1.26      ohara     537: static int send_cmo_polynomial_in_one_variable(OXFILE *oxfp, cmo_polynomial_in_one_variable* c)
                    538: {
1.27      ohara     539:     cell* el = list_first((cmo_list *)c);
                    540:     int len = list_length((cmo_list *)c);
1.26      ohara     541:     send_int32(oxfp, len);
                    542:        send_int32(oxfp, c->var);
                    543:
1.27      ohara     544:     while(!list_endof((cmo_list *)c, el)) {
                    545:         send_int32(oxfp, el->exp);
1.26      ohara     546:         send_cmo(oxfp, el->cmo);
                    547:         el = list_next(el);
                    548:     }
                    549:     return 0;
                    550: }
                    551:
1.13      ohara     552: static int send_cmo_monomial32(OXFILE *oxfp, cmo_monomial32* c)
1.1       ohara     553: {
                    554:     int i;
                    555:     int len = c->length;
1.13      ohara     556:     send_int32(oxfp, len);
1.1       ohara     557:     for(i=0; i<len; i++) {
1.13      ohara     558:         send_int32(oxfp, c->exps[i]);
1.1       ohara     559:     }
1.13      ohara     560:     send_cmo(oxfp, c->coef);
1.1       ohara     561:     return 0;
                    562: }
                    563:
1.13      ohara     564: static int send_cmo_zz(OXFILE *oxfp, cmo_zz* c)
1.1       ohara     565: {
1.13      ohara     566:     send_mpz(oxfp, c->mpz);
1.1       ohara     567:     return 0;
                    568: }
                    569:
1.26      ohara     570: static int send_cmo_recursive_polynomial(OXFILE *oxfp, cmo_recursive_polynomial* c)
                    571: {
1.27      ohara     572:        send_cmo(oxfp, (cmo *)c->ringdef);
1.26      ohara     573:     send_cmo(oxfp, c->coef);
                    574:     return 0;
                    575: }
                    576:
1.28      ohara     577: static int send_cmo_tree(OXFILE *oxfp, cmo_tree *c)
                    578: {
                    579:     send_cmo(oxfp, (cmo *)c->name);
                    580:     send_cmo(oxfp, (cmo *)c->attributes);
                    581:     send_cmo(oxfp, (cmo *)c->leaves);
                    582:     return 0;
                    583: }
                    584:
                    585: static int send_cmo_lambda(OXFILE *oxfp, cmo_lambda *c)
                    586: {
                    587:     send_cmo(oxfp, (cmo *)c->args);
                    588:     send_cmo(oxfp, (cmo *)c->body);
                    589:     return 0;
                    590: }
                    591:
1.13      ohara     592: static int send_cmo_error2(OXFILE *oxfp, cmo_error2* c)
1.1       ohara     593: {
1.13      ohara     594:     send_cmo(oxfp, c->ob);
1.1       ohara     595:     return 0;
                    596: }
                    597:
1.8       ohara     598: /* sending a CMO.  (Remarks: OX_tag is already sent.) */
1.13      ohara     599: void send_cmo(OXFILE *oxfp, cmo* c)
1.1       ohara     600: {
                    601:     int tag = c->tag;
                    602:
1.13      ohara     603:     c = call_hook_before_send_cmo(oxfp, c);
1.2       ohara     604:
1.13      ohara     605:     send_int32(oxfp, tag);
1.1       ohara     606:     switch(tag) {
                    607:     case CMO_NULL:
                    608:     case CMO_ZERO:
                    609:     case CMO_DMS_GENERIC:
1.13      ohara     610:         send_cmo_null(oxfp, c);  /* empty function. */
1.1       ohara     611:         break;
                    612:     case CMO_INT32:
1.13      ohara     613:         send_cmo_int32(oxfp, (cmo_int32 *)c);
1.1       ohara     614:         break;
                    615:     case CMO_STRING:
1.13      ohara     616:         send_cmo_string(oxfp, (cmo_string *)c);
1.1       ohara     617:         break;
                    618:     case CMO_MATHCAP:
                    619:     case CMO_ERROR2:
                    620:     case CMO_RING_BY_NAME:
                    621:     case CMO_INDETERMINATE:
1.13      ohara     622:         send_cmo_mathcap(oxfp, (cmo_mathcap *)c);
1.1       ohara     623:         break;
                    624:     case CMO_LIST:
1.13      ohara     625:         send_cmo_list(oxfp, (cmo_list *)c);
1.1       ohara     626:         break;
                    627:     case CMO_MONOMIAL32:
1.13      ohara     628:         send_cmo_monomial32(oxfp, (cmo_monomial32 *)c);
1.1       ohara     629:         break;
                    630:     case CMO_ZZ:
1.13      ohara     631:         send_cmo_zz(oxfp, (cmo_zz *)c);
1.1       ohara     632:         break;
                    633:     case CMO_DISTRIBUTED_POLYNOMIAL:
1.13      ohara     634:         send_cmo_distributed_polynomial(oxfp, (cmo_distributed_polynomial *)c);
1.26      ohara     635:         break;
                    636:     case CMO_RECURSIVE_POLYNOMIAL:
                    637:         send_cmo_recursive_polynomial(oxfp, (cmo_recursive_polynomial *)c);
                    638:         break;
                    639:     case CMO_POLYNOMIAL_IN_ONE_VARIABLE:
                    640:         send_cmo_polynomial_in_one_variable(oxfp, (cmo_polynomial_in_one_variable *)c);
1.28      ohara     641:         break;
                    642:     case CMO_TREE:
                    643:         send_cmo_tree(oxfp, (cmo_tree *)c);
                    644:         break;
                    645:     case CMO_LAMBDA:
                    646:         send_cmo_lambda(oxfp, (cmo_lambda *)c);
1.1       ohara     647:         break;
                    648:     default:
1.13      ohara     649:         call_hook_after_send_cmo(oxfp, c);
1.1       ohara     650:     }
                    651: }
                    652:
1.13      ohara     653: static int send_mpz(OXFILE *oxfp, mpz_ptr mpz)
1.1       ohara     654: {
                    655:     int i;
                    656:     int len = abs(mpz->_mp_size);
1.13      ohara     657:     send_int32(oxfp, mpz->_mp_size);
1.1       ohara     658:     for(i=0; i<len; i++) {
1.13      ohara     659:         send_int32(oxfp, mpz->_mp_d[i]);
1.1       ohara     660:     }
                    661:     return 0;
                    662: }
                    663:
                    664: ox_data* new_ox_data(cmo* c)
                    665: {
1.25      ohara     666:     ox_data* m = MALLOC(sizeof(ox_data));
1.1       ohara     667:     m->tag = OX_DATA;
                    668:     m->cmo = c;
                    669:     return m;
                    670: }
                    671:
                    672: ox_command* new_ox_command(int sm_code)
                    673: {
1.25      ohara     674:     ox_command* m = MALLOC(sizeof(ox_command));
1.1       ohara     675:     m->tag = OX_COMMAND;
                    676:     m->command = sm_code;
                    677:     return m;
                    678: }
                    679:
                    680: ox_sync_ball* new_ox_sync_ball()
                    681: {
1.25      ohara     682:     ox_sync_ball *m = MALLOC(sizeof(ox_sync_ball));
1.1       ohara     683:     m->tag = OX_SYNC_BALL;
                    684:     return m;
1.19      ohara     685: }
                    686:
                    687: int ox_stderr_init(FILE *fp)
                    688: {
1.21      ohara     689:     ox_stderr = fp;
                    690:     if (ox_stderr != NULL) {
                    691:         setbuf(ox_stderr, NULL);
                    692:     }
1.27      ohara     693:     return 0;
1.20      ohara     694: }
                    695:
                    696: int ox_printf(char *format, ...)
                    697: {
1.21      ohara     698:     if (ox_stderr != NULL) {
                    699:         va_list ap;
                    700:         va_start(ap, format);
                    701:         vfprintf(ox_stderr, format, ap);
                    702:     }
1.27      ohara     703:     return 0;
1.1       ohara     704: }

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