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

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

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