]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/base/lib_set_term.c
ncurses 6.2 - patch 20210403
[ncurses.git] / ncurses / base / lib_set_term.c
1 /****************************************************************************
2  * Copyright 2018-2020,2021 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_set_term.c
39 **
40 **      The routine set_term().
41 **
42 */
43
44 #include <curses.priv.h>
45 #include <tic.h>
46
47 #if USE_GPM_SUPPORT
48 #ifdef HAVE_LIBDL
49 /* use dynamic loader to avoid linkage dependency */
50 #include <dlfcn.h>
51 #endif
52 #endif
53
54 #undef CUR
55 #define CUR SP_TERMTYPE
56
57 MODULE_ID("$Id: lib_set_term.c,v 1.176 2021/04/03 22:23:39 tom Exp $")
58
59 #ifdef USE_TERM_DRIVER
60 #define MaxColors      InfoOf(sp).maxcolors
61 #define NumLabels      InfoOf(sp).numlabels
62 #else
63 #define MaxColors      max_colors
64 #define NumLabels      num_labels
65 #endif
66
67 NCURSES_EXPORT(SCREEN *)
68 set_term(SCREEN *screenp)
69 {
70     SCREEN *oldSP;
71     SCREEN *newSP;
72
73     T((T_CALLED("set_term(%p)"), (void *) screenp));
74
75     _nc_lock_global(curses);
76
77     oldSP = CURRENT_SCREEN;
78     _nc_set_screen(screenp);
79     newSP = screenp;
80
81     if (newSP != 0) {
82         TINFO_SET_CURTERM(newSP, newSP->_term);
83 #if !USE_REENTRANT
84         curscr = CurScreen(newSP);
85         newscr = NewScreen(newSP);
86         stdscr = StdScreen(newSP);
87         COLORS = newSP->_color_count;
88         COLOR_PAIRS = newSP->_pair_count;
89 #endif
90     } else {
91         TINFO_SET_CURTERM(oldSP, 0);
92 #if !USE_REENTRANT
93         curscr = 0;
94         newscr = 0;
95         stdscr = 0;
96         COLORS = 0;
97         COLOR_PAIRS = 0;
98 #endif
99     }
100
101     _nc_unlock_global(curses);
102
103     T((T_RETURN("%p"), (void *) oldSP));
104     return (oldSP);
105 }
106
107 static void
108 _nc_free_keytry(TRIES * kt)
109 {
110     if (kt != 0) {
111         _nc_free_keytry(kt->child);
112         _nc_free_keytry(kt->sibling);
113         free(kt);
114     }
115 }
116
117 static bool
118 delink_screen(SCREEN *sp)
119 {
120     SCREEN *last = 0;
121     SCREEN *temp;
122     bool result = FALSE;
123
124     for (each_screen(temp)) {
125         if (temp == sp) {
126             if (last)
127                 last->_next_screen = sp->_next_screen;
128             else
129                 _nc_screen_chain = sp->_next_screen;
130             result = TRUE;
131             break;
132         }
133         last = temp;
134     }
135     return result;
136 }
137
138 /*
139  * Free the storage associated with the given SCREEN sp.
140  */
141 NCURSES_EXPORT(void)
142 delscreen(SCREEN *sp)
143 {
144
145     T((T_CALLED("delscreen(%p)"), (void *) sp));
146
147     _nc_lock_global(curses);
148     if (delink_screen(sp)) {
149 #ifdef USE_SP_RIPOFF
150         ripoff_t *rop;
151         if (safe_ripoff_sp && safe_ripoff_sp != safe_ripoff_stack) {
152             for (rop = safe_ripoff_stack;
153                  rop != safe_ripoff_sp && (rop - safe_ripoff_stack) < N_RIPS;
154                  rop++) {
155                 if (rop->win) {
156                     (void) delwin(rop->win);
157                     rop->win = 0;
158                 }
159             }
160         }
161 #endif
162
163         (void) _nc_freewin(CurScreen(sp));
164         (void) _nc_freewin(NewScreen(sp));
165         (void) _nc_freewin(StdScreen(sp));
166
167         if (sp->_slk != 0) {
168
169             if (sp->_slk->ent != 0) {
170                 int i;
171
172                 for (i = 0; i < sp->_slk->labcnt; ++i) {
173                     FreeIfNeeded(sp->_slk->ent[i].ent_text);
174                     FreeIfNeeded(sp->_slk->ent[i].form_text);
175                 }
176                 free(sp->_slk->ent);
177             }
178             free(sp->_slk);
179             sp->_slk = 0;
180         }
181
182         _nc_free_keytry(sp->_keytry);
183         sp->_keytry = 0;
184
185         _nc_free_keytry(sp->_key_ok);
186         sp->_key_ok = 0;
187
188         FreeIfNeeded(sp->_current_attr);
189
190         FreeIfNeeded(sp->_color_table);
191         FreeIfNeeded(sp->_color_pairs);
192
193         FreeIfNeeded(sp->_oldnum_list);
194         FreeIfNeeded(sp->oldhash);
195         FreeIfNeeded(sp->newhash);
196         FreeIfNeeded(sp->hashtab);
197
198         FreeIfNeeded(sp->_acs_map);
199         FreeIfNeeded(sp->_screen_acs_map);
200
201         NCURSES_SP_NAME(_nc_flush) (NCURSES_SP_ARG);
202         NCURSES_SP_NAME(del_curterm) (NCURSES_SP_ARGx sp->_term);
203         FreeIfNeeded(sp->out_buffer);
204         if (_nc_find_prescr() == sp) {
205             _nc_forget_prescr();
206         }
207 #if USE_GPM_SUPPORT
208 #ifdef HAVE_LIBDL
209         if (sp->_dlopen_gpm != 0) {
210             dlclose(sp->_dlopen_gpm);
211             sp->_dlopen_gpm = 0;
212         }
213 #endif
214 #endif /* USE_GPM_SUPPORT */
215         free(sp);
216
217         /*
218          * If this was the current screen, reset everything that the
219          * application might try to use (except cur_term, which may have
220          * multiple references in different screens).
221          */
222         if (sp == CURRENT_SCREEN) {
223 #if !USE_REENTRANT
224             curscr = 0;
225             newscr = 0;
226             stdscr = 0;
227             COLORS = 0;
228             COLOR_PAIRS = 0;
229 #endif
230             _nc_set_screen(0);
231 #if USE_WIDEC_SUPPORT
232             if (SP == 0) {
233                 FreeIfNeeded(_nc_wacs);
234                 _nc_wacs = 0;
235             }
236 #endif
237         }
238     }
239     _nc_unlock_global(curses);
240
241     returnVoid;
242 }
243
244 static bool
245 no_mouse_event(SCREEN *sp GCC_UNUSED)
246 {
247     return FALSE;
248 }
249
250 static bool
251 no_mouse_inline(SCREEN *sp GCC_UNUSED)
252 {
253     return FALSE;
254 }
255
256 static bool
257 no_mouse_parse(SCREEN *sp GCC_UNUSED, int code GCC_UNUSED)
258 {
259     return TRUE;
260 }
261
262 static void
263 no_mouse_resume(SCREEN *sp GCC_UNUSED)
264 {
265 }
266
267 static void
268 no_mouse_wrap(SCREEN *sp GCC_UNUSED)
269 {
270 }
271
272 #if NCURSES_EXT_FUNCS && USE_COLORFGBG
273 static const char *
274 extract_fgbg(const char *src, int *result)
275 {
276     const char *dst = 0;
277     char *tmp = 0;
278     long value = strtol(src, &tmp, 0);
279
280     if ((dst = tmp) == 0) {
281         dst = src;
282     } else if (value >= 0) {
283         *result = (int) value;
284     }
285     while (*dst != 0 && *dst != ';')
286         dst++;
287     if (*dst == ';')
288         dst++;
289     return dst;
290 }
291 #endif
292
293 #define ReturnScreenError() do { _nc_set_screen(0); \
294                             returnCode(ERR); } while (0)
295
296 /* OS-independent screen initializations */
297 NCURSES_EXPORT(int)
298 NCURSES_SP_NAME(_nc_setupscreen) (
299 #if NCURSES_SP_FUNCS
300                                      SCREEN **spp,
301 #endif
302                                      int slines,
303                                      int scolumns,
304                                      FILE *output,
305                                      int filtered,
306                                      int slk_format)
307 {
308 #ifndef USE_TERM_DRIVER
309     static const TTY null_TTY;  /* all zeros iff uninitialized */
310 #endif
311     char *env;
312     int bottom_stolen = 0;
313     SCREEN *sp;
314 #ifndef USE_TERM_DRIVER
315     bool support_cookies = USE_XMC_SUPPORT;
316 #endif
317
318     T((T_CALLED("_nc_setupscreen(%d, %d, %p, %d, %d)"),
319        slines, scolumns, (void *) output, filtered, slk_format));
320
321     assert(CURRENT_SCREEN == 0);        /* has been reset in newterm() ! */
322
323 #if NCURSES_SP_FUNCS
324     assert(spp != 0);
325     sp = *spp;
326
327     if (!sp) {
328         sp = _nc_alloc_screen_sp();
329         T(("_nc_alloc_screen_sp %p", (void *) sp));
330         *spp = sp;
331     }
332     if (sp == NULL) {
333         ReturnScreenError();
334     }
335     if ((sp->_acs_map = typeCalloc(chtype, ACS_LEN)) == NULL) {
336         ReturnScreenError();
337     }
338     if ((sp->_screen_acs_map = typeCalloc(bool, ACS_LEN)) == NULL) {
339         free(sp->_acs_map);
340         ReturnScreenError();
341     }
342
343     T(("created SP %p", (void *) sp));
344     sp->_next_screen = _nc_screen_chain;
345     _nc_screen_chain = sp;
346
347     if ((sp->_current_attr = typeCalloc(NCURSES_CH_T, 1)) == 0) {
348         ReturnScreenError();
349     }
350 #else
351     if (!_nc_alloc_screen()
352         || ((SP->_acs_map = typeCalloc(chtype, ACS_LEN)) == 0)
353         || ((SP->_screen_acs_map = typeCalloc(bool, ACS_LEN)) == 0)) {
354         returnCode(ERR);
355     }
356
357     T(("created SP %p", (void *) SP));
358
359     sp = SP;                    /* fixup so SET_LINES and SET_COLS works */
360     sp->_next_screen = _nc_screen_chain;
361     _nc_screen_chain = sp;
362
363     if ((sp->_current_attr = typeCalloc(NCURSES_CH_T, 1)) == 0) {
364         returnCode(ERR);
365     }
366 #endif
367
368     /*
369      * We should always check the screensize, just in case.
370      */
371     _nc_set_screen(sp);
372     sp->_term = cur_term;
373 #ifdef USE_TERM_DRIVER
374     TCBOf(sp)->csp = sp;
375     _nc_get_screensize(sp, sp->_term, &slines, &scolumns);
376 #else
377     _nc_get_screensize(sp, &slines, &scolumns);
378 #endif
379     SET_LINES(slines);
380     SET_COLS(scolumns);
381
382     T((T_CREATE("screen %s %dx%d"),
383        NCURSES_SP_NAME(termname) (NCURSES_SP_ARG), slines, scolumns));
384
385     sp->_filtered = filtered;
386
387     /* implement filter mode */
388     if (filtered) {
389         slines = 1;
390         SET_LINES(slines);
391 #ifdef USE_TERM_DRIVER
392         CallDriver(sp, td_setfilter);
393 #else
394         /* *INDENT-EQLS* */
395         clear_screen     = ABSENT_STRING;
396         cursor_address   = ABSENT_STRING;
397         cursor_down      = ABSENT_STRING;
398         cursor_up        = ABSENT_STRING;
399         parm_down_cursor = ABSENT_STRING;
400         parm_up_cursor   = ABSENT_STRING;
401         row_address      = ABSENT_STRING;
402         cursor_home      = carriage_return;
403
404         if (back_color_erase)
405             clr_eos = ABSENT_STRING;
406
407 #endif
408         T(("filter screensize %dx%d", slines, scolumns));
409     }
410 #ifdef __DJGPP__
411     T(("setting output mode to binary"));
412     fflush(output);
413     setmode(output, O_BINARY);
414 #endif
415 #if defined(EXP_WIN32_DRIVER)
416     T(("setting output mode to binary"));
417     fflush(output);
418     _setmode(fileno(output), _O_BINARY);
419 #endif
420     NCURSES_SP_NAME(_nc_set_buffer) (NCURSES_SP_ARGx output, TRUE);
421     sp->_lines = (NCURSES_SIZE_T) slines;
422     sp->_lines_avail = (NCURSES_SIZE_T) slines;
423     sp->_columns = (NCURSES_SIZE_T) scolumns;
424
425     fflush(output);
426     sp->_ofd = output ? fileno(output) : -1;
427     sp->_ofp = output;
428 #if defined(EXP_WIN32_DRIVER)
429     if (output)
430         _setmode(fileno(output), _O_BINARY);
431 #endif
432     sp->out_limit = (size_t) ((2 + slines) * (6 + scolumns));
433     if ((sp->out_buffer = malloc(sp->out_limit)) == 0)
434         sp->out_limit = 0;
435     sp->out_inuse = 0;
436
437     SP_PRE_INIT(sp);
438     SetNoPadding(sp);
439
440 #if NCURSES_EXT_FUNCS
441     sp->_default_color = FALSE;
442     sp->_has_sgr_39_49 = FALSE;
443
444     /*
445      * Set our assumption of the terminal's default foreground and background
446      * colors.  The curs_color man-page states that we can assume that the
447      * background is black.  The origin of this assumption appears to be
448      * terminals that displayed colored text, but no colored backgrounds, e.g.,
449      * the first colored terminals around 1980.  More recent ones with better
450      * technology can display not only colored backgrounds, but all
451      * combinations.  So a terminal might be something other than "white" on
452      * black (green/black looks monochrome too), but black on white or even
453      * on ivory.
454      *
455      * White-on-black is the simplest thing to use for monochrome.  Almost
456      * all applications that use color paint both text and background, so
457      * the distinction is moot.  But a few do not - which is why we leave this
458      * configurable (a better solution is to use assume_default_colors() for
459      * the rare applications that do require that sort of appearance, since
460      * is appears that more users expect to be able to make a white-on-black
461      * or black-on-white display under control of the application than not).
462      */
463 #ifdef USE_ASSUMED_COLOR
464     sp->_default_fg = COLOR_WHITE;
465     sp->_default_bg = COLOR_BLACK;
466 #else
467     sp->_default_fg = COLOR_DEFAULT;
468     sp->_default_bg = COLOR_DEFAULT;
469 #endif
470
471     /*
472      * Allow those assumed/default color assumptions to be overridden at
473      * runtime:
474      */
475     if ((env = getenv("NCURSES_ASSUMED_COLORS")) != 0) {
476         int fg, bg;
477         char sep1, sep2;
478         int count = sscanf(env, "%d%c%d%c", &fg, &sep1, &bg, &sep2);
479         if (count >= 1) {
480             sp->_default_fg = ((fg >= 0 && fg < MaxColors) ? fg : COLOR_DEFAULT);
481             if (count >= 3) {
482                 sp->_default_bg = ((bg >= 0 && bg < MaxColors) ? bg : COLOR_DEFAULT);
483             }
484             TR(TRACE_CHARPUT | TRACE_MOVE,
485                ("from environment assumed fg=%d, bg=%d",
486                 sp->_default_fg,
487                 sp->_default_bg));
488         }
489     }
490 #if USE_COLORFGBG
491     /*
492      * If rxvt's $COLORFGBG variable is set, use it to specify the assumed
493      * default colors.  Note that rxvt (mis)uses bold colors, equating a bold
494      * color to that value plus 8.  We'll only use the non-bold color for now -
495      * decide later if it is worth having default attributes as well.
496      */
497     if (getenv("COLORFGBG") != 0) {
498         const char *p = getenv("COLORFGBG");
499         TR(TRACE_CHARPUT | TRACE_MOVE, ("decoding COLORFGBG %s", p));
500         p = extract_fgbg(p, &(sp->_default_fg));
501         p = extract_fgbg(p, &(sp->_default_bg));
502         if (*p)                 /* assume rxvt was compiled with xpm support */
503             p = extract_fgbg(p, &(sp->_default_bg));
504         TR(TRACE_CHARPUT | TRACE_MOVE, ("decoded fg=%d, bg=%d",
505                                         sp->_default_fg, sp->_default_bg));
506         if (sp->_default_fg >= MaxColors) {
507             if (set_a_foreground != ABSENT_STRING
508                 && !strcmp(set_a_foreground, "\033[3%p1%dm")) {
509                 set_a_foreground = strdup("\033[3%?%p1%{8}%>%t9%e%p1%d%;m");
510             } else {
511                 sp->_default_fg %= MaxColors;
512             }
513         }
514         if (sp->_default_bg >= MaxColors) {
515             if (set_a_background != ABSENT_STRING
516                 && !strcmp(set_a_background, "\033[4%p1%dm")) {
517                 set_a_background = strdup("\033[4%?%p1%{8}%>%t9%e%p1%d%;m");
518             } else {
519                 sp->_default_bg %= MaxColors;
520             }
521         }
522     }
523 #endif
524 #endif /* NCURSES_EXT_FUNCS */
525
526     sp->_maxclick = DEFAULT_MAXCLICK;
527     sp->_mouse_event = no_mouse_event;
528     sp->_mouse_inline = no_mouse_inline;
529     sp->_mouse_parse = no_mouse_parse;
530     sp->_mouse_resume = no_mouse_resume;
531     sp->_mouse_wrap = no_mouse_wrap;
532     sp->_mouse_fd = -1;
533
534     /*
535      * If we've no magic cookie support, we suppress attributes that xmc would
536      * affect, i.e., the attributes that affect the rendition of a space.
537      */
538     sp->_ok_attributes = NCURSES_SP_NAME(termattrs) (NCURSES_SP_ARG);
539     if (NCURSES_SP_NAME(has_colors) (NCURSES_SP_ARG)) {
540         sp->_ok_attributes |= A_COLOR;
541     }
542 #ifdef USE_TERM_DRIVER
543     _nc_cookie_init(sp);
544 #else
545 #if USE_XMC_SUPPORT
546     /*
547      * If we have no magic-cookie support compiled-in, or if it is suppressed
548      * in the environment, reset the support-flag.
549      */
550     if (magic_cookie_glitch >= 0) {
551         if (getenv("NCURSES_NO_MAGIC_COOKIE") != 0) {
552             support_cookies = FALSE;
553         }
554     }
555 #endif
556
557     if (!support_cookies && magic_cookie_glitch >= 0) {
558         T(("will disable attributes to work w/o magic cookies"));
559     }
560
561     if (magic_cookie_glitch > 0) {      /* tvi, wyse */
562
563         sp->_xmc_triggers = sp->_ok_attributes & XMC_CONFLICT;
564 #if 0
565         /*
566          * We "should" treat colors as an attribute.  The wyse350 (and its
567          * clones) appear to be the only ones that have both colors and magic
568          * cookies.
569          */
570         if (has_colors()) {
571             sp->_xmc_triggers |= A_COLOR;
572         }
573 #endif
574         sp->_xmc_suppress = sp->_xmc_triggers & (chtype) ~(A_BOLD);
575
576         T(("magic cookie attributes %s", _traceattr(sp->_xmc_suppress)));
577         /*
578          * Supporting line-drawing may be possible.  But make the regular
579          * video attributes work first.
580          */
581         acs_chars = ABSENT_STRING;
582         ena_acs = ABSENT_STRING;
583         enter_alt_charset_mode = ABSENT_STRING;
584         exit_alt_charset_mode = ABSENT_STRING;
585 #if USE_XMC_SUPPORT
586         /*
587          * To keep the cookie support simple, suppress all of the optimization
588          * hooks except for clear_screen and the cursor addressing.
589          */
590         if (support_cookies) {
591             clr_eol = ABSENT_STRING;
592             clr_eos = ABSENT_STRING;
593             set_attributes = ABSENT_STRING;
594         }
595 #endif
596     } else if (magic_cookie_glitch == 0) {      /* hpterm */
597     }
598
599     /*
600      * If magic cookies are not supported, cancel the strings that set
601      * video attributes.
602      */
603     if (!support_cookies && magic_cookie_glitch >= 0) {
604         magic_cookie_glitch = ABSENT_NUMERIC;
605         set_attributes = ABSENT_STRING;
606         enter_blink_mode = ABSENT_STRING;
607         enter_bold_mode = ABSENT_STRING;
608         enter_dim_mode = ABSENT_STRING;
609         enter_reverse_mode = ABSENT_STRING;
610         enter_standout_mode = ABSENT_STRING;
611         enter_underline_mode = ABSENT_STRING;
612     }
613
614     /* initialize normal acs before wide, since we use mapping in the latter */
615 #if !USE_WIDEC_SUPPORT
616     if (_nc_unicode_locale() && _nc_locale_breaks_acs(sp->_term)) {
617         acs_chars = NULL;
618         ena_acs = NULL;
619         enter_alt_charset_mode = NULL;
620         exit_alt_charset_mode = NULL;
621         set_attributes = NULL;
622     }
623 #endif
624 #endif
625
626     NCURSES_SP_NAME(_nc_init_acs) (NCURSES_SP_ARG);
627 #if USE_WIDEC_SUPPORT
628     sp->_screen_unicode = _nc_unicode_locale();
629     if (_nc_wacs == 0) {
630         _nc_init_wacs();
631     }
632     if (_nc_wacs == 0) {
633         ReturnScreenError();
634     }
635
636     sp->_screen_acs_fix = (sp->_screen_unicode
637                            && _nc_locale_breaks_acs(sp->_term));
638 #endif
639     env = _nc_get_locale();
640     sp->_legacy_coding = ((env == 0)
641                           || !strcmp(env, "C")
642                           || !strcmp(env, "POSIX"));
643     T(("legacy-coding %d", sp->_legacy_coding));
644
645     sp->_nc_sp_idcok = TRUE;
646     sp->_nc_sp_idlok = FALSE;
647
648     sp->oldhash = 0;
649     sp->newhash = 0;
650
651     T(("creating newscr"));
652     NewScreen(sp) = NCURSES_SP_NAME(newwin) (NCURSES_SP_ARGx slines, scolumns,
653                                              0, 0);
654     if (NewScreen(sp) == 0) {
655         ReturnScreenError();
656     }
657     T(("creating curscr"));
658     CurScreen(sp) = NCURSES_SP_NAME(newwin) (NCURSES_SP_ARGx slines, scolumns,
659                                              0, 0);
660     if (CurScreen(sp) == 0) {
661         ReturnScreenError();
662     }
663 #if !USE_REENTRANT
664     newscr = NewScreen(sp);
665     curscr = CurScreen(sp);
666 #endif
667 #if USE_SIZECHANGE
668     sp->_resize = NCURSES_SP_NAME(resizeterm);
669     sp->_ungetch = safe_ungetch;
670 #endif
671
672     NewScreen(sp)->_clear = TRUE;
673     CurScreen(sp)->_clear = FALSE;
674
675     /*
676      * Get the current tty-modes. setupterm() may already have done this,
677      * unless we use the term-driver.
678      */
679 #ifndef USE_TERM_DRIVER
680     if (cur_term != 0 &&
681         !memcmp(&cur_term->Ottyb, &null_TTY, sizeof(TTY)))
682 #endif
683     {
684         NCURSES_SP_NAME(def_shell_mode) (NCURSES_SP_ARG);
685         NCURSES_SP_NAME(def_prog_mode) (NCURSES_SP_ARG);
686     }
687
688     if (safe_ripoff_sp && safe_ripoff_sp != safe_ripoff_stack) {
689         ripoff_t *rop;
690
691         for (rop = safe_ripoff_stack;
692              rop != safe_ripoff_sp && (rop - safe_ripoff_stack) < N_RIPS;
693              rop++) {
694
695             /* If we must simulate soft labels, grab off the line to be used.
696                We assume that we must simulate, if it is none of the standard
697                formats (4-4 or 3-2-3) for which there may be some hardware
698                support. */
699             if (rop->hook == _nc_slk_initialize) {
700                 if (!(NumLabels <= 0 || !SLK_STDFMT(slk_format))) {
701                     continue;
702                 }
703             }
704             if (rop->hook) {
705                 int count;
706                 WINDOW *w;
707
708                 count = (rop->line < 0) ? -rop->line : rop->line;
709                 T(("ripping off %i lines at %s", count,
710                    ((rop->line < 0)
711                     ? "bottom"
712                     : "top")));
713
714                 w = NCURSES_SP_NAME(newwin) (NCURSES_SP_ARGx
715                                              count, scolumns,
716                                              ((rop->line < 0)
717                                               ? sp->_lines_avail - count
718                                               : 0),
719                                              0);
720                 if (w) {
721                     rop->win = w;
722                     rop->hook(w, scolumns);
723                 } else {
724                     ReturnScreenError();
725                 }
726                 if (rop->line < 0) {
727                     bottom_stolen += count;
728                 } else {
729                     sp->_topstolen = (NCURSES_SIZE_T) (sp->_topstolen + count);
730                 }
731                 sp->_lines_avail = (NCURSES_SIZE_T) (sp->_lines_avail - count);
732             }
733         }
734         /* reset the stack */
735         safe_ripoff_sp = safe_ripoff_stack;
736     }
737
738     T(("creating stdscr"));
739     assert((sp->_lines_avail + sp->_topstolen + bottom_stolen) == slines);
740     if ((StdScreen(sp) = NCURSES_SP_NAME(newwin) (NCURSES_SP_ARGx
741                                                   sp->_lines_avail,
742                                                   scolumns, 0, 0)) == 0) {
743         ReturnScreenError();
744     }
745     SET_LINES(sp->_lines_avail);
746 #if !USE_REENTRANT
747     stdscr = StdScreen(sp);
748 #endif
749     sp->_prescreen = FALSE;
750     returnCode(OK);
751 }
752
753 #if NCURSES_SP_FUNCS
754 NCURSES_EXPORT(int)
755 _nc_setupscreen(int slines GCC_UNUSED,
756                 int scolumns GCC_UNUSED,
757                 FILE *output,
758                 int filtered,
759                 int slk_format)
760 {
761     SCREEN *sp = 0;
762     int rc = NCURSES_SP_NAME(_nc_setupscreen) (&sp,
763                                                slines,
764                                                scolumns,
765                                                output,
766                                                filtered,
767                                                slk_format);
768     if (rc != OK)
769         _nc_set_screen(0);
770     return rc;
771 }
772 #endif
773
774 /*
775  * The internal implementation interprets line as the number of lines to rip
776  * off from the top or bottom.
777  */
778 NCURSES_EXPORT(int)
779 NCURSES_SP_NAME(_nc_ripoffline) (NCURSES_SP_DCLx
780                                  int line,
781                                  int (*init) (WINDOW *, int))
782 {
783     int code = ERR;
784     TR_FUNC_BFR(1);
785
786     START_TRACE();
787     T((T_CALLED("ripoffline(%p,%d,%s)"),
788        (void *) SP_PARM, line,
789        TR_FUNC_ARG(0, init)));
790
791 #if NCURSES_SP_FUNCS
792     if (SP_PARM != 0 && SP_PARM->_prescreen)
793 #endif
794     {
795         if (line == 0) {
796             code = OK;
797         } else {
798             if (safe_ripoff_sp == 0) {
799                 safe_ripoff_sp = safe_ripoff_stack;
800             }
801             if (safe_ripoff_sp < safe_ripoff_stack + N_RIPS) {
802                 safe_ripoff_sp->line = line;
803                 safe_ripoff_sp->hook = init;
804                 (safe_ripoff_sp)++;
805                 T(("ripped-off %d:%d chunks",
806                    (int) (safe_ripoff_sp - safe_ripoff_stack), N_RIPS));
807                 code = OK;
808             }
809         }
810     }
811
812     returnCode(code);
813 }
814
815 #if NCURSES_SP_FUNCS
816 NCURSES_EXPORT(int)
817 _nc_ripoffline(int line, int (*init) (WINDOW *, int))
818 {
819     int rc;
820     _nc_lock_global(prescreen);
821     START_TRACE();
822     rc = NCURSES_SP_NAME(_nc_ripoffline) (CURRENT_SCREEN_PRE, line, init);
823     _nc_unlock_global(prescreen);
824     return rc;
825 }
826 #endif
827
828 NCURSES_EXPORT(int)
829 NCURSES_SP_NAME(ripoffline) (NCURSES_SP_DCLx
830                              int line,
831                              int (*init) (WINDOW *, int))
832 {
833     START_TRACE();
834     return NCURSES_SP_NAME(_nc_ripoffline) (NCURSES_SP_ARGx
835                                             (line < 0) ? -1 : 1,
836                                             init);
837 }
838
839 #if NCURSES_SP_FUNCS
840 NCURSES_EXPORT(int)
841 ripoffline(int line, int (*init) (WINDOW *, int))
842 {
843     int rc;
844     _nc_lock_global(prescreen);
845     START_TRACE();
846     rc = NCURSES_SP_NAME(ripoffline) (CURRENT_SCREEN_PRE, line, init);
847     _nc_unlock_global(prescreen);
848     return rc;
849 }
850 #endif