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