Comment changes and clarifications.
[bpt/emacs.git] / lib-src / emacsclient.c
index 6970c1a..ea1e6cd 100644 (file)
@@ -1,6 +1,6 @@
 /* Client process that communicates with GNU Emacs acting as server.
    Copyright (C) 1986, 1987, 1994, 1999, 2000, 2001, 2002, 2003, 2004,
-                 2005, 2006 Free Software Foundation, Inc.
+                 2005, 2006, 2007 Free Software Foundation, Inc.
 
 This file is part of GNU Emacs.
 
@@ -129,6 +129,9 @@ char *socket_name = NULL;
 /* If non-NULL, the filename of the authentication file.  */
 char *server_file = NULL;
 
+/* PID of the Emacs server process.  */
+int emacs_pid = 0;
+
 void print_help_and_exit () NO_RETURN;
 
 struct option longopts[] =
@@ -149,16 +152,15 @@ struct option longopts[] =
 /* Message functions. */
 
 #ifdef WINDOWSNT
-/* I first tried to check for STDOUT.  The check did not work,
-   I get a valid handle also in nonconsole apps.
-   Instead I test for console title, which seems to work.  */
 int
-w32_window_app()
+w32_window_app ()
 {
   static int window_app = -1;
   char szTitle[MAX_PATH];
 
   if (window_app < 0)
+    /* Checking for STDOUT does not work; it's a valid handle also in
+       nonconsole apps.  Testing for the console title seems to work. */
     window_app = (GetConsoleTitleA (szTitle, MAX_PATH) == 0);
 
   return window_app;
@@ -168,18 +170,10 @@ w32_window_app()
 void
 message (int is_error, char *message, ...)
 {
-  char buf [2048];
-  char *msg = buf;
+  char msg [2048];
   va_list args;
 
   va_start (args, message);
-
-  if (is_error)
-    {
-      sprintf (buf, "%s: ", progname);
-      msg = strchr (buf, '\0');
-    }
-
   vsprintf (msg, message, args);
   va_end (args);
 
@@ -193,7 +187,12 @@ message (int is_error, char *message, ...)
     }
   else
 #endif
-    fprintf (is_error ? stderr : stdout, msg);
+    {
+      FILE *f = is_error ? stderr : stdout;
+
+      fputs (msg, f);
+      fflush (f);
+    }
 }
 
 /* Decode the options from argv and argc.
@@ -298,6 +297,43 @@ Report bugs to bug-gnu-emacs@gnu.org.\n", progname);
 }
 
 \f
+#ifdef WINDOWSNT
+
+/*
+  execvp wrapper for Windows. Quotes arguments with embedded spaces.
+
+  This is necessary due to the broken implementation of exec* routines in
+  the Microsoft libraries: they concatenate the arguments together without
+  quoting special characters, and pass the result to CreateProcess, with
+  predictably bad results.  By contrast, Posix execvp passes the arguments
+  directly into the argv array of the child process.
+*/
+int
+w32_execvp (path, argv)
+     char *path;
+     char **argv;
+{
+  int i;
+
+  /* Required to allow a .BAT script as alternate editor.  */
+  argv[0] = (char *) alternate_editor;
+
+  for (i = 0; argv[i]; i++)
+    if (strchr (argv[i], ' '))
+      {
+       char *quoted = alloca (strlen (argv[i]) + 3);
+       sprintf (quoted, "\"%s\"", argv[i]);
+       argv[i] = quoted;
+      }
+
+  return execvp (path, argv);
+}
+
+#undef execvp
+#define execvp w32_execvp
+
+#endif /* WINDOWSNT */
+
 /*
   Try to run a different command, or --if no alternate editor is
   defined-- exit with an errorcode.
@@ -310,9 +346,7 @@ fail (argc, argv)
   if (alternate_editor)
     {
       int i = optind - 1;
-#ifdef WINDOWSNT
-      argv[i] = (char *)alternate_editor;
-#endif
+
       execvp (alternate_editor, argv + i);
       message (TRUE, "%s: error executing alternate editor \"%s\"\n",
                progname, alternate_editor);
@@ -450,20 +484,52 @@ file_name_absolute_p (filename)
   if (filename[0] == '\0') return FALSE;
 
 #ifdef WINDOWSNT
-  /* X:\xxx is always absolute; X:xxx is an error and will fail.  */
+  /* X:\xxx is always absolute.  */
   if (isalpha (filename[0])
       && filename[1] == ':' && (filename[2] == '\\' || filename[2] == '/'))
     return TRUE;
 
   /* Both \xxx and \\xxx\yyy are absolute.  */
   if (filename[0] == '\\') return TRUE;
+
+  /*
+    FIXME:  There's a corner case not dealt with, "x:y", where:
+
+    1) x is a valid drive designation (usually a letter in the A-Z range)
+       and y is a path, relative to the current directory on drive x.  This
+       is absolute, *after* fixing the y part to include the current
+       directory in x.
+
+    2) x is a relative file name, and y is an NTFS stream name.  This is a
+       correct relative path, but it is very unusual.
+
+    The trouble is that first case items are also valid examples of the
+    second case, i.e., "c:test" can be understood as drive:path or as
+    file:stream.
+
+    The "right" fix would involve checking whether
+    - the current drive/partition is NTFS,
+    - x is a valid (and accesible) drive designator,
+    - x:y already exists as a file:stream in the current directory,
+    - y already exists on the current directory of drive x,
+    - the auspices are favorable,
+    and then taking an "informed decision" based on the above.
+
+    Whatever the result, Emacs currently does a very bad job of dealing
+    with NTFS file:streams: it cannot visit them, and the only way to
+    create one is by setting `buffer-file-name' to point to it (either
+    manually or with emacsclient). So perhaps resorting to 1) and ignoring
+    2) for now is the right thing to do.
+
+    Anyway, something to decide After the Release.
+  */
 #endif
 
   return FALSE;
 }
 
 #ifdef WINDOWSNT
-/* Wrapper to make WSACleanup a cdecl, as required by atexit().  */
+/* Wrapper to make WSACleanup a cdecl, as required by atexit.  */
 void
 __cdecl close_winsock ()
 {
@@ -489,7 +555,7 @@ initialize_sockets ()
 /*
  * Read the information needed to set up a TCP comm channel with
  * the Emacs server: host, port, pid and authentication string.
-*/
+ */
 int
 get_server_config (server, authentication)
      struct sockaddr_in *server;
@@ -550,29 +616,7 @@ get_server_config (server, authentication)
 
   fclose (config);
 
-#ifdef WINDOWSNT
-  /*
-    Modern Windows restrict which processes can set the foreground window.
-    So, for emacsclient to be able to force Emacs into the foreground, we
-    have to call AllowSetForegroundWindow().  Unfortunately, older Windows
-    (W95, W98 and NT) don't have this function, so we have to check first.
-
-    We're doing this here because it has to be done before sending info
-    to Emacs, and otherwise we'll need a global variable just to pass around
-    the pid, which is also inelegant.
-   */
-  {
-    HMODULE hUser32;
-
-    if (hUser32 = LoadLibrary ("user32.dll"))
-      {
-        FARPROC set_fg;
-        if (set_fg = GetProcAddress (hUser32, "AllowSetForegroundWindow"))
-          set_fg (atoi (pid));
-        FreeLibrary (hUser32);
-      }
-  }
-#endif
+  emacs_pid = atoi (pid);
 
   return TRUE;
 }
@@ -589,7 +633,7 @@ set_tcp_socket ()
     return INVALID_SOCKET;
 
   if (server.sin_addr.s_addr != inet_addr ("127.0.0.1"))
-    message (TRUE, "%s: connected to remote socket at %s\n",
+    message (FALSE, "%s: connected to remote socket at %s\n",
              progname, inet_ntoa (server.sin_addr));
 
   /*
@@ -833,6 +877,62 @@ set_socket ()
   exit (EXIT_FAILURE);
 }
 
+#ifdef WINDOWSNT
+FARPROC set_fg;  /* Pointer to AllowSetForegroundWindow.  */
+FARPROC get_wc;  /* Pointer to RealGetWindowClassA.  */
+
+BOOL CALLBACK
+w32_find_emacs_process (hWnd, lParam)
+     HWND hWnd;
+     LPARAM lParam;
+{
+  DWORD pid;
+  char class[6];
+
+  /* Reject any window not of class "Emacs".  */
+  if (! get_wc (hWnd, class, sizeof (class))
+      || strcmp (class, "Emacs"))
+    return TRUE;
+
+  /* We only need the process id, not the thread id.  */
+  (void) GetWindowThreadProcessId (hWnd, &pid);
+
+  /* Not the one we're looking for.  */
+  if (pid != (DWORD) emacs_pid) return TRUE;
+
+  /* OK, let's raise it.  */
+  set_fg (emacs_pid);
+
+  /* Stop enumeration.  */
+  return FALSE;
+}
+
+/*
+ * Search for a window of class "Emacs" and owned by a process with
+ * process id = emacs_pid.  If found, allow it to grab the focus.
+ */
+void
+w32_give_focus ()
+{
+  HMODULE hUser32;
+
+  /* It shouldn't happen when dealing with TCP sockets.  */
+  if (!emacs_pid) return;
+
+  if (!(hUser32 = LoadLibrary ("user32.dll"))) return;
+
+  /* Modern Windows restrict which processes can set the foreground window.
+     emacsclient can allow Emacs to grab the focus by calling the function
+     AllowSetForegroundWindow.  Unfortunately, older Windows (W95, W98 and
+     NT) lack this function, so we have to check its availability.  */
+  if ((set_fg = GetProcAddress (hUser32, "AllowSetForegroundWindow"))
+      && (get_wc = GetProcAddress (hUser32, "RealGetWindowClassA")))
+    EnumWindows (w32_find_emacs_process, (LPARAM) 0);
+
+  FreeLibrary (hUser32);
+}
+#endif
+
 int
 main (argc, argv)
      int argc;
@@ -866,15 +966,20 @@ main (argc, argv)
   if (cwd == 0)
     {
       /* getwd puts message in STRING if it fails.  */
-#ifdef HAVE_GETCWD
       message (TRUE, "%s: %s (%s)\n", progname,
-              "Cannot get current working directory", strerror (errno));
+#ifdef HAVE_GETCWD
+              "Cannot get current working directory",
 #else
-      message (TRUE, "%s: %s (%s)\n", progname, string, strerror (errno));
+              string,
 #endif
+              strerror (errno));
       fail (argc, argv);
     }
 
+#ifdef WINDOWSNT
+  w32_give_focus ();
+#endif
+
   if (nowait)
     SEND_STRING ("-nowait ");