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                         2009                    *
34  ****************************************************************************/
35 
36 /*
37 **	lib_newterm.c
38 **
39 **	The newterm() function.
40 **
41 */
42 
43 #include <curses.priv.h>
44 
45 #if SVR4_TERMIO && !defined(_POSIX_SOURCE)
46 #define _POSIX_SOURCE
47 #endif
48 
49 #ifndef CUR
50 #define CUR SP_TERMTYPE
51 #endif
52 
53 #include <tic.h>
54 
55 MODULE_ID("$Id: lib_newterm.c,v 1.86 2010/05/20 23:25:18 tom Exp $")
56 
57 #ifdef USE_TERM_DRIVER
58 #define NumLabels      InfoOf(SP_PARM).numlabels
59 #else
60 #define NumLabels      num_labels
61 #endif
62 
63 #ifndef ONLCR			/* Allows compilation under the QNX 4.2 OS */
64 #define ONLCR 0
65 #endif
66 
67 /*
68  * SVr4/XSI Curses specify that hardware echo is turned off in initscr, and not
69  * restored during the curses session.  The library simulates echo in software.
70  * (The behavior is unspecified if the application enables hardware echo).
71  *
72  * The newterm function also initializes terminal settings, and since initscr
73  * is supposed to behave as if it calls newterm, we do it here.
74  */
75 static NCURSES_INLINE int
76 _nc_initscr(NCURSES_SP_DCL0)
77 {
78     int result = ERR;
79     TERMINAL *term = TerminalOf(SP_PARM);
80 
81     /* for extended XPG4 conformance requires cbreak() at this point */
82     /* (SVr4 curses does this anyway) */
83     if (NCURSES_SP_NAME(cbreak) (NCURSES_SP_ARG) == OK) {
84 	TTY buf;
85 
86 	buf = term->Nttyb;
87 #ifdef TERMIOS
88 	buf.c_lflag &= (unsigned) ~(ECHO | ECHONL);
89 	buf.c_iflag &= (unsigned) ~(ICRNL | INLCR | IGNCR);
90 	buf.c_oflag &= (unsigned) ~(ONLCR);
91 #elif HAVE_SGTTY_H
92 	buf.sg_flags &= ~(ECHO | CRMOD);
93 #else
94 	memset(&buf, 0, sizeof(buf));
95 #endif
96 	result = NCURSES_SP_NAME(_nc_set_tty_mode) (NCURSES_SP_ARGx &buf);
97 	if (result == OK)
98 	    term->Nttyb = buf;
99     }
100     return result;
101 }
102 
103 /*
104  * filter() has to be called before either initscr() or newterm(), so there is
105  * apparently no way to make this flag apply to some terminals and not others,
106  * aside from possibly delaying a filter() call until some terminals have been
107  * initialized.
108  */
109 NCURSES_EXPORT(void)
110 NCURSES_SP_NAME(filter) (NCURSES_SP_DCL0)
111 {
112     START_TRACE();
113     T((T_CALLED("filter(%p)"), (void *) SP_PARM));
114 #if NCURSES_SP_FUNCS
115     if (IsPreScreen(SP_PARM)) {
116 	SP_PARM->_filtered = TRUE;
117     }
118 #else
119     _nc_prescreen.filter_mode = TRUE;
120 #endif
121     returnVoid;
122 }
123 
124 #if NCURSES_SP_FUNCS
125 NCURSES_EXPORT(void)
126 filter(void)
127 {
128     START_TRACE();
129     T((T_CALLED("filter()")));
130     _nc_prescreen.filter_mode = TRUE;
131     returnVoid;
132 }
133 #endif
134 
135 #if NCURSES_EXT_FUNCS
136 /*
137  * An extension, allowing the application to open a new screen without
138  * requiring it to also be filtered.
139  */
140 NCURSES_EXPORT(void)
141 NCURSES_SP_NAME(nofilter) (NCURSES_SP_DCL0)
142 {
143     START_TRACE();
144     T((T_CALLED("nofilter(%p)"), (void *) SP_PARM));
145 #if NCURSES_SP_FUNCS
146     if (IsPreScreen(SP_PARM)) {
147 	SP_PARM->_filtered = FALSE;
148     }
149 #else
150     _nc_prescreen.filter_mode = FALSE;
151 #endif
152     returnVoid;
153 }
154 
155 #if NCURSES_SP_FUNCS
156 NCURSES_EXPORT(void)
157 nofilter(void)
158 {
159     START_TRACE();
160     T((T_CALLED("nofilter()")));
161     _nc_prescreen.filter_mode = FALSE;
162     returnVoid;
163 }
164 #endif
165 #endif /* NCURSES_EXT_FUNCS */
166 
167 NCURSES_EXPORT(SCREEN *)
168 NCURSES_SP_NAME(newterm) (NCURSES_SP_DCLx
169 			  NCURSES_CONST char *name,
170 			  FILE *ofp,
171 			  FILE *ifp)
172 {
173     int value;
174     int errret;
175     SCREEN *result = 0;
176     SCREEN *current;
177     TERMINAL *its_term;
178     FILE *_ofp = ofp ? ofp : stdout;
179     FILE *_ifp = ifp ? ifp : stdin;
180     int cols;
181     int slk_format;
182     int filter_mode;
183     TERMINAL *new_term = 0;
184 
185     START_TRACE();
186     T((T_CALLED("newterm(%p, \"%s\", %p,%p)"),
187        (void *) SP_PARM,
188        name,
189        (void *) ofp,
190        (void *) ifp));
191 
192 #if NCURSES_SP_FUNCS
193     assert(SP_PARM != 0);
194     if (SP_PARM == 0)
195 	returnSP(SP_PARM);
196 #endif
197 
198     _nc_init_pthreads();
199     _nc_lock_global(curses);
200 
201     current = CURRENT_SCREEN;
202     its_term = (current ? current->_term : 0);
203 
204     INIT_TERM_DRIVER();
205     /* this loads the capability entry, then sets LINES and COLS */
206     if (
207 #if NCURSES_SP_FUNCS
208 	   SP_PARM->_prescreen &&
209 #endif
210 	   TINFO_SETUP_TERM(&new_term, name,
211 			    fileno(_ofp), &errret, FALSE) != ERR) {
212 
213 	_nc_set_screen(0);
214 #ifdef USE_TERM_DRIVER
215 	assert(new_term != 0);
216 #endif
217 
218 #if NCURSES_SP_FUNCS
219 	slk_format = SP_PARM->slk_format;
220 	filter_mode = SP_PARM->_filtered;
221 #else
222 	slk_format = _nc_globals.slk_format;
223 	filter_mode = _nc_prescreen.filter_mode;
224 #endif
225 
226 	/*
227 	 * This actually allocates the screen structure, and saves the original
228 	 * terminal settings.
229 	 */
230 	if (NCURSES_SP_NAME(_nc_setupscreen) (
231 #if NCURSES_SP_FUNCS
232 						 &SP_PARM,
233 #endif
234 						 *(ptrLines(SP_PARM)),
235 						 *(ptrCols(SP_PARM)),
236 						 _ofp,
237 						 filter_mode,
238 						 slk_format) == ERR) {
239 	    _nc_set_screen(current);
240 	    result = 0;
241 	} else {
242 #ifdef USE_TERM_DRIVER
243 	    TERMINAL_CONTROL_BLOCK *TCB;
244 #elif !NCURSES_SP_FUNCS
245 	    _nc_set_screen(CURRENT_SCREEN);
246 #endif
247 	    assert(SP_PARM != 0);
248 	    cols = *(ptrCols(SP_PARM));
249 #ifdef USE_TERM_DRIVER
250 	    _nc_set_screen(SP_PARM);
251 	    TCB = (TERMINAL_CONTROL_BLOCK *) new_term;
252 	    TCB->csp = SP_PARM;
253 #endif
254 	    /*
255 	     * In setupterm() we did a set_curterm(), but it was before we set
256 	     * CURRENT_SCREEN.  So the "current" screen's terminal pointer was
257 	     * overwritten with a different terminal.  Later, in
258 	     * _nc_setupscreen(), we set CURRENT_SCREEN and the terminal
259 	     * pointer in the new screen.
260 	     *
261 	     * Restore the terminal-pointer for the pre-existing screen, if
262 	     * any.
263 	     */
264 	    if (current)
265 		current->_term = its_term;
266 
267 #ifdef USE_TERM_DRIVER
268 	    SP_PARM->_term = new_term;
269 #else
270 	    new_term = SP_PARM->_term;
271 #endif
272 
273 	    /* allow user to set maximum escape delay from the environment */
274 	    if ((value = _nc_getenv_num("ESCDELAY")) >= 0) {
275 		NCURSES_SP_NAME(set_escdelay) (NCURSES_SP_ARGx value);
276 	    }
277 
278 	    /* if the terminal type has real soft labels, set those up */
279 	    if (slk_format && NumLabels > 0 && SLK_STDFMT(slk_format))
280 		_nc_slk_initialize(StdScreen(SP_PARM), cols);
281 
282 	    SP_PARM->_ifd = fileno(_ifp);
283 	    NCURSES_SP_NAME(typeahead) (NCURSES_SP_ARGx fileno(_ifp));
284 #ifdef TERMIOS
285 	    SP_PARM->_use_meta = ((new_term->Ottyb.c_cflag & CSIZE) == CS8 &&
286 				  !(new_term->Ottyb.c_iflag & ISTRIP));
287 #else
288 	    SP_PARM->_use_meta = FALSE;
289 #endif
290 	    SP_PARM->_endwin = FALSE;
291 #ifndef USE_TERM_DRIVER
292 	    /*
293 	     * Check whether we can optimize scrolling under dumb terminals in
294 	     * case we do not have any of these capabilities, scrolling
295 	     * optimization will be useless.
296 	     */
297 	    SP_PARM->_scrolling = ((scroll_forward && scroll_reverse) ||
298 				   ((parm_rindex ||
299 				     parm_insert_line ||
300 				     insert_line) &&
301 				    (parm_index ||
302 				     parm_delete_line ||
303 				     delete_line)));
304 #endif
305 
306 	    NCURSES_SP_NAME(baudrate) (NCURSES_SP_ARG);		/* sets a field in the screen structure */
307 
308 	    SP_PARM->_keytry = 0;
309 
310 	    /* compute movement costs so we can do better move optimization */
311 #ifdef USE_TERM_DRIVER
312 	    TCBOf(SP_PARM)->drv->scinit(SP_PARM);
313 #else
314 	    /*
315 	     * Check for mismatched graphic-rendition capabilities.  Most SVr4
316 	     * terminfo trees contain entries that have rmul or rmso equated to
317 	     * sgr0 (Solaris curses copes with those entries).  We do this only
318 	     * for curses, since many termcap applications assume that
319 	     * smso/rmso and smul/rmul are paired, and will not function
320 	     * properly if we remove rmso or rmul.  Curses applications
321 	     * shouldn't be looking at this detail.
322 	     */
323 #define SGR0_TEST(mode) (mode != 0) && (exit_attribute_mode == 0 || strcmp(mode, exit_attribute_mode))
324 	    SP_PARM->_use_rmso = SGR0_TEST(exit_standout_mode);
325 	    SP_PARM->_use_rmul = SGR0_TEST(exit_underline_mode);
326 
327 	    /* compute movement costs so we can do better move optimization */
328 	    _nc_mvcur_init();
329 
330 	    /* initialize terminal to a sane state */
331 	    _nc_screen_init();
332 #endif
333 
334 	    /* Initialize the terminal line settings. */
335 	    _nc_initscr(NCURSES_SP_ARG);
336 
337 	    _nc_signal_handler(TRUE);
338 	    result = SP_PARM;
339 	}
340     }
341     _nc_unlock_global(curses);
342     returnSP(result);
343 }
344 
345 #if NCURSES_SP_FUNCS
346 NCURSES_EXPORT(SCREEN *)
347 newterm(NCURSES_CONST char *name, FILE *ofp, FILE *ifp)
348 {
349     return NCURSES_SP_NAME(newterm) (CURRENT_SCREEN_PRE, name, ofp, ifp);
350 }
351 #endif
352