blob: edbf956da04ef7321f35c38fb7bd0dc4c487504d [file] [log] [blame]
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +05301/****************************************************************************
Steve Kondikae271bc2015-11-15 02:50:53 +01002 * Copyright (c) 1998-2013,2014 Free Software Foundation, Inc. *
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +05303 * *
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: *
11 * *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
14 * *
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. *
22 * *
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 *
26 * authorization. *
27 ****************************************************************************/
28
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 *
Steve Kondikae271bc2015-11-15 02:50:53 +010033 * and: Juergen Pfeifer 2009 *
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053034 ****************************************************************************/
35
36/*
37** lib_newterm.c
38**
39** The newterm() function.
40**
41*/
42
43#include <curses.priv.h>
44
Steve Kondikae271bc2015-11-15 02:50:53 +010045#ifndef CUR
46#define CUR SP_TERMTYPE
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053047#endif
48
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053049#include <tic.h>
50
Steve Kondikae271bc2015-11-15 02:50:53 +010051MODULE_ID("$Id: lib_newterm.c,v 1.92 2014/04/26 18:00:39 tom Exp $")
52
53#ifdef USE_TERM_DRIVER
54#define NumLabels InfoOf(SP_PARM).numlabels
55#else
56#define NumLabels num_labels
57#endif
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053058
59#ifndef ONLCR /* Allows compilation under the QNX 4.2 OS */
60#define ONLCR 0
61#endif
62
63/*
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).
67 *
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.
70 */
71static NCURSES_INLINE int
Steve Kondikae271bc2015-11-15 02:50:53 +010072_nc_initscr(NCURSES_SP_DCL0)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053073{
74 int result = ERR;
Steve Kondikae271bc2015-11-15 02:50:53 +010075 TERMINAL *term = TerminalOf(SP_PARM);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053076
77 /* for extended XPG4 conformance requires cbreak() at this point */
78 /* (SVr4 curses does this anyway) */
Steve Kondikae271bc2015-11-15 02:50:53 +010079 if (NCURSES_SP_NAME(cbreak) (NCURSES_SP_ARG) == OK) {
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053080 TTY buf;
81
Steve Kondikae271bc2015-11-15 02:50:53 +010082 buf = term->Nttyb;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053083#ifdef TERMIOS
Steve Kondikae271bc2015-11-15 02:50:53 +010084 buf.c_lflag &= (unsigned) ~(ECHO | ECHONL);
85 buf.c_iflag &= (unsigned) ~(ICRNL | INLCR | IGNCR);
86 buf.c_oflag &= (unsigned) ~(ONLCR);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053087#elif HAVE_SGTTY_H
88 buf.sg_flags &= ~(ECHO | CRMOD);
89#else
90 memset(&buf, 0, sizeof(buf));
91#endif
Steve Kondikae271bc2015-11-15 02:50:53 +010092 result = NCURSES_SP_NAME(_nc_set_tty_mode) (NCURSES_SP_ARGx &buf);
93 if (result == OK)
94 term->Nttyb = buf;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053095 }
96 return result;
97}
98
99/*
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
103 * initialized.
104 */
105NCURSES_EXPORT(void)
Steve Kondikae271bc2015-11-15 02:50:53 +0100106NCURSES_SP_NAME(filter) (NCURSES_SP_DCL0)
107{
108 START_TRACE();
109 T((T_CALLED("filter(%p)"), (void *) SP_PARM));
110#if NCURSES_SP_FUNCS
111 if (IsPreScreen(SP_PARM)) {
112 SP_PARM->_filtered = TRUE;
113 }
114#else
115 _nc_prescreen.filter_mode = TRUE;
116#endif
117 returnVoid;
118}
119
120#if NCURSES_SP_FUNCS
121NCURSES_EXPORT(void)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530122filter(void)
123{
124 START_TRACE();
Steve Kondikae271bc2015-11-15 02:50:53 +0100125 T((T_CALLED("filter()")));
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530126 _nc_prescreen.filter_mode = TRUE;
127 returnVoid;
128}
Steve Kondikae271bc2015-11-15 02:50:53 +0100129#endif
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530130
131#if NCURSES_EXT_FUNCS
132/*
133 * An extension, allowing the application to open a new screen without
134 * requiring it to also be filtered.
135 */
136NCURSES_EXPORT(void)
Steve Kondikae271bc2015-11-15 02:50:53 +0100137NCURSES_SP_NAME(nofilter) (NCURSES_SP_DCL0)
138{
139 START_TRACE();
140 T((T_CALLED("nofilter(%p)"), (void *) SP_PARM));
141#if NCURSES_SP_FUNCS
142 if (IsPreScreen(SP_PARM)) {
143 SP_PARM->_filtered = FALSE;
144 }
145#else
146 _nc_prescreen.filter_mode = FALSE;
147#endif
148 returnVoid;
149}
150
151#if NCURSES_SP_FUNCS
152NCURSES_EXPORT(void)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530153nofilter(void)
154{
155 START_TRACE();
Steve Kondikae271bc2015-11-15 02:50:53 +0100156 T((T_CALLED("nofilter()")));
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530157 _nc_prescreen.filter_mode = FALSE;
158 returnVoid;
159}
160#endif
Steve Kondikae271bc2015-11-15 02:50:53 +0100161#endif /* NCURSES_EXT_FUNCS */
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530162
163NCURSES_EXPORT(SCREEN *)
Steve Kondikae271bc2015-11-15 02:50:53 +0100164NCURSES_SP_NAME(newterm) (NCURSES_SP_DCLx
165 NCURSES_CONST char *name,
166 FILE *ofp,
167 FILE *ifp)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530168{
169 int value;
170 int errret;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530171 SCREEN *result = 0;
Steve Kondikae271bc2015-11-15 02:50:53 +0100172 SCREEN *current;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530173 TERMINAL *its_term;
Steve Kondikae271bc2015-11-15 02:50:53 +0100174 FILE *_ofp = ofp ? ofp : stdout;
175 FILE *_ifp = ifp ? ifp : stdin;
176 int cols;
177 int slk_format;
178 int filter_mode;
179 TERMINAL *new_term = 0;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530180
181 START_TRACE();
Steve Kondikae271bc2015-11-15 02:50:53 +0100182 T((T_CALLED("newterm(%p, \"%s\", %p,%p)"),
183 (void *) SP_PARM,
184 (name ? name : ""),
185 (void *) ofp,
186 (void *) ifp));
187
188#if NCURSES_SP_FUNCS
189 assert(SP_PARM != 0);
190 if (SP_PARM == 0)
191 returnSP(SP_PARM);
192#endif
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530193
194 _nc_init_pthreads();
195 _nc_lock_global(curses);
196
Steve Kondikae271bc2015-11-15 02:50:53 +0100197 current = CURRENT_SCREEN;
198 its_term = (current ? current->_term : 0);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530199
Steve Kondikae271bc2015-11-15 02:50:53 +0100200 INIT_TERM_DRIVER();
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530201 /* this loads the capability entry, then sets LINES and COLS */
Steve Kondikae271bc2015-11-15 02:50:53 +0100202 if (
203 TINFO_SETUP_TERM(&new_term, name,
204 fileno(_ofp), &errret, FALSE) != ERR) {
205
206 _nc_set_screen(0);
207#ifdef USE_TERM_DRIVER
208 assert(new_term != 0);
209#endif
210
211#if NCURSES_SP_FUNCS
212 slk_format = SP_PARM->slk_format;
213 filter_mode = SP_PARM->_filtered;
214#else
215 slk_format = _nc_globals.slk_format;
216 filter_mode = _nc_prescreen.filter_mode;
217#endif
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530218
219 /*
220 * This actually allocates the screen structure, and saves the original
221 * terminal settings.
222 */
Steve Kondikae271bc2015-11-15 02:50:53 +0100223 if (NCURSES_SP_NAME(_nc_setupscreen) (
224#if NCURSES_SP_FUNCS
225 &SP_PARM,
226#endif
227 *(ptrLines(SP_PARM)),
228 *(ptrCols(SP_PARM)),
229 _ofp,
230 filter_mode,
231 slk_format) == ERR) {
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530232 _nc_set_screen(current);
233 result = 0;
234 } else {
Steve Kondikae271bc2015-11-15 02:50:53 +0100235#ifdef USE_TERM_DRIVER
236 TERMINAL_CONTROL_BLOCK *TCB;
237#elif !NCURSES_SP_FUNCS
238 _nc_set_screen(CURRENT_SCREEN);
239#endif
240 assert(SP_PARM != 0);
241 cols = *(ptrCols(SP_PARM));
242#ifdef USE_TERM_DRIVER
243 _nc_set_screen(SP_PARM);
244 TCB = (TERMINAL_CONTROL_BLOCK *) new_term;
245 TCB->csp = SP_PARM;
246#endif
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530247 /*
248 * In setupterm() we did a set_curterm(), but it was before we set
Steve Kondikae271bc2015-11-15 02:50:53 +0100249 * CURRENT_SCREEN. So the "current" screen's terminal pointer was
250 * overwritten with a different terminal. Later, in
251 * _nc_setupscreen(), we set CURRENT_SCREEN and the terminal
252 * pointer in the new screen.
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530253 *
254 * Restore the terminal-pointer for the pre-existing screen, if
255 * any.
256 */
257 if (current)
258 current->_term = its_term;
259
Steve Kondikae271bc2015-11-15 02:50:53 +0100260#ifdef USE_TERM_DRIVER
261 SP_PARM->_term = new_term;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530262#else
Steve Kondikae271bc2015-11-15 02:50:53 +0100263 new_term = SP_PARM->_term;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530264#endif
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530265
Steve Kondikae271bc2015-11-15 02:50:53 +0100266 /* allow user to set maximum escape delay from the environment */
267 if ((value = _nc_getenv_num("ESCDELAY")) >= 0) {
268 NCURSES_SP_NAME(set_escdelay) (NCURSES_SP_ARGx value);
269 }
270
271 /* if the terminal type has real soft labels, set those up */
272 if (slk_format && NumLabels > 0 && SLK_STDFMT(slk_format))
273 _nc_slk_initialize(StdScreen(SP_PARM), cols);
274
275 SP_PARM->_ifd = fileno(_ifp);
276 NCURSES_SP_NAME(typeahead) (NCURSES_SP_ARGx fileno(_ifp));
277#ifdef TERMIOS
278 SP_PARM->_use_meta = ((new_term->Ottyb.c_cflag & CSIZE) == CS8 &&
279 !(new_term->Ottyb.c_iflag & ISTRIP)) ||
280 USE_KLIBC_KBD;
281#else
282 SP_PARM->_use_meta = FALSE;
283#endif
284 SP_PARM->_endwin = FALSE;
285#ifndef USE_TERM_DRIVER
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530286 /*
287 * Check whether we can optimize scrolling under dumb terminals in
288 * case we do not have any of these capabilities, scrolling
289 * optimization will be useless.
290 */
Steve Kondikae271bc2015-11-15 02:50:53 +0100291 SP_PARM->_scrolling = ((scroll_forward && scroll_reverse) ||
292 ((parm_rindex ||
293 parm_insert_line ||
294 insert_line) &&
295 (parm_index ||
296 parm_delete_line ||
297 delete_line)));
298#endif
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530299
Steve Kondikae271bc2015-11-15 02:50:53 +0100300 NCURSES_SP_NAME(baudrate) (NCURSES_SP_ARG); /* sets a field in the screen structure */
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530301
Steve Kondikae271bc2015-11-15 02:50:53 +0100302 SP_PARM->_keytry = 0;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530303
Steve Kondikae271bc2015-11-15 02:50:53 +0100304 /* compute movement costs so we can do better move optimization */
305#ifdef USE_TERM_DRIVER
306 TCBOf(SP_PARM)->drv->td_scinit(SP_PARM);
307#else /* ! USE_TERM_DRIVER */
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530308 /*
309 * Check for mismatched graphic-rendition capabilities. Most SVr4
310 * terminfo trees contain entries that have rmul or rmso equated to
311 * sgr0 (Solaris curses copes with those entries). We do this only
312 * for curses, since many termcap applications assume that
313 * smso/rmso and smul/rmul are paired, and will not function
314 * properly if we remove rmso or rmul. Curses applications
315 * shouldn't be looking at this detail.
316 */
317#define SGR0_TEST(mode) (mode != 0) && (exit_attribute_mode == 0 || strcmp(mode, exit_attribute_mode))
Steve Kondikae271bc2015-11-15 02:50:53 +0100318 SP_PARM->_use_rmso = SGR0_TEST(exit_standout_mode);
319 SP_PARM->_use_rmul = SGR0_TEST(exit_underline_mode);
320#if USE_ITALIC
321 SP_PARM->_use_ritm = SGR0_TEST(exit_italics_mode);
322#endif
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530323
324 /* compute movement costs so we can do better move optimization */
325 _nc_mvcur_init();
326
327 /* initialize terminal to a sane state */
328 _nc_screen_init();
Steve Kondikae271bc2015-11-15 02:50:53 +0100329#endif /* USE_TERM_DRIVER */
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530330
331 /* Initialize the terminal line settings. */
Steve Kondikae271bc2015-11-15 02:50:53 +0100332 _nc_initscr(NCURSES_SP_ARG);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530333
334 _nc_signal_handler(TRUE);
Steve Kondikae271bc2015-11-15 02:50:53 +0100335 result = SP_PARM;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530336 }
337 }
338 _nc_unlock_global(curses);
339 returnSP(result);
340}
Steve Kondikae271bc2015-11-15 02:50:53 +0100341
342#if NCURSES_SP_FUNCS
343NCURSES_EXPORT(SCREEN *)
344newterm(NCURSES_CONST char *name, FILE *ofp, FILE *ifp)
345{
346 return NCURSES_SP_NAME(newterm) (CURRENT_SCREEN_PRE, name, ofp, ifp);
347}
348#endif