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