]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/base/lib_set_term.c
9bb46fc7f8b4849cccabf3789b8f5b91cf15c4f1
[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.131 2009/10/31 22:28:04 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 = 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"), (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 = 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)"), (void *) 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     static SCREEN *sp;
286
287     START_TRACE();
288     T((T_CALLED("new_prescr()")));
289
290     if (sp == 0) {
291         sp = _nc_alloc_screen_sp();
292         if (sp != 0) {
293             sp->rsp = sp->rippedoff;
294             sp->_filtered = _nc_prescreen.filter_mode;
295             sp->_use_env = _nc_prescreen.use_env;
296 #if NCURSES_NO_PADDING
297             sp->_no_padding = _nc_prescreen._no_padding;
298 #endif
299             sp->slk_format = 0;
300             sp->_slk = 0;
301             sp->_prescreen = TRUE;
302             SP_PRE_INIT(sp);
303 #if USE_REENTRANT
304             sp->_TABSIZE = _nc_prescreen._TABSIZE;
305             sp->_ESCDELAY = _nc_prescreen._ESCDELAY;
306 #endif
307         }
308     }
309     returnSP(sp);
310 }
311 #endif
312
313 #define ReturnScreenError() _nc_set_screen(0); \
314                             returnCode(ERR)
315
316 /* OS-independent screen initializations */
317 NCURSES_EXPORT(int)
318 NCURSES_SP_NAME(_nc_setupscreen) (
319 #if NCURSES_SP_FUNCS
320                                      SCREEN **spp,
321 #endif
322                                      int slines,
323                                      int scolumns,
324                                      FILE *output,
325                                      bool filtered,
326                                      int slk_format)
327 {
328     char *env;
329     int bottom_stolen = 0;
330     ripoff_t *rop;
331     SCREEN *sp;
332 #ifndef USE_TERM_DRIVER
333     bool support_cookies = USE_XMC_SUPPORT;
334 #endif
335
336     T((T_CALLED("_nc_setupscreen(%d, %d, %p, %d, %d)"),
337        slines, scolumns, (void *) output, filtered, slk_format));
338
339     assert(CURRENT_SCREEN == 0);        /* has been reset in newterm() ! */
340
341 #if NCURSES_SP_FUNCS
342     assert(spp != 0);
343     sp = *spp;
344
345     if (!sp) {
346         sp = _nc_alloc_screen_sp();
347         *spp = sp;
348     }
349     if (!sp
350         || ((sp->_acs_map = typeCalloc(chtype, ACS_LEN)) == 0)
351         || ((sp->_screen_acs_map = typeCalloc(bool, ACS_LEN)) == 0)) {
352         ReturnScreenError();
353     }
354
355     T(("created SP %p", sp));
356     sp->_next_screen = _nc_screen_chain;
357     _nc_screen_chain = sp;
358
359     if ((sp->_current_attr = typeCalloc(NCURSES_CH_T, 1)) == 0) {
360         ReturnScreenError();
361     }
362 #else
363     if (!_nc_alloc_screen()
364         || ((SP->_acs_map = typeCalloc(chtype, ACS_LEN)) == 0)
365         || ((SP->_screen_acs_map = typeCalloc(bool, ACS_LEN)) == 0)) {
366         returnCode(ERR);
367     }
368
369     T(("created SP %p", (void *) SP));
370
371     sp = SP;                    /* fixup so SET_LINES and SET_COLS works */
372     sp->_next_screen = _nc_screen_chain;
373     _nc_screen_chain = sp;
374
375     if ((sp->_current_attr = typeCalloc(NCURSES_CH_T, 1)) == 0)
376         returnCode(ERR);
377 #endif
378
379     /*
380      * We should always check the screensize, just in case.
381      */
382     _nc_set_screen(sp);
383     sp->_term = cur_term;
384 #ifdef USE_TERM_DRIVER
385     TCBOf(sp)->csp = sp;
386     _nc_get_screensize(sp, sp->_term, &slines, &scolumns);
387 #else
388     _nc_get_screensize(sp, &slines, &scolumns);
389 #endif
390     SET_LINES(slines);
391     SET_COLS(scolumns);
392
393     T((T_CREATE("screen %s %dx%d"),
394        NCURSES_SP_NAME(termname) (NCURSES_SP_ARG), slines, scolumns));
395
396     sp->_filtered = filtered;
397
398     /* implement filter mode */
399     if (filtered) {
400         slines = 1;
401         SET_LINES(slines);
402 #ifdef USE_TERM_DRIVER
403         CallDriver(sp, setfilter);
404 #else
405         clear_screen = 0;
406         cursor_down = parm_down_cursor = 0;
407         cursor_address = 0;
408         cursor_up = parm_up_cursor = 0;
409         row_address = 0;
410
411         cursor_home = carriage_return;
412 #endif
413         T(("filter screensize %dx%d", slines, scolumns));
414     }
415 #ifdef __DJGPP__
416     T(("setting output mode to binary"));
417     fflush(output);
418     setmode(output, O_BINARY);
419 #endif
420     NCURSES_SP_NAME(_nc_set_buffer) (NCURSES_SP_ARGx output, TRUE);
421     sp->_lines = slines;
422     sp->_lines_avail = slines;
423     sp->_columns = scolumns;
424     sp->_ofp = output;
425     SP_PRE_INIT(sp);
426     SetNoPadding(sp);
427
428 #if NCURSES_EXT_FUNCS
429     sp->_default_color = FALSE;
430     sp->_has_sgr_39_49 = FALSE;
431
432     /*
433      * Set our assumption of the terminal's default foreground and background
434      * colors.  The curs_color man-page states that we can assume that the
435      * background is black.  The origin of this assumption appears to be
436      * terminals that displayed colored text, but no colored backgrounds, e.g.,
437      * the first colored terminals around 1980.  More recent ones with better
438      * technology can display not only colored backgrounds, but all
439      * combinations.  So a terminal might be something other than "white" on
440      * black (green/black looks monochrome too), but black on white or even
441      * on ivory.
442      *
443      * White-on-black is the simplest thing to use for monochrome.  Almost
444      * all applications that use color paint both text and background, so
445      * the distinction is moot.  But a few do not - which is why we leave this
446      * configurable (a better solution is to use assume_default_colors() for
447      * the rare applications that do require that sort of appearance, since
448      * is appears that more users expect to be able to make a white-on-black
449      * or black-on-white display under control of the application than not).
450      */
451 #ifdef USE_ASSUMED_COLOR
452     sp->_default_fg = COLOR_WHITE;
453     sp->_default_bg = COLOR_BLACK;
454 #else
455     sp->_default_fg = C_MASK;
456     sp->_default_bg = C_MASK;
457 #endif
458
459     /*
460      * Allow those assumed/default color assumptions to be overridden at
461      * runtime:
462      */
463     if ((env = getenv("NCURSES_ASSUMED_COLORS")) != 0) {
464         int fg, bg;
465         char sep1, sep2;
466         int count = sscanf(env, "%d%c%d%c", &fg, &sep1, &bg, &sep2);
467         if (count >= 1) {
468             sp->_default_fg = ((fg >= 0 && fg < MaxColors) ? fg : C_MASK);
469             if (count >= 3) {
470                 sp->_default_bg = ((bg >= 0 && bg < MaxColors) ? bg : C_MASK);
471             }
472             TR(TRACE_CHARPUT | TRACE_MOVE,
473                ("from environment assumed fg=%d, bg=%d",
474                 sp->_default_fg,
475                 sp->_default_bg));
476         }
477     }
478 #if USE_COLORFGBG
479     /*
480      * If rxvt's $COLORFGBG variable is set, use it to specify the assumed
481      * default colors.  Note that rxvt (mis)uses bold colors, equating a bold
482      * color to that value plus 8.  We'll only use the non-bold color for now -
483      * decide later if it is worth having default attributes as well.
484      */
485     if (getenv("COLORFGBG") != 0) {
486         char *p = getenv("COLORFGBG");
487         TR(TRACE_CHARPUT | TRACE_MOVE, ("decoding COLORFGBG %s", p));
488         p = extract_fgbg(p, &(sp->_default_fg));
489         p = extract_fgbg(p, &(sp->_default_bg));
490         if (*p)                 /* assume rxvt was compiled with xpm support */
491             p = extract_fgbg(p, &(sp->_default_bg));
492         TR(TRACE_CHARPUT | TRACE_MOVE, ("decoded fg=%d, bg=%d",
493                                         sp->_default_fg, sp->_default_bg));
494         if (sp->_default_fg >= MaxColors) {
495             if (set_a_foreground != ABSENT_STRING
496                 && !strcmp(set_a_foreground, "\033[3%p1%dm")) {
497                 set_a_foreground = "\033[3%?%p1%{8}%>%t9%e%p1%d%;m";
498             } else {
499                 sp->_default_fg %= MaxColors;
500             }
501         }
502         if (sp->_default_bg >= MaxColors) {
503             if (set_a_background != ABSENT_STRING
504                 && !strcmp(set_a_background, "\033[4%p1%dm")) {
505                 set_a_background = "\033[4%?%p1%{8}%>%t9%e%p1%d%;m";
506             } else {
507                 sp->_default_bg %= MaxColors;
508             }
509         }
510     }
511 #endif
512 #endif /* NCURSES_EXT_FUNCS */
513
514     sp->_maxclick = DEFAULT_MAXCLICK;
515     sp->_mouse_event = no_mouse_event;
516     sp->_mouse_inline = no_mouse_inline;
517     sp->_mouse_parse = no_mouse_parse;
518     sp->_mouse_resume = no_mouse_resume;
519     sp->_mouse_wrap = no_mouse_wrap;
520     sp->_mouse_fd = -1;
521
522     /*
523      * If we've no magic cookie support, we suppress attributes that xmc would
524      * affect, i.e., the attributes that affect the rendition of a space.
525      */
526     sp->_ok_attributes = NCURSES_SP_NAME(termattrs) (NCURSES_SP_ARG);
527     if (NCURSES_SP_NAME(has_colors) (NCURSES_SP_ARG)) {
528         sp->_ok_attributes |= A_COLOR;
529     }
530 #ifdef USE_TERM_DRIVER
531     _nc_cookie_init(sp);
532 #else
533 #if USE_XMC_SUPPORT
534     /*
535      * If we have no magic-cookie support compiled-in, or if it is suppressed
536      * in the environment, reset the support-flag.
537      */
538     if (magic_cookie_glitch >= 0) {
539         if (getenv("NCURSES_NO_MAGIC_COOKIE") != 0) {
540             support_cookies = FALSE;
541         }
542     }
543 #endif
544
545     if (!support_cookies && magic_cookie_glitch >= 0) {
546         T(("will disable attributes to work w/o magic cookies"));
547     }
548
549     if (magic_cookie_glitch > 0) {      /* tvi, wyse */
550
551         sp->_xmc_triggers = sp->_ok_attributes & (
552                                                      A_STANDOUT |
553                                                      A_UNDERLINE |
554                                                      A_REVERSE |
555                                                      A_BLINK |
556                                                      A_DIM |
557                                                      A_BOLD |
558                                                      A_INVIS |
559                                                      A_PROTECT
560             );
561 #if 0
562         /*
563          * We "should" treat colors as an attribute.  The wyse350 (and its
564          * clones) appear to be the only ones that have both colors and magic
565          * cookies.
566          */
567         if (has_colors()) {
568             sp->_xmc_triggers |= A_COLOR;
569         }
570 #endif
571         sp->_xmc_suppress = sp->_xmc_triggers & (chtype) ~(A_BOLD);
572
573         T(("magic cookie attributes %s", _traceattr(sp->_xmc_suppress)));
574         /*
575          * Supporting line-drawing may be possible.  But make the regular
576          * video attributes work first.
577          */
578         acs_chars = ABSENT_STRING;
579         ena_acs = ABSENT_STRING;
580         enter_alt_charset_mode = ABSENT_STRING;
581         exit_alt_charset_mode = ABSENT_STRING;
582 #if USE_XMC_SUPPORT
583         /*
584          * To keep the cookie support simple, suppress all of the optimization
585          * hooks except for clear_screen and the cursor addressing.
586          */
587         if (support_cookies) {
588             clr_eol = ABSENT_STRING;
589             clr_eos = ABSENT_STRING;
590             set_attributes = ABSENT_STRING;
591         }
592 #endif
593     } else if (magic_cookie_glitch == 0) {      /* hpterm */
594     }
595
596     /*
597      * If magic cookies are not supported, cancel the strings that set
598      * video attributes.
599      */
600     if (!support_cookies && magic_cookie_glitch >= 0) {
601         magic_cookie_glitch = ABSENT_NUMERIC;
602         set_attributes = ABSENT_STRING;
603         enter_blink_mode = ABSENT_STRING;
604         enter_bold_mode = ABSENT_STRING;
605         enter_dim_mode = ABSENT_STRING;
606         enter_reverse_mode = ABSENT_STRING;
607         enter_standout_mode = ABSENT_STRING;
608         enter_underline_mode = ABSENT_STRING;
609     }
610
611     /* initialize normal acs before wide, since we use mapping in the latter */
612 #if !USE_WIDEC_SUPPORT
613     if (_nc_unicode_locale() && _nc_locale_breaks_acs(sp->_term)) {
614         acs_chars = NULL;
615         ena_acs = NULL;
616         enter_alt_charset_mode = NULL;
617         exit_alt_charset_mode = NULL;
618         set_attributes = NULL;
619     }
620 #endif
621 #endif
622
623     NCURSES_SP_NAME(_nc_init_acs) (NCURSES_SP_ARG);
624 #if USE_WIDEC_SUPPORT
625     _nc_init_wacs();
626
627     sp->_screen_acs_fix = (_nc_unicode_locale()
628                            && _nc_locale_breaks_acs(sp->_term));
629 #endif
630     env = _nc_get_locale();
631     sp->_legacy_coding = ((env == 0)
632                           || !strcmp(env, "C")
633                           || !strcmp(env, "POSIX"));
634     T(("legacy-coding %d", sp->_legacy_coding));
635
636     sp->_nc_sp_idcok = TRUE;
637     sp->_nc_sp_idlok = FALSE;
638
639     sp->oldhash = 0;
640     sp->newhash = 0;
641
642     T(("creating newscr"));
643     sp->_newscr = NCURSES_SP_NAME(newwin) (NCURSES_SP_ARGx slines, scolumns,
644                                            0, 0);
645     if (sp->_newscr == 0) {
646         ReturnScreenError();
647     }
648     T(("creating curscr"));
649     sp->_curscr = NCURSES_SP_NAME(newwin) (NCURSES_SP_ARGx slines, scolumns,
650                                            0, 0);
651     if (sp->_curscr == 0) {
652         ReturnScreenError();
653     }
654 #if !USE_REENTRANT
655     newscr = sp->_newscr;
656     curscr = sp->_curscr;
657 #endif
658 #if USE_SIZECHANGE
659     sp->_resize = NCURSES_SP_NAME(resizeterm);
660 #endif
661
662     sp->_newscr->_clear = TRUE;
663     sp->_curscr->_clear = FALSE;
664
665     NCURSES_SP_NAME(def_shell_mode) (NCURSES_SP_ARG);
666     NCURSES_SP_NAME(def_prog_mode) (NCURSES_SP_ARG);
667
668     if (safe_ripoff_sp && safe_ripoff_sp != safe_ripoff_stack) {
669         for (rop = safe_ripoff_stack;
670              rop != safe_ripoff_sp && (rop - safe_ripoff_stack) < N_RIPS;
671              rop++) {
672
673             /* If we must simulate soft labels, grab off the line to be used.
674                We assume that we must simulate, if it is none of the standard
675                formats (4-4 or 3-2-3) for which there may be some hardware
676                support. */
677             if (rop->hook == _nc_slk_initialize) {
678                 if (!(NumLabels <= 0 || !SLK_STDFMT(slk_format))) {
679                     continue;
680                 }
681             }
682             if (rop->hook) {
683                 int count;
684                 WINDOW *w;
685
686                 count = (rop->line < 0) ? -rop->line : rop->line;
687                 T(("ripping off %i lines at %s", count,
688                    ((rop->line < 0)
689                     ? "bottom"
690                     : "top")));
691
692                 w = NCURSES_SP_NAME(newwin) (NCURSES_SP_ARGx
693                                              count, scolumns,
694                                              ((rop->line < 0)
695                                               ? sp->_lines_avail - count
696                                               : 0),
697                                              0);
698                 if (w) {
699                     rop->win = w;
700                     rop->hook(w, scolumns);
701                 } else {
702                     ReturnScreenError();
703                 }
704                 if (rop->line < 0)
705                     bottom_stolen += count;
706                 else
707                     sp->_topstolen += count;
708                 sp->_lines_avail -= count;
709             }
710         }
711         /* reset the stack */
712         safe_ripoff_sp = safe_ripoff_stack;
713     }
714
715     T(("creating stdscr"));
716     assert((sp->_lines_avail + sp->_topstolen + bottom_stolen) == slines);
717     if ((sp->_stdscr = NCURSES_SP_NAME(newwin) (NCURSES_SP_ARGx
718                                                 sp->_lines_avail,
719                                                 scolumns, 0, 0)) == 0) {
720         ReturnScreenError();
721     }
722     SET_LINES(sp->_lines_avail);
723 #if !USE_REENTRANT
724     stdscr = sp->_stdscr;
725 #endif
726     sp->_prescreen = FALSE;
727     returnCode(OK);
728 }
729
730 #if NCURSES_SP_FUNCS
731 NCURSES_EXPORT(int)
732 _nc_setupscreen(int slines GCC_UNUSED,
733                 int scolumns GCC_UNUSED,
734                 FILE *output,
735                 bool filtered,
736                 int slk_format)
737 {
738     SCREEN *sp = 0;
739     int rc = NCURSES_SP_NAME(_nc_setupscreen) (&sp,
740                                                slines,
741                                                scolumns,
742                                                output,
743                                                filtered,
744                                                slk_format);
745     if (rc != OK)
746         _nc_set_screen(0);
747     return rc;
748 }
749 #endif
750
751 /*
752  * The internal implementation interprets line as the number of lines to rip
753  * off from the top or bottom.
754  */
755 NCURSES_EXPORT(int)
756 NCURSES_SP_NAME(_nc_ripoffline) (NCURSES_SP_DCLx
757                                  int line,
758                                  int (*init) (WINDOW *, int))
759 {
760     int code = ERR;
761
762     START_TRACE();
763     T((T_CALLED("ripoffline(%p,%d,%p)"), (void *) SP_PARM, line, (void *) init));
764
765 #if NCURSES_SP_FUNCS
766     if (SP_PARM != 0 && SP_PARM->_prescreen)
767 #endif
768     {
769         if (line == 0) {
770             code = OK;
771         } else {
772             if (safe_ripoff_sp == 0)
773                 safe_ripoff_sp = safe_ripoff_stack;
774             if (safe_ripoff_sp < safe_ripoff_stack + N_RIPS) {
775                 safe_ripoff_sp->line = line;
776                 safe_ripoff_sp->hook = init;
777                 (safe_ripoff_sp)++;
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     return NCURSES_SP_NAME(_nc_ripoffline) (CURRENT_SCREEN_PRE, line, init);
791 }
792 #endif
793
794 NCURSES_EXPORT(int)
795 NCURSES_SP_NAME(ripoffline) (NCURSES_SP_DCLx
796                              int line,
797                              int (*init) (WINDOW *, int))
798 {
799     return NCURSES_SP_NAME(_nc_ripoffline) (NCURSES_SP_ARGx
800                                             (line < 0) ? -1 : 1,
801                                             init);
802 }
803
804 #if NCURSES_SP_FUNCS
805 NCURSES_EXPORT(int)
806 ripoffline(int line, int (*init) (WINDOW *, int))
807 {
808     return NCURSES_SP_NAME(ripoffline) (CURRENT_SCREEN_PRE, line, init);
809 }
810 #endif