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