From: Thomas E. Dickey Date: Sun, 25 Dec 2016 01:53:57 +0000 (+0000) Subject: ncurses 6.0 - patch 20161224 X-Git-Tag: v6.1~57 X-Git-Url: http://ncurses.scripts.mit.edu/?p=ncurses.git;a=commitdiff_plain;h=62ca6190a9a8ddccb2c4d5ca7b2ef9f88432da65 ncurses 6.0 - patch 20161224 + correct parmeters for copywin call in _nc_Synchronize_Attributes() (patch by Leon Winter). + improve color-handling section in terminfo manual page (prompted by patch by Mihail Konev). + modify programs clear, tput and tset to pass the actual tty file descriptor to setupterm rather than the standard output, making padding work. --- diff --git a/MANIFEST b/MANIFEST index 656325d6..9e05ad10 100644 --- a/MANIFEST +++ b/MANIFEST @@ -1064,6 +1064,8 @@ ./progs/tput.c ./progs/transform.c ./progs/tset.c +./progs/tty_settings.c +./progs/tty_settings.h ./test/Makefile.in ./test/README ./test/aclocal.m4 diff --git a/NEWS b/NEWS index 8cdb035a..5bf4573a 100644 --- a/NEWS +++ b/NEWS @@ -25,7 +25,7 @@ -- sale, use or other dealings in this Software without prior written -- -- authorization. -- ------------------------------------------------------------------------------- --- $Id: NEWS,v 1.2711 2016/12/17 23:41:02 tom Exp $ +-- $Id: NEWS,v 1.2715 2016/12/24 23:45:38 tom Exp $ ------------------------------------------------------------------------------- This is a log of changes that ncurses has gone through since Zeyd started @@ -45,6 +45,15 @@ See the AUTHORS file for the corresponding full names. Changes through 1.9.9e did not credit all contributions; it is not possible to add this information. +20161224 + + correct parmeters for copywin call in _nc_Synchronize_Attributes() + (patch by Leon Winter). + + improve color-handling section in terminfo manual page (prompted by + patch by Mihail Konev). + + modify programs clear, tput and tset to pass the actual tty file + descriptor to setupterm rather than the standard output, making + padding work. + 20161217 + add tput-colorcube demo script. + add -r and -s options to tput-initc demo, to match usage in xterm. diff --git a/VERSION b/VERSION index 1e6ab168..4a84d2f9 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -5:0:9 6.0 20161217 +5:0:9 6.0 20161224 diff --git a/dist.mk b/dist.mk index e39e5852..a2e7e4ee 100644 --- a/dist.mk +++ b/dist.mk @@ -25,7 +25,7 @@ # use or other dealings in this Software without prior written # # authorization. # ############################################################################## -# $Id: dist.mk,v 1.1137 2016/12/17 16:05:02 tom Exp $ +# $Id: dist.mk,v 1.1138 2016/12/24 13:45:15 tom Exp $ # Makefile for creating ncurses distributions. # # This only needs to be used directly as a makefile by developers, but @@ -37,7 +37,7 @@ SHELL = /bin/sh # These define the major/minor/patch versions of ncurses. NCURSES_MAJOR = 6 NCURSES_MINOR = 0 -NCURSES_PATCH = 20161217 +NCURSES_PATCH = 20161224 # We don't append the patch to the version, since this only applies to releases VERSION = $(NCURSES_MAJOR).$(NCURSES_MINOR) diff --git a/doc/html/man/adacurses6-config.1.html b/doc/html/man/adacurses6-config.1.html index da09cbbe..e6ac7168 100644 --- a/doc/html/man/adacurses6-config.1.html +++ b/doc/html/man/adacurses6-config.1.html @@ -131,7 +131,7 @@

SEE ALSO

        curses(3x)
 
-       This describes ncurses version 6.0 (patch 20161203).
+       This describes ncurses version 6.0 (patch 20161224).
 
 
 
diff --git a/doc/html/man/captoinfo.1m.html b/doc/html/man/captoinfo.1m.html
index 8d9ef150..8d2b1266 100644
--- a/doc/html/man/captoinfo.1m.html
+++ b/doc/html/man/captoinfo.1m.html
@@ -198,7 +198,7 @@
 

SEE ALSO

        infocmp(1m), curses(3x), terminfo(5)
 
-       This describes ncurses version 6.0 (patch 20161203).
+       This describes ncurses version 6.0 (patch 20161224).
 
 
 

AUTHOR

diff --git a/doc/html/man/clear.1.html b/doc/html/man/clear.1.html
index a779802c..f76da466 100644
--- a/doc/html/man/clear.1.html
+++ b/doc/html/man/clear.1.html
@@ -135,7 +135,7 @@
 

SEE ALSO

        tput(1), terminfo(5)
 
-       This describes ncurses version 6.0 (patch 20161203).
+       This describes ncurses version 6.0 (patch 20161224).
 
 
 
diff --git a/doc/html/man/form.3x.html b/doc/html/man/form.3x.html
index a845821a..2d800ca9 100644
--- a/doc/html/man/form.3x.html
+++ b/doc/html/man/form.3x.html
@@ -239,7 +239,7 @@
        curses(3x) and related pages whose names begin "form_" for
        detailed descriptions of the entry points.
 
-       This describes ncurses version 6.0 (patch 20161203).
+       This describes ncurses version 6.0 (patch 20161224).
 
 
 
diff --git a/doc/html/man/infocmp.1m.html b/doc/html/man/infocmp.1m.html
index 9ea09e81..5e242700 100644
--- a/doc/html/man/infocmp.1m.html
+++ b/doc/html/man/infocmp.1m.html
@@ -478,7 +478,7 @@
 
        http://invisible-island.net/ncurses/tctest.html
 
-       This describes ncurses version 6.0 (patch 20161203).
+       This describes ncurses version 6.0 (patch 20161224).
 
 
 

AUTHOR

diff --git a/doc/html/man/infotocap.1m.html b/doc/html/man/infotocap.1m.html
index 9f623760..0f64f0d4 100644
--- a/doc/html/man/infotocap.1m.html
+++ b/doc/html/man/infotocap.1m.html
@@ -88,7 +88,7 @@
 

SEE ALSO

        curses(3x), tic(1m), infocmp(1m), terminfo(5)
 
-       This describes ncurses version 6.0 (patch 20161203).
+       This describes ncurses version 6.0 (patch 20161224).
 
 
 

AUTHOR

diff --git a/doc/html/man/menu.3x.html b/doc/html/man/menu.3x.html
index 05c9ed62..670b4d3f 100644
--- a/doc/html/man/menu.3x.html
+++ b/doc/html/man/menu.3x.html
@@ -217,7 +217,7 @@
        curses(3x) and related pages whose names begin "menu_" for
        detailed descriptions of the entry points.
 
-       This describes ncurses version 6.0 (patch 20161203).
+       This describes ncurses version 6.0 (patch 20161224).
 
 
 
diff --git a/doc/html/man/ncurses.3x.html b/doc/html/man/ncurses.3x.html
index 19952bbe..173c335b 100644
--- a/doc/html/man/ncurses.3x.html
+++ b/doc/html/man/ncurses.3x.html
@@ -60,7 +60,7 @@
        sonable optimization.  This implementation is "new curses"
        (ncurses) and is the approved replacement for 4.4BSD clas-
        sic  curses,  which has been discontinued.  This describes
-       ncurses version 6.0 (patch 20161203).
+       ncurses version 6.0 (patch 20161224).
 
        The ncurses library emulates the curses library of  System
        V  Release  4  UNIX,  and  XPG4 (X/Open Portability Guide)
diff --git a/doc/html/man/ncurses6-config.1.html b/doc/html/man/ncurses6-config.1.html
index c0539d40..46a7c97f 100644
--- a/doc/html/man/ncurses6-config.1.html
+++ b/doc/html/man/ncurses6-config.1.html
@@ -114,7 +114,7 @@
 

SEE ALSO

        curses(3x)
 
-       This describes ncurses version 6.0 (patch 20161203).
+       This describes ncurses version 6.0 (patch 20161224).
 
 
 
diff --git a/doc/html/man/panel.3x.html b/doc/html/man/panel.3x.html
index b7cd6a28..5b913c55 100644
--- a/doc/html/man/panel.3x.html
+++ b/doc/html/man/panel.3x.html
@@ -208,7 +208,7 @@
 

SEE ALSO

        curses(3x), curs_variables(3x),
 
-       This describes ncurses version 6.0 (patch 20161203).
+       This describes ncurses version 6.0 (patch 20161224).
 
 
 

AUTHOR

diff --git a/doc/html/man/tabs.1.html b/doc/html/man/tabs.1.html
index 8771059b..e3098707 100644
--- a/doc/html/man/tabs.1.html
+++ b/doc/html/man/tabs.1.html
@@ -164,7 +164,7 @@
 

SEE ALSO

        tset(1), infocmp(1m), curses(3x), terminfo(5).
 
-       This describes ncurses version 6.0 (patch 20161203).
+       This describes ncurses version 6.0 (patch 20161224).
 
 
 
diff --git a/doc/html/man/terminfo.5.html b/doc/html/man/terminfo.5.html
index 87f9e8e4..becb6219 100644
--- a/doc/html/man/terminfo.5.html
+++ b/doc/html/man/terminfo.5.html
@@ -33,7 +33,7 @@
   ****************************************************************************
   * @Id: terminfo.head,v 1.22 2016/10/15 17:02:31 tom Exp @
   * Head of terminfo man page ends here
-  * @Id: terminfo.tail,v 1.74 2016/11/05 21:15:43 Alain.Williams Exp @
+  * @Id: terminfo.tail,v 1.75 2016/12/24 22:54:11 tom Exp @
   * Beginning of terminfo.tail file
   * This file is part of ncurses.
   * See "terminfo.head" for copyright.
@@ -75,7 +75,7 @@
        nals by giving a set of capabilities which they  have,  by
        specifying how to perform screen operations, and by speci-
        fying padding requirements and  initialization  sequences.
-       This describes ncurses version 6.0 (patch 20161203).
+       This describes ncurses version 6.0 (patch 20161224).
 
        Entries in terminfo consist of a sequence of `,' separated
        fields (embedded commas may be escaped with a backslash or
@@ -2067,41 +2067,57 @@
 
 
 

Color Handling

-       Most  color  terminals are either "Tektronix-like" or "HP-
-       like".  Tektronix-like terminals have a predefined set  of
-       N  colors  (where N usually 8), and can set character-cell
-       foreground and background characters independently, mixing
-       them  into  N * N  color-pairs.  On HP-like terminals, the
-       use must set each color pair up separately (foreground and
-       background  are  not  independently  settable).   Up  to M
-       color-pairs may be  set  up  from  2*M  different  colors.
-       ANSI-compatible terminals are Tektronix-like.
+       The  curses  library  functions  init_pair  and init_color
+       manipulate the color pairs and color values  discussed  in
+       this  section (see curs_color(3x) for details on these and
+       related functions).
+
+       Most color terminals are either "Tektronix-like"  or  "HP-
+       like":
+
+       o   Tektronix-like  terminals  have  a predefined set of N
+           colors (where N is usually 8), and can set  character-
+           cell  foreground  and  background  characters indepen-
+           dently, mixing them into N * N color-pairs.
+
+       o   On HP-like terminals, the user  must  set  each  color
+           pair  up separately (foreground and background are not
+           independently settable).  Up to M color-pairs  may  be
+           set  up  from  2*M  different colors.  ANSI-compatible
+           terminals are Tektronix-like.
 
        Some basic color capabilities are independent of the color
        method.  The numeric capabilities colors and pairs specify
-       the  maximum numbers of colors and color-pairs that can be
-       displayed simultaneously.  The op (original  pair)  string
-       resets  foreground  and background colors to their default
-       values for the terminal.  The oc string resets all  colors
-       or  color-pairs  to their default values for the terminal.
-       Some terminals  (including  many  PC  terminal  emulators)
-       erase  screen  areas  with  the  current  background color
-       rather than the power-up default background; these  should
+       the maximum numbers of colors and color-pairs that can  be
+       displayed  simultaneously.   The op (original pair) string
+       resets foreground and background colors to  their  default
+       values  for the terminal.  The oc string resets all colors
+       or color-pairs to their default values for  the  terminal.
+       Some  terminals  (including  many  PC  terminal emulators)
+       erase screen  areas  with  the  current  background  color
+       rather  than the power-up default background; these should
        have the boolean capability bce.
 
-       To  change the current foreground or background color on a
-       Tektronix-type terminal, use setaf (set  ANSI  foreground)
-       and  setab  (set ANSI background) or setf (set foreground)
-       and setb (set background).  These take one parameter,  the
-       color  number.   The  SVr4  documentation  describes  only
-       setaf/setab; the XPG4 draft says  that  "If  the  terminal
-       supports ANSI escape sequences to set background and fore-
-       ground, they should be coded as setaf and  setab,  respec-
-       tively.   If  the terminal supports other escape sequences
-       to set background and foreground, they should be coded  as
-       setf and setb, respectively.  The vidputs function and the
-       refresh  functions  use  setaf  and  setab  if  they   are
-       defined."
+       While the curses library works with color pairs  (reflect-
+       ing  the  inability  of some devices to set foreground and
+       background colors independently), there are separate capa-
+       bilities for setting these features:
+
+       o   To  change  the current foreground or background color
+           on a Tektronix-type  terminal,  use  setaf  (set  ANSI
+           foreground)  and  setab  (set ANSI background) or setf
+           (set foreground) and  setb  (set  background).   These
+           take  one parameter, the color number.  The SVr4 docu-
+           mentation describes only setaf/setab; the  XPG4  draft
+           says  that  "If  the  terminal  supports  ANSI  escape
+           sequences  to  set  background  and  foreground,  they
+           should be coded as setaf and setab, respectively.
+
+       o   If the terminal supports other escape sequences to set
+           background and foreground, they  should  be  coded  as
+           setf  and  setb,  respectively.   The  vidputs and the
+           refresh functions use the setaf and setab capabilities
+           if they are defined.
 
        The  setaf/setab  and setf/setb capabilities take a single
        numeric argument each.  Argument values 0-7 of setaf/setab
@@ -2141,21 +2157,25 @@
        On an HP-like terminal, use scp with a  color-pair  number
        parameter to set which color pair is current.
 
-       On  a  Tektronix-like  terminal, the capability ccc may be
-       present to indicate that colors can be modified.   If  so,
-       the initc capability will take a color number (0 to colors
-       - 1)and three more parameters which  describe  the  color.
-       These three parameters default to being interpreted as RGB
-       (Red, Green, Blue) values.  If the boolean capability  hls
-       is present, they are instead as HLS (Hue, Lightness, Satu-
-       ration) indices.  The ranges are terminal-dependent.
-
-       On an HP-like terminal, initp may give  a  capability  for
-       changing  a  color-pair value.  It will take seven parame-
-       ters; a color-pair number (0 to max_pairs -  1),  and  two
-       triples  describing  first  background and then foreground
-       colors.  These parameters must be (Red,  Green,  Blue)  or
-       (Hue, Lightness, Saturation) depending on hls.
+       Some terminals allow the color values to be modified:
+
+       o   On  a  Tektronix-like terminal, the capability ccc may
+           be present to indicate that colors  can  be  modified.
+           If  so,  the initc capability will take a color number
+           (0 to colors  -  1)and  three  more  parameters  which
+           describe the color.  These three parameters default to
+           being interpreted as RGB (Red,  Green,  Blue)  values.
+           If  the  boolean  capability  hls is present, they are
+           instead as HLS (Hue, Lightness,  Saturation)  indices.
+           The ranges are terminal-dependent.
+
+       o   On  an  HP-like  terminal, initp may give a capability
+           for changing a color-pair value.  It will  take  seven
+           parameters;  a color-pair number (0 to max_pairs - 1),
+           and two triples describing first background  and  then
+           foreground  colors.   These  parameters  must be (Red,
+           Green, Blue) or (Hue, Lightness, Saturation) depending
+           on hls.
 
        On  some  color terminals, colors collide with highlights.
        You can register these collisions with the ncv capability.
@@ -2493,8 +2513,8 @@
 
 
 

SEE ALSO

-       tic(1m),   infocmp(1m),  curses(3x),  printf(3),  term(5).
-       term_variables(3x).
+       tic(1m),    infocmp(1m),    curses(3x),    curs_color(3x),
+       printf(3), term(5).  term_variables(3x).
 
 
 

AUTHORS

diff --git a/doc/html/man/tic.1m.html b/doc/html/man/tic.1m.html
index ddb1c450..380d6ecf 100644
--- a/doc/html/man/tic.1m.html
+++ b/doc/html/man/tic.1m.html
@@ -401,7 +401,7 @@
        infocmp(1m),   captoinfo(1m),   infotocap(1m),    toe(1m),
        curses(3x), term(5).  terminfo(5).
 
-       This describes ncurses version 6.0 (patch 20161203).
+       This describes ncurses version 6.0 (patch 20161224).
 
 
 

AUTHOR

diff --git a/doc/html/man/toe.1m.html b/doc/html/man/toe.1m.html
index 0559dcf9..5f6a9bb9 100644
--- a/doc/html/man/toe.1m.html
+++ b/doc/html/man/toe.1m.html
@@ -117,7 +117,7 @@
        tic(1m),   infocmp(1m),   captoinfo(1m),    infotocap(1m),
        curses(3x), terminfo(5).
 
-       This describes ncurses version 6.0 (patch 20161203).
+       This describes ncurses version 6.0 (patch 20161224).
 
 
 
diff --git a/doc/html/man/tput.1.html b/doc/html/man/tput.1.html
index 2e4e5595..ac789f04 100644
--- a/doc/html/man/tput.1.html
+++ b/doc/html/man/tput.1.html
@@ -441,7 +441,7 @@
        clear(1),   stty(1),   tabs(1),   tset(1),    terminfo(5),
        curs_termcap(3x).
 
-       This describes ncurses version 6.0 (patch 20161203).
+       This describes ncurses version 6.0 (patch 20161224).
 
 
 
diff --git a/doc/html/man/tset.1.html b/doc/html/man/tset.1.html
index 22004866..4c5ad2fd 100644
--- a/doc/html/man/tset.1.html
+++ b/doc/html/man/tset.1.html
@@ -387,7 +387,7 @@
        csh(1),   sh(1),   stty(1),   curs_terminfo(3x),   tty(4),
        terminfo(5), ttys(5), environ(7)
 
-       This describes ncurses version 6.0 (patch 20161203).
+       This describes ncurses version 6.0 (patch 20161224).
 
 
 
diff --git a/form/frm_driver.c b/form/frm_driver.c
index 87c1188e..66031d69 100644
--- a/form/frm_driver.c
+++ b/form/frm_driver.c
@@ -32,7 +32,7 @@
 
 #include "form.priv.h"
 
-MODULE_ID("$Id: frm_driver.c,v 1.119 2016/10/29 22:30:10 tom Exp $")
+MODULE_ID("$Id: frm_driver.c,v 1.120 2016/12/24 22:28:28 Leon.Winter Exp $")
 
 /*----------------------------------------------------------------------------
   This is the core module of the form library. It contains the majority
@@ -1275,7 +1275,8 @@ _nc_Synchronize_Attributes(FIELD *field)
 	      copywin(form->w, formwin,
 		      0, 0,
 		      field->frow, field->fcol,
-		      field->rows - 1, field->cols - 1, 0);
+		      field->frow + field->rows - 1,
+		      field->fcol + field->cols - 1, 0);
 	      wsyncup(formwin);
 	      Buffer_To_Window(field, form->w);
 	      SetStatus(field, _NEWTOP);	/* fake refresh to paint all */
diff --git a/man/terminfo.tail b/man/terminfo.tail
index 4ee27a0d..80ce6432 100644
--- a/man/terminfo.tail
+++ b/man/terminfo.tail
@@ -1,4 +1,4 @@
-.\" $Id: terminfo.tail,v 1.74 2016/11/05 21:15:43 Alain.Williams Exp $
+.\" $Id: terminfo.tail,v 1.75 2016/12/24 22:54:11 tom Exp $
 .\" Beginning of terminfo.tail file
 .\" This file is part of ncurses.
 .\" See "terminfo.head" for copyright.
@@ -1324,16 +1324,24 @@ character pairs right to left in sequence; these become the ACSC string.
 .PP
 .SS Color Handling
 .PP
-Most color terminals are either \*(``Tektronix-like\*('' or \*(``HP-like\*(''.
+The curses library functions \fBinit_pair\fP and \fBinit_color\fP
+manipulate the \fIcolor pairs\fP and \fIcolor values\fP discussed in this
+section
+(see \fBcurs_color\fP(3X) for details on these and related functions).
+.PP
+Most color terminals are either \*(``Tektronix-like\*('' or \*(``HP-like\*('':
+.bP
 Tektronix-like
-terminals have a predefined set of N colors (where N usually 8), and can set
+terminals have a predefined set of \fIN\fP colors
+(where \fIN\fP is usually 8),
+and can set
 character-cell foreground and background characters independently, mixing them
-into N\ *\ N color-pairs.
-On HP-like terminals, the use must set each color
+into \fIN\fP\ *\ \fIN\fP color-pairs.
+.bP
+On HP-like terminals, the user must set each color
 pair up separately (foreground and background are not independently settable).
-Up to M color-pairs may be set up from 2*M different colors.
-ANSI-compatible
-terminals are Tektronix-like.
+Up to \fIM\fP color-pairs may be set up from 2*\fIM\fP different colors.
+ANSI-compatible terminals are Tektronix-like.
 .PP
 Some basic color capabilities are independent of the color method.
 The numeric
@@ -1349,6 +1357,11 @@ terminal emulators) erase screen areas with the current background color rather
 than the power-up default background; these should have the boolean capability
 \fBbce\fR.
 .PP
+While the curses library works with \fIcolor pairs\fP
+(reflecting the inability of some devices to set foreground
+and background colors independently),
+there are separate capabilities for setting these features:
+.bP
 To change the current foreground or background color on a Tektronix-type
 terminal, use \fBsetaf\fR (set ANSI foreground) and \fBsetab\fR (set ANSI
 background) or \fBsetf\fR (set foreground) and \fBsetb\fR (set background).
@@ -1357,12 +1370,12 @@ The SVr4 documentation describes
 only \fBsetaf\fR/\fBsetab\fR; the XPG4 draft says that "If the terminal
 supports ANSI escape sequences to set background and foreground, they should
 be coded as \fBsetaf\fR and \fBsetab\fR, respectively.
+.bP
 If the terminal
 supports other escape sequences to set background and foreground, they should
 be coded as \fBsetf\fR and \fBsetb\fR, respectively.
-The \fBvidputs\fR
-function and the refresh functions use \fBsetaf\fR and \fBsetab\fR if they are
-defined."
+The \fBvidputs\fR and the \fBrefresh\fP functions
+use the \fBsetaf\fR and \fBsetab\fR capabilities if they are defined.
 .PP
 The \fBsetaf\fR/\fBsetab\fR and \fBsetf\fR/\fBsetb\fR capabilities take a
 single numeric argument each.
@@ -1411,6 +1424,8 @@ otherwise red/blue will be interchanged on the display.
 On an HP-like terminal, use \fBscp\fR with a color-pair number parameter to set
 which color pair is current.
 .PP
+Some terminals allow the \fIcolor values\fP to be modified:
+.bP
 On a Tektronix-like terminal, the capability \fBccc\fR may be present to
 indicate that colors can be modified.
 If so, the \fBinitc\fR capability will
@@ -1422,7 +1437,7 @@ If the boolean capability \fBhls\fR is present,
 they are instead as HLS (Hue, Lightness, Saturation) indices.
 The ranges are
 terminal-dependent.
-.PP
+.bP
 On an HP-like terminal, \fBinitp\fR may give a capability for changing a
 color-pair value.
 It will take seven parameters; a color-pair number (0 to
@@ -1811,6 +1826,7 @@ files containing terminal descriptions
 \fB@TIC@\fR(1M),
 \fB@INFOCMP@\fR(1M),
 \fBcurses\fR(3X),
+\fBcurs_color\fR(3X),
 \fBprintf\fR(3),
 \fBterm\fR(\*n).
 \fBterm_variables\fR(3X).
diff --git a/ncurses/tinfo/lib_ttyflags.c b/ncurses/tinfo/lib_ttyflags.c
index 43bed353..3faa3ad8 100644
--- a/ncurses/tinfo/lib_ttyflags.c
+++ b/ncurses/tinfo/lib_ttyflags.c
@@ -1,5 +1,5 @@
 /****************************************************************************
- * Copyright (c) 1998-2012,2014 Free Software Foundation, Inc.              *
+ * Copyright (c) 1998-2014,2016 Free Software Foundation, Inc.              *
  *                                                                          *
  * Permission is hereby granted, free of charge, to any person obtaining a  *
  * copy of this software and associated documentation files (the            *
@@ -41,42 +41,42 @@
 #define CUR SP_TERMTYPE
 #endif
 
-MODULE_ID("$Id: lib_ttyflags.c,v 1.30 2014/04/26 18:47:20 juergen Exp $")
+MODULE_ID("$Id: lib_ttyflags.c,v 1.31 2016/12/24 21:41:24 tom Exp $")
 
 NCURSES_EXPORT(int)
 NCURSES_SP_NAME(_nc_get_tty_mode) (NCURSES_SP_DCLx TTY * buf)
 {
+    TERMINAL *termp = TerminalOf(SP_PARM);
     int result = OK;
 
-    if (buf == 0 || SP_PARM == 0) {
+    if (buf == 0 || termp == 0) {
 	result = ERR;
     } else {
-	TERMINAL *termp = TerminalOf(SP_PARM);
 
-	if (0 == termp) {
-	    result = ERR;
-	} else {
 #ifdef USE_TERM_DRIVER
+	if (SP_PARM != 0) {
 	    result = CallDriver_2(SP_PARM, td_sgmode, FALSE, buf);
+	} else {
+	    result = ERR;
+	}
 #else
-	    for (;;) {
-		if (GET_TTY(termp->Filedes, buf) != 0) {
-		    if (errno == EINTR)
-			continue;
-		    result = ERR;
-		}
-		break;
+	for (;;) {
+	    if (GET_TTY(termp->Filedes, buf) != 0) {
+		if (errno == EINTR)
+		    continue;
+		result = ERR;
 	    }
-#endif
+	    break;
 	}
-
-	if (result == ERR)
-	    memset(buf, 0, sizeof(*buf));
+#endif
 
 	TR(TRACE_BITS, ("_nc_get_tty_mode(%d): %s",
 			termp ? termp->Filedes : -1,
 			_nc_trace_ttymode(buf)));
     }
+    if (result == ERR && buf != 0)
+	memset(buf, 0, sizeof(*buf));
+
     return (result);
 }
 
diff --git a/ncurses/tinfo/tinfo_driver.c b/ncurses/tinfo/tinfo_driver.c
index f239b699..4a6426fc 100644
--- a/ncurses/tinfo/tinfo_driver.c
+++ b/ncurses/tinfo/tinfo_driver.c
@@ -34,6 +34,7 @@
 #include 
 #define CUR ((TERMINAL*)TCB)->type.
 #include 
+#include 		/* ospeed */
 
 #if HAVE_NANOSLEEP
 #include 
@@ -50,7 +51,7 @@
 # endif
 #endif
 
-MODULE_ID("$Id: tinfo_driver.c,v 1.41 2016/09/10 20:14:56 tom Exp $")
+MODULE_ID("$Id: tinfo_driver.c,v 1.43 2016/12/24 23:20:08 tom Exp $")
 
 /*
  * SCO defines TIOCGSIZE and the corresponding struct.  Other systems (SunOS,
@@ -113,6 +114,33 @@ drv_Name(TERMINAL_CONTROL_BLOCK * TCB)
     return "tinfo";
 }
 
+static void
+get_baudrate(TERMINAL * termp)
+{
+    int my_ospeed;
+    int result;
+    if (GET_TTY(termp->Filedes, &termp->Nttyb) == OK) {
+#ifdef TERMIOS
+	termp->Nttyb.c_oflag &= (unsigned) (~OFLAGS_TABS);
+#else
+	termp->Nttyb.sg_flags &= (unsigned) (~XTABS);
+#endif
+    }
+#ifdef USE_OLD_TTY
+    result = (int) cfgetospeed(&(termp->Nttyb));
+    my_ospeed = (NCURSES_OSPEED) _nc_ospeed(result);
+#else /* !USE_OLD_TTY */
+#ifdef TERMIOS
+    my_ospeed = (NCURSES_OSPEED) cfgetospeed(&(termp->Nttyb));
+#else
+    my_ospeed = (NCURSES_OSPEED) termp->Nttyb.sg_ospeed;
+#endif
+    result = _nc_baudrate(my_ospeed);
+#endif
+    termp->_baudrate = result;
+    ospeed = (NCURSES_OSPEED) my_ospeed;
+}
+
 #undef SETUP_FAIL
 #define SETUP_FAIL FALSE
 
@@ -166,6 +194,16 @@ drv_CanHandle(TERMINAL_CONTROL_BLOCK * TCB, const char *tname, int *errret)
     if (command_character)
 	_nc_tinfo_cmdch(termp, *command_character);
 
+    /*
+     * If an application calls setupterm() rather than initscr() or
+     * newterm(), we will not have the def_prog_mode() call in
+     * _nc_setupscreen().  Do it now anyway, so we can initialize the
+     * baudrate.
+     */
+    if (sp == 0 && NC_ISATTY(termp->Filedes)) {
+	get_baudrate(termp);
+    }
+
     if (generic_type) {
 	/*
 	 * BSD 4.3's termcap contains mis-typed "gn" for wy99.  Do a sanity
diff --git a/package/debian-mingw/changelog b/package/debian-mingw/changelog
index 89c49fe0..851aa148 100644
--- a/package/debian-mingw/changelog
+++ b/package/debian-mingw/changelog
@@ -1,8 +1,8 @@
-ncurses6 (6.0+20161217) unstable; urgency=low
+ncurses6 (6.0+20161224) unstable; urgency=low
 
   * latest weekly patch
 
- -- Thomas E. Dickey   Sat, 17 Dec 2016 11:05:02 -0500
+ -- Thomas E. Dickey   Sat, 24 Dec 2016 08:45:15 -0500
 
 ncurses6 (5.9-20131005) unstable; urgency=low
 
diff --git a/package/debian-mingw64/changelog b/package/debian-mingw64/changelog
index 89c49fe0..851aa148 100644
--- a/package/debian-mingw64/changelog
+++ b/package/debian-mingw64/changelog
@@ -1,8 +1,8 @@
-ncurses6 (6.0+20161217) unstable; urgency=low
+ncurses6 (6.0+20161224) unstable; urgency=low
 
   * latest weekly patch
 
- -- Thomas E. Dickey   Sat, 17 Dec 2016 11:05:02 -0500
+ -- Thomas E. Dickey   Sat, 24 Dec 2016 08:45:15 -0500
 
 ncurses6 (5.9-20131005) unstable; urgency=low
 
diff --git a/package/debian/changelog b/package/debian/changelog
index 64e59207..7a4e180d 100644
--- a/package/debian/changelog
+++ b/package/debian/changelog
@@ -1,8 +1,8 @@
-ncurses6 (6.0+20161217) unstable; urgency=low
+ncurses6 (6.0+20161224) unstable; urgency=low
 
   * latest weekly patch
 
- -- Thomas E. Dickey   Sat, 17 Dec 2016 11:05:02 -0500
+ -- Thomas E. Dickey   Sat, 24 Dec 2016 08:45:15 -0500
 
 ncurses6 (5.9-20120608) unstable; urgency=low
 
diff --git a/package/mingw-ncurses.nsi b/package/mingw-ncurses.nsi
index 5b10c37f..952e1a52 100644
--- a/package/mingw-ncurses.nsi
+++ b/package/mingw-ncurses.nsi
@@ -1,4 +1,4 @@
-; $Id: mingw-ncurses.nsi,v 1.189 2016/12/17 16:05:02 tom Exp $
+; $Id: mingw-ncurses.nsi,v 1.190 2016/12/24 13:45:15 tom Exp $
 
 ; TODO add examples
 ; TODO bump ABI to 6
@@ -10,7 +10,7 @@
 !define VERSION_MAJOR "6"
 !define VERSION_MINOR "0"
 !define VERSION_YYYY  "2016"
-!define VERSION_MMDD  "1217"
+!define VERSION_MMDD  "1224"
 !define VERSION_PATCH ${VERSION_YYYY}${VERSION_MMDD}
 
 !define MY_ABI   "5"
diff --git a/package/mingw-ncurses.spec b/package/mingw-ncurses.spec
index 89ae453f..4bbf4dc9 100644
--- a/package/mingw-ncurses.spec
+++ b/package/mingw-ncurses.spec
@@ -3,7 +3,7 @@
 Summary: shared libraries for terminal handling
 Name: mingw32-ncurses6
 Version: 6.0
-Release: 20161217
+Release: 20161224
 License: X11
 Group: Development/Libraries
 Source: ncurses-%{version}-%{release}.tgz
diff --git a/package/ncurses.spec b/package/ncurses.spec
index 3847651b..0c30f5af 100644
--- a/package/ncurses.spec
+++ b/package/ncurses.spec
@@ -1,7 +1,7 @@
 Summary: shared libraries for terminal handling
 Name: ncurses6
 Version: 6.0
-Release: 20161217
+Release: 20161224
 License: X11
 Group: Development/Libraries
 Source: ncurses-%{version}-%{release}.tgz
diff --git a/progs/Makefile.in b/progs/Makefile.in
index 7de8c9e5..3368684c 100644
--- a/progs/Makefile.in
+++ b/progs/Makefile.in
@@ -1,4 +1,4 @@
-# $Id: Makefile.in,v 1.99 2016/10/22 00:13:20 tom Exp $
+# $Id: Makefile.in,v 1.100 2016/12/24 17:35:01 tom Exp $
 ##############################################################################
 # Copyright (c) 1998-2015,2016 Free Software Foundation, Inc.                #
 #                                                                            #
@@ -251,7 +251,8 @@ toe$x: $(DEPS_TOE) $(DEPS_CURSES)
 
 DEPS_CLEAR = \
 	$(MODEL)/clear$o \
-	$(MODEL)/clear_cmd$o
+	$(MODEL)/clear_cmd$o \
+	$(MODEL)/tty_settings$o
 
 clear$x: $(DEPS_CLEAR) $(DEPS_CURSES)
 	@ECHO_LD@ $(LINK) $(DEPS_CLEAR) $(LDFLAGS_TINFO) -o $@
@@ -267,7 +268,8 @@ DEPS_TPUT = \
 	$(MODEL)/clear_cmd$o \
 	$(MODEL)/reset_cmd$o \
 	$(MODEL)/tparm_type$o \
-	$(MODEL)/transform$o
+	$(MODEL)/transform$o \
+	$(MODEL)/tty_settings$o
 
 tput$x: $(DEPS_TPUT) $(DEPS_CURSES) transform.h
 	@ECHO_LD@ $(LINK) $(DEPS_TPUT) $(LDFLAGS_TINFO) -o $@
@@ -282,7 +284,8 @@ infocmp$x: $(DEPS_INFOCMP) $(DEPS_CURSES)
 DEPS_TSET = \
 	$(MODEL)/tset$o \
 	$(MODEL)/reset_cmd$o \
-	$(MODEL)/transform$o
+	$(MODEL)/transform$o \
+	$(MODEL)/tty_settings$o
 
 tset$x: $(DEPS_TSET) $(DEPS_CURSES) transform.h
 	@ECHO_LD@ $(LINK) $(DEPS_TSET) $(LDFLAGS_TINFO) -o $@
diff --git a/progs/clear.c b/progs/clear.c
index e9203a29..559d296c 100644
--- a/progs/clear.c
+++ b/progs/clear.c
@@ -36,17 +36,27 @@
  * clear.c --  clears the terminal's screen
  */
 
-#define USE_LIBTINFO 1
+#define USE_LIBTINFO
 #include 
+#include 
 
-MODULE_ID("$Id: clear.c,v 1.15 2016/10/23 00:36:36 tom Exp $")
+MODULE_ID("$Id: clear.c,v 1.17 2016/12/24 19:33:39 tom Exp $")
+
+const char *_nc_progname = "clear";
 
 int
 main(
 	int argc GCC_UNUSED,
 	char *argv[]GCC_UNUSED)
 {
-    setupterm((char *) 0, STDOUT_FILENO, (int *) 0);
+    TTY tty_settings;
+    int fd;
+
+    _nc_progname = _nc_rootname(argv[0]);
+
+    fd = save_tty_settings(&tty_settings);
+
+    setupterm((char *) 0, fd, (int *) 0);
 
     ExitProgram((clear_cmd() == ERR)
 		? EXIT_FAILURE
diff --git a/progs/modules b/progs/modules
index f84d5df2..dc7d61cf 100644
--- a/progs/modules
+++ b/progs/modules
@@ -1,4 +1,4 @@
-# $Id: modules,v 1.20 2016/10/21 23:15:41 tom Exp $
+# $Id: modules,v 1.21 2016/12/24 17:33:59 tom Exp $
 # Program modules (some are in ncurses lib!)
 ##############################################################################
 # Copyright (c) 1998-2014,2016 Free Software Foundation, Inc.                #
@@ -32,17 +32,18 @@
 #
 
 @ base
-clear		progs		$(srcdir)	$(HEADER_DEPS)
+clear		progs		$(srcdir)	$(HEADER_DEPS) tty_settings.h
 tic		progs		$(srcdir)	$(HEADER_DEPS) transform.h $(srcdir)/dump_entry.h $(srcdir)/tparm_type.h
 toe		progs		$(srcdir)	$(HEADER_DEPS)             $(INCDIR)/hashed_db.h
 clear_cmd	progs		$(srcdir)	$(HEADER_DEPS) clear_cmd.h
 dump_entry	progs		$(srcdir)	$(HEADER_DEPS)             $(srcdir)/dump_entry.h ../include/parametrized.h $(INCDIR)/capdefaults.c termsort.c
 infocmp		progs		$(srcdir)	$(HEADER_DEPS)             $(srcdir)/dump_entry.h
-reset_cmd	progs		$(srcdir)	$(HEADER_DEPS) reset_cmd.h
+reset_cmd	progs		$(srcdir)	$(HEADER_DEPS) reset_cmd.h tty_settings.h
 tabs		progs		$(srcdir)	$(HEADER_DEPS)
 tparm_type	progs		$(srcdir)	$(HEADER_DEPS)             $(srcdir)/tparm_type.h
-tput		progs		$(srcdir)	$(HEADER_DEPS) transform.h $(srcdir)/dump_entry.h $(srcdir)/tparm_type.h termsort.c reset_cmd.h
-tset		progs		$(srcdir)	$(HEADER_DEPS) transform.h $(srcdir)/dump_entry.h ../include/termcap.h reset_cmd.h 
+tput		progs		$(srcdir)	$(HEADER_DEPS) transform.h $(srcdir)/dump_entry.h $(srcdir)/tparm_type.h termsort.c reset_cmd.h tty_settings.h
+tset		progs		$(srcdir)	$(HEADER_DEPS) transform.h $(srcdir)/dump_entry.h ../include/termcap.h reset_cmd.h  tty_settings.h
 transform	progs		$(srcdir)	$(HEADER_DEPS) transform.h
+tty_settings	progs		$(srcdir)	$(HEADER_DEPS) tty_settings.h
 
 # vile:makemode
diff --git a/progs/reset_cmd.c b/progs/reset_cmd.c
index e0831e27..8614bf78 100644
--- a/progs/reset_cmd.c
+++ b/progs/reset_cmd.c
@@ -31,6 +31,7 @@
  ****************************************************************************/
 
 #include 
+#include 
 
 #include 
 #include 
@@ -51,7 +52,7 @@
 #include 
 #endif
 
-MODULE_ID("$Id: reset_cmd.c,v 1.9 2016/10/23 01:08:11 tom Exp $")
+MODULE_ID("$Id: reset_cmd.c,v 1.11 2016/12/24 23:20:57 tom Exp $")
 
 /*
  * SCO defines TIOCGSIZE and the corresponding struct.  Other systems (SunOS,
@@ -73,11 +74,8 @@ MODULE_ID("$Id: reset_cmd.c,v 1.9 2016/10/23 01:08:11 tom Exp $")
 # endif
 #endif
 
-static int my_fd;
 static FILE *my_file;
-static TTY original_settings;
 
-static bool can_restore = FALSE;
 static bool use_reset = FALSE;	/* invoked as reset */
 static bool use_init = FALSE;	/* invoked as init */
 
@@ -104,17 +102,6 @@ failed(const char *msg)
     /* NOTREACHED */
 }
 
-static bool
-get_tty_settings(int fd, TTY * tty_settings)
-{
-    bool success = TRUE;
-    my_fd = fd;
-    if (fd < 0 || GET_TTY(my_fd, tty_settings) < 0) {
-	success = FALSE;
-    }
-    return success;
-}
-
 static bool
 cat_file(char *file)
 {
@@ -215,13 +202,9 @@ out_char(int c)
  * a child program dies in raw mode.
  */
 void
-reset_tty_settings(TTY * tty_settings)
+reset_tty_settings(int fd, TTY * tty_settings)
 {
-#ifdef TERMIOS
-    tcgetattr(my_fd, tty_settings);
-#else
-    stty(my_fd, tty_settings);
-#endif
+    GET_TTY(fd, tty_settings);
 
 #ifdef TERMIOS
 #if defined(VDISCARD) && defined(CDISCARD)
@@ -355,7 +338,7 @@ reset_tty_settings(TTY * tty_settings)
 	);
 #endif
 
-    SET_TTY(my_fd, tty_settings);
+    SET_TTY(fd, tty_settings);
 }
 
 /*
@@ -487,7 +470,7 @@ sent_string(const char *s)
 
 /* Output startup string. */
 bool
-send_init_strings(TTY * old_settings)
+send_init_strings(int fd GCC_UNUSED, TTY * old_settings)
 {
     int i;
     bool need_flush = FALSE;
@@ -497,7 +480,7 @@ send_init_strings(TTY * old_settings)
     if (old_settings != 0 &&
 	old_settings->c_oflag & (TAB3 | ONLCR | OCRNL | ONLRET)) {
 	old_settings->c_oflag &= (TAB3 | ONLCR | OCRNL | ONLRET);
-	SET_TTY(my_fd, old_settings);
+	SET_TTY(fd, old_settings);
     }
 #endif
     if (use_reset || use_init) {
@@ -626,40 +609,6 @@ print_tty_chars(TTY * old_settings, TTY * new_settings)
     show_tty_change(old_settings, new_settings, "Interrupt", VINTR, CINTR);
 }
 
-/*
- * Open a file descriptor on the current terminal, to obtain its settings.
- * stderr is less likely to be redirected than stdout; try that first.
- */
-int
-save_tty_settings(TTY * tty_settings)
-{
-    if (!get_tty_settings(STDERR_FILENO, tty_settings) &&
-	!get_tty_settings(STDOUT_FILENO, tty_settings) &&
-	!get_tty_settings(STDIN_FILENO, tty_settings) &&
-	!get_tty_settings(open("/dev/tty", O_RDWR), tty_settings)) {
-	failed("terminal attributes");
-    }
-    can_restore = TRUE;
-    original_settings = *tty_settings;
-    return my_fd;
-}
-
-void
-restore_tty_settings(void)
-{
-    if (can_restore)
-	SET_TTY(my_fd, &original_settings);
-}
-
-/* Set the modes if they've changed. */
-void
-update_tty_settings(TTY * old_settings, TTY * new_settings)
-{
-    if (memcmp(new_settings, old_settings, sizeof(TTY))) {
-	SET_TTY(my_fd, new_settings);
-    }
-}
-
 #if HAVE_SIZECHANGE
 /*
  * Set window size if not set already, but update our copy of the values if the
diff --git a/progs/reset_cmd.h b/progs/reset_cmd.h
index 19aee023..f5426eb8 100644
--- a/progs/reset_cmd.h
+++ b/progs/reset_cmd.h
@@ -31,7 +31,7 @@
  ****************************************************************************/
 
 /*
- * $Id: reset_cmd.h,v 1.6 2016/10/22 23:34:37 tom Exp $
+ * $Id: reset_cmd.h,v 1.7 2016/12/24 18:08:48 tom Exp $
  *
  * Utility functions for resetting terminal.
  */
@@ -46,16 +46,13 @@
 #undef CTRL
 #define CTRL(x)	((x) & 0x1f)
 
-extern bool send_init_strings(TTY * /* old_settings */);
-extern int save_tty_settings(TTY * /* tty_settings */);
+extern bool send_init_strings(int /* fd */, TTY * /* old_settings */);
 extern void print_tty_chars(TTY * /* old_settings */, TTY * /* new_settings */);
 extern void reset_flush(void);
 extern void reset_start(FILE * /* fp */, bool /* is_reset */, bool /* is_init */ );
-extern void reset_tty_settings(TTY * /* tty_settings */);
-extern void restore_tty_settings(void);
+extern void reset_tty_settings(int /* fd */, TTY * /* tty_settings */);
 extern void set_control_chars(TTY * /* tty_settings */, int /* erase */, int /* intr */, int /* kill */);
 extern void set_conversions(TTY * /* tty_settings */);
-extern void update_tty_settings(TTY * /* old_settings */, TTY * /* new_settings */);
 
 #if HAVE_SIZECHANGE
 extern void set_window_size(int /* fd */, short * /* high */, short * /* wide */);
diff --git a/progs/tput.c b/progs/tput.c
index b2d44f02..481ae635 100644
--- a/progs/tput.c
+++ b/progs/tput.c
@@ -48,8 +48,9 @@
 #include 
 #endif
 #include 
+#include 
 
-MODULE_ID("$Id: tput.c,v 1.63 2016/10/23 01:08:28 tom Exp $")
+MODULE_ID("$Id: tput.c,v 1.65 2016/12/24 18:44:32 tom Exp $")
 
 #define PUTS(s)		fputs(s, stdout)
 
@@ -117,7 +118,7 @@ exit_code(int token, int value)
 }
 
 static int
-tput_cmd(int argc, char *argv[])
+tput_cmd(int fd, TTY * saved_settings, int argc, char *argv[])
 {
     NCURSES_CONST char *name;
     char *s;
@@ -128,29 +129,27 @@ tput_cmd(int argc, char *argv[])
 
     name = check_aliases(argv[0], FALSE);
     if (is_reset || is_init) {
-	TTY mode, oldmode;
+	TTY oldmode;
 
 	int terasechar = -1;	/* new erase character */
 	int intrchar = -1;	/* new interrupt character */
 	int tkillchar = -1;	/* new kill character */
 
-	int my_fd = save_tty_settings(&mode);
-
 	reset_start(stdout, is_reset, is_init);
-	reset_tty_settings(&mode);
+	reset_tty_settings(fd, saved_settings);
 
 #if HAVE_SIZECHANGE
-	set_window_size(my_fd, &lines, &columns);
+	set_window_size(fd, &lines, &columns);
 #else
-	(void) my_fd;
+	(void) fd;
 #endif
-	set_control_chars(&mode, terasechar, intrchar, tkillchar);
-	set_conversions(&mode);
-	if (send_init_strings(&oldmode)) {
+	set_control_chars(saved_settings, terasechar, intrchar, tkillchar);
+	set_conversions(saved_settings);
+	if (send_init_strings(fd, &oldmode)) {
 	    reset_flush();
 	}
 
-	update_tty_settings(&oldmode, &mode);
+	update_tty_settings(&oldmode, saved_settings);
 	return 0;
     }
 
@@ -262,6 +261,8 @@ main(int argc, char **argv)
     int c;
     char buf[BUFSIZ];
     int result = 0;
+    int fd;
+    TTY tty_settings;
 
     prg_name = check_aliases(_nc_rootname(argv[0]), TRUE);
 
@@ -302,13 +303,15 @@ main(int argc, char **argv)
     if (term == 0 || *term == '\0')
 	quit(2, "No value for $TERM and no -T specified");
 
-    if (setupterm(term, STDOUT_FILENO, &errret) != OK && errret <= 0)
+    fd = save_tty_settings(&tty_settings);
+
+    if (setupterm(term, fd, &errret) != OK && errret <= 0)
 	quit(3, "unknown terminal \"%s\"", term);
 
     if (cmdline) {
 	if ((argc <= 0) && !(is_clear || is_reset || is_init))
 	    usage();
-	ExitProgram(tput_cmd(argc, argv));
+	ExitProgram(tput_cmd(fd, &tty_settings, argc, argv));
     }
 
     while (fgets(buf, sizeof(buf), stdin) != 0) {
@@ -329,7 +332,7 @@ main(int argc, char **argv)
 	argvec[argnum] = 0;
 
 	if (argnum != 0
-	    && tput_cmd(argnum, argvec) != 0) {
+	    && tput_cmd(fd, &tty_settings, argnum, argvec) != 0) {
 	    if (result == 0)
 		result = 4;	/* will return value >4 */
 	    ++result;
diff --git a/progs/tset.c b/progs/tset.c
index f21cb6d7..d3007e95 100644
--- a/progs/tset.c
+++ b/progs/tset.c
@@ -88,6 +88,7 @@
 #include 
 #include 
 #include 
+#include 
 
 #if HAVE_GETTTYNAM && HAVE_TTYENT_H
 #include 
@@ -96,7 +97,7 @@
 char *ttyname(int fd);
 #endif
 
-MODULE_ID("$Id: tset.c,v 1.113 2016/10/22 23:34:47 tom Exp $")
+MODULE_ID("$Id: tset.c,v 1.115 2016/12/24 18:46:42 tom Exp $")
 
 #ifndef environ
 extern char **environ;
@@ -627,7 +628,7 @@ get_termcap_entry(int fd, char *userarg)
 	    ttype = askuser(0);
     }
     /* Find the terminfo entry.  If it doesn't exist, ask the user. */
-    while (setupterm((NCURSES_CONST char *) ttype, STDOUT_FILENO, &errret)
+    while (setupterm((NCURSES_CONST char *) ttype, fd, &errret)
 	   != OK) {
 	if (errret == 0) {
 	    (void) fprintf(stderr, "%s: unknown terminal type %s\n",
@@ -834,7 +835,7 @@ main(int argc, char **argv)
 
     if (same_program(_nc_progname, PROG_RESET)) {
 	reset_start(stderr, TRUE, FALSE);
-	reset_tty_settings(&mode);
+	reset_tty_settings(my_fd, &mode);
     } else {
 	reset_start(stderr, FALSE, FALSE);
     }
@@ -852,7 +853,7 @@ main(int argc, char **argv)
 	    set_conversions(&mode);
 
 	    if (!noinit) {
-		if (send_init_strings(&oldmode)) {
+		if (send_init_strings(my_fd, &oldmode)) {
 		    (void) putc('\r', stderr);
 		    (void) fflush(stderr);
 		    (void) napms(1000);		/* Settle the terminal. */
diff --git a/progs/tty_settings.c b/progs/tty_settings.c
new file mode 100644
index 00000000..93d9e2ab
--- /dev/null
+++ b/progs/tty_settings.c
@@ -0,0 +1,109 @@
+/****************************************************************************
+ * Copyright (c) 2016 Free Software Foundation, Inc.                        *
+ *                                                                          *
+ * Permission is hereby granted, free of charge, to any person obtaining a  *
+ * copy of this software and associated documentation files (the            *
+ * "Software"), to deal in the Software without restriction, including      *
+ * without limitation the rights to use, copy, modify, merge, publish,      *
+ * distribute, distribute with modifications, sublicense, and/or sell       *
+ * copies of the Software, and to permit persons to whom the Software is    *
+ * furnished to do so, subject to the following conditions:                 *
+ *                                                                          *
+ * The above copyright notice and this permission notice shall be included  *
+ * in all copies or substantial portions of the Software.                   *
+ *                                                                          *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
+ * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
+ * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
+ * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
+ * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
+ * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
+ *                                                                          *
+ * Except as contained in this notice, the name(s) of the above copyright   *
+ * holders shall not be used in advertising or otherwise to promote the     *
+ * sale, use or other dealings in this Software without prior written       *
+ * authorization.                                                           *
+ ****************************************************************************/
+
+/****************************************************************************
+ *  Author: Thomas E. Dickey                                                *
+ ****************************************************************************/
+
+#define USE_LIBTINFO
+#include 
+
+#include 
+
+MODULE_ID("$Id: tty_settings.c,v 1.2 2016/12/24 19:31:11 tom Exp $")
+
+static int my_fd;
+static TTY original_settings;
+static bool can_restore = FALSE;
+
+static void
+exit_error(void)
+{
+    restore_tty_settings();
+    (void) fprintf(stderr, "\n");
+    ExitProgram(EXIT_FAILURE);
+    /* NOTREACHED */
+}
+
+static void
+failed(const char *msg)
+{
+    char temp[BUFSIZ];
+
+    _nc_STRCPY(temp, _nc_progname, sizeof(temp));
+    _nc_STRCAT(temp, ": ", sizeof(temp));
+    _nc_STRNCAT(temp, msg, sizeof(temp), sizeof(temp) - strlen(temp) - 2);
+    perror(temp);
+    exit_error();
+    /* NOTREACHED */
+}
+
+static bool
+get_tty_settings(int fd, TTY * tty_settings)
+{
+    bool success = TRUE;
+    my_fd = fd;
+    if (fd < 0 || GET_TTY(my_fd, tty_settings) < 0) {
+	success = FALSE;
+    }
+    return success;
+}
+
+/*
+ * Open a file descriptor on the current terminal, to obtain its settings.
+ * stderr is less likely to be redirected than stdout; try that first.
+ */
+int
+save_tty_settings(TTY * tty_settings)
+{
+    if (!get_tty_settings(STDERR_FILENO, tty_settings) &&
+	!get_tty_settings(STDOUT_FILENO, tty_settings) &&
+	!get_tty_settings(STDIN_FILENO, tty_settings) &&
+	!get_tty_settings(open("/dev/tty", O_RDWR), tty_settings)) {
+	failed("terminal attributes");
+    }
+    can_restore = TRUE;
+    original_settings = *tty_settings;
+    return my_fd;
+}
+
+void
+restore_tty_settings(void)
+{
+    if (can_restore)
+	SET_TTY(my_fd, &original_settings);
+}
+
+/* Set the modes if they've changed. */
+void
+update_tty_settings(TTY * old_settings, TTY * new_settings)
+{
+    if (memcmp(new_settings, old_settings, sizeof(TTY))) {
+	SET_TTY(my_fd, new_settings);
+    }
+}
diff --git a/progs/tty_settings.h b/progs/tty_settings.h
new file mode 100644
index 00000000..1104291e
--- /dev/null
+++ b/progs/tty_settings.h
@@ -0,0 +1,50 @@
+/****************************************************************************
+ * Copyright (c) 2016 Free Software Foundation, Inc.                        *
+ *                                                                          *
+ * Permission is hereby granted, free of charge, to any person obtaining a  *
+ * copy of this software and associated documentation files (the            *
+ * "Software"), to deal in the Software without restriction, including      *
+ * without limitation the rights to use, copy, modify, merge, publish,      *
+ * distribute, distribute with modifications, sublicense, and/or sell       *
+ * copies of the Software, and to permit persons to whom the Software is    *
+ * furnished to do so, subject to the following conditions:                 *
+ *                                                                          *
+ * The above copyright notice and this permission notice shall be included  *
+ * in all copies or substantial portions of the Software.                   *
+ *                                                                          *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
+ * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
+ * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
+ * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
+ * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
+ * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
+ *                                                                          *
+ * Except as contained in this notice, the name(s) of the above copyright   *
+ * holders shall not be used in advertising or otherwise to promote the     *
+ * sale, use or other dealings in this Software without prior written       *
+ * authorization.                                                           *
+ ****************************************************************************/
+
+/****************************************************************************
+ *  Author: Thomas E Dickey                                                 *
+ ****************************************************************************/
+
+/*
+ * $Id: tty_settings.h,v 1.1 2016/12/24 18:17:44 tom Exp $
+ *
+ * Utility functions for saving/restoring terminal settings.
+ */
+#ifndef TTY_SETTINGS_H
+#define TTY_SETTINGS_H 1
+/* *INDENT-OFF* */
+
+#include 
+
+extern int save_tty_settings(TTY * /* tty_settings */ );
+extern void restore_tty_settings(void);
+extern void update_tty_settings(TTY * /* old_settings */, TTY * /* new_settings */ );
+
+/* *INDENT-ON* */
+
+#endif /* TTY_SETTINGS_H */