[BACK]Return to oxshell.rr CVS log [TXT][DIR] Up to [local] / OpenXM / src / asir-contrib / packages / src

File: [local] / OpenXM / src / asir-contrib / packages / src / oxshell.rr (download)

Revision 1.9, Fri Sep 20 07:47:28 2013 UTC (10 years, 8 months ago) by takayama
Branch: MAIN
CVS Tags: RELEASE_1_3_1_13b, HEAD
Changes since 1.8: +5 -136 lines

The initial version of a polymake interface module.

/*$OpenXM: OpenXM/src/asir-contrib/packages/src/oxshell.rr,v 1.9 2013/09/20 07:47:28 takayama Exp $ */

module oxshell;

/* ------------ list of local functions ---------- */
localf oxshell$
localf get_value$
localf set_value$
localf tfb_to_tree$
localf facets$
localf get_Sm1_proc$
localf set_Sm1_proc$
localf find_proc$
localf check_server$
localf start$
localf start_unix$
localf start_windows$
localf sm1flush$
localf sm1push$
localf sm1$
localf sm1pop$
localf keep_tmp$

/*  ------------ static variables ---------------- */
static Sm1_proc$
localf init_Sm1_proc $
def init_Sm1_proc() {
  Sm1_proc = -1$
}
init_Sm1_proc() $

static V_sm1_pop$

/* Functions to get and set static variables out of the module */
def get_Sm1_proc() {
  return Sm1_proc;
}
def set_Sm1_proc(A) {
  Sm1_proc = A;
}

#define    SM1L_FIND_PROC(P)  P = getopt(proc);\
                          if (type(P) == -1) {\
                              P = find_proc();\
                         }
/*&usage begin: oxshell.oxshell(L)
  It executes command {L} on a ox_shell server.
  {L} must be an array.
  The result is the outputs to stdout and stderr.
  A temporary file will be generated under $TMP. cf. oxshell.keep_tmp()
  example: oxshell.oxshell(["ls"]);
  ref: ox_shell, oxshell.set_value, oxshell.get_value, oxshell of sm1. 
end: */
def oxshell(L) {
  SM1L_FIND_PROC(P);
  P = check_server(P);
  if (type(L) != 4) error("oxshell.oxshell(), argument must be an array.");
  ox_push_cmo(P,map(rtostr,L));
  sm1(P, " addStdoutStderr oxshell [@@@stdout @@@stderr] ");
  return(ox_pop_cmo(P));
}

/*&usage begin: oxshell.set_value(NAME,V)
  It set the value {V} to the variable {Name} on the server ox_shell.
  example: oxshell.set_value("abc","Hello world!");
           oxshell.oxshell(["cat", "stringIn://abc"]);
  ref: oxshell.oxshell, oxshell.get_value
end: */
def set_value(V,A) {
  SM1L_FIND_PROC(P);
  ox_push_cmo(P,rtostr(A));
  sm1(P,"  /"+rtostr(V)+"  set ");
}

def keep_tmp() {
  SM1L_FIND_PROC(P);
  sm1(P," [(keep_tmp_files) (1)] oxshell ");
}

/*&usage begin: oxshell.get_value(NAME,V)
  It get the value of the variable {NAME} on the server ox_shell.
  example: oxshell.set_value("abc","Hello world!");
           oxshell.oxshell(["cp", "stringIn://abc", "stringOut://result"]);
           oxshell.get_value("result");
   What we do is a file $TMP/abc* is generated with the contents Hello world! and copied to $TMP/result*
   The contents of the file is stored in the variable result on ox_sm1.
  ref: oxshell.oxshell, oxshell.set_value
end: */
def get_value(V) {
  SM1L_FIND_PROC(P);
  sm1(P," "+rtostr(V)+" ");
  return(ox_pop_cmo(P));  
}

def tfb_to_tree(S) {
  SM1L_FIND_PROC(P);
  sm1(P," ("+rtostr(S)+") tfbToTree ");
  return(ox_pop_cmo(P));
}

def find_proc() {
  /*! extern Sm1_proc; */
  if (Sm1_proc == -1) {
     A = ox_get_serverinfo();
     /* Look for ox_sm1. Not yet written */
     /* Start sm1 automatically if there is not ox_sm1 */
     Sm1_proc = start();
  }
  return(Sm1_proc);
}
/* Search : oxPrintMessage, see cmoDebugCMO, too. */
/************** end of configure *******************************/

def check_server(P) {
  M=ox_get_serverinfo(P);
  if (M == []) {
    return(start());
  }
  if (M[0][1] != "Ox_system=ox_sm1_ox_sm1_forAsir") {
    print("Warning: the server number ",0)$
    print(P,0)$
    print(" is not ox_sm1_forAsir server.")$
    print("Starting ox_sm1_forAsir server on the localhost.")$
    return(start());
  }
  return(P);
}


def start() {
  extern Xm_unix;
  if (ox_ostype()[0] == "windows" && Xm_unix == 0)
    return start_windows(0);
  else
    return start_unix();
}

def start_unix() {
 extern Sm1_lib;
 extern Xm_noX;
 /*! extern Sm1_proc; */
 if (Xm_noX) {
   P = ox_launch_nox(0,Sm1_lib+"/bin/ox_sm1_forAsir");
 }else{
   P = ox_launch(0,Sm1_lib+"/bin/ox_sm1_forAsir");
 }
 if (Xm_noX) {
   sm1(P," oxNoX ");
 }
 ox_check_errors(P);
 Sm1_proc = P;
 return(P);
}

def start_windows(U) {
  extern Sm1_lib;
  extern Xm_noX;
  /*! extern Sm1_proc; */
  extern Xm_use_timer_to_start_server;

  CPort = generate_port();
  SPort = generate_port();
 
  if (U) {  /* Test on unix. */
    Com = "ox -ox " + Sm1_lib+"/bin/ox_sm1_forAsir " +
          " -data "+ rtostr(SPort) +" -control "+ rtostr(CPort);
    Com = "oxlog xterm -iconic -T ox_sm1 -n ox_sm1 -e "+ Com + "& ";
  }else{
    /* OpenXM_HOME_WIN="c:\\cygwin\\home\\nobuki\\OpenXM";
       OpenXM_HOME_WIN2="/home/nobuki/OpenXM";
       OpenXM_START_EXE = "c:/windows/command/start";
    */
    OpenXM_HOME_WIN = ox_getenv("OpenXM_HOME_WIN");
    OpenXM_START_EXE = ox_getenv("OpenXM_START_EXE");
    /* Sm1_lib = OpenXM_HOME_WIN2+"/lib/sm1"; */
    Sm1_lib = OpenXM_HOME_WIN+"/lib/sm1"; 
    Ox_sm1_forAsir = OpenXM_HOME_WIN+"\\lib\\sm1\\bin\\ox_sm1_forAsir.exe"; 
    /* Ox_sm1_forAsir = OpenXM_HOME_WIN+"\\bin\\ox_sm1.exe";  for debug */
    F = open_file(Ox_sm1_forAsir);
    if (F < 0) {
      error("sm1. ox_sm1_forAsir.exe is not existing.  Check your installation.");
      return(-1);
    }
    close_file(F);
    Com = OpenXM_HOME_WIN+"\\bin\\ox -ox " + 
          Ox_sm1_forAsir + " " +
          " -data "+ rtostr(SPort) +" -control "+ rtostr(CPort);
    Com = OpenXM_START_EXE+" /MIN "+Com ;
  }

  print(Com +"\n");
  shell(Com);
  if (Xm_use_timer_to_start_server) {
    print("Waiting for 3 seconds."); sleep(3000); 
  }else{
    purge_stdin(); print("Type in Return to connect to the server.");
    get_line();
  }
  print("Trying to connect to the server...",0)$
  CSocket = try_connect("localhost",CPort);
  SSocket = try_connect("localhost",SPort);
  P = register_server(CSocket,CPort,SSocket,SPort);
  print(" Done.");

 /* 
 if (Xm_noX) {
   P = ox_launch_nox(0,Sm1_lib+"/bin/ox_sm1_forAsir");
 }else{
   P = ox_launch(0,Sm1_lib+"/bin/ox_sm1_forAsir");
 }
 */
 if (Xm_noX) {
   sm1(P," oxNoX ");
 }
 ox_check_errors(P);
 Sm1_proc = P;
 return(P);
}

/*   ox_sm1  */
/* P is the process number */
def sm1flush(P) {
  ox_execute_string(P,"[(flush)] extension pop");
}

def sm1push(P,F) {
  G = ox_ptod(F);
  ox_push_cmo(P,G);  
}

def sm1(P,F) {
  ox_execute_string(P,F);
  sm1flush(P);
}


def sm1pop(P) {
  return(ox_pop_cmo(P));
}



endmodule;


def ox_shell(L) {
  oxshell.oxshell(L);
}


end$