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