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