1 /****************************************************************************
2 * Copyright (c) 1998-2017,2018 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.101 2018/04/07 20:36:22 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 T((T_CALLED("_nc_initscr(%p) ->term %p"), (void *) SP_PARM, (void *) term));
80 if (NCURSES_SP_NAME(cbreak) (NCURSES_SP_ARG) == OK) {
85 buf.c_lflag &= (unsigned) ~(ECHO | ECHONL);
86 buf.c_iflag &= (unsigned) ~(ICRNL | INLCR | IGNCR);
87 buf.c_oflag &= (unsigned) ~(ONLCR);
89 buf.sg_flags &= ~(ECHO | CRMOD);
91 memset(&buf, 0, sizeof(buf));
93 result = NCURSES_SP_NAME(_nc_set_tty_mode) (NCURSES_SP_ARGx &buf);
101 * filter() has to be called before either initscr() or newterm(), so there is
102 * apparently no way to make this flag apply to some terminals and not others,
103 * aside from possibly delaying a filter() call until some terminals have been
107 NCURSES_SP_NAME(filter) (NCURSES_SP_DCL0)
110 T((T_CALLED("filter(%p)"), (void *) SP_PARM));
112 if (IsPreScreen(SP_PARM)) {
113 SP_PARM->_filtered = TRUE;
116 _nc_prescreen.filter_mode = TRUE;
126 T((T_CALLED("filter()")));
127 _nc_prescreen.filter_mode = TRUE;
132 #if NCURSES_EXT_FUNCS
134 * An extension, allowing the application to open a new screen without
135 * requiring it to also be filtered.
138 NCURSES_SP_NAME(nofilter) (NCURSES_SP_DCL0)
141 T((T_CALLED("nofilter(%p)"), (void *) SP_PARM));
143 if (IsPreScreen(SP_PARM)) {
144 SP_PARM->_filtered = FALSE;
147 _nc_prescreen.filter_mode = FALSE;
157 T((T_CALLED("nofilter()")));
158 _nc_prescreen.filter_mode = FALSE;
162 #endif /* NCURSES_EXT_FUNCS */
164 NCURSES_EXPORT(SCREEN *)
165 NCURSES_SP_NAME(newterm) (NCURSES_SP_DCLx
174 FILE *_ofp = ofp ? ofp : stdout;
175 FILE *_ifp = ifp ? ifp : stdin;
176 TERMINAL *new_term = 0;
179 T((T_CALLED("newterm(%p, \"%s\", %p,%p)"),
186 assert(SP_PARM != 0);
192 _nc_lock_global(curses);
194 current = CURRENT_SCREEN;
195 its_term = (current ? current->_term : 0);
198 /* this loads the capability entry, then sets LINES and COLS */
200 TINFO_SETUP_TERM(&new_term, name,
201 fileno(_ofp), &errret, FALSE) != ERR) {
206 #ifdef USE_TERM_DRIVER
207 assert(new_term != 0);
211 slk_format = SP_PARM->slk_format;
212 filter_mode = SP_PARM->_filtered;
214 slk_format = _nc_globals.slk_format;
215 filter_mode = _nc_prescreen.filter_mode;
219 * This actually allocates the screen structure, and saves the original
222 if (NCURSES_SP_NAME(_nc_setupscreen) (
226 *(ptrLines(SP_PARM)),
230 slk_format) == ERR) {
231 _nc_set_screen(current);
237 #ifdef USE_TERM_DRIVER
238 TERMINAL_CONTROL_BLOCK *TCB;
239 #elif !NCURSES_SP_FUNCS
240 _nc_set_screen(CURRENT_SCREEN);
242 assert(SP_PARM != 0);
243 cols = *(ptrCols(SP_PARM));
244 #ifdef USE_TERM_DRIVER
245 _nc_set_screen(SP_PARM);
246 TCB = (TERMINAL_CONTROL_BLOCK *) new_term;
250 * In setupterm() we did a set_curterm(), but it was before we set
251 * CURRENT_SCREEN. So the "current" screen's terminal pointer was
252 * overwritten with a different terminal. Later, in
253 * _nc_setupscreen(), we set CURRENT_SCREEN and the terminal
254 * pointer in the new screen.
256 * Restore the terminal-pointer for the pre-existing screen, if
260 current->_term = its_term;
262 #ifdef USE_TERM_DRIVER
263 SP_PARM->_term = new_term;
265 new_term = SP_PARM->_term;
268 /* allow user to set maximum escape delay from the environment */
269 if ((value = _nc_getenv_num("ESCDELAY")) >= 0) {
270 #if NCURSES_EXT_FUNCS
271 NCURSES_SP_NAME(set_escdelay) (NCURSES_SP_ARGx value);
277 /* if the terminal type has real soft labels, set those up */
278 if (slk_format && NumLabels > 0 && SLK_STDFMT(slk_format))
279 _nc_slk_initialize(StdScreen(SP_PARM), cols);
281 SP_PARM->_ifd = fileno(_ifp);
282 NCURSES_SP_NAME(typeahead) (NCURSES_SP_ARGx fileno(_ifp));
284 SP_PARM->_use_meta = ((new_term->Ottyb.c_cflag & CSIZE) == CS8 &&
285 !(new_term->Ottyb.c_iflag & ISTRIP)) ||
288 SP_PARM->_use_meta = FALSE;
290 SP_PARM->_endwin = ewInitial;
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->td_scinit(SP_PARM);
313 #else /* ! USE_TERM_DRIVER */
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 SP_PARM->_use_ritm = SGR0_TEST(exit_italics_mode);
330 /* compute movement costs so we can do better move optimization */
333 /* initialize terminal to a sane state */
335 #endif /* USE_TERM_DRIVER */
337 /* Initialize the terminal line settings. */
338 _nc_initscr(NCURSES_SP_ARG);
340 _nc_signal_handler(TRUE);
344 _nc_unlock_global(curses);
349 NCURSES_EXPORT(SCREEN *)
350 newterm(const char *name, FILE *ofp, FILE *ifp)
353 _nc_lock_global(prescreen);
355 rc = NCURSES_SP_NAME(newterm) (CURRENT_SCREEN_PRE, name, ofp, ifp);
357 _nc_unlock_global(prescreen);