From afb00cc4f13b890168744ac2e46e1df436c061ec Mon Sep 17 00:00:00 2001
From: Francois Henrotte <francois.henrotte@ulg.ac.be>
Date: Thu, 1 Nov 2012 13:24:28 +0000
Subject: [PATCH]

---
 Common/OS.cpp                    | 18 +++++++
 Common/OS.h                      |  1 +
 Fltk/onelabWindow.cpp            |  5 +-
 contrib/onelab/OnelabClients.cpp | 82 +++++++++++++-------------------
 contrib/onelab/OnelabClients.h   |  4 ++
 contrib/onelab/OnelabMessage.cpp |  8 ----
 contrib/onelab/OnelabMessage.h   |  5 +-
 contrib/onelab/OnelabParser.cpp  |  9 ++--
 contrib/onelab/loader.cpp        |  2 +
 contrib/onelab/metamodel.cpp     |  6 +--
 contrib/onelab/myOS.cpp          | 18 +++++++
 contrib/onelab/myOS.h            |  1 +
 12 files changed, 91 insertions(+), 68 deletions(-)

diff --git a/Common/OS.cpp b/Common/OS.cpp
index 853e9f4258..296e787e69 100644
--- a/Common/OS.cpp
+++ b/Common/OS.cpp
@@ -228,3 +228,21 @@ int SystemCall(const std::string &command, bool blocking)
   return system(cmd.c_str());
 #endif
 }
+
+#ifndef MAXPATHLEN
+#define MAXPATHLEN 1024
+#endif
+
+std::string getCurrentWorkdir(){
+  char path[MAXPATHLEN];
+  if(!getcwd(path, MAXPATHLEN)) return "";
+  std::string str = path;
+  //match the convention of SplitFileName that delivers directory path
+  //endig with a directory separator
+#if defined(WIN32)
+  str.append("\\");
+#else
+  str.append("/");
+#endif
+  return str;
+}
diff --git a/Common/OS.h b/Common/OS.h
index 0bb1cf2c57..dacf126e04 100644
--- a/Common/OS.h
+++ b/Common/OS.h
@@ -21,5 +21,6 @@ int StatFile(const std::string &fileName);
 int KillProcess(int pid);
 int CreateDirectory(const std::string &dirName);
 int SystemCall(const std::string &command, bool blocking=false);
+std::string getCurrentWorkdir();
 
 #endif
diff --git a/Fltk/onelabWindow.cpp b/Fltk/onelabWindow.cpp
index ea91af7d42..93f33ce908 100644
--- a/Fltk/onelabWindow.cpp
+++ b/Fltk/onelabWindow.cpp
@@ -1416,8 +1416,9 @@ int metamodel_cb(const std::string &name, const std::string &action)
     onelab::number n("IsMetamodel", 1.);
     n.setVisible(false);
     onelab::server::instance()->set(n);
-    std::vector<std::string> split = SplitFileName(name);
-    onelab::string s1("Arguments/WorkingDir", split[0]);
+    std::vector<std::string> split = SplitFileName(name); 
+    onelab::string s1("Arguments/WorkingDir",
+		      split[0].size()?split[0]:getCurrentWorkdir());
     s1.setVisible(false);
     onelab::server::instance()->set(s1);
     onelab::string s2("Arguments/FileName", split[1]);
diff --git a/contrib/onelab/OnelabClients.cpp b/contrib/onelab/OnelabClients.cpp
index eb20132063..4bfc3241f9 100644
--- a/contrib/onelab/OnelabClients.cpp
+++ b/contrib/onelab/OnelabClients.cpp
@@ -410,13 +410,25 @@ bool localSolverClient::checkCommandLine(){
 	      getCommandLine().c_str(), getName().c_str());
   if(!isActive()) return true;
 
-  if(!getCommandLine().empty()){
+  if(getCommandLine().empty()){
+    if(OLMsg::hasGmsh) {
+      OLMsg::Error("No command line for client <%s>", getName().c_str());
+      return false; // restores control to gmsh
+    }
+    else{ // asks the user in console mode
+      std::cout << "\nONELAB: Enter pathname of the executable file for <" << getName() << ">" << std::endl;
+      std::string cmdl;
+      std::getline (std::cin,cmdl);
+      setCommandLine(cmdl);
+      return checkCommandLine();
+    }
+  }
+  else{
     if(isNative()){
       setAction("initialize");
-      if(!run()){  // initializes native clients
+      if(!run()){ // initializes native clients, false otherwise
 	OLMsg::Error("Invalid commandline <%s> for client <%s>",
 	   FixExecPath(getCommandLine()).c_str(), getName().c_str());
-	//setCommandLine("");
 	return false;
       }
     }
@@ -425,7 +437,7 @@ bool localSolverClient::checkCommandLine(){
       char cbuf [1024];
       FILE *fp;
       commandLine.assign(FixExecPath(getCommandLine()));
-      cmd.assign("which "+commandLine);
+      cmd.assign(whichCmd + commandLine);
       fp = POPEN(cmd.c_str(), "r");
       if(fgets(cbuf, 1024, fp) == NULL){
 	OLMsg::Error("The executable <%s> does not exist",
@@ -435,20 +447,6 @@ bool localSolverClient::checkCommandLine(){
       }
       OLMsg::Info("The executable <%s> exists", commandLine.c_str());
       PCLOSE(fp);
-      return true;
-    }
-  }
-  else{
-    OLMsg::Error("No commandline for client <%s>", getName().c_str());
-    if(OLMsg::hasGmsh) {
-      return false; // exits metamodel and restores control to gmsh
-    }
-    else{ // asks the user in console mode
-      std::cout << "\nONELAB:Enter the command line (with path) of the executable file of <" << getName() << ">" << std::endl;
-      std::string cmdl;
-      std::getline (std::cin,cmdl);
-      setCommandLine(cmdl);
-      return cmdl.size();
     }
   }
   return true;
@@ -462,11 +460,7 @@ bool localSolverClient::buildRmCommand(std::string &cmd){
     cmd.append("cd " + getWorkingDir() + cmdSep);
 
   if(getList("OutputFiles",choices)){
-#if defined(WIN32)
-    cmd.append("del ");
-#else
-    cmd.append("rm -rf ");
-#endif
+    cmd.append(removeCmd); // defined in OnelabClients.h
     if(choices.size()){
       for(unsigned int i = 0; i < choices.size(); i++)
 	cmd.append(choices[i]+" ");
@@ -635,16 +629,6 @@ bool remoteClient::syncOutputFile(const std::string &wdir, const std::string &fi
 
 // client METAMODEL
 
-
-bool MetaModel::checkCommandLines(){
-  bool allDefined=true;
-  for(citer it = _clients.begin(); it != _clients.end(); it++){
-    allDefined = allDefined && (*it)->checkCommandLine();
-  }
-  saveCommandLines(genericNameFromArgs);
-  return allDefined;
-}
-
 void MetaModel::construct()
 {
   OLMsg::Info("Metamodel now CONSTRUCTING");
@@ -764,7 +748,7 @@ void InterfacedClient::compute(){
   std::vector<std::string> choices;
 
   analyze();
-  if(OLMsg::GetErrorNum()) return;
+  if(OLMsg::GetErrorCount()) return;
   OLMsg::Info("Computes <%s>", getName().c_str());
   setAction("compute");
 
@@ -810,7 +794,7 @@ void NativeClient::compute() {
   std::vector<std::string> choices;
 
   analyze();
-  if(OLMsg::GetErrorNum()) return;
+  if(OLMsg::GetErrorCount()) return;
   OLMsg::Info("Computes <%s>", getName().c_str());
   setAction("compute");
 
@@ -888,7 +872,7 @@ void EncapsulatedClient::compute(){
   std::vector<std::string> choices;
 
   analyze();
-  if(OLMsg::GetErrorNum()) return;
+  if(OLMsg::GetErrorCount()) return;
   OLMsg::Info("Computes <%s>", getName().c_str());
   setAction("compute");
 
@@ -936,7 +920,7 @@ void RemoteInterfacedClient::compute(){
   std::vector<std::string> choices;
 
   analyze();
-  if(OLMsg::GetErrorNum()) return;
+  if(OLMsg::GetErrorCount()) return;
   OLMsg::Info("Computes <%s>", getName().c_str());
   setAction("compute");
 
@@ -1001,7 +985,7 @@ void RemoteNativeClient::compute(){
   std::vector<std::string> choices;
 
   analyze();
-  if(OLMsg::GetErrorNum()) return;
+  if(OLMsg::GetErrorCount()) return;
   OLMsg::Info("Computes <%s> changed=%d", getName().c_str());
   setAction("compute");
 
@@ -1039,7 +1023,7 @@ void RemoteEncapsulatedClient::compute(){
   std::vector<std::string> choices;
 
   analyze();
-  if(OLMsg::GetErrorNum()) return;
+  if(OLMsg::GetErrorCount()) return;
   OLMsg::Info("Computes <%s> changed=%d", getName().c_str());
   setAction("compute");
 
@@ -1174,17 +1158,17 @@ std::string getUserHomedir(){
 #include <direct.h>
 #endif
 
-#ifndef MAXPATHLEN
-#define MAXPATHLEN 1024
-#endif
-
+// #ifndef MAXPATHLEN
+// #define MAXPATHLEN 1024
+// #endif
 
-std::string getCurrentWorkdir(){
-  char path[MAXPATHLEN];
-  if(!getcwd(path, MAXPATHLEN)) return "";
-  std::string str = path;
-  return str;
-}
+// std::string getCurrentWorkdir(){
+//   char path[MAXPATHLEN];
+//   if(!getcwd(path, MAXPATHLEN)) return "";
+//   std::string str = path;
+//   str.append(dirSep);
+//   return str;
+// }
 
 std::string sanitize(const std::string &in)
 {
diff --git a/contrib/onelab/OnelabClients.h b/contrib/onelab/OnelabClients.h
index 435192230b..0679361d8a 100644
--- a/contrib/onelab/OnelabClients.h
+++ b/contrib/onelab/OnelabClients.h
@@ -23,9 +23,13 @@ enum parseMode {REGISTER, ANALYZE, COMPUTE, EXIT};
 #if defined(WIN32)
 static std::string dirSep("\\");
 static std::string cmdSep(" & ");
+static std::string removeCmd("del ");
+static std::string whichCmd("where ");
 #else
 static std::string dirSep("/");
 static std::string cmdSep(" ; ");
+static std::string removeCmd("rm -rf ");
+static std::string whichCmd("which ");
 #endif
 
 // TOOLS 
diff --git a/contrib/onelab/OnelabMessage.cpp b/contrib/onelab/OnelabMessage.cpp
index 6d028085b5..0818134d8d 100644
--- a/contrib/onelab/OnelabMessage.cpp
+++ b/contrib/onelab/OnelabMessage.cpp
@@ -50,14 +50,6 @@ static int vsnprintf(char *str, size_t size, const char *fmt, va_list ap)
 #define vsnprintf _vsnprintf
 #endif
 
-int OLMsg::GetErrorNum(){
-  return _errorCount;
-}
-
-void OLMsg::ResetErrorNum(){
-  _errorCount=0;
-}
-
 void OLMsg::Init(int argc, char **argv)
 {
   time_t now;
diff --git a/contrib/onelab/OnelabMessage.h b/contrib/onelab/OnelabMessage.h
index ffd187a8b1..9ce6245fd6 100644
--- a/contrib/onelab/OnelabMessage.h
+++ b/contrib/onelab/OnelabMessage.h
@@ -73,8 +73,6 @@ class OLMsg {
   /* static int GetVerbosity(){ return _verbosity; } */
   /* static std::string GetLaunchDate(){ return _launchDate; } */
   /* static std::string GetCommandLineArgs(){ return _commandLine; } */
-  static int GetErrorNum();
-  static void ResetErrorNum();
   static void Fatal(const char *fmt, ...);
   static void Error(const char *fmt, ...);
   static void Warning(const char *fmt, ...);
@@ -89,8 +87,11 @@ class OLMsg {
   static void ResetProgressMeter(){ if(!_commRank) _progressMeterCurrent = 0; }
   static double &Timer(std::string str){ return _timers[str]; }
   static void PrintTimers();
+
   static void ResetErrorCounter(){ _warningCount = 0; _errorCount = 0; }
   static void PrintErrorCounter(const char *title);
+  static int GetErrorCount(){ return _errorCount; }
+
   static double GetValue(const char *text, double defaultval);
   static std::string GetString(const char *text, std::string defaultval);
   static int GetAnswer(const char *question, int defaultval, const char *zero,
diff --git a/contrib/onelab/OnelabParser.cpp b/contrib/onelab/OnelabParser.cpp
index 0618a0a7a0..b7c1f8fb20 100644
--- a/contrib/onelab/OnelabParser.cpp
+++ b/contrib/onelab/OnelabParser.cpp
@@ -71,6 +71,7 @@ void MetaModel::saveCommandLines(const std::string fileName){
   std::vector<std::string> arguments, buffer;
   size_t cursor, pos;
   std::string loaderPathName=OLMsg::GetOnelabString("LoaderPathName");
+  OLMsg::Info("Save command lines for loader <%s>", loaderPathName.c_str());
 
   std::string fileNameSave = getWorkingDir()+fileName+onelabExtension+".save";
   std::ifstream infile(fileNameSave.c_str());
@@ -87,7 +88,7 @@ void MetaModel::saveCommandLines(const std::string fileName){
 	  do{
 	    getline (infile,line);
 	    if(keep) buffer.push_back(line);
-	  } while ((pos=line.find(olkey::olendif)) != std::string::npos);
+	  } while ((pos=line.find(olkey::olendif)) == std::string::npos);
 	}
 	else
 	  OLMsg::Error("Incorrect statement <%s> in <%s>",
@@ -1441,7 +1442,7 @@ void MetaModel::client_sentence(const std::string &name,
 	set(strings[0]);
       }
       localSolverClient *c;
-      if(!OLMsg::GetErrorNum())
+      if(!OLMsg::GetErrorCount())
 	if((c=findClientByName(name)))
 	  if(c->checkCommandLine())
 	    c->analyze();
@@ -1458,7 +1459,7 @@ void MetaModel::client_sentence(const std::string &name,
 	  bool changed = onelab::server::instance()->getChanged(c->getName());
 	  bool started = isStarted(changed);
 
-	  std::cout << c->getName() << " active=" << c->getActive() << " changed=" << changed << " started=" << started << " errors=" << OLMsg::GetErrorNum() << std::endl;
+	  std::cout << c->getName() << " active=" << c->getActive() << " changed=" << changed << " started=" << started << " errors=" << OLMsg::GetErrorCount() << std::endl;
 	  if(started) c->compute();
 	}
       }
@@ -1541,7 +1542,7 @@ void MetaModel::client_sentence(const std::string &name,
     }
     localSolverClient *c;
     if((c=findClientByName(name))) {
-      if(isTodo(REGISTER) && !OLMsg::GetErrorNum())
+      if(isTodo(REGISTER) && !OLMsg::GetErrorCount())
 	if(onelab::server::instance()->getChanged(c->getName())){
 	  c->compute();
 	  c->GmshMerge(choices);
diff --git a/contrib/onelab/loader.cpp b/contrib/onelab/loader.cpp
index 6ced543660..c5fea65c0b 100644
--- a/contrib/onelab/loader.cpp
+++ b/contrib/onelab/loader.cpp
@@ -272,6 +272,8 @@ int main(int argc, char *argv[]){
       caseName=argv[i];
       modelName.assign(SplitFileName(caseName)[1]);
       workingDir.assign(SplitFileName(caseName)[0]);
+      if(workingDir.empty())
+	workingDir.assign(getCurrentWorkdir());
       i++;
     }
   }
diff --git a/contrib/onelab/metamodel.cpp b/contrib/onelab/metamodel.cpp
index da73a4f2b4..7777b50eb0 100644
--- a/contrib/onelab/metamodel.cpp
+++ b/contrib/onelab/metamodel.cpp
@@ -21,7 +21,7 @@ int metamodel(const std::string &action){
 
   OLMsg::Info("Start metamodel");
   OLMsg::hasGmsh = OLMsg::GetOnelabNumber("IsMetamodel");
-  OLMsg::ResetErrorNum();
+  OLMsg::ResetErrorCounter();
 
   parseMode todo;
   // if(action == "initialize")
@@ -50,7 +50,7 @@ int metamodel(const std::string &action){
     freopen(mystderr.c_str(),"w",stderr);
   }
 
-  if(OLMsg::GetErrorNum()) myModel->setTodo(EXIT);
+  if(OLMsg::GetErrorCount()) myModel->setTodo(EXIT);
 
   if( myModel->isTodo(ANALYZE)){
     myModel->analyze();
@@ -64,7 +64,7 @@ int metamodel(const std::string &action){
     OLMsg::Error("Main: Unknown Action <%d>", todo);
   delete myModel;
 
-  if((errors=OLMsg::GetErrorNum())){
+  if((errors=OLMsg::GetErrorCount())){
     OLMsg::Error("Leave metamodel - %d errors",errors);
     OLMsg::Info("==============================================");
     return 0;
diff --git a/contrib/onelab/myOS.cpp b/contrib/onelab/myOS.cpp
index 7bbbcdb3a3..ea4b44fa71 100644
--- a/contrib/onelab/myOS.cpp
+++ b/contrib/onelab/myOS.cpp
@@ -214,3 +214,21 @@ int SystemCall(const std::string &command, bool blocking)
   return system(cmd.c_str());
 #endif
 }
+
+#ifndef MAXPATHLEN
+#define MAXPATHLEN 1024
+#endif
+
+std::string getCurrentWorkdir(){
+  char path[MAXPATHLEN];
+  if(!getcwd(path, MAXPATHLEN)) return "";
+  std::string str = path;
+  //match the convention of SplitFileName that delivers directory path
+  //endig with a directory separator
+#if defined(WIN32)
+  str.append("\\");
+#else
+  str.append("/");
+#endif
+  return str;
+}
diff --git a/contrib/onelab/myOS.h b/contrib/onelab/myOS.h
index 6a17031a58..54665fe40d 100644
--- a/contrib/onelab/myOS.h
+++ b/contrib/onelab/myOS.h
@@ -20,5 +20,6 @@ int UnlinkFile(const std::string &fileName);
 int StatFile(const std::string &fileName);
 int KillProcess(int pid);
 int SystemCall(const std::string &command, bool blocking=false);
+std::string getCurrentWorkdir();
 
 #endif
-- 
GitLab