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