Back to index

tetex-bin  3.0
lib_newterm.c
Go to the documentation of this file.
00001 /****************************************************************************
00002  * Copyright (c) 1998-2002,2004 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  *     and: Thomas E. Dickey                        1996-on                 *
00033  ****************************************************************************/
00034 
00035 /*
00036 **     lib_newterm.c
00037 **
00038 **     The newterm() function.
00039 **
00040 */
00041 
00042 #include <curses.priv.h>
00043 
00044 #if SVR4_TERMIO && !defined(_POSIX_SOURCE)
00045 #define _POSIX_SOURCE
00046 #endif
00047 
00048 #include <term.h>           /* clear_screen, cup & friends, cur_term */
00049 #include <tic.h>
00050 
00051 MODULE_ID("$Id: lib_newterm.c,v 1.58 2004/08/14 20:36:39 tom Exp $")
00052 
00053 #ifndef ONLCR               /* Allows compilation under the QNX 4.2 OS */
00054 #define ONLCR 0
00055 #endif
00056 
00057 /*
00058  * SVr4/XSI Curses specify that hardware echo is turned off in initscr, and not
00059  * restored during the curses session.  The library simulates echo in software.
00060  * (The behavior is unspecified if the application enables hardware echo).
00061  *
00062  * The newterm function also initializes terminal settings, and since initscr
00063  * is supposed to behave as if it calls newterm, we do it here.
00064  */
00065 static inline int
00066 _nc_initscr(void)
00067 {
00068     int result = ERR;
00069 
00070     /* for extended XPG4 conformance requires cbreak() at this point */
00071     /* (SVr4 curses does this anyway) */
00072     if (cbreak() == OK) {
00073        TTY buf;
00074 
00075        buf = cur_term->Nttyb;
00076 #ifdef TERMIOS
00077        buf.c_lflag &= ~(ECHO | ECHONL);
00078        buf.c_iflag &= ~(ICRNL | INLCR | IGNCR);
00079        buf.c_oflag &= ~(ONLCR);
00080 #else
00081        buf.sg_flags &= ~(ECHO | CRMOD);
00082 #endif
00083        if ((result = _nc_set_tty_mode(&buf)) == OK)
00084            cur_term->Nttyb = buf;
00085     }
00086     return result;
00087 }
00088 
00089 /*
00090  * filter() has to be called before either initscr() or newterm(), so there is
00091  * apparently no way to make this flag apply to some terminals and not others,
00092  * aside from possibly delaying a filter() call until some terminals have been
00093  * initialized.
00094  */
00095 static int filter_mode = FALSE;
00096 
00097 NCURSES_EXPORT(void)
00098 filter(void)
00099 {
00100     T((T_CALLED("filter")));
00101     filter_mode = TRUE;
00102     returnVoid;
00103 }
00104 
00105 NCURSES_EXPORT(SCREEN *)
00106 newterm(NCURSES_CONST char *name, FILE *ofp, FILE *ifp)
00107 {
00108     int errret;
00109     int slk_format = _nc_slk_format;
00110     SCREEN *current;
00111 
00112     START_TRACE();
00113     T((T_CALLED("newterm(\"%s\",%p,%p)"), name, ofp, ifp));
00114 
00115     /* this loads the capability entry, then sets LINES and COLS */
00116     if (setupterm(name, fileno(ofp), &errret) == ERR)
00117        returnSP(0);
00118 
00119     /* implement filter mode */
00120     if (filter_mode) {
00121        LINES = 1;
00122 
00123        clear_screen = 0;
00124        cursor_down = parm_down_cursor = 0;
00125        cursor_address = 0;
00126        cursor_up = parm_up_cursor = 0;
00127        row_address = 0;
00128 
00129        cursor_home = carriage_return;
00130     }
00131 
00132     /* If we must simulate soft labels, grab off the line to be used.
00133        We assume that we must simulate, if it is none of the standard
00134        formats (4-4  or 3-2-3) for which there may be some hardware
00135        support. */
00136     if (num_labels <= 0 || !SLK_STDFMT(slk_format))
00137        if (slk_format) {
00138            if (ERR == _nc_ripoffline(-SLK_LINES(slk_format),
00139                                   _nc_slk_initialize))
00140               returnSP(0);
00141        }
00142     /* this actually allocates the screen structure, and saves the
00143      * original terminal settings.
00144      */
00145     current = SP;
00146     _nc_set_screen(0);
00147     if (_nc_setupscreen(LINES, COLS, ofp) == ERR) {
00148        _nc_set_screen(current);
00149        returnSP(0);
00150     }
00151 
00152     /* if the terminal type has real soft labels, set those up */
00153     if (slk_format && num_labels > 0 && SLK_STDFMT(slk_format))
00154        _nc_slk_initialize(stdscr, COLS);
00155 
00156     SP->_ifd = fileno(ifp);
00157     typeahead(fileno(ifp));
00158 #ifdef TERMIOS
00159     SP->_use_meta = ((cur_term->Ottyb.c_cflag & CSIZE) == CS8 &&
00160                    !(cur_term->Ottyb.c_iflag & ISTRIP));
00161 #else
00162     SP->_use_meta = FALSE;
00163 #endif
00164     SP->_endwin = FALSE;
00165 
00166     /* Check whether we can optimize scrolling under dumb terminals in case
00167      * we do not have any of these capabilities, scrolling optimization
00168      * will be useless.
00169      */
00170     SP->_scrolling = ((scroll_forward && scroll_reverse) ||
00171                     ((parm_rindex || parm_insert_line || insert_line) &&
00172                      (parm_index || parm_delete_line || delete_line)));
00173 
00174     baudrate();                    /* sets a field in the SP structure */
00175 
00176     SP->_keytry = 0;
00177 
00178     /*
00179      * Check for mismatched graphic-rendition capabilities.  Most SVr4
00180      * terminfo trees contain entries that have rmul or rmso equated to
00181      * sgr0 (Solaris curses copes with those entries).  We do this only for
00182      * curses, since many termcap applications assume that smso/rmso and
00183      * smul/rmul are paired, and will not function properly if we remove
00184      * rmso or rmul.  Curses applications shouldn't be looking at this
00185      * detail.
00186      */
00187 #define SGR0_TEST(mode) (mode != 0) && (exit_attribute_mode == 0 || strcmp(mode, exit_attribute_mode))
00188     SP->_use_rmso = SGR0_TEST(exit_standout_mode);
00189     SP->_use_rmul = SGR0_TEST(exit_underline_mode);
00190 
00191     /* compute movement costs so we can do better move optimization */
00192     _nc_mvcur_init();
00193 
00194     /* initialize terminal to a sane state */
00195     _nc_screen_init();
00196 
00197     /* Initialize the terminal line settings. */
00198     _nc_initscr();
00199 
00200     _nc_signal_handler(TRUE);
00201 
00202 #if USE_SIZECHANGE
00203     /*
00204      * Pretend we received a SIGWINCH, just in case we're starting up in a
00205      * terminal that cannot initialize its size properly (Debian #265631).
00206      */
00207     SP->_sig_winch = TRUE;
00208 #endif
00209 
00210     returnSP(SP);
00211 }