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