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