]> code.delx.au - gnu-emacs/blobdiff - src/keyboard.c
image.c, indent.c: Use bool for booleans.
[gnu-emacs] / src / keyboard.c
index 3d4061accfc03362a25a502edb95b420518fff41..f3d7df5e98fbafcd537c364b38355445f7c4abd6 100644 (file)
@@ -19,11 +19,11 @@ along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
 
 #include <config.h>
 
+#define BLOCKINPUT_INLINE EXTERN_INLINE
 #define KEYBOARD_INLINE EXTERN_INLINE
 
-#include <signal.h>
 #include <stdio.h>
-#include <setjmp.h>
+
 #include "lisp.h"
 #include "termchar.h"
 #include "termopts.h"
@@ -57,42 +57,28 @@ along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
 #include <sys/ioctl.h>
 #endif /* not MSDOS */
 
+#if defined USABLE_FIONREAD && defined USG5_4
+# include <sys/filio.h>
+#endif
+
 #include "syssignal.h"
 
 #include <sys/types.h>
 #include <unistd.h>
 #include <fcntl.h>
 
-/* This is to get the definitions of the XK_ symbols.  */
-#ifdef HAVE_X_WINDOWS
-#include "xterm.h"
-#endif
-
-#ifdef HAVE_NTGUI
-#include "w32term.h"
-#endif /* HAVE_NTGUI */
-
-#ifdef HAVE_NS
-#include "nsterm.h"
-#endif
+#ifdef HAVE_WINDOW_SYSTEM
+#include TERM_HEADER
+#endif /* HAVE_WINDOW_SYSTEM */
 
-/* Variables for blockinput.h:  */
+/* Variables for blockinput.h: */
 
-/* Non-zero if interrupt input is blocked right now.  */
+/* Positive if interrupt input is blocked right now.  */
 volatile int interrupt_input_blocked;
 
-/* Nonzero means an input interrupt has arrived
-   during the current critical section.  */
-int interrupt_input_pending;
-
-/* This var should be (interrupt_input_pending || pending_atimers).
-   The QUIT macro checks this instead of interrupt_input_pending and
-   pending_atimers separately, to reduce code size.  So, any code that
-   changes interrupt_input_pending or pending_atimers should update
-   this too.  */
-#ifdef SYNC_INPUT
-int pending_signals;
-#endif
+/* Nonzero means an input interrupt or alarm signal has arrived.
+   The QUIT macro checks this.  */
+volatile int pending_signals;
 
 #define KBD_BUFFER_SIZE 4096
 
@@ -151,7 +137,7 @@ static ptrdiff_t before_command_echo_length;
 
 /* For longjmp to where kbd input is being done.  */
 
-static jmp_buf getcjmp;
+static sys_jmp_buf getcjmp;
 
 /* True while doing kbd input.  */
 int waiting_for_input;
@@ -233,7 +219,11 @@ static Lisp_Object last_point_position_window;
    last event came from a macro.  We use this to determine when to
    generate switch-frame events.  This may be cleared by functions
    like Fselect_frame, to make sure that a switch-frame event is
-   generated by the next character.  */
+   generated by the next character.
+
+   FIXME: This is modified by a signal handler so it should be volatile.
+   It's exported to Lisp, though, so it can't simply be marked
+   'volatile' here.  */
 Lisp_Object internal_last_event_frame;
 
 /* The timestamp of the last input event we received from the X server.
@@ -395,22 +385,9 @@ int interrupt_input;
 /* Nonzero while interrupts are temporarily deferred during redisplay.  */
 int interrupts_deferred;
 
-/* Allow configure to inhibit use of FIONREAD.  */
-#ifdef BROKEN_FIONREAD
-#undef FIONREAD
-#endif
-
-/* We are unable to use interrupts if FIONREAD is not available,
-   so flush SIGIO so we won't try.  */
-#if !defined (FIONREAD)
-#ifdef SIGIO
-#undef SIGIO
-#endif
-#endif
-
 /* If we support a window system, turn on the code to poll periodically
    to detect C-g.  It isn't actually used when doing interrupt input.  */
-#if defined (HAVE_WINDOW_SYSTEM) && !defined (USE_ASYNC_EVENTS)
+#ifdef HAVE_WINDOW_SYSTEM
 #define POLL_FOR_INPUT
 #endif
 
@@ -434,7 +411,6 @@ static EMACS_TIME timer_last_idleness_start_time;
 /* Function for init_keyboard to call with no args (if nonzero).  */
 static void (*keyboard_init_hook) (void);
 
-static int read_avail_input (int);
 static void get_input_pending (int *, int);
 static int readable_events (int);
 static Lisp_Object read_char_x_menu_prompt (ptrdiff_t, Lisp_Object *,
@@ -453,24 +429,23 @@ static Lisp_Object modify_event_symbol (ptrdiff_t, int, Lisp_Object,
                                         Lisp_Object *, ptrdiff_t);
 static Lisp_Object make_lispy_switch_frame (Lisp_Object);
 static int help_char_p (Lisp_Object);
-static void save_getcjmp (jmp_buf);
-static void restore_getcjmp (jmp_buf);
+static void save_getcjmp (sys_jmp_buf);
+static void restore_getcjmp (sys_jmp_buf);
 static Lisp_Object apply_modifiers (int, Lisp_Object);
 static void clear_event (struct input_event *);
 static Lisp_Object restore_kboard_configuration (Lisp_Object);
-static void interrupt_signal (int signalnum);
-#ifdef SIGIO
-static void input_available_signal (int signo);
+#ifdef USABLE_SIGIO
+static void deliver_input_available_signal (int signo);
 #endif
-static void handle_interrupt (void);
+static void handle_interrupt (bool);
 static _Noreturn void quit_throw_to_read_char (int);
 static void process_special_events (void);
 static void timer_start_idle (void);
 static void timer_stop_idle (void);
 static void timer_resume_idle (void);
-static void handle_user_signal (int);
+static void deliver_user_signal (int);
 static char *find_user_signal_name (int);
-static int store_user_signal_events (void);
+static void store_user_signal_events (void);
 
 /* These setters are used only in this file, so they can be private.  */
 static inline void
@@ -845,7 +820,7 @@ This function is called by the editor initialization to begin editing.  */)
 
   /* If we enter while input is blocked, don't lock up here.
      This may happen through the debugger during redisplay.  */
-  if (INPUT_BLOCKED_P)
+  if (input_blocked_p ())
     return Qnil;
 
   command_loop_level++;
@@ -1032,7 +1007,7 @@ restore_kboard_configuration (Lisp_Object was_locked)
       pop_kboard ();
       /* The pop should not change the kboard.  */
       if (single_kboard && current_kboard != prev)
-        abort ();
+        emacs_abort ();
     }
   return Qnil;
 }
@@ -1238,8 +1213,7 @@ This also exits all active minibuffers.  */)
 
   /* Unblock input if we enter with input blocked.  This may happen if
      redisplay traps e.g. during tool-bar update with input blocked.  */
-  while (INPUT_BLOCKED_P)
-    UNBLOCK_INPUT;
+  totally_unblock_input ();
 
   Fthrow (Qtop_level, Qnil);
 }
@@ -1434,8 +1408,7 @@ command_loop_1 (void)
        Fkill_emacs (Qnil);
 
       /* Make sure the current window's buffer is selected.  */
-      if (XBUFFER (XWINDOW (selected_window)->buffer) != current_buffer)
-       set_buffer_internal (XBUFFER (XWINDOW (selected_window)->buffer));
+      set_buffer_internal (XBUFFER (XWINDOW (selected_window)->buffer));
 
       /* Display any malloc warning that just came out.  Use while because
         displaying one warning can cause another.  */
@@ -1483,15 +1456,6 @@ command_loop_1 (void)
            }
        }
 
-#if 0
-      /* Select the frame that the last event came from.  Usually,
-        switch-frame events will take care of this, but if some lisp
-        code swallows a switch-frame event, we'll fix things up here.
-        Is this a good idea?  */
-      if (FRAMEP (internal_last_event_frame)
-         && !EQ (internal_last_event_frame, selected_frame))
-       Fselect_frame (internal_last_event_frame, Qnil);
-#endif
       /* If it has changed current-menubar from previous value,
         really recompute the menubar from the value.  */
       if (! NILP (Vlucid_menu_bar_dirty_flag)
@@ -1513,8 +1477,7 @@ command_loop_1 (void)
       /* A filter may have run while we were reading the input.  */
       if (! FRAME_LIVE_P (XFRAME (selected_frame)))
        Fkill_emacs (Qnil);
-      if (XBUFFER (XWINDOW (selected_window)->buffer) != current_buffer)
-       set_buffer_internal (XBUFFER (XWINDOW (selected_window)->buffer));
+      set_buffer_internal (XBUFFER (XWINDOW (selected_window)->buffer));
 
       ++num_input_keys;
 
@@ -2032,17 +1995,9 @@ static struct atimer *poll_timer;
 void
 poll_for_input_1 (void)
 {
-/* Tell ns_read_socket() it is being called asynchronously so it can avoid
-   doing anything dangerous.  */
-#ifdef HAVE_NS
-  ++handling_signal;
-#endif
-  if (interrupt_input_blocked == 0
+  if (! input_blocked_p ()
       && !waiting_for_input)
-    read_avail_input (0);
-#ifdef HAVE_NS
-  --handling_signal;
-#endif
+    gobble_input ();
 }
 
 /* Timer callback function for poll_timer.  TIMER is equal to
@@ -2052,14 +2007,7 @@ static void
 poll_for_input (struct atimer *timer)
 {
   if (poll_suppress_count == 0)
-    {
-#ifdef SYNC_INPUT
-      interrupt_input_pending = 1;
-      pending_signals = 1;
-#else
-      poll_for_input_1 ();
-#endif
-    }
+    pending_signals = 1;
 }
 
 #endif /* POLL_FOR_INPUT */
@@ -2337,8 +2285,8 @@ read_char (int commandflag, ptrdiff_t nmaps, Lisp_Object *maps,
 {
   volatile Lisp_Object c;
   ptrdiff_t jmpcount;
-  jmp_buf local_getcjmp;
-  jmp_buf save_jump;
+  sys_jmp_buf local_getcjmp;
+  sys_jmp_buf save_jump;
   volatile int key_already_recorded = 0;
   Lisp_Object tem, save;
   volatile Lisp_Object previous_echo_area_message;
@@ -2379,15 +2327,6 @@ read_char (int commandflag, ptrdiff_t nmaps, Lisp_Object *maps,
       goto reread_first;
     }
 
-  if (unread_command_char != -1)
-    {
-      XSETINT (c, unread_command_char);
-      unread_command_char = -1;
-
-      reread = 1;
-      goto reread_first;
-    }
-
   if (CONSP (Vunread_command_events))
     {
       int was_disabled = 0;
@@ -2572,7 +2511,6 @@ read_char (int commandflag, ptrdiff_t nmaps, Lisp_Object *maps,
       && !NILP (prev_event) && ! EVENT_HAS_PARAMETERS (prev_event)
       /* Don't bring up a menu if we already have another event.  */
       && NILP (Vunread_command_events)
-      && unread_command_char < 0
       && !detect_input_pending_run_timers (0))
     {
       c = read_char_minibuf_menu_prompt (commandflag, nmaps, maps);
@@ -2594,7 +2532,7 @@ read_char (int commandflag, ptrdiff_t nmaps, Lisp_Object *maps,
      it *must not* be in effect when we call redisplay.  */
 
   jmpcount = SPECPDL_INDEX ();
-  if (_setjmp (local_getcjmp))
+  if (sys_setjmp (local_getcjmp))
     {
       /* Handle quits while reading the keyboard.  */
       /* We must have saved the outer value of getcjmp here,
@@ -2616,13 +2554,13 @@ read_char (int commandflag, ptrdiff_t nmaps, Lisp_Object *maps,
            Lisp_Object last = KVAR (kb, kbd_queue);
            /* We shouldn't get here if we were in single-kboard mode!  */
            if (single_kboard)
-             abort ();
+             emacs_abort ();
            if (CONSP (last))
              {
                while (CONSP (XCDR (last)))
                  last = XCDR (last);
                if (!NILP (XCDR (last)))
-                 abort ();
+                 emacs_abort ();
              }
            if (!CONSP (last))
              kset_kbd_queue (kb, Fcons (c, Qnil));
@@ -2708,8 +2646,7 @@ read_char (int commandflag, ptrdiff_t nmaps, Lisp_Object *maps,
       && !EQ (XCAR (prev_event), Qmenu_bar)
       && !EQ (XCAR (prev_event), Qtool_bar)
       /* Don't bring up a menu if we already have another event.  */
-      && NILP (Vunread_command_events)
-      && unread_command_char < 0)
+      && NILP (Vunread_command_events))
     {
       c = read_char_x_menu_prompt (nmaps, maps, prev_event, used_mouse_menu);
 
@@ -2795,7 +2732,7 @@ read_char (int commandflag, ptrdiff_t nmaps, Lisp_Object *maps,
       if (current_kboard->kbd_queue_has_data)
        {
          if (!CONSP (KVAR (current_kboard, kbd_queue)))
-           abort ();
+           emacs_abort ();
          c = XCAR (KVAR (current_kboard, kbd_queue));
          kset_kbd_queue (current_kboard,
                          XCDR (KVAR (current_kboard, kbd_queue)));
@@ -2862,7 +2799,7 @@ read_char (int commandflag, ptrdiff_t nmaps, Lisp_Object *maps,
              while (CONSP (XCDR (last)))
                last = XCDR (last);
              if (!NILP (XCDR (last)))
-               abort ();
+               emacs_abort ();
            }
          if (!CONSP (last))
            kset_kbd_queue (kb, Fcons (c, Qnil));
@@ -3391,7 +3328,7 @@ record_char (Lisp_Object c)
      If you, dear reader, have a better idea, you've got the source.  :-) */
   if (dribble)
     {
-      BLOCK_INPUT;
+      block_input ();
       if (INTEGERP (c))
        {
          if (XUINT (c) < 0x100)
@@ -3417,7 +3354,7 @@ record_char (Lisp_Object c)
        }
 
       fflush (dribble);
-      UNBLOCK_INPUT;
+      unblock_input ();
     }
 }
 
@@ -3427,13 +3364,13 @@ record_char (Lisp_Object c)
    See read_process_output.  */
 
 static void
-save_getcjmp (jmp_buf temp)
+save_getcjmp (sys_jmp_buf temp)
 {
   memcpy (temp, getcjmp, sizeof getcjmp);
 }
 
 static void
-restore_getcjmp (jmp_buf temp)
+restore_getcjmp (sys_jmp_buf temp)
 {
   memcpy (getcjmp, temp, sizeof getcjmp);
 }
@@ -3561,17 +3498,15 @@ kbd_buffer_store_event (register struct input_event *event)
    Else, if EVENT is a quit event, store the quit event
    in HOLD_QUIT, and return (thus ignoring further events).
 
-   This is used in read_avail_input to postpone the processing
-   of the quit event until all subsequent input events have been
-   parsed (and discarded).
- */
+   This is used to postpone the processing of the quit event until all
+   subsequent input events have been parsed (and discarded).  */
 
 void
 kbd_buffer_store_event_hold (register struct input_event *event,
                             struct input_event *hold_quit)
 {
   if (event->kind == NO_EVENT)
-    abort ();
+    emacs_abort ();
 
   if (hold_quit && hold_quit->kind != NO_EVENT)
     return;
@@ -3615,7 +3550,7 @@ kbd_buffer_store_event_hold (register struct input_event *event,
 
          if (hold_quit)
            {
-             memcpy (hold_quit, event, sizeof (*event));
+             *hold_quit = *event;
              return;
            }
 
@@ -3634,7 +3569,8 @@ kbd_buffer_store_event_hold (register struct input_event *event,
          }
 
          last_event_timestamp = event->timestamp;
-         handle_interrupt ();
+
+         handle_interrupt (0);
          return;
        }
 
@@ -3670,10 +3606,8 @@ kbd_buffer_store_event_hold (register struct input_event *event,
           /* Don't read keyboard input until we have processed kbd_buffer.
              This happens when pasting text longer than KBD_BUFFER_SIZE/2.  */
           hold_keyboard_input ();
-#ifdef SIGIO
           if (!noninteractive)
-            signal (SIGIO, SIG_IGN);
-#endif
+            ignore_sigio ();
           stop_polling ();
         }
 #endif /* subprocesses */
@@ -3692,7 +3626,6 @@ kbd_buffer_store_event_hold (register struct input_event *event,
       if (immediate_quit && NILP (Vinhibit_quit))
        {
          immediate_quit = 0;
-         sigfree ();
          QUIT;
        }
     }
@@ -3842,10 +3775,6 @@ kbd_buffer_get_event (KBOARD **kbp,
       /* Start reading input again, we have processed enough so we can
          accept new events again.  */
       unhold_keyboard_input ();
-#ifdef SIGIO
-      if (!noninteractive)
-        signal (SIGIO, input_available_signal);
-#endif /* SIGIO */
       start_polling ();
     }
 #endif /* subprocesses */
@@ -3887,10 +3816,9 @@ kbd_buffer_get_event (KBOARD **kbp,
       /* One way or another, wait until input is available; then, if
         interrupt handlers have not read it, read it now.  */
 
-/* Note SIGIO has been undef'd if FIONREAD is missing.  */
-#ifdef SIGIO
-      gobble_input (0);
-#endif /* SIGIO */
+#ifdef USABLE_SIGIO
+      gobble_input ();
+#endif
       if (kbd_fetch_ptr != kbd_store_ptr)
        break;
 #if defined (HAVE_MOUSE) || defined (HAVE_GPM)
@@ -3915,8 +3843,7 @@ kbd_buffer_get_event (KBOARD **kbp,
        wait_reading_process_output (0, 0, -1, 1, Qnil, NULL, 0);
 
       if (!interrupt_input && kbd_fetch_ptr == kbd_store_ptr)
-       /* Pass 1 for EXPECT since we just waited to have input.  */
-       read_avail_input (1);
+       gobble_input ();
     }
 
   if (CONSP (Vunread_command_events))
@@ -3966,7 +3893,7 @@ kbd_buffer_get_event (KBOARD **kbp,
 #else
          /* We're getting selection request events, but we don't have
              a window system.  */
-         abort ();
+         emacs_abort ();
 #endif
        }
 
@@ -4142,7 +4069,7 @@ kbd_buffer_get_event (KBOARD **kbp,
                *used_mouse_menu = 1;
 #endif
 #ifdef HAVE_NS
-             /* certain system events are non-key events */
+             /* Certain system events are non-key events.  */
              if (used_mouse_menu
                   && event->kind == NS_NONKEY_EVENT)
                *used_mouse_menu = 1;
@@ -4170,7 +4097,7 @@ kbd_buffer_get_event (KBOARD **kbp,
         so x remains nil.  */
       x = Qnil;
 
-      /* XXX Can f or mouse_position_hook be NULL here? */
+      /* XXX Can f or mouse_position_hook be NULL here?  */
       if (f && FRAME_TERMINAL (f)->mouse_position_hook)
         (*FRAME_TERMINAL (f)->mouse_position_hook) (&f, 0, &bar_window,
                                                     &part, &x, &y, &t);
@@ -4203,7 +4130,7 @@ kbd_buffer_get_event (KBOARD **kbp,
   else
     /* We were promised by the above while loop that there was
        something for us to read!  */
-    abort ();
+    emacs_abort ();
 
   input_pending = readable_events (0);
 
@@ -4272,7 +4199,7 @@ process_special_events (void)
 #else
          /* We're getting selection request events, but we don't have
              a window system.  */
-         abort ();
+         emacs_abort ();
 #endif
        }
     }
@@ -4350,9 +4277,8 @@ struct input_event last_timer_event EXTERNALLY_VISIBLE;
    ...).  Each element has the form (FUN . ARGS).  */
 Lisp_Object pending_funcalls;
 
-/* If TIMER is a valid timer, return nonzero and place its value into
-   *RESULT.  Otherwise return zero.  */
-static int
+/* Return true if TIMER is a valid timer, placing its value into *RESULT.  */
+static bool
 decode_timer (Lisp_Object timer, EMACS_TIME *result)
 {
   Lisp_Object *vector;
@@ -4363,7 +4289,7 @@ decode_timer (Lisp_Object timer, EMACS_TIME *result)
   if (! NILP (vector[0]))
     return 0;
 
-  return decode_time_components (vector[1], vector[2], vector[3], vector[4],
+  return decode_time_components (vector[1], vector[2], vector[3], vector[8],
                                 result, 0);
 }
 
@@ -4381,25 +4307,18 @@ decode_timer (Lisp_Object timer, EMACS_TIME *result)
    should be done.  */
 
 static EMACS_TIME
-timer_check_2 (void)
+timer_check_2 (Lisp_Object timers, Lisp_Object idle_timers)
 {
   EMACS_TIME nexttime;
   EMACS_TIME now;
   EMACS_TIME idleness_now;
-  Lisp_Object timers, idle_timers, chosen_timer;
-  struct gcpro gcpro1, gcpro2, gcpro3;
+  Lisp_Object chosen_timer;
+  struct gcpro gcpro1;
 
   nexttime = invalid_emacs_time ();
 
-  /* Always consider the ordinary timers.  */
-  timers = Vtimer_list;
-  /* Consider the idle timers only if Emacs is idle.  */
-  if (EMACS_TIME_VALID_P (timer_idleness_start_time))
-    idle_timers = Vtimer_idle_list;
-  else
-    idle_timers = Qnil;
   chosen_timer = Qnil;
-  GCPRO3 (timers, idle_timers, chosen_timer);
+  GCPRO1 (chosen_timer);
 
   /* First run the code that was delayed.  */
   while (CONSP (pending_funcalls))
@@ -4548,13 +4467,35 @@ EMACS_TIME
 timer_check (void)
 {
   EMACS_TIME nexttime;
+  Lisp_Object timers, idle_timers;
+  struct gcpro gcpro1, gcpro2;
+
+  Lisp_Object tem = Vinhibit_quit;
+  Vinhibit_quit = Qt;
+
+  /* We use copies of the timers' lists to allow a timer to add itself
+     again, without locking up Emacs if the newly added timer is
+     already ripe when added.  */
+
+  /* Always consider the ordinary timers.  */
+  timers = Fcopy_sequence (Vtimer_list);
+  /* Consider the idle timers only if Emacs is idle.  */
+  if (EMACS_TIME_VALID_P (timer_idleness_start_time))
+    idle_timers = Fcopy_sequence (Vtimer_idle_list);
+  else
+    idle_timers = Qnil;
+
+  Vinhibit_quit = tem;
+
+  GCPRO2 (timers, idle_timers);
 
   do
     {
-      nexttime = timer_check_2 ();
+      nexttime = timer_check_2 (timers, idle_timers);
     }
   while (EMACS_SECS (nexttime) == 0 && EMACS_NSECS (nexttime) == 0);
 
+  UNGCPRO;
   return nexttime;
 }
 
@@ -4565,7 +4506,7 @@ in the same style as (current-time).
 
 The value when Emacs is not idle is nil.
 
-NSEC is a multiple of the system clock resolution.  */)
+PSEC is a multiple of the system clock resolution.  */)
   (void)
 {
   if (EMACS_TIME_VALID_P (timer_idleness_start_time))
@@ -5626,7 +5567,7 @@ make_lispy_event (struct input_event *event)
          else if (FRAMEP (event->frame_or_window))
            f = XFRAME (event->frame_or_window);
          else
-           abort ();
+           emacs_abort ();
 
          if (FRAME_WINDOW_P (f))
            fuzz = double_click_fuzz;
@@ -5733,7 +5674,7 @@ make_lispy_event (struct input_event *event)
        else
          /* Every mouse event should either have the down_modifier or
              the up_modifier set.  */
-         abort ();
+         emacs_abort ();
 
        {
          /* Get the symbol we should use for the mouse click.  */
@@ -5794,7 +5735,7 @@ make_lispy_event (struct input_event *event)
          else if (FRAMEP (event->frame_or_window))
            fr = XFRAME (event->frame_or_window);
          else
-           abort ();
+           emacs_abort ();
 
          fuzz = FRAME_WINDOW_P (fr)
            ? double_click_fuzz : double_click_fuzz / 8;
@@ -5814,7 +5755,7 @@ make_lispy_event (struct input_event *event)
          else
            /* Every wheel event should either have the down_modifier or
               the up_modifier set.  */
-           abort ();
+           emacs_abort ();
 
           if (event->kind == HORIZ_WHEEL_EVENT)
             symbol_num += 2;
@@ -5983,7 +5924,7 @@ make_lispy_event (struct input_event *event)
       {
        char *name = find_user_signal_name (event->code);
        if (!name)
-         abort ();
+         emacs_abort ();
        return intern (name);
       }
 
@@ -6064,7 +6005,7 @@ make_lispy_event (struct input_event *event)
 
       /* The 'kind' field of the event is something we don't recognize.  */
     default:
-      abort ();
+      emacs_abort ();
     }
 }
 
@@ -6241,7 +6182,7 @@ apply_modifiers_uncached (int modifiers, char *base, int base_len, int base_len_
     /* Only the event queue may use the `up' modifier; it should always
        be turned into a click or drag event before presented to lisp code.  */
     if (modifiers & up_modifier)
-      abort ();
+      emacs_abort ();
 
     if (modifiers & alt_modifier)   { *p++ = 'A'; *p++ = '-'; }
     if (modifiers & ctrl_modifier)  { *p++ = 'C'; *p++ = '-'; }
@@ -6336,7 +6277,7 @@ parse_modifiers (Lisp_Object symbol)
                            Qnil);
 
       if (modifiers & ~INTMASK)
-       abort ();
+       emacs_abort ();
       XSETFASTINT (mask, modifiers);
       elements = Fcons (unmodified, Fcons (mask, Qnil));
 
@@ -6781,41 +6722,10 @@ get_input_pending (int *addr, int flags)
     return;
 
   /* Try to read some input and see how much we get.  */
-  gobble_input (0);
+  gobble_input ();
   *addr = (!NILP (Vquit_flag) || readable_events (flags));
 }
 
-/* Interface to read_avail_input, blocking SIGIO or SIGALRM if necessary.  */
-
-void
-gobble_input (int expected)
-{
-#ifdef SIGIO
-  if (interrupt_input)
-    {
-      SIGMASKTYPE mask;
-      mask = sigblock (sigmask (SIGIO));
-      read_avail_input (expected);
-      sigsetmask (mask);
-    }
-  else
-#ifdef POLL_FOR_INPUT
-  /* XXX This condition was (read_socket_hook && !interrupt_input),
-     but read_socket_hook is not global anymore.  Let's pretend that
-     it's always set.  */
-  if (!interrupt_input && poll_suppress_count == 0)
-    {
-      SIGMASKTYPE mask;
-      mask = sigblock (sigmask (SIGALRM));
-      read_avail_input (expected);
-      sigsetmask (mask);
-    }
-  else
-#endif
-#endif
-    read_avail_input (expected);
-}
-
 /* Put a BUFFER_SWITCH_EVENT in the buffer
    so that read_key_sequence will notice the new current buffer.  */
 
@@ -6841,14 +6751,9 @@ record_asynch_buffer_change (void)
     return;
 
   /* Make sure no interrupt happens while storing the event.  */
-#ifdef SIGIO
+#ifdef USABLE_SIGIO
   if (interrupt_input)
-    {
-      SIGMASKTYPE mask;
-      mask = sigblock (sigmask (SIGIO));
-      kbd_buffer_store_event (&event);
-      sigsetmask (mask);
-    }
+    kbd_buffer_store_event (&event);
   else
 #endif
     {
@@ -6861,21 +6766,18 @@ record_asynch_buffer_change (void)
 /* Read any terminal input already buffered up by the system
    into the kbd_buffer, but do not wait.
 
-   EXPECTED should be nonzero if the caller knows there is some input.
-
-   Returns the number of keyboard chars read, or -1 meaning
+   Return the number of keyboard chars read, or -1 meaning
    this is a bad time to try to read input.  */
 
-static int
-read_avail_input (int expected)
+int
+gobble_input (void)
 {
   int nread = 0;
   int err = 0;
   struct terminal *t;
 
   /* Store pending user signal events, if any.  */
-  if (store_user_signal_events ())
-    expected = 0;
+  store_user_signal_events ();
 
   /* Loop through the available terminals, and call their input hooks.  */
   t = terminal_list;
@@ -6888,15 +6790,18 @@ read_avail_input (int expected)
           int nr;
           struct input_event hold_quit;
 
+         if (input_blocked_p ())
+           {
+             pending_signals = 1;
+             break;
+           }
+
           EVENT_INIT (hold_quit);
           hold_quit.kind = NO_EVENT;
 
           /* No need for FIONREAD or fcntl; just say don't wait.  */
-          while (nr = (*t->read_socket_hook) (t, expected, &hold_quit), nr > 0)
-            {
-              nread += nr;
-              expected = 0;
-            }
+         while (0 < (nr = (*t->read_socket_hook) (t, &hold_quit)))
+           nread += nr;
 
           if (nr == -1)          /* Not OK to read input now.  */
             {
@@ -6916,7 +6821,7 @@ read_avail_input (int expected)
                    this process rather than to the whole process
                    group?  Perhaps on systems with FIONREAD Emacs is
                    alone in its group.  */
-                kill (getpid (), SIGHUP);
+               terminate_due_to_signal (SIGHUP, 10);
 
               /* XXX Is calling delete_terminal safe here?  It calls delete_frame.  */
              {
@@ -6991,7 +6896,6 @@ decode_keyboard_code (struct tty_display_info *tty,
 
 int
 tty_read_avail_input (struct terminal *terminal,
-                      int expected,
                       struct input_event *hold_quit)
 {
   /* Using KBD_BUFFER_SIZE - 1 here avoids reading more than
@@ -7013,7 +6917,7 @@ tty_read_avail_input (struct terminal *terminal,
 
   if (terminal->type != output_termcap
       && terminal->type != output_msdos_raw)
-    abort ();
+    emacs_abort ();
 
   /* XXX I think the following code should be moved to separate hook
      functions in system-dependent files.  */
@@ -7065,7 +6969,7 @@ tty_read_avail_input (struct terminal *terminal,
 #endif /* HAVE_GPM */
 
 /* Determine how many characters we should *try* to read.  */
-#ifdef FIONREAD
+#ifdef USABLE_FIONREAD
   /* Find out how much input is available.  */
   if (ioctl (fileno (tty->input), FIONREAD, &n_to_read) < 0)
     {
@@ -7078,14 +6982,12 @@ tty_read_avail_input (struct terminal *terminal,
     return 0;
   if (n_to_read > sizeof cbuf)
     n_to_read = sizeof cbuf;
-#else /* no FIONREAD */
-#if defined (USG) || defined (CYGWIN)
+#elif defined USG || defined CYGWIN
   /* Read some input if available, but don't wait.  */
   n_to_read = sizeof cbuf;
   fcntl (fileno (tty->input), F_SETFL, O_NDELAY);
 #else
-  you lose;
-#endif
+# error "Cannot read without possibly delaying"
 #endif
 
 #ifdef subprocesses
@@ -7134,7 +7036,7 @@ tty_read_avail_input (struct terminal *terminal,
 #endif
          );
 
-#ifndef FIONREAD
+#ifndef USABLE_FIONREAD
 #if defined (USG) || defined (CYGWIN)
   fcntl (fileno (tty->input), F_SETFL, 0);
 #endif /* USG or CYGWIN */
@@ -7205,85 +7107,83 @@ tty_read_avail_input (struct terminal *terminal,
   return nread;
 }
 \f
-#if defined SYNC_INPUT || defined SIGIO
 static void
 handle_async_input (void)
 {
-  interrupt_input_pending = 0;
-#ifdef SYNC_INPUT
-  pending_signals = pending_atimers;
-#endif
-/* Tell ns_read_socket() it is being called asynchronously so it can avoid
-   doing anything dangerous.  */
-#ifdef HAVE_NS
-  ++handling_signal;
-#endif
+#ifdef USABLE_SIGIO
   while (1)
     {
-      int nread;
-      nread = read_avail_input (1);
+      int nread = gobble_input ();
       /* -1 means it's not ok to read the input now.
         UNBLOCK_INPUT will read it later; now, avoid infinite loop.
         0 means there was no keyboard input available.  */
       if (nread <= 0)
        break;
     }
-#ifdef HAVE_NS
-  --handling_signal;
 #endif
 }
-#endif /* SYNC_INPUT || SIGIO */
 
-#ifdef SYNC_INPUT
 void
 process_pending_signals (void)
 {
-  if (interrupt_input_pending)
-    handle_async_input ();
+  pending_signals = 0;
+  handle_async_input ();
   do_pending_atimers ();
 }
-#endif
 
-#ifdef SIGIO   /* for entire page */
-/* Note SIGIO has been undef'd if FIONREAD is missing.  */
+/* Undo any number of BLOCK_INPUT calls down to level LEVEL,
+   and also (if the level is now 0) reinvoke any pending signal.  */
 
-static void
-input_available_signal (int signo)
+void
+unblock_input_to (int level)
 {
-  /* Must preserve main program's value of errno.  */
-  int old_errno = errno;
-  SIGNAL_THREAD_CHECK (signo);
+  interrupt_input_blocked = level;
+  if (level == 0)
+    {
+      if (pending_signals)
+       process_pending_signals ();
+    }
+  else if (level < 0)
+    emacs_abort ();
+}
 
-#ifdef SYNC_INPUT
-  interrupt_input_pending = 1;
-  pending_signals = 1;
-#endif
+/* End critical section.
 
-  if (input_available_clear_time)
-    *input_available_clear_time = make_emacs_time (0, 0);
+   If doing signal-driven input, and a signal came in when input was
+   blocked, reinvoke the signal handler now to deal with it.  */
 
-#ifndef SYNC_INPUT
-  handle_async_input ();
-#endif
+void
+unblock_input (void)
+{
+  unblock_input_to (interrupt_input_blocked - 1);
+}
+
+/* Undo any number of BLOCK_INPUT calls,
+   and also reinvoke any pending signal.  */
 
-  errno = old_errno;
+void
+totally_unblock_input (void)
+{
+  unblock_input_to (0);
 }
-#endif /* SIGIO */
 
-/* Send ourselves a SIGIO.
+#ifdef USABLE_SIGIO
 
-   This function exists so that the UNBLOCK_INPUT macro in
-   blockinput.h can have some way to take care of input we put off
-   dealing with, without assuming that every file which uses
-   UNBLOCK_INPUT also has #included the files necessary to get SIGIO.  */
 void
-reinvoke_input_signal (void)
+handle_input_available_signal (int sig)
 {
-#ifdef SIGIO
-  handle_async_input ();
-#endif
+  pending_signals = 1;
+
+  if (input_available_clear_time)
+    *input_available_clear_time = make_emacs_time (0, 0);
 }
 
+static void
+deliver_input_available_signal (int sig)
+{
+  deliver_process_signal (sig, handle_input_available_signal);
+}
+#endif /* USABLE_SIGIO */
 
 \f
 /* User signal events.  */
@@ -7308,6 +7208,7 @@ static struct user_signal_info *user_signals = NULL;
 void
 add_user_signal (int sig, const char *name)
 {
+  struct sigaction action;
   struct user_signal_info *p;
 
   for (p = user_signals; p; p = p->next)
@@ -7322,18 +7223,16 @@ add_user_signal (int sig, const char *name)
   p->next = user_signals;
   user_signals = p;
 
-  signal (sig, handle_user_signal);
+  emacs_sigaction_init (&action, deliver_user_signal);
+  sigaction (sig, &action, 0);
 }
 
 static void
 handle_user_signal (int sig)
 {
-  int old_errno = errno;
   struct user_signal_info *p;
   const char *special_event_name = NULL;
 
-  SIGNAL_THREAD_CHECK (sig);
-
   if (SYMBOLP (Vdebug_on_event))
     special_event_name = SSDATA (SYMBOL_NAME (Vdebug_on_event));
 
@@ -7354,9 +7253,9 @@ handle_user_signal (int sig)
           }
 
        p->npending++;
-#ifdef SIGIO
+#ifdef USABLE_SIGIO
        if (interrupt_input)
-         kill (getpid (), SIGIO);
+         handle_input_available_signal (sig);
        else
 #endif
          {
@@ -7367,8 +7266,12 @@ handle_user_signal (int sig)
          }
        break;
       }
+}
 
-  errno = old_errno;
+static void
+deliver_user_signal (int sig)
+{
+  deliver_process_signal (sig, handle_user_signal);
 }
 
 static char *
@@ -7383,27 +7286,24 @@ find_user_signal_name (int sig)
   return NULL;
 }
 
-static int
+static void
 store_user_signal_events (void)
 {
   struct user_signal_info *p;
   struct input_event buf;
-  int nstored = 0;
+  bool buf_initialized = 0;
 
   for (p = user_signals; p; p = p->next)
     if (p->npending > 0)
       {
-       SIGMASKTYPE mask;
-
-       if (nstored == 0)
+       if (! buf_initialized)
          {
            memset (&buf, 0, sizeof buf);
            buf.kind = USER_SIGNAL_EVENT;
            buf.frame_or_window = selected_frame;
+           buf_initialized = 1;
          }
-       nstored += p->npending;
 
-       mask = sigblock (sigmask (p->sig));
        do
          {
            buf.code = p->sig;
@@ -7411,10 +7311,7 @@ store_user_signal_events (void)
            p->npending--;
          }
        while (p->npending > 0);
-       sigsetmask (mask);
       }
-
-  return nstored;
 }
 
 \f
@@ -8456,7 +8353,7 @@ append_tool_bar_item (void)
 
   /* Append entries from tool_bar_item_properties to the end of
      tool_bar_items_vector.  */
-  vcopy (tool_bar_items_vector, ntool_bar_items, 
+  vcopy (tool_bar_items_vector, ntool_bar_items,
         XVECTOR (tool_bar_item_properties)->contents, TOOL_BAR_ITEM_NSLOTS);
   ntool_bar_items += TOOL_BAR_ITEM_NSLOTS;
 }
@@ -9574,7 +9471,7 @@ read_key_sequence (Lisp_Object *keybuf, int bufsize, Lisp_Object prompt,
                         because we may get input from a subprocess which
                         wants to change the selected window and stuff (say,
                         emacsclient).  */
-                     record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
+                     record_unwind_current_buffer ();
 
                      if (! FRAME_LIVE_P (XFRAME (selected_frame)))
                        Fkill_emacs (Qnil);
@@ -10450,7 +10347,7 @@ clear_input_pending (void)
 int
 requeued_events_pending_p (void)
 {
-  return (!NILP (Vunread_command_events) || unread_command_char != -1);
+  return (!NILP (Vunread_command_events));
 }
 
 
@@ -10460,7 +10357,7 @@ Actually, the value is nil only if we can be sure that no input is available;
 if there is a doubt, the value is t.  */)
   (void)
 {
-  if (!NILP (Vunread_command_events) || unread_command_char != -1
+  if (!NILP (Vunread_command_events)
       || !NILP (Vunread_post_input_method_events)
       || !NILP (Vunread_input_method_events))
     return (Qt);
@@ -10618,9 +10515,9 @@ The file will be closed when Emacs exits.  */)
 {
   if (dribble)
     {
-      BLOCK_INPUT;
+      block_input ();
       fclose (dribble);
-      UNBLOCK_INPUT;
+      unblock_input ();
       dribble = 0;
     }
   if (!NILP (file))
@@ -10648,7 +10545,6 @@ Also end any kbd macro being defined.  */)
   update_mode_lines++;
 
   Vunread_command_events = Qnil;
-  unread_command_char = -1;
 
   discard_tty_input ();
 
@@ -10788,17 +10684,10 @@ clear_waiting_for_input (void)
    Otherwise, tell QUIT to kill Emacs.  */
 
 static void
-interrupt_signal (int signalnum)       /* If we don't have an argument, some */
-                                       /* compilers complain in signal calls.  */
+handle_interrupt_signal (int sig)
 {
-  /* Must preserve main program's value of errno.  */
-  int old_errno = errno;
-  struct terminal *terminal;
-
-  SIGNAL_THREAD_CHECK (signalnum);
-
   /* See if we have an active terminal on our controlling tty.  */
-  terminal = get_named_tty ("/dev/tty");
+  struct terminal *terminal = get_named_tty ("/dev/tty");
   if (!terminal)
     {
       /* If there are no frames there, let's pretend that we are a
@@ -10817,16 +10706,21 @@ interrupt_signal (int signalnum)      /* If we don't have an argument, some */
          from the controlling tty.  */
       internal_last_event_frame = terminal->display_info.tty->top_frame;
 
-      handle_interrupt ();
+      handle_interrupt (1);
     }
+}
 
-  errno = old_errno;
+static void
+deliver_interrupt_signal (int sig)
+{
+  deliver_process_signal (sig, handle_interrupt_signal);
 }
 
+
 /* If Emacs is stuck because `inhibit-quit' is true, then keep track
    of the number of times C-g has been requested.  If C-g is pressed
    enough times, then quit anyway.  See bug#6585.  */
-static int force_quit_count;
+static int volatile force_quit_count;
 
 /* This routine is called at interrupt level in response to C-g.
 
@@ -10840,7 +10734,7 @@ static int force_quit_count;
    non-nil, it stops the job right away.  */
 
 static void
-handle_interrupt (void)
+handle_interrupt (bool in_signal_handler)
 {
   char c;
 
@@ -10849,10 +10743,16 @@ handle_interrupt (void)
   /* XXX This code needs to be revised for multi-tty support.  */
   if (!NILP (Vquit_flag) && get_named_tty ("/dev/tty"))
     {
-      /* If SIGINT isn't blocked, don't let us be interrupted by
-        another SIGINT, it might be harmful due to non-reentrancy
-        in I/O functions.  */
-      sigblock (sigmask (SIGINT));
+      if (! in_signal_handler)
+       {
+         /* If SIGINT isn't blocked, don't let us be interrupted by
+            a SIGINT.  It might be harmful due to non-reentrancy
+            in I/O functions.  */
+         sigset_t blocked;
+         sigemptyset (&blocked);
+         sigaddset (&blocked, SIGINT);
+         pthread_sigmask (SIG_BLOCK, &blocked, 0);
+       }
 
       fflush (stdout);
       reset_all_sys_modes ();
@@ -10914,7 +10814,7 @@ handle_interrupt (void)
 #endif /* not MSDOS */
       fflush (stdout);
       if (((c = getchar ()) & ~040) == 'Y')
-       abort ();
+       emacs_abort ();
       while (c != '\n') c = getchar ();
 #ifdef MSDOS
       printf ("\r\nContinuing...\r\n");
@@ -10923,7 +10823,6 @@ handle_interrupt (void)
 #endif /* not MSDOS */
       fflush (stdout);
       init_all_sys_modes ();
-      sigfree ();
     }
   else
     {
@@ -10936,20 +10835,19 @@ handle_interrupt (void)
          struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
 
          immediate_quit = 0;
-          sigfree ();
+         pthread_sigmask (SIG_SETMASK, &empty_mask, 0);
          saved = gl_state;
          GCPRO4 (saved.object, saved.global_code,
                  saved.current_syntax_table, saved.old_prop);
          Fsignal (Qquit, Qnil);
-         /* FIXME: AFAIK, `quit' can never return, so this code is dead!  */
          gl_state = saved;
          UNGCPRO;
        }
       else
         { /* Else request quit when it's safe.  */
-          if (NILP (Vquit_flag))
-           force_quit_count = 0;
-         if (++force_quit_count == 3)
+         int count = NILP (Vquit_flag) ? 1 : force_quit_count + 1;
+         force_quit_count = count;
+         if (count == 3)
             {
               immediate_quit = 1;
               Vinhibit_quit = Qnil;
@@ -10958,6 +10856,8 @@ handle_interrupt (void)
         }
     }
 
+  pthread_sigmask (SIG_SETMASK, &empty_mask, 0);
+
 /* TODO: The longjmp in this call throws the NS event loop integration off,
          and it seems to do fine without this.  Probably some attention
         needs to be paid to the setting of waiting_for_input in
@@ -10967,7 +10867,7 @@ handle_interrupt (void)
          separate event loop thread like W32.  */
 #ifndef HAVE_NS
   if (waiting_for_input && !echoing)
-      quit_throw_to_read_char (1);
+    quit_throw_to_read_char (in_signal_handler);
 #endif
 }
 
@@ -10981,29 +10881,18 @@ quit_throw_to_read_char (int from_signal)
   if (!from_signal && EQ (Vquit_flag, Qkill_emacs))
     Fkill_emacs (Qnil);
 
-  sigfree ();
   /* Prevent another signal from doing this before we finish.  */
   clear_waiting_for_input ();
   input_pending = 0;
 
   Vunread_command_events = Qnil;
-  unread_command_char = -1;
 
-#if 0 /* Currently, sit_for is called from read_char without turning
-        off polling.  And that can call set_waiting_for_input.
-        It seems to be harmless.  */
-#ifdef POLL_FOR_INPUT
-  /* May be > 1 if in recursive minibuffer.  */
-  if (poll_suppress_count == 0)
-    abort ();
-#endif
-#endif
   if (FRAMEP (internal_last_event_frame)
       && !EQ (internal_last_event_frame, selected_frame))
     do_switch_frame (make_lispy_switch_frame (internal_last_event_frame),
                     0, 0, Qnil);
 
-  _longjmp (getcjmp, 1);
+  sys_longjmp (getcjmp, 1);
 }
 \f
 DEFUN ("set-input-interrupt-mode", Fset_input_interrupt_mode,
@@ -11016,8 +10905,7 @@ See also `current-input-mode'.  */)
   (Lisp_Object interrupt)
 {
   int new_interrupt_input;
-#ifdef SIGIO
-/* Note SIGIO has been undef'd if FIONREAD is missing.  */
+#ifdef USABLE_SIGIO
 #ifdef HAVE_X_WINDOWS
   if (x_display_list != NULL)
     {
@@ -11028,9 +10916,9 @@ See also `current-input-mode'.  */)
   else
 #endif /* HAVE_X_WINDOWS */
     new_interrupt_input = !NILP (interrupt);
-#else /* not SIGIO */
+#else /* not USABLE_SIGIO */
   new_interrupt_input = 0;
-#endif /* not SIGIO */
+#endif /* not USABLE_SIGIO */
 
   if (new_interrupt_input != interrupt_input)
     {
@@ -11251,7 +11139,7 @@ The `posn-' functions access elements of such lists.  */)
 
   if (WINDOWP (frame_or_window))
     {
-      struct window *w = decode_valid_window (frame_or_window);
+      struct window *w = decode_live_window (frame_or_window);
 
       XSETINT (x, (XINT (x)
                   + WINDOW_LEFT_EDGE_X (w)
@@ -11353,7 +11241,7 @@ delete_kboard (KBOARD *kb)
 
   for (kbp = &all_kboards; *kbp != kb; kbp = &(*kbp)->next_kboard)
     if (*kbp == NULL)
-      abort ();
+      emacs_abort ();
   *kbp = kb->next_kboard;
 
   /* Prevent a dangling reference to KB.  */
@@ -11364,7 +11252,7 @@ delete_kboard (KBOARD *kb)
       current_kboard = FRAME_KBOARD (XFRAME (selected_frame));
       single_kboard = 0;
       if (current_kboard == kb)
-       abort ();
+       emacs_abort ();
     }
 
   wipe_kboard (kb);
@@ -11374,12 +11262,11 @@ delete_kboard (KBOARD *kb)
 void
 init_keyboard (void)
 {
-  /* This is correct before outermost invocation of the editor loop */
+  /* This is correct before outermost invocation of the editor loop */
   command_loop_level = -1;
   immediate_quit = 0;
   quit_char = Ctl ('g');
   Vunread_command_events = Qnil;
-  unread_command_char = -1;
   timer_idleness_start_time = invalid_emacs_time ();
   total_keys = 0;
   recent_keys_index = 0;
@@ -11390,10 +11277,7 @@ init_keyboard (void)
 #endif
   input_pending = 0;
   interrupt_input_blocked = 0;
-  interrupt_input_pending = 0;
-#ifdef SYNC_INPUT
   pending_signals = 0;
-#endif
 
   /* This means that command_loop_1 won't try to select anything the first
      time through.  */
@@ -11413,21 +11297,26 @@ init_keyboard (void)
       /* Before multi-tty support, these handlers used to be installed
          only if the current session was a tty session.  Now an Emacs
          session may have multiple display types, so we always handle
-         SIGINT.  There is special code in interrupt_signal to exit
+         SIGINT.  There is special code in handle_interrupt_signal to exit
          Emacs on SIGINT when there are no termcap frames on the
          controlling terminal.  */
-      signal (SIGINT, interrupt_signal);
+      struct sigaction action;
+      emacs_sigaction_init (&action, deliver_interrupt_signal);
+      sigaction (SIGINT, &action, 0);
 #ifndef DOS_NT
       /* For systems with SysV TERMIO, C-g is set up for both SIGINT and
         SIGQUIT and we can't tell which one it will give us.  */
-      signal (SIGQUIT, interrupt_signal);
+      sigaction (SIGQUIT, &action, 0);
 #endif /* not DOS_NT */
     }
-/* Note SIGIO has been undef'd if FIONREAD is missing.  */
-#ifdef SIGIO
+#ifdef USABLE_SIGIO
   if (!noninteractive)
-    signal (SIGIO, input_available_signal);
-#endif /* SIGIO */
+    {
+      struct sigaction action;
+      emacs_sigaction_init (&action, deliver_input_available_signal);
+      sigaction (SIGIO, &action, 0);
+    }
+#endif
 
 /* Use interrupt input by default, if it works and noninterrupt input
    has deficiencies.  */
@@ -11438,7 +11327,7 @@ init_keyboard (void)
   interrupt_input = 0;
 #endif
 
-  sigfree ();
+  pthread_sigmask (SIG_SETMASK, &empty_mask, 0);
   dribble = 0;
 
   if (keyboard_init_hook)
@@ -11706,9 +11595,6 @@ as they will already have been added once as they were read for the first time.
 An element of the form (t . EVENT) forces EVENT to be added to that list.  */);
   Vunread_command_events = Qnil;
 
-  DEFVAR_INT ("unread-command-char", unread_command_char,
-             doc: /* If not -1, an object to be read as next command input event.  */);
-
   DEFVAR_LISP ("unread-post-input-method-events", Vunread_post_input_method_events,
               doc: /* List of events to be processed as input by input methods.
 These events are processed before `unread-command-events'