| version 1.2, 1999/11/02 06:11:57 | 
version 1.12, 1999/11/07 12:12:55 | 
 | 
 | 
|  /* -*- mode: C; coding: euc-japan -*- */ | 
 /* -*- mode: C; coding: euc-japan -*- */ | 
|  /* $OpenXM$ */ | 
 /* $OpenXM: OpenXM/src/ox_math/ox.c,v 1.11 1999/11/06 21:39:36 ohara Exp $ */ | 
|  /* $Id$ */ | 
  | 
|   | 
  | 
|  /* | 
 /* | 
|  関数の名前付け規約(その2): | 
 関数の名前付け規約(その2): | 
| Line 32  YYY_cmo_XXX 関数が処理する.  cmo の内部に cmo_ZZZ への | 
 
  | 
| Line 31  YYY_cmo_XXX 関数が処理する.  cmo の内部に cmo_ZZZ への | 
 
 
 | 
|  #include <errno.h> | 
 #include <errno.h> | 
|  #include <fcntl.h> | 
 #include <fcntl.h> | 
|  #include <gmp.h> | 
 #include <gmp.h> | 
|   | 
 #include <unistd.h> | 
|   | 
 #include <sys/file.h> | 
|   | 
  | 
|  #include "mysocket.h" | 
 #include "mysocket.h" | 
|  #include "ox.h" | 
 #include "ox.h" | 
| Line 45  static int          cmolen_cmo_string(cmo_string* c); | 
 
  | 
| Line 46  static int          cmolen_cmo_string(cmo_string* c); | 
 
 
 | 
|  static int          cmolen_cmo_zz(cmo_zz* c); | 
 static int          cmolen_cmo_zz(cmo_zz* c); | 
|  static int          cmolen_cmo_monomial32(cmo_monomial32* c); | 
 static int          cmolen_cmo_monomial32(cmo_monomial32* c); | 
|   | 
  | 
|  static char*        dump_cmo_int32(char* array, cmo_int32* m); | 
 static int          dump_cmo_int32(cmo_int32* m); | 
|  static char*        dump_cmo_list(char* array, cmo_list* m); | 
 static int          dump_cmo_list(cmo_list* m); | 
|  static char*        dump_cmo_mathcap(char* array, cmo_mathcap* m); | 
 static int          dump_cmo_mathcap(cmo_mathcap* m); | 
|  static char*        dump_cmo_null(char* array, cmo_null* m); | 
 static int          dump_cmo_null(cmo_null* m); | 
|  static char*        dump_cmo_string(char* array, cmo_string* m); | 
 static int          dump_cmo_string(cmo_string* m); | 
|  static char*        dump_cmo_monomial32(char* array, cmo_monomial32* c); | 
 static int          dump_cmo_monomial32(cmo_monomial32* c); | 
|  static char*        dump_cmo_zz(char* array, cmo_zz* c); | 
 static int          dump_cmo_zz(cmo_zz* c); | 
|  static char*        dump_integer(char* array, int x); | 
 static int          dump_string(char *s, int len); | 
|  static char*        dump_mpz(char* array, mpz_ptr mpz); | 
 static int          dump_integer(int x); | 
|   | 
 static int          dump_mpz(mpz_ptr mpz); | 
|   | 
  | 
|  static int          funcs(int cmo_type); | 
 static int          funcs(int cmo_type); | 
|   | 
  | 
|   | 
 static int          login_with_otp(int fd, char* passwd); | 
|   | 
 static char         *create_otp(); | 
|   | 
  | 
|  /* CMO_xxx の値順にならべること(デバッグのため) */ | 
 /* CMO_xxx の値順にならべること(デバッグのため) */ | 
|  static int          read_password(int fd, char* passwd); | 
 static cmo_null*         receive_cmo_null(int fd); | 
|  static cmo_null*    receive_cmo_null(int fd); | 
 static cmo_int32*        receive_cmo_int32(int fd); | 
|  static cmo_int32*   receive_cmo_int32(int fd); | 
 static cmo_string*       receive_cmo_string(int fd); | 
|  static cmo_string*  receive_cmo_string(int fd); | 
 static cmo_mathcap*      receive_cmo_mathcap(int fd); | 
|  static cmo_mathcap* receive_cmo_mathcap(int fd); | 
 static cmo_list*         receive_cmo_list(int fd); | 
|  static cmo_list*    receive_cmo_list(int fd); | 
 static cmo_monomial32*   receive_cmo_monomial32(int fd); | 
|  static cmo_zz*      receive_cmo_zz(int fd); | 
 static cmo_zz*           receive_cmo_zz(int fd); | 
|  static cmo_zero*    receive_cmo_zero(int fd); | 
 static cmo_zero*         receive_cmo_zero(int fd); | 
|  static cmo_dms_generic* receive_cmo_dms_generic(int fd); | 
 static cmo_dms_generic*  receive_cmo_dms_generic(int fd); | 
|  static cmo_error2*  receive_cmo_error2(int fd); | 
 static cmo_ring_by_name* receive_cmo_ring_by_name(int fd); | 
|  static void         receive_mpz(int fd, mpz_ptr mpz); | 
 static cmo_distributed_polynomial* receive_cmo_distributed_polynomial(int fd); | 
|   | 
  | 
|   | 
 static cmo_error2*       receive_cmo_error2(int fd); | 
|   | 
 static void              receive_mpz(int fd, mpz_ptr mpz); | 
|   | 
  | 
|  static int          send_cmo_null(int fd, cmo_null* c); | 
 static int          send_cmo_null(int fd, cmo_null* c); | 
|  static int          send_cmo_int32(int fd, cmo_int32* m); | 
 static int          send_cmo_int32(int fd, cmo_int32* m); | 
|  static int          send_cmo_string(int fd, cmo_string* m); | 
 static int          send_cmo_string(int fd, cmo_string* m); | 
|  static int          send_cmo_mathcap(int fd, cmo_mathcap* c); | 
 static int          send_cmo_mathcap(int fd, cmo_mathcap* c); | 
|  static int          send_cmo_list(int fd, cmo_list* c); | 
 static int          send_cmo_list(int fd, cmo_list* c); | 
|   | 
 static int          send_cmo_monomial32(int fd, cmo_monomial32* c); | 
|  static int          send_cmo_zz(int fd, cmo_zz* c); | 
 static int          send_cmo_zz(int fd, cmo_zz* c); | 
|  static int          send_cmo_error2(int fd, cmo_error2* c); | 
 static int          send_cmo_error2(int fd, cmo_error2* c); | 
|  static int          send_mpz(int fd, mpz_ptr mpz); | 
 static int          send_mpz(int fd, mpz_ptr mpz); | 
|   | 
 static int          send_cmo_distributed_polynomial(int fd, cmo_distributed_polynomial* c); | 
|   | 
  | 
|   | 
  | 
|  /* エラーハンドリングのため */ | 
 /* エラーハンドリングのため */ | 
|  static int current_received_serial = 0; | 
 static int current_received_serial = 0; | 
|   | 
  | 
|  /* エラーを起こしたときはサーバは次のようにすればよい.  */ | 
 /* エラーを起こしたときにサーバは次を呼び出す.  */ | 
|  cmo_error2* gen_error_object(int err_code) | 
 cmo_error2* make_error_object(int err_code, cmo *ob) | 
|  { | 
 { | 
|      cmo_list* li = new_cmo_list(); | 
     cmo_list* li = new_cmo_list(); | 
|      append_cmo_list(li, (cmo *)new_cmo_int32(current_received_serial)); | 
     append_cmo_list(li, (cmo *)new_cmo_int32(current_received_serial)); | 
|      append_cmo_list(li, (cmo *)new_cmo_int32(err_code)); | 
     append_cmo_list(li, (cmo *)new_cmo_int32(err_code)); | 
|   | 
     append_cmo_list(li, ob); | 
|      /* 他の情報を加えるならココ */ | 
     /* 他の情報を加えるならココ */ | 
|      return new_cmo_error2((cmo *)li); | 
     return new_cmo_error2((cmo *)li); | 
|  } | 
 } | 
| Line 135  int send_ox_tag(int fd, int tag) | 
 
  | 
| Line 145  int send_ox_tag(int fd, int tag) | 
 
 
 | 
|      return send_int32(fd, next_serial()); | 
     return send_int32(fd, next_serial()); | 
|  } | 
 } | 
|   | 
  | 
|   | 
  | 
|  /* CMO_LIST 関係の関数群 */ | 
 /* CMO_LIST 関係の関数群 */ | 
|  cell* new_cell(cmo* newcmo) | 
 cell* new_cell() | 
|  { | 
 { | 
|      cell* h = malloc(sizeof(cell)); | 
     cell* h = malloc(sizeof(cell)); | 
|      h->next = NULL; | 
     h->next = NULL; | 
|      h->cmo  = newcmo; | 
     h->cmo  = NULL; | 
|      return h; | 
     return h; | 
|  } | 
 } | 
|   | 
  | 
| Line 150  cell* next_cell(cell* this) | 
 
  | 
| Line 159  cell* next_cell(cell* this) | 
 
 
 | 
|      return this->next; | 
     return this->next; | 
|  } | 
 } | 
|   | 
  | 
|  static cell* *tailp(cmo_list* this) { | 
 static cell *tail(cmo_list* this) { | 
|      cell **cp = &this->head; | 
     cell *cp = this->head; | 
|      while (*cp != NULL) { | 
     while (cp->next != NULL) { | 
|          cp = &((*cp)->next); | 
         cp = cp->next; | 
|      } | 
     } | 
|      return cp; | 
     return cp; | 
|  } | 
 } | 
|   | 
  | 
|  int length_cmo_list(cmo_list* this) | 
  | 
|  { | 
  | 
|      return this->length; | 
  | 
|  } | 
  | 
|   | 
  | 
|  int append_cmo_list(cmo_list* this, cmo* newcmo) | 
 int append_cmo_list(cmo_list* this, cmo* newcmo) | 
|  { | 
 { | 
|      /* リストの最後尾のNULLを保持している変数へのポインタ */ | 
     cell *cp = tail(this); | 
|      cell **cp = tailp(this); | 
     cp->cmo  = newcmo; | 
|      *cp = new_cell(newcmo); | 
     cp->next = new_cell(); | 
|      this->length++; | 
     this->length++; | 
|      return 0; | 
     return 0; | 
|  } | 
 } | 
|   | 
  | 
|   | 
 int length_cmo_list(cmo_list* this) | 
|   | 
 { | 
|   | 
     return this->length; | 
|   | 
 } | 
|   | 
  | 
|  /** receive_cmo_XXX 関数群 **/ | 
 /** receive_cmo_XXX 関数群 **/ | 
|  static cmo_null* receive_cmo_null(int fd) | 
 static cmo_null* receive_cmo_null(int fd) | 
|  { | 
 { | 
| Line 203  static cmo_mathcap* receive_cmo_mathcap(int fd) | 
 
  | 
| Line 212  static cmo_mathcap* receive_cmo_mathcap(int fd) | 
 
 
 | 
|   | 
  | 
|  static cmo_list* receive_cmo_list(int fd) | 
 static cmo_list* receive_cmo_list(int fd) | 
|  { | 
 { | 
|      cmo* newcmo; | 
     cmo* ob; | 
|      cmo_list* c = new_cmo_list(); | 
     cmo_list* c = new_cmo_list(); | 
|      int len = receive_int32(fd); | 
     int len = receive_int32(fd); | 
|   | 
  | 
|      while (len>0) { | 
     while (len>0) { | 
|          newcmo = receive_cmo(fd); | 
         ob = receive_cmo(fd); | 
|          append_cmo_list(c, newcmo); | 
         append_cmo_list(c, ob); | 
|          len--; | 
         len--; | 
|      } | 
     } | 
|      return c; | 
     return c; | 
|  } | 
 } | 
|   | 
  | 
|   | 
 static cmo_monomial32* receive_cmo_monomial32(int fd) | 
|   | 
 { | 
|   | 
     int i; | 
|   | 
     int len = receive_int32(fd); | 
|   | 
     cmo_monomial32* c = new_cmo_monomial32(len); | 
|   | 
  | 
|   | 
     for(i=0; i<len; i++) { | 
|   | 
         c->exps[i] = receive_int32(fd); | 
|   | 
     } | 
|   | 
     c->coef = receive_cmo(fd); | 
|   | 
     return c; | 
|   | 
 } | 
|   | 
  | 
|  static cmo_zz* receive_cmo_zz(int fd) | 
 static cmo_zz* receive_cmo_zz(int fd) | 
|  { | 
 { | 
|      cmo_zz* c = new_cmo_zz(); | 
     cmo_zz* c = new_cmo_zz(); | 
| Line 235  static cmo_dms_generic* receive_cmo_dms_generic(int fd | 
 
  | 
| Line 257  static cmo_dms_generic* receive_cmo_dms_generic(int fd | 
 
 
 | 
|  static cmo_ring_by_name* receive_cmo_ring_by_name(int fd) | 
 static cmo_ring_by_name* receive_cmo_ring_by_name(int fd) | 
|  { | 
 { | 
|      cmo* ob = receive_cmo(fd); | 
     cmo* ob = receive_cmo(fd); | 
|          /* 意味的チェックが必要 */ | 
     /* 意味的チェックが必要 */ | 
|      return new_cmo_ring_by_name(ob); | 
     return new_cmo_ring_by_name(ob); | 
|  } | 
 } | 
|   | 
  | 
|   | 
 static cmo_distributed_polynomial* receive_cmo_distributed_polynomial(int fd) | 
|   | 
 { | 
|   | 
     cmo* ob; | 
|   | 
     cmo_distributed_polynomial* c = new_cmo_distributed_polynomial(); | 
|   | 
     int len = receive_int32(fd); | 
|   | 
     c->ringdef = receive_cmo(fd); | 
|   | 
  | 
|   | 
     while (len>0) { | 
|   | 
         ob = receive_cmo(fd); | 
|   | 
         append_cmo_list(c, ob); | 
|   | 
         len--; | 
|   | 
     } | 
|   | 
     return c; | 
|   | 
 } | 
|   | 
  | 
|  static cmo_error2* receive_cmo_error2(int fd) | 
 static cmo_error2* receive_cmo_error2(int fd) | 
|  { | 
 { | 
|      cmo* ob = receive_cmo(fd); | 
     cmo* ob = receive_cmo(fd); | 
|      return new_cmo_error2(ob); | 
     return new_cmo_error2(ob); | 
|  } | 
 } | 
|   | 
  | 
|   | 
 /* receive_ox_tag() == OX_DATA の後に呼び出される */ | 
|   | 
 /* 関数ポインタを使った方がきれいに書けるような気がする.  */ | 
|   | 
 /* if (foo[tag] != NULL) foo[tag](fd); とか */ | 
|   | 
  | 
|   | 
 cmo* receive_cmo(int fd) | 
|   | 
 { | 
|   | 
     cmo* m; | 
|   | 
     int tag; | 
|   | 
     tag = receive_int32(fd); | 
|   | 
  | 
|   | 
     switch(tag) { | 
|   | 
     case CMO_NULL: | 
|   | 
         m = receive_cmo_null(fd); | 
|   | 
         break; | 
|   | 
     case CMO_INT32: | 
|   | 
         m = (cmo *)receive_cmo_int32(fd); | 
|   | 
         break; | 
|   | 
     case CMO_STRING: | 
|   | 
         m = (cmo *)receive_cmo_string(fd); | 
|   | 
         break; | 
|   | 
     case CMO_MATHCAP: | 
|   | 
         m = (cmo *)receive_cmo_mathcap(fd); | 
|   | 
         break; | 
|   | 
     case CMO_LIST: | 
|   | 
         m = (cmo *)receive_cmo_list(fd); | 
|   | 
         break; | 
|   | 
     case CMO_MONOMIAL32: | 
|   | 
         m = (cmo *)receive_cmo_monomial32(fd); | 
|   | 
         break; | 
|   | 
     case CMO_ZZ: | 
|   | 
         m = (cmo *)receive_cmo_zz(fd); | 
|   | 
         break; | 
|   | 
     case CMO_ZERO: | 
|   | 
         m = (cmo *)receive_cmo_zero(fd); | 
|   | 
         break; | 
|   | 
     case CMO_DMS_GENERIC: | 
|   | 
         m = (cmo *)receive_cmo_dms_generic(fd); | 
|   | 
         break; | 
|   | 
     case CMO_RING_BY_NAME: | 
|   | 
         m = (cmo *)receive_cmo_ring_by_name(fd); | 
|   | 
         break; | 
|   | 
     case CMO_DISTRIBUTED_POLYNOMIAL: | 
|   | 
         m = (cmo *)receive_cmo_distributed_polynomial(fd); | 
|   | 
         break; | 
|   | 
     case CMO_ERROR2: | 
|   | 
         m = (cmo *)receive_cmo_error2(fd); | 
|   | 
         break; | 
|   | 
     case CMO_DATUM: | 
|   | 
     case CMO_QQ: | 
|   | 
     default: | 
|   | 
         fprintf(stderr, "unknown cmo-type: tag = (%d)\n", m->tag); | 
|   | 
     } | 
|   | 
     return m; | 
|   | 
 } | 
|   | 
  | 
|  static void receive_mpz(int fd, mpz_ptr mpz) | 
 static void receive_mpz(int fd, mpz_ptr mpz) | 
|  { | 
 { | 
|      int i; | 
     int i; | 
| Line 283  cmo_int32* new_cmo_int32(int i) | 
 
  | 
| Line 375  cmo_int32* new_cmo_int32(int i) | 
 
 
 | 
|  cmo_string* new_cmo_string(char* s) | 
 cmo_string* new_cmo_string(char* s) | 
|  { | 
 { | 
|      cmo_string* c = malloc(sizeof(cmo_string)); | 
     cmo_string* c = malloc(sizeof(cmo_string)); | 
|          char *s2 = malloc(strlen(s)+1); | 
  | 
|          strcpy(s2, s); | 
  | 
|   | 
  | 
|      c->tag = CMO_STRING; | 
     c->tag = CMO_STRING; | 
|      c->s   = s2; | 
     if (s != NULL) { | 
|   | 
         c->s = malloc(strlen(s)+1); | 
|   | 
         strcpy(c->s, s); | 
|   | 
     }else { | 
|   | 
         c->s = NULL; | 
|   | 
     } | 
|      return c; | 
     return c; | 
|  } | 
 } | 
|   | 
  | 
| Line 304  cmo_list* new_cmo_list() | 
 
  | 
| Line 398  cmo_list* new_cmo_list() | 
 
 
 | 
|      cmo_list* c = malloc(sizeof(cmo_list)); | 
     cmo_list* c = malloc(sizeof(cmo_list)); | 
|      c->tag    = CMO_LIST; | 
     c->tag    = CMO_LIST; | 
|      c->length = 0; | 
     c->length = 0; | 
|      c->head   = NULL; | 
     c->head->next = NULL; | 
|      return c; | 
     return c; | 
|  } | 
 } | 
|   | 
  | 
| Line 318  cmo_monomial32* new_cmo_monomial32() | 
 
  | 
| Line 412  cmo_monomial32* new_cmo_monomial32() | 
 
 
 | 
|  cmo_monomial32* new_cmo_monomial32_size(int size) | 
 cmo_monomial32* new_cmo_monomial32_size(int size) | 
|  { | 
 { | 
|      cmo_monomial32* c = new_cmo_monomial32(); | 
     cmo_monomial32* c = new_cmo_monomial32(); | 
|          if (size>0) { | 
     if (size>0) { | 
|                  c->length = size; | 
         c->length = size; | 
|                  c->exps = malloc(sizeof(int)*size); | 
         c->exps = malloc(sizeof(int)*size); | 
|          } | 
     } | 
|      return c; | 
     return c; | 
|  } | 
 } | 
|   | 
  | 
| Line 347  cmo_zz* new_cmo_zz_set_si(int i) | 
 
  | 
| Line 441  cmo_zz* new_cmo_zz_set_si(int i) | 
 
 
 | 
|      return c; | 
     return c; | 
|  } | 
 } | 
|   | 
  | 
|   | 
 cmo_zz *new_cmo_zz_set_string(char *s) | 
|   | 
 { | 
|   | 
     cmo_zz* c = new_cmo_zz_noinit(); | 
|   | 
     mpz_init_set_str(c->mpz, s, 10); | 
|   | 
     return c; | 
|   | 
 } | 
|   | 
  | 
|  cmo_zz* new_cmo_zz_size(int size) | 
 cmo_zz* new_cmo_zz_size(int size) | 
|  { | 
 { | 
|      cmo_zz* c = new_cmo_zz(); | 
     cmo_zz* c = new_cmo_zz(); | 
| Line 384  cmo_indeterminate* new_cmo_indeterminate(cmo* ob) | 
 
  | 
| Line 485  cmo_indeterminate* new_cmo_indeterminate(cmo* ob) | 
 
 
 | 
|      return c; | 
     return c; | 
|  } | 
 } | 
|   | 
  | 
|   | 
 cmo_distributed_polynomial* new_cmo_distributed_polynomial() | 
|   | 
 { | 
|   | 
     cmo_distributed_polynomial* c = malloc(sizeof(cmo_distributed_polynomial)); | 
|   | 
     c->tag     = CMO_DISTRIBUTED_POLYNOMIAL; | 
|   | 
     c->length  = 0; | 
|   | 
     c->head->next = NULL; | 
|   | 
     c->ringdef = NULL; | 
|   | 
     return c; | 
|   | 
 } | 
|   | 
  | 
|  cmo_error2* new_cmo_error2(cmo* ob) | 
 cmo_error2* new_cmo_error2(cmo* ob) | 
|  { | 
 { | 
|      cmo_error2* c = malloc(sizeof(cmo_error2)); | 
     cmo_error2* c = malloc(sizeof(cmo_error2)); | 
| Line 392  cmo_error2* new_cmo_error2(cmo* ob) | 
 
  | 
| Line 503  cmo_error2* new_cmo_error2(cmo* ob) | 
 
 
 | 
|      return c; | 
     return c; | 
|  } | 
 } | 
|   | 
  | 
|  /* receive_ox_tag() == OX_DATA の後に呼び出される */ | 
  | 
|  /* 関数ポインタを使った方がきれいに書けるような気がする.  */ | 
  | 
|  /* if (foo[tag] != NULL) foo[tag](fd); とか */ | 
  | 
|   | 
  | 
|  cmo* receive_cmo(int fd) | 
  | 
|  { | 
  | 
|      cmo* m; | 
  | 
|      int tag; | 
  | 
|      tag = receive_int32(fd); | 
  | 
|   | 
  | 
|      switch(tag) { | 
  | 
|      case CMO_NULL: | 
  | 
|          m = receive_cmo_null(fd); | 
  | 
|          break; | 
  | 
|      case CMO_INT32: | 
  | 
|          m = (cmo *)receive_cmo_int32(fd); | 
  | 
|          break; | 
  | 
|      case CMO_STRING: | 
  | 
|          m = (cmo *)receive_cmo_string(fd); | 
  | 
|          break; | 
  | 
|      case CMO_MATHCAP: | 
  | 
|          m = (cmo *)receive_cmo_mathcap(fd); | 
  | 
|          break; | 
  | 
|      case CMO_LIST: | 
  | 
|          m = (cmo *)receive_cmo_list(fd); | 
  | 
|          break; | 
  | 
|      case CMO_ZZ: | 
  | 
|          m = (cmo *)receive_cmo_zz(fd); | 
  | 
|          break; | 
  | 
|      case CMO_ERROR2: | 
  | 
|          m = (cmo *)receive_cmo_error2(fd); | 
  | 
|          break; | 
  | 
|      case CMO_DATUM: | 
  | 
|      case CMO_QQ: | 
  | 
|      case CMO_ZERO: | 
  | 
|      case CMO_DMS: | 
  | 
|      default: | 
  | 
|          fprintf(stderr, "unknown cmo-type: tag = (%d)\n", m->tag); | 
  | 
|      } | 
  | 
|      return m; | 
  | 
|  } | 
  | 
|   | 
  | 
|  void send_ox_command(int fd, int sm_command) | 
 void send_ox_command(int fd, int sm_command) | 
|  { | 
 { | 
|      send_ox_tag(fd, OX_COMMAND); | 
     send_ox_tag(fd, OX_COMMAND); | 
| Line 443  void send_ox_command(int fd, int sm_command) | 
 
  | 
| Line 512  void send_ox_command(int fd, int sm_command) | 
 
 
 | 
|  int print_cmo(cmo* c) | 
 int print_cmo(cmo* c) | 
|  { | 
 { | 
|      int tag = c->tag; | 
     int tag = c->tag; | 
|      fprintf(stderr, "local::tag = (%d): ", tag); | 
  | 
|   | 
 #ifdef DEBUG | 
|   | 
     symbol* symp = lookup_by_tag(tag); | 
|   | 
     if (symp != NULL) { | 
|   | 
         fprintf(stderr, "local::tag = %s: ", symp->key); | 
|   | 
     }else { | 
|   | 
         fprintf(stderr, "local::tag = %d: ", tag); | 
|   | 
     } | 
|   | 
 #endif | 
|   | 
  | 
|      switch(tag) { | 
     switch(tag) { | 
|      case CMO_LIST: | 
     case CMO_LIST: | 
|          print_cmo_list((cmo_list *)c); | 
         print_cmo_list((cmo_list *)c); | 
| Line 492  int print_cmo_string(cmo_string* c) | 
 
  | 
| Line 570  int print_cmo_string(cmo_string* c) | 
 
 
 | 
|      fprintf(stderr, "cmo_string = (%s)\n", c->s); | 
     fprintf(stderr, "cmo_string = (%s)\n", c->s); | 
|  } | 
 } | 
|   | 
  | 
|  /* ox_start() から呼び出す */ | 
  | 
|  static int read_password(int fd, char* passwd) | 
  | 
|  { | 
  | 
|      char buff[1024]; | 
  | 
|      int n; | 
  | 
|      if ((n = read(fd, buff, 1024)) != strlen(passwd)) { | 
  | 
|          fprintf(stderr, "Socket#%d: Login incorrect.\n", fd); | 
  | 
|          fprintf(stderr, "password = (%s), received = (%s).\n", passwd, buff); | 
  | 
|          fprintf(stderr, "         = (%d), received = (%d).\n", strlen(passwd), n); | 
  | 
|      } | 
  | 
|      fflush(stderr); | 
  | 
|  } | 
  | 
|   | 
  | 
|  void ox_close(ox_file_t sv) | 
 void ox_close(ox_file_t sv) | 
|  { | 
 { | 
|      send_ox_command(sv->control, SM_control_kill); | 
     send_ox_command(sv->control, SM_control_kill); | 
|  #if DEBUG | 
 #ifdef DEBUG | 
|      sleep(2); /* OpenXM server の終了を待つ. あまり意味はない. */ | 
     sleep(2); /* OpenXM server の終了を待つ. あまり意味はない. */ | 
|      fprintf(stderr, "I have closed an Open XM server.\n"); | 
     fprintf(stderr, "I have closed the connection to an Open XM server.\n"); | 
|  #endif | 
 #endif | 
|  } | 
 } | 
|   | 
  | 
|  void ox_executeStringByLocalParser(ox_file_t sv, char* s) | 
 void ox_executeStringByLocalParser(ox_file_t sv, char* s) | 
|  { | 
 { | 
|      /* 文字列ををスタックにプッシュ. */ | 
     if (s != NULL) { | 
|      send_ox_cmo(sv->stream, (cmo *)new_cmo_string(s)); | 
         /* 文字列ををスタックにプッシュ. */ | 
|   | 
         send_ox_cmo(sv->stream, (cmo *)new_cmo_string(s)); | 
|      /* サーバに実行させる. */ | 
         /* サーバに実行させる. */ | 
|      send_ox_command(sv->stream, SM_executeStringByLocalParser); | 
         send_ox_command(sv->stream, SM_executeStringByLocalParser); | 
|   | 
     } | 
|  } | 
 } | 
|   | 
  | 
|  /* ox_mathcap() をコールする.  */ | 
 /* ox_mathcap() をコールする.  */ | 
| Line 549  cmo* ox_pop_cmo(ox_file_t sv, int fd) | 
 
  | 
| Line 615  cmo* ox_pop_cmo(ox_file_t sv, int fd) | 
 
 
 | 
|      return receive_cmo(fd); | 
     return receive_cmo(fd); | 
|  } | 
 } | 
|   | 
  | 
|  /* | 
 /* 手抜き. (後で改善しよう...) */ | 
|   | 
 static char *create_otp() | 
|   | 
 { | 
|   | 
     static char otp[] = "otpasswd"; | 
|   | 
     return otp; | 
|   | 
 } | 
|   | 
  | 
|   | 
 /* OneTimePassword の処理 */ | 
|   | 
 static int login_with_otp(int fd, char* passwd) | 
|   | 
 { | 
|   | 
     int len   = strlen(passwd)+1; | 
|   | 
     char *buf = alloca(len); | 
|   | 
     int n     = read(fd, buf, len); | 
|   | 
     int ret   = strcmp(passwd, buf); | 
|   | 
  | 
|   | 
 #ifdef DEBUG | 
|   | 
     if (ret != 0) { | 
|   | 
         fprintf(stderr, "Socket#%d: Login incorrect.\n", fd); | 
|   | 
     }else { | 
|   | 
         fprintf(stderr, "Socket#%d: login!.\n", fd); | 
|   | 
     } | 
|   | 
     fprintf(stderr, "password = (%s), %d bytes.\n", passwd, len); | 
|   | 
     fprintf(stderr, "received = (%s), %d bytes.\n", buf, n); | 
|   | 
     fflush(stderr); | 
|   | 
 #endif | 
|   | 
  | 
|   | 
     return ret; | 
|   | 
 } | 
|   | 
  | 
|   | 
 static int exists_ox(char *dir, char *prog) | 
|   | 
 { | 
|   | 
     char *path = alloca(strlen(dir)+strlen(prog)+6); | 
|   | 
     sprintf(path, "%s/%s", dir, prog); | 
|   | 
     return access(path, X_OK|R_OK); | 
|   | 
 } | 
|   | 
  | 
|   | 
 static char *search_ox(char *prog) | 
|   | 
 { | 
|   | 
     char *env = getenv("OpenXM_HOME"); | 
|   | 
     char *dir; | 
|   | 
     if (env != NULL) { | 
|   | 
         dir = malloc(strlen(env)+5); | 
|   | 
         sprintf(dir, "%s/bin", env); | 
|   | 
         if (exists_ox(dir, prog) == 0) { | 
|   | 
             return dir; | 
|   | 
         } | 
|   | 
         free(dir); | 
|   | 
     } | 
|   | 
     dir = "/usr/local/OpenXM/bin"; | 
|   | 
     if (exists_ox(dir, prog) == 0) { | 
|   | 
         return dir; | 
|   | 
     } | 
|   | 
     dir = "."; | 
|   | 
     if (exists_ox(dir, prog) == 0) { | 
|   | 
         return dir; | 
|   | 
     } | 
|   | 
     return NULL; | 
|   | 
 } | 
|   | 
  | 
|   | 
 static int mysocketAccept2(int fd, char *pass) | 
|   | 
 { | 
|   | 
     fd = mysocketAccept(fd); | 
|   | 
     if(login_with_otp(fd, pass)==0) { | 
|   | 
         decideByteOrderClient(fd, 0); | 
|   | 
         return fd; | 
|   | 
     } | 
|   | 
     close(fd); | 
|   | 
     return -1; | 
|   | 
 } | 
|   | 
  | 
|   | 
 /* | 
|   | 
    (-reverse 版の ox_start) | 
|     ox_start は クライアントが呼び出すための関数である. | 
    ox_start は クライアントが呼び出すための関数である. | 
|     サーバでは使われない.  prog1 は コントロールサーバであり, | 
    サーバでは使われない.  ctl_prog は コントロールサーバであり, | 
|     -ox, -reverse, -data, -control, -pass, -host | 
    -ox, -reverse, -data, -control, -pass, -host | 
|     というオプションを理解することを仮定する. prog2 は計算サーバである. | 
    というオプションを理解することを仮定する. dat_prog は計算サーバである. | 
|     接続時には, sv->control を先にオープンする. | 
    接続時には, sv->control を先にオープンする. | 
|  */ | 
 */ | 
|   | 
  | 
|  ox_file_t ox_start(char* host, char* prog1, char* prog2) | 
 ox_file_t ox_start(char* host, char* ctl_prog, char* dat_prog) | 
|  { | 
 { | 
|      static char pass[] = "passwd"; | 
     char *pass; | 
|      char ctl[16], dat[16]; | 
     char ctl[16], dat[16]; | 
|      short portControl = 0; /* short であることに注意 */ | 
     short portControl = 0; /* short であることに注意 */ | 
|      short portStream  = 0; | 
     short portStream  = 0; | 
|      ox_file_t sv = malloc(sizeof(__ox_file_struct)); | 
     ox_file_t sv = NULL; | 
|   | 
     char *dir; | 
|   | 
  | 
|   | 
     if ((dir = search_ox(ctl_prog)) == NULL) { | 
|   | 
         fprintf(stderr, "client:: %s not found.\n", ctl_prog); | 
|   | 
         return NULL; | 
|   | 
     } | 
|   | 
     sv = malloc(sizeof(__ox_file_struct)); | 
|      sv->control = mysocketListen(host, &portControl); | 
     sv->control = mysocketListen(host, &portControl); | 
|      sv->stream  = mysocketListen(host, &portStream); | 
     sv->stream  = mysocketListen(host, &portStream); | 
|   | 
  | 
|      sprintf(ctl, "%d", portControl); | 
     sprintf(ctl, "%d", portControl); | 
|      sprintf(dat, "%d", portStream); | 
     sprintf(dat, "%d", portStream); | 
|   | 
     pass = create_otp(); | 
|   | 
  | 
|      if (fork() == 0) { | 
     if (fork() == 0) { | 
|          dup2(2, 1); | 
         dup2(2, 1); | 
|          dup2(open(DEFAULT_LOGFILE, O_RDWR|O_CREAT|O_TRUNC, 0644), 2); | 
         dup2(open(DEFAULT_LOGFILE, O_RDWR|O_CREAT|O_TRUNC, 0644), 2); | 
|          execl(prog1, prog1, "-reverse", "-ox", prog2, | 
         chdir(dir); | 
|   | 
         execl(ctl_prog, ctl_prog, "-reverse", "-ox", dat_prog, | 
|                "-data", dat, "-control", ctl, "-pass", pass, | 
               "-data", dat, "-control", ctl, "-pass", pass, | 
|                "-host", host, NULL); | 
               "-host", host, NULL); | 
|      } | 
     } | 
|   | 
  | 
|      sv->control = mysocketAccept(sv->control); | 
     if ((sv->control = mysocketAccept2(sv->control, pass)) == -1) { | 
|      read_password(sv->control, pass); | 
         close(sv->stream); | 
|      decideByteOrder(sv->control, sv->control, 0); | 
         return NULL; | 
|   | 
     } | 
|   | 
     /* 10マイクロ秒, 時間稼ぎする. */ | 
|      usleep(10); | 
     usleep(10); | 
|      sv->stream  = mysocketAccept(sv->stream); | 
     if((sv->stream  = mysocketAccept2(sv->stream, pass)) == -1) { | 
|      read_password(sv->stream, pass); | 
         return NULL; | 
|      decideByteOrder(sv->stream, sv->stream, 0); | 
     } | 
|   | 
     return sv; | 
|   | 
 } | 
|   | 
  | 
|   | 
 /* | 
|   | 
    (-insecure 版の ox_start)  まだ、中身はありません。 | 
|   | 
    ox_start_insecure_nonreverse は クライアントが呼び出すための関数である. | 
|   | 
    接続時には, sv->control を先にオープンする. | 
|   | 
    既定値: | 
|   | 
    portControl = 1200 | 
|   | 
    portStream  = 1300 | 
|   | 
 */ | 
|   | 
  | 
|   | 
 ox_file_t ox_start_insecure_nonreverse(char* host, short portControl, short portStream) | 
|   | 
 { | 
|   | 
     ox_file_t sv = malloc(sizeof(__ox_file_struct)); | 
|   | 
  | 
|   | 
     sv->control = mysocketOpen(host, portControl); | 
|   | 
 #if 0 | 
|   | 
     /* ox は insecure のとき byte order の決定が正しくできないようだ... */ | 
|   | 
     decideByteOrderClient(sv->control, 0); | 
|   | 
 #endif | 
|   | 
     /* 10マイクロ秒, 時間稼ぎする. */ | 
|   | 
     usleep(10); | 
|   | 
     sv->stream  = mysocketOpen(host, portStream); | 
|   | 
     decideByteOrderClient(sv->stream, 0); | 
|      return sv; | 
     return sv; | 
|  } | 
 } | 
|   | 
  | 
| Line 601  void ox_reset(ox_file_t sv) | 
 
  | 
| Line 773  void ox_reset(ox_file_t sv) | 
 
 
 | 
|      } | 
     } | 
|   | 
  | 
|      send_ox_tag(sv->stream, OX_SYNC_BALL); | 
     send_ox_tag(sv->stream, OX_SYNC_BALL); | 
|  #if DEBUG | 
 #ifdef DEBUG | 
|      fprintf(stderr, "I have reset an Open XM server.\n"); | 
     fprintf(stderr, "I have reset an Open XM server.\n"); | 
|  #endif | 
 #endif | 
|  } | 
 } | 
| Line 633  static int cmolen_cmo_mathcap(cmo_mathcap* c) | 
 
  | 
| Line 805  static int cmolen_cmo_mathcap(cmo_mathcap* c) | 
 
 
 | 
|   | 
  | 
|  static int cmolen_cmo_list(cmo_list* c) | 
 static int cmolen_cmo_list(cmo_list* c) | 
|  { | 
 { | 
|      int size = sizeof(c->head); | 
     int size = sizeof(int); | 
|      cell* cp = c->head; | 
     cell* cp = c->head; | 
|   | 
  | 
|      while(cp != NULL) { | 
     while(cp->next != NULL) { | 
|          size += cmolen_cmo(cp->cmo); | 
         size += cmolen_cmo(cp->cmo); | 
|          cp = cp->next; | 
         cp = cp->next; | 
|      } | 
     } | 
| Line 645  static int cmolen_cmo_list(cmo_list* c) | 
 
  | 
| Line 817  static int cmolen_cmo_list(cmo_list* c) | 
 
 
 | 
|   | 
  | 
|  static int cmolen_cmo_monomial32(cmo_monomial32* c) | 
 static int cmolen_cmo_monomial32(cmo_monomial32* c) | 
|  { | 
 { | 
|          int len = (c->length + 1)*sizeof(int); | 
     int len = (c->length + 1)*sizeof(int); | 
|          return len + cmolen_cmo(c->coef); | 
     return len + cmolen_cmo(c->coef); | 
|  } | 
 } | 
|   | 
  | 
|  static int cmolen_cmo_zz(cmo_zz* c) | 
 static int cmolen_cmo_zz(cmo_zz* c) | 
| Line 655  static int cmolen_cmo_zz(cmo_zz* c) | 
 
  | 
| Line 827  static int cmolen_cmo_zz(cmo_zz* c) | 
 
 
 | 
|      return sizeof(len) + len*sizeof(int); | 
     return sizeof(len) + len*sizeof(int); | 
|  } | 
 } | 
|   | 
  | 
|   | 
 static int cmolen_cmo_distributed_polynomial(cmo_distributed_polynomial* c) | 
|   | 
 { | 
|   | 
     return cmolen_cmo_list((cmo_list *)c) + cmolen_cmo(c->ringdef); | 
|   | 
 } | 
|   | 
  | 
|  /* CMO がバイトエンコードされた場合のバイト列の長さを求める */ | 
 /* CMO がバイトエンコードされた場合のバイト列の長さを求める */ | 
|  int cmolen_cmo(cmo* c) | 
 int cmolen_cmo(cmo* c) | 
|  { | 
 { | 
| Line 674  int cmolen_cmo(cmo* c) | 
 
  | 
| Line 851  int cmolen_cmo(cmo* c) | 
 
 
 | 
|          break; | 
         break; | 
|      case CMO_MATHCAP: | 
     case CMO_MATHCAP: | 
|      case CMO_RING_BY_NAME: | 
     case CMO_RING_BY_NAME: | 
|   | 
     case CMO_INDETERMINATE: | 
|      case CMO_ERROR2: | 
     case CMO_ERROR2: | 
|          size += cmolen_cmo_mathcap((cmo_mathcap *)c); | 
         size += cmolen_cmo_mathcap((cmo_mathcap *)c); | 
|          break; | 
         break; | 
| Line 686  int cmolen_cmo(cmo* c) | 
 
  | 
| Line 864  int cmolen_cmo(cmo* c) | 
 
 
 | 
|      case CMO_ZZ: | 
     case CMO_ZZ: | 
|          size += cmolen_cmo_zz((cmo_zz *)c); | 
         size += cmolen_cmo_zz((cmo_zz *)c); | 
|          break; | 
         break; | 
|   | 
     case CMO_DISTRIBUTED_POLYNOMIAL: | 
|   | 
         size += cmolen_cmo_distributed_polynomial((cmo_distributed_polynomial *)c); | 
|   | 
         break; | 
|      default: | 
     default: | 
|      } | 
     } | 
|      return size; | 
     return size; | 
|  } | 
 } | 
|   | 
  | 
|  static char* dump_cmo_null(char* array, cmo_null* m) | 
 static int  d_ptr; | 
|   | 
 static char *d_buf; | 
|   | 
  | 
|   | 
 int init_dump_buffer(char *s) | 
|  { | 
 { | 
|      return array; | 
     d_buf = s; | 
|   | 
     d_ptr = 0; | 
|  } | 
 } | 
|   | 
  | 
|  static char* dump_cmo_int32(char* array, cmo_int32* m) | 
 static int dump_cmo_null(cmo_null* m) | 
|  { | 
 { | 
|      return dump_integer(array, m->i); | 
     return 0; | 
|  } | 
 } | 
|   | 
  | 
|  static char* dump_cmo_string(char* array, cmo_string* m) | 
 static int dump_cmo_int32(cmo_int32* m) | 
|  { | 
 { | 
|   | 
     dump_integer(m->i); | 
|   | 
 } | 
|   | 
  | 
|   | 
 static int dump_cmo_string(cmo_string* m) | 
|   | 
 { | 
|      int len = strlen(m->s); | 
     int len = strlen(m->s); | 
|      array = dump_integer(array, len); | 
     dump_integer(len); | 
|      memcpy(array, m->s, len); | 
     dump_string(m->s, len); | 
|      return array + len; | 
  | 
|  } | 
 } | 
|   | 
  | 
|  static char* dump_cmo_mathcap(char* array, cmo_mathcap* c) | 
 static int dump_cmo_mathcap(cmo_mathcap* c) | 
|  { | 
 { | 
|      return dump_cmo(array, c->ob); | 
     dump_cmo(c->ob); | 
|  } | 
 } | 
|   | 
  | 
|  static char* dump_cmo_list(char* array, cmo_list* m) | 
 static int dump_cmo_list(cmo_list* m) | 
|  { | 
 { | 
|      cell* cp = m->head; | 
     cell* cp = m->head; | 
|      int len = length_cmo_list(m); | 
     int len = length_cmo_list(m); | 
|      array = dump_integer(array, len); | 
     dump_integer(len); | 
|   | 
  | 
|      while(cp != NULL) { | 
     while(cp->next != NULL) { | 
|          array = dump_cmo(array, cp->cmo); | 
         dump_cmo(cp->cmo); | 
|          cp = cp->next; | 
         cp = cp->next; | 
|      } | 
     } | 
|      return array; | 
  | 
|  } | 
 } | 
|   | 
  | 
|  static char* dump_cmo_monomial32(char* array, cmo_monomial32* c) | 
 static int dump_cmo_monomial32(cmo_monomial32* c) | 
|  { | 
 { | 
|          int i; | 
     int i; | 
|          int length = c->length; | 
     int length = c->length; | 
|          array = dump_integer(array, c->length); | 
     dump_integer(c->length); | 
|          for(i=0; i<length; i++) { | 
     for(i=0; i<length; i++) { | 
|                  array = dump_integer(array, c->exps[i]); | 
         dump_integer(c->exps[i]); | 
|          } | 
     } | 
|          array = dump_cmo(array, c->coef); | 
     dump_cmo(c->coef); | 
|          return array; | 
  | 
|  } | 
 } | 
|   | 
  | 
|  static char* dump_cmo_zz(char* array, cmo_zz* c) | 
 static int dump_cmo_zz(cmo_zz* c) | 
|  { | 
 { | 
|      return dump_mpz(array, c->mpz); | 
     dump_mpz(c->mpz); | 
|  } | 
 } | 
|   | 
  | 
|   | 
 static int dump_cmo_distributed_polynomial(cmo_distributed_polynomial* m) | 
|   | 
 { | 
|   | 
     cell* cp = m->head; | 
|   | 
     int len = length_cmo_list((cmo_list *)m); | 
|   | 
     dump_integer(len); | 
|   | 
     dump_cmo(m->ringdef); | 
|   | 
     while(cp != NULL) { | 
|   | 
         dump_cmo(cp->cmo); | 
|   | 
         cp = cp->next; | 
|   | 
     } | 
|   | 
 } | 
|   | 
  | 
|  /* タグを書き出してから、各関数を呼び出す */ | 
 /* タグを書き出してから、各関数を呼び出す */ | 
|  char* dump_cmo(char* array, cmo* m) | 
 int dump_cmo(cmo* m) | 
|  { | 
 { | 
|      array = dump_integer(array, m->tag); | 
     dump_integer(m->tag); | 
|      switch(m->tag) { | 
     switch(m->tag) { | 
|      case CMO_NULL: | 
     case CMO_NULL: | 
|      case CMO_ZERO: | 
     case CMO_ZERO: | 
|      case CMO_DMS_GENERIC: | 
     case CMO_DMS_GENERIC: | 
|          return dump_cmo_null(array, m); | 
         dump_cmo_null(m); | 
|   | 
         break; | 
|      case CMO_INT32: | 
     case CMO_INT32: | 
|          return dump_cmo_int32(array, (cmo_int32 *)m); | 
         dump_cmo_int32((cmo_int32 *)m); | 
|   | 
         break; | 
|      case CMO_STRING: | 
     case CMO_STRING: | 
|          return dump_cmo_string(array, (cmo_string *)m); | 
         dump_cmo_string((cmo_string *)m); | 
|   | 
         break; | 
|      case CMO_MATHCAP: | 
     case CMO_MATHCAP: | 
|      case CMO_RING_BY_NAME: | 
     case CMO_RING_BY_NAME: | 
|   | 
     case CMO_INDETERMINATE: | 
|      case CMO_ERROR2: | 
     case CMO_ERROR2: | 
|          return dump_cmo_mathcap(array, (cmo_mathcap *)m); | 
         dump_cmo_mathcap((cmo_mathcap *)m); | 
|   | 
         break; | 
|      case CMO_LIST: | 
     case CMO_LIST: | 
|          return dump_cmo_list(array, (cmo_list *)m); | 
         dump_cmo_list((cmo_list *)m); | 
|   | 
         break; | 
|      case CMO_MONOMIAL32: | 
     case CMO_MONOMIAL32: | 
|          return dump_cmo_monomial32(array, (cmo_monomial32 *)m); | 
         dump_cmo_monomial32((cmo_monomial32 *)m); | 
|   | 
         break; | 
|      case CMO_ZZ: | 
     case CMO_ZZ: | 
|          return dump_cmo_zz(array, (cmo_zz *)m); | 
         dump_cmo_zz((cmo_zz *)m); | 
|   | 
         break; | 
|   | 
     case CMO_DISTRIBUTED_POLYNOMIAL: | 
|   | 
         dump_cmo_distributed_polynomial((cmo_distributed_polynomial *)m); | 
|   | 
         break; | 
|      default: | 
     default: | 
|          return NULL; | 
  | 
|      } | 
     } | 
|  } | 
 } | 
|   | 
  | 
|  static char* dump_integer(char* array, int x) | 
 static int dump_mpz(mpz_ptr mpz) | 
|  { | 
 { | 
|      int nx = htonl(x); | 
  | 
|      memcpy(array, &nx, sizeof(int)); | 
  | 
|      return array + sizeof(int); | 
  | 
|  } | 
  | 
|   | 
  | 
|  static char* dump_mpz(char* array, mpz_ptr mpz) | 
  | 
|  { | 
  | 
|      int i; | 
     int i; | 
|      int len = abs(mpz->_mp_size); | 
     int len = abs(mpz->_mp_size); | 
|      array = dump_integer(array, mpz->_mp_size); | 
     dump_integer(mpz->_mp_size); | 
|      for(i=0; i<len; i++) { | 
     for(i=0; i<len; i++) { | 
|          array = dump_integer(array, mpz->_mp_d[i]); | 
         dump_integer(mpz->_mp_d[i]); | 
|      } | 
     } | 
|      return array; | 
     return; | 
|  } | 
 } | 
|   | 
  | 
|   | 
 static int dump_string(char *s, int len) | 
|   | 
 { | 
|   | 
     memcpy(&d_buf[d_ptr], s, len); | 
|   | 
     d_ptr += len; | 
|   | 
 } | 
|   | 
  | 
|   | 
 static int dump_integer(int x) | 
|   | 
 { | 
|   | 
     int nx = htonl(x); | 
|   | 
     dump_string((char *)&nx, sizeof(int)); | 
|   | 
 } | 
|   | 
  | 
|   | 
 int dump_ox_data(ox_data* m) | 
|   | 
 { | 
|   | 
     dump_integer(OX_DATA); | 
|   | 
     dump_integer(-1); | 
|   | 
     dump_cmo(m->cmo); | 
|   | 
 } | 
|   | 
  | 
|   | 
 int dump_ox_command(ox_command* m) | 
|   | 
 { | 
|   | 
     dump_integer(OX_COMMAND); | 
|   | 
     dump_integer(-1); | 
|   | 
     dump_integer(m->command); | 
|   | 
 } | 
|   | 
  | 
|  int send_ox(ox_file_t s, ox *m) | 
 int send_ox(ox_file_t s, ox *m) | 
|  { | 
 { | 
|      int tag = m->tag; | 
     int tag = m->tag; | 
| Line 830  static int send_cmo_int32(int fd, cmo_int32* m) | 
 
  | 
| Line 1057  static int send_cmo_int32(int fd, cmo_int32* m) | 
 
 
 | 
|   | 
  | 
|  static int send_cmo_string(int fd, cmo_string* m) | 
 static int send_cmo_string(int fd, cmo_string* m) | 
|  { | 
 { | 
|      int len = strlen(m->s); | 
     int len = (m->s != NULL)? strlen(m->s): 0; | 
|      send_int32(fd, len); | 
     send_int32(fd, len); | 
|      if (len > 0) { | 
     if (len > 0) { | 
|          write(fd, m->s, len); | 
         write(fd, m->s, len); | 
| Line 850  static int send_cmo_list(int fd, cmo_list* c) | 
 
  | 
| Line 1077  static int send_cmo_list(int fd, cmo_list* c) | 
 
 
 | 
|      int len = length_cmo_list(c); | 
     int len = length_cmo_list(c); | 
|      send_int32(fd, len); | 
     send_int32(fd, len); | 
|   | 
  | 
|      while(cp != NULL) { | 
     while(cp->next != NULL) { | 
|          send_cmo(fd, cp->cmo); | 
         send_cmo(fd, cp->cmo); | 
|          cp = cp->next; | 
         cp = cp->next; | 
|      } | 
     } | 
|      return 0; | 
     return 0; | 
|  } | 
 } | 
|   | 
  | 
|   | 
 static int send_cmo_distributed_polynomial(int fd, cmo_distributed_polynomial* c) | 
|   | 
 { | 
|   | 
     cell* cp = c->head; | 
|   | 
     int len = length_cmo_list((cmo_list *)c); | 
|   | 
     send_int32(fd, len); | 
|   | 
     send_cmo(fd, c->ringdef); | 
|   | 
  | 
|   | 
     while(cp->next != NULL) { | 
|   | 
         send_cmo(fd, cp->cmo); | 
|   | 
         cp = cp->next; | 
|   | 
     } | 
|   | 
     return 0; | 
|   | 
 } | 
|   | 
  | 
|   | 
 static int send_cmo_monomial32(int fd, cmo_monomial32* c) | 
|   | 
 { | 
|   | 
     int i; | 
|   | 
     int len = c->length; | 
|   | 
     send_int32(fd, len); | 
|   | 
     for(i=0; i<len; i++) { | 
|   | 
         send_int32(fd, c->exps[i]); | 
|   | 
     } | 
|   | 
     send_cmo(fd, c->coef); | 
|   | 
     return 0; | 
|   | 
 } | 
|   | 
  | 
|  static int send_cmo_zz(int fd, cmo_zz* c) | 
 static int send_cmo_zz(int fd, cmo_zz* c) | 
|  { | 
 { | 
|      send_mpz(fd, c->mpz); | 
     send_mpz(fd, c->mpz); | 
|   | 
     return 0; | 
|  } | 
 } | 
|   | 
  | 
|  static int send_cmo_error2(int fd, cmo_error2* c) | 
 static int send_cmo_error2(int fd, cmo_error2* c) | 
| Line 876  int send_cmo(int fd, cmo* c) | 
 
  | 
| Line 1130  int send_cmo(int fd, cmo* c) | 
 
 
 | 
|      send_int32(fd, tag); | 
     send_int32(fd, tag); | 
|      switch(tag) { | 
     switch(tag) { | 
|      case CMO_NULL: | 
     case CMO_NULL: | 
|   | 
     case CMO_ZERO: | 
|   | 
     case CMO_DMS_GENERIC: | 
|          send_cmo_null(fd, c);  /* 空の関数 */ | 
         send_cmo_null(fd, c);  /* 空の関数 */ | 
|          break; | 
         break; | 
|      case CMO_INT32: | 
     case CMO_INT32: | 
| Line 885  int send_cmo(int fd, cmo* c) | 
 
  | 
| Line 1141  int send_cmo(int fd, cmo* c) | 
 
 
 | 
|          send_cmo_string(fd, (cmo_string *)c); | 
         send_cmo_string(fd, (cmo_string *)c); | 
|          break; | 
         break; | 
|      case CMO_MATHCAP: | 
     case CMO_MATHCAP: | 
|   | 
     case CMO_ERROR2: | 
|   | 
     case CMO_RING_BY_NAME: | 
|   | 
     case CMO_INDETERMINATE: | 
|          send_cmo_mathcap(fd, (cmo_mathcap *)c); | 
         send_cmo_mathcap(fd, (cmo_mathcap *)c); | 
|          break; | 
         break; | 
|      case CMO_LIST: | 
     case CMO_LIST: | 
|          send_cmo_list(fd, (cmo_list *)c); | 
         send_cmo_list(fd, (cmo_list *)c); | 
|          break; | 
         break; | 
|   | 
     case CMO_MONOMIAL32: | 
|   | 
         send_cmo_monomial32(fd, (cmo_monomial32 *)c); | 
|   | 
         break; | 
|      case CMO_ZZ: | 
     case CMO_ZZ: | 
|          send_cmo_zz(fd, (cmo_zz *)c); | 
         send_cmo_zz(fd, (cmo_zz *)c); | 
|          break; | 
         break; | 
|      case CMO_ERROR2: | 
     case CMO_DISTRIBUTED_POLYNOMIAL: | 
|          send_cmo_error2(fd, (cmo_error2 *)c); | 
         send_cmo_distributed_polynomial(fd, (cmo_distributed_polynomial *)c); | 
|          break; | 
         break; | 
|      default: | 
     default: | 
|      } | 
     } | 
| Line 927  ox_command* new_ox_command(int sm_code) | 
 
  | 
| Line 1189  ox_command* new_ox_command(int sm_code) | 
 
 
 | 
|      return m; | 
     return m; | 
|  } | 
 } | 
|   | 
  | 
|  char* dump_ox_data(char* array, ox_data* m) | 
  | 
|  { | 
  | 
|      array = dump_integer(array, OX_DATA); | 
  | 
|      array = dump_integer(array, -1); | 
  | 
|      return dump_cmo(array, m->cmo); | 
  | 
|  } | 
  | 
|   | 
  | 
|  char* dump_ox_command(char* array, ox_command* m) | 
  | 
|  { | 
  | 
|      array = dump_integer(array, OX_COMMAND); | 
  | 
|      array = dump_integer(array, -1); | 
  | 
|      return dump_integer(array, m->command); | 
  | 
|  } | 
  | 
|   | 
  | 
|  #define  MAX_TYPES  8 | 
 #define  MAX_TYPES  8 | 
|  static int known_types[] = { | 
 static int known_types[] = { | 
|      -1,   /* gate keeper */ | 
     -1,   /* gate keeper */ | 
| Line 953  static int known_types[] = { | 
 
  | 
| Line 1201  static int known_types[] = { | 
 
 
 | 
|      CMO_ERROR2, | 
     CMO_ERROR2, | 
|  }; | 
 }; | 
|   | 
  | 
|  #define ID_TEMP   "(CMO_MATHCAP, (CMO_LIST, (CMO_LIST, (CMO_INT32, %d), (CMO_STRING, \"%s\"), (CMO_STRING, \"%s\"), (CMO_STRING, \"%s\")), (CMO_LIST, (CMO_INT32, 1), (CMO_INT32, 2), (CMO_INT32, 4), (CMO_INT32, 5), (CMO_INT32, 17), (CMO_INT32, 20), (CMO_INT32, 2130706434))))\n" | 
 #define ID_TEMP   "(CMO_LIST, (CMO_INT32, %d), (CMO_STRING, \"%s\"), (CMO_STRING, \"%s\"), (CMO_STRING, \"%s\"))" | 
|   | 
  | 
|  cmo* make_mathcap_object2(int ver, char* ver_s, char* sysname) | 
  | 
|   | 
 static cmo_list* make_list_of_id(int ver, char* ver_s, char* sysname) | 
|  { | 
 { | 
|      cmo *cap; | 
     cmo_list *cap; | 
|      char buff[8192]; | 
     char buff[512]; | 
|   | 
  | 
|      setgetc(mygetc); | 
     setgetc(mygetc); | 
|      sprintf(buff, ID_TEMP, ver, sysname, ver_s, getenv("HOSTTYPE")); | 
     sprintf(buff, ID_TEMP, ver, sysname, ver_s, getenv("HOSTTYPE")); | 
|      setmode_mygetc(buff, 8192); | 
     setmode_mygetc(buff, 512); | 
|      cap = parse(); | 
     cap = parse(); | 
|      resetgetc(); | 
     resetgetc(); | 
|   | 
  | 
|      return cap; | 
     return cap; | 
|  } | 
 } | 
|   | 
  | 
|  cmo* make_mathcap_object(int version, char* id_string) | 
 static cmo_list *make_list_of_tag(int type) | 
|  { | 
 { | 
|      cmo_list *li_ver, *li_cmo, *li; | 
     cmo_list *li = new_cmo_list(); | 
|   | 
     symbol *symp; | 
|      int i; | 
     int i = 0; | 
|      li_ver = new_cmo_list(); | 
     while((symp = lookup(i++))->key != NULL) { | 
|      append_cmo_list(li_ver, (cmo *)new_cmo_int32(version)); | 
         if (symp->type == type) { | 
|      append_cmo_list(li_ver, (cmo *)new_cmo_string(id_string)); | 
             append_cmo_list(li, (cmo *)new_cmo_int32(symp->tag)); | 
|   | 
  | 
|      li_cmo = new_cmo_list(); | 
  | 
|      for(i=0; i<MAX_TYPES; i++) { | 
  | 
|          if (known_types[i] != -1) { | 
  | 
|              append_cmo_list(li_cmo, (cmo *)new_cmo_int32(known_types[i])); | 
  | 
|          } | 
         } | 
|      } | 
     } | 
|   | 
     return li; | 
|   | 
 } | 
|   | 
  | 
|      li = new_cmo_list(); | 
 cmo* make_mathcap_object(int version, char *id_string) | 
|   | 
 { | 
|   | 
     char sysname[]   = "ox_math"; | 
|   | 
     cmo_list *li     = new_cmo_list(); | 
|   | 
     cmo_list *li_ver = make_list_of_id(version, id_string, sysname); | 
|   | 
     cmo_list *li_cmo = make_list_of_tag(IS_CMO); | 
|   | 
     cmo_list *li_sm  = make_list_of_tag(IS_SM); | 
|   | 
  | 
|      append_cmo_list(li, (cmo *)li_ver); | 
     append_cmo_list(li, (cmo *)li_ver); | 
|      append_cmo_list(li, (cmo *)li_cmo); | 
     append_cmo_list(li, (cmo *)li_cmo); | 
|   | 
     append_cmo_list(li, (cmo *)li_sm); | 
|   | 
  | 
|      return (cmo *)new_cmo_mathcap((cmo *)li); | 
     return (cmo *)new_cmo_mathcap((cmo *)li); | 
|  } | 
 } | 
|   | 
  | 
| Line 1008  void setCmotypeDisable(int type) | 
 
  | 
| Line 1262  void setCmotypeDisable(int type) | 
 
 
 | 
|      int i = funcs(type); | 
     int i = funcs(type); | 
|      known_types[i] = -1; | 
     known_types[i] = -1; | 
|  } | 
 } | 
|   | 
  | 
|  #if 0 | 
 #if 0 | 
|  cmo* (*received_funcs[])(int fd) = { | 
 cmo* (*received_funcs[])(int fd) = { | 
|      NULL,  /* gate keeper */ | 
     NULL,  /* gate keeper */ | 
| Line 1028  cmo* receive_cmo2(int fd) | 
 
  | 
| Line 1283  cmo* receive_cmo2(int fd) | 
 
 
 | 
|          return foo(fd); | 
         return foo(fd); | 
|      } | 
     } | 
|  } | 
 } | 
|  #endif | 
 #endif | 
|   | 
  | 
|  /* ファイルディスクリプタ fd の通信路での integer の byte order を決定する */ | 
 /* ファイルディスクリプタ fd の通信路での integer の byte order を決定する */ | 
|  /* 実際には order (0,1,or 0xFF)をみてはいない */ | 
 /* 実際には order (0,1,or 0xFF)をみてはいない */ | 
|  int decideByteOrder(int fd_read, int fd_write, int order) | 
 int decideByteOrderClient(oxfd fd, int order) | 
|  { | 
 { | 
|      char zero = '\0'; | 
     char zero = OX_BYTE_NETWORK_BYTE_ORDER; | 
|      char dest; | 
     char dest; | 
|      write(fd_write, &zero, 1); | 
     read(fd, &dest, sizeof(char)); | 
|      read(fd_read, &dest, 1); | 
     write(fd, &zero, sizeof(char)); | 
|      return 0; | 
     return 0; | 
|  } | 
 } | 
|   | 
  | 
|  /* Server 側ではこちらを用いる */ | 
 /* Server 側ではこちらを用いる */ | 
|  int decideByteOrder2(int fd_read, int fd_write, int order) | 
 /* いまの実装は dup されていることが前提になっている */ | 
|   | 
 int decideByteOrderServer(oxfd fd, int order) | 
|  { | 
 { | 
|      char zero = '\0'; | 
     char zero = OX_BYTE_NETWORK_BYTE_ORDER; | 
|      char dest; | 
     char dest; | 
|      read(fd_read, &dest, 1); | 
     write(fd, &zero, sizeof(char)); | 
|      write(fd_write, &zero, 1); | 
     read(fd, &dest, sizeof(char)); | 
|      return 0; | 
     return 0; | 
|  } | 
 } | 
|   | 
  | 
|  /* cmo と string の変換関数群 */ | 
 /* cmo と string (ここではC言語のstring) の変換関数群 */ | 
|   | 
 char *convert_zz_to_string(cmo_zz *c) | 
|  cmo_zz *new_cmo_zz_set_string(char *s) | 
  | 
|  { | 
 { | 
|      cmo_zz* c = new_cmo_zz_noinit(); | 
  | 
|      mpz_init_set_str(c->mpz, s, 10); | 
  | 
|      return c; | 
  | 
|  } | 
  | 
|   | 
  | 
|  char *convert_zz_to_cstring(cmo_zz *c) | 
  | 
|  { | 
  | 
|      return mpz_get_str(NULL, 10, c->mpz); | 
     return mpz_get_str(NULL, 10, c->mpz); | 
|  } | 
 } | 
|   | 
  | 
|  char *convert_cmo_to_cstring(cmo *m) | 
 char *convert_cmo_to_string(cmo *m) | 
|  { | 
 { | 
|   | 
     symbol *symp; | 
|      switch(m->tag) { | 
     switch(m->tag) { | 
|      case CMO_ZZ: | 
     case CMO_ZZ: | 
|          return convert_zz_to_cstring((cmo_zz *)m); | 
         return convert_zz_to_string((cmo_zz *)m); | 
|      case CMO_INT32: | 
     case CMO_INT32: | 
|          return convert_int_to_cstring(((cmo_int32 *)m)->i); | 
         return convert_int_to_string(((cmo_int32 *)m)->i); | 
|      case CMO_STRING: | 
     case CMO_STRING: | 
|          return ((cmo_string *)m)->s; | 
         return ((cmo_string *)m)->s; | 
|      case CMO_NULL: | 
     case CMO_NULL: | 
|          return convert_null_to_cstring(); | 
         return convert_null_to_string(); | 
|      default: | 
     default: | 
|          fprintf(stderr, "sorry, not implemented CMO\n"); | 
 #ifdef DEBUG | 
|   | 
         symp = lookup_by_tag(m->tag); | 
|   | 
         fprintf(stderr, "I do not know how to convert %s to a string.\n", symp->key); | 
|   | 
 #endif | 
|          /* まだ実装していません. */ | 
         /* まだ実装していません. */ | 
|          return NULL; | 
         return NULL; | 
|      } | 
     } | 
|  } | 
 } | 
|   | 
  | 
|  char *convert_null_to_cstring() | 
 char *convert_null_to_string() | 
|  { | 
 { | 
|      static char* null_string = ""; | 
     static char* null_string = ""; | 
|      return null_string; | 
     return null_string; | 
|  } | 
 } | 
|   | 
  | 
|  char *convert_int_to_cstring(int integer) | 
 char *convert_int_to_string(int integer) | 
|  { | 
 { | 
|      char buff[1024]; | 
     char buff[1024]; | 
|      char *s; | 
     char *s; |