2009-07-27 Reini Urban <rurban@x-ray.at>

	* lib-src/winclient.c: Support cygwin paths. Add longer STARTUP_DELAY 
        and CONNECT_RETRIES due to long GUI and DDE initialization on cygwin.

diff -u lib-src/winclient.c.orig lib-src/winclient.c
--- lib-src/winclient.c.orig	2009-06-24 09:30:22.234375000 +0200
+++ lib-src/winclient.c	2009-07-27 10:12:46.828125000 +0200
@@ -1,5 +1,6 @@
 /* DDE client for XEmacs.
    Copyright (C) 2002 Alastair J. Houghton
+   Copyright (C) 2009 Reini Urban
 
    This file is part of XEmacs.
 
@@ -31,6 +32,9 @@
 #include <stdio.h>
 #include <ctype.h>
 #include <errno.h>
+#ifdef __CYGWIN__
+#include <sys/cygwin.h>
+#endif
 
 static void error (const char* s1, const char* s2);
 static void fatal (const char *s1, const char *s2);
@@ -40,6 +44,8 @@
 /* -- Post-Include Defines -------------------------------------------------- */
 
 /* Timeouts & delays */
+#define STARTUP_DELAY           10000 	        /* ms */
+#define CONNECT_RETRIES         10
 #define CONNECT_DELAY		500		/* ms */
 #define TRANSACTION_TIMEOUT	5000		/* ms */
 #define MAX_INPUT_IDLE_WAIT     INFINITE	/* ms */
@@ -205,12 +211,15 @@
       /* Wait for the process to enter an idle state */
       WaitForInputIdle (pi.hProcess, MAX_INPUT_IDLE_WAIT);
 
+      /* Wait longer to let the process init the GUI and initialize the DDE server */
+      Sleep (STARTUP_DELAY);
+
       /* Close the handles */
       CloseHandle (pi.hThread);
       CloseHandle (pi.hProcess);
       
-      /* Try to connect */
-      for (n = 0; n < 5; n++)
+      /* Try to connect. Process startup and XEmacs init might be slow */
+      for (n = 0; n < CONNECT_RETRIES; n++)
 	{
 	  Sleep (CONNECT_DELAY);
 	  
@@ -272,6 +281,27 @@
   char            *buf = NULL;
   unsigned        len;
   
+  /* On Cygwin convert the path - first part - back to POSIX */
+#ifdef __CYGWIN__
+  int             err;
+  int             pathlen;
+  char            *fullpath;
+
+  pathlen = strlen(lpszFileName1) + 260 + 1001;
+  fullpath = (char *) xmalloc (pathlen);
+  err = cygwin_conv_path(CCP_WIN_A_TO_POSIX | CCP_RELATIVE,
+                         lpszFileName1, fullpath, pathlen);
+# ifdef DEBUG
+  fprintf(stderr, "DEBUG doFile(hConv, \"%s\", \"%s\")\n", lpszFileName1, lpszFileName2);
+  fprintf(stderr, "DEBUG W2P \"%s\" => \"%s\" err=%d\n", lpszFileName1, fullpath, err);
+# endif
+  if (!err) {
+      strcpy(lpszFileName1, fullpath);
+      if (strlen(lpszFileName2))
+          strcat(lpszFileName1, "/");
+  }
+#endif
+
   /* Calculate the buffer length */
   len = strlen (lpszFileName1) + strlen (lpszFileName2)
     + strlen (COMMAND_FORMAT);
@@ -296,6 +326,13 @@
   DdeClientTransaction (buf, len, hConv, NULL,
 			0, XTYP_EXECUTE, TRANSACTION_TIMEOUT, NULL);
 
+#ifdef __CYGWIN__
+# ifdef DEBUG
+  fprintf(stderr, "DEBUG DDE => %s\n", buf);
+# endif
+  if (!err)
+      free (fullpath);
+#endif
   free (buf);
   
   return 0;
@@ -404,13 +441,20 @@
   int             ret = 0;
   HANDLE          hFindFile = NULL;
   WIN32_FIND_DATA wfd;
+#ifdef __CYGWIN__
+  int             err;
+#endif
 
   /* Retrieve arguments */
   while ((arg = getNextArg ((const char**)&lpszCommandLine, &len)) != NULL)
     {
       /* First find the canonical path name */
       fullpath = filepart = NULL;
+#ifdef __CYGWIN__
+      pathlen = len + 260 + 1001;
+#else
       pathlen = GetFullPathName (arg, 0, fullpath, &filepart);
+#endif
 
       fullpath = (char *) xmalloc (pathlen);
 
@@ -425,6 +469,17 @@
 	  break;
 	}
 
+#ifdef __CYGWIN__
+      err = cygwin_conv_path(CCP_POSIX_TO_WIN_A | CCP_RELATIVE, arg, fullpath, pathlen);
+# ifdef DEBUG
+      fprintf(stderr, "DEBUG P2W \"%s\" => \"%s\" err=%d\n", arg, fullpath, err);
+# endif
+      if (err)
+          ret = doFile (hConv, "", arg);
+      else
+          arg = fullpath;
+#endif
+
       GetFullPathName (arg, pathlen, fullpath, &filepart);
 
       /* Find the first matching file */
@@ -454,7 +509,10 @@
 
       /* Release the path name buffers */
       free (fullpath);
-      free (arg);
+#ifdef __CYGWIN__
+      if (err)
+          free (arg);
+#endif
 
       if (ret)
 	break;
