[BACK]Return to ox.c CVS log [TXT][DIR] Up to [local] / OpenXM / src / ox_toolkit

Diff for /OpenXM/src/ox_toolkit/ox.c between version 1.1 and 1.6

version 1.1, 1999/12/09 22:44:56 version 1.6, 1999/12/22 11:26:37
Line 1 
Line 1 
 /* -*- mode: C; coding: euc-japan -*- */  /* -*- mode: C; coding: euc-japan -*- */
 /* $OpenXM$ */  /* $OpenXM: OpenXM/src/ox_toolkit/ox.c,v 1.5 1999/12/16 06:58:01 ohara Exp $ */
   
 /*  /*
 関数の名前付け規約(その2):  関数の名前付け規約(その2):
Line 30  YYY_cmo_XXX 関数が処理する.  cmo の内部に cmo_ZZZ への
Line 30  YYY_cmo_XXX 関数が処理する.  cmo の内部に cmo_ZZZ への
 #include <unistd.h>  #include <unistd.h>
 #include <errno.h>  #include <errno.h>
 #include <fcntl.h>  #include <fcntl.h>
 #include <gmp.h>  
 #include <unistd.h>  
 #include <sys/file.h>  #include <sys/file.h>
   #include <gmp.h>
   
 #include "mysocket.h"  #include "mysocket.h"
 #include "ox.h"  #include "ox.h"
Line 60  static int          dump_mpz(mpz_ptr mpz);
Line 59  static int          dump_mpz(mpz_ptr mpz);
 static int          login_with_otp(int fd, char* passwd);  static int          login_with_otp(int fd, char* passwd);
 static char         *create_otp();  static char         *create_otp();
   
 /* CMO_xxx の値順にならべること(デバッグのため) */  /* CMO_xxx の値の順にならべること(デバッグのため) */
 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);
Line 89  static int          send_cmo_distributed_polynomial(in
Line 88  static int          send_cmo_distributed_polynomial(in
   
 static void         resize_mpz(mpz_ptr mpz, int size);  static void         resize_mpz(mpz_ptr mpz, int size);
   
   static int          print_cmo_int32(cmo_int32* c);
   static int          print_cmo_list(cmo_list* li);
   static int          print_cmo_mathcap(cmo_mathcap* c);
   static int          print_cmo_string(cmo_string* c);
   
   static char*        new_string_set_cmo_null();
   static char*        new_string_set_cmo_int32(int integer);
   static char*        new_string_set_cmo_list(cmo_list *c);
   static char*        new_string_set_cmo_zz(cmo_zz *c);
   
   int current_fd = 0;
   int set_current_fd(int fd)
   {
           current_fd = fd;
   }
   
   /* hook 関数 */
   static hook_t hook_before_send_cmo = NULL;
   static hook_t hook_after_send_cmo  = NULL;
   
   int add_hook_before_send_cmo(hook_t func)
   {
           hook_before_send_cmo = func;
   }
   
   int add_hook_after_send_cmo(hook_t func)
   {
           hook_after_send_cmo = func;
   }
   
   static cmo *call_hook_before_send_cmo(int fd, cmo *c)
   {
           if (hook_before_send_cmo != NULL) {
                   return hook_before_send_cmo(fd, c);
           }
           return c;
   }
   
   static cmo *call_hook_after_send_cmo(int fd, cmo *c)
   {
           if (hook_after_send_cmo != NULL) {
                   return hook_after_send_cmo(fd, c);
           }
           return c;
   }
   
 /* エラーハンドリングのため */  /* エラーハンドリングのため */
 static int current_received_serial = 0;  static int current_received_serial = 0;
   
Line 177  int append_cmo_list(cmo_list* this, cmo* newcmo)
Line 221  int append_cmo_list(cmo_list* this, cmo* newcmo)
     return 0;      return 0;
 }  }
   
   cmo *nth_cmo_list(cmo_list* this, int n)
   {
           cell *cp = this->head;
           if(this->length <= n) {
                   return NULL;
           }
           while(n-- > 0) {
                   cp = cp->next;
           }
           return cp->cmo;
   }
   
 int length_cmo_list(cmo_list* this)  int length_cmo_list(cmo_list* this)
 {  {
     return this->length;      return this->length;
Line 547  int print_cmo(cmo* c)
Line 603  int print_cmo(cmo* c)
     }      }
 }  }
   
 int print_cmo_int32(cmo_int32* c)  static int print_cmo_int32(cmo_int32* c)
 {  {
     fprintf(stderr, ", %d)", c->i);      fprintf(stderr, ", %d)", c->i);
 }  }
   
 int print_cmo_list(cmo_list* li)  static int print_cmo_list(cmo_list* li)
 {  {
     cell* cp = li->head;      cell* cp = li->head;
     while(cp->next != NULL) {      while(cp->next != NULL) {
Line 563  int print_cmo_list(cmo_list* li)
Line 619  int print_cmo_list(cmo_list* li)
     fprintf(stderr, ")");      fprintf(stderr, ")");
 }  }
   
 int print_cmo_mathcap(cmo_mathcap* c)  static int print_cmo_mathcap(cmo_mathcap* c)
 {  {
     fprintf(stderr, ", ");      fprintf(stderr, ", ");
     print_cmo(c->ob);      print_cmo(c->ob);
     fprintf(stderr, ")");      fprintf(stderr, ")");
 }  }
   
 int print_cmo_string(cmo_string* c)  static int print_cmo_string(cmo_string* c)
 {  {
     fprintf(stderr, ", \"%s\")", c->s);      fprintf(stderr, ", \"%s\")", c->s);
 }  }
Line 584  void ox_close(ox_file_t sv)
Line 640  void ox_close(ox_file_t sv)
 #endif  #endif
 }  }
   
 void ox_executeStringByLocalParser(ox_file_t sv, char* s)  void ox_shutdown(ox_file_t sv)
 {  {
     if (s != NULL) {          /* 後で SM_shutdown を用いるものに書き換える予定. */
         /* 文字列ををスタックにプッシュ. */          ox_close(sv);
         send_ox_cmo(sv->stream, (cmo *)new_cmo_string(s));  
         /* サーバに実行させる. */  
         send_ox_command(sv->stream, SM_executeStringByLocalParser);  
     }  
 }  }
   
   int ox_cmo_rpc(ox_file_t sv, char *function, int argc, cmo *argv[])
   {
           int i = argc;
           while(i-- > 0) {
                   send_ox_cmo(sv->stream, argv[i]);
           }
           send_ox_cmo(sv->stream, (cmo *)new_cmo_int32(argc));
           send_ox_cmo(sv->stream, (cmo *)new_cmo_string(function));
           send_ox_command(sv->stream, SM_executeFunction);
   }
   
   void ox_execute_string(ox_file_t sv, char* s)
   {
           send_ox_cmo(sv->stream, (cmo *)new_cmo_string(s));
           send_ox_command(sv->stream, SM_executeStringByLocalParser);
   }
   
   void ox_push_cmd(ox_file_t sv, int sm_code)
   {
       send_ox_command(sv->stream, sm_code);
   }
   
 /* ox_mathcap() をコールする.  */  /* ox_mathcap() をコールする.  */
 cmo_mathcap* ox_mathcap(ox_file_t sv)  cmo_mathcap* ox_mathcap(ox_file_t sv)
 {  {
Line 603  cmo_mathcap* ox_mathcap(ox_file_t sv)
Line 677  cmo_mathcap* ox_mathcap(ox_file_t sv)
     return (cmo_mathcap *)receive_cmo(sv->stream);      return (cmo_mathcap *)receive_cmo(sv->stream);
 }  }
   
 char* ox_popString(ox_file_t sv, int fd)  char* ox_popString(ox_file_t sv)
 {  {
     cmo_string* m = NULL;      cmo_string* m = NULL;
   
     send_ox_command(fd, SM_popString);      send_ox_command(sv->stream, SM_popString);
     receive_ox_tag(fd); /* OX_DATA */      receive_ox_tag(sv->stream); /* OX_DATA */
     m = (cmo_string *)receive_cmo(fd);      m = (cmo_string *)receive_cmo(sv->stream);
     return m->s;      return m->s;
 }  }
   
 cmo* ox_pop_cmo(ox_file_t sv, int fd)  int ox_pops(ox_file_t sv, int num)
 {  {
     send_ox_command(fd, SM_popCMO);          send_ox_cmo(sv->stream, (cmo *)new_cmo_int32(num));
     receive_ox_tag(fd); /* OX_DATA */      send_ox_command(sv->stream, SM_pops);
     return receive_cmo(fd);  
 }  }
   
   cmo* ox_pop_cmo(ox_file_t sv)
   {
       send_ox_command(sv->stream, SM_popCMO);
       receive_ox_tag(sv->stream); /* OX_DATA */
       return receive_cmo(sv->stream);
   }
   
   void ox_push_cmo(ox_file_t sv, cmo *c)
   {
           send_ox_cmo(sv->stream, c);
   }
   
   /* バッファのフラッシュの振りをする. */
   int ox_flush(ox_file_t sv)
   {
           return 1;
   }
   
 /* 手抜き. (後で改善しよう...) */  /* 手抜き. (後で改善しよう...) */
 static char *create_otp()  static char *create_otp()
 {  {
Line 649  static int login_with_otp(int fd, char* passwd)
Line 740  static int login_with_otp(int fd, char* passwd)
     return ret;      return ret;
 }  }
   
 static int exists_ox(char *dir, char *prog)  static int chdir_openxm_home_bin()
 {  {
     char *path = alloca(strlen(dir)+strlen(prog)+6);      char *dir;
     sprintf(path, "%s/%s", dir, prog);      char *base = getenv("OpenXM_HOME");
     return access(path, X_OK|R_OK);  
           if (base != NULL) {
                   dir = alloca(strlen(base)+5);
                   sprintf(dir, "%s/bin", base);
           }else {
                   dir = "/usr/local/OpenXM/bin";
           }
           return chdir(dir);
 }  }
   
 static char *search_ox(char *prog)  /* example: which("xterm", getenv("PATH")); */
   static char *which(char *prog, char *path_env)
 {  {
     char *env = getenv("OpenXM_HOME");          char *tok;
     char *dir;          char *path;
     if (env != NULL) {          char delim[] = ":";
         dir = malloc(strlen(env)+5);          char *e = alloca(strlen(path_env)+1);
         sprintf(dir, "%s/bin", env);          strcpy(e, path_env);
         if (exists_ox(dir, prog) == 0) {          tok = strtok(e, delim);
             return dir;          while (tok != NULL) {
         }                  char *path = malloc(strlen(tok)+strlen(prog)+2);
         free(dir);                  sprintf(path, "%s/%s", tok, prog);
     }                  if (access(path, X_OK&R_OK) == 0) {
     dir = "/usr/local/OpenXM/bin";                          return path;
     if (exists_ox(dir, prog) == 0) {                  }
         return dir;                  free(path);
     }                  tok = strtok(NULL, delim);
     dir = ".";          }
     if (exists_ox(dir, prog) == 0) {          return NULL;
         return dir;  
     }  
     return NULL;  
 }  }
   
 static int mysocketAccept2(int fd, char *pass)  static int mysocketAccept2(int fd, char *pass)
Line 690  static int mysocketAccept2(int fd, char *pass)
Line 786  static int mysocketAccept2(int fd, char *pass)
     return -1;      return -1;
 }  }
   
   /* 0 でなければ、oxlog を介して ox を起動する。*/
   static int flag_ox_start_with_oxlog = 1;
   
 /*  /*
    (-reverse 版の ox_start)     (-reverse 版の ox_start)
    ox_start は クライアントが呼び出すための関数である.     ox_start は クライアントが呼び出すための関数である.
    サーバでは使われない.  ctl_prog は コントロールサーバであり,     サーバでは使われない.  ctl_prog はコントロールサーバであり,
    -ox, -reverse, -data, -control, -pass, -host     -ox, -reverse, -data, -control, -pass, -host
    というオプションを理解することを仮定する. dat_prog は計算サーバである.     というオプションを理解することを仮定する. dat_prog は計算サーバである.
    接続時には, sv->control を先にオープンする.     接続時には, sv->control を先にオープンする.
Line 701  static int mysocketAccept2(int fd, char *pass)
Line 800  static int mysocketAccept2(int fd, char *pass)
   
 ox_file_t ox_start(char* host, char* ctl_prog, char* dat_prog)  ox_file_t ox_start(char* host, char* ctl_prog, char* dat_prog)
 {  {
       ox_file_t sv = NULL;
     char *pass;      char *pass;
     char ctl[16], dat[16];      char ctl[128], dat[128];
     short portControl = 0; /* short であることに注意 */      short portControl = 0; /* short であることに注意 */
     short portStream  = 0;      short portStream  = 0;
     ox_file_t sv = NULL;  
     char *dir;      char *dir;
           char *oxlog = "oxlog";
   
     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 = 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);
Line 721  ox_file_t ox_start(char* host, char* ctl_prog, char* d
Line 817  ox_file_t ox_start(char* host, char* ctl_prog, char* d
     pass = create_otp();      pass = create_otp();
   
     if (fork() == 0) {      if (fork() == 0) {
         dup2(2, 1);                  chdir_openxm_home_bin();
         dup2(open(DEFAULT_LOGFILE, O_RDWR|O_CREAT|O_TRUNC, 0644), 2);                  if (flag_ox_start_with_oxlog) {
         chdir(dir);                          execl(oxlog, oxlog, "xterm", "-icon", "-e", ctl_prog,
         execl(ctl_prog, ctl_prog, "-reverse", "-ox", dat_prog,                                    "-reverse", "-ox", dat_prog,
               "-data", dat, "-control", ctl, "-pass", pass,                                    "-data", dat, "-control", ctl, "-pass", pass,
               "-host", host, NULL);                                    "-host", host, NULL);
                   }else {
                           dup2(2, 1);
                           dup2(open(tempnam("/tmp", "ox."), O_RDWR|O_CREAT|O_TRUNC, 0644),  2);
                           execl(ctl_prog, ctl_prog, "-reverse", "-ox", dat_prog,
                                     "-data", dat, "-control", ctl, "-pass", pass,
                                     "-host", host, NULL);
                   }
                   exit(1);
     }      }
   
     if ((sv->control = mysocketAccept2(sv->control, pass)) == -1) {      if ((sv->control = mysocketAccept2(sv->control, pass)) == -1) {
Line 741  ox_file_t ox_start(char* host, char* ctl_prog, char* d
Line 845  ox_file_t ox_start(char* host, char* ctl_prog, char* d
     return sv;      return sv;
 }  }
   
   /* ssh -f host oxlog xterm -e ox -ox ox_asir ... */
   int ssh_ox_server(char *host, char *ctl_prog, char *dat_prog, short portControl, short portStream)
   {
           if (fork() == 0) {
                   execl("ssh", "ssh", "-f", host, "oxlog", "xterm", "-icon",
                             "-e", ctl_prog, "-insecure", "-ox", dat_prog,
                             "-data", portStream, "-control", portControl, "-host", host, NULL);
                   exit(1);
           }
   }
   
 /*  /*
    (-insecure 版の ox_start)  まだ、中身はありません。     (-insecure 版の ox_start)  まだ、中身はありません。
    ox_start_insecure_nonreverse は クライアントが呼び出すための関数である.     ox_start_insecure_nonreverse は クライアントが呼び出すための関数である.
Line 766  ox_file_t ox_start_insecure_nonreverse(char* host, sho
Line 881  ox_file_t ox_start_insecure_nonreverse(char* host, sho
     return sv;      return sv;
 }  }
   
   ox_file_t ox_start_insecure_nonreverse2(char* host, char *ctl_prog, char *dat_prog)
   {
           short portControl= 1200; /* 自動生成させよう... */
           short portStream = 1300;
   
           ssh_ox_server(host, ctl_prog, dat_prog, portControl, portStream);
           return ox_start_insecure_nonreverse(host, portControl, portStream);
   }
   
 void ox_reset(ox_file_t sv)  void ox_reset(ox_file_t sv)
 {  {
     send_ox_command(sv->control, SM_control_reset_connection);      send_ox_command(sv->control, SM_control_reset_connection);
Line 1023  int dump_ox_command(ox_command* m)
Line 1147  int dump_ox_command(ox_command* m)
     dump_integer(m->command);      dump_integer(m->command);
 }  }
   
 int send_ox(ox_file_t s, ox *m)  int send_ox(int fd, ox *m)
 {  {
     int tag = m->tag;  
     int code;      int code;
     if (tag == OX_DATA) {          switch(m->tag) {
         send_ox_cmo(s->stream, ((ox_data *)m)->cmo);          case OX_DATA:
     }else if (tag == OX_COMMAND) {          send_ox_cmo(fd, ((ox_data *)m)->cmo);
         code = ((ox_command *)m)->command;                  break;
         if (code >= 1024) {          case OX_COMMAND:
             /* control command */                  send_ox_command(fd, ((ox_command *)m)->command);
             send_ox_command(s->control, code);                  break;
         }else {          default:
             send_ox_command(s->stream, code);  #if 0
         }  
     }else {  
         /* CMO?? */          /* CMO?? */
         send_ox_cmo(s->stream, (cmo *)m);          send_ox_cmo(s->stream, (cmo *)m);
   #endif
     }      }
 }  }
   
Line 1132  int send_cmo(int fd, cmo* c)
Line 1254  int send_cmo(int fd, cmo* c)
 {  {
     int tag = c->tag;      int tag = c->tag;
   
           c = call_hook_before_send_cmo(fd, c);
   
     send_int32(fd, tag);      send_int32(fd, tag);
     switch(tag) {      switch(tag) {
     case CMO_NULL:      case CMO_NULL:
Line 1164  int send_cmo(int fd, cmo* c)
Line 1288  int send_cmo(int fd, cmo* c)
         send_cmo_distributed_polynomial(fd, (cmo_distributed_polynomial *)c);          send_cmo_distributed_polynomial(fd, (cmo_distributed_polynomial *)c);
         break;          break;
     default:      default:
                   call_hook_after_send_cmo(fd, c);
     }      }
 }  }
   
Line 1208  static cmo_list* make_list_of_id(int ver, char* ver_s,
Line 1333  static cmo_list* make_list_of_id(int ver, char* ver_s,
     cmo_list *cap;      cmo_list *cap;
     char buff[512];      char buff[512];
   
     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, 512);      init_parser(buff);
     cap = (cmo_list *)parse();      cap = (cmo_list *)parse();
     resetgetc();  
   
     return cap;      return cap;
 }  }
Line 1274  int decideByteOrderServer(oxfd fd, int order)
Line 1397  int decideByteOrderServer(oxfd fd, int order)
 }  }
   
 /* cmo と string (ここではC言語のstring) の変換関数群 */  /* cmo と string (ここではC言語のstring) の変換関数群 */
 char *convert_zz_to_string(cmo_zz *c)  static char *new_string_set_cmo_zz(cmo_zz *c)
 {  {
     return mpz_get_str(NULL, 10, c->mpz);      return mpz_get_str(NULL, 10, c->mpz);
 }  }
   
 char *convert_null_to_string()  static char *new_string_set_cmo_null()
 {  {
     static char* null_string = "";      static char* null_string = "";
     return null_string;      return null_string;
 }  }
   
 char *convert_int_to_string(int integer)  static char *new_string_set_cmo_int32(int integer)
 {  {
     char buff[1024];      char buff[1024];
     char *s;      char *s;
Line 1297  char *convert_int_to_string(int integer)
Line 1420  char *convert_int_to_string(int integer)
     return s;      return s;
 }  }
   
 char *convert_cmo_list_to_string(cmo_list *m)  static char *new_string_set_cmo_list(cmo_list *m)
 {  {
     char *s;      char *s;
     int i;      int i;
Line 1307  char *convert_cmo_list_to_string(cmo_list *m)
Line 1430  char *convert_cmo_list_to_string(cmo_list *m)
   
     cell *cp = m->head;      cell *cp = m->head;
     for(i = 0; i < len; i++) {      for(i = 0; i < len; i++) {
         sp[i] = convert_cmo_to_string(cp->cmo);          sp[i] = new_string_set_cmo(cp->cmo);
         size += strlen(sp[i]) + 3;          size += strlen(sp[i]) + 3;
         cp = cp->next;          cp = cp->next;
     }      }
Line 1323  char *convert_cmo_list_to_string(cmo_list *m)
Line 1446  char *convert_cmo_list_to_string(cmo_list *m)
     return s;      return s;
 }  }
   
 char *convert_cmo_to_string(cmo *m)  char *new_string_set_cmo(cmo *m)
 {  {
     symbol *symp;      symbol *symp;
     switch(m->tag) {      switch(m->tag) {
     case CMO_ZZ:      case CMO_ZZ:
         return convert_zz_to_string((cmo_zz *)m);          return new_string_set_cmo_zz((cmo_zz *)m);
     case CMO_INT32:      case CMO_INT32:
         return convert_int_to_string(((cmo_int32 *)m)->i);          return new_string_set_cmo_int32(((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_string();          return new_string_set_cmo_null();
     case CMO_LIST:      case CMO_LIST:
         return convert_cmo_list_to_string((cmo_list *)m);          return new_string_set_cmo_list((cmo_list *)m);
     default:      default:
 #ifdef DEBUG  #ifdef DEBUG
         symp = lookup_by_tag(m->tag);          symp = lookup_by_tag(m->tag);

Legend:
Removed from v.1.1  
changed lines
  Added in v.1.6

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>