+\f
+/* A signal handler that passes the signal to the Emacs process.
+ Useful for SIGWINCH. */
+
+SIGTYPE
+pass_signal_to_emacs (int signalnum)
+{
+ int old_errno = errno;
+
+ if (emacs_pid)
+ kill (emacs_pid, signalnum);
+
+ signal (signalnum, pass_signal_to_emacs);
+ errno = old_errno;
+}
+
+/* Signal handler for SIGCONT; notify the Emacs process that it can
+ now resume our tty frame. */
+
+SIGTYPE
+handle_sigcont (int signalnum)
+{
+ int old_errno = errno;
+
+ if (tcgetpgrp (1) == getpgrp ())
+ {
+ /* We are in the foreground. */
+ send_to_emacs (emacs_socket, "-resume \n");
+ }
+ else
+ {
+ /* We are in the background; cancel the continue. */
+ kill (getpid (), SIGSTOP);
+ }
+
+ signal (signalnum, handle_sigcont);
+ errno = old_errno;
+}
+
+/* Signal handler for SIGTSTP; notify the Emacs process that we are
+ going to sleep. Normally the suspend is initiated by Emacs via
+ server-handle-suspend-tty, but if the server gets out of sync with
+ reality, we may get a SIGTSTP on C-z. Handling this signal and
+ notifying Emacs about it should get things under control again. */
+
+SIGTYPE
+handle_sigtstp (int signalnum)
+{
+ int old_errno = errno;
+ sigset_t set;
+
+ if (emacs_socket)
+ send_to_emacs (emacs_socket, "-suspend \n");
+
+ /* Unblock this signal and call the default handler by temprarily
+ changing the handler and resignalling. */
+ sigprocmask (SIG_BLOCK, NULL, &set);
+ sigdelset (&set, signalnum);
+ signal (signalnum, SIG_DFL);
+ kill (getpid (), signalnum);
+ sigprocmask (SIG_SETMASK, &set, NULL); /* Let's the above signal through. */
+ signal (signalnum, handle_sigtstp);
+
+ errno = old_errno;
+}
+/* Set up signal handlers before opening a frame on the current tty. */
+
+void
+init_signals (void)
+{
+ /* Set up signal handlers. */
+ signal (SIGWINCH, pass_signal_to_emacs);
+
+ /* Don't pass SIGINT and SIGQUIT to Emacs, because it has no way of
+ deciding which terminal the signal came from. C-g is now a
+ normal input event on secondary terminals. */
+#if 0
+ signal (SIGINT, pass_signal_to_emacs);
+ signal (SIGQUIT, pass_signal_to_emacs);
+#endif
+
+ signal (SIGCONT, handle_sigcont);
+ signal (SIGTSTP, handle_sigtstp);
+ signal (SIGTTOU, handle_sigtstp);
+}
+
+