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