[svn] GpgEX - r36 - trunk/src

svn author marcus cvs at cvs.gnupg.org
Tue Oct 23 00:25:10 CEST 2007


Author: marcus
Date: 2007-10-23 00:24:59 +0200 (Tue, 23 Oct 2007)
New Revision: 36

Modified:
   trunk/src/ChangeLog
   trunk/src/client.cc
   trunk/src/exechelp.c
   trunk/src/exechelp.h
Log:
2007-10-22  Marcus Brinkmann  <marcus at g10code.de>

	* exechelp.h (gpgex_spawn_detached): Change signature to just a
	command line.
	* exechelp.c: Rewritten.
	* client.cc (replace_dollar_s): New function.
	(default_uiserver_cmdline): Use it.
	(uiserver_connect): Change caller of gpgex_spawn_detached.


Modified: trunk/src/ChangeLog
===================================================================
--- trunk/src/ChangeLog	2007-10-16 22:50:46 UTC (rev 35)
+++ trunk/src/ChangeLog	2007-10-22 22:24:59 UTC (rev 36)
@@ -1,3 +1,12 @@
+2007-10-22  Marcus Brinkmann  <marcus at g10code.de>
+
+	* exechelp.h (gpgex_spawn_detached): Change signature to just a
+	command line.
+	* exechelp.c: Rewritten.
+	* client.cc (replace_dollar_s): New function.
+	(default_uiserver_cmdline): Use it.
+	(uiserver_connect): Change caller of gpgex_spawn_detached.
+
 2007-10-17  Marcus Brinkmann  <marcus at g10code.de>
 
 	* client.cc (default_uiserver_name): Fix check for dirty hack.

Modified: trunk/src/client.cc
===================================================================
--- trunk/src/client.cc	2007-10-16 22:50:46 UTC (rev 35)
+++ trunk/src/client.cc	2007-10-22 22:24:59 UTC (rev 36)
@@ -41,57 +41,140 @@
 
 
 static const char *
-default_uiserver_name (void)
+default_socket_name (void)
 {
   static string name;
 
   if (name.size () == 0)
     {
       char *dir = NULL;
-
-      dir = read_w32_registry_string ("HKEY_LOCAL_MACHINE", REGKEY,
-				      "Install Directory");
+      
+      dir = default_homedir ();
       if (dir)
 	{
-	  char *uiserver = NULL;
-	  int uiserver_malloced = 1;
-	  
-	  uiserver = read_w32_registry_string (NULL, REGKEY, "UI Server");
-	  if (!uiserver)
-	    {
-	      uiserver = "bin\\kleopatra.exe";
-	      uiserver_malloced = 0;
-	    }
+	  try { name = ((string) dir) + "\\S.uiserver"; } catch (...) {}
+	  free ((void *) dir);
+	}
+    }
 
-	  /* FIXME: Very dirty work-around to make kleopatra find its
-	     DLLs.  */
-	  if (!strcmp (uiserver, "bin\\kleopatra.exe"))
-	    chdir (dir);
+  return name.c_str ();
+}
 
-	  try { name = ((string) dir) + "\\" + uiserver; } catch (...) {}
 
-	  if (uiserver_malloced)
-	    free (uiserver);
-	  free ((void *) dir);
+/* Substitute all substrings "$s" in BUFFER by the value of the
+   default socket and replace all "$$" by "$".  Free BUFFER if
+   necessary and return a newly malloced buffer.  */
+static char *
+replace_dollar_s (char *str)
+{
+  int n;
+  char *src;
+  char *dst;
+  const char *socket_name = default_socket_name ();
+  int socket_name_len = strlen (socket_name);
+  char *new_str;
+
+  n = 0;
+  src = str;
+
+  while (*src)
+    {
+      if (*src == '$')
+	{
+	  src++;
+	  if (*src == '\0')
+	    break;
+	  else if (*src == 's')
+	    /* Socket name and surrounding quotes.  */
+	    n += socket_name_len + 2;
+	  else
+	    n++;
 	}
+      else
+	n++;
+      src++;
     }
+  /* Terminating zero.  */
+  n++;
 
-  return name.c_str ();
+  new_str = (char *) malloc (n);
+  if (!new_str)
+    return NULL;
+
+  src = str;
+  dst = new_str;
+  while (*src)
+    {
+      if (*src == '$')
+	{
+	  src++;
+	  if (*src == '\0')
+	    break;
+	  else if (*src == 's')
+	    {
+	      /* Socket name and surrounding quotes.  */
+	      *(dst++) = '"';
+	      memcpy (dst, socket_name, socket_name_len);
+	      dst += socket_name_len;
+	      *(dst++) = '"';
+	    }
+	  else
+	    /* Pass through the next character.  */
+	    *(dst++) = *src;
+	}
+      else
+	*(dst++) = *src;
+      src++;
+    }
+  /* Terminating zero.  */
+  *dst = '\0';
+  
+  return new_str;
 }
 
+
 static const char *
-default_socket_name (void)
+default_uiserver_cmdline (void)
 {
   static string name;
 
   if (name.size () == 0)
     {
       char *dir = NULL;
-      
-      dir = default_homedir ();
+
+      dir = read_w32_registry_string ("HKEY_LOCAL_MACHINE", REGKEY,
+				      "Install Directory");
       if (dir)
 	{
-	  try { name = ((string) dir) + "\\S.uiserver"; } catch (...) {}
+	  char *uiserver = NULL;
+	  char *old_uiserver = NULL;
+	  int uiserver_malloced = 1;
+	  
+	  uiserver = read_w32_registry_string (NULL, REGKEY, "UI Server");
+	  if (!uiserver)
+	    {
+	      uiserver = "bin\\kleopatra.exe --uiserver-socket $s";
+	      uiserver_malloced = 0;
+	    }
+
+	  old_uiserver = uiserver;
+	  uiserver = replace_dollar_s (old_uiserver);
+	  if (uiserver_malloced)
+	    free (old_uiserver);
+	  if (!uiserver)
+	    {
+	      free ((void *) dir);
+	      return NULL;
+	    }
+
+	  /* FIXME: Very dirty work-around to make kleopatra find
+	     its DLLs.  */
+	  if (!strncmp (uiserver, "bin\\kleopatra.exe", 17))
+	    chdir (dir);
+	  
+	  try { name = ((string) dir) + "\\" + uiserver; } catch (...) {}
+	  
+	  free (uiserver);
 	  free ((void *) dir);
 	}
     }
@@ -184,16 +267,11 @@
   rc = assuan_socket_connect (ctx, socket_name, -1);
   if (rc)
     {
-      const char *argv[3];
       int count;
 
       (void) TRACE_LOG ("UI server not running, starting it");
 
-      argv[0] = "--uiserver-socket";
-      argv[1] = socket_name;
-      argv[2] = NULL;
-
-      rc = gpgex_spawn_detached (default_uiserver_name (), argv);
+      rc = gpgex_spawn_detached (default_uiserver_cmdline ());
       if (rc)
 	return TRACE_GPGERR (rc);
 

Modified: trunk/src/exechelp.c
===================================================================
--- trunk/src/exechelp.c	2007-10-16 22:50:46 UTC (rev 35)
+++ trunk/src/exechelp.c	2007-10-22 22:24:59 UTC (rev 36)
@@ -38,99 +38,11 @@
 #define DEBUG_W32_SPAWN 0
 
 
-/* Replacement function.  */
-
-#define stpcpy my_sptcpy
-
-static char *
-stpcpy (char *d, const char *s)
-{
-  do
-    *d++ = *s;
-  while (*s++ != '\0');
-
-  return d - 1;
-}
-
-
-/* Helper function to build_w32_commandline. */
-static char *
-build_w32_commandline_copy (char *buffer, const char *string)
-{
-  char *p = buffer;
-  const char *s;
-
-  if (!*string) /* Empty string. */
-    p = stpcpy (p, "\"\"");
-  else if (strpbrk (string, " \t\n\v\f\""))
-    {
-      /* Need to do some kind of quoting.  */
-      p = stpcpy (p, "\"");
-      for (s=string; *s; s++)
-        {
-          *p++ = *s;
-          if (*s == '\"')
-            *p++ = *s;
-        }
-      *p++ = '\"';
-      *p = 0;
-    }
-  else
-    p = stpcpy (p, string);
-
-  return p;
-}
-
-/* Build a command line for use with W32's CreateProcess.  On success
-   CMDLINE gets the address of a newly allocated string.  */
-static gpg_error_t
-build_w32_commandline (const char *pgmname, const char * const *argv, 
-                       char **cmdline)
-{
-  int i, n;
-  const char *s;
-  char *buf, *p;
-
-  *cmdline = NULL;
-  n = 0;
-  s = pgmname;
-  n += strlen (s) + 1 + 2;  /* (1 space, 2 quotes) */
-  for (; *s; s++)
-    if (*s == '\"')
-      n++;  /* Need to double inner quotes.  */
-  for (i=0; (s=argv[i]); i++)
-    {
-      n += strlen (s) + 1 + 2;  /* (1 space, 2 quoting) */
-      for (; *s; s++)
-        if (*s == '\"')
-          n++;  /* Need to double inner quotes.  */
-    }
-  n++;
-
-  buf = p = malloc (n);
-  if (!buf)
-    return gpg_error_from_syserror ();
-
-  p = build_w32_commandline_copy (p, pgmname);
-  for (i = 0; argv[i]; i++) 
-    {
-      *p++ = ' ';
-      p = build_w32_commandline_copy (p, argv[i]);
-    }
-
-  *cmdline = buf;
-  return 0;
-}
-
-
-/* Fork and exec the PGMNAME with /dev/null as stdin, stdout and
-   stderr.  The arguments for the process are expected in the NULL
-   terminated array ARGV.  The program name itself should not be
-   included there.  Returns 0 on success or an error code.  */
+/* Fork and exec the program with /dev/null as stdin, stdout and
+   stderr.  Returns 0 on success or an error code.  */
 gpg_error_t
-gpgex_spawn_detached (const char *pgmname, const char *const argv[])
+gpgex_spawn_detached (const char *cmdline)
 {
-  gpg_error_t err;
   SECURITY_ATTRIBUTES sec_attr;
   PROCESS_INFORMATION pi = 
     {
@@ -141,23 +53,10 @@
     };
   STARTUPINFO si;
   int cr_flags;
-  char *cmdline;
-  int i;
 
-  TRACE_BEG1 (DEBUG_ASSUAN, "gpgex_spawn_detached", pgmname,
-	      "pgmname=%s", pgmname);
-  i = 0;
-  while (argv[i])
-    {
-      TRACE_LOG2 ("argv[%2i] = %s", i, argv[i]);
-      i++;
-    }
+  TRACE_BEG1 (DEBUG_ASSUAN, "gpgex_spawn_detached", cmdline,
+	      "cmdline=%s", cmdline);
 
-  /* Build the command line.  */
-  err = build_w32_commandline (pgmname, argv, &cmdline);
-  if (err)
-    return err; 
-
   /* Prepare security attributes.  */
   memset (&sec_attr, 0, sizeof sec_attr);
   sec_attr.nLength = sizeof sec_attr;
@@ -173,12 +72,10 @@
   cr_flags = (CREATE_DEFAULT_ERROR_MODE
               | GetPriorityClass (GetCurrentProcess ())
 	      | CREATE_NEW_PROCESS_GROUP
-              | DETACHED_PROCESS); 
+              | DETACHED_PROCESS);
 
-  (void) TRACE_LOG2 ("CreateProcess, path=`%s' cmdline=`%s'\n",
-		     pgmname, cmdline);
-  if (!CreateProcess (pgmname,     /* pgmname; Program to start.  */
-                      cmdline,       /* Command line arguments.  */
+  if (!CreateProcess (NULL,          /* pgmname; Program to start.  */
+                      (char *) cmdline, /* Command line arguments.  */
                       &sec_attr,     /* Process security attributes.  */
                       &sec_attr,     /* Thread security attributes.  */
                       TRUE,          /* Inherit handles.  */
@@ -190,10 +87,8 @@
                       ))
     {
       (void) TRACE_LOG1 ("CreateProcess failed: %i\n", GetLastError ());
-      free (cmdline);
       return gpg_error (GPG_ERR_GENERAL);
     }
-  free (cmdline);
   
   /* Process has been created suspended; resume it now. */
   CloseHandle (pi.hThread); 

Modified: trunk/src/exechelp.h
===================================================================
--- trunk/src/exechelp.h	2007-10-16 22:50:46 UTC (rev 35)
+++ trunk/src/exechelp.h	2007-10-22 22:24:59 UTC (rev 36)
@@ -30,12 +30,9 @@
 #endif
 #endif
 
-/* Fork and exec the PGMNAME with /dev/null as stdin, stdout and
-   stderr.  The arguments for the process are expected in the NULL
-   terminated array ARGV.  The program name itself should not be
-   included there.  Returns 0 on success or an error code.  */
-gpg_error_t gpgex_spawn_detached (const char *pgmname,
-				  const char *const argv[]);
+/* Fork and exec CMDLINE with /dev/null as stdin, stdout and stderr.
+   Returns 0 on success or an error code.  */
+gpg_error_t gpgex_spawn_detached (const char *cmdline);
 #ifdef __cplusplus
 #if 0
 {




More information about the Gnupg-commits mailing list