1 /****************************************************************************
2 * Copyright (c) 1998-2010,2011 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>
46 #define CUR SP_TERMTYPE
51 MODULE_ID("$Id: lib_newterm.c,v 1.87 2011/04/16 16:42:10 tom Exp $")
53 #ifdef USE_TERM_DRIVER
54 #define NumLabels InfoOf(SP_PARM).numlabels
56 #define NumLabels num_labels
59 #ifndef ONLCR /* Allows compilation under the QNX 4.2 OS */
64 * SVr4/XSI Curses specify that hardware echo is turned off in initscr, and not
65 * restored during the curses session. The library simulates echo in software.
66 * (The behavior is unspecified if the application enables hardware echo).
68 * The newterm function also initializes terminal settings, and since initscr
69 * is supposed to behave as if it calls newterm, we do it here.
71 static NCURSES_INLINE int
72 _nc_initscr(NCURSES_SP_DCL0)
75 TERMINAL *term = TerminalOf(SP_PARM);
77 /* for extended XPG4 conformance requires cbreak() at this point */
78 /* (SVr4 curses does this anyway) */
79 if (NCURSES_SP_NAME(cbreak) (NCURSES_SP_ARG) == OK) {
84 buf.c_lflag &= (unsigned) ~(ECHO | ECHONL);
85 buf.c_iflag &= (unsigned) ~(ICRNL | INLCR | IGNCR);
86 buf.c_oflag &= (unsigned) ~(ONLCR);
88 buf.sg_flags &= ~(ECHO | CRMOD);
90 memset(&buf, 0, sizeof(buf));
92 result = NCURSES_SP_NAME(_nc_set_tty_mode) (NCURSES_SP_ARGx &buf);
100 * filter() has to be called before either initscr() or newterm(), so there is
101 * apparently no way to make this flag apply to some terminals and not others,
102 * aside from possibly delaying a filter() call until some terminals have been
106 NCURSES_SP_NAME(filter) (NCURSES_SP_DCL0)
109 T((T_CALLED("filter(%p)"), (void *) SP_PARM));
111 if (IsPreScreen(SP_PARM)) {
112 SP_PARM->_filtered = TRUE;
115 _nc_prescreen.filter_mode = TRUE;
125 T((T_CALLED("filter()")));
126 _nc_prescreen.filter_mode = TRUE;
131 #if NCURSES_EXT_FUNCS
133 * An extension, allowing the application to open a new screen without
134 * requiring it to also be filtered.
137 NCURSES_SP_NAME(nofilter) (NCURSES_SP_DCL0)
140 T((T_CALLED("nofilter(%p)"), (void *) SP_PARM));
142 if (IsPreScreen(SP_PARM)) {
143 SP_PARM->_filtered = FALSE;
146 _nc_prescreen.filter_mode = FALSE;
156 T((T_CALLED("nofilter()")));
157 _nc_prescreen.filter_mode = FALSE;
161 #endif /* NCURSES_EXT_FUNCS */
163 NCURSES_EXPORT(SCREEN *)
164 NCURSES_SP_NAME(newterm) (NCURSES_SP_DCLx
165 NCURSES_CONST char *name,
174 FILE *_ofp = ofp ? ofp : stdout;
175 FILE *_ifp = ifp ? ifp : stdin;
179 TERMINAL *new_term = 0;
182 T((T_CALLED("newterm(%p, \"%s\", %p,%p)"),
189 assert(SP_PARM != 0);
195 _nc_lock_global(curses);
197 current = CURRENT_SCREEN;
198 its_term = (current ? current->_term : 0);
201 /* this loads the capability entry, then sets LINES and COLS */
204 SP_PARM->_prescreen &&
206 TINFO_SETUP_TERM(&new_term, name,
207 fileno(_ofp), &errret, FALSE) != ERR) {
210 #ifdef USE_TERM_DRIVER
211 assert(new_term != 0);
215 slk_format = SP_PARM->slk_format;
216 filter_mode = SP_PARM->_filtered;
218 slk_format = _nc_globals.slk_format;
219 filter_mode = _nc_prescreen.filter_mode;
223 * This actually allocates the screen structure, and saves the original
226 if (NCURSES_SP_NAME(_nc_setupscreen) (
230 *(ptrLines(SP_PARM)),
234 slk_format) == ERR) {
235 _nc_set_screen(current);
238 #ifdef USE_TERM_DRIVER
239 TERMINAL_CONTROL_BLOCK *TCB;
240 #elif !NCURSES_SP_FUNCS
241 _nc_set_screen(CURRENT_SCREEN);
243 assert(SP_PARM != 0);
244 cols = *(ptrCols(SP_PARM));
245 #ifdef USE_TERM_DRIVER
246 _nc_set_screen(SP_PARM);
247 TCB = (TERMINAL_CONTROL_BLOCK *) new_term;
251 * In setupterm() we did a set_curterm(), but it was before we set
252 * CURRENT_SCREEN. So the "current" screen's terminal pointer was
253 * overwritten with a different terminal. Later, in
254 * _nc_setupscreen(), we set CURRENT_SCREEN and the terminal
255 * pointer in the new screen.
257 * Restore the terminal-pointer for the pre-existing screen, if
261 current->_term = its_term;
263 #ifdef USE_TERM_DRIVER
264 SP_PARM->_term = new_term;
266 new_term = SP_PARM->_term;
269 /* allow user to set maximum escape delay from the environment */
270 if ((value = _nc_getenv_num("ESCDELAY")) >= 0) {
271 NCURSES_SP_NAME(set_escdelay) (NCURSES_SP_ARGx value);
274 /* if the terminal type has real soft labels, set those up */
275 if (slk_format && NumLabels > 0 && SLK_STDFMT(slk_format))
276 _nc_slk_initialize(StdScreen(SP_PARM), cols);
278 SP_PARM->_ifd = fileno(_ifp);
279 NCURSES_SP_NAME(typeahead) (NCURSES_SP_ARGx fileno(_ifp));
281 SP_PARM->_use_meta = ((new_term->Ottyb.c_cflag & CSIZE) == CS8 &&
282 !(new_term->Ottyb.c_iflag & ISTRIP));
284 SP_PARM->_use_meta = FALSE;
286 SP_PARM->_endwin = FALSE;
287 #ifndef USE_TERM_DRIVER
289 * Check whether we can optimize scrolling under dumb terminals in
290 * case we do not have any of these capabilities, scrolling
291 * optimization will be useless.
293 SP_PARM->_scrolling = ((scroll_forward && scroll_reverse) ||
302 NCURSES_SP_NAME(baudrate) (NCURSES_SP_ARG); /* sets a field in the screen structure */
304 SP_PARM->_keytry = 0;
306 /* compute movement costs so we can do better move optimization */
307 #ifdef USE_TERM_DRIVER
308 TCBOf(SP_PARM)->drv->scinit(SP_PARM);
311 * Check for mismatched graphic-rendition capabilities. Most SVr4
312 * terminfo trees contain entries that have rmul or rmso equated to
313 * sgr0 (Solaris curses copes with those entries). We do this only
314 * for curses, since many termcap applications assume that
315 * smso/rmso and smul/rmul are paired, and will not function
316 * properly if we remove rmso or rmul. Curses applications
317 * shouldn't be looking at this detail.
319 #define SGR0_TEST(mode) (mode != 0) && (exit_attribute_mode == 0 || strcmp(mode, exit_attribute_mode))
320 SP_PARM->_use_rmso = SGR0_TEST(exit_standout_mode);
321 SP_PARM->_use_rmul = SGR0_TEST(exit_underline_mode);
323 /* compute movement costs so we can do better move optimization */
326 /* initialize terminal to a sane state */
330 /* Initialize the terminal line settings. */
331 _nc_initscr(NCURSES_SP_ARG);
333 _nc_signal_handler(TRUE);
337 _nc_unlock_global(curses);
342 NCURSES_EXPORT(SCREEN *)
343 newterm(NCURSES_CONST char *name, FILE *ofp, FILE *ifp)
345 return NCURSES_SP_NAME(newterm) (CURRENT_SCREEN_PRE, name, ofp, ifp);