1 /* $OpenBSD: lib_newterm.c,v 1.14 2023/10/17 09:52:08 nicm Exp $ */
2
3 /****************************************************************************
4 * Copyright 2018-2020,2022 Thomas E. Dickey *
5 * Copyright 1998-2016,2017 Free Software Foundation, Inc. *
6 * *
7 * Permission is hereby granted, free of charge, to any person obtaining a *
8 * copy of this software and associated documentation files (the *
9 * "Software"), to deal in the Software without restriction, including *
10 * without limitation the rights to use, copy, modify, merge, publish, *
11 * distribute, distribute with modifications, sublicense, and/or sell *
12 * copies of the Software, and to permit persons to whom the Software is *
13 * furnished to do so, subject to the following conditions: *
14 * *
15 * The above copyright notice and this permission notice shall be included *
16 * in all copies or substantial portions of the Software. *
17 * *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
21 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
22 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
23 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
24 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
25 * *
26 * Except as contained in this notice, the name(s) of the above copyright *
27 * holders shall not be used in advertising or otherwise to promote the *
28 * sale, use or other dealings in this Software without prior written *
29 * authorization. *
30 ****************************************************************************/
31
32 /****************************************************************************
33 * Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 *
34 * and: Eric S. Raymond <esr@snark.thyrsus.com> *
35 * and: Thomas E. Dickey 1996-on *
36 * and: Juergen Pfeifer 2009 *
37 ****************************************************************************/
38
39 /*
40 ** lib_newterm.c
41 **
42 ** The newterm() function.
43 **
44 */
45
46 #include <curses.priv.h>
47
48 #ifndef CUR
49 #define CUR SP_TERMTYPE
50 #endif
51
52 #include <tic.h>
53
54 MODULE_ID("$Id: lib_newterm.c,v 1.14 2023/10/17 09:52:08 nicm Exp $")
55
56 #ifdef USE_TERM_DRIVER
57 #define NumLabels InfoOf(SP_PARM).numlabels
58 #else
59 #define NumLabels num_labels
60 #endif
61
62 #ifndef ONLCR /* Allows compilation under the QNX 4.2 OS */
63 #define ONLCR 0
64 #endif
65
66 /*
67 * SVr4/XSI Curses specify that hardware echo is turned off in initscr, and not
68 * restored during the curses session. The library simulates echo in software.
69 * (The behavior is unspecified if the application enables hardware echo).
70 *
71 * The newterm function also initializes terminal settings, and since initscr
72 * is supposed to behave as if it calls newterm, we do it here.
73 */
74 static NCURSES_INLINE int
_nc_initscr(NCURSES_SP_DCL0)75 _nc_initscr(NCURSES_SP_DCL0)
76 {
77 int result = ERR;
78 TERMINAL *term = TerminalOf(SP_PARM);
79
80 /* for extended XPG4 conformance requires cbreak() at this point */
81 /* (SVr4 curses does this anyway) */
82 T((T_CALLED("_nc_initscr(%p) ->term %p"), (void *) SP_PARM, (void *) term));
83 if (NCURSES_SP_NAME(cbreak) (NCURSES_SP_ARG) == OK) {
84 TTY buf;
85
86 buf = term->Nttyb;
87 #ifdef TERMIOS
88 buf.c_lflag &= (unsigned) ~(ECHO | ECHONL);
89 buf.c_iflag &= (unsigned) ~(ICRNL | INLCR | IGNCR);
90 buf.c_oflag &= (unsigned) ~(ONLCR);
91 #elif HAVE_SGTTY_H
92 buf.sg_flags &= ~(ECHO | CRMOD);
93 #elif defined(EXP_WIN32_DRIVER)
94 buf.dwFlagIn = CONMODE_IN_DEFAULT;
95 buf.dwFlagOut = CONMODE_OUT_DEFAULT | VT_FLAG_OUT;
96 if (WINCONSOLE.isTermInfoConsole) {
97 buf.dwFlagIn |= VT_FLAG_IN;
98 }
99 #else
100 memset(&buf, 0, sizeof(buf));
101 #endif
102 result = NCURSES_SP_NAME(_nc_set_tty_mode) (NCURSES_SP_ARGx &buf);
103 if (result == OK)
104 term->Nttyb = buf;
105 }
106 returnCode(result);
107 }
108
109 /*
110 * filter() has to be called before either initscr() or newterm(), so there is
111 * apparently no way to make this flag apply to some terminals and not others,
112 * aside from possibly delaying a filter() call until some terminals have been
113 * initialized.
114 */
115 NCURSES_EXPORT(void)
NCURSES_SP_NAME(filter)116 NCURSES_SP_NAME(filter) (NCURSES_SP_DCL0)
117 {
118 START_TRACE();
119 T((T_CALLED("filter(%p)"), (void *) SP_PARM));
120 #if NCURSES_SP_FUNCS
121 if (IsPreScreen(SP_PARM)) {
122 SP_PARM->_filtered = TRUE;
123 }
124 #else
125 _nc_prescreen.filter_mode = TRUE;
126 #endif
127 returnVoid;
128 }
129
130 #if NCURSES_SP_FUNCS
131 NCURSES_EXPORT(void)
filter(void)132 filter(void)
133 {
134 START_TRACE();
135 T((T_CALLED("filter()")));
136 _nc_prescreen.filter_mode = TRUE;
137 returnVoid;
138 }
139 #endif
140
141 #if NCURSES_EXT_FUNCS
142 /*
143 * An extension, allowing the application to open a new screen without
144 * requiring it to also be filtered.
145 */
146 NCURSES_EXPORT(void)
NCURSES_SP_NAME(nofilter)147 NCURSES_SP_NAME(nofilter) (NCURSES_SP_DCL0)
148 {
149 START_TRACE();
150 T((T_CALLED("nofilter(%p)"), (void *) SP_PARM));
151 #if NCURSES_SP_FUNCS
152 if (IsPreScreen(SP_PARM)) {
153 SP_PARM->_filtered = FALSE;
154 }
155 #else
156 _nc_prescreen.filter_mode = FALSE;
157 #endif
158 returnVoid;
159 }
160
161 #if NCURSES_SP_FUNCS
162 NCURSES_EXPORT(void)
nofilter(void)163 nofilter(void)
164 {
165 START_TRACE();
166 T((T_CALLED("nofilter()")));
167 _nc_prescreen.filter_mode = FALSE;
168 returnVoid;
169 }
170 #endif
171 #endif /* NCURSES_EXT_FUNCS */
172
173 NCURSES_EXPORT(SCREEN *)
NCURSES_SP_NAME(newterm)174 NCURSES_SP_NAME(newterm) (NCURSES_SP_DCLx
175 const char *name,
176 FILE *ofp,
177 FILE *ifp)
178 {
179 int errret;
180 SCREEN *result = 0;
181 SCREEN *current;
182 TERMINAL *its_term;
183 FILE *_ofp = ofp ? ofp : stdout;
184 FILE *_ifp = ifp ? ifp : stdin;
185 TERMINAL *new_term = 0;
186
187 START_TRACE();
188 T((T_CALLED("newterm(%p, \"%s\", %p,%p)"),
189 (void *) SP_PARM,
190 (name ? name : ""),
191 (void *) ofp,
192 (void *) ifp));
193
194 #if NCURSES_SP_FUNCS
195 assert(SP_PARM != 0);
196 if (SP_PARM == 0)
197 returnSP(SP_PARM);
198 #endif
199
200 _nc_init_pthreads();
201 _nc_lock_global(curses);
202
203 current = CURRENT_SCREEN;
204 its_term = (current ? current->_term : 0);
205
206 #if defined(EXP_WIN32_DRIVER)
207 _setmode(fileno(_ifp), _O_BINARY);
208 _setmode(fileno(_ofp), _O_BINARY);
209 #endif
210
211 INIT_TERM_DRIVER();
212 /* this loads the capability entry, then sets LINES and COLS */
213 if (
214 TINFO_SETUP_TERM(&new_term, name,
215 fileno(_ofp), &errret, FALSE) != ERR) {
216 int slk_format;
217 int filter_mode;
218
219 _nc_set_screen(0);
220 #ifdef USE_TERM_DRIVER
221 assert(new_term != 0);
222 #endif
223
224 #if NCURSES_SP_FUNCS
225 slk_format = SP_PARM->slk_format;
226 filter_mode = SP_PARM->_filtered;
227 #else
228 slk_format = _nc_globals.slk_format;
229 filter_mode = _nc_prescreen.filter_mode;
230 #endif
231
232 /*
233 * This actually allocates the screen structure, and saves the original
234 * terminal settings.
235 */
236 if (NCURSES_SP_NAME(_nc_setupscreen) (
237 #if NCURSES_SP_FUNCS
238 &SP_PARM,
239 #endif
240 *(ptrLines(SP_PARM)),
241 *(ptrCols(SP_PARM)),
242 _ofp,
243 filter_mode,
244 slk_format) == ERR) {
245 _nc_set_screen(current);
246 result = 0;
247 } else {
248 int value;
249 int cols;
250
251 #ifdef USE_TERM_DRIVER
252 TERMINAL_CONTROL_BLOCK *TCB;
253 #elif !NCURSES_SP_FUNCS
254 _nc_set_screen(CURRENT_SCREEN);
255 #endif
256 assert(SP_PARM != 0);
257 cols = *(ptrCols(SP_PARM));
258 #ifdef USE_TERM_DRIVER
259 _nc_set_screen(SP_PARM);
260 TCB = (TERMINAL_CONTROL_BLOCK *) new_term;
261 TCB->csp = SP_PARM;
262 #endif
263 /*
264 * In setupterm() we did a set_curterm(), but it was before we set
265 * CURRENT_SCREEN. So the "current" screen's terminal pointer was
266 * overwritten with a different terminal. Later, in
267 * _nc_setupscreen(), we set CURRENT_SCREEN and the terminal
268 * pointer in the new screen.
269 *
270 * Restore the terminal-pointer for the pre-existing screen, if
271 * any.
272 */
273 if (current)
274 current->_term = its_term;
275
276 #ifdef USE_TERM_DRIVER
277 SP_PARM->_term = new_term;
278 #else
279 new_term = SP_PARM->_term;
280 #endif
281
282 /* allow user to set maximum escape delay from the environment */
283 if ((value = _nc_getenv_num("ESCDELAY")) >= 0) {
284 #if NCURSES_EXT_FUNCS
285 NCURSES_SP_NAME(set_escdelay) (NCURSES_SP_ARGx value);
286 #else
287 ESCDELAY = value;
288 #endif
289 }
290
291 /* if the terminal type has real soft labels, set those up */
292 if (slk_format && NumLabels > 0 && SLK_STDFMT(slk_format))
293 _nc_slk_initialize(StdScreen(SP_PARM), cols);
294
295 SP_PARM->_ifd = fileno(_ifp);
296 NCURSES_SP_NAME(typeahead) (NCURSES_SP_ARGx fileno(_ifp));
297 #ifdef TERMIOS
298 SP_PARM->_use_meta = ((new_term->Ottyb.c_cflag & CSIZE) == CS8 &&
299 !(new_term->Ottyb.c_iflag & ISTRIP)) ||
300 USE_KLIBC_KBD;
301 #else
302 SP_PARM->_use_meta = FALSE;
303 #endif
304 SP_PARM->_endwin = ewInitial;
305 #ifndef USE_TERM_DRIVER
306 /*
307 * Check whether we can optimize scrolling under dumb terminals in
308 * case we do not have any of these capabilities, scrolling
309 * optimization will be useless.
310 */
311 SP_PARM->_scrolling = ((scroll_forward && scroll_reverse) ||
312 ((parm_rindex ||
313 parm_insert_line ||
314 insert_line) &&
315 (parm_index ||
316 parm_delete_line ||
317 delete_line)));
318 #endif
319
320 NCURSES_SP_NAME(baudrate) (NCURSES_SP_ARG); /* sets a field in the screen structure */
321
322 SP_PARM->_keytry = 0;
323
324 /* compute movement costs so we can do better move optimization */
325 #ifdef USE_TERM_DRIVER
326 TCBOf(SP_PARM)->drv->td_scinit(SP_PARM);
327 #else /* ! USE_TERM_DRIVER */
328 /*
329 * Check for mismatched graphic-rendition capabilities. Most SVr4
330 * terminfo trees contain entries that have rmul or rmso equated to
331 * sgr0 (Solaris curses copes with those entries). We do this only
332 * for curses, since many termcap applications assume that
333 * smso/rmso and smul/rmul are paired, and will not function
334 * properly if we remove rmso or rmul. Curses applications
335 * shouldn't be looking at this detail.
336 */
337 #define SGR0_TEST(mode) (mode != 0) && (exit_attribute_mode == 0 || strcmp(mode, exit_attribute_mode))
338 SP_PARM->_use_rmso = SGR0_TEST(exit_standout_mode);
339 SP_PARM->_use_rmul = SGR0_TEST(exit_underline_mode);
340 #if USE_ITALIC
341 SP_PARM->_use_ritm = SGR0_TEST(exit_italics_mode);
342 #endif
343
344 /* compute movement costs so we can do better move optimization */
345 _nc_mvcur_init();
346
347 /* initialize terminal to a sane state */
348 _nc_screen_init();
349 #endif /* USE_TERM_DRIVER */
350
351 /* Initialize the terminal line settings. */
352 _nc_initscr(NCURSES_SP_ARG);
353
354 _nc_signal_handler(TRUE);
355 result = SP_PARM;
356 }
357 }
358 _nc_unlock_global(curses);
359 returnSP(result);
360 }
361
362 #if NCURSES_SP_FUNCS
363 NCURSES_EXPORT(SCREEN *)
newterm(const char * name,FILE * ofp,FILE * ifp)364 newterm(const char *name, FILE *ofp, FILE *ifp)
365 {
366 SCREEN *rc;
367
368 _nc_init_pthreads();
369 _nc_lock_global(prescreen);
370 START_TRACE();
371 rc = NCURSES_SP_NAME(newterm) (CURRENT_SCREEN_PRE, name, ofp, ifp);
372 _nc_forget_prescr();
373 _nc_unlock_global(prescreen);
374
375 return rc;
376 }
377 #endif
378