From beee067c4a98a09044dd31fc95b398a1909c06cc Mon Sep 17 00:00:00 2001
From: Christophe Geuzaine <cgeuzaine@ulg.ac.be>
Date: Wed, 22 Jul 2015 08:37:32 +0000
Subject: [PATCH] New command line option "-run", which acts like the "Run"
 button in the GUI: it runs the ONELAB solvers (even the local Gmsh client:
 this is not the case if one uses simply "-").

"-run" should be used in all your scripts instead of "-" from now on.
---
 Common/CommandLine.cpp            |  8 ++++
 Common/gmshLocalNetworkClient.cpp | 67 +++++++++++++++++++++----------
 doc/texinfo/commandline.texi      |  2 +
 3 files changed, 56 insertions(+), 21 deletions(-)

diff --git a/Common/CommandLine.cpp b/Common/CommandLine.cpp
index 0dea547882..05719854b2 100644
--- a/Common/CommandLine.cpp
+++ b/Common/CommandLine.cpp
@@ -117,6 +117,7 @@ std::vector<std::pair<std::string, std::string> > GetUsage()
   s.push_back(mp("-listen",            "Always listen to incoming connection requests"));
   s.push_back(mp("-minterpreter string", "Name of Octave interpreter"));
   s.push_back(mp("-pyinterpreter string", "Name of Python interpreter"));
+  s.push_back(mp("-run",               "Run ONELAB solver(s)"));
   s.push_back(mp("Display options:", ""));
   s.push_back(mp("-n",                 "Hide all meshes and post-processing views on startup"));
   s.push_back(mp("-nodb",              "Disable double buffering"));
@@ -308,6 +309,13 @@ void GetOptions(int argc, char *argv[])
         CTX::instance()->batch = -99;
         i++;
       }
+      else if(!strcmp(argv[i] + 1, "run")) {
+        // same as '-', but will run local Gmsh client (if no other clients are
+        // requested, e.g. by opening a '.pro' or '.py' file)
+        CTX::instance()->batch = -99;
+        CTX::instance()->launchSolverAtStartup = -2;
+        i++;
+      }
       else if(!strcmp(argv[i] + 1, "onelab")) {
         i++;
         if(argv[i] && argv[i + 1] && argv[i + 1][0] != '-'){
diff --git a/Common/gmshLocalNetworkClient.cpp b/Common/gmshLocalNetworkClient.cpp
index 1bf5bd5d06..ef92f8471a 100644
--- a/Common/gmshLocalNetworkClient.cpp
+++ b/Common/gmshLocalNetworkClient.cpp
@@ -798,12 +798,27 @@ void resetDb(bool runGmshClient)
 void solver_batch_cb(void *data)
 {
   int num = (intptr_t)data;
-  if(num < 0) return;
-  std::string name = opt_solver_name(num, GMSH_GET, "");
-  std::string exe = opt_solver_executable(num, GMSH_GET, "");
-  std::string host = opt_solver_remote_login(num, GMSH_GET, "");
-  if(exe.empty()){
-    Msg::Error("Solver executable name not provided");
+  std::string name, exe, host;
+
+  if(num == -1){
+    // no solver to run
+    return;
+  }
+  else if(num == -2){
+    // just run local Gmsh client
+  }
+  else if(num >= 0){
+    // run local Gmsh client + solver num
+    name = opt_solver_name(num, GMSH_GET, "");
+    exe = opt_solver_executable(num, GMSH_GET, "");
+    host = opt_solver_remote_login(num, GMSH_GET, "");
+    if(exe.empty()){
+      Msg::Error("Solver executable name not provided");
+      return;
+    }
+  }
+  else{
+    Msg::Error("Unknown client to run in batch mode (%d)", num);
     return;
   }
 
@@ -812,15 +827,21 @@ void solver_batch_cb(void *data)
   onelab::server::instance()->set(n);
 
   // create client
-  onelab::localNetworkClient *c = new gmshLocalNetworkClient(name, exe, host);
-  c->setIndex(num);
-  onelab::string o(c->getName() + "/Action");
+  onelab::localNetworkClient *c = 0;
+  onelab::string o;
+  if(name.size()){
+    c = new gmshLocalNetworkClient(name, exe, host);
+    c->setIndex(num);
+    o = c->getName() + "/Action";
+  }
 
   // initialize
   onelabUtils::runGmshClient("initialize", CTX::instance()->solver.autoMesh);
-  o.setValue("initialize");
-  onelab::server::instance()->set(o);
-  c->run();
+  if(c){
+    o.setValue("initialize");
+    onelab::server::instance()->set(o);
+    c->run();
+  }
 
   // load db
   if(CTX::instance()->solver.autoSaveDatabase){
@@ -830,20 +851,24 @@ void solver_batch_cb(void *data)
 
   // check
   onelabUtils::runGmshClient("check", CTX::instance()->solver.autoMesh);
-  onelabUtils::guessModelName(c);
-  o.setValue("check");
-  onelab::server::instance()->set(o);
-  c->run();
+  if(c){
+    onelabUtils::guessModelName(c);
+    o.setValue("check");
+    onelab::server::instance()->set(o);
+    c->run();
+  }
 
   // compute
   initializeLoops();
   do{
     onelabUtils::runGmshClient("compute", CTX::instance()->solver.autoMesh);
-    onelabUtils::guessModelName(c);
-    o.setValue("compute");
-    onelab::server::instance()->set(o);
-    c->run();
-    onelab::server::instance()->setChanged(false, c->getName());
+    if(c){
+      onelabUtils::guessModelName(c);
+      o.setValue("compute");
+      onelab::server::instance()->set(o);
+      c->run();
+      onelab::server::instance()->setChanged(false, c->getName());
+    }
   } while(incrementLoops());
 
   if(CTX::instance()->solver.autoSaveDatabase ||
diff --git a/doc/texinfo/commandline.texi b/doc/texinfo/commandline.texi
index 7539ea3f6f..05d910c30c 100644
--- a/doc/texinfo/commandline.texi
+++ b/doc/texinfo/commandline.texi
@@ -88,6 +88,8 @@ Always listen to incoming connection requests
 Name of Octave interpreter
 @item -pyinterpreter string
 Name of Python interpreter
+@item -run
+Run ONELAB solver(s)
 @end ftable
  Display options:
 @ftable @code
-- 
GitLab