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