remove sigio blocking
[bpt/emacs.git] / lib-src / emacsclient.c
index 07300db..ddc1b6d 100644 (file)
@@ -1,6 +1,6 @@
 /* Client process that communicates with GNU Emacs acting as server.
 
-Copyright (C) 1986-1987, 1994, 1999-2012 Free Software Foundation, Inc.
+Copyright (C) 1986-1987, 1994, 1999-2014 Free Software Foundation, Inc.
 
 This file is part of GNU Emacs.
 
@@ -44,6 +44,10 @@ char *w32_getenv (char *);
 
 #else /* !WINDOWSNT */
 
+# ifdef HAVE_NTGUI
+# include <windows.h>
+# endif /* HAVE_NTGUI */
+
 # include "syswait.h"
 
 # ifdef HAVE_INET_SOCKETS
@@ -61,10 +65,6 @@ char *w32_getenv (char *);
 # define CLOSE_SOCKET close
 # define INITIALIZE()
 
-# ifndef WCONTINUED
-#  define WCONTINUED 8
-# endif
-
 #define egetenv(VAR) getenv(VAR)
 
 #endif /* !WINDOWSNT */
@@ -82,13 +82,6 @@ char *w32_getenv (char *);
 #include <signal.h>
 #include <errno.h>
 
-
-\f
-char *getenv (const char *), *getwd (char *);
-#ifdef HAVE_GETCWD
-char *(getcwd) (char *, size_t);
-#endif
-
 #ifndef VERSION
 #define VERSION "unspecified"
 #endif
@@ -102,14 +95,6 @@ char *(getcwd) (char *, size_t);
 #define EXIT_FAILURE 1
 #endif
 
-#ifndef FALSE
-#define FALSE 0
-#endif
-
-#ifndef TRUE
-#define TRUE 1
-#endif
-
 /* Additional space when allocating buffers for filenames, etc.  */
 #define EXTRA_SPACE 100
 
@@ -140,6 +125,9 @@ int current_frame = 1;
 /* The display on which Emacs should work.  --display.  */
 const char *display = NULL;
 
+/* The alternate display we should try if Emacs does not support display.  */
+const char *alt_display = NULL;
+
 /* The parent window ID, if we are opening a frame via XEmbed.  */
 char *parent_id = NULL;
 
@@ -182,9 +170,7 @@ struct option longopts[] =
   { "socket-name",     required_argument, NULL, 's' },
 #endif
   { "server-file",     required_argument, NULL, 'f' },
-#ifndef WINDOWSNT
   { "display", required_argument, NULL, 'd' },
-#endif
   { "parent-id", required_argument, NULL, 'p' },
   { 0, 0, 0, 0 }
 };
@@ -218,7 +204,7 @@ get_current_dir_name (void)
   char *buf;
   const char *pwd;
   struct stat dotstat, pwdstat;
-  /* If PWD is accurate, use it instead of calling getwd.  PWD is
+  /* If PWD is accurate, use it instead of calling getcwd.  PWD is
      sometimes a nicer name, and using it may avoid a fatal error if a
      parent directory is searchable but not readable.  */
     if ((pwd = egetenv ("PWD")) != 0
@@ -235,7 +221,6 @@ get_current_dir_name (void)
       buf = (char *) xmalloc (strlen (pwd) + 1);
       strcpy (buf, pwd);
     }
-#ifdef HAVE_GETCWD
   else
     {
       size_t buf_size = 1024;
@@ -262,20 +247,6 @@ get_current_dir_name (void)
            }
         }
     }
-#else
-  else
-    {
-      /* We need MAXPATHLEN here.  */
-      buf = (char *) xmalloc (MAXPATHLEN + 1);
-      if (getwd (buf) == NULL)
-        {
-          int tmp_errno = errno;
-          free (buf);
-          errno = tmp_errno;
-          return NULL;
-        }
-    }
-#endif
   return buf;
 }
 #endif
@@ -385,32 +356,6 @@ w32_getenv (char *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 (void)
@@ -472,9 +417,9 @@ ttyname (int fd)
 
 /* Display a normal or error message.
    On Windows, use a message box if compiled as a Windows app.  */
-static void message (int, const char *, ...) ATTRIBUTE_FORMAT_PRINTF (2, 3);
+static void message (bool, const char *, ...) ATTRIBUTE_FORMAT_PRINTF (2, 3);
 static void
-message (int is_error, const char *format, ...)
+message (bool is_error, const char *format, ...)
 {
   va_list args;
 
@@ -567,7 +512,7 @@ decode_options (int argc, char **argv)
          break;
 
        case 'V':
-         message (FALSE, "emacsclient %s\n", VERSION);
+         message (false, "emacsclient %s\n", VERSION);
          exit (EXIT_SUCCESS);
          break;
 
@@ -594,7 +539,7 @@ decode_options (int argc, char **argv)
           break;
 
        default:
-         message (TRUE, "Try `%s --help' for more information\n", progname);
+         message (true, "Try `%s --help' for more information\n", progname);
          exit (EXIT_FAILURE);
          break;
        }
@@ -605,16 +550,29 @@ decode_options (int argc, char **argv)
      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.  */
+     they want Emacs to connect to their current display.
+
+     Some window systems have a notion of default display not
+     reflected in the DISPLAY variable.  If the user didn't give us an
+     explicit display, try this platform-specific after trying the
+     display in DISPLAY (if any).  */
   if (!current_frame && !tty && !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";
+      /* Set these here so we use a default_display only when the user
+         didn't give us an explicit display.  */
+#if defined (NS_IMPL_COCOA)
+      alt_display = "ns";
+#elif defined (HAVE_NTGUI)
+      alt_display = "w32";
 #endif
+
+      display = egetenv ("DISPLAY");
+    }
+
+  if (!display)
+    {
+      display = alt_display;
+      alt_display = NULL;
     }
 
   /* A null-string display is invalid.  */
@@ -640,7 +598,7 @@ decode_options (int argc, char **argv)
 
   if (alternate_editor && alternate_editor[0] == '\0')
     {
-      message (TRUE, "--alternate-editor argument or ALTERNATE_EDITOR variable cannot be\n\
+      message (true, "--alternate-editor argument or ALTERNATE_EDITOR variable cannot be\n\
 an empty string");
       exit (EXIT_FAILURE);
     }
@@ -655,8 +613,8 @@ print_help_and_exit (void)
      message aligns properly both in a tty and in a Windows message box.
      Please try to preserve them; otherwise the output is very hard to read
      when using emacsclientw.  */
-  message (FALSE,
-          "Usage: %s [OPTIONS] FILE...\n\
+  message (false,
+          "Usage: %s [OPTIONS] FILE...\n%s%s%s", progname, "\
 Tell the Emacs server to visit the specified files.\n\
 Every FILE can be either just a FILENAME or [+LINE[:COLUMN]] FILENAME.\n\
 \n\
@@ -666,6 +624,7 @@ The following OPTIONS are accepted:\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\
+", "\
 -F ALIST, --frame-parameters=ALIST\n\
                        Set the parameters of a new frame\n\
 -e, --eval             Evaluate the FILE arguments as ELisp expressions\n\
@@ -673,6 +632,7 @@ The following OPTIONS are accepted:\n\
 -q, --quiet            Don't display messages on success\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, --socket-name=SOCKET\n\
@@ -687,7 +647,7 @@ The following OPTIONS are accepted:\n\
                        mode and try connecting again\n"
 #endif /* not WINDOWSNT */
 "\n\
-Report bugs with M-x report-emacs-bug.\n", progname);
+Report bugs with M-x report-emacs-bug.\n");
   exit (EXIT_SUCCESS);
 }
 
@@ -703,7 +663,7 @@ fail (void)
       int i = optind - 1;
 
       execvp (alternate_editor, main_argv + i);
-      message (TRUE, "%s: error executing alternate editor \"%s\"\n",
+      message (true, "%s: error executing alternate editor \"%s\"\n",
               progname, alternate_editor);
     }
   exit (EXIT_FAILURE);
@@ -717,7 +677,7 @@ main (int argc, char **argv)
 {
   main_argv = argv;
   progname = argv[0];
-  message (TRUE, "%s: Sorry, the Emacs server is supported only\n"
+  message (true, "%s: Sorry, the Emacs server is supported only\n"
           "on systems with Berkeley sockets.\n",
           argv[0]);
   fail ();
@@ -748,11 +708,11 @@ sock_err_message (const char *function_name)
                  | FORMAT_MESSAGE_ARGUMENT_ARRAY,
                  NULL, WSAGetLastError (), 0, (LPTSTR)&msg, 0, NULL);
 
-  message (TRUE, "%s: %s: %s\n", progname, function_name, msg);
+  message (true, "%s: %s: %s\n", progname, function_name, msg);
 
   LocalFree (msg);
 #else
-  message (TRUE, "%s: %s: %s\n", progname, function_name, strerror (errno));
+  message (true, "%s: %s: %s\n", progname, function_name, strerror (errno));
 #endif
 }
 
@@ -783,7 +743,7 @@ send_to_emacs (HSOCKET s, const char *data)
          int sent = send (s, send_buffer, sblen, 0);
          if (sent < 0)
            {
-             message (TRUE, "%s: failed to send %d bytes to socket: %s\n",
+             message (true, "%s: failed to send %d bytes to socket: %s\n",
                       progname, sblen, strerror (errno));
              fail ();
            }
@@ -878,25 +838,25 @@ static int
 file_name_absolute_p (const char *filename)
 {
   /* Sanity check, it shouldn't happen.  */
-  if (! filename) return FALSE;
+  if (! filename) return false;
 
   /* /xxx is always an absolute path.  */
-  if (filename[0] == '/') return TRUE;
+  if (filename[0] == '/') return true;
 
   /* Empty filenames (which shouldn't happen) are relative.  */
-  if (filename[0] == '\0') return FALSE;
+  if (filename[0] == '\0') return false;
 
 #ifdef WINDOWSNT
   /* X:\xxx is always absolute.  */
   if (isalpha ((unsigned char) filename[0])
       && filename[1] == ':' && (filename[2] == '\\' || filename[2] == '/'))
-    return TRUE;
+    return true;
 
   /* Both \xxx and \\xxx\yyy are absolute.  */
-  if (filename[0] == '\\') return TRUE;
+  if (filename[0] == '\\') return true;
 #endif
 
-  return FALSE;
+  return false;
 }
 
 #ifdef WINDOWSNT
@@ -915,7 +875,7 @@ initialize_sockets (void)
 
   if (WSAStartup (MAKEWORD (2, 0), &wsaData))
     {
-      message (TRUE, "%s: error initializing WinSock2\n", progname);
+      message (true, "%s: error initializing WinSock2\n", progname);
       exit (EXIT_FAILURE);
     }
 
@@ -966,14 +926,14 @@ get_server_config (const char *config_file, struct sockaddr_in *server,
     }
 
   if (! config)
-    return FALSE;
+    return false;
 
   if (fgets (dotted, sizeof dotted, config)
       && (port = strchr (dotted, ':')))
     *port++ = '\0';
   else
     {
-      message (TRUE, "%s: invalid configuration info\n", progname);
+      message (true, "%s: invalid configuration info\n", progname);
       exit (EXIT_FAILURE);
     }
 
@@ -983,13 +943,13 @@ get_server_config (const char *config_file, struct sockaddr_in *server,
 
   if (! fread (authentication, AUTH_KEY_LENGTH, 1, config))
     {
-      message (TRUE, "%s: cannot read authentication info\n", progname);
+      message (true, "%s: cannot read authentication info\n", progname);
       exit (EXIT_FAILURE);
     }
 
   fclose (config);
 
-  return TRUE;
+  return true;
 }
 
 static HSOCKET
@@ -1004,7 +964,7 @@ set_tcp_socket (const char *local_server_file)
     return INVALID_SOCKET;
 
   if (server.sin_addr.s_addr != inet_addr ("127.0.0.1") && !quiet)
-    message (FALSE, "%s: connected to remote socket at %s\n",
+    message (false, "%s: connected to remote socket at %s\n",
              progname, inet_ntoa (server.sin_addr));
 
   /* Open up an AF_INET socket.  */
@@ -1057,7 +1017,7 @@ find_tty (const char **tty_type, const char **tty_name, int noabort)
        return 0;
       else
        {
-         message (TRUE, "%s: could not get terminal name\n", progname);
+         message (true, "%s: could not get terminal name\n", progname);
          fail ();
        }
     }
@@ -1068,7 +1028,7 @@ find_tty (const char **tty_type, const char **tty_name, int noabort)
        return 0;
       else
        {
-         message (TRUE, "%s: please set the TERM variable to your terminal type\n",
+         message (true, "%s: please set the TERM variable to your terminal type\n",
                   progname);
          fail ();
        }
@@ -1081,7 +1041,7 @@ find_tty (const char **tty_type, const char **tty_name, int noabort)
       else
        {
          /* This causes nasty, MULTI_KBOARD-related input lockouts. */
-         message (TRUE, "%s: opening a frame in an Emacs term buffer"
+         message (true, "%s: opening a frame in an Emacs term buffer"
                   " is not supported\n", progname);
          fail ();
        }
@@ -1137,16 +1097,18 @@ static void
 handle_sigcont (int signalnum)
 {
   int old_errno = errno;
+  pid_t pgrp = getpgrp ();
+  pid_t tcpgrp = tcgetpgrp (1);
 
-  if (tcgetpgrp (1) == getpgrp ())
+  if (tcpgrp == pgrp)
     {
-      /* We are in the foreground. */
+      /* We are in the foreground.  */
       send_to_emacs (emacs_socket, "-resume \n");
     }
-  else
+  else if (0 <= tcpgrp && tty)
     {
-      /* We are in the background; cancel the continue. */
-      kill (getpid (), SIGSTOP);
+      /* We are in the background; cancel the continue.  */
+      kill (-pgrp, SIGTTIN);
     }
 
   signal (signalnum, handle_sigcont);
@@ -1173,7 +1135,7 @@ handle_sigtstp (int signalnum)
   sigprocmask (SIG_BLOCK, NULL, &set);
   sigdelset (&set, signalnum);
   signal (signalnum, SIG_DFL);
-  kill (getpid (), signalnum);
+  raise (signalnum);
   sigprocmask (SIG_SETMASK, &set, NULL); /* Let's the above signal through. */
   signal (signalnum, handle_sigtstp);
 
@@ -1212,7 +1174,7 @@ set_local_socket (const char *local_socket_name)
   /* Open up an AF_UNIX socket in this person's home directory.  */
   if ((s = socket (AF_UNIX, SOCK_STREAM, 0)) < 0)
     {
-      message (TRUE, "%s: socket: %s\n", progname, strerror (errno));
+      message (true, "%s: socket: %s\n", progname, strerror (errno));
       return INVALID_SOCKET;
     }
 
@@ -1263,7 +1225,7 @@ set_local_socket (const char *local_socket_name)
       strcpy (server.sun_path, local_socket_name);
     else
       {
-        message (TRUE, "%s: socket-name %s too long\n",
+        message (true, "%s: socket-name %s too long\n",
                  progname, local_socket_name);
         fail ();
       }
@@ -1302,7 +1264,7 @@ set_local_socket (const char *local_socket_name)
                  strcpy (server.sun_path, user_socket_name);
                else
                  {
-                   message (TRUE, "%s: socket-name %s too long\n",
+                   message (true, "%s: socket-name %s too long\n",
                             progname, user_socket_name);
                    exit (EXIT_FAILURE);
                  }
@@ -1326,7 +1288,7 @@ set_local_socket (const char *local_socket_name)
           we are root. */
        if (0 != geteuid ())
          {
-           message (TRUE, "%s: Invalid socket owner\n", progname);
+           message (true, "%s: Invalid socket owner\n", progname);
            return INVALID_SOCKET;
          }
        break;
@@ -1334,12 +1296,12 @@ set_local_socket (const char *local_socket_name)
       case 2:
        /* `stat' failed */
        if (saved_errno == ENOENT)
-         message (TRUE,
+         message (true,
                   "%s: can't find socket; have you started the server?\n\
 To start the server in Emacs, type \"M-x server-start\".\n",
                   progname);
        else
-         message (TRUE, "%s: can't stat %s: %s\n",
+         message (true, "%s: can't stat %s: %s\n",
                   progname, server.sun_path, strerror (saved_errno));
        return INVALID_SOCKET;
       }
@@ -1348,7 +1310,7 @@ To start the server in Emacs, type \"M-x server-start\".\n",
   if (connect (s, (struct sockaddr *) &server, strlen (server.sun_path) + 2)
       < 0)
     {
-      message (TRUE, "%s: connect: %s\n", progname, strerror (errno));
+      message (true, "%s: connect: %s\n", progname, strerror (errno));
       return INVALID_SOCKET;
     }
 
@@ -1371,7 +1333,7 @@ set_socket (int no_exit_if_error)
       s = set_local_socket (socket_name);
       if ((s != INVALID_SOCKET) || no_exit_if_error)
        return s;
-      message (TRUE, "%s: error accessing socket \"%s\"\n",
+      message (true, "%s: error accessing socket \"%s\"\n",
               progname, socket_name);
       exit (EXIT_FAILURE);
     }
@@ -1387,7 +1349,7 @@ set_socket (int no_exit_if_error)
       if ((s != INVALID_SOCKET) || no_exit_if_error)
        return s;
 
-      message (TRUE, "%s: error accessing server file \"%s\"\n",
+      message (true, "%s: error accessing server file \"%s\"\n",
               progname, local_server_file);
       exit (EXIT_FAILURE);
     }
@@ -1405,7 +1367,7 @@ set_socket (int no_exit_if_error)
     return s;
 
   /* No implicit or explicit socket, and no alternate editor.  */
-  message (TRUE, "%s: No socket or alternate editor.  Please use:\n\n"
+  message (true, "%s: No socket or alternate editor.  Please use:\n\n"
 #ifndef NO_SOCKETS_IN_FILE_SYSTEM
 "\t--socket-name\n"
 #endif
@@ -1415,10 +1377,37 @@ set_socket (int no_exit_if_error)
   exit (EXIT_FAILURE);
 }
 
-#ifdef WINDOWSNT
+#ifdef HAVE_NTGUI
 FARPROC set_fg;  /* Pointer to AllowSetForegroundWindow.  */
 FARPROC get_wc;  /* Pointer to RealGetWindowClassA.  */
 
+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);
+    }
+}
+
 BOOL CALLBACK
 w32_find_emacs_process (HWND hWnd, LPARAM lParam)
 {
@@ -1467,7 +1456,7 @@ w32_give_focus (void)
       && (get_wc = GetProcAddress (user32, "RealGetWindowClassA")))
     EnumWindows (w32_find_emacs_process, (LPARAM) 0);
 }
-#endif
+#endif /* HAVE_NTGUI */
 
 /* Start the emacs daemon and try to connect to it.  */
 
@@ -1487,15 +1476,15 @@ start_daemon_and_retry_set_socket (void)
 
       if ((w == -1) || !WIFEXITED (status) || WEXITSTATUS (status))
        {
-         message (TRUE, "Error: Could not start the Emacs daemon\n");
+         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");
+      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");
+         message (true, "Error: Cannot connect even after starting the Emacs daemon\n");
          exit (EXIT_FAILURE);
        }
     }
@@ -1508,7 +1497,10 @@ start_daemon_and_retry_set_socket (void)
     {
       char emacs[] = "emacs";
       char daemon_option[] = "--daemon";
-      char *d_argv[] = {emacs, daemon_option, 0 };
+      char *d_argv[3];
+      d_argv[0] = emacs;
+      d_argv[1] = daemon_option;
+      d_argv[2] = 0;
       if (socket_name != NULL)
        {
          /* Pass  --daemon=socket_name as argument.  */
@@ -1520,7 +1512,7 @@ start_daemon_and_retry_set_socket (void)
          d_argv[1] = daemon_arg;
        }
       execvp ("emacs", d_argv);
-      message (TRUE, "%s: error starting emacs daemon\n", progname);
+      message (true, "%s: error starting emacs daemon\n", progname);
     }
 #endif /* WINDOWSNT */
 }
@@ -1537,23 +1529,35 @@ main (int argc, char **argv)
   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.  */
+#ifdef HAVE_NTGUI
+  /* On Windows 7 and later, we need to explicitly associate
+     emacsclient with emacs so the UI behaves sensibly.  This
+     association does no harm if we're not actually connecting to an
+     Emacs using a window display.  */
   w32_set_user_model_id ();
-#endif
+#endif /* HAVE_NTGUI */
 
   /* Process options.  */
   decode_options (argc, argv);
 
   if ((argc - optind < 1) && !eval && current_frame)
     {
-      message (TRUE, "%s: file name or argument required\n"
+      message (true, "%s: file name or argument required\n"
               "Try `%s --help' for more information\n",
               progname, progname);
       exit (EXIT_FAILURE);
     }
 
+#ifndef WINDOWSNT
+  if (tty)
+    {
+      pid_t pgrp = getpgrp ();
+      pid_t tcpgrp = tcgetpgrp (1);
+      if (0 <= tcpgrp && tcpgrp != pgrp)
+       kill (-pgrp, SIGTTIN);
+    }
+#endif /* !WINDOWSNT */
+
   /* If alternate_editor is the empty string, start the emacs daemon
      in case of failure to connect.  */
   start_daemon_if_needed = (alternate_editor
@@ -1571,15 +1575,15 @@ main (int argc, char **argv)
   cwd = get_current_dir_name ();
   if (cwd == 0)
     {
-      /* getwd puts message in STRING if it fails.  */
-      message (TRUE, "%s: %s\n", progname,
+      message (true, "%s: %s\n", progname,
               "Cannot get current working directory");
       fail ();
     }
 
-#ifdef WINDOWSNT
+#ifdef HAVE_NTGUI
+  if (display && !strcmp (display, "w32"))
   w32_give_focus ();
-#endif
+#endif /* HAVE_NTGUI */
 
   /* Send over our environment and current directory. */
   if (!current_frame)
@@ -1721,7 +1725,8 @@ main (int argc, char **argv)
       needlf = 2;
     }
   fflush (stdout);
-  fsync (1);
+  while (fdatasync (1) != 0 && errno == EINTR)
+    continue;
 
   /* Now, wait for an answer and print any messages.  */
   while (exit_status == EXIT_SUCCESS)
@@ -1748,46 +1753,56 @@ main (int argc, char **argv)
          if (end_p != NULL)
            *end_p++ = '\0';
 
-         if (strprefix ("-emacs-pid ", p))
-           {
-             /* -emacs-pid PID: The process id of the Emacs process. */
-             emacs_pid = strtol (p + strlen ("-emacs-pid"), NULL, 10);
-           }
-         else if (strprefix ("-window-system-unsupported ", p))
-           {
-             /* -window-system-unsupported: Emacs was compiled without X
-                support.  Try again on the terminal. */
-             nowait = 0;
-             tty = 1;
-             goto retry;
-           }
-         else if (strprefix ("-print ", p))
-           {
-             /* -print STRING: Print STRING on the terminal. */
-             str = unquote_argument (p + strlen ("-print "));
-             if (needlf)
-               printf ("\n");
-             printf ("%s", str);
-             needlf = str[0] == '\0' ? needlf : str[strlen (str) - 1] != '\n';
-           }
-         else if (strprefix ("-print-nonl ", p))
-           {
-             /* -print-nonl STRING: Print STRING on the terminal.
-                Used to continue a preceding -print command.  */
-             str = unquote_argument (p + strlen ("-print-nonl "));
-             printf ("%s", str);
-             needlf = str[0] == '\0' ? needlf : str[strlen (str) - 1] != '\n';
-           }
-         else if (strprefix ("-error ", p))
-           {
-             /* -error DESCRIPTION: Signal an error on the terminal. */
-             str = unquote_argument (p + strlen ("-error "));
-             if (needlf)
-               printf ("\n");
-             fprintf (stderr, "*ERROR*: %s", str);
-             needlf = str[0] == '\0' ? needlf : str[strlen (str) - 1] != '\n';
-             exit_status = EXIT_FAILURE;
-           }
+          if (strprefix ("-emacs-pid ", p))
+            {
+              /* -emacs-pid PID: The process id of the Emacs process. */
+              emacs_pid = strtol (p + strlen ("-emacs-pid"), NULL, 10);
+            }
+          else if (strprefix ("-window-system-unsupported ", p))
+            {
+              /* -window-system-unsupported: Emacs was compiled without support
+                 for whatever window system we tried.  Try the alternate
+                 display, or, failing that, try the terminal.  */
+              if (alt_display)
+                {
+                  display = alt_display;
+                  alt_display = NULL;
+                }
+              else
+                {
+                  nowait = 0;
+                  tty = 1;
+                }
+
+              goto retry;
+            }
+          else if (strprefix ("-print ", p))
+            {
+              /* -print STRING: Print STRING on the terminal. */
+              str = unquote_argument (p + strlen ("-print "));
+              if (needlf)
+                printf ("\n");
+              printf ("%s", str);
+              needlf = str[0] == '\0' ? needlf : str[strlen (str) - 1] != '\n';
+            }
+          else if (strprefix ("-print-nonl ", p))
+            {
+              /* -print-nonl STRING: Print STRING on the terminal.
+                 Used to continue a preceding -print command.  */
+              str = unquote_argument (p + strlen ("-print-nonl "));
+              printf ("%s", str);
+              needlf = str[0] == '\0' ? needlf : str[strlen (str) - 1] != '\n';
+            }
+          else if (strprefix ("-error ", p))
+            {
+              /* -error DESCRIPTION: Signal an error on the terminal. */
+              str = unquote_argument (p + strlen ("-error "));
+              if (needlf)
+                printf ("\n");
+              fprintf (stderr, "*ERROR*: %s", str);
+              needlf = str[0] == '\0' ? needlf : str[strlen (str) - 1] != '\n';
+              exit_status = EXIT_FAILURE;
+            }
 #ifdef SIGSTOP
          else if (strprefix ("-suspend ", p))
            {
@@ -1812,7 +1827,8 @@ main (int argc, char **argv)
   if (needlf)
     printf ("\n");
   fflush (stdout);
-  fsync (1);
+  while (fdatasync (1) != 0 && errno == EINTR)
+    continue;
 
   if (rl < 0)
     exit_status = EXIT_FAILURE;