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

1.1       ohara       1: /* -*- mode: C; coding: euc-japan -*- */
1.37    ! iwane       2: /* $OpenXM: OpenXM/src/ox_toolkit/ox.c,v 1.36 2013/10/20 15:29:12 iwane 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.36      iwane     390:     int n = sizeof(mpz->_mp_d[0]) / sizeof(int);
1.13      ohara     391:     int size  = receive_int32(oxfp);
1.1       ohara     392:     int len   = abs(size);
1.36      iwane     393:     int *ptr;
                    394:     if (n == 1) {
                    395:         resize_mpz(mpz, size);
1.37    ! iwane     396:     } else if (size >= 0) {
        !           397:         resize_mpz(mpz, (size+1) / n);
1.36      iwane     398:     } else {
1.37    ! iwane     399:         resize_mpz(mpz, (size-1) / n);
1.36      iwane     400:     }
                    401:
                    402:     ptr = (int *)mpz->_mp_d;
                    403:     for(i=0; i<len; i++) {
                    404:         ptr[i] = receive_int32(oxfp);
                    405:     }
1.1       ohara     406: }
                    407:
1.13      ohara     408: void send_ox_command(OXFILE *oxfp, int sm_command)
1.1       ohara     409: {
1.13      ohara     410:     send_ox_tag(oxfp, OX_COMMAND);
                    411:     send_int32(oxfp, sm_command);
                    412:     oxf_flush(oxfp);
1.1       ohara     413: }
                    414:
1.13      ohara     415: void ox_close(OXFILE *sv)
1.1       ohara     416: {
1.13      ohara     417:     send_ox_command(oxf_control(sv), SM_control_kill);
1.7       ohara     418:     sleep(2);
1.13      ohara     419:     /* We wait thar an OpenXM server terminates. */
1.20      ohara     420:     ox_printf("I have closed the connection to an Open XM server.\n");
1.1       ohara     421: }
                    422:
1.13      ohara     423: void ox_shutdown(OXFILE *sv)
1.3       ohara     424: {
1.13      ohara     425:     /* We need to use SM_shutdown but yet ... */
                    426:     ox_close(sv);
1.3       ohara     427: }
                    428:
1.13      ohara     429: void ox_cmo_rpc(OXFILE *sv, char *function, int argc, cmo *argv[])
1.3       ohara     430: {
1.13      ohara     431:     int i = argc;
                    432:     while(i-- > 0) {
                    433:         send_ox_cmo(sv, argv[i]);
                    434:     }
                    435:     send_ox_cmo(sv, (cmo *)new_cmo_int32(argc));
                    436:     send_ox_cmo(sv, (cmo *)new_cmo_string(function));
                    437:     send_ox_command(sv, SM_executeFunction);
1.3       ohara     438: }
                    439:
1.13      ohara     440: void ox_execute_string(OXFILE *sv, char* s)
1.3       ohara     441: {
1.13      ohara     442:     send_ox_cmo(sv, (cmo *)new_cmo_string(s));
                    443:     send_ox_command(sv, SM_executeStringByLocalParser);
1.3       ohara     444: }
                    445:
1.13      ohara     446: void ox_push_cmd(OXFILE *sv, int sm_code)
1.1       ohara     447: {
1.13      ohara     448:     send_ox_command(sv, sm_code);
1.1       ohara     449: }
                    450:
1.13      ohara     451: cmo_mathcap* ox_mathcap(OXFILE *sv)
1.1       ohara     452: {
1.13      ohara     453:     send_ox_command(sv, SM_mathcap);
                    454:     send_ox_command(sv, SM_popCMO);
                    455:     receive_ox_tag(sv);          /* OX_DATA */
                    456:     return (cmo_mathcap *)receive_cmo(sv);
1.1       ohara     457: }
                    458:
1.13      ohara     459: char* ox_popString(OXFILE *sv)
1.1       ohara     460: {
                    461:     cmo_string* m = NULL;
                    462:
1.13      ohara     463:     send_ox_command(sv, SM_popString);
                    464:     receive_ox_tag(sv); /* OX_DATA */
                    465:     m = (cmo_string *)receive_cmo(sv);
1.1       ohara     466:     return m->s;
                    467: }
                    468:
1.13      ohara     469: void ox_pops(OXFILE *sv, int num)
1.3       ohara     470: {
1.13      ohara     471:     send_ox_cmo(sv, (cmo *)new_cmo_int32(num));
                    472:     send_ox_command(sv, SM_pops);
1.3       ohara     473: }
                    474:
1.13      ohara     475: cmo* ox_pop_cmo(OXFILE *sv)
1.3       ohara     476: {
1.13      ohara     477:     send_ox_command(sv, SM_popCMO);
                    478:     receive_ox_tag(sv); /* OX_DATA */
                    479:     return receive_cmo(sv);
1.3       ohara     480: }
                    481:
1.13      ohara     482: void ox_push_cmo(OXFILE *sv, cmo *c)
1.3       ohara     483: {
1.13      ohara     484:     send_ox_cmo(sv, c);
1.3       ohara     485: }
                    486:
1.7       ohara     487: /* a dummy function for flushing a connection. */
1.13      ohara     488: int ox_flush(OXFILE *sv)
1.5       ohara     489: {
1.13      ohara     490:     return 1;
1.5       ohara     491: }
                    492:
1.13      ohara     493: void ox_reset(OXFILE *sv)
1.1       ohara     494: {
1.13      ohara     495:     send_ox_command(oxf_control(sv), SM_control_reset_connection);
1.31      iwane     496:     while(receive_ox_tag(sv) != OX_SYNC_BALL) {
                    497:         receive_cmo(sv); /* skipping a message. */
1.1       ohara     498:     }
                    499:
1.13      ohara     500:     send_ox_tag(sv, OX_SYNC_BALL);
1.20      ohara     501:     ox_printf("I have reset an Open XM server.\n");
1.1       ohara     502: }
                    503:
1.13      ohara     504: void send_ox(OXFILE *oxfp, ox *m)
1.1       ohara     505: {
                    506:     switch(m->tag) {
1.13      ohara     507:     case OX_DATA:
                    508:         send_ox_cmo(oxfp, ((ox_data *)m)->cmo);
1.1       ohara     509:         break;
1.13      ohara     510:     case OX_COMMAND:
                    511:         send_ox_command(oxfp, ((ox_command *)m)->command);
1.1       ohara     512:         break;
                    513:     default:
                    514:         /* CMO?? */
1.13      ohara     515:         send_ox_cmo(oxfp, (cmo *)m);
1.1       ohara     516:     }
                    517: }
                    518:
1.13      ohara     519: void send_ox_cmo(OXFILE *oxfp, cmo* m)
1.1       ohara     520: {
1.13      ohara     521:     send_ox_tag(oxfp, OX_DATA);
                    522:     send_cmo(oxfp, m);
                    523:     oxf_flush(oxfp);
1.1       ohara     524: }
                    525:
1.8       ohara     526: /* send_cmo_* functions */
1.13      ohara     527: static int send_cmo_null(OXFILE *oxfp, cmo_null* c)
1.1       ohara     528: {
                    529:     return 0;
                    530: }
                    531:
1.13      ohara     532: static int send_cmo_int32(OXFILE *oxfp, cmo_int32* m)
1.1       ohara     533: {
1.13      ohara     534:     return send_int32(oxfp, m->i);
1.1       ohara     535: }
                    536:
1.13      ohara     537: static int send_cmo_string(OXFILE *oxfp, cmo_string* m)
1.1       ohara     538: {
                    539:     int len = (m->s != NULL)? strlen(m->s): 0;
1.13      ohara     540:     send_int32(oxfp, len);
1.1       ohara     541:     if (len > 0) {
1.13      ohara     542:         oxf_write(m->s, 1, len, oxfp);
1.1       ohara     543:     }
                    544:     return 0;
                    545: }
                    546:
1.13      ohara     547: static int send_cmo_mathcap(OXFILE *oxfp, cmo_mathcap* c)
1.1       ohara     548: {
1.13      ohara     549:     send_cmo(oxfp, c->ob);
1.1       ohara     550:     return 0;
                    551: }
                    552:
1.13      ohara     553: static int send_cmo_list(OXFILE *oxfp, cmo_list* c)
1.1       ohara     554: {
1.13      ohara     555:     cell* el = list_first(c);
                    556:     int len = list_length(c);
                    557:     send_int32(oxfp, len);
                    558:
                    559:     while(!list_endof(c, el)) {
                    560:         send_cmo(oxfp, el->cmo);
                    561:         el = list_next(el);
1.1       ohara     562:     }
                    563:     return 0;
                    564: }
                    565:
1.13      ohara     566: static int send_cmo_distributed_polynomial(OXFILE *oxfp, cmo_distributed_polynomial* c)
1.1       ohara     567: {
1.13      ohara     568:     cell* el = list_first((cmo_list *)c);
                    569:     int len = list_length((cmo_list *)c);
                    570:     send_int32(oxfp, len);
                    571:     send_cmo(oxfp, c->ringdef);
                    572:     while(!list_endof((cmo_list *)c, el)) {
                    573:         send_cmo(oxfp, el->cmo);
                    574:         el = list_next(el);
1.1       ohara     575:     }
                    576:     return 0;
                    577: }
                    578:
1.26      ohara     579: static int send_cmo_polynomial_in_one_variable(OXFILE *oxfp, cmo_polynomial_in_one_variable* c)
                    580: {
1.27      ohara     581:     cell* el = list_first((cmo_list *)c);
                    582:     int len = list_length((cmo_list *)c);
1.26      ohara     583:     send_int32(oxfp, len);
                    584:        send_int32(oxfp, c->var);
                    585:
1.27      ohara     586:     while(!list_endof((cmo_list *)c, el)) {
                    587:         send_int32(oxfp, el->exp);
1.26      ohara     588:         send_cmo(oxfp, el->cmo);
                    589:         el = list_next(el);
                    590:     }
                    591:     return 0;
                    592: }
                    593:
1.32      ohara     594: static int send_cmo_double(OXFILE *oxfp, cmo_double* c)
                    595: {
                    596:     return send_double(oxfp, c->d);
                    597: }
                    598:
1.13      ohara     599: static int send_cmo_monomial32(OXFILE *oxfp, cmo_monomial32* c)
1.1       ohara     600: {
                    601:     int i;
                    602:     int len = c->length;
1.13      ohara     603:     send_int32(oxfp, len);
1.1       ohara     604:     for(i=0; i<len; i++) {
1.13      ohara     605:         send_int32(oxfp, c->exps[i]);
1.1       ohara     606:     }
1.13      ohara     607:     send_cmo(oxfp, c->coef);
1.1       ohara     608:     return 0;
                    609: }
                    610:
1.13      ohara     611: static int send_cmo_zz(OXFILE *oxfp, cmo_zz* c)
1.1       ohara     612: {
1.13      ohara     613:     send_mpz(oxfp, c->mpz);
1.1       ohara     614:     return 0;
                    615: }
                    616:
1.33      ohara     617: static int send_cmo_qq(OXFILE *oxfp, cmo_qq* c)
                    618: {
1.34      ohara     619:     send_mpz(oxfp, mpq_numref(c->mpq));
                    620:     send_mpz(oxfp, mpq_denref(c->mpq));
1.33      ohara     621:     return 0;
                    622: }
                    623:
1.26      ohara     624: static int send_cmo_recursive_polynomial(OXFILE *oxfp, cmo_recursive_polynomial* c)
                    625: {
1.27      ohara     626:        send_cmo(oxfp, (cmo *)c->ringdef);
1.26      ohara     627:     send_cmo(oxfp, c->coef);
                    628:     return 0;
                    629: }
                    630:
1.28      ohara     631: static int send_cmo_tree(OXFILE *oxfp, cmo_tree *c)
                    632: {
                    633:     send_cmo(oxfp, (cmo *)c->name);
                    634:     send_cmo(oxfp, (cmo *)c->attributes);
                    635:     send_cmo(oxfp, (cmo *)c->leaves);
                    636:     return 0;
                    637: }
                    638:
                    639: static int send_cmo_lambda(OXFILE *oxfp, cmo_lambda *c)
                    640: {
                    641:     send_cmo(oxfp, (cmo *)c->args);
                    642:     send_cmo(oxfp, (cmo *)c->body);
                    643:     return 0;
                    644: }
                    645:
1.13      ohara     646: static int send_cmo_error2(OXFILE *oxfp, cmo_error2* c)
1.1       ohara     647: {
1.13      ohara     648:     send_cmo(oxfp, c->ob);
1.1       ohara     649:     return 0;
                    650: }
                    651:
1.8       ohara     652: /* sending a CMO.  (Remarks: OX_tag is already sent.) */
1.13      ohara     653: void send_cmo(OXFILE *oxfp, cmo* c)
1.1       ohara     654: {
                    655:     int tag = c->tag;
                    656:
1.13      ohara     657:     c = call_hook_before_send_cmo(oxfp, c);
1.2       ohara     658:
1.13      ohara     659:     send_int32(oxfp, tag);
1.1       ohara     660:     switch(tag) {
                    661:     case CMO_NULL:
                    662:     case CMO_ZERO:
                    663:     case CMO_DMS_GENERIC:
1.13      ohara     664:         send_cmo_null(oxfp, c);  /* empty function. */
1.1       ohara     665:         break;
                    666:     case CMO_INT32:
1.13      ohara     667:         send_cmo_int32(oxfp, (cmo_int32 *)c);
1.1       ohara     668:         break;
                    669:     case CMO_STRING:
1.13      ohara     670:         send_cmo_string(oxfp, (cmo_string *)c);
1.1       ohara     671:         break;
                    672:     case CMO_MATHCAP:
                    673:     case CMO_ERROR2:
                    674:     case CMO_RING_BY_NAME:
                    675:     case CMO_INDETERMINATE:
1.13      ohara     676:         send_cmo_mathcap(oxfp, (cmo_mathcap *)c);
1.1       ohara     677:         break;
                    678:     case CMO_LIST:
1.13      ohara     679:         send_cmo_list(oxfp, (cmo_list *)c);
1.1       ohara     680:         break;
                    681:     case CMO_MONOMIAL32:
1.13      ohara     682:         send_cmo_monomial32(oxfp, (cmo_monomial32 *)c);
1.1       ohara     683:         break;
                    684:     case CMO_ZZ:
1.13      ohara     685:         send_cmo_zz(oxfp, (cmo_zz *)c);
1.33      ohara     686:         break;
                    687:     case CMO_QQ:
                    688:         send_cmo_qq(oxfp, (cmo_qq *)c);
1.1       ohara     689:         break;
                    690:     case CMO_DISTRIBUTED_POLYNOMIAL:
1.13      ohara     691:         send_cmo_distributed_polynomial(oxfp, (cmo_distributed_polynomial *)c);
1.26      ohara     692:         break;
                    693:     case CMO_RECURSIVE_POLYNOMIAL:
                    694:         send_cmo_recursive_polynomial(oxfp, (cmo_recursive_polynomial *)c);
                    695:         break;
                    696:     case CMO_POLYNOMIAL_IN_ONE_VARIABLE:
                    697:         send_cmo_polynomial_in_one_variable(oxfp, (cmo_polynomial_in_one_variable *)c);
1.28      ohara     698:         break;
1.32      ohara     699:        case CMO_64BIT_MACHINE_DOUBLE:
                    700:        case CMO_IEEE_DOUBLE_FLOAT:
                    701:                send_cmo_double(oxfp, (cmo_double *)c);
                    702:                break;
1.28      ohara     703:     case CMO_TREE:
                    704:         send_cmo_tree(oxfp, (cmo_tree *)c);
                    705:         break;
                    706:     case CMO_LAMBDA:
                    707:         send_cmo_lambda(oxfp, (cmo_lambda *)c);
1.1       ohara     708:         break;
                    709:     default:
1.13      ohara     710:         call_hook_after_send_cmo(oxfp, c);
1.1       ohara     711:     }
                    712: }
                    713:
1.13      ohara     714: static int send_mpz(OXFILE *oxfp, mpz_ptr mpz)
1.1       ohara     715: {
                    716:     int i;
1.35      iwane     717:        int n = sizeof(mpz->_mp_d[0]) / sizeof(int);
                    718:     int len = abs(mpz->_mp_size) * n;
                    719:        int *ptr = (int *)mpz->_mp_d;
1.37    ! iwane     720:     int size;
1.35      iwane     721:     send_int32(oxfp, mpz->_mp_size * n);
1.37    ! iwane     722:     if (len > 0 && ptr[len-1] == 0) {
        !           723:         len--;
        !           724:     }
        !           725:     size = mpz->_mp_size < 0 ? -len : len;
        !           726:     send_int32(oxfp, size);
1.1       ohara     727:     for(i=0; i<len; i++) {
1.35      iwane     728:         send_int32(oxfp, ptr[i]);
1.1       ohara     729:     }
                    730:     return 0;
                    731: }
                    732:
                    733: ox_data* new_ox_data(cmo* c)
                    734: {
1.25      ohara     735:     ox_data* m = MALLOC(sizeof(ox_data));
1.1       ohara     736:     m->tag = OX_DATA;
                    737:     m->cmo = c;
                    738:     return m;
                    739: }
                    740:
                    741: ox_command* new_ox_command(int sm_code)
                    742: {
1.25      ohara     743:     ox_command* m = MALLOC(sizeof(ox_command));
1.1       ohara     744:     m->tag = OX_COMMAND;
                    745:     m->command = sm_code;
                    746:     return m;
                    747: }
                    748:
                    749: ox_sync_ball* new_ox_sync_ball()
                    750: {
1.25      ohara     751:     ox_sync_ball *m = MALLOC(sizeof(ox_sync_ball));
1.1       ohara     752:     m->tag = OX_SYNC_BALL;
                    753:     return m;
1.19      ohara     754: }
                    755:
                    756: int ox_stderr_init(FILE *fp)
                    757: {
1.21      ohara     758:     ox_stderr = fp;
                    759:     if (ox_stderr != NULL) {
                    760:         setbuf(ox_stderr, NULL);
                    761:     }
1.27      ohara     762:     return 0;
1.20      ohara     763: }
                    764:
                    765: int ox_printf(char *format, ...)
                    766: {
1.21      ohara     767:     if (ox_stderr != NULL) {
                    768:         va_list ap;
                    769:         va_start(ap, format);
                    770:         vfprintf(ox_stderr, format, ap);
                    771:     }
1.27      ohara     772:     return 0;
1.1       ohara     773: }

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