handle pending sigwinches better, etc.


git-svn-id: svn://svn.savannah.gnu.org/nano/trunk/nano@1652 35c25a1d-7b9e-4130-9fde-d3aeb78583b8
diff --git a/ChangeLog b/ChangeLog
index 50a9b35..2a31254 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -15,6 +15,11 @@
 	- Add more intuitive Meta-key aliases for moving to the
 	  beginning and ending lines of a paragraph at the search
 	  prompt: Meta-P and Meta-N. (DLR)
+	- Block SIGWINCH after setting up its handler, and only unblock
+	  and handle it when we're in a stable state, i.e, when we're
+	  waiting for input from the user.  New function
+	  allow_pending_sigwinch(); changes to signal_init(),
+	  get_kbinput(), and get_verbatim_kbinput(). (DLR)
 - files.c:
   add_open_files()
 	- Make the saving of marked status in open_files->file_flags
@@ -50,6 +55,9 @@
 	  doesn't need to be called every time through the loop.  Call it
 	  instead of cbreak() on such systems, as it overrides cbreak()
 	  anyway. (DLR)
+	- Add more descriptive comments explaining the termios and
+	  curses setup routines, and turn the keypad on before setting
+	  the input mode. (DLR)
 - search.c:
   do_replace_loop()
 	- Fix segfault when doing a regex replace of a string that
diff --git a/src/nano.c b/src/nano.c
index 050c3f0..3fee0db 100644
--- a/src/nano.c
+++ b/src/nano.c
@@ -2815,6 +2815,7 @@
 #ifndef NANO_SMALL
     act.sa_handler = handle_sigwinch;
     sigaction(SIGWINCH, &act, NULL);
+    allow_pending_sigwinch(FALSE);
 #endif
 
 #ifdef _POSIX_VDISABLE
@@ -2988,6 +2989,17 @@
     /* Jump back to the main loop. */
     siglongjmp(jmpbuf, 1);
 }
+
+void allow_pending_sigwinch(int allow)
+{
+    sigset_t winch;
+    sigemptyset(&winch);
+    sigaddset(&winch, SIGWINCH);
+    if (allow)
+	sigprocmask(SIG_UNBLOCK, &winch, NULL);
+    else
+	sigprocmask(SIG_BLOCK, &winch, NULL);
+}
 #endif /* !NANO_SMALL */
 
 /* If the NumLock key has made the keypad go awry, print an error
@@ -3419,9 +3431,11 @@
 	    filename = mallocstrcpy(filename, argv[optind]);
     }
 
-    /* First back up the old settings so they can be restored, duh */
+    /* Termios initialization stuff: Back up the old settings so that
+     * they can be restored, disable SIGINT on ^C and SIGQUIT on ^\,
+     * since we need them for Cancel and Replace, and disable
+     * implementation-defined input processing. */
     tcgetattr(0, &oldterm);
-
 #ifdef _POSIX_VDISABLE
     term = oldterm;
     term.c_cc[VINTR] = _POSIX_VDISABLE;
@@ -3430,21 +3444,32 @@
     tcsetattr(0, TCSANOW, &term);
 #endif
 
-    /* now ncurses init stuff... */
+   /* Curses initialization stuff: Start curses, save the state of the
+    * the terminal mode, disable translation of carriage return (^M)
+    * into newline (^J) so we can catch the Enter key and use ^J for
+    * Justify, turn the keypad on for the windows that read input, put
+    * the terminal in cbreak mode (read one character at a time and
+    * interpret the special control keys) if we can selectively disable
+    * the special control keys or raw mode (read one character at a
+    * time and don't interpret the special control keys) if we
+    * can't, and turn off echoing of characters as they're typed. */
     initscr();
     savetty();
     nonl();
+    keypad(edit, TRUE);
+    keypad(bottomwin, TRUE);
 #ifdef _POSIX_VDISABLE
     cbreak();
 #else
-    /* We're going to have to do it the old way, i.e, on Cygwin. */
     raw();
 #endif
     noecho();
 
-    /* Set up some global variables */
+    /* Set up the global variables and the shortcuts. */
     global_init(0);
     shortcut_init(0);
+
+    /* Set up the signal handlers. */
     signal_init();
 
 #ifdef DEBUG
@@ -3456,10 +3481,6 @@
     mouse_init();
 #endif
 
-    /* Turn the keypad on */
-    keypad(edit, TRUE);
-    keypad(bottomwin, TRUE);
-
 #ifdef DEBUG
     fprintf(stderr, "Main: bottom win\n");
 #endif
diff --git a/src/proto.h b/src/proto.h
index 7a5a49c..b09ed62 100644
--- a/src/proto.h
+++ b/src/proto.h
@@ -331,6 +331,7 @@
 RETSIGTYPE do_cont(int signal);
 #ifndef NANO_SMALL
 void handle_sigwinch(int s);
+void allow_pending_sigwinch(int allow);
 #endif
 void print_numlock_warning(void);
 #ifndef NANO_SMALL
diff --git a/src/winio.c b/src/winio.c
index fbae044..51c734a 100644
--- a/src/winio.c
+++ b/src/winio.c
@@ -45,9 +45,17 @@
 {
     int kbinput, retval;
 
+#ifndef NANO_SMALL
+    allow_pending_sigwinch(TRUE);
+#endif
+
     kbinput = get_ignored_kbinput(win);
     retval = get_accepted_kbinput(win, kbinput, meta);
 
+#ifndef NANO_SMALL
+    allow_pending_sigwinch(FALSE);
+#endif
+
     return retval;
 }
 
@@ -59,6 +67,10 @@
 {
     int kbinput, *verbatim_kbinput;
 
+#ifndef NANO_SMALL
+    allow_pending_sigwinch(TRUE);
+#endif
+
     /* Turn the keypad off so that we don't get extended keypad values,
      * all of which are outside the ASCII range, and switch to raw mode
      * so that we can type ^C, ^Q, ^S, ^Z, and ^\ (and ^Y on the Hurd)
@@ -98,6 +110,11 @@
 #ifdef DEBUG
     fprintf(stderr, "get_verbatim_kbinput(): verbatim_kbinput = %s\n", verbatim_kbinput);
 #endif
+
+#ifndef NANO_SMALL
+    allow_pending_sigwinch(FALSE);
+#endif
+
     return verbatim_kbinput;
 }