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