From 1b00135d3e3ccf815adac985c3f48f387b8d4ac9 Mon Sep 17 00:00:00 2001
From: Christophe Geuzaine <cgeuzaine@ulg.ac.be>
Date: Sun, 17 Oct 2004 01:53:49 +0000
Subject: [PATCH] Trying to fix Patrick's problems when launching getdp on
 Windows...

When gmsh is started from the command line on Windows, cygwin does
not (for some obscure reason) prepend '/cygdrive/x/' to the path. Since
we check for '/cygdrive/' in SystemCall() in order to sanitize the
path before passing it to CreateProcess(), the system call fails.

I think the right way to deal with this is to actually remove the
path change in SystemCall() (since we only change the leading part),
and use the official cygwin API to change the path of every piece
that will eventually end up in the argument of CreateProcess().
And that's what "FixWindowsPath()" now does...
---
 Fltk/Callbacks.cpp  | 54 +++++++++++++++++++++++++--------------------
 Fltk/Solvers.cpp    | 22 ++++++++++--------
 Parser/OpenFile.cpp | 42 +++++++++++++----------------------
 Parser/OpenFile.h   |  1 +
 4 files changed, 59 insertions(+), 60 deletions(-)

diff --git a/Fltk/Callbacks.cpp b/Fltk/Callbacks.cpp
index 57d79b2206..57961827be 100644
--- a/Fltk/Callbacks.cpp
+++ b/Fltk/Callbacks.cpp
@@ -1,4 +1,4 @@
-// $Id: Callbacks.cpp,v 1.283 2004-10-16 22:15:16 geuzaine Exp $
+// $Id: Callbacks.cpp,v 1.284 2004-10-17 01:53:49 geuzaine Exp $
 //
 // Copyright (C) 1997-2004 C. Geuzaine, J.-F. Remacle
 //
@@ -1537,15 +1537,17 @@ void _replace_multi_format(char *in, char *val, char *out){
 
 void help_online_cb(CALLBACK_ARGS)
 {
-  char cmd[1000];
-  _replace_multi_format(CTX.web_browser, "http://www.geuz.org/gmsh/doc/texinfo/", cmd);
+  char prog[1024], cmd[1024];
+  FixWindowsPath(CTX.web_browser, prog);
+  _replace_multi_format(prog, "http://www.geuz.org/gmsh/doc/texinfo/", cmd);
   SystemCall(cmd);
 }
 
 void help_credits_cb(CALLBACK_ARGS)
 {
-  char cmd[1000];
-  _replace_multi_format(CTX.web_browser, "http://www.geuz.org/gmsh/doc/CREDITS", cmd);
+  char prog[1024], cmd[1024];
+  FixWindowsPath(CTX.web_browser, prog);
+  _replace_multi_format(prog, "http://www.geuz.org/gmsh/doc/CREDITS", cmd);
   SystemCall(cmd);
 }
 
@@ -1595,8 +1597,10 @@ void geometry_physical_cb(CALLBACK_ARGS)
 
 void geometry_edit_cb(CALLBACK_ARGS)
 {
-  char cmd[1000];
-  _replace_multi_format(CTX.editor, CTX.filename, cmd);
+  char prog[1024], file[1024], cmd[1024];
+  FixWindowsPath(CTX.editor, prog);
+  FixWindowsPath(CTX.filename, file);
+  _replace_multi_format(prog, file, cmd);
   SystemCall(cmd);
 }
 
@@ -2467,11 +2471,10 @@ static void _add_physical(char *what)
         add_physical(List1, CTX.filename, type, &num);
 
 	GMSH_Solve_Plugin *sp = GMSH_PluginManager::instance()->findSolverPlugin();
-	if (sp)
-	  {
-	    sp->receiveNewPhysicalGroup(type,num);
-	    sp->writeSolverFile(CTX.filename);
-	  }
+	if (sp){
+	  sp->receiveNewPhysicalGroup(type,num);
+	  sp->writeSolverFile(CTX.filename);
+	}
 
         List_Reset(List1);
         ZeroHighlight(THEM);
@@ -2877,8 +2880,9 @@ void solver_cb(CALLBACK_ARGS)
     WID->solver[num].input[0]->value(file);
   }
   if(SINFO[num].nboptions) {
-    sprintf(tmp, "%s \"%s\"", SINFO[num].option_command,
-            (char *)WID->solver[num].input[0]->value());
+    char file[1024];
+    FixWindowsPath((char *)WID->solver[num].input[0]->value(), file);
+    sprintf(tmp, "%s \"%s\"", SINFO[num].option_command, file);           
     Solver(num, tmp);
   }
   WID->create_solver_window(num);
@@ -2895,17 +2899,20 @@ void solver_file_open_cb(CALLBACK_ARGS)
   if(file_chooser(0, 1, "Open problem definition file", tmp, 0)) {
     WID->solver[num].input[0]->value(file_chooser_get_name(1));
     if(SINFO[num].nboptions) {
-      sprintf(tmp, "%s \"%s\"", SINFO[num].option_command,
-              file_chooser_get_name(1));
+      char file[1024];
+      FixWindowsPath(file_chooser_get_name(1), file);
+      sprintf(tmp, "%s \"%s\"", SINFO[num].option_command, file);
       Solver(num, tmp);
     }
   }
 }
 void solver_file_edit_cb(CALLBACK_ARGS)
 {
-  char cmd[1000];
+  char prog[1024], file[1024], cmd[1024];
   int num = (int)data;
-  sprintf(cmd, CTX.editor, WID->solver[num].input[0]->value());
+  FixWindowsPath(CTX.editor, prog);
+  FixWindowsPath((char*)WID->solver[num].input[0]->value(), file);
+  _replace_multi_format(prog, file, cmd);
   SystemCall(cmd);
 }
 
@@ -2938,7 +2945,8 @@ void solver_command_cb(CALLBACK_ARGS)
     WID->create_message_window();
 
   if(strlen(WID->solver[num].input[1]->value())) {
-    sprintf(tmp, "\"%s\"", WID->solver[num].input[1]->value());
+    FixWindowsPath((char*)WID->solver[num].input[1]->value(), mesh);
+    sprintf(tmp, "\"%s\"", mesh);
     sprintf(mesh, SINFO[num].mesh_command, tmp);
   }
   else {
@@ -2956,16 +2964,14 @@ void solver_command_cb(CALLBACK_ARGS)
       return;
     }
     sprintf(command, SINFO[num].button_command[idx],
-            SINFO[num].option[usedopts][WID->solver[num].choice[usedopts]->
-                                        value()]);
+            SINFO[num].option[usedopts][WID->solver[num].choice[usedopts]->value()]);
   }
   else {
     strcpy(command, SINFO[num].button_command[idx]);
   }
 
-  sprintf(arg, "\"%s\" %s %s", WID->solver[num].input[0]->value(), mesh,
-          command);
-
+  FixWindowsPath((char*)WID->solver[num].input[0]->value(), tmp);
+  sprintf(arg, "\"%s\" %s %s", tmp, mesh, command);
   Solver(num, arg);
 }
 
diff --git a/Fltk/Solvers.cpp b/Fltk/Solvers.cpp
index 8a71c879e5..e7253527cc 100644
--- a/Fltk/Solvers.cpp
+++ b/Fltk/Solvers.cpp
@@ -1,4 +1,4 @@
-// $Id: Solvers.cpp,v 1.26 2004-07-30 12:22:02 geuzaine Exp $
+// $Id: Solvers.cpp,v 1.27 2004-10-17 01:53:49 geuzaine Exp $
 //
 // Copyright (C) 1997-2004 C. Geuzaine, J.-F. Remacle
 //
@@ -45,17 +45,22 @@ SolverInfo SINFO[MAXSOLVERS];
 int Solver(int num, char *args)
 {
   int sock, type, stop = 0, i, j, n;
-  char command[1000], socket_name[1000], str[1000];
+  char command[1024], socket_name[1024], str[1024], prog[1024];
+
+  FixWindowsPath(SINFO[num].executable_name, prog);
 
   if(!SINFO[num].client_server) {
-    sprintf(command, "%s %s &", SINFO[num].executable_name, args);
+    sprintf(command, "%s %s", prog, args);
+#if !defined(WIN32)
+    strcat(command, " &");
+#endif
     Gmsh_StartClient(command, NULL);
     return 1;
   }
 
-  sprintf(socket_name, "%s.gmshsock-%d", CTX.home_dir, num);
-  sprintf(command, "%s %s -socket \"%s\"", SINFO[num].executable_name,
-          args, socket_name);
+  sprintf(str, "%s.gmshsock-%d", CTX.home_dir, num);
+  FixWindowsPath(str, socket_name);
+  sprintf(command, "%s %s -socket \"%s\"", prog, args, socket_name);
 #if !defined(WIN32)
   strcat(command, " &");
 #endif
@@ -73,9 +78,8 @@ int Solver(int num, char *args)
       Msg(GERROR, "Socket listen failed on '%s'", socket_name);
       break;
     case -4:
-      Msg(GERROR,
-          "Solver not responding (is '%s' correctly installed and in your path?)",
-          SINFO[num].executable_name);
+      Msg(GERROR, "Socket listen timeout on '%s'", socket_name);
+      Msg(GERROR, "Is '%s' correctly installed?", prog);
       break;
     case -5:
       Msg(GERROR, "Socket accept failed on '%s'", socket_name);
diff --git a/Parser/OpenFile.cpp b/Parser/OpenFile.cpp
index 6046ff6190..a8f4d26789 100644
--- a/Parser/OpenFile.cpp
+++ b/Parser/OpenFile.cpp
@@ -1,4 +1,4 @@
-// $Id: OpenFile.cpp,v 1.62 2004-09-13 18:02:52 geuzaine Exp $
+// $Id: OpenFile.cpp,v 1.63 2004-10-17 01:53:49 geuzaine Exp $
 //
 // Copyright (C) 1997-2004 C. Geuzaine, J.-F. Remacle
 //
@@ -19,6 +19,10 @@
 // 
 // Please report all bugs and problems to <gmsh@geuz.org>.
 
+#if defined(__CYGWIN__)
+#include <sys/cygwin.h>
+#endif
+
 #include "Gmsh.h"
 #include "Numeric.h"
 #include "Context.h"
@@ -58,6 +62,14 @@ void FixRelativePath(char *in, char *out){
   }
 }
 
+void FixWindowsPath(char *in, char *out){
+#if defined(__CYGWIN__)
+  cygwin_conv_to_win32_path(in, out);
+#else
+  strcpy(out, in);
+#endif
+}
+
 void SetBoundingBox(double xmin, double xmax,
 		    double ymin, double ymax, 
 		    double zmin, double zmax)
@@ -320,40 +332,16 @@ void OpenProblemMacFinder(const char *filename)
   }
 }
 
-// replace "/cygwin/x/" with "x:/"
-void decygwin(char *in, char *out)
-{
-  unsigned int i = 0, j = 0;
-
-  while(i < strlen(in)) {
-    if(!strncmp(in + i, "/cygdrive/", 10)) {
-      out[j++] = in[i + 10];    // drive letter
-      out[j++] = ':';
-      out[j++] = '/';
-      i += 12;
-    }
-    else {
-      out[j++] = in[i++];
-    }
-  }
-  out[j] = '\0';
-}
-
 void SystemCall(char *command)
 {
 #if defined(WIN32) && defined(HAVE_FLTK)
   STARTUPINFO suInfo;           // Process startup information
   PROCESS_INFORMATION prInfo;   // Process information
-
   memset(&suInfo, 0, sizeof(suInfo));
   suInfo.cb = sizeof(suInfo);
-
-  char copy[strlen(command) + 1];
-  decygwin(command, copy);
-  Msg(INFO, "Calling '%s'", copy);
-  CreateProcess(NULL, copy, NULL, NULL, FALSE,
+  Msg(INFO, "Calling '%s'", command);
+  CreateProcess(NULL, command, NULL, NULL, FALSE,
                 NORMAL_PRIORITY_CLASS, NULL, NULL, &suInfo, &prInfo);
-
 #else
   if(!system(NULL)) {
     Msg(GERROR, "Could not find /bin/sh: aborting system call");
diff --git a/Parser/OpenFile.h b/Parser/OpenFile.h
index c1407700e5..036663c954 100644
--- a/Parser/OpenFile.h
+++ b/Parser/OpenFile.h
@@ -27,6 +27,7 @@ void OpenProblemMacFinder(const char *filename);
 int MergeProblem(char *filename, int warn_if_missing=0);
 void SystemCall(char *command);
 void FixRelativePath(char *in, char *out);
+void FixWindowsPath(char *in, char *out);
 void SetBoundingBox(double xmin, double xmax,
 		    double ymin, double ymax, 
 		    double zmin, double zmax);
-- 
GitLab