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