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

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

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