Annotation of OpenXM/src/ox_math/ox.c, Revision 1.2
1.1 ohara 1: /* -*- mode: C; coding: euc-japan -*- */
2: /* $OpenXM$ */
1.2 ! ohara 3: /* $Id: ox.c,v 1.7 1999/11/01 17:58:47 ohara Exp $ */
1.1 ohara 4:
5: /*
6: 関数の名前付け規約(その2):
7: (1) receive_cmo 関数はCMOタグとデータ本体を受信する. この関数は CMOタグの
8: 値が事前に分からないときに使用する. 返り値として、cmo へのポインタを返す.
9: (2) receive_cmo_XXX 関数は, CMOタグを親の関数で受信してから呼び出される関
10: 数で、データ本体のみを受信し、cmo_XXX へのポインタを返す. しかも、
11: 関数内部で new_cmo_XXX 関数を呼び出す.
12: (3) send_cmo 関数はCMOタグとデータ本体を送信する.
13: (4) send_cmo_XXX 関数はCMOタグを親の関数で送信してから呼び出される関数で、
14: データ本体のみを送信する.
15:
16: ----
17: (5) receive_ox_XXX 関数は存在しない(作らない). receive_cmo を利用する.
18: (6) send_ox_XXX 関数は OX タグを含めて送信する.
19: (7) ox_XXX 関数は一連の送受信を含むより抽象的な操作を表現する.
20: ox_XXX 関数は、第一引数として、ox_file_t型の変数 sv をとる.
21:
22: (8) YYY_cmo 関数と YYY_cmo_XXX 関数の関係は次の通り:
23: まず YYY_cmo 関数で cmo のタグを処理し、タグを除いた残りの部分を
24: YYY_cmo_XXX 関数が処理する. cmo の内部に cmo_ZZZ へのポインタが
25: あるときには、その種類によらずに YYY_cmo 関数を呼び出す.
26: */
27:
28: #include <stdio.h>
29: #include <stdlib.h>
30: #include <string.h>
31: #include <unistd.h>
32: #include <errno.h>
33: #include <fcntl.h>
34: #include <gmp.h>
35:
36: #include "mysocket.h"
37: #include "ox.h"
38: #include "parse.h"
39:
40: static int cmolen_cmo_int32(cmo_int32* c);
41: static int cmolen_cmo_list(cmo_list* c);
42: static int cmolen_cmo_mathcap(cmo_mathcap* c);
43: static int cmolen_cmo_null(cmo_null* c);
44: static int cmolen_cmo_string(cmo_string* c);
45: static int cmolen_cmo_zz(cmo_zz* c);
1.2 ! ohara 46: static int cmolen_cmo_monomial32(cmo_monomial32* c);
1.1 ohara 47:
48: static char* dump_cmo_int32(char* array, cmo_int32* m);
49: static char* dump_cmo_list(char* array, cmo_list* m);
50: static char* dump_cmo_mathcap(char* array, cmo_mathcap* m);
51: static char* dump_cmo_null(char* array, cmo_null* m);
52: static char* dump_cmo_string(char* array, cmo_string* m);
1.2 ! ohara 53: static char* dump_cmo_monomial32(char* array, cmo_monomial32* c);
1.1 ohara 54: static char* dump_cmo_zz(char* array, cmo_zz* c);
55: static char* dump_integer(char* array, int x);
56: static char* dump_mpz(char* array, mpz_ptr mpz);
57:
58: static int funcs(int cmo_type);
59:
1.2 ! ohara 60: /* CMO_xxx の値順にならべること(デバッグのため) */
1.1 ohara 61: static int read_password(int fd, char* passwd);
1.2 ! ohara 62: static cmo_null* receive_cmo_null(int fd);
1.1 ohara 63: static cmo_int32* receive_cmo_int32(int fd);
1.2 ! ohara 64: static cmo_string* receive_cmo_string(int fd);
! 65: static cmo_mathcap* receive_cmo_mathcap(int fd);
1.1 ohara 66: static cmo_list* receive_cmo_list(int fd);
67: static cmo_zz* receive_cmo_zz(int fd);
1.2 ! ohara 68: static cmo_zero* receive_cmo_zero(int fd);
! 69: static cmo_dms_generic* receive_cmo_dms_generic(int fd);
! 70: static cmo_error2* receive_cmo_error2(int fd);
1.1 ohara 71: static void receive_mpz(int fd, mpz_ptr mpz);
72:
1.2 ! ohara 73: static int send_cmo_null(int fd, cmo_null* c);
1.1 ohara 74: static int send_cmo_int32(int fd, cmo_int32* m);
1.2 ! ohara 75: static int send_cmo_string(int fd, cmo_string* m);
! 76: static int send_cmo_mathcap(int fd, cmo_mathcap* c);
1.1 ohara 77: static int send_cmo_list(int fd, cmo_list* c);
78: static int send_cmo_zz(int fd, cmo_zz* c);
1.2 ! ohara 79: static int send_cmo_error2(int fd, cmo_error2* c);
1.1 ohara 80: static int send_mpz(int fd, mpz_ptr mpz);
81:
82:
83: /* エラーハンドリングのため */
84: static int current_received_serial = 0;
85:
86: /* エラーを起こしたときはサーバは次のようにすればよい. */
1.2 ! ohara 87: cmo_error2* gen_error_object(int err_code)
1.1 ohara 88: {
89: cmo_list* li = new_cmo_list();
90: append_cmo_list(li, (cmo *)new_cmo_int32(current_received_serial));
91: append_cmo_list(li, (cmo *)new_cmo_int32(err_code));
92: /* 他の情報を加えるならココ */
1.2 ! ohara 93: return new_cmo_error2((cmo *)li);
1.1 ohara 94: }
95:
96: /* add at Mon Sep 7 15:51:28 JST 1998 */
97: #define DEFAULT_SERIAL_NUMBER 0x0000ffff
98: #define receive_serial_number(x) (receive_int32(x))
99:
100: /* 新しいシリアル番号を得る */
101: int next_serial()
102: {
103: static int serial_number = DEFAULT_SERIAL_NUMBER;
104: return serial_number++;
105: }
106:
107: /* int32 型のオブジェクトを送信する. */
108: int send_int32(int fd, int int32)
109: {
110: int32 = htonl(int32);
111: return write(fd, &int32, sizeof(int));
112: }
113:
114: /* int32 型のオブジェクトを受信する. */
115: int receive_int32(int fd)
116: {
117: int tag;
118: read(fd, &tag, sizeof(int));
119: return ntohl(tag);
120: }
121:
122: /* (OX_tag, serial number) を受信する. */
123: int receive_ox_tag(int fd)
124: {
125: int serial;
126: int tag = receive_int32(fd);
127: current_received_serial = receive_serial_number(fd);
128: return tag;
129: }
130:
131: /* (OX_tag, serial number) を送信する. */
132: int send_ox_tag(int fd, int tag)
133: {
134: send_int32(fd, tag);
135: return send_int32(fd, next_serial());
136: }
137:
138:
139: /* CMO_LIST 関係の関数群 */
140: cell* new_cell(cmo* newcmo)
141: {
142: cell* h = malloc(sizeof(cell));
143: h->next = NULL;
144: h->cmo = newcmo;
145: return h;
146: }
147:
1.2 ! ohara 148: cell* next_cell(cell* this)
! 149: {
! 150: return this->next;
! 151: }
! 152:
1.1 ohara 153: static cell* *tailp(cmo_list* this) {
154: cell **cp = &this->head;
155: while (*cp != NULL) {
156: cp = &((*cp)->next);
157: }
158: return cp;
159: }
160:
161: int length_cmo_list(cmo_list* this)
162: {
163: return this->length;
164: }
165:
166: int append_cmo_list(cmo_list* this, cmo* newcmo)
167: {
168: /* リストの最後尾のNULLを保持している変数へのポインタ */
169: cell **cp = tailp(this);
170: *cp = new_cell(newcmo);
171: this->length++;
172: return 0;
173: }
174:
175: /** receive_cmo_XXX 関数群 **/
1.2 ! ohara 176: static cmo_null* receive_cmo_null(int fd)
1.1 ohara 177: {
1.2 ! ohara 178: return new_cmo_null();
1.1 ohara 179: }
180:
181: static cmo_int32* receive_cmo_int32(int fd)
182: {
183: int i = receive_int32(fd);
184: return new_cmo_int32(i);
185: }
186:
1.2 ! ohara 187: static cmo_string* receive_cmo_string(int fd)
! 188: {
! 189: int len = receive_int32(fd);
! 190: char* s = malloc(len+1);
! 191: memset(s, '\0', len+1);
! 192: if (len > 0) {
! 193: read(fd, s, len);
! 194: }
! 195: return new_cmo_string(s);
! 196: }
! 197:
! 198: static cmo_mathcap* receive_cmo_mathcap(int fd)
! 199: {
! 200: cmo* ob = receive_cmo(fd);
! 201: return new_cmo_mathcap(ob);
! 202: }
! 203:
1.1 ohara 204: static cmo_list* receive_cmo_list(int fd)
205: {
206: cmo* newcmo;
207: cmo_list* c = new_cmo_list();
208: int len = receive_int32(fd);
209:
210: while (len>0) {
211: newcmo = receive_cmo(fd);
212: append_cmo_list(c, newcmo);
213: len--;
214: }
215: return c;
216: }
217:
1.2 ! ohara 218: static cmo_zz* receive_cmo_zz(int fd)
! 219: {
! 220: cmo_zz* c = new_cmo_zz();
! 221: receive_mpz(fd, c->mpz);
! 222: return c;
! 223: }
! 224:
! 225: static cmo_zero* receive_cmo_zero(int fd)
1.1 ohara 226: {
1.2 ! ohara 227: return new_cmo_zero();
1.1 ohara 228: }
229:
1.2 ! ohara 230: static cmo_dms_generic* receive_cmo_dms_generic(int fd)
1.1 ohara 231: {
1.2 ! ohara 232: return new_cmo_dms_generic();
1.1 ohara 233: }
234:
1.2 ! ohara 235: static cmo_ring_by_name* receive_cmo_ring_by_name(int fd)
1.1 ohara 236: {
1.2 ! ohara 237: cmo* ob = receive_cmo(fd);
! 238: /* 意味的チェックが必要 */
! 239: return new_cmo_ring_by_name(ob);
1.1 ohara 240: }
241:
1.2 ! ohara 242: static cmo_error2* receive_cmo_error2(int fd)
1.1 ohara 243: {
1.2 ! ohara 244: cmo* ob = receive_cmo(fd);
! 245: return new_cmo_error2(ob);
1.1 ohara 246: }
247:
248: static void receive_mpz(int fd, mpz_ptr mpz)
249: {
250: int i;
251: int size = receive_int32(fd);
252: int len = abs(size);
253: resize_mpz(mpz, size);
254:
255: for(i=0; i<len; i++) {
256: mpz->_mp_d[i] = receive_int32(fd);
257: }
258: }
259:
260: void resize_mpz(mpz_ptr mpz, int size)
261: {
262: _mpz_realloc(mpz, abs(size));
263: mpz->_mp_size = size;
264: }
265:
266: /** new_cmo_XXX 関数群 **/
1.2 ! ohara 267: cmo_null* new_cmo_null()
1.1 ohara 268: {
1.2 ! ohara 269: cmo_null* m = malloc(sizeof(cmo_null));
! 270: m->tag = CMO_NULL;
! 271: return m;
1.1 ohara 272: }
273:
274: cmo_int32* new_cmo_int32(int i)
275: {
276: cmo_int32* c;
277: c = malloc(sizeof(cmo_int32));
278: c->tag = CMO_INT32;
279: c->i = i;
280: return c;
281: }
282:
1.2 ! ohara 283: cmo_string* new_cmo_string(char* s)
! 284: {
! 285: cmo_string* c = malloc(sizeof(cmo_string));
! 286: char *s2 = malloc(strlen(s)+1);
! 287: strcpy(s2, s);
! 288:
! 289: c->tag = CMO_STRING;
! 290: c->s = s2;
! 291: return c;
! 292: }
! 293:
! 294: cmo_mathcap* new_cmo_mathcap(cmo* ob)
! 295: {
! 296: cmo_mathcap* c = malloc(sizeof(cmo_mathcap));
! 297: c->tag = CMO_MATHCAP;
! 298: c->ob = ob;
! 299: return c;
! 300: }
! 301:
1.1 ohara 302: cmo_list* new_cmo_list()
303: {
304: cmo_list* c = malloc(sizeof(cmo_list));
305: c->tag = CMO_LIST;
306: c->length = 0;
307: c->head = NULL;
308: return c;
309: }
310:
1.2 ! ohara 311: cmo_monomial32* new_cmo_monomial32()
1.1 ohara 312: {
1.2 ! ohara 313: cmo_monomial32* c = malloc(sizeof(cmo_monomial32));
! 314: c->tag = CMO_MONOMIAL32;
1.1 ohara 315: return c;
316: }
317:
1.2 ! ohara 318: cmo_monomial32* new_cmo_monomial32_size(int size)
1.1 ohara 319: {
1.2 ! ohara 320: cmo_monomial32* c = new_cmo_monomial32();
! 321: if (size>0) {
! 322: c->length = size;
! 323: c->exps = malloc(sizeof(int)*size);
! 324: }
1.1 ohara 325: return c;
326: }
327:
328: cmo_zz* new_cmo_zz()
329: {
330: cmo_zz* c = malloc(sizeof(cmo_zz));
331: c->tag = CMO_ZZ;
332: mpz_init(c->mpz);
333: return c;
334: }
335:
336: cmo_zz* new_cmo_zz_noinit()
337: {
338: cmo_zz* c = malloc(sizeof(cmo_zz));
339: c->tag = CMO_ZZ;
340: return c;
341: }
342:
343: cmo_zz* new_cmo_zz_set_si(int i)
344: {
345: cmo_zz* c = new_cmo_zz();
346: mpz_set_si(c->mpz, i);
347: return c;
348: }
349:
350: cmo_zz* new_cmo_zz_size(int size)
351: {
352: cmo_zz* c = new_cmo_zz();
353: resize_mpz(c->mpz, size);
354: return c;
355: }
356:
1.2 ! ohara 357: cmo_zero* new_cmo_zero()
! 358: {
! 359: cmo_zero* m = malloc(sizeof(cmo_zero));
! 360: m->tag = CMO_ZERO;
! 361: return m;
! 362: }
! 363:
! 364: cmo_dms_generic* new_cmo_dms_generic()
! 365: {
! 366: cmo_dms_generic* m = malloc(sizeof(cmo_dms_generic));
! 367: m->tag = CMO_DMS_GENERIC;
! 368: return m;
! 369: }
! 370:
! 371: cmo_ring_by_name* new_cmo_ring_by_name(cmo* ob)
! 372: {
! 373: cmo_ring_by_name* c = malloc(sizeof(cmo_ring_by_name));
! 374: c->tag = CMO_RING_BY_NAME;
! 375: c->ob = ob;
! 376: return c;
! 377: }
! 378:
! 379: cmo_indeterminate* new_cmo_indeterminate(cmo* ob)
! 380: {
! 381: cmo_indeterminate* c = malloc(sizeof(cmo_indeterminate));
! 382: c->tag = CMO_INDETERMINATE;
! 383: c->ob = ob;
! 384: return c;
! 385: }
! 386:
! 387: cmo_error2* new_cmo_error2(cmo* ob)
! 388: {
! 389: cmo_error2* c = malloc(sizeof(cmo_error2));
! 390: c->tag = CMO_ERROR2;
! 391: c->ob = ob;
! 392: return c;
! 393: }
! 394:
1.1 ohara 395: /* receive_ox_tag() == OX_DATA の後に呼び出される */
396: /* 関数ポインタを使った方がきれいに書けるような気がする. */
397: /* if (foo[tag] != NULL) foo[tag](fd); とか */
398:
399: cmo* receive_cmo(int fd)
400: {
401: cmo* m;
402: int tag;
403: tag = receive_int32(fd);
404:
405: switch(tag) {
406: case CMO_NULL:
407: m = receive_cmo_null(fd);
408: break;
409: case CMO_INT32:
410: m = (cmo *)receive_cmo_int32(fd);
411: break;
412: case CMO_STRING:
413: m = (cmo *)receive_cmo_string(fd);
414: break;
1.2 ! ohara 415: case CMO_MATHCAP:
! 416: m = (cmo *)receive_cmo_mathcap(fd);
1.1 ohara 417: break;
418: case CMO_LIST:
419: m = (cmo *)receive_cmo_list(fd);
420: break;
1.2 ! ohara 421: case CMO_ZZ:
! 422: m = (cmo *)receive_cmo_zz(fd);
1.1 ohara 423: break;
424: case CMO_ERROR2:
1.2 ! ohara 425: m = (cmo *)receive_cmo_error2(fd);
1.1 ohara 426: break;
427: case CMO_DATUM:
428: case CMO_QQ:
429: case CMO_ZERO:
430: case CMO_DMS:
431: default:
432: fprintf(stderr, "unknown cmo-type: tag = (%d)\n", m->tag);
433: }
434: return m;
435: }
436:
437: void send_ox_command(int fd, int sm_command)
438: {
439: send_ox_tag(fd, OX_COMMAND);
440: send_int32(fd, sm_command);
441: }
442:
443: int print_cmo(cmo* c)
444: {
445: int tag = c->tag;
446: fprintf(stderr, "local::tag = (%d): ", tag);
447: switch(tag) {
448: case CMO_LIST:
449: print_cmo_list((cmo_list *)c);
450: break;
451: case CMO_INT32:
452: print_cmo_int32((cmo_int32 *)c);
453: break;
454: case CMO_MATHCAP:
455: print_cmo_mathcap((cmo_mathcap *)c);
456: break;
457: case CMO_STRING:
458: print_cmo_string((cmo_string *)c);
459: break;
460: case CMO_NULL:
461: fprintf(stderr, "\n");
462: break;
463: default:
464: fprintf(stderr, "print_cmo() does not know how to print.\n");
465: }
466: }
467:
468: int print_cmo_int32(cmo_int32* c)
469: {
470: fprintf(stderr, "cmo_int32 = (%d)\n", c->i);
471: }
472:
473: int print_cmo_list(cmo_list* li)
474: {
475: cell* cp = li->head;
476: fprintf(stderr, "length = (%d)\nlist:\n", li->length);
477: while(cp != NULL) {
478: print_cmo(cp->cmo);
479: cp=cp->next;
480: }
481: fprintf(stderr, "end of list\n");
482: }
483:
484: int print_cmo_mathcap(cmo_mathcap* c)
485: {
486: fprintf(stderr, "\n");
1.2 ! ohara 487: print_cmo(c->ob);
1.1 ohara 488: }
489:
490: int print_cmo_string(cmo_string* c)
491: {
492: fprintf(stderr, "cmo_string = (%s)\n", c->s);
493: }
494:
495: /* ox_start() から呼び出す */
496: static int read_password(int fd, char* passwd)
497: {
498: char buff[1024];
499: int n;
500: if ((n = read(fd, buff, 1024)) != strlen(passwd)) {
501: fprintf(stderr, "Socket#%d: Login incorrect.\n", fd);
502: fprintf(stderr, "password = (%s), received = (%s).\n", passwd, buff);
503: fprintf(stderr, " = (%d), received = (%d).\n", strlen(passwd), n);
504: }
505: fflush(stderr);
506: }
507:
508: void ox_close(ox_file_t sv)
509: {
510: send_ox_command(sv->control, SM_control_kill);
511: #if DEBUG
512: sleep(2); /* OpenXM server の終了を待つ. あまり意味はない. */
513: fprintf(stderr, "I have closed an Open XM server.\n");
514: #endif
515: }
516:
517: void ox_executeStringByLocalParser(ox_file_t sv, char* s)
518: {
519: /* 文字列ををスタックにプッシュ. */
520: send_ox_cmo(sv->stream, (cmo *)new_cmo_string(s));
521:
522: /* サーバに実行させる. */
523: send_ox_command(sv->stream, SM_executeStringByLocalParser);
524: }
525:
526: /* ox_mathcap() をコールする. */
527: cmo_mathcap* ox_mathcap(ox_file_t sv)
528: {
529: send_ox_command(sv->stream, SM_mathcap);
530: send_ox_command(sv->stream, SM_popCMO);
531: receive_ox_tag(sv->stream); /* OX_DATA */
532: return (cmo_mathcap *)receive_cmo(sv->stream);
533: }
534:
535: char* ox_popString(ox_file_t sv, int fd)
536: {
537: cmo_string* m = NULL;
538:
539: send_ox_command(fd, SM_popString);
540: receive_ox_tag(fd); /* OX_DATA */
541: m = (cmo_string *)receive_cmo(fd);
542: return m->s;
543: }
544:
545: cmo* ox_pop_cmo(ox_file_t sv, int fd)
546: {
547: send_ox_command(fd, SM_popCMO);
548: receive_ox_tag(fd); /* OX_DATA */
549: return receive_cmo(fd);
550: }
551:
552: /*
553: ox_start は クライアントが呼び出すための関数である.
554: サーバでは使われない. prog1 は コントロールサーバであり,
555: -ox, -reverse, -data, -control, -pass, -host
556: というオプションを理解することを仮定する. prog2 は計算サーバである.
557: 接続時には, sv->control を先にオープンする.
558: */
559:
560: ox_file_t ox_start(char* host, char* prog1, char* prog2)
561: {
562: static char pass[] = "passwd";
563: char ctl[16], dat[16];
564: short portControl = 0; /* short であることに注意 */
565: short portStream = 0;
566: ox_file_t sv = malloc(sizeof(__ox_file_struct));
567:
568: sv->control = mysocketListen(host, &portControl);
569: sv->stream = mysocketListen(host, &portStream);
570: sprintf(ctl, "%d", portControl);
571: sprintf(dat, "%d", portStream);
572:
573: if (fork() == 0) {
574: dup2(2, 1);
575: dup2(open(DEFAULT_LOGFILE, O_RDWR|O_CREAT|O_TRUNC, 0644), 2);
576: execl(prog1, prog1, "-reverse", "-ox", prog2,
577: "-data", dat, "-control", ctl, "-pass", pass,
578: "-host", host, NULL);
579: }
580:
581: sv->control = mysocketAccept(sv->control);
582: read_password(sv->control, pass);
583: decideByteOrder(sv->control, sv->control, 0);
584: usleep(10);
585: sv->stream = mysocketAccept(sv->stream);
586: read_password(sv->stream, pass);
587: decideByteOrder(sv->stream, sv->stream, 0);
588:
589: return sv;
590: }
591:
592: void ox_reset(ox_file_t sv)
593: {
594: send_ox_command(sv->control, SM_control_reset_connection);
595:
596: receive_ox_tag(sv->control); /* OX_DATA */
597: receive_cmo(sv->control); /* (CMO_INT32, 0) */
598:
599: while(receive_ox_tag(sv->stream) != OX_SYNC_BALL) {
600: receive_cmo(sv->stream); /* skipping a message. */
601: }
602:
603: send_ox_tag(sv->stream, OX_SYNC_BALL);
604: #if DEBUG
605: fprintf(stderr, "I have reset an Open XM server.\n");
606: #endif
607: }
608:
1.2 ! ohara 609: /* 以下は bconv.c で必要とする関数群である. */
! 610:
! 611: /* cmolen 関数は cmo の(送信時の)バイト長を返す. */
! 612: /* cmolen_XXX 関数は cmo_XXX の tag を除いたバイト長を返す. */
! 613:
! 614: static int cmolen_cmo_null(cmo_null* c)
! 615: {
! 616: return 0;
! 617: }
1.1 ohara 618:
619: static int cmolen_cmo_int32(cmo_int32* c)
620: {
621: return sizeof(int);
622: }
623:
1.2 ! ohara 624: static int cmolen_cmo_string(cmo_string* c)
! 625: {
! 626: return sizeof(int)+strlen(c->s);
! 627: }
! 628:
! 629: static int cmolen_cmo_mathcap(cmo_mathcap* c)
! 630: {
! 631: return cmolen_cmo(c->ob);
! 632: }
! 633:
1.1 ohara 634: static int cmolen_cmo_list(cmo_list* c)
635: {
636: int size = sizeof(c->head);
637: cell* cp = c->head;
638:
639: while(cp != NULL) {
640: size += cmolen_cmo(cp->cmo);
641: cp = cp->next;
642: }
643: return size;
644: }
645:
1.2 ! ohara 646: static int cmolen_cmo_monomial32(cmo_monomial32* c)
1.1 ohara 647: {
1.2 ! ohara 648: int len = (c->length + 1)*sizeof(int);
! 649: return len + cmolen_cmo(c->coef);
1.1 ohara 650: }
651:
652: static int cmolen_cmo_zz(cmo_zz* c)
653: {
654: int len = abs(c->mpz->_mp_size);
655: return sizeof(len) + len*sizeof(int);
656: }
657:
658: /* CMO がバイトエンコードされた場合のバイト列の長さを求める */
659: int cmolen_cmo(cmo* c)
660: {
661: int size = sizeof(int);
662:
663: switch(c->tag) {
664: case CMO_NULL:
1.2 ! ohara 665: case CMO_ZERO:
! 666: case CMO_DMS_GENERIC:
1.1 ohara 667: size += cmolen_cmo_null(c);
668: break;
669: case CMO_INT32:
670: size += cmolen_cmo_int32((cmo_int32 *)c);
671: break;
672: case CMO_STRING:
673: size += cmolen_cmo_string((cmo_string *)c);
674: break;
1.2 ! ohara 675: case CMO_MATHCAP:
! 676: case CMO_RING_BY_NAME:
! 677: case CMO_ERROR2:
! 678: size += cmolen_cmo_mathcap((cmo_mathcap *)c);
! 679: break;
1.1 ohara 680: case CMO_LIST:
681: size += cmolen_cmo_list((cmo_list *)c);
682: break;
1.2 ! ohara 683: case CMO_MONOMIAL32:
! 684: size += cmolen_cmo_monomial32((cmo_monomial32 *)c);
! 685: break;
1.1 ohara 686: case CMO_ZZ:
687: size += cmolen_cmo_zz((cmo_zz *)c);
688: break;
689: default:
690: }
691: return size;
692: }
693:
1.2 ! ohara 694: static char* dump_cmo_null(char* array, cmo_null* m)
! 695: {
! 696: return array;
! 697: }
! 698:
1.1 ohara 699: static char* dump_cmo_int32(char* array, cmo_int32* m)
700: {
701: return dump_integer(array, m->i);
702: }
703:
1.2 ! ohara 704: static char* dump_cmo_string(char* array, cmo_string* m)
! 705: {
! 706: int len = strlen(m->s);
! 707: array = dump_integer(array, len);
! 708: memcpy(array, m->s, len);
! 709: return array + len;
! 710: }
! 711:
! 712: static char* dump_cmo_mathcap(char* array, cmo_mathcap* c)
! 713: {
! 714: return dump_cmo(array, c->ob);
! 715: }
! 716:
1.1 ohara 717: static char* dump_cmo_list(char* array, cmo_list* m)
718: {
719: cell* cp = m->head;
720: int len = length_cmo_list(m);
721: array = dump_integer(array, len);
722:
723: while(cp != NULL) {
724: array = dump_cmo(array, cp->cmo);
725: cp = cp->next;
726: }
727: return array;
728: }
729:
1.2 ! ohara 730: static char* dump_cmo_monomial32(char* array, cmo_monomial32* c)
1.1 ohara 731: {
1.2 ! ohara 732: int i;
! 733: int length = c->length;
! 734: array = dump_integer(array, c->length);
! 735: for(i=0; i<length; i++) {
! 736: array = dump_integer(array, c->exps[i]);
! 737: }
! 738: array = dump_cmo(array, c->coef);
! 739: return array;
1.1 ohara 740: }
741:
742: static char* dump_cmo_zz(char* array, cmo_zz* c)
743: {
744: return dump_mpz(array, c->mpz);
745: }
746:
747: /* タグを書き出してから、各関数を呼び出す */
748: char* dump_cmo(char* array, cmo* m)
749: {
750: array = dump_integer(array, m->tag);
751: switch(m->tag) {
752: case CMO_NULL:
1.2 ! ohara 753: case CMO_ZERO:
! 754: case CMO_DMS_GENERIC:
1.1 ohara 755: return dump_cmo_null(array, m);
756: case CMO_INT32:
757: return dump_cmo_int32(array, (cmo_int32 *)m);
758: case CMO_STRING:
759: return dump_cmo_string(array, (cmo_string *)m);
1.2 ! ohara 760: case CMO_MATHCAP:
! 761: case CMO_RING_BY_NAME:
! 762: case CMO_ERROR2:
! 763: return dump_cmo_mathcap(array, (cmo_mathcap *)m);
1.1 ohara 764: case CMO_LIST:
765: return dump_cmo_list(array, (cmo_list *)m);
1.2 ! ohara 766: case CMO_MONOMIAL32:
! 767: return dump_cmo_monomial32(array, (cmo_monomial32 *)m);
1.1 ohara 768: case CMO_ZZ:
769: return dump_cmo_zz(array, (cmo_zz *)m);
770: default:
771: return NULL;
772: }
773: }
774:
775: static char* dump_integer(char* array, int x)
776: {
777: int nx = htonl(x);
778: memcpy(array, &nx, sizeof(int));
779: return array + sizeof(int);
780: }
781:
782: static char* dump_mpz(char* array, mpz_ptr mpz)
783: {
784: int i;
785: int len = abs(mpz->_mp_size);
786: array = dump_integer(array, mpz->_mp_size);
787: for(i=0; i<len; i++) {
788: array = dump_integer(array, mpz->_mp_d[i]);
789: }
790: return array;
791: }
792:
793:
794: int send_ox(ox_file_t s, ox *m)
795: {
796: int tag = m->tag;
797: int code;
798: if (tag == OX_DATA) {
799: send_ox_cmo(s->stream, ((ox_data *)m)->cmo);
800: }else if (tag == OX_COMMAND) {
801: code = ((ox_command *)m)->command;
802: if (code >= 1024) {
803: /* control command */
804: send_ox_command(s->control, code);
805: }else {
806: send_ox_command(s->stream, code);
807: }
808: }else {
809: /* CMO?? */
810: send_ox_cmo(s->stream, (cmo *)m);
811: }
812: }
813:
814: int send_ox_cmo(int fd, cmo* m)
815: {
816: send_ox_tag(fd, OX_DATA);
817: send_cmo(fd, m);
818: }
819:
1.2 ! ohara 820: /* send_cmo_xxx 関数群 */
! 821: static int send_cmo_null(int fd, cmo_null* c)
! 822: {
! 823: return 0;
! 824: }
1.1 ohara 825:
1.2 ! ohara 826: static int send_cmo_int32(int fd, cmo_int32* m)
1.1 ohara 827: {
1.2 ! ohara 828: send_int32(fd, m->i);
1.1 ohara 829: }
830:
1.2 ! ohara 831: static int send_cmo_string(int fd, cmo_string* m)
1.1 ohara 832: {
1.2 ! ohara 833: int len = strlen(m->s);
! 834: send_int32(fd, len);
! 835: if (len > 0) {
! 836: write(fd, m->s, len);
! 837: }
1.1 ohara 838: return 0;
839: }
840:
1.2 ! ohara 841: static int send_cmo_mathcap(int fd, cmo_mathcap* c)
1.1 ohara 842: {
1.2 ! ohara 843: send_cmo(fd, c->ob);
! 844: return 0;
1.1 ohara 845: }
846:
847: static int send_cmo_list(int fd, cmo_list* c)
848: {
849: cell* cp = c->head;
850: int len = length_cmo_list(c);
851: send_int32(fd, len);
852:
853: while(cp != NULL) {
854: send_cmo(fd, cp->cmo);
855: cp = cp->next;
856: }
857: return 0;
858: }
859:
1.2 ! ohara 860: static int send_cmo_zz(int fd, cmo_zz* c)
1.1 ohara 861: {
1.2 ! ohara 862: send_mpz(fd, c->mpz);
1.1 ohara 863: }
864:
1.2 ! ohara 865: static int send_cmo_error2(int fd, cmo_error2* c)
1.1 ohara 866: {
1.2 ! ohara 867: send_cmo(fd, c->ob);
1.1 ohara 868: return 0;
869: }
870:
871: /* CMOを送る. OX_tag は送信済*/
872: int send_cmo(int fd, cmo* c)
873: {
874: int tag = c->tag;
875:
876: send_int32(fd, tag);
877: switch(tag) {
878: case CMO_NULL:
879: send_cmo_null(fd, c); /* 空の関数 */
880: break;
881: case CMO_INT32:
882: send_cmo_int32(fd, (cmo_int32 *)c);
883: break;
884: case CMO_STRING:
885: send_cmo_string(fd, (cmo_string *)c);
886: break;
887: case CMO_MATHCAP:
888: send_cmo_mathcap(fd, (cmo_mathcap *)c);
889: break;
890: case CMO_LIST:
891: send_cmo_list(fd, (cmo_list *)c);
892: break;
893: case CMO_ZZ:
894: send_cmo_zz(fd, (cmo_zz *)c);
895: break;
896: case CMO_ERROR2:
1.2 ! ohara 897: send_cmo_error2(fd, (cmo_error2 *)c);
1.1 ohara 898: break;
899: default:
900: }
901: }
902:
903: static int send_mpz(int fd, mpz_ptr mpz)
904: {
905: int i;
906: int len = abs(mpz->_mp_size);
907: send_int32(fd, mpz->_mp_size);
908: for(i=0; i<len; i++) {
909: send_int32(fd, mpz->_mp_d[i]);
910: }
911: return 0;
912: }
913:
914: ox_data* new_ox_data(cmo* c)
915: {
916: ox_data* m = malloc(sizeof(ox_data));
917: m->tag = OX_DATA;
918: m->cmo = c;
919: return m;
920: }
921:
922: ox_command* new_ox_command(int sm_code)
923: {
924: ox_command* m = malloc(sizeof(ox_command));
925: m->tag = OX_COMMAND;
926: m->command = sm_code;
927: return m;
928: }
929:
930: char* dump_ox_data(char* array, ox_data* m)
931: {
932: array = dump_integer(array, OX_DATA);
933: array = dump_integer(array, -1);
934: return dump_cmo(array, m->cmo);
935: }
936:
937: char* dump_ox_command(char* array, ox_command* m)
938: {
939: array = dump_integer(array, OX_COMMAND);
940: array = dump_integer(array, -1);
941: return dump_integer(array, m->command);
942: }
943:
944: #define MAX_TYPES 8
945: static int known_types[] = {
946: -1, /* gate keeper */
947: CMO_NULL,
948: CMO_INT32,
949: CMO_STRING,
950: CMO_MATHCAP,
951: CMO_LIST,
952: CMO_ZZ,
953: CMO_ERROR2,
954: };
955:
1.2 ! ohara 956: #define ID_TEMP "(CMO_MATHCAP, (CMO_LIST, (CMO_LIST, (CMO_INT32, %d), (CMO_STRING, \"%s\"), (CMO_STRING, \"%s\"), (CMO_STRING, \"%s\")), (CMO_LIST, (CMO_INT32, 1), (CMO_INT32, 2), (CMO_INT32, 4), (CMO_INT32, 5), (CMO_INT32, 17), (CMO_INT32, 20), (CMO_INT32, 2130706434))))\n"
1.1 ohara 957:
1.2 ! ohara 958: cmo* make_mathcap_object2(int ver, char* ver_s, char* sysname)
1.1 ohara 959: {
1.2 ! ohara 960: cmo *cap;
! 961: char buff[8192];
1.1 ohara 962:
963: setgetc(mygetc);
1.2 ! ohara 964: sprintf(buff, ID_TEMP, ver, sysname, ver_s, getenv("HOSTTYPE"));
! 965: setmode_mygetc(buff, 8192);
! 966: cap = parse();
1.1 ohara 967: resetgetc();
968:
1.2 ! ohara 969: return cap;
1.1 ohara 970: }
971:
972: cmo* make_mathcap_object(int version, char* id_string)
973: {
974: cmo_list *li_ver, *li_cmo, *li;
975:
976: int i;
977: li_ver = new_cmo_list();
978: append_cmo_list(li_ver, (cmo *)new_cmo_int32(version));
979: append_cmo_list(li_ver, (cmo *)new_cmo_string(id_string));
980:
981: li_cmo = new_cmo_list();
982: for(i=0; i<MAX_TYPES; i++) {
983: if (known_types[i] != -1) {
984: append_cmo_list(li_cmo, (cmo *)new_cmo_int32(known_types[i]));
985: }
986: }
987:
988: li = new_cmo_list();
989: append_cmo_list(li, (cmo *)li_ver);
990: append_cmo_list(li, (cmo *)li_cmo);
991:
1.2 ! ohara 992: return (cmo *)new_cmo_mathcap((cmo *)li);
1.1 ohara 993: }
994:
995: static int funcs(int cmo_type)
996: {
997: int i;
998: for(i=0; i<MAX_TYPES; i++) {
999: if (known_types[i] == cmo_type) {
1000: return i;
1001: }
1002: }
1003: return 0;
1004: }
1005:
1006: void setCmotypeDisable(int type)
1007: {
1008: int i = funcs(type);
1009: known_types[i] = -1;
1010: }
1011: #if 0
1012: cmo* (*received_funcs[])(int fd) = {
1013: NULL, /* gate keeper */
1014: receive_cmo_null,
1015: receive_cmo_int32,
1016: receive_cmo_string,
1017: receive_cmo_mathcap,
1018: receive_cmo_list,
1019: receive_cmo_zz,
1.2 ! ohara 1020: receive_cmo_error2
1.1 ohara 1021: };
1022:
1023: cmo* receive_cmo2(int fd)
1024: {
1025: int tag;
1026: cmo* (*foo)() = received_funcs[funcs(tag)];
1027: if (foo != NULL) {
1028: return foo(fd);
1029: }
1030: }
1031: #endif
1032: /* ファイルディスクリプタ fd の通信路での integer の byte order を決定する */
1033: /* 実際には order (0,1,or 0xFF)をみてはいない */
1034: int decideByteOrder(int fd_read, int fd_write, int order)
1035: {
1036: char zero = '\0';
1037: char dest;
1038: write(fd_write, &zero, 1);
1039: read(fd_read, &dest, 1);
1040: return 0;
1041: }
1042:
1043: /* Server 側ではこちらを用いる */
1044: int decideByteOrder2(int fd_read, int fd_write, int order)
1045: {
1046: char zero = '\0';
1047: char dest;
1048: read(fd_read, &dest, 1);
1049: write(fd_write, &zero, 1);
1050: return 0;
1051: }
1052:
1053: /* cmo と string の変換関数群 */
1054:
1055: cmo_zz *new_cmo_zz_set_string(char *s)
1056: {
1057: cmo_zz* c = new_cmo_zz_noinit();
1058: mpz_init_set_str(c->mpz, s, 10);
1059: return c;
1060: }
1061:
1.2 ! ohara 1062: char *convert_zz_to_cstring(cmo_zz *c)
1.1 ohara 1063: {
1064: return mpz_get_str(NULL, 10, c->mpz);
1065: }
1066:
1.2 ! ohara 1067: char *convert_cmo_to_cstring(cmo *m)
1.1 ohara 1068: {
1069: switch(m->tag) {
1070: case CMO_ZZ:
1.2 ! ohara 1071: return convert_zz_to_cstring((cmo_zz *)m);
1.1 ohara 1072: case CMO_INT32:
1.2 ! ohara 1073: return convert_int_to_cstring(((cmo_int32 *)m)->i);
1.1 ohara 1074: case CMO_STRING:
1075: return ((cmo_string *)m)->s;
1076: case CMO_NULL:
1.2 ! ohara 1077: return convert_null_to_cstring();
1.1 ohara 1078: default:
1079: fprintf(stderr, "sorry, not implemented CMO\n");
1080: /* まだ実装していません. */
1081: return NULL;
1082: }
1083: }
1084:
1.2 ! ohara 1085: char *convert_null_to_cstring()
1.1 ohara 1086: {
1087: static char* null_string = "";
1088: return null_string;
1089: }
1090:
1.2 ! ohara 1091: char *convert_int_to_cstring(int integer)
1.1 ohara 1092: {
1093: char buff[1024];
1094: char *s;
1095:
1096: sprintf(buff, "%d", integer);
1097: s = malloc(strlen(buff)+1);
1098: strcpy(s, buff);
1099:
1100: return s;
1101: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>