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