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