Support for opening X frames from a tty session.
[bpt/emacs.git] / src / sysdep.c
index 81299b3..d57fe14 100644 (file)
@@ -45,6 +45,8 @@ extern void srandom P_ ((unsigned int));
 #endif
 #endif
 
+#include "sysselect.h"
+
 #include "blockinput.h"
 #undef NULL
 
@@ -250,8 +252,8 @@ int emacs_ospeed;
 void croak P_ ((char *));
 
 #ifdef AIXHFT
-void hft_init P_ ((struct tty_output *));
-void hft_reset P_ ((struct tty_output *));
+void hft_init P_ ((struct tty_display_info *));
+void hft_reset P_ ((struct tty_display_info *));
 #endif
 
 /* Temporary used by `sigblock' when defined in terms of signprocmask.  */
@@ -270,11 +272,6 @@ discard_tty_input ()
   if (noninteractive)
     return;
 
-  /* Discarding input is not safe when the input could contain
-     replies from the X server.  So don't do it.  */
-  if (read_socket_hook)
-    return;
-
 #ifdef VMS
   end_kbd_input ();
   SYS$QIOW (0, fileno (TTY_INPUT (CURTTY())), IO$_READVBLK|IO$M_PURGE, input_iosb, 0, 0,
@@ -283,7 +280,7 @@ discard_tty_input ()
 #else /* not VMS */
 #ifdef APOLLO
   {
-    struct tty_output *tty;
+    struct tty_display_info *tty;
     for (tty = tty_list; tty; tty = tty->next)
       {
         int zero = 0;
@@ -296,7 +293,7 @@ discard_tty_input ()
     ;
 #else /* not MSDOS */
   {
-    struct tty_output *tty;
+    struct tty_display_info *tty;
     for (tty = tty_list; tty; tty = tty->next)
       {
         EMACS_GET_TTY (fileno (TTY_INPUT (tty)), &buf);
@@ -309,15 +306,18 @@ discard_tty_input ()
 #endif /* not WINDOWSNT */
 }
 
+\f
 #ifdef SIGTSTP
 
 /* Arrange for character C to be read as the next input from
-   the terminal.  */
+   the terminal.
+   XXX What if we have multiple ttys?
+*/
 
 void
 stuff_char (char c)
 {
-  if (read_socket_hook)
+  if (! FRAME_TERMCAP_P (SELECTED_FRAME ()))
     return;
 
 /* Should perhaps error if in batch mode */
@@ -331,7 +331,7 @@ stuff_char (char c)
 #endif /* SIGTSTP */
 \f
 void
-init_baud_rate (struct tty_output *tty)
+init_baud_rate (int fd)
 {
   if (noninteractive)
     emacs_ospeed = 0;
@@ -346,7 +346,7 @@ init_baud_rate (struct tty_output *tty)
 #ifdef VMS
       struct sensemode sg;
 
-      SYS$QIOW (0, fileno (TTY_INPUT (tty)), IO$_SENSEMODE, &sg, 0, 0,
+      SYS$QIOW (0, fd, IO$_SENSEMODE, &sg, 0, 0,
                &sg.class, 12, 0, 0, 0, 0 );
       emacs_ospeed = sg.xmit_baud;
 #else /* not VMS */
@@ -354,7 +354,7 @@ init_baud_rate (struct tty_output *tty)
       struct termios sg;
 
       sg.c_cflag = B9600;
-      tcgetattr (fileno (TTY_INPUT (tty)), &sg);
+      tcgetattr (fd, &sg);
       emacs_ospeed = cfgetospeed (&sg);
 #if defined (USE_GETOBAUD) && defined (getobaud)
       /* m88k-motorola-sysv3 needs this (ghazi@noc.rutgers.edu) 9/1/94. */
@@ -367,16 +367,16 @@ init_baud_rate (struct tty_output *tty)
 
       sg.c_cflag = B9600;
 #ifdef HAVE_TCATTR
-      tcgetattr (fileno (TTY_INPUT (tty)), &sg);
+      tcgetattr (fd, &sg);
 #else
-      ioctl (fileno (TTY_INPUT (tty)), TCGETA, &sg);
+      ioctl (fd, TCGETA, &sg);
 #endif
       emacs_ospeed = sg.c_cflag & CBAUD;
 #else /* neither VMS nor TERMIOS nor TERMIO */
       struct sgttyb sg;
 
       sg.sg_ospeed = B9600;
-      if (ioctl (fileno (TTY_INPUT (tty)), TIOCGETP, &sg) < 0)
+      if (ioctl (fd, TIOCGETP, &sg) < 0)
        abort ();
       emacs_ospeed = sg.sg_ospeed;
 #endif /* not HAVE_TERMIO */
@@ -392,6 +392,7 @@ init_baud_rate (struct tty_output *tty)
     baud_rate = 1200;
 }
 
+\f
 /*ARGSUSED*/
 void
 set_exclusive_use (fd)
@@ -907,18 +908,40 @@ restore_signal_handlers (saved_handlers)
     }
 }
 \f
+#ifndef SIGIO
+/* If SIGIO is broken, don't do anything. */
+void
+init_sigio (int fd)
+{
+}
+
+void
+reset_sigio (int fd)
+{
+}
+
+void
+request_sigio (void)
+{
+}
+
+void
+unrequest_sigio (void)
+{
+}
+
+#else
 #ifdef F_SETFL
 
-int old_fcntl_flags;
+int old_fcntl_flags[MAXDESC];
 
 void
 init_sigio (fd)
      int fd;
 {
 #ifdef FASYNC
-  /* XXX What if we get called with more than one fds? */
-  old_fcntl_flags = fcntl (fd, F_GETFL, 0) & ~FASYNC;
-  fcntl (fd, F_SETFL, old_fcntl_flags | FASYNC);
+  old_fcntl_flags[fd] = fcntl (fd, F_GETFL, 0) & ~FASYNC;
+  fcntl (fd, F_SETFL, old_fcntl_flags[fd] | FASYNC);
 #endif
   interrupts_deferred = 0;
 }
@@ -927,16 +950,23 @@ void
 reset_sigio (fd)
      int fd;
 {
-  fcntl (fd, F_SETFL, old_fcntl_flags);
+#ifdef FASYNC
+  fcntl (fd, F_SETFL, old_fcntl_flags[fd]);
+#endif
 }
 
 #ifdef FASYNC          /* F_SETFL does not imply existence of FASYNC */
+/* XXX Uhm, FASYNC is not used anymore here. */
 
 void
 request_sigio ()
 {
+  /* XXX read_socket_hook is not global anymore.  Is blocking SIGIO
+     bad under X? */
+#if 0
   if (read_socket_hook)
     return;
+#endif
 
 #ifdef SIGWINCH
   sigunblock (sigmask (SIGWINCH));
@@ -948,10 +978,14 @@ request_sigio ()
 
 void
 unrequest_sigio (void)
-{
+{ 
+  /* XXX read_socket_hook is not global anymore.  Is blocking SIGIO
+     bad under X? */
+#if 0
   if (read_socket_hook)
     return;
-
+#endif
+  
 #ifdef SIGWINCH
   sigblock (sigmask (SIGWINCH));
 #endif
@@ -976,7 +1010,7 @@ request_sigio ()
 }
 
 void
-unrequest_sigio (struct tty_output *tty)
+unrequest_sigio ()
 {
   int off = 0;
 
@@ -1048,6 +1082,7 @@ unrequest_sigio ()
 #endif /* STRIDE */
 #endif /* FASYNC */
 #endif /* F_SETFL */
+#endif /* SIGIO */
 \f
 /* Saving and restoring the process group of Emacs's terminal.  */
 
@@ -1078,24 +1113,25 @@ int inherited_pgroup;
    group, redirect the TTY to point to our own process group.  We need
    to be in our own process group to receive SIGIO properly.  */
 void
-narrow_foreground_group (struct tty_output *tty)
+narrow_foreground_group (int fd)
 {
   int me = getpid ();
 
-  setpgrp (0, inherited_pgroup);
+  if (! inherited_pgroup)
+    inherited_pgroup = getpgid (0);
   /* XXX This only works on the controlling tty. */
   if (inherited_pgroup != me)
-    EMACS_SET_TTY_PGRP (fileno (TTY_INPUT (tty)), &me);
-  setpgrp (0, me);
+    EMACS_SET_TTY_PGRP (fd, &me);
+  setpgid (0, me);
 }
 
 /* Set the tty to our original foreground group.  */
 void
-widen_foreground_group (struct tty_output *tty)
+widen_foreground_group (int fd)
 {
   if (inherited_pgroup != getpid ())
-    EMACS_SET_TTY_PGRP (fileno (TTY_INPUT (tty)), &inherited_pgroup);
-  setpgrp (0, inherited_pgroup);
+    EMACS_SET_TTY_PGRP (fd, &inherited_pgroup);
+  setpgid (0, inherited_pgroup);
 }
 
 #endif /* BSD_PGRPS */
@@ -1261,7 +1297,7 @@ int lmode;
 
 #ifndef F_SETOWN_BUG
 #ifdef F_SETOWN
-int old_fcntl_owner;
+int old_fcntl_owner[MAXDESC];
 #endif /* F_SETOWN */
 #endif /* F_SETOWN_BUG */
 
@@ -1289,16 +1325,14 @@ static struct tchars new_tchars = {-1,-1,-1,-1,-1,-1};
 void
 init_all_sys_modes (void)
 {
-  struct tty_output *tty = tty_list;
-  while (tty) {
+  struct tty_display_info *tty;
+  for (tty = tty_list; tty; tty = tty->next)
     init_sys_modes (tty);
-    tty = tty->next;
-  }
 }
 
 void
 init_sys_modes (tty_out)
-     struct tty_output *tty_out;
+     struct tty_display_info *tty_out;
 {
   struct emacs_tty tty;
 
@@ -1357,243 +1391,264 @@ nil means don't delete them until `list-processes' is run.  */);
 #endif /* VMS */
 
 #ifdef BSD_PGRPS
-  if (! read_socket_hook && EQ (Vwindow_system, Qnil))
-    narrow_foreground_group (tty_out);
+#if 0
+  /* read_socket_hook is not global anymore.  I think doing this
+     unconditionally will not cause any problems. */
+  if (! read_socket_hook && EQ (Vinitial_window_system, Qnil))
 #endif
-
-#ifdef HAVE_WINDOW_SYSTEM
-  /* Emacs' window system on MSDOG uses the `internal terminal' and therefore
-     needs the initialization code below.  */
-  if (!read_socket_hook && EQ (Vwindow_system, Qnil))
+    narrow_foreground_group (fileno (TTY_INPUT (tty_out)));
 #endif
-    {
-      if (! tty_out->old_tty)
-        tty_out->old_tty = (struct emacs_tty *) xmalloc (sizeof (struct emacs_tty));
+
+  if (! tty_out->old_tty)
+    tty_out->old_tty = (struct emacs_tty *) xmalloc (sizeof (struct emacs_tty));
       
-      EMACS_GET_TTY (fileno (TTY_INPUT (tty_out)), tty_out->old_tty);
+  EMACS_GET_TTY (fileno (TTY_INPUT (tty_out)), tty_out->old_tty);
 
-      tty = *tty_out->old_tty;
+  tty = *tty_out->old_tty;
 
 #if defined (HAVE_TERMIO) || defined (HAVE_TERMIOS)
-      XSETINT (Vtty_erase_char, tty.main.c_cc[VERASE]);
+  XSETINT (Vtty_erase_char, tty.main.c_cc[VERASE]);
 
 #ifdef DGUX
-      /* This allows meta to be sent on 8th bit.  */
-      tty.main.c_iflag &= ~INPCK;      /* don't check input for parity */
+  /* This allows meta to be sent on 8th bit.  */
+  tty.main.c_iflag &= ~INPCK;  /* don't check input for parity */
 #endif
-      tty.main.c_iflag |= (IGNBRK);    /* Ignore break condition */
-      tty.main.c_iflag &= ~ICRNL;      /* Disable map of CR to NL on input */
+  tty.main.c_iflag |= (IGNBRK);        /* Ignore break condition */
+  tty.main.c_iflag &= ~ICRNL;  /* Disable map of CR to NL on input */
 #ifdef INLCR  /* I'm just being cautious,
                 since I can't check how widespread INLCR is--rms.  */
-      tty.main.c_iflag &= ~INLCR;      /* Disable map of NL to CR on input */
+  tty.main.c_iflag &= ~INLCR;  /* Disable map of NL to CR on input */
 #endif
 #ifdef ISTRIP
-      tty.main.c_iflag &= ~ISTRIP;     /* don't strip 8th bit on input */
+  tty.main.c_iflag &= ~ISTRIP; /* don't strip 8th bit on input */
 #endif
-      tty.main.c_lflag &= ~ECHO;       /* Disable echo */
-      tty.main.c_lflag &= ~ICANON;     /* Disable erase/kill processing */
+  tty.main.c_lflag &= ~ECHO;   /* Disable echo */
+  tty.main.c_lflag &= ~ICANON; /* Disable erase/kill processing */
 #ifdef IEXTEN
-      tty.main.c_lflag &= ~IEXTEN;     /* Disable other editing characters.  */
+  tty.main.c_lflag &= ~IEXTEN; /* Disable other editing characters.  */
 #endif
-      tty.main.c_lflag |= ISIG;        /* Enable signals */
-      if (flow_control)
-       {
-         tty.main.c_iflag |= IXON;     /* Enable start/stop output control */
+  tty.main.c_lflag |= ISIG;    /* Enable signals */
+  if (tty_out->flow_control)
+    {
+      tty.main.c_iflag |= IXON;        /* Enable start/stop output control */
 #ifdef IXANY
-         tty.main.c_iflag &= ~IXANY;
+      tty.main.c_iflag &= ~IXANY;
 #endif /* IXANY */
-       }
-      else
-       tty.main.c_iflag &= ~IXON;      /* Disable start/stop output control */
-      tty.main.c_oflag &= ~ONLCR;      /* Disable map of NL to CR-NL
-                                          on output */
-      tty.main.c_oflag &= ~TAB3;       /* Disable tab expansion */
+    }
+  else
+    tty.main.c_iflag &= ~IXON; /* Disable start/stop output control */
+  tty.main.c_oflag &= ~ONLCR;  /* Disable map of NL to CR-NL
+                                   on output */
+  tty.main.c_oflag &= ~TAB3;   /* Disable tab expansion */
 #ifdef CS8
-      if (tty_out->meta_key)
-       {
-         tty.main.c_cflag |= CS8;      /* allow 8th bit on input */
-         tty.main.c_cflag &= ~PARENB;/* Don't check parity */
-       }
+  if (tty_out->meta_key)
+    {
+      tty.main.c_cflag |= CS8; /* allow 8th bit on input */
+      tty.main.c_cflag &= ~PARENB;/* Don't check parity */
+    }
 #endif
+  if (tty_out->input == stdin)
+    {
       tty.main.c_cc[VINTR] = quit_char;        /* C-g (usually) gives SIGINT */
       /* Set up C-g for both SIGQUIT and SIGINT.
-        We don't know which we will get, but we handle both alike
-        so which one it really gives us does not matter.  */
+         We don't know which we will get, but we handle both alike
+         so which one it really gives us does not matter.  */
       tty.main.c_cc[VQUIT] = quit_char;
-      tty.main.c_cc[VMIN] = 1; /* Input should wait for at least 1 char */
-      tty.main.c_cc[VTIME] = 0;        /* no matter how long that takes.  */
+    }
+  else
+    {
+      /* We normally don't get interrupt or quit signals from tty
+         devices other than our controlling terminal; therefore,
+         we must handle C-g as normal input.  Unfortunately, this
+         means that the interrupt and quit feature must be
+         disabled on secondary ttys, or we would not even see the
+         keypress.
+         
+         Note that even though emacsclient could have special code
+         to pass SIGINT to Emacs, we should _not_ enable
+         interrupt/quit keys for emacsclient frames.  This means
+         that we can't break out of loops in C code from a
+         secondary tty frame, but we can always decide what
+         display the C-g came from, which is more important from a
+         usability point of view.  (Consider the case when two
+         people work together using the same Emacs instance.)  */
+      tty.main.c_cc[VINTR] = CDISABLE;
+      tty.main.c_cc[VQUIT] = CDISABLE;
+    }
+  tty.main.c_cc[VMIN] = 1;     /* Input should wait for at least 1 char */
+  tty.main.c_cc[VTIME] = 0;    /* no matter how long that takes.  */
 #ifdef VSWTCH
-      tty.main.c_cc[VSWTCH] = CDISABLE;        /* Turn off shell layering use
+  tty.main.c_cc[VSWTCH] = CDISABLE;    /* Turn off shell layering use
                                           of C-z */
 #endif /* VSWTCH */
-
+  
 #if defined (mips) || defined (HAVE_TCATTR)
 #ifdef VSUSP
-      tty.main.c_cc[VSUSP] = CDISABLE; /* Turn off mips handling of C-z.  */
+  tty.main.c_cc[VSUSP] = CDISABLE;     /* Turn off mips handling of C-z.  */
 #endif /* VSUSP */
 #ifdef V_DSUSP
-      tty.main.c_cc[V_DSUSP] = CDISABLE; /* Turn off mips handling of C-y.  */
+  tty.main.c_cc[V_DSUSP] = CDISABLE; /* Turn off mips handling of C-y.  */
 #endif /* V_DSUSP */
 #ifdef VDSUSP /* Some systems have VDSUSP, some have V_DSUSP.  */
-      tty.main.c_cc[VDSUSP] = CDISABLE;
+  tty.main.c_cc[VDSUSP] = CDISABLE;
 #endif /* VDSUSP */
 #ifdef VLNEXT
-      tty.main.c_cc[VLNEXT] = CDISABLE;
+  tty.main.c_cc[VLNEXT] = CDISABLE;
 #endif /* VLNEXT */
 #ifdef VREPRINT
-      tty.main.c_cc[VREPRINT] = CDISABLE;
+  tty.main.c_cc[VREPRINT] = CDISABLE;
 #endif /* VREPRINT */
 #ifdef VWERASE
-      tty.main.c_cc[VWERASE] = CDISABLE;
+  tty.main.c_cc[VWERASE] = CDISABLE;
 #endif /* VWERASE */
 #ifdef VDISCARD
-      tty.main.c_cc[VDISCARD] = CDISABLE;
+  tty.main.c_cc[VDISCARD] = CDISABLE;
 #endif /* VDISCARD */
 
-      if (flow_control)
-       {
+  if (tty_out->flow_control)
+    {
 #ifdef VSTART
-         tty.main.c_cc[VSTART] = '\021';
+      tty.main.c_cc[VSTART] = '\021';
 #endif /* VSTART */
 #ifdef VSTOP
-         tty.main.c_cc[VSTOP] = '\023';
+      tty.main.c_cc[VSTOP] = '\023';
 #endif /* VSTOP */
-       }
-      else
-       {
+    }
+  else
+    {
 #ifdef VSTART
-         tty.main.c_cc[VSTART] = CDISABLE;
+      tty.main.c_cc[VSTART] = CDISABLE;
 #endif /* VSTART */
 #ifdef VSTOP
-         tty.main.c_cc[VSTOP] = CDISABLE;
+      tty.main.c_cc[VSTOP] = CDISABLE;
 #endif /* VSTOP */
-       }
+    }
 #endif /* mips or HAVE_TCATTR */
 
 #ifdef SET_LINE_DISCIPLINE
-      /* Need to explicitly request TERMIODISC line discipline or
-         Ultrix's termios does not work correctly.  */
-      tty.main.c_line = SET_LINE_DISCIPLINE;
+  /* Need to explicitly request TERMIODISC line discipline or
+     Ultrix's termios does not work correctly.  */
+  tty.main.c_line = SET_LINE_DISCIPLINE;
 #endif
 #ifdef AIX
 #ifndef IBMR2AIX
-      /* AIX enhanced edit loses NULs, so disable it.  */
-      tty.main.c_line = 0;
-      tty.main.c_iflag &= ~ASCEDIT;
+  /* AIX enhanced edit loses NULs, so disable it.  */
+  tty.main.c_line = 0;
+  tty.main.c_iflag &= ~ASCEDIT;
 #else
-      tty.main.c_cc[VSTRT] = 255;
-      tty.main.c_cc[VSTOP] = 255;
-      tty.main.c_cc[VSUSP] = 255;
-      tty.main.c_cc[VDSUSP] = 255;
+  tty.main.c_cc[VSTRT] = 255;
+  tty.main.c_cc[VSTOP] = 255;
+  tty.main.c_cc[VSUSP] = 255;
+  tty.main.c_cc[VDSUSP] = 255;
 #endif /* IBMR2AIX */
-      if (flow_control)
-       {
+  if (tty_out->flow_control)
+    {
 #ifdef VSTART
-         tty.main.c_cc[VSTART] = '\021';
+      tty.main.c_cc[VSTART] = '\021';
 #endif /* VSTART */
 #ifdef VSTOP
-         tty.main.c_cc[VSTOP] = '\023';
+      tty.main.c_cc[VSTOP] = '\023';
 #endif /* VSTOP */
-       }
-      /* Also, PTY overloads NUL and BREAK.
-        don't ignore break, but don't signal either, so it looks like NUL.
-        This really serves a purpose only if running in an XTERM window
-        or via TELNET or the like, but does no harm elsewhere.  */
-      tty.main.c_iflag &= ~IGNBRK;
-      tty.main.c_iflag &= ~BRKINT;
+    }
+  /* Also, PTY overloads NUL and BREAK.
+     don't ignore break, but don't signal either, so it looks like NUL.
+     This really serves a purpose only if running in an XTERM window
+     or via TELNET or the like, but does no harm elsewhere.  */
+  tty.main.c_iflag &= ~IGNBRK;
+  tty.main.c_iflag &= ~BRKINT;
 #endif
 #else /* if not HAVE_TERMIO */
 #ifdef VMS
-      tty.main.tt_char |= TT$M_NOECHO;
-      if (meta_key)
-       tty.main.tt_char |= TT$M_EIGHTBIT;
-      if (flow_control)
-       tty.main.tt_char |= TT$M_TTSYNC;
-      else
-       tty.main.tt_char &= ~TT$M_TTSYNC;
-      tty.main.tt2_char |= TT2$M_PASTHRU | TT2$M_XON;
+  tty.main.tt_char |= TT$M_NOECHO;
+  if (meta_key)
+    tty.main.tt_char |= TT$M_EIGHTBIT;
+  if (tty_out->flow_control)
+    tty.main.tt_char |= TT$M_TTSYNC;
+  else
+    tty.main.tt_char &= ~TT$M_TTSYNC;
+  tty.main.tt2_char |= TT2$M_PASTHRU | TT2$M_XON;
 #else /* not VMS (BSD, that is) */
 #ifndef DOS_NT
-      XSETINT (Vtty_erase_char, tty.main.sg_erase);
-      tty.main.sg_flags &= ~(ECHO | CRMOD | XTABS);
-      if (meta_key)
-       tty.main.sg_flags |= ANYP;
-      tty.main.sg_flags |= interrupt_input ? RAW : CBREAK;
+  XSETINT (Vtty_erase_char, tty.main.sg_erase);
+  tty.main.sg_flags &= ~(ECHO | CRMOD | XTABS);
+  if (meta_key)
+    tty.main.sg_flags |= ANYP;
+  tty.main.sg_flags |= interrupt_input ? RAW : CBREAK;
 #endif /* not DOS_NT */
 #endif /* not VMS (BSD, that is) */
 #endif /* not HAVE_TERMIO */
 
-      /* If going to use CBREAK mode, we must request C-g to interrupt
-        and turn off start and stop chars, etc.  If not going to use
-        CBREAK mode, do this anyway so as to turn off local flow
-        control for user coming over network on 4.2; in this case,
-        only t_stopc and t_startc really matter.  */
+  /* If going to use CBREAK mode, we must request C-g to interrupt
+     and turn off start and stop chars, etc.  If not going to use
+     CBREAK mode, do this anyway so as to turn off local flow
+     control for user coming over network on 4.2; in this case,
+     only t_stopc and t_startc really matter.  */
 #ifndef HAVE_TERMIO
 #ifdef HAVE_TCHARS
-      /* Note: if not using CBREAK mode, it makes no difference how we
-        set this */
-      tty.tchars = new_tchars;
-      tty.tchars.t_intrc = quit_char;
-      if (flow_control)
-       {
-         tty.tchars.t_startc = '\021';
-         tty.tchars.t_stopc = '\023';
-       }
-
-      tty.lmode = LDECCTQ | LLITOUT | LPASS8 | LNOFLSH | tty_out->old_tty.lmode;
+  /* Note: if not using CBREAK mode, it makes no difference how we
+     set this */
+  tty.tchars = new_tchars;
+  tty.tchars.t_intrc = quit_char;
+  if (tty_out->flow_control)
+    {
+      tty.tchars.t_startc = '\021';
+      tty.tchars.t_stopc = '\023';
+    }
+  
+  tty.lmode = LDECCTQ | LLITOUT | LPASS8 | LNOFLSH | tty_out->old_tty.lmode;
 #ifdef ultrix
-      /* Under Ultrix 4.2a, leaving this out doesn't seem to hurt
-        anything, and leaving it in breaks the meta key.  Go figure.  */
-      tty.lmode &= ~LLITOUT;
+  /* Under Ultrix 4.2a, leaving this out doesn't seem to hurt
+     anything, and leaving it in breaks the meta key.  Go figure.  */
+  tty.lmode &= ~LLITOUT;
 #endif
-
+  
 #ifdef BSD4_1
-      lmode = tty.lmode;
+  lmode = tty.lmode;
 #endif
 
 #endif /* HAVE_TCHARS */
 #endif /* not HAVE_TERMIO */
 
 #ifdef HAVE_LTCHARS
-      tty.ltchars = new_ltchars;
+  tty.ltchars = new_ltchars;
 #endif /* HAVE_LTCHARS */
 #ifdef MSDOS   /* Demacs 1.1.2 91/10/20 Manabu Higashida, MW Aug 1993 */
-      if (!tty_out->term_initted)
-       internal_terminal_init ();
-      dos_ttraw ();
+  if (!tty_out->term_initted)
+    internal_terminal_init ();
+  dos_ttraw ();
 #endif
 
-      EMACS_SET_TTY (fileno (TTY_INPUT (tty_out)), &tty, 0);
+  EMACS_SET_TTY (fileno (TTY_INPUT (tty_out)), &tty, 0);
 
-      /* This code added to insure that, if flow-control is not to be used,
-        we have an unlocked terminal at the start. */
+  /* This code added to insure that, if flow-control is not to be used,
+     we have an unlocked terminal at the start. */
 
 #ifdef TCXONC
-      if (!flow_control) ioctl (fileno (TTY_INPUT (tty_out)), TCXONC, 1);
+  if (!tty_out->flow_control) ioctl (fileno (TTY_INPUT (tty_out)), TCXONC, 1);
 #endif
 #ifndef APOLLO
 #ifdef TIOCSTART
-      if (!flow_control) ioctl (fileno (TTY_INPUT (tty_out)), TIOCSTART, 0);
+  if (!tty_out->flow_control) ioctl (fileno (TTY_INPUT (tty_out)), TIOCSTART, 0);
 #endif
 #endif
 
 #if defined (HAVE_TERMIOS) || defined (HPUX9)
 #ifdef TCOON
-      if (!flow_control) tcflow (fileno (TTY_INPUT (tty_out)), TCOON);
+  if (!tty_out->flow_control) tcflow (fileno (TTY_INPUT (tty_out)), TCOON);
 #endif
 #endif
 
 #ifdef AIXHFT
-      hft_init (tty_out);
+  hft_init (tty_out);
 #ifdef IBMR2AIX
-      {
-       /* IBM's HFT device usually thinks a ^J should be LF/CR.  We need it
-          to be only LF.  This is the way that is done. */
-       struct termio tty;
-
-       if (ioctl (1, HFTGETID, &tty) != -1)
-         write (1, "\033[20l", 5);
-      }
+  {
+    /* IBM's HFT device usually thinks a ^J should be LF/CR.  We need it
+       to be only LF.  This is the way that is done. */
+    struct termio tty;
+    
+    if (ioctl (1, HFTGETID, &tty) != -1)
+      write (1, "\033[20l", 5);
+  }
 #endif
 #endif /* AIXHFT */
 
@@ -1602,17 +1657,16 @@ nil means don't delete them until `list-processes' is run.  */);
       SYS$QIOW (0, fileno (TTY_INPUT (tty_out)), IO$_SETMODE|IO$M_OUTBAND, 0, 0, 0,
                interrupt_signal, oob_chars, 0, 0, 0, 0);
 */
-      queue_kbd_input (0);
+  queue_kbd_input (0);
 #endif /* VMS */
-    }
 
 #ifdef F_SETFL
 #ifndef F_SETOWN_BUG
 #ifdef F_GETOWN                /* F_SETFL does not imply existence of F_GETOWN */
-  if (interrupt_input
-      && ! read_socket_hook && EQ (Vwindow_system, Qnil))
+  if (interrupt_input)
     {
-      old_fcntl_owner = fcntl (fileno (TTY_INPUT (tty_out)), F_GETOWN, 0);
+      old_fcntl_owner[fileno (TTY_INPUT (tty_out))] =
+        fcntl (fileno (TTY_INPUT (tty_out)), F_GETOWN, 0);
       fcntl (fileno (TTY_INPUT (tty_out)), F_SETOWN, getpid ());
       init_sigio (fileno (TTY_INPUT (tty_out)));
     }
@@ -1636,23 +1690,15 @@ nil means don't delete them until `list-processes' is run.  */);
 #else
   setbuf (TTY_OUTPUT (tty_out), (char *) _sobuf);
 #endif
-#ifdef HAVE_WINDOW_SYSTEM
-  /* Emacs' window system on MSDOG uses the `internal terminal' and therefore
-     needs the initialization code below.  */
-  if (EQ (Vwindow_system, Qnil)
-#ifndef WINDOWSNT
-      /* When running in tty mode on NT/Win95, we have a read_socket
-        hook, but still need the rest of the initialization code below.  */
-      && (! read_socket_hook)
-#endif
-      )
-#endif
+
+  tty_set_terminal_modes (tty_out->display);
 
   if (!tty_out->term_initted)
     {
       Lisp_Object tail, frame;
       FOR_EACH_FRAME (tail, frame)
         {
+          /* XXX This needs to be revised. */
           if (FRAME_TERMCAP_P (XFRAME (frame))
               && FRAME_TTY (XFRAME (frame)) == tty_out)
             init_frame_faces (XFRAME (frame));
@@ -1683,11 +1729,11 @@ nil means don't delete them until `list-processes' is run.  */);
    At the time this is called, init_sys_modes has not been done yet.  */
 
 int
-tabs_safe_p (struct tty_output *tty)
+tabs_safe_p (int fd)
 {
   struct emacs_tty etty;
 
-  EMACS_GET_TTY (fileno (TTY_INPUT (tty)), &etty);
+  EMACS_GET_TTY (fd, &etty);
   return EMACS_TTY_TABS_OK (&etty);
 }
 \f
@@ -1696,9 +1742,7 @@ tabs_safe_p (struct tty_output *tty)
    We store 0 if there's no valid information.  */
 
 void
-get_tty_size (tty_out, widthp, heightp)
-     struct tty_output *tty_out;
-     int *widthp, *heightp;
+get_tty_size (int fd, int *widthp, int *heightp)
 {
 
 #ifdef TIOCGWINSZ
@@ -1706,7 +1750,7 @@ get_tty_size (tty_out, widthp, heightp)
   /* BSD-style.  */
   struct winsize size;
 
-  if (ioctl (fileno (TTY_INPUT (tty_out)), TIOCGWINSZ, &size) == -1)
+  if (ioctl (fd, TIOCGWINSZ, &size) == -1)
     *widthp = *heightp = 0;
   else
     {
@@ -1720,7 +1764,7 @@ get_tty_size (tty_out, widthp, heightp)
   /* SunOS - style.  */
   struct ttysize size;
 
-  if (ioctl (fileno (TTY_INPUT (tty_out)), TIOCGSIZE, &size) == -1)
+  if (ioctl (fd, TIOCGSIZE, &size) == -1)
     *widthp = *heightp = 0;
   else
     {
@@ -1733,7 +1777,7 @@ get_tty_size (tty_out, widthp, heightp)
 
   struct sensemode tty;
 
-  SYS$QIOW (0, fileno (TTY_INPUT (tty_out)), IO$_SENSEMODE, &tty, 0, 0,
+  SYS$QIOW (0, fd, IO$_SENSEMODE, &tty, 0, 0,
            &tty.class, 12, 0, 0, 0, 0);
   *widthp = tty.scr_wid;
   *heightp = tty.scr_len;
@@ -1746,7 +1790,6 @@ get_tty_size (tty_out, widthp, heightp)
   *widthp = 0;
   *heightp = 0;
 #endif
-
 #endif /* not VMS */
 #endif /* not SunOS-style */
 #endif /* not BSD-style */
@@ -1793,18 +1836,16 @@ set_window_size (fd, height, width)
 void
 reset_all_sys_modes (void)
 {
-  struct tty_output *tty = tty_list;
-  while (tty) {
+  struct tty_display_info *tty;
+  for (tty = tty_list; tty; tty = tty->next)
     reset_sys_modes (tty);
-    tty = tty->next;
-  }
 }
 
 /* Prepare the terminal for closing it; move the cursor to the
    bottom of the frame, turn off interrupt-driven I/O, etc.  */
 void
 reset_sys_modes (tty_out)
-     struct tty_output *tty_out;
+     struct tty_display_info *tty_out;
 {
   if (noninteractive)
     {
@@ -1813,21 +1854,14 @@ reset_sys_modes (tty_out)
     }
   if (!tty_out->term_initted)
     return;
-#ifdef HAVE_WINDOW_SYSTEM
-  /* Emacs' window system on MSDOG uses the `internal terminal' and therefore
-     needs the clean-up code below.  */
-  if (!EQ (Vwindow_system, Qnil)
-#ifndef WINDOWSNT
-      /* When running in tty mode on NT/Win95, we have a read_socket
-        hook, but still need the rest of the clean-up code below.  */
-      || read_socket_hook
-#endif
-      )
-    return;
-#endif
+  
   cmgoto (tty_out, FrameRows (tty_out) - 1, 0);
+#if 0  /* XXX This doesn't work anymore, the signature has changed. */
   tty_clear_end_of_line (tty_out, FrameCols (tty_out));
+#endif
   cmgoto (tty_out, FrameRows (tty_out) - 1, 0);
+  fflush (tty_out->output);
+  
 #if defined (IBMR2AIX) && defined (AIXHFT)
   {
     /* HFT devices normally use ^J as a LF/CR.  We forced it to
@@ -1839,12 +1873,12 @@ reset_sys_modes (tty_out)
   }
 #endif
 
-  tty_reset_terminal_modes (tty_out);
+  tty_reset_terminal_modes (tty_out->display);
   fflush (TTY_OUTPUT (tty_out));
 #ifdef BSD_SYSTEM
 #ifndef BSD4_1
   /* Avoid possible loss of output when changing terminal modes.  */
-  fsync (TTY_OUTPUT (tty_out));
+  fsync (fileno (TTY_OUTPUT (tty_out)));
 #endif
 #endif
 
@@ -1853,8 +1887,9 @@ reset_sys_modes (tty_out)
 #ifdef F_SETOWN                /* F_SETFL does not imply existence of F_SETOWN */
   if (interrupt_input)
     {
-      reset_sigio (tty_out);
-      fcntl (fileno (TTY_INPUT (tty_out)), F_SETOWN, old_fcntl_owner);
+      reset_sigio (fileno (TTY_INPUT (tty_out)));
+      fcntl (fileno (TTY_INPUT (tty_out)), F_SETOWN,
+             old_fcntl_owner[fileno (TTY_INPUT (tty_out))]);
     }
 #endif /* F_SETOWN */
 #endif /* F_SETOWN_BUG */
@@ -1865,7 +1900,7 @@ reset_sys_modes (tty_out)
 #endif /* F_SETFL */
 #ifdef BSD4_1
   if (interrupt_input)
-    reset_sigio (tty_out);
+    reset_sigio (fileno (TTY_INPUT (tty_out)));
 #endif /* BSD4_1 */
 
   if (tty_out->old_tty)
@@ -1889,7 +1924,7 @@ reset_sys_modes (tty_out)
 #endif
 
 #ifdef BSD_PGRPS
-  widen_foreground_group (tty_out);
+  widen_foreground_group (fileno (TTY_INPUT (tty_out)));
 #endif
 }
 \f
@@ -2507,7 +2542,9 @@ sys_select (nfds, rfds, wfds, efds, timeout)
      SELECT_TYPE *rfds, *wfds, *efds;
      EMACS_TIME *timeout;
 {
-  int ravail = 0;
+  /* XXX This needs to be updated for multi-tty support.  Is there
+     anybody who needs to emulate select these days?  */ 
+ int ravail = 0;
   SELECT_TYPE orfds;
   int timeoutval;
   int *local_timeout;
@@ -2522,7 +2559,7 @@ sys_select (nfds, rfds, wfds, efds, timeout)
 #if defined (HAVE_SELECT) && defined (HAVE_X_WINDOWS)
   /* If we're using X, then the native select will work; we only need the
      emulation for non-X usage.  */
-  if (!NILP (Vwindow_system))
+  if (!NILP (Vinitial_window_system))
     return select (nfds, rfds, wfds, efds, timeout);
 #endif
   timeoutval = timeout ? EMACS_SECS (*timeout) : 100000;
@@ -2631,7 +2668,7 @@ sys_select (nfds, rfds, wfds, efds, timeout)
     }
   return ravail;
 }
-#endif /* not WINDOWSNT */
+#endif not WINDOWSNT
 
 /* Read keyboard input into the standard buffer,
    waiting for at least one character.  */
@@ -2646,6 +2683,8 @@ sys_select (nfds, rfds, wfds, efds, timeout)
 void
 read_input_waiting ()
 {
+  /* XXX This needs to be updated for multi-tty support.  Is there
+     anybody who needs to emulate select these days?  */
   int nread, i;
   extern int quit_char;
 
@@ -2706,6 +2745,10 @@ read_input_waiting ()
     }
 }
 
+#if !defined (HAVE_SELECT) || defined (BROKEN_SELECT_NON_X)
+#define select sys_select
+#endif
+
 #endif /* not HAVE_SELECT */
 #endif /* not VMS */
 #endif /* not MSDOS */
@@ -5101,7 +5144,7 @@ srandom (seed)
 
 /* Called from init_sys_modes.  */
 void
-hft_init (struct tty_output *tty_out)
+hft_init (struct tty_display_info *tty_out)
 {
   int junk;
 
@@ -5147,16 +5190,12 @@ hft_init (struct tty_output *tty_out)
     keymap.hfkey[1].hf_char = 127;
     hftctl (0, HFSKBD, &buf);
   }
-  /* The HFT system on AIX doesn't optimize for scrolling, so it's really ugly
-     at times.  */
-  TTY_LINE_INS_DEL_OK (tty_out) = 0;
-  TTY_CHAR_INS_DEL_OK (tty_out) = 0;
 }
 
 /* Reset the rubout key to backspace.  */
 
 void
-hft_reset (struct tty_output *tty_out)
+hft_reset (struct tty_display_info *tty_out)
 {
   struct hfbuf buf;
   struct hfkeymap keymap;