1 /****************************************************************************
2 * Copyright (c) 1998-2008,2009 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)
49 #include <term.h> /* clear_screen, cup & friends, cur_term */
52 MODULE_ID("$Id: lib_newterm.c,v 1.75 2009/04/18 19:22:08 tom Exp $")
54 #ifndef ONLCR /* Allows compilation under the QNX 4.2 OS */
59 * SVr4/XSI Curses specify that hardware echo is turned off in initscr, and not
60 * restored during the curses session. The library simulates echo in software.
61 * (The behavior is unspecified if the application enables hardware echo).
63 * The newterm function also initializes terminal settings, and since initscr
64 * is supposed to behave as if it calls newterm, we do it here.
66 static NCURSES_INLINE int
71 /* for extended XPG4 conformance requires cbreak() at this point */
72 /* (SVr4 curses does this anyway) */
76 buf = cur_term->Nttyb;
78 buf.c_lflag &= ~(ECHO | ECHONL);
79 buf.c_iflag &= ~(ICRNL | INLCR | IGNCR);
80 buf.c_oflag &= ~(ONLCR);
82 buf.sg_flags &= ~(ECHO | CRMOD);
84 memset(&buf, 0, sizeof(buf));
86 if ((result = _nc_set_tty_mode(&buf)) == OK)
87 cur_term->Nttyb = buf;
93 * filter() has to be called before either initscr() or newterm(), so there is
94 * apparently no way to make this flag apply to some terminals and not others,
95 * aside from possibly delaying a filter() call until some terminals have been
99 NCURSES_SP_NAME(filter) (NCURSES_SP_DCL0)
102 T((T_CALLED("filter")));
103 _nc_prescreen.filter_mode = TRUE;
111 NCURSES_SP_NAME(filter) (CURRENT_SCREEN);
115 #if NCURSES_EXT_FUNCS
117 * An extension, allowing the application to open a new screen without
118 * requiring it to also be filtered.
121 NCURSES_SP_NAME(nofilter) (NCURSES_SP_DCL0)
124 T((T_CALLED("nofilter")));
125 _nc_prescreen.filter_mode = FALSE;
133 NCURSES_SP_NAME(nofilter) (CURRENT_SCREEN);
136 #endif /* NCURSES_EXT_FUNCS */
138 NCURSES_EXPORT(SCREEN *)
139 NCURSES_SP_NAME(newterm) (NCURSES_SP_DCLx
140 NCURSES_CONST char *name,
151 T((T_CALLED("newterm(\"%s\",%p,%p)"), name, ofp, ifp));
154 _nc_lock_global(curses);
157 its_term = (SP_PARM ? SP_PARM->_term : 0);
159 /* this loads the capability entry, then sets LINES and COLS */
160 if (setupterm(name, fileno(ofp), &errret) != ERR) {
161 int slk_format = _nc_globals.slk_format;
164 * This actually allocates the screen structure, and saves the original
169 /* allow user to set maximum escape delay from the environment */
170 if ((value = _nc_getenv_num("ESCDELAY")) >= 0) {
174 if (_nc_setupscreen(LINES,
177 _nc_prescreen.filter_mode,
178 slk_format) == ERR) {
179 _nc_set_screen(current);
182 assert(SP_PARM != 0);
184 * In setupterm() we did a set_curterm(), but it was before we set
185 * SP. So the "current" screen's terminal pointer was overwritten
186 * with a different terminal. Later, in _nc_setupscreen(), we set
187 * SP and the terminal pointer in the new screen.
189 * Restore the terminal-pointer for the pre-existing screen, if
193 current->_term = its_term;
195 /* if the terminal type has real soft labels, set those up */
196 if (slk_format && num_labels > 0 && SLK_STDFMT(slk_format))
197 _nc_slk_initialize(stdscr, COLS);
199 SP->_ifd = fileno(ifp);
200 typeahead(fileno(ifp));
202 SP->_use_meta = ((cur_term->Ottyb.c_cflag & CSIZE) == CS8 &&
203 !(cur_term->Ottyb.c_iflag & ISTRIP));
205 SP->_use_meta = FALSE;
210 * Check whether we can optimize scrolling under dumb terminals in
211 * case we do not have any of these capabilities, scrolling
212 * optimization will be useless.
214 SP->_scrolling = ((scroll_forward && scroll_reverse) ||
222 baudrate(); /* sets a field in the SP structure */
227 * Check for mismatched graphic-rendition capabilities. Most SVr4
228 * terminfo trees contain entries that have rmul or rmso equated to
229 * sgr0 (Solaris curses copes with those entries). We do this only
230 * for curses, since many termcap applications assume that
231 * smso/rmso and smul/rmul are paired, and will not function
232 * properly if we remove rmso or rmul. Curses applications
233 * shouldn't be looking at this detail.
235 #define SGR0_TEST(mode) (mode != 0) && (exit_attribute_mode == 0 || strcmp(mode, exit_attribute_mode))
236 SP->_use_rmso = SGR0_TEST(exit_standout_mode);
237 SP->_use_rmul = SGR0_TEST(exit_underline_mode);
239 /* compute movement costs so we can do better move optimization */
242 /* initialize terminal to a sane state */
245 /* Initialize the terminal line settings. */
248 _nc_signal_handler(TRUE);
253 _nc_unlock_global(curses);
258 NCURSES_EXPORT(SCREEN *)
259 newterm(NCURSES_CONST char *name, FILE *ofp, FILE *ifp)
261 return NCURSES_SP_NAME(newterm) (CURRENT_SCREEN, name, ofp, ifp);