]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/base/lib_newterm.c
ncurses 6.2 - patch 20200531
[ncurses.git] / ncurses / base / lib_newterm.c
1 /****************************************************************************
2  * Copyright 2018,2020 Thomas E. Dickey                                     *
3  * Copyright 1998-2016,2017 Free Software Foundation, Inc.                  *
4  *                                                                          *
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:                 *
12  *                                                                          *
13  * The above copyright notice and this permission notice shall be included  *
14  * in all copies or substantial portions of the Software.                   *
15  *                                                                          *
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.                               *
23  *                                                                          *
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       *
27  * authorization.                                                           *
28  ****************************************************************************/
29
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  ****************************************************************************/
36
37 /*
38 **      lib_newterm.c
39 **
40 **      The newterm() function.
41 **
42 */
43
44 #include <curses.priv.h>
45
46 #ifndef CUR
47 #define CUR SP_TERMTYPE
48 #endif
49
50 #include <tic.h>
51
52 MODULE_ID("$Id: lib_newterm.c,v 1.102 2020/02/02 23:34:34 tom Exp $")
53
54 #ifdef USE_TERM_DRIVER
55 #define NumLabels      InfoOf(SP_PARM).numlabels
56 #else
57 #define NumLabels      num_labels
58 #endif
59
60 #ifndef ONLCR                   /* Allows compilation under the QNX 4.2 OS */
61 #define ONLCR 0
62 #endif
63
64 /*
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).
68  *
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.
71  */
72 static NCURSES_INLINE int
73 _nc_initscr(NCURSES_SP_DCL0)
74 {
75     int result = ERR;
76     TERMINAL *term = TerminalOf(SP_PARM);
77
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) {
82         TTY buf;
83
84         buf = term->Nttyb;
85 #ifdef TERMIOS
86         buf.c_lflag &= (unsigned) ~(ECHO | ECHONL);
87         buf.c_iflag &= (unsigned) ~(ICRNL | INLCR | IGNCR);
88         buf.c_oflag &= (unsigned) ~(ONLCR);
89 #elif HAVE_SGTTY_H
90         buf.sg_flags &= ~(ECHO | CRMOD);
91 #else
92         memset(&buf, 0, sizeof(buf));
93 #endif
94         result = NCURSES_SP_NAME(_nc_set_tty_mode) (NCURSES_SP_ARGx &buf);
95         if (result == OK)
96             term->Nttyb = buf;
97     }
98     returnCode(result);
99 }
100
101 /*
102  * filter() has to be called before either initscr() or newterm(), so there is
103  * apparently no way to make this flag apply to some terminals and not others,
104  * aside from possibly delaying a filter() call until some terminals have been
105  * initialized.
106  */
107 NCURSES_EXPORT(void)
108 NCURSES_SP_NAME(filter) (NCURSES_SP_DCL0)
109 {
110     START_TRACE();
111     T((T_CALLED("filter(%p)"), (void *) SP_PARM));
112 #if NCURSES_SP_FUNCS
113     if (IsPreScreen(SP_PARM)) {
114         SP_PARM->_filtered = TRUE;
115     }
116 #else
117     _nc_prescreen.filter_mode = TRUE;
118 #endif
119     returnVoid;
120 }
121
122 #if NCURSES_SP_FUNCS
123 NCURSES_EXPORT(void)
124 filter(void)
125 {
126     START_TRACE();
127     T((T_CALLED("filter()")));
128     _nc_prescreen.filter_mode = TRUE;
129     returnVoid;
130 }
131 #endif
132
133 #if NCURSES_EXT_FUNCS
134 /*
135  * An extension, allowing the application to open a new screen without
136  * requiring it to also be filtered.
137  */
138 NCURSES_EXPORT(void)
139 NCURSES_SP_NAME(nofilter) (NCURSES_SP_DCL0)
140 {
141     START_TRACE();
142     T((T_CALLED("nofilter(%p)"), (void *) SP_PARM));
143 #if NCURSES_SP_FUNCS
144     if (IsPreScreen(SP_PARM)) {
145         SP_PARM->_filtered = FALSE;
146     }
147 #else
148     _nc_prescreen.filter_mode = FALSE;
149 #endif
150     returnVoid;
151 }
152
153 #if NCURSES_SP_FUNCS
154 NCURSES_EXPORT(void)
155 nofilter(void)
156 {
157     START_TRACE();
158     T((T_CALLED("nofilter()")));
159     _nc_prescreen.filter_mode = FALSE;
160     returnVoid;
161 }
162 #endif
163 #endif /* NCURSES_EXT_FUNCS */
164
165 NCURSES_EXPORT(SCREEN *)
166 NCURSES_SP_NAME(newterm) (NCURSES_SP_DCLx
167                           const char *name,
168                           FILE *ofp,
169                           FILE *ifp)
170 {
171     int errret;
172     SCREEN *result = 0;
173     SCREEN *current;
174     TERMINAL *its_term;
175     FILE *_ofp = ofp ? ofp : stdout;
176     FILE *_ifp = ifp ? ifp : stdin;
177     TERMINAL *new_term = 0;
178
179     START_TRACE();
180     T((T_CALLED("newterm(%p, \"%s\", %p,%p)"),
181        (void *) SP_PARM,
182        (name ? name : ""),
183        (void *) ofp,
184        (void *) ifp));
185
186 #if NCURSES_SP_FUNCS
187     assert(SP_PARM != 0);
188     if (SP_PARM == 0)
189         returnSP(SP_PARM);
190 #endif
191
192     _nc_init_pthreads();
193     _nc_lock_global(curses);
194
195     current = CURRENT_SCREEN;
196     its_term = (current ? current->_term : 0);
197
198     INIT_TERM_DRIVER();
199     /* this loads the capability entry, then sets LINES and COLS */
200     if (
201            TINFO_SETUP_TERM(&new_term, name,
202                             fileno(_ofp), &errret, FALSE) != ERR) {
203         int slk_format;
204         int filter_mode;
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
218
219         /*
220          * This actually allocates the screen structure, and saves the original
221          * terminal settings.
222          */
223         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) {
232             _nc_set_screen(current);
233             result = 0;
234         } else {
235             int value;
236             int cols;
237
238 #ifdef USE_TERM_DRIVER
239             TERMINAL_CONTROL_BLOCK *TCB;
240 #elif !NCURSES_SP_FUNCS
241             _nc_set_screen(CURRENT_SCREEN);
242 #endif
243             assert(SP_PARM != 0);
244             cols = *(ptrCols(SP_PARM));
245 #ifdef USE_TERM_DRIVER
246             _nc_set_screen(SP_PARM);
247             TCB = (TERMINAL_CONTROL_BLOCK *) new_term;
248             TCB->csp = SP_PARM;
249 #endif
250             /*
251              * In setupterm() we did a set_curterm(), but it was before we set
252              * CURRENT_SCREEN.  So the "current" screen's terminal pointer was
253              * overwritten with a different terminal.  Later, in
254              * _nc_setupscreen(), we set CURRENT_SCREEN and the terminal
255              * pointer in the new screen.
256              *
257              * Restore the terminal-pointer for the pre-existing screen, if
258              * any.
259              */
260             if (current)
261                 current->_term = its_term;
262
263 #ifdef USE_TERM_DRIVER
264             SP_PARM->_term = new_term;
265 #else
266             new_term = SP_PARM->_term;
267 #endif
268
269             /* allow user to set maximum escape delay from the environment */
270             if ((value = _nc_getenv_num("ESCDELAY")) >= 0) {
271 #if NCURSES_EXT_FUNCS
272                 NCURSES_SP_NAME(set_escdelay) (NCURSES_SP_ARGx value);
273 #else
274                 ESCDELAY = value;
275 #endif
276             }
277
278             /* if the terminal type has real soft labels, set those up */
279             if (slk_format && NumLabels > 0 && SLK_STDFMT(slk_format))
280                 _nc_slk_initialize(StdScreen(SP_PARM), cols);
281
282             SP_PARM->_ifd = fileno(_ifp);
283             NCURSES_SP_NAME(typeahead) (NCURSES_SP_ARGx fileno(_ifp));
284 #ifdef TERMIOS
285             SP_PARM->_use_meta = ((new_term->Ottyb.c_cflag & CSIZE) == CS8 &&
286                                   !(new_term->Ottyb.c_iflag & ISTRIP)) ||
287                 USE_KLIBC_KBD;
288 #else
289             SP_PARM->_use_meta = FALSE;
290 #endif
291             SP_PARM->_endwin = ewInitial;
292 #ifndef USE_TERM_DRIVER
293             /*
294              * Check whether we can optimize scrolling under dumb terminals in
295              * case we do not have any of these capabilities, scrolling
296              * optimization will be useless.
297              */
298             SP_PARM->_scrolling = ((scroll_forward && scroll_reverse) ||
299                                    ((parm_rindex ||
300                                      parm_insert_line ||
301                                      insert_line) &&
302                                     (parm_index ||
303                                      parm_delete_line ||
304                                      delete_line)));
305 #endif
306
307             NCURSES_SP_NAME(baudrate) (NCURSES_SP_ARG);         /* sets a field in the screen structure */
308
309             SP_PARM->_keytry = 0;
310
311             /* compute movement costs so we can do better move optimization */
312 #ifdef USE_TERM_DRIVER
313             TCBOf(SP_PARM)->drv->td_scinit(SP_PARM);
314 #else /* ! USE_TERM_DRIVER */
315             /*
316              * Check for mismatched graphic-rendition capabilities.  Most SVr4
317              * terminfo trees contain entries that have rmul or rmso equated to
318              * sgr0 (Solaris curses copes with those entries).  We do this only
319              * for curses, since many termcap applications assume that
320              * smso/rmso and smul/rmul are paired, and will not function
321              * properly if we remove rmso or rmul.  Curses applications
322              * shouldn't be looking at this detail.
323              */
324 #define SGR0_TEST(mode) (mode != 0) && (exit_attribute_mode == 0 || strcmp(mode, exit_attribute_mode))
325             SP_PARM->_use_rmso = SGR0_TEST(exit_standout_mode);
326             SP_PARM->_use_rmul = SGR0_TEST(exit_underline_mode);
327 #if USE_ITALIC
328             SP_PARM->_use_ritm = SGR0_TEST(exit_italics_mode);
329 #endif
330
331             /* compute movement costs so we can do better move optimization */
332             _nc_mvcur_init();
333
334             /* initialize terminal to a sane state */
335             _nc_screen_init();
336 #endif /* USE_TERM_DRIVER */
337
338             /* Initialize the terminal line settings. */
339             _nc_initscr(NCURSES_SP_ARG);
340
341             _nc_signal_handler(TRUE);
342             result = SP_PARM;
343         }
344     }
345     _nc_unlock_global(curses);
346     returnSP(result);
347 }
348
349 #if NCURSES_SP_FUNCS
350 NCURSES_EXPORT(SCREEN *)
351 newterm(const char *name, FILE *ofp, FILE *ifp)
352 {
353     SCREEN *rc;
354     _nc_lock_global(prescreen);
355     START_TRACE();
356     rc = NCURSES_SP_NAME(newterm) (CURRENT_SCREEN_PRE, name, ofp, ifp);
357     _nc_forget_prescr();
358     _nc_unlock_global(prescreen);
359     return rc;
360 }
361 #endif