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

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

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