]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/base/lib_newterm.c
ncurses 6.0 - patch 20170701
[ncurses.git] / ncurses / base / lib_newterm.c
1 /****************************************************************************
2  * Copyright (c) 1998-2016,2017 Free Software Foundation, Inc.              *
3  *                                                                          *
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                 *
33  *     and: Juergen Pfeifer                         2009                    *
34  ****************************************************************************/
35
36 /*
37 **      lib_newterm.c
38 **
39 **      The newterm() function.
40 **
41 */
42
43 #include <curses.priv.h>
44
45 #ifndef CUR
46 #define CUR SP_TERMTYPE
47 #endif
48
49 #include <tic.h>
50
51 MODULE_ID("$Id: lib_newterm.c,v 1.99 2017/07/01 18:14:07 tom Exp $")
52
53 #ifdef USE_TERM_DRIVER
54 #define NumLabels      InfoOf(SP_PARM).numlabels
55 #else
56 #define NumLabels      num_labels
57 #endif
58
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  */
71 static NCURSES_INLINE int
72 _nc_initscr(NCURSES_SP_DCL0)
73 {
74     int result = ERR;
75     TERMINAL *term = TerminalOf(SP_PARM);
76
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) {
81         TTY buf;
82
83         buf = term->Nttyb;
84 #ifdef TERMIOS
85         buf.c_lflag &= (unsigned) ~(ECHO | ECHONL);
86         buf.c_iflag &= (unsigned) ~(ICRNL | INLCR | IGNCR);
87         buf.c_oflag &= (unsigned) ~(ONLCR);
88 #elif HAVE_SGTTY_H
89         buf.sg_flags &= ~(ECHO | CRMOD);
90 #else
91         memset(&buf, 0, sizeof(buf));
92 #endif
93         result = NCURSES_SP_NAME(_nc_set_tty_mode) (NCURSES_SP_ARGx &buf);
94         if (result == OK)
95             term->Nttyb = buf;
96     }
97     returnCode(result);
98 }
99
100 /*
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
104  * initialized.
105  */
106 NCURSES_EXPORT(void)
107 NCURSES_SP_NAME(filter) (NCURSES_SP_DCL0)
108 {
109     START_TRACE();
110     T((T_CALLED("filter(%p)"), (void *) SP_PARM));
111 #if NCURSES_SP_FUNCS
112     if (IsPreScreen(SP_PARM)) {
113         SP_PARM->_filtered = TRUE;
114     }
115 #else
116     _nc_prescreen.filter_mode = TRUE;
117 #endif
118     returnVoid;
119 }
120
121 #if NCURSES_SP_FUNCS
122 NCURSES_EXPORT(void)
123 filter(void)
124 {
125     START_TRACE();
126     T((T_CALLED("filter()")));
127     _nc_prescreen.filter_mode = TRUE;
128     returnVoid;
129 }
130 #endif
131
132 #if NCURSES_EXT_FUNCS
133 /*
134  * An extension, allowing the application to open a new screen without
135  * requiring it to also be filtered.
136  */
137 NCURSES_EXPORT(void)
138 NCURSES_SP_NAME(nofilter) (NCURSES_SP_DCL0)
139 {
140     START_TRACE();
141     T((T_CALLED("nofilter(%p)"), (void *) SP_PARM));
142 #if NCURSES_SP_FUNCS
143     if (IsPreScreen(SP_PARM)) {
144         SP_PARM->_filtered = FALSE;
145     }
146 #else
147     _nc_prescreen.filter_mode = FALSE;
148 #endif
149     returnVoid;
150 }
151
152 #if NCURSES_SP_FUNCS
153 NCURSES_EXPORT(void)
154 nofilter(void)
155 {
156     START_TRACE();
157     T((T_CALLED("nofilter()")));
158     _nc_prescreen.filter_mode = FALSE;
159     returnVoid;
160 }
161 #endif
162 #endif /* NCURSES_EXT_FUNCS */
163
164 NCURSES_EXPORT(SCREEN *)
165 NCURSES_SP_NAME(newterm) (NCURSES_SP_DCLx
166                           NCURSES_CONST char *name,
167                           FILE *ofp,
168                           FILE *ifp)
169 {
170     int errret;
171     SCREEN *result = 0;
172     SCREEN *current;
173     TERMINAL *its_term;
174     FILE *_ofp = ofp ? ofp : stdout;
175     FILE *_ifp = ifp ? ifp : stdin;
176     TERMINAL *new_term = 0;
177
178     START_TRACE();
179     T((T_CALLED("newterm(%p, \"%s\", %p,%p)"),
180        (void *) SP_PARM,
181        (name ? name : ""),
182        (void *) ofp,
183        (void *) ifp));
184
185 #if NCURSES_SP_FUNCS
186     assert(SP_PARM != 0);
187     if (SP_PARM == 0)
188         returnSP(SP_PARM);
189 #endif
190
191     _nc_init_pthreads();
192     _nc_lock_global(curses);
193
194     current = CURRENT_SCREEN;
195     its_term = (current ? current->_term : 0);
196
197     INIT_TERM_DRIVER();
198     /* this loads the capability entry, then sets LINES and COLS */
199     if (
200            TINFO_SETUP_TERM(&new_term, name,
201                             fileno(_ofp), &errret, FALSE) != ERR) {
202         int slk_format;
203         int filter_mode;
204
205         _nc_set_screen(0);
206 #ifdef USE_TERM_DRIVER
207         assert(new_term != 0);
208 #endif
209
210 #if NCURSES_SP_FUNCS
211         slk_format = SP_PARM->slk_format;
212         filter_mode = SP_PARM->_filtered;
213 #else
214         slk_format = _nc_globals.slk_format;
215         filter_mode = _nc_prescreen.filter_mode;
216 #endif
217
218         /*
219          * This actually allocates the screen structure, and saves the original
220          * terminal settings.
221          */
222         if (NCURSES_SP_NAME(_nc_setupscreen) (
223 #if NCURSES_SP_FUNCS
224                                                  &SP_PARM,
225 #endif
226                                                  *(ptrLines(SP_PARM)),
227                                                  *(ptrCols(SP_PARM)),
228                                                  _ofp,
229                                                  filter_mode,
230                                                  slk_format) == ERR) {
231             _nc_set_screen(current);
232             result = 0;
233         } else {
234             int value;
235             int cols;
236
237 #ifdef USE_TERM_DRIVER
238             TERMINAL_CONTROL_BLOCK *TCB;
239 #elif !NCURSES_SP_FUNCS
240             _nc_set_screen(CURRENT_SCREEN);
241 #endif
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;
247             TCB->csp = SP_PARM;
248 #endif
249             /*
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.
255              *
256              * Restore the terminal-pointer for the pre-existing screen, if
257              * any.
258              */
259             if (current)
260                 current->_term = its_term;
261
262 #ifdef USE_TERM_DRIVER
263             SP_PARM->_term = new_term;
264 #else
265             new_term = SP_PARM->_term;
266 #endif
267
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);
272 #else
273                 ESCDELAY = value;
274 #endif
275             }
276
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);
280
281             SP_PARM->_ifd = fileno(_ifp);
282             NCURSES_SP_NAME(typeahead) (NCURSES_SP_ARGx fileno(_ifp));
283 #ifdef TERMIOS
284             SP_PARM->_use_meta = ((new_term->Ottyb.c_cflag & CSIZE) == CS8 &&
285                                   !(new_term->Ottyb.c_iflag & ISTRIP)) ||
286                 USE_KLIBC_KBD;
287 #else
288             SP_PARM->_use_meta = FALSE;
289 #endif
290             SP_PARM->_endwin = FALSE;
291 #ifndef USE_TERM_DRIVER
292             /*
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.
296              */
297             SP_PARM->_scrolling = ((scroll_forward && scroll_reverse) ||
298                                    ((parm_rindex ||
299                                      parm_insert_line ||
300                                      insert_line) &&
301                                     (parm_index ||
302                                      parm_delete_line ||
303                                      delete_line)));
304 #endif
305
306             NCURSES_SP_NAME(baudrate) (NCURSES_SP_ARG);         /* sets a field in the screen structure */
307
308             SP_PARM->_keytry = 0;
309
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 */
314             /*
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.
322              */
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);
326 #if USE_ITALIC
327             SP_PARM->_use_ritm = SGR0_TEST(exit_italics_mode);
328 #endif
329
330             /* compute movement costs so we can do better move optimization */
331             _nc_mvcur_init();
332
333             /* initialize terminal to a sane state */
334             _nc_screen_init();
335 #endif /* USE_TERM_DRIVER */
336
337             /* Initialize the terminal line settings. */
338             _nc_initscr(NCURSES_SP_ARG);
339
340             _nc_signal_handler(TRUE);
341             result = SP_PARM;
342         }
343     }
344     _nc_unlock_global(curses);
345     returnSP(result);
346 }
347
348 #if NCURSES_SP_FUNCS
349 NCURSES_EXPORT(SCREEN *)
350 newterm(NCURSES_CONST char *name, FILE *ofp, FILE *ifp)
351 {
352     SCREEN *rc;
353     _nc_lock_global(prescreen);
354     START_TRACE();
355     rc = NCURSES_SP_NAME(newterm) (CURRENT_SCREEN_PRE, name, ofp, ifp);
356     _nc_forget_prescr();
357     _nc_unlock_global(prescreen);
358     return rc;
359 }
360 #endif