1 /****************************************************************************
2 * Copyright 2018-2020,2022 Thomas E. Dickey *
3 * Copyright 1998-2016,2017 Free Software Foundation, Inc. *
5 * Permission is hereby granted, free of charge, to any person obtaining a *
6 * copy of this software and associated documentation files (the *
7 * "Software"), to deal in the Software without restriction, including *
8 * without limitation the rights to use, copy, modify, merge, publish, *
9 * distribute, distribute with modifications, sublicense, and/or sell *
10 * copies of the Software, and to permit persons to whom the Software is *
11 * furnished to do so, subject to the following conditions: *
13 * The above copyright notice and this permission notice shall be included *
14 * in all copies or substantial portions of the Software. *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
19 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
22 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
24 * Except as contained in this notice, the name(s) of the above copyright *
25 * holders shall not be used in advertising or otherwise to promote the *
26 * sale, use or other dealings in this Software without prior written *
28 ****************************************************************************/
30 /****************************************************************************
31 * Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 *
32 * and: Eric S. Raymond <esr@snark.thyrsus.com> *
33 * and: Thomas E. Dickey 1996-on *
34 * and: Juergen Pfeifer 2009 *
35 ****************************************************************************/
40 ** The newterm() function.
44 #include <curses.priv.h>
47 #define CUR SP_TERMTYPE
52 MODULE_ID("$Id: lib_newterm.c,v 1.104 2022/07/09 18:58:58 tom Exp $")
54 #ifdef USE_TERM_DRIVER
55 #define NumLabels InfoOf(SP_PARM).numlabels
57 #define NumLabels num_labels
60 #ifndef ONLCR /* Allows compilation under the QNX 4.2 OS */
65 * SVr4/XSI Curses specify that hardware echo is turned off in initscr, and not
66 * restored during the curses session. The library simulates echo in software.
67 * (The behavior is unspecified if the application enables hardware echo).
69 * The newterm function also initializes terminal settings, and since initscr
70 * is supposed to behave as if it calls newterm, we do it here.
72 static NCURSES_INLINE int
73 _nc_initscr(NCURSES_SP_DCL0)
76 TERMINAL *term = TerminalOf(SP_PARM);
78 /* for extended XPG4 conformance requires cbreak() at this point */
79 /* (SVr4 curses does this anyway) */
80 T((T_CALLED("_nc_initscr(%p) ->term %p"), (void *) SP_PARM, (void *) term));
81 if (NCURSES_SP_NAME(cbreak) (NCURSES_SP_ARG) == OK) {
86 buf.c_lflag &= (unsigned) ~(ECHO | ECHONL);
87 buf.c_iflag &= (unsigned) ~(ICRNL | INLCR | IGNCR);
88 buf.c_oflag &= (unsigned) ~(ONLCR);
90 buf.sg_flags &= ~(ECHO | CRMOD);
91 #elif defined(EXP_WIN32_DRIVER)
92 buf.dwFlagIn = CONMODE_IN_DEFAULT;
93 buf.dwFlagOut = CONMODE_OUT_DEFAULT | VT_FLAG_OUT;
94 if (WINCONSOLE.isTermInfoConsole) {
95 buf.dwFlagIn |= VT_FLAG_IN;
98 memset(&buf, 0, sizeof(buf));
100 result = NCURSES_SP_NAME(_nc_set_tty_mode) (NCURSES_SP_ARGx &buf);
108 * filter() has to be called before either initscr() or newterm(), so there is
109 * apparently no way to make this flag apply to some terminals and not others,
110 * aside from possibly delaying a filter() call until some terminals have been
114 NCURSES_SP_NAME(filter) (NCURSES_SP_DCL0)
117 T((T_CALLED("filter(%p)"), (void *) SP_PARM));
119 if (IsPreScreen(SP_PARM)) {
120 SP_PARM->_filtered = TRUE;
123 _nc_prescreen.filter_mode = TRUE;
133 T((T_CALLED("filter()")));
134 _nc_prescreen.filter_mode = TRUE;
139 #if NCURSES_EXT_FUNCS
141 * An extension, allowing the application to open a new screen without
142 * requiring it to also be filtered.
145 NCURSES_SP_NAME(nofilter) (NCURSES_SP_DCL0)
148 T((T_CALLED("nofilter(%p)"), (void *) SP_PARM));
150 if (IsPreScreen(SP_PARM)) {
151 SP_PARM->_filtered = FALSE;
154 _nc_prescreen.filter_mode = FALSE;
164 T((T_CALLED("nofilter()")));
165 _nc_prescreen.filter_mode = FALSE;
169 #endif /* NCURSES_EXT_FUNCS */
171 NCURSES_EXPORT(SCREEN *)
172 NCURSES_SP_NAME(newterm) (NCURSES_SP_DCLx
181 FILE *_ofp = ofp ? ofp : stdout;
182 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);
204 #if defined(EXP_WIN32_DRIVER)
205 _setmode(fileno(_ifp), _O_BINARY);
206 _setmode(fileno(_ofp), _O_BINARY);
210 /* this loads the capability entry, then sets LINES and COLS */
212 TINFO_SETUP_TERM(&new_term, name,
213 fileno(_ofp), &errret, FALSE) != ERR) {
218 #ifdef USE_TERM_DRIVER
219 assert(new_term != 0);
223 slk_format = SP_PARM->slk_format;
224 filter_mode = SP_PARM->_filtered;
226 slk_format = _nc_globals.slk_format;
227 filter_mode = _nc_prescreen.filter_mode;
231 * This actually allocates the screen structure, and saves the original
234 if (NCURSES_SP_NAME(_nc_setupscreen) (
238 *(ptrLines(SP_PARM)),
242 slk_format) == ERR) {
243 _nc_set_screen(current);
249 #ifdef USE_TERM_DRIVER
250 TERMINAL_CONTROL_BLOCK *TCB;
251 #elif !NCURSES_SP_FUNCS
252 _nc_set_screen(CURRENT_SCREEN);
254 assert(SP_PARM != 0);
255 cols = *(ptrCols(SP_PARM));
256 #ifdef USE_TERM_DRIVER
257 _nc_set_screen(SP_PARM);
258 TCB = (TERMINAL_CONTROL_BLOCK *) new_term;
262 * In setupterm() we did a set_curterm(), but it was before we set
263 * CURRENT_SCREEN. So the "current" screen's terminal pointer was
264 * overwritten with a different terminal. Later, in
265 * _nc_setupscreen(), we set CURRENT_SCREEN and the terminal
266 * pointer in the new screen.
268 * Restore the terminal-pointer for the pre-existing screen, if
272 current->_term = its_term;
274 #ifdef USE_TERM_DRIVER
275 SP_PARM->_term = new_term;
277 new_term = SP_PARM->_term;
280 /* allow user to set maximum escape delay from the environment */
281 if ((value = _nc_getenv_num("ESCDELAY")) >= 0) {
282 #if NCURSES_EXT_FUNCS
283 NCURSES_SP_NAME(set_escdelay) (NCURSES_SP_ARGx value);
289 /* if the terminal type has real soft labels, set those up */
290 if (slk_format && NumLabels > 0 && SLK_STDFMT(slk_format))
291 _nc_slk_initialize(StdScreen(SP_PARM), cols);
293 SP_PARM->_ifd = fileno(_ifp);
294 NCURSES_SP_NAME(typeahead) (NCURSES_SP_ARGx fileno(_ifp));
296 SP_PARM->_use_meta = ((new_term->Ottyb.c_cflag & CSIZE) == CS8 &&
297 !(new_term->Ottyb.c_iflag & ISTRIP)) ||
300 SP_PARM->_use_meta = FALSE;
302 SP_PARM->_endwin = ewInitial;
303 #ifndef USE_TERM_DRIVER
305 * Check whether we can optimize scrolling under dumb terminals in
306 * case we do not have any of these capabilities, scrolling
307 * optimization will be useless.
309 SP_PARM->_scrolling = ((scroll_forward && scroll_reverse) ||
318 NCURSES_SP_NAME(baudrate) (NCURSES_SP_ARG); /* sets a field in the screen structure */
320 SP_PARM->_keytry = 0;
322 /* compute movement costs so we can do better move optimization */
323 #ifdef USE_TERM_DRIVER
324 TCBOf(SP_PARM)->drv->td_scinit(SP_PARM);
325 #else /* ! USE_TERM_DRIVER */
327 * Check for mismatched graphic-rendition capabilities. Most SVr4
328 * terminfo trees contain entries that have rmul or rmso equated to
329 * sgr0 (Solaris curses copes with those entries). We do this only
330 * for curses, since many termcap applications assume that
331 * smso/rmso and smul/rmul are paired, and will not function
332 * properly if we remove rmso or rmul. Curses applications
333 * shouldn't be looking at this detail.
335 #define SGR0_TEST(mode) (mode != 0) && (exit_attribute_mode == 0 || strcmp(mode, exit_attribute_mode))
336 SP_PARM->_use_rmso = SGR0_TEST(exit_standout_mode);
337 SP_PARM->_use_rmul = SGR0_TEST(exit_underline_mode);
339 SP_PARM->_use_ritm = SGR0_TEST(exit_italics_mode);
342 /* compute movement costs so we can do better move optimization */
345 /* initialize terminal to a sane state */
347 #endif /* USE_TERM_DRIVER */
349 /* Initialize the terminal line settings. */
350 _nc_initscr(NCURSES_SP_ARG);
352 _nc_signal_handler(TRUE);
356 _nc_unlock_global(curses);
361 NCURSES_EXPORT(SCREEN *)
362 newterm(const char *name, FILE *ofp, FILE *ifp)
367 _nc_lock_global(prescreen);
369 rc = NCURSES_SP_NAME(newterm) (CURRENT_SCREEN_PRE, name, ofp, ifp);
371 _nc_unlock_global(prescreen);