ncurses vendor branch: Bring in additional source files
[dragonfly.git] / contrib / ncurses / ncurses / tinfo / setbuf.c
1 /****************************************************************************
2  * Copyright (c) 1998-2009,2010 Free Software Foundation, Inc.              *
3  *                                                                          *
4  * Permission is hereby granted, free of charge, to any person obtaining a  *
5  * copy of this software and associated documentation files (the            *
6  * "Software"), to deal in the Software without restriction, including      *
7  * without limitation the rights to use, copy, modify, merge, publish,      *
8  * distribute, distribute with modifications, sublicense, and/or sell       *
9  * copies of the Software, and to permit persons to whom the Software is    *
10  * furnished to do so, subject to the following conditions:                 *
11  *                                                                          *
12  * The above copyright notice and this permission notice shall be included  *
13  * in all copies or substantial portions of the Software.                   *
14  *                                                                          *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
16  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
18  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
19  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
20  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
21  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
22  *                                                                          *
23  * Except as contained in this notice, the name(s) of the above copyright   *
24  * holders shall not be used in advertising or otherwise to promote the     *
25  * sale, use or other dealings in this Software without prior written       *
26  * authorization.                                                           *
27  ****************************************************************************/
28
29 /****************************************************************************
30  *  Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
31  *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
32  *     and: Thomas E. Dickey                        1996-on                 *
33  *     and: Juergen Pfeifer                         2008                    *
34  ****************************************************************************/
35
36 /*
37 **      setbuf.c
38 **
39 **      Support for set_term(), reset_shell_mode(), reset_prog_mode().
40 **
41 */
42
43 #include <curses.priv.h>
44
45 MODULE_ID("$Id: setbuf.c,v 1.16 2010/08/28 21:08:31 tom Exp $")
46
47 /*
48  * If the output file descriptor is connected to a tty (the typical case) it
49  * will probably be line-buffered.  Keith Bostic pointed out that we don't want
50  * this; it hoses people running over networks by forcing out a bunch of small
51  * packets instead of one big one, so screen updates on ptys look jerky.
52  * Restore block buffering to prevent this minor lossage.
53  *
54  * The buffer size is a compromise.  Ideally we'd like a buffer that can hold
55  * the maximum possible update size (the whole screen plus cup commands to
56  * change lines as it's painted).  On a 66-line xterm this can become
57  * excessive.  So we min it with the amount of data we think we can get through
58  * two Ethernet packets (maximum packet size - 100 for TCP/IP overhead).
59  *
60  * Why two ethernet packets?  It used to be one, on the theory that said
61  * packets define the maximum size of atomic update.  But that's less than the
62  * 2000 chars on a 25 x 80 screen, and we don't want local updates to flicker
63  * either.  Two packet lengths will handle up to a 35 x 80 screen.
64  *
65  * The magic '6' is the estimated length of the end-of-line cup sequence to go
66  * to the next line.  It's generous.  We used to mess with the buffering in
67  * init_mvcur() after cost computation, but that lost the sequences emitted by
68  * init_acs() in setupscreen().
69  *
70  * "The setvbuf function may be used only after the stream pointed to by stream
71  * has been associated with an open file and before any other operation is
72  * performed on the stream." (ISO 7.9.5.6.)
73  *
74  * Grrrr...
75  *
76  * On a lighter note, many implementations do in fact allow an application to
77  * reset the buffering after it has been written to.  We try to do this because
78  * otherwise we leave stdout in buffered mode after endwin() is called.  (This
79  * also happens with SVr4 curses).
80  *
81  * There are pros/cons:
82  *
83  * con:
84  *      There is no guarantee that we can reestablish buffering once we've
85  *      dropped it.
86  *
87  *      We _may_ lose data if the implementation does not coordinate this with
88  *      fflush.
89  *
90  * pro:
91  *      An implementation is more likely to refuse to change the buffering than
92  *      to do it in one of the ways mentioned above.
93  *
94  *      The alternative is to have the application try to change buffering
95  *      itself, which is certainly no improvement.
96  *
97  * Just in case it does not work well on a particular system, the calls to
98  * change buffering are all via the macro NC_BUFFERED.  Some implementations
99  * do indeed get confused by changing setbuf on/off, and will overrun the
100  * buffer.  So we disable this by default (there may yet be a workaround).
101  */
102 NCURSES_EXPORT(void)
103 NCURSES_SP_NAME(_nc_set_buffer) (NCURSES_SP_DCLx FILE *ofp, bool buffered)
104 {
105     int Cols;
106     int Lines;
107
108     if (0 == SP_PARM)
109         return;
110
111     Cols = *(ptrCols(SP_PARM));
112     Lines = *(ptrLines(SP_PARM));
113
114     /* optional optimization hack -- do before any output to ofp */
115 #if HAVE_SETVBUF || HAVE_SETBUFFER
116     if (SP_PARM->_buffered != buffered) {
117         unsigned buf_len;
118         char *buf_ptr;
119
120         if (getenv("NCURSES_NO_SETBUF") != 0)
121             return;
122
123         fflush(ofp);
124 #ifdef __DJGPP__
125         setmode(ofp, O_BINARY);
126 #endif
127         if (buffered != 0) {
128             buf_len = (unsigned) min(Lines * (Cols + 6), 2800);
129             if ((buf_ptr = SP_PARM->_setbuf) == 0) {
130                 if ((buf_ptr = typeMalloc(char, buf_len)) == NULL)
131                       return;
132                 SP_PARM->_setbuf = buf_ptr;
133                 /* Don't try to free this! */
134             }
135 #if !USE_SETBUF_0
136             else
137                 return;
138 #endif
139         } else {
140 #if !USE_SETBUF_0
141             return;
142 #else
143             buf_len = 0;
144             buf_ptr = 0;
145 #endif
146         }
147
148 #if HAVE_SETVBUF
149 #ifdef SETVBUF_REVERSED         /* pre-svr3? */
150         (void) setvbuf(ofp, buf_ptr, buf_len, buf_len ? _IOFBF : _IOLBF);
151 #else
152         (void) setvbuf(ofp, buf_ptr, buf_len ? _IOFBF : _IOLBF, buf_len);
153 #endif
154 #elif HAVE_SETBUFFER
155         (void) setbuffer(ofp, buf_ptr, (int) buf_len);
156 #endif
157
158         SP_PARM->_buffered = buffered;
159     }
160 #endif /* HAVE_SETVBUF || HAVE_SETBUFFER */
161 }
162
163 #if NCURSES_SP_FUNCS
164 NCURSES_EXPORT(void)
165 _nc_set_buffer(FILE *ofp, bool buffered)
166 {
167     NCURSES_SP_NAME(_nc_set_buffer) (CURRENT_SCREEN, ofp, buffered);
168 }
169 #endif