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