1 /****************************************************************************
2 * Copyright (c) 1998-2009,2010 Free Software Foundation, Inc. *
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: *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
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. *
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 *
27 ****************************************************************************/
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 ****************************************************************************/
39 ** The newterm() function.
43 #include <curses.priv.h>
45 #if SVR4_TERMIO && !defined(_POSIX_SOURCE)
50 #define CUR SP_TERMTYPE
55 MODULE_ID("$Id: lib_newterm.c,v 1.86 2010/05/20 23:25:18 tom Exp $")
57 #ifdef USE_TERM_DRIVER
58 #define NumLabels InfoOf(SP_PARM).numlabels
60 #define NumLabels num_labels
63 #ifndef ONLCR /* Allows compilation under the QNX 4.2 OS */
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).
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.
75 static NCURSES_INLINE
int
76 _nc_initscr(NCURSES_SP_DCL0
)
79 TERMINAL
*term
= TerminalOf(SP_PARM
);
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
) {
88 buf
.c_lflag
&= (unsigned) ~(ECHO
| ECHONL
);
89 buf
.c_iflag
&= (unsigned) ~(ICRNL
| INLCR
| IGNCR
);
90 buf
.c_oflag
&= (unsigned) ~(ONLCR
);
92 buf
.sg_flags
&= ~(ECHO
| CRMOD
);
94 memset(&buf
, 0, sizeof(buf
));
96 result
= NCURSES_SP_NAME(_nc_set_tty_mode
) (NCURSES_SP_ARGx
&buf
);
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
110 NCURSES_SP_NAME(filter
) (NCURSES_SP_DCL0
)
113 T((T_CALLED("filter(%p)"), (void *) SP_PARM
));
115 if (IsPreScreen(SP_PARM
)) {
116 SP_PARM
->_filtered
= TRUE
;
119 _nc_prescreen
.filter_mode
= TRUE
;
129 T((T_CALLED("filter()")));
130 _nc_prescreen
.filter_mode
= TRUE
;
135 #if NCURSES_EXT_FUNCS
137 * An extension, allowing the application to open a new screen without
138 * requiring it to also be filtered.
141 NCURSES_SP_NAME(nofilter
) (NCURSES_SP_DCL0
)
144 T((T_CALLED("nofilter(%p)"), (void *) SP_PARM
));
146 if (IsPreScreen(SP_PARM
)) {
147 SP_PARM
->_filtered
= FALSE
;
150 _nc_prescreen
.filter_mode
= FALSE
;
160 T((T_CALLED("nofilter()")));
161 _nc_prescreen
.filter_mode
= FALSE
;
165 #endif /* NCURSES_EXT_FUNCS */
167 NCURSES_EXPORT(SCREEN
*)
168 NCURSES_SP_NAME(newterm
) (NCURSES_SP_DCLx
169 NCURSES_CONST
char *name
,
178 FILE *_ofp
= ofp
? ofp
: stdout
;
179 FILE *_ifp
= ifp
? ifp
: stdin
;
183 TERMINAL
*new_term
= 0;
186 T((T_CALLED("newterm(%p, \"%s\", %p,%p)"),
193 assert(SP_PARM
!= 0);
199 _nc_lock_global(curses
);
201 current
= CURRENT_SCREEN
;
202 its_term
= (current
? current
->_term
: 0);
205 /* this loads the capability entry, then sets LINES and COLS */
208 SP_PARM
->_prescreen
&&
210 TINFO_SETUP_TERM(&new_term
, name
,
211 fileno(_ofp
), &errret
, FALSE
) != ERR
) {
214 #ifdef USE_TERM_DRIVER
215 assert(new_term
!= 0);
219 slk_format
= SP_PARM
->slk_format
;
220 filter_mode
= SP_PARM
->_filtered
;
222 slk_format
= _nc_globals
.slk_format
;
223 filter_mode
= _nc_prescreen
.filter_mode
;
227 * This actually allocates the screen structure, and saves the original
230 if (NCURSES_SP_NAME(_nc_setupscreen
) (
234 *(ptrLines(SP_PARM
)),
238 slk_format
) == ERR
) {
239 _nc_set_screen(current
);
242 #ifdef USE_TERM_DRIVER
243 TERMINAL_CONTROL_BLOCK
*TCB
;
244 #elif !NCURSES_SP_FUNCS
245 _nc_set_screen(CURRENT_SCREEN
);
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
;
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.
261 * Restore the terminal-pointer for the pre-existing screen, if
265 current
->_term
= its_term
;
267 #ifdef USE_TERM_DRIVER
268 SP_PARM
->_term
= new_term
;
270 new_term
= SP_PARM
->_term
;
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
);
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
);
282 SP_PARM
->_ifd
= fileno(_ifp
);
283 NCURSES_SP_NAME(typeahead
) (NCURSES_SP_ARGx
fileno(_ifp
));
285 SP_PARM
->_use_meta
= ((new_term
->Ottyb
.c_cflag
& CSIZE
) == CS8
&&
286 !(new_term
->Ottyb
.c_iflag
& ISTRIP
));
288 SP_PARM
->_use_meta
= FALSE
;
290 SP_PARM
->_endwin
= FALSE
;
291 #ifndef USE_TERM_DRIVER
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.
297 SP_PARM
->_scrolling
= ((scroll_forward
&& scroll_reverse
) ||
306 NCURSES_SP_NAME(baudrate
) (NCURSES_SP_ARG
); /* sets a field in the screen structure */
308 SP_PARM
->_keytry
= 0;
310 /* compute movement costs so we can do better move optimization */
311 #ifdef USE_TERM_DRIVER
312 TCBOf(SP_PARM
)->drv
->scinit(SP_PARM
);
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.
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
);
327 /* compute movement costs so we can do better move optimization */
330 /* initialize terminal to a sane state */
334 /* Initialize the terminal line settings. */
335 _nc_initscr(NCURSES_SP_ARG
);
337 _nc_signal_handler(TRUE
);
341 _nc_unlock_global(curses
);
346 NCURSES_EXPORT(SCREEN
*)
347 newterm(NCURSES_CONST
char *name
, FILE *ofp
, FILE *ifp
)
349 return NCURSES_SP_NAME(newterm
) (CURRENT_SCREEN_PRE
, name
, ofp
, ifp
);