Back to index

tetex-bin  3.0
setbuf.c
Go to the documentation of this file.
00001 /****************************************************************************
00002  * Copyright (c) 1998-2000,2003 Free Software Foundation, Inc.              *
00003  *                                                                          *
00004  * Permission is hereby granted, free of charge, to any person obtaining a  *
00005  * copy of this software and associated documentation files (the            *
00006  * "Software"), to deal in the Software without restriction, including      *
00007  * without limitation the rights to use, copy, modify, merge, publish,      *
00008  * distribute, distribute with modifications, sublicense, and/or sell       *
00009  * copies of the Software, and to permit persons to whom the Software is    *
00010  * furnished to do so, subject to the following conditions:                 *
00011  *                                                                          *
00012  * The above copyright notice and this permission notice shall be included  *
00013  * in all copies or substantial portions of the Software.                   *
00014  *                                                                          *
00015  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
00016  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
00017  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
00018  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
00019  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
00020  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
00021  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
00022  *                                                                          *
00023  * Except as contained in this notice, the name(s) of the above copyright   *
00024  * holders shall not be used in advertising or otherwise to promote the     *
00025  * sale, use or other dealings in this Software without prior written       *
00026  * authorization.                                                           *
00027  ****************************************************************************/
00028 
00029 /****************************************************************************
00030  *  Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
00031  *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
00032  ****************************************************************************/
00033 
00034 /*
00035 **     setbuf.c
00036 **
00037 **     Support for set_term(), reset_shell_mode(), reset_prog_mode().
00038 **
00039 */
00040 
00041 #include <curses.priv.h>
00042 
00043 MODULE_ID("$Id: setbuf.c,v 1.12 2003/11/15 23:55:34 tom Exp $")
00044 
00045 /*
00046  * If the output file descriptor is connected to a tty (the typical case) it
00047  * will probably be line-buffered.  Keith Bostic pointed out that we don't want
00048  * this; it hoses people running over networks by forcing out a bunch of small
00049  * packets instead of one big one, so screen updates on ptys look jerky.
00050  * Restore block buffering to prevent this minor lossage.
00051  *
00052  * The buffer size is a compromise.  Ideally we'd like a buffer that can hold
00053  * the maximum possible update size (the whole screen plus cup commands to
00054  * change lines as it's painted).  On a 66-line xterm this can become
00055  * excessive.  So we min it with the amount of data we think we can get through
00056  * two Ethernet packets (maximum packet size - 100 for TCP/IP overhead).
00057  *
00058  * Why two ethernet packets?  It used to be one, on the theory that said
00059  * packets define the maximum size of atomic update.  But that's less than the
00060  * 2000 chars on a 25 x 80 screen, and we don't want local updates to flicker
00061  * either.  Two packet lengths will handle up to a 35 x 80 screen.
00062  *
00063  * The magic '6' is the estimated length of the end-of-line cup sequence to go
00064  * to the next line.  It's generous.  We used to mess with the buffering in
00065  * init_mvcur() after cost computation, but that lost the sequences emitted by
00066  * init_acs() in setupscreen().
00067  *
00068  * "The setvbuf function may be used only after the stream pointed to by stream
00069  * has been associated with an open file and before any other operation is
00070  * performed on the stream." (ISO 7.9.5.6.)
00071  *
00072  * Grrrr...
00073  *
00074  * On a lighter note, many implementations do in fact allow an application to
00075  * reset the buffering after it has been written to.  We try to do this because
00076  * otherwise we leave stdout in buffered mode after endwin() is called.  (This
00077  * also happens with SVr4 curses).
00078  *
00079  * There are pros/cons:
00080  *
00081  * con:
00082  *     There is no guarantee that we can reestablish buffering once we've
00083  *     dropped it.
00084  *
00085  *     We _may_ lose data if the implementation does not coordinate this with
00086  *     fflush.
00087  *
00088  * pro:
00089  *     An implementation is more likely to refuse to change the buffering than
00090  *     to do it in one of the ways mentioned above.
00091  *
00092  *     The alternative is to have the application try to change buffering
00093  *     itself, which is certainly no improvement.
00094  *
00095  * Just in case it does not work well on a particular system, the calls to
00096  * change buffering are all via the macro NC_BUFFERED.  Some implementations
00097  * do indeed get confused by changing setbuf on/off, and will overrun the
00098  * buffer.  So we disable this by default (there may yet be a workaround).
00099  */
00100 NCURSES_EXPORT(void)
00101 _nc_set_buffer(FILE *ofp, bool buffered)
00102 {
00103     /* optional optimization hack -- do before any output to ofp */
00104 #if HAVE_SETVBUF || HAVE_SETBUFFER
00105     if (SP->_buffered != (int)buffered) {
00106        unsigned buf_len;
00107        char *buf_ptr;
00108 
00109        if (getenv("NCURSES_NO_SETBUF") != 0)
00110            return;
00111 
00112        fflush(ofp);
00113 #ifdef __DJGPP__
00114        setmode(ofp, O_BINARY);
00115 #endif
00116        if (buffered != 0) {
00117            buf_len = min(LINES * (COLS + 6), 2800);
00118            if ((buf_ptr = SP->_setbuf) == 0) {
00119               if ((buf_ptr = typeMalloc(char, buf_len)) == NULL)
00120                     return;
00121               SP->_setbuf = buf_ptr;
00122               /* Don't try to free this! */
00123            }
00124 #if !USE_SETBUF_0
00125            else
00126               return;
00127 #endif
00128        } else {
00129 #if !USE_SETBUF_0
00130            return;
00131 #else
00132            buf_len = 0;
00133            buf_ptr = 0;
00134 #endif
00135        }
00136 
00137 #if HAVE_SETVBUF
00138 #ifdef SETVBUF_REVERSED            /* pre-svr3? */
00139        (void) setvbuf(ofp, buf_ptr, buf_len, buf_len ? _IOFBF : _IOLBF);
00140 #else
00141        (void) setvbuf(ofp, buf_ptr, buf_len ? _IOFBF : _IOLBF, buf_len);
00142 #endif
00143 #elif HAVE_SETBUFFER
00144        (void) setbuffer(ofp, buf_ptr, (int) buf_len);
00145 #endif
00146 
00147        SP->_buffered = buffered;
00148     }
00149 #endif /* HAVE_SETVBUF || HAVE_SETBUFFER */
00150 }