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