Convert some more functions to standard C.
[bpt/emacs.git] / lib-src / emacsclient.c
index ef14669..bafc7e0 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, 2007, 2008 Free Software Foundation, Inc.
+                 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
 
 This file is part of GNU Emacs.
 
@@ -41,17 +41,23 @@ along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
 
 #else /* !WINDOWSNT */
 
-# include <sys/types.h>
+# include "syswait.h"
 
 # ifdef HAVE_INET_SOCKETS
 #  include <netinet/in.h>
 # endif
 
+# include <arpa/inet.h>
+
 # define INVALID_SOCKET -1
 # define HSOCKET int
 # define CLOSE_SOCKET close
 # define INITIALIZE()
 
+# ifndef WCONTINUED
+#  define WCONTINUED 8
+# endif
+
 #endif /* !WINDOWSNT */
 
 #undef signal
@@ -64,22 +70,18 @@ along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
 #include <unistd.h>
 #endif
 
-#ifdef VMS
-# include "vms-pwd.h"
-#else /* not VMS */
 #ifdef WINDOWSNT
 # include <io.h>
 #else /* not WINDOWSNT */
 # include <pwd.h>
 #endif /* not WINDOWSNT */
-#endif /* not VMS */
 #include <sys/stat.h>
 
 #include <signal.h>
 #include <errno.h>
 
 \f
-char *getenv (), *getwd ();
+char *getenv (const char *), *getwd (char *);
 char *(getcwd) ();
 
 #ifdef WINDOWSNT
@@ -113,6 +115,10 @@ char *w32_getenv ();
 #ifndef NO_RETURN
 #define NO_RETURN
 #endif
+
+/* Additional space when allocating buffers for filenames, etc.  */
+#define EXTRA_SPACE 100
+
 \f
 /* Name used to invoke this program.  */
 char *progname;
@@ -129,12 +135,12 @@ int eval = 0;
 /* Nonzero means don't open a new frame.  Inverse of --create-frame.  */
 int current_frame = 1;
 
-/* Nonzero means open a new graphical frame. */
-int window_system = 0;
-
 /* The display on which Emacs should work.  --display.  */
 char *display = NULL;
 
+/* The parent window ID, if we are opening a frame via XEmbed.  */
+char *parent_id = NULL;
+
 /* Nonzero means open a new Emacs frame on the current terminal. */
 int tty = 0;
 
@@ -151,7 +157,7 @@ char *server_file = NULL;
 /* PID of the Emacs server process.  */
 int emacs_pid = 0;
 
-void print_help_and_exit () NO_RETURN;
+void print_help_and_exit (void) NO_RETURN;
 
 struct option longopts[] =
 {
@@ -160,6 +166,7 @@ struct option longopts[] =
   { "help",    no_argument,       NULL, 'H' },
   { "version", no_argument,       NULL, 'V' },
   { "tty",     no_argument,       NULL, 't' },
+  { "nw",      no_argument,       NULL, 't' },
   { "create-frame", no_argument,   NULL, 'c' },
   { "alternate-editor", required_argument, NULL, 'a' },
 #ifndef NO_SOCKETS_IN_FILE_SYSTEM
@@ -169,6 +176,7 @@ struct option longopts[] =
 #ifndef WINDOWSNT
   { "display", required_argument, NULL, 'd' },
 #endif
+  { "parent-id", required_argument, NULL, 'p' },
   { 0, 0, 0, 0 }
 };
 
@@ -176,8 +184,7 @@ struct option longopts[] =
 /* Like malloc but get fatal error if memory is exhausted.  */
 
 long *
-xmalloc (size)
-     unsigned int size;
+xmalloc (unsigned int size)
 {
   long *result = (long *) malloc (size);
   if (result == NULL)
@@ -228,7 +235,7 @@ xstrdup (const char *s)
    Any other returned value must be freed with free.  This is used
    only when get_current_dir_name is not defined on the system.  */
 char*
-get_current_dir_name ()
+get_current_dir_name (void)
 {
   char *buf;
   char *pwd;
@@ -304,10 +311,7 @@ get_current_dir_name ()
    Return NULL if the variable was not found, or it was empty.
    This code is based on w32_get_resource (w32.c).  */
 char *
-w32_get_resource (predefined, key, type)
-     HKEY predefined;
-     char *key;
-     LPDWORD type;
+w32_get_resource (HKEY predefined, char *key, LPDWORD type)
 {
   HKEY hrootkey = NULL;
   char *result = NULL;
@@ -319,8 +323,8 @@ w32_get_resource (predefined, key, type)
        {
          result = (char *) xmalloc (cbData);
 
-         if ((RegQueryValueEx (hrootkey, key, NULL, type, result, &cbData) != ERROR_SUCCESS) ||
-             (*result == 0))
+         if ((RegQueryValueEx (hrootkey, key, NULL, type, result, &cbData) != ERROR_SUCCESS)
+             || (*result == 0))
            {
              free (result);
              result = NULL;
@@ -336,12 +340,11 @@ w32_get_resource (predefined, key, type)
 /*
   getenv wrapper for Windows
 
-  This is needed to duplicate Emacs's behavior, which is to look for enviroment
+  This is needed to duplicate Emacs's behavior, which is to look for environment
   variables in the registry if they don't appear in the environment.
 */
 char *
-w32_getenv (envvar)
-     char *envvar;
+w32_getenv (char *envvar)
 {
   char *value;
   DWORD dwType;
@@ -352,8 +355,13 @@ w32_getenv (envvar)
 
   if (! (value = w32_get_resource (HKEY_CURRENT_USER, envvar, &dwType)) &&
       ! (value = w32_get_resource (HKEY_LOCAL_MACHINE, envvar, &dwType)))
-    /* Not found in the registry.  */
-    return NULL;
+    {
+      /* "w32console" is what Emacs on Windows uses for tty-type under -nw.  */
+      if (strcmp (envvar, "TERM") == 0)
+       return xstrdup ("w32console");
+      /* Found neither in the environment nor in the registry.  */
+      return NULL;
+    }
 
   if (dwType == REG_SZ)
     /* Registry; no need to expand.  */
@@ -383,8 +391,35 @@ w32_getenv (envvar)
   return NULL;
 }
 
+void
+w32_set_user_model_id (void)
+{
+  HMODULE shell;
+  HRESULT (WINAPI * set_user_model) (wchar_t * id);
+
+  /* On Windows 7 and later, we need to set the user model ID
+     to associate emacsclient launched files with Emacs frames
+     in the UI.  */
+  shell = LoadLibrary("shell32.dll");
+  if (shell)
+    {
+      set_user_model
+       = (void *) GetProcAddress (shell,
+                                  "SetCurrentProcessExplicitAppUserModelID");
+      /* If the function is defined, then we are running on Windows 7
+        or newer, and the UI uses this to group related windows
+        together.  Since emacs, runemacs, emacsclient are related, we
+        want them grouped even though the executables are different,
+        so we need to set a consistent ID between them.  */
+      if (set_user_model)
+       set_user_model (L"GNU.Emacs");
+
+      FreeLibrary (shell);
+    }
+}
+
 int
-w32_window_app ()
+w32_window_app (void)
 {
   static int window_app = -1;
   char szTitle[MAX_PATH];
@@ -407,13 +442,11 @@ w32_window_app ()
   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
+  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;
+w32_execvp (const char *path, char **argv)
 {
   int i;
 
@@ -434,6 +467,13 @@ w32_execvp (path, argv)
 #undef execvp
 #define execvp w32_execvp
 
+/* Emulation of ttyname for Windows.  */
+char *
+ttyname (int fd)
+{
+  return "CONOUT$";
+}
+
 #endif /* WINDOWSNT */
 
 /* Display a normal or error message.
@@ -470,15 +510,13 @@ message (int is_error, char *message, ...)
    The global variable `optind' will say how many arguments we used up.  */
 
 void
-decode_options (argc, argv)
-     int argc;
-     char **argv;
+decode_options (int argc, char **argv)
 {
   alternate_editor = egetenv ("ALTERNATE_EDITOR");
 
   while (1)
     {
-      int opt = getopt_long (argc, argv,
+      int opt = getopt_long_only (argc, argv,
 #ifndef NO_SOCKETS_IN_FILE_SYSTEM
                             "VHnea:s:f:d:tc",
 #else
@@ -514,11 +552,9 @@ decode_options (argc, argv)
             to allow it, for the occasional case where the user is
             connecting with a w32 client to a server compiled with X11
             support.  */
-#if 1 /* !defined WINDOWS */
        case 'd':
          display = optarg;
          break;
-#endif
 
        case 'n':
          nowait = 1;
@@ -542,6 +578,11 @@ decode_options (argc, argv)
           current_frame = 0;
           break;
 
+       case 'p':
+         parent_id = optarg;
+          current_frame = 0;
+         break;
+
        case 'H':
          print_help_and_exit ();
          break;
@@ -553,45 +594,49 @@ decode_options (argc, argv)
        }
     }
 
-  /* We used to set `display' to $DISPLAY by default, but this changed the
-     default behavior and is sometimes inconvenient.  So instead of forcing
-     users to say "--display ''" when they want to use Emacs's existing tty
-     or display connection, we force them to use "--display $DISPLAY" if
-     they want Emacs to connect to their current display.
-     -c still implicitly passes --display $DISPLAY unless -t was specified
-     so as to try and mimick the behavior of `emacs' which either uses
-     the current tty or the current $DISPLAY.  */
+  /* If the -c option is used (without -t) and no --display argument
+     is provided, try $DISPLAY.
+     Without the -c option, we used to set `display' to $DISPLAY by
+     default, but this changed the default behavior and is sometimes
+     inconvenient.  So we force users to use "--display $DISPLAY" if
+     they want Emacs to connect to their current display.  */
   if (!current_frame && !tty && !display)
-    display = egetenv ("DISPLAY");
+    {
+      display = egetenv ("DISPLAY");
+#ifdef NS_IMPL_COCOA
+      /* Under Cocoa, we don't really use displays the same way as in X,
+         so provide a dummy. */
+      if (!display || strlen (display) == 0)
+        display = "ns";
+#endif
+    }
 
+  /* A null-string display is invalid.  */
   if (display && strlen (display) == 0)
     display = NULL;
 
-  if (!tty && display)
-    window_system = 1;
-#if !defined (WINDOWSNT) && !defined (HAVE_CARBON)
-  else if (!current_frame)
+  /* If no display is available, new frames are tty frames.  */
+  if (!current_frame && !display)
     tty = 1;
-#endif
 
   /* --no-wait implies --current-frame on ttys when there are file
-       arguments or expressions given.  */
+     arguments or expressions given.  */
   if (nowait && tty && argc - optind > 0)
     current_frame = 1;
 
-  if (current_frame)
+#ifdef WINDOWSNT
+  if (alternate_editor && alternate_editor[0] == '\0')
     {
-      tty = 0;
-      window_system = 0;
+      message (TRUE, "--alternate-editor argument or ALTERNATE_EDITOR variable cannot be\n\
+an empty string");
+      exit (EXIT_FAILURE);
     }
-
-  if (tty)
-    window_system = 0;
+#endif /* WINDOWSNT */
 }
 
 \f
 void
-print_help_and_exit ()
+print_help_and_exit (void)
 {
   /* Spaces and tabs are significant in this message; they're chosen so the
      message aligns properly both in a tty and in a Windows message box.
@@ -605,22 +650,28 @@ Every FILE can be either just a FILENAME or [+LINE[:COLUMN]] FILENAME.\n\
 The following OPTIONS are accepted:\n\
 -V, --version          Just print version info and return\n\
 -H, --help             Print this usage information message\n\
--t, --tty              Open a new Emacs frame on the current terminal\n\
+-nw, -t, --tty                 Open a new Emacs frame on the current terminal\n\
 -c, --create-frame     Create a new frame instead of trying to\n\
                        use the current Emacs frame\n\
 -e, --eval             Evaluate the FILE arguments as ELisp expressions\n\
 -n, --no-wait          Don't wait for the server to return\n\
--d, --display=DISPLAY  Visit the file in the given display\n"
+-d DISPLAY, --display=DISPLAY\n\
+                       Visit the file in the given display\n\
+--parent-id=ID          Open in parent window ID, via XEmbed\n"
 #ifndef NO_SOCKETS_IN_FILE_SYSTEM
-"-s, --socket-name=FILENAME\n\
+"-s SOCKET, --socket-name=SOCKET\n\
                        Set filename of the UNIX socket for communication\n"
 #endif
-"-f, --server-file=FILENAME\n\
+"-f SERVER, --server-file=SERVER\n\
                        Set filename of the TCP authentication file\n\
--a, --alternate-editor=EDITOR\n\
-                       Editor to fallback to if the server is not running\n\
-\n\
-Report bugs to bug-gnu-emacs@gnu.org.\n", progname);
+-a EDITOR, --alternate-editor=EDITOR\n\
+                       Editor to fallback to if the server is not running\n"
+#ifndef WINDOWSNT
+"                      If EDITOR is the empty string, start Emacs in daemon\n\
+                       mode and try connecting again\n"
+#endif /* not WINDOWSNT */
+"\n\
+Report bugs with M-x report-emacs-bug.\n", progname);
   exit (EXIT_SUCCESS);
 }
 
@@ -672,8 +723,7 @@ main (argc, argv)
 #define AUTH_KEY_LENGTH      64
 #define SEND_BUFFER_SIZE   4096
 
-extern char *strerror ();
-extern int errno;
+extern char *strerror (int);
 
 /* Buffer to accumulate data to send in TCP connections.  */
 char send_buffer[SEND_BUFFER_SIZE + 1];
@@ -684,8 +734,7 @@ HSOCKET emacs_socket = 0;
 /* On Windows, the socket library was historically separate from the standard
    C library, so errors are handled differently.  */
 void
-sock_err_message (function_name)
-     char *function_name;
+sock_err_message (char *function_name)
 {
 #ifdef WINDOWSNT
   char* msg = NULL;
@@ -709,9 +758,7 @@ sock_err_message (function_name)
    - the buffer is full (but this shouldn't happen)
    Otherwise, we just accumulate it.  */
 void
-send_to_emacs (s, data)
-     HSOCKET s;
-     char *data;
+send_to_emacs (HSOCKET s, char *data)
 {
   while (data)
     {
@@ -748,11 +795,9 @@ send_to_emacs (s, data)
    any initial -.  Change spaces to underscores, too, so that the
    return value never contains a space.
 
-   Does not change the string.  Outputs the result to STREAM.  */
+   Does not change the string.  Outputs the result to S.  */
 void
-quote_argument (s, str)
-     HSOCKET s;
-     char *str;
+quote_argument (HSOCKET s, char *str)
 {
   char *copy = (char *) xmalloc (strlen (str) * 2 + 1);
   char *p, *q;
@@ -792,8 +837,7 @@ quote_argument (s, str)
    modifying the string in place.   Returns STR. */
 
 char *
-unquote_argument (str)
-     char *str;
+unquote_argument (char *str)
 {
   char *p, *q;
 
@@ -824,8 +868,7 @@ unquote_argument (str)
 
 \f
 int
-file_name_absolute_p (filename)
-     const unsigned char *filename;
+file_name_absolute_p (const unsigned char *filename)
 {
   /* Sanity check, it shouldn't happen.  */
   if (! filename) return FALSE;
@@ -851,15 +894,15 @@ file_name_absolute_p (filename)
 
 #ifdef WINDOWSNT
 /* Wrapper to make WSACleanup a cdecl, as required by atexit.  */
-void
-__cdecl close_winsock ()
+void __cdecl
+close_winsock (void)
 {
   WSACleanup ();
 }
 
 /* Initialize the WinSock2 library.  */
 void
-initialize_sockets ()
+initialize_sockets (void)
 {
   WSADATA wsaData;
 
@@ -879,9 +922,7 @@ initialize_sockets ()
  * the Emacs server: host, port, pid and authentication string.
  */
 int
-get_server_config (server, authentication)
-     struct sockaddr_in *server;
-     char *authentication;
+get_server_config (struct sockaddr_in *server, char *authentication)
 {
   char dotted[32];
   char *port;
@@ -896,14 +937,16 @@ get_server_config (server, authentication)
 
       if (home)
         {
-          char *path = alloca (32 + strlen (home) + strlen (server_file));
+          char *path = alloca (strlen (home) + strlen (server_file)
+                              + EXTRA_SPACE);
           sprintf (path, "%s/.emacs.d/server/%s", home, server_file);
           config = fopen (path, "rb");
         }
 #ifdef WINDOWSNT
       if (!config && (home = egetenv ("APPDATA")))
         {
-          char *path = alloca (32 + strlen (home) + strlen (server_file));
+          char *path = alloca (strlen (home) + strlen (server_file)
+                              + EXTRA_SPACE);
           sprintf (path, "%s/.emacs.d/server/%s", home, server_file);
           config = fopen (path, "rb");
         }
@@ -944,7 +987,7 @@ get_server_config (server, authentication)
 }
 
 HSOCKET
-set_tcp_socket ()
+set_tcp_socket (void)
 {
   HSOCKET s;
   struct sockaddr_in server;
@@ -998,6 +1041,57 @@ strprefix (char *prefix, char *string)
   return !strncmp (prefix, string, strlen (prefix));
 }
 
+/* Get tty name and type.  If successful, return the type in TTY_TYPE
+   and the name in TTY_NAME, and return 1.  Otherwise, fail if NOABORT
+   is zero, or return 0 if NOABORT is non-zero.  */
+
+int
+find_tty (char **tty_type, char **tty_name, int noabort)
+{
+  char *type = egetenv ("TERM");
+  char *name = ttyname (fileno (stdout));
+
+  if (!name)
+    {
+      if (noabort)
+       return 0;
+      else
+       {
+         message (TRUE, "%s: could not get terminal name\n", progname);
+         fail ();
+       }
+    }
+
+  if (!type)
+    {
+      if (noabort)
+       return 0;
+      else
+       {
+         message (TRUE, "%s: please set the TERM variable to your terminal type\n",
+                  progname);
+         fail ();
+       }
+    }
+
+  if (strcmp (type, "eterm") == 0)
+    {
+      if (noabort)
+       return 0;
+      else
+       {
+         /* This causes nasty, MULTI_KBOARD-related input lockouts. */
+         message (TRUE, "%s: opening a frame in an Emacs term buffer"
+                  " is not supported\n", progname);
+         fail ();
+       }
+    }
+
+  *tty_name = name;
+  *tty_type = type;
+  return 1;
+}
+
 
 #if !defined (NO_SOCKETS_IN_FILE_SYSTEM)
 
@@ -1007,8 +1101,7 @@ strprefix (char *prefix, char *string)
    0 - success: none of the above */
 
 static int
-socket_status (socket_name)
-     char *socket_name;
+socket_status (char *socket_name)
 {
   struct stat statbfr;
 
@@ -1086,6 +1179,8 @@ handle_sigtstp (int signalnum)
 
   errno = old_errno;
 }
+
+
 /* Set up signal handlers before opening a frame on the current tty.  */
 
 void
@@ -1109,7 +1204,7 @@ init_signals (void)
 
 
 HSOCKET
-set_local_socket ()
+set_local_socket (void)
 {
   HSOCKET s;
   struct sockaddr_un server;
@@ -1131,9 +1226,12 @@ set_local_socket ()
     int default_sock = !socket_name;
     int saved_errno = 0;
     char *server_name = "server";
+    char *tmpdir;
 
-    if (socket_name && !index (socket_name, '/') && !index (socket_name, '\\'))
-      { /* socket_name is a file name component.  */
+    if (socket_name && !strchr (socket_name, '/')
+       && !strchr (socket_name, '\\'))
+      {
+       /* socket_name is a file name component.  */
        server_name = socket_name;
        socket_name = NULL;
        default_sock = 1;       /* Try both UIDs.  */
@@ -1141,9 +1239,13 @@ set_local_socket ()
 
     if (default_sock)
       {
-       socket_name = alloca (100 + strlen (server_name));
-       sprintf (socket_name, "/tmp/emacs%d/%s",
-                (int) geteuid (), server_name);
+       tmpdir = egetenv ("TMPDIR");
+       if (!tmpdir)
+         tmpdir = "/tmp";
+       socket_name = alloca (strlen (tmpdir) + strlen (server_name)
+                             + EXTRA_SPACE);
+       sprintf (socket_name, "%s/emacs%d/%s",
+                tmpdir, (int) geteuid (), server_name);
       }
 
     if (strlen (socket_name) < sizeof (server.sun_path))
@@ -1177,9 +1279,10 @@ set_local_socket ()
            if (pw && (pw->pw_uid != geteuid ()))
              {
                /* We're running under su, apparently. */
-               socket_name = alloca (100 + strlen (server_name));
-               sprintf (socket_name, "/tmp/emacs%d/%s",
-                        (int) pw->pw_uid, server_name);
+               socket_name = alloca (strlen (tmpdir) + strlen (server_name)
+                                     + EXTRA_SPACE);
+               sprintf (socket_name, "%s/emacs%d/%s",
+                        tmpdir, (int) pw->pw_uid, server_name);
 
                if (strlen (socket_name) < sizeof (server.sun_path))
                  strcpy (server.sun_path, socket_name);
@@ -1236,7 +1339,7 @@ To start the server in Emacs, type \"M-x server-start\".\n",
 #endif /* ! NO_SOCKETS_IN_FILE_SYSTEM */
 
 HSOCKET
-set_socket ()
+set_socket (int no_exit_if_error)
 {
   HSOCKET s;
 
@@ -1247,7 +1350,7 @@ set_socket ()
   if (socket_name)
     {
       s = set_local_socket ();
-      if ((s != INVALID_SOCKET) || alternate_editor)
+      if ((s != INVALID_SOCKET) || no_exit_if_error)
        return s;
       message (TRUE, "%s: error accessing socket \"%s\"\n",
               progname, socket_name);
@@ -1262,7 +1365,7 @@ set_socket ()
   if (server_file)
     {
       s = set_tcp_socket ();
-      if ((s != INVALID_SOCKET) || alternate_editor)
+      if ((s != INVALID_SOCKET) || no_exit_if_error)
        return s;
 
       message (TRUE, "%s: error accessing server file \"%s\"\n",
@@ -1280,7 +1383,7 @@ set_socket ()
   /* Implicit server file.  */
   server_file = "server";
   s = set_tcp_socket ();
-  if ((s != INVALID_SOCKET) || alternate_editor)
+  if ((s != INVALID_SOCKET) || no_exit_if_error)
     return s;
 
   /* No implicit or explicit socket, and no alternate editor.  */
@@ -1299,9 +1402,7 @@ FARPROC set_fg;  /* Pointer to AllowSetForegroundWindow.  */
 FARPROC get_wc;  /* Pointer to RealGetWindowClassA.  */
 
 BOOL CALLBACK
-w32_find_emacs_process (hWnd, lParam)
-     HWND hWnd;
-     LPARAM lParam;
+w32_find_emacs_process (HWND hWnd, LPARAM lParam)
 {
   DWORD pid;
   char class[6];
@@ -1329,43 +1430,103 @@ w32_find_emacs_process (hWnd, lParam)
  * process id = emacs_pid.  If found, allow it to grab the focus.
  */
 void
-w32_give_focus ()
+w32_give_focus (void)
 {
-  HMODULE hUser32;
+  HANDLE user32;
 
   /* It shouldn't happen when dealing with TCP sockets.  */
   if (!emacs_pid) return;
 
-  if (!(hUser32 = LoadLibrary ("user32.dll"))) return;
+  user32 = GetModuleHandle ("user32.dll");
+
+  if (!user32)
+    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")))
+  if ((set_fg = GetProcAddress (user32, "AllowSetForegroundWindow"))
+      && (get_wc = GetProcAddress (user32, "RealGetWindowClassA")))
     EnumWindows (w32_find_emacs_process, (LPARAM) 0);
-
-  FreeLibrary (hUser32);
 }
 #endif
 
+/* Start the emacs daemon and try to connect to it.  */
+
+void
+start_daemon_and_retry_set_socket (void)
+{
+#ifndef WINDOWSNT
+  pid_t dpid;
+  int status;
+
+  dpid = fork ();
+
+  if (dpid > 0)
+    {
+      pid_t w;
+      w = waitpid (dpid, &status, WUNTRACED | WCONTINUED);
+
+      if ((w == -1) || !WIFEXITED (status) || WEXITSTATUS(status))
+       {
+         message (TRUE, "Error: Could not start the Emacs daemon\n");
+         exit (EXIT_FAILURE);
+       }
+
+      /* Try connecting, the daemon should have started by now.  */
+      message (TRUE, "Emacs daemon should have started, trying to connect again\n");
+      if ((emacs_socket = set_socket (1)) == INVALID_SOCKET)
+       {
+         message (TRUE, "Error: Cannot connect even after starting the Emacs daemon\n");
+         exit (EXIT_FAILURE);
+       }
+    }
+  else if (dpid < 0)
+    {
+      fprintf (stderr, "Error: Cannot fork!\n");
+      exit (1);
+    }
+  else
+    {
+      char *d_argv[] = {"emacs", "--daemon", 0 };
+      if (socket_name != NULL)
+       {
+         /* Pass  --daemon=socket_name as argument.  */
+         char *deq = "--daemon=";
+         char *daemon_arg = alloca (strlen (deq)
+                                    + strlen (socket_name) + 1);
+         strcpy (daemon_arg, deq);
+         strcat (daemon_arg, socket_name);
+         d_argv[1] = daemon_arg;
+       }
+      execvp ("emacs", d_argv);
+      message (TRUE, "%s: error starting emacs daemon\n", progname);
+    }
+#endif /* WINDOWSNT */
+}
+
 int
-main (argc, argv)
-     int argc;
-     char **argv;
+main (int argc, char **argv)
 {
   int i, rl, needlf = 0;
   char *cwd, *str;
   char string[BUFSIZ+1];
+  int null_socket_name, null_server_file, start_daemon_if_needed;
 
   main_argv = argv;
   progname = argv[0];
 
+#ifdef WINDOWSNT
+  /* On Windows 7 and later, we need to explicitly associate emacsclient
+     with emacs so the UI behaves sensibly.  */
+  w32_set_user_model_id ();
+#endif
+
   /* Process options.  */
   decode_options (argc, argv);
 
-  if ((argc - optind < 1) && !eval && !tty && !window_system)
+  if ((argc - optind < 1) && !eval && current_frame)
     {
       message (TRUE, "%s: file name or argument required\n"
               "Try `%s --help' for more information\n",
@@ -1373,9 +1534,34 @@ main (argc, argv)
       exit (EXIT_FAILURE);
     }
 
-  if ((emacs_socket = set_socket ()) == INVALID_SOCKET)
-    fail ();
+  /* If alternate_editor is the empty string, start the emacs daemon
+     in case of failure to connect.  */
+  start_daemon_if_needed = (alternate_editor
+                           && (alternate_editor[0] == '\0'));
+  if (start_daemon_if_needed)
+    {
+      /* set_socket changes the values for socket_name and
+        server_file, we need to reset them, if they were NULL before
+        for the second call to set_socket.  */
+      null_socket_name = (socket_name == NULL);
+      null_server_file = (server_file == NULL);
+    }
 
+  if ((emacs_socket = set_socket (alternate_editor
+                                 || start_daemon_if_needed)) == INVALID_SOCKET)
+    if (start_daemon_if_needed)
+      {
+       /* Reset socket_name and server_file if they were NULL
+          before the set_socket call.  */
+       if (null_socket_name)
+         socket_name = NULL;
+       if (null_server_file)
+         server_file = NULL;
+
+       start_daemon_and_retry_set_socket ();
+      }
+    else
+      fail ();
 
   cwd = get_current_dir_name ();
   if (cwd == 0)
@@ -1390,7 +1576,7 @@ main (argc, argv)
   w32_give_focus ();
 #endif
 
-  /* Send over our environment. */
+  /* Send over our environment and current directory. */
   if (!current_frame)
     {
       extern char **environ;
@@ -1404,15 +1590,10 @@ main (argc, argv)
           send_to_emacs (emacs_socket, " ");
         }
     }
-
-  /* Send over our current directory. */
-  if (!current_frame)
-    {
-      send_to_emacs (emacs_socket, "-dir ");
-      quote_argument (emacs_socket, cwd);
-      send_to_emacs (emacs_socket, "/");
-      send_to_emacs (emacs_socket, " ");
-    }
+  send_to_emacs (emacs_socket, "-dir ");
+  quote_argument (emacs_socket, cwd);
+  send_to_emacs (emacs_socket, "/");
+  send_to_emacs (emacs_socket, " ");
 
  retry:
   if (nowait)
@@ -1428,53 +1609,40 @@ main (argc, argv)
       send_to_emacs (emacs_socket, " ");
     }
 
-  if (tty)
+  if (parent_id)
     {
-      char *type = egetenv ("TERM");
-      char *tty_name = NULL;
-#ifndef WINDOWSNT
-      tty_name = ttyname (fileno (stdin));
-#endif
-
-      if (! tty_name)
-        {
-          message (TRUE, "%s: could not get terminal name\n", progname);
-          fail ();
-        }
+      send_to_emacs (emacs_socket, "-parent-id ");
+      quote_argument (emacs_socket, parent_id);
+      send_to_emacs (emacs_socket, " ");
+    }
 
-      if (! type)
-        {
-          message (TRUE, "%s: please set the TERM variable to your terminal type\n",
-                   progname);
-          fail ();
-        }
+  /* If using the current frame, send tty information to Emacs anyway.
+     In daemon mode, Emacs may need to occupy this tty if no other
+     frame is available.  */
+  if (tty || (current_frame && !eval))
+    {
+      char *tty_type, *tty_name;
 
-      if (! strcmp (type, "eterm"))
-        {
-          /* This causes nasty, MULTI_KBOARD-related input lockouts. */
-          message (TRUE, "%s: opening a frame in an Emacs term buffer"
-                   " is not supported\n", progname);
-          fail ();
-        }
+      if (find_tty (&tty_type, &tty_name, !tty))
+       {
 #if !defined (NO_SOCKETS_IN_FILE_SYSTEM)
-      init_signals ();
+         init_signals ();
 #endif
-
-      send_to_emacs (emacs_socket, "-tty ");
-      quote_argument (emacs_socket, tty_name);
-      send_to_emacs (emacs_socket, " ");
-      quote_argument (emacs_socket, type);
-      send_to_emacs (emacs_socket, " ");
+         send_to_emacs (emacs_socket, "-tty ");
+         quote_argument (emacs_socket, tty_name);
+         send_to_emacs (emacs_socket, " ");
+         quote_argument (emacs_socket, tty_type);
+         send_to_emacs (emacs_socket, " ");
+       }
     }
 
-  if (window_system)
+  if (!current_frame && !tty)
     send_to_emacs (emacs_socket, "-window-system ");
 
   if ((argc - optind > 0))
     {
       for (i = optind; i < argc; i++)
        {
-          int relative = 0;
 
          if (eval)
             {
@@ -1496,19 +1664,16 @@ main (argc, argv)
                   send_to_emacs (emacs_socket, " ");
                   continue;
                 }
-              else
-                relative = 1;
             }
-         else if (! file_name_absolute_p (argv[i]))
-#ifndef WINDOWSNT
-           relative = 1;
-#else
-           /* Call GetFullPathName so filenames of the form X:Y, where X is
-              a valid drive designator, are interpreted as drive:path, not
-              file:stream, and treated as absolute.
-              The user can still pass a file:stream if desired (for example,
-              .\X:Y), but it is not very useful, as Emacs currently does a
-              very bad job of dealing wih NTFS streams. */
+#ifdef WINDOWSNT
+         else if (! file_name_absolute_p (argv[i])
+                  && (isalpha (argv[i][0]) && argv[i][1] == ':'))
+           /* Windows can have a different default directory for each
+              drive, so the cwd passed via "-dir" is not sufficient
+              to account for that.
+              If the user uses <drive>:<relpath>, we hence need to be
+              careful to expand <relpath> with the default directory
+              corresponding to <drive>.  */
            {
              char *filename = (char *) xmalloc (MAX_PATH);
              DWORD size;
@@ -1517,37 +1682,24 @@ main (argc, argv)
              if (size > 0 && size < MAX_PATH)
                argv[i] = filename;
              else
-               {
-                 relative = 1;
-                 free (filename);
-               }
+               free (filename);
            }
 #endif
 
           send_to_emacs (emacs_socket, "-file ");
-          if (relative)
-            {
-              quote_argument (emacs_socket, cwd);
-              send_to_emacs (emacs_socket, "/");
-            }
           quote_argument (emacs_socket, argv[i]);
           send_to_emacs (emacs_socket, " ");
         }
     }
-  else
+  else if (eval)
     {
-      if (!tty && !window_system)
-        {
-          while ((str = fgets (string, BUFSIZ, stdin)))
-            {
-              if (eval)
-                send_to_emacs (emacs_socket, "-eval ");
-              else
-                send_to_emacs (emacs_socket, "-file ");
-              quote_argument (emacs_socket, str);
-            }
-          send_to_emacs (emacs_socket, " ");
-        }
+      /* Read expressions interactively.  */
+      while ((str = fgets (string, BUFSIZ, stdin)))
+       {
+         send_to_emacs (emacs_socket, "-eval ");
+         quote_argument (emacs_socket, str);
+       }
+      send_to_emacs (emacs_socket, " ");
     }
 
   send_to_emacs (emacs_socket, "\n");
@@ -1580,7 +1732,6 @@ main (argc, argv)
         {
           /* -window-system-unsupported: Emacs was compiled without X
               support.  Try again on the terminal. */
-          window_system = 0;
           nowait = 0;
           tty = 1;
           goto retry;