xref: /openbsd-src/lib/libcurses/base/lib_newterm.c (revision de8cc8edbc71bd3e3bc7fbffa27ba0e564c37d8b)
1 /* $OpenBSD: lib_newterm.c,v 1.13 2010/01/12 23:22:06 nicm Exp $ */
2 
3 /****************************************************************************
4  * Copyright (c) 1998-2007,2008 Free Software Foundation, Inc.              *
5  *                                                                          *
6  * Permission is hereby granted, free of charge, to any person obtaining a  *
7  * copy of this software and associated documentation files (the            *
8  * "Software"), to deal in the Software without restriction, including      *
9  * without limitation the rights to use, copy, modify, merge, publish,      *
10  * distribute, distribute with modifications, sublicense, and/or sell       *
11  * copies of the Software, and to permit persons to whom the Software is    *
12  * furnished to do so, subject to the following conditions:                 *
13  *                                                                          *
14  * The above copyright notice and this permission notice shall be included  *
15  * in all copies or substantial portions of the Software.                   *
16  *                                                                          *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
18  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
20  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
21  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
22  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
23  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
24  *                                                                          *
25  * Except as contained in this notice, the name(s) of the above copyright   *
26  * holders shall not be used in advertising or otherwise to promote the     *
27  * sale, use or other dealings in this Software without prior written       *
28  * authorization.                                                           *
29  ****************************************************************************/
30 
31 /****************************************************************************
32  *  Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
33  *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
34  *     and: Thomas E. Dickey                        1996-on                 *
35  ****************************************************************************/
36 
37 /*
38 **	lib_newterm.c
39 **
40 **	The newterm() function.
41 **
42 */
43 
44 #include <curses.priv.h>
45 
46 #if SVR4_TERMIO && !defined(_POSIX_SOURCE)
47 #define _POSIX_SOURCE
48 #endif
49 
50 #include <term.h>		/* clear_screen, cup & friends, cur_term */
51 #include <tic.h>
52 
53 MODULE_ID("$Id: lib_newterm.c,v 1.13 2010/01/12 23:22:06 nicm Exp $")
54 
55 #ifndef ONLCR			/* Allows compilation under the QNX 4.2 OS */
56 #define ONLCR 0
57 #endif
58 
59 /*
60  * SVr4/XSI Curses specify that hardware echo is turned off in initscr, and not
61  * restored during the curses session.  The library simulates echo in software.
62  * (The behavior is unspecified if the application enables hardware echo).
63  *
64  * The newterm function also initializes terminal settings, and since initscr
65  * is supposed to behave as if it calls newterm, we do it here.
66  */
67 static NCURSES_INLINE int
68 _nc_initscr(void)
69 {
70     int result = ERR;
71 
72     /* for extended XPG4 conformance requires cbreak() at this point */
73     /* (SVr4 curses does this anyway) */
74     if (cbreak() == OK) {
75 	TTY buf;
76 
77 	buf = cur_term->Nttyb;
78 #ifdef TERMIOS
79 	buf.c_lflag &= ~(ECHO | ECHONL);
80 	buf.c_iflag &= ~(ICRNL | INLCR | IGNCR);
81 	buf.c_oflag &= ~(ONLCR);
82 #elif HAVE_SGTTY_H
83 	buf.sg_flags &= ~(ECHO | CRMOD);
84 #else
85 	memset(&buf, 0, sizeof(buf));
86 #endif
87 	if ((result = _nc_set_tty_mode(&buf)) == OK)
88 	    cur_term->Nttyb = buf;
89     }
90     return result;
91 }
92 
93 /*
94  * filter() has to be called before either initscr() or newterm(), so there is
95  * apparently no way to make this flag apply to some terminals and not others,
96  * aside from possibly delaying a filter() call until some terminals have been
97  * initialized.
98  */
99 NCURSES_EXPORT(void)
100 filter(void)
101 {
102     START_TRACE();
103     T((T_CALLED("filter")));
104     _nc_prescreen.filter_mode = TRUE;
105     returnVoid;
106 }
107 
108 #if NCURSES_EXT_FUNCS
109 /*
110  * An extension, allowing the application to open a new screen without
111  * requiring it to also be filtered.
112  */
113 NCURSES_EXPORT(void)
114 nofilter(void)
115 {
116     START_TRACE();
117     T((T_CALLED("nofilter")));
118     _nc_prescreen.filter_mode = FALSE;
119     returnVoid;
120 }
121 #endif
122 
123 NCURSES_EXPORT(SCREEN *)
124 newterm(NCURSES_CONST char *name, FILE *ofp, FILE *ifp)
125 {
126     int value;
127     int errret;
128     SCREEN *current;
129     SCREEN *result = 0;
130     TERMINAL *its_term;
131 
132     START_TRACE();
133     T((T_CALLED("newterm(\"%s\",%p,%p)"), name, ofp, ifp));
134 
135     _nc_init_pthreads();
136     _nc_lock_global(curses);
137 
138     current = SP;
139     its_term = (SP ? SP->_term : 0);
140 
141     /* this loads the capability entry, then sets LINES and COLS */
142     if (setupterm(name, fileno(ofp), &errret) != ERR) {
143 	int slk_format = _nc_globals.slk_format;
144 
145 	/*
146 	 * This actually allocates the screen structure, and saves the original
147 	 * terminal settings.
148 	 */
149 	_nc_set_screen(0);
150 
151 	/* allow user to set maximum escape delay from the environment */
152 	if ((value = _nc_getenv_num("ESCDELAY")) >= 0) {
153 	    set_escdelay(value);
154 	}
155 
156 	if (_nc_setupscreen(LINES,
157 			    COLS,
158 			    ofp,
159 			    _nc_prescreen.filter_mode,
160 			    slk_format) == ERR) {
161 	    _nc_set_screen(current);
162 	    result = 0;
163 	} else {
164 	    assert(SP != 0);
165 	    /*
166 	     * In setupterm() we did a set_curterm(), but it was before we set
167 	     * SP.  So the "current" screen's terminal pointer was overwritten
168 	     * with a different terminal.  Later, in _nc_setupscreen(), we set
169 	     * SP and the terminal pointer in the new screen.
170 	     *
171 	     * Restore the terminal-pointer for the pre-existing screen, if
172 	     * any.
173 	     */
174 	    if (current)
175 		current->_term = its_term;
176 
177 	    /* if the terminal type has real soft labels, set those up */
178 	    if (slk_format && num_labels > 0 && SLK_STDFMT(slk_format))
179 		_nc_slk_initialize(stdscr, COLS);
180 
181 	    SP->_ifd = fileno(ifp);
182 	    typeahead(fileno(ifp));
183 #ifdef TERMIOS
184 	    SP->_use_meta = ((cur_term->Ottyb.c_cflag & CSIZE) == CS8 &&
185 			     !(cur_term->Ottyb.c_iflag & ISTRIP));
186 #else
187 	    SP->_use_meta = FALSE;
188 #endif
189 	    SP->_endwin = FALSE;
190 
191 	    /*
192 	     * Check whether we can optimize scrolling under dumb terminals in
193 	     * case we do not have any of these capabilities, scrolling
194 	     * optimization will be useless.
195 	     */
196 	    SP->_scrolling = ((scroll_forward && scroll_reverse) ||
197 			      ((parm_rindex ||
198 				parm_insert_line ||
199 				insert_line) &&
200 			       (parm_index ||
201 				parm_delete_line ||
202 				delete_line)));
203 
204 	    baudrate();		/* sets a field in the SP structure */
205 
206 	    SP->_keytry = 0;
207 
208 	    /*
209 	     * Check for mismatched graphic-rendition capabilities.  Most SVr4
210 	     * terminfo trees contain entries that have rmul or rmso equated to
211 	     * sgr0 (Solaris curses copes with those entries).  We do this only
212 	     * for curses, since many termcap applications assume that
213 	     * smso/rmso and smul/rmul are paired, and will not function
214 	     * properly if we remove rmso or rmul.  Curses applications
215 	     * shouldn't be looking at this detail.
216 	     */
217 #define SGR0_TEST(mode) (mode != 0) && (exit_attribute_mode == 0 || strcmp(mode, exit_attribute_mode))
218 	    SP->_use_rmso = SGR0_TEST(exit_standout_mode);
219 	    SP->_use_rmul = SGR0_TEST(exit_underline_mode);
220 
221 	    /* compute movement costs so we can do better move optimization */
222 	    _nc_mvcur_init();
223 
224 	    /* initialize terminal to a sane state */
225 	    _nc_screen_init();
226 
227 	    /* Initialize the terminal line settings. */
228 	    _nc_initscr();
229 
230 	    _nc_signal_handler(TRUE);
231 
232 	    result = SP;
233 	}
234     }
235     _nc_unlock_global(curses);
236     returnSP(result);
237 }
238