lib_set_term.c revision 176187
150276Speter/****************************************************************************
2176187Srafan * Copyright (c) 1998-2007,2008 Free Software Foundation, Inc.              *
350276Speter *                                                                          *
450276Speter * Permission is hereby granted, free of charge, to any person obtaining a  *
550276Speter * copy of this software and associated documentation files (the            *
650276Speter * "Software"), to deal in the Software without restriction, including      *
750276Speter * without limitation the rights to use, copy, modify, merge, publish,      *
850276Speter * distribute, distribute with modifications, sublicense, and/or sell       *
950276Speter * copies of the Software, and to permit persons to whom the Software is    *
1050276Speter * furnished to do so, subject to the following conditions:                 *
1150276Speter *                                                                          *
1250276Speter * The above copyright notice and this permission notice shall be included  *
1350276Speter * in all copies or substantial portions of the Software.                   *
1450276Speter *                                                                          *
1550276Speter * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
1650276Speter * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
1750276Speter * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
1850276Speter * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
1950276Speter * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
2050276Speter * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
2150276Speter * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
2250276Speter *                                                                          *
2350276Speter * Except as contained in this notice, the name(s) of the above copyright   *
2450276Speter * holders shall not be used in advertising or otherwise to promote the     *
2550276Speter * sale, use or other dealings in this Software without prior written       *
2650276Speter * authorization.                                                           *
2750276Speter ****************************************************************************/
2850276Speter
2950276Speter/****************************************************************************
3050276Speter *  Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
3150276Speter *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
32166124Srafan *     and: Thomas E. Dickey                        1996-on                 *
3350276Speter ****************************************************************************/
3450276Speter
3550276Speter/*
3650276Speter**	lib_set_term.c
3750276Speter**
3850276Speter**	The routine set_term().
3950276Speter**
4050276Speter*/
4150276Speter
4250276Speter#include <curses.priv.h>
4350276Speter
4462449Speter#include <term.h>		/* cur_term */
4562449Speter#include <tic.h>
4650276Speter
47176187SrafanMODULE_ID("$Id: lib_set_term.c,v 1.103 2008/02/03 20:31:08 tom Exp $")
4850276Speter
4976726SpeterNCURSES_EXPORT(SCREEN *)
50166124Srafanset_term(SCREEN *screenp)
5150276Speter{
5262449Speter    SCREEN *oldSP;
5350276Speter
5462449Speter    T((T_CALLED("set_term(%p)"), screenp));
5550276Speter
56174993Srafan    _nc_lock_global(set_SP);
57174993Srafan
5862449Speter    oldSP = SP;
5962449Speter    _nc_set_screen(screenp);
6050276Speter
6162449Speter    set_curterm(SP->_term);
62174993Srafan#if !USE_REENTRANT
6362449Speter    curscr = SP->_curscr;
6462449Speter    newscr = SP->_newscr;
6562449Speter    stdscr = SP->_stdscr;
6662449Speter    COLORS = SP->_color_count;
6762449Speter    COLOR_PAIRS = SP->_pair_count;
68174993Srafan#endif
6950276Speter
70174993Srafan    _nc_unlock_global(set_SP);
71174993Srafan
7262449Speter    T((T_RETURN("%p"), oldSP));
7362449Speter    return (oldSP);
7450276Speter}
7550276Speter
7662449Speterstatic void
77174993Srafan_nc_free_keytry(TRIES * kt)
7850276Speter{
7962449Speter    if (kt != 0) {
8062449Speter	_nc_free_keytry(kt->child);
8162449Speter	_nc_free_keytry(kt->sibling);
8262449Speter	free(kt);
8362449Speter    }
8450276Speter}
8550276Speter
8650276Speter/*
8750276Speter * Free the storage associated with the given SCREEN sp.
8850276Speter */
8976726SpeterNCURSES_EXPORT(void)
90166124Srafandelscreen(SCREEN *sp)
9150276Speter{
9262449Speter    SCREEN **scan = &_nc_screen_chain;
93166124Srafan    int i;
9450276Speter
9562449Speter    T((T_CALLED("delscreen(%p)"), sp));
9650276Speter
97174993Srafan    _nc_lock_global(set_SP);
9862449Speter    while (*scan) {
9962449Speter	if (*scan == sp) {
10062449Speter	    *scan = sp->_next_screen;
10162449Speter	    break;
10250276Speter	}
10362449Speter	scan = &(*scan)->_next_screen;
10462449Speter    }
10550276Speter
10676726Speter    (void) _nc_freewin(sp->_curscr);
10776726Speter    (void) _nc_freewin(sp->_newscr);
10876726Speter    (void) _nc_freewin(sp->_stdscr);
109166124Srafan
110166124Srafan    if (sp->_slk != 0) {
111166124Srafan	if (sp->_slk->ent != 0) {
112166124Srafan	    for (i = 0; i < sp->_slk->labcnt; ++i) {
113166124Srafan		FreeIfNeeded(sp->_slk->ent[i].ent_text);
114166124Srafan		FreeIfNeeded(sp->_slk->ent[i].form_text);
115166124Srafan	    }
116166124Srafan	    free(sp->_slk->ent);
117166124Srafan	}
118166124Srafan	free(sp->_slk);
119166124Srafan	sp->_slk = 0;
120166124Srafan    }
121166124Srafan
12262449Speter    _nc_free_keytry(sp->_keytry);
123166124Srafan    sp->_keytry = 0;
124166124Srafan
12562449Speter    _nc_free_keytry(sp->_key_ok);
126166124Srafan    sp->_key_ok = 0;
12750276Speter
128166124Srafan    FreeIfNeeded(sp->_current_attr);
129166124Srafan
13062449Speter    FreeIfNeeded(sp->_color_table);
13162449Speter    FreeIfNeeded(sp->_color_pairs);
13250276Speter
13362449Speter    FreeIfNeeded(sp->oldhash);
13462449Speter    FreeIfNeeded(sp->newhash);
135166124Srafan    FreeIfNeeded(sp->hashtab);
13650276Speter
137166124Srafan    FreeIfNeeded(sp->_acs_map);
138166124Srafan    FreeIfNeeded(sp->_screen_acs_map);
139166124Srafan
14062449Speter    del_curterm(sp->_term);
14150276Speter
14262449Speter    /*
14362449Speter     * If the associated output stream has been closed, we can discard the
14462449Speter     * set-buffer.  Limit the error check to EBADF, since fflush may fail
14562449Speter     * for other reasons than trying to operate upon a closed stream.
14662449Speter     */
14762449Speter    if (sp->_ofp != 0
14862449Speter	&& sp->_setbuf != 0
14962449Speter	&& fflush(sp->_ofp) != 0
15062449Speter	&& errno == EBADF) {
15162449Speter	free(sp->_setbuf);
15262449Speter    }
15350276Speter
15462449Speter    free(sp);
15562449Speter
15662449Speter    /*
15762449Speter     * If this was the current screen, reset everything that the
15862449Speter     * application might try to use (except cur_term, which may have
15962449Speter     * multiple references in different screens).
16062449Speter     */
16162449Speter    if (sp == SP) {
162174993Srafan#if !USE_REENTRANT
16362449Speter	curscr = 0;
16462449Speter	newscr = 0;
16562449Speter	stdscr = 0;
16662449Speter	COLORS = 0;
16762449Speter	COLOR_PAIRS = 0;
168174993Srafan#endif
16962449Speter	_nc_set_screen(0);
17062449Speter    }
171174993Srafan    _nc_unlock_global(set_SP);
172174993Srafan
17362449Speter    returnVoid;
17450276Speter}
17550276Speter
17662449Speterstatic bool
177166124Srafanno_mouse_event(SCREEN *sp GCC_UNUSED)
17862449Speter{
17962449Speter    return FALSE;
18062449Speter}
18150276Speter
18262449Speterstatic bool
183166124Srafanno_mouse_inline(SCREEN *sp GCC_UNUSED)
18462449Speter{
18562449Speter    return FALSE;
18662449Speter}
18762449Speter
18862449Speterstatic bool
18962449Speterno_mouse_parse(int code GCC_UNUSED)
19062449Speter{
19162449Speter    return TRUE;
19262449Speter}
19362449Speter
19462449Speterstatic void
195166124Srafanno_mouse_resume(SCREEN *sp GCC_UNUSED)
19662449Speter{
19762449Speter}
19862449Speter
19962449Speterstatic void
200166124Srafanno_mouse_wrap(SCREEN *sp GCC_UNUSED)
20162449Speter{
20262449Speter}
20362449Speter
20466963Speter#if NCURSES_EXT_FUNCS && USE_COLORFGBG
20562449Speterstatic char *
20662449Speterextract_fgbg(char *src, int *result)
20762449Speter{
20862449Speter    char *dst = 0;
20962449Speter    long value = strtol(src, &dst, 0);
21062449Speter
21162449Speter    if (dst == 0) {
21262449Speter	dst = src;
21362449Speter    } else if (value >= 0) {
21498503Speter	*result = value;
21562449Speter    }
21662449Speter    while (*dst != 0 && *dst != ';')
21762449Speter	dst++;
21862449Speter    if (*dst == ';')
21962449Speter	dst++;
22062449Speter    return dst;
22162449Speter}
22262449Speter#endif
22362449Speter
224166124Srafan/* OS-independent screen initializations */
22576726SpeterNCURSES_EXPORT(int)
226174993Srafan_nc_setupscreen(int slines GCC_UNUSED,
227174993Srafan		int scolumns GCC_UNUSED,
228166124Srafan		FILE *output,
229166124Srafan		bool filtered,
230166124Srafan		int slk_format)
23150276Speter{
232176187Srafan    char *env;
23362449Speter    int bottom_stolen = 0;
234166124Srafan    bool support_cookies = USE_XMC_SUPPORT;
235174993Srafan    ripoff_t *rop;
23650276Speter
237166124Srafan    T((T_CALLED("_nc_setupscreen(%d, %d, %p, %d, %d)"),
238166124Srafan       slines, scolumns, output, filtered, slk_format));
239166124Srafan
24062449Speter    assert(SP == 0);		/* has been reset in newterm() ! */
241166124Srafan    if (!_nc_alloc_screen()
242166124Srafan	|| ((SP->_acs_map = typeCalloc(chtype, ACS_LEN)) == 0)
243166124Srafan	|| ((SP->_screen_acs_map = typeCalloc(bool, ACS_LEN)) == 0)) {
244166124Srafan	returnCode(ERR);
245166124Srafan    }
24650276Speter
247166124Srafan    T(("created SP %p", SP));
24862449Speter    SP->_next_screen = _nc_screen_chain;
24962449Speter    _nc_screen_chain = SP;
25050276Speter
251166124Srafan    if ((SP->_current_attr = typeCalloc(NCURSES_CH_T, 1)) == 0)
252166124Srafan	returnCode(ERR);
253166124Srafan
254174993Srafan    /*
255174993Srafan     * We should always check the screensize, just in case.
256174993Srafan     */
257174993Srafan    _nc_get_screensize(&slines, &scolumns);
258174993Srafan    SET_LINES(slines);
259174993Srafan    SET_COLS(scolumns);
260174993Srafan    T((T_CREATE("screen %s %dx%d"), termname(), LINES, COLS));
261174993Srafan
262166124Srafan    SP->_filtered = filtered;
263166124Srafan
264166124Srafan    /* implement filter mode */
265166124Srafan    if (filtered) {
266174993Srafan	slines = 1;
267174993Srafan	SET_LINES(slines);
268166124Srafan	clear_screen = 0;
269166124Srafan	cursor_down = parm_down_cursor = 0;
270166124Srafan	cursor_address = 0;
271166124Srafan	cursor_up = parm_up_cursor = 0;
272166124Srafan	row_address = 0;
273166124Srafan
274166124Srafan	cursor_home = carriage_return;
275166124Srafan	T(("filter screensize %dx%d", LINES, COLS));
276166124Srafan    }
277166124Srafan#ifdef __DJGPP__
278166124Srafan    T(("setting output mode to binary"));
279166124Srafan    fflush(output);
280166124Srafan    setmode(output, O_BINARY);
281166124Srafan#endif
28262449Speter    _nc_set_buffer(output, TRUE);
28362449Speter    SP->_term = cur_term;
28462449Speter    SP->_lines = slines;
28562449Speter    SP->_lines_avail = slines;
28662449Speter    SP->_columns = scolumns;
28762449Speter    SP->_cursrow = -1;
28862449Speter    SP->_curscol = -1;
28962449Speter    SP->_nl = TRUE;
29062449Speter    SP->_raw = FALSE;
29162449Speter    SP->_cbreak = 0;
29262449Speter    SP->_echo = TRUE;
29362449Speter    SP->_fifohead = -1;
29462449Speter    SP->_endwin = TRUE;
29562449Speter    SP->_ofp = output;
29662449Speter    SP->_cursor = -1;		/* cannot know real cursor shape */
29766963Speter
29866963Speter#if NCURSES_NO_PADDING
29962449Speter    SP->_no_padding = getenv("NCURSES_NO_PADDING") != 0;
30062449Speter    TR(TRACE_CHARPUT | TRACE_MOVE, ("padding will%s be used",
30176726Speter				    SP->_no_padding ? " not" : ""));
30250276Speter#endif
30366963Speter
30466963Speter#if NCURSES_EXT_FUNCS
30562449Speter    SP->_default_color = FALSE;
30662449Speter    SP->_has_sgr_39_49 = FALSE;
30766963Speter
30866963Speter    /*
30966963Speter     * Set our assumption of the terminal's default foreground and background
31066963Speter     * colors.  The curs_color man-page states that we can assume that the
31166963Speter     * background is black.  The origin of this assumption appears to be
31266963Speter     * terminals that displayed colored text, but no colored backgrounds, e.g.,
31366963Speter     * the first colored terminals around 1980.  More recent ones with better
31466963Speter     * technology can display not only colored backgrounds, but all
31566963Speter     * combinations.  So a terminal might be something other than "white" on
31666963Speter     * black (green/black looks monochrome too), but black on white or even
31766963Speter     * on ivory.
31866963Speter     *
31966963Speter     * White-on-black is the simplest thing to use for monochrome.  Almost
32066963Speter     * all applications that use color paint both text and background, so
32166963Speter     * the distinction is moot.  But a few do not - which is why we leave this
32266963Speter     * configurable (a better solution is to use assume_default_colors() for
32366963Speter     * the rare applications that do require that sort of appearance, since
32466963Speter     * is appears that more users expect to be able to make a white-on-black
32566963Speter     * or black-on-white display under control of the application than not).
32666963Speter     */
32766963Speter#ifdef USE_ASSUMED_COLOR
32862449Speter    SP->_default_fg = COLOR_WHITE;
32962449Speter    SP->_default_bg = COLOR_BLACK;
33066963Speter#else
33166963Speter    SP->_default_fg = C_MASK;
33266963Speter    SP->_default_bg = C_MASK;
33366963Speter#endif
33466963Speter
335166124Srafan    /*
336166124Srafan     * Allow those assumed/default color assumptions to be overridden at
337166124Srafan     * runtime:
338166124Srafan     */
339166124Srafan    if (getenv("NCURSES_ASSUMED_COLORS") != 0) {
340166124Srafan	char *p = getenv("NCURSES_ASSUMED_COLORS");
341166124Srafan	int fg, bg;
342166124Srafan	char sep1, sep2;
343166124Srafan	int count = sscanf(p, "%d%c%d%c", &fg, &sep1, &bg, &sep2);
344166124Srafan	if (count >= 1) {
345166124Srafan	    SP->_default_fg = (fg >= 0 && fg < max_colors) ? fg : C_MASK;
346166124Srafan	    if (count >= 3) {
347166124Srafan		SP->_default_bg = (bg >= 0 && bg < max_colors) ? bg : C_MASK;
348166124Srafan	    }
349166124Srafan	    TR(TRACE_CHARPUT | TRACE_MOVE,
350166124Srafan	       ("from environment assumed fg=%d, bg=%d",
351166124Srafan		SP->_default_fg,
352166124Srafan		SP->_default_bg));
353166124Srafan	}
354166124Srafan    }
35566963Speter#if USE_COLORFGBG
35662449Speter    /*
35762449Speter     * If rxvt's $COLORFGBG variable is set, use it to specify the assumed
35862449Speter     * default colors.  Note that rxvt (mis)uses bold colors, equating a bold
35962449Speter     * color to that value plus 8.  We'll only use the non-bold color for now -
36062449Speter     * decide later if it is worth having default attributes as well.
36162449Speter     */
36262449Speter    if (getenv("COLORFGBG") != 0) {
36362449Speter	char *p = getenv("COLORFGBG");
36497049Speter	TR(TRACE_CHARPUT | TRACE_MOVE, ("decoding COLORFGBG %s", p));
36562449Speter	p = extract_fgbg(p, &(SP->_default_fg));
36662449Speter	p = extract_fgbg(p, &(SP->_default_bg));
36797049Speter	if (*p)			/* assume rxvt was compiled with xpm support */
36897049Speter	    p = extract_fgbg(p, &(SP->_default_bg));
36997049Speter	TR(TRACE_CHARPUT | TRACE_MOVE, ("decoded fg=%d, bg=%d",
37097049Speter					SP->_default_fg, SP->_default_bg));
371166124Srafan	if (SP->_default_fg >= max_colors) {
37298503Speter	    if (set_a_foreground != ABSENT_STRING
37398503Speter		&& !strcmp(set_a_foreground, "\033[3%p1%dm")) {
37498503Speter		set_a_foreground = "\033[3%?%p1%{8}%>%t9%e%p1%d%;m";
37598503Speter	    } else {
37698503Speter		SP->_default_fg %= max_colors;
37798503Speter	    }
37898503Speter	}
379166124Srafan	if (SP->_default_bg >= max_colors) {
38098503Speter	    if (set_a_background != ABSENT_STRING
38198503Speter		&& !strcmp(set_a_background, "\033[4%p1%dm")) {
38298503Speter		set_a_background = "\033[4%?%p1%{8}%>%t9%e%p1%d%;m";
38398503Speter	    } else {
38498503Speter		SP->_default_bg %= max_colors;
38598503Speter	    }
38698503Speter	}
38762449Speter    }
38862449Speter#endif
38962449Speter#endif /* NCURSES_EXT_FUNCS */
39050276Speter
39162449Speter    SP->_maxclick = DEFAULT_MAXCLICK;
39262449Speter    SP->_mouse_event = no_mouse_event;
39362449Speter    SP->_mouse_inline = no_mouse_inline;
39462449Speter    SP->_mouse_parse = no_mouse_parse;
39562449Speter    SP->_mouse_resume = no_mouse_resume;
39662449Speter    SP->_mouse_wrap = no_mouse_wrap;
39762449Speter    SP->_mouse_fd = -1;
39850276Speter
39962449Speter    /* initialize the panel hooks */
40062449Speter    SP->_panelHook.top_panel = (struct panel *) 0;
40162449Speter    SP->_panelHook.bottom_panel = (struct panel *) 0;
40262449Speter    SP->_panelHook.stdscr_pseudo_panel = (struct panel *) 0;
40350276Speter
40462449Speter    /*
405166124Srafan     * If we've no magic cookie support, we suppress attributes that xmc would
406166124Srafan     * affect, i.e., the attributes that affect the rendition of a space.
40762449Speter     */
408166124Srafan    SP->_ok_attributes = termattrs();
409166124Srafan    if (has_colors()) {
410166124Srafan	SP->_ok_attributes |= A_COLOR;
411166124Srafan    }
412166124Srafan#if USE_XMC_SUPPORT
413166124Srafan    /*
414166124Srafan     * If we have no magic-cookie support compiled-in, or if it is suppressed
415166124Srafan     * in the environment, reset the support-flag.
416166124Srafan     */
417166124Srafan    if (magic_cookie_glitch >= 0) {
418166124Srafan	if (getenv("NCURSES_NO_MAGIC_COOKIE") != 0) {
419166124Srafan	    support_cookies = FALSE;
420166124Srafan	}
421166124Srafan    }
422166124Srafan#endif
42350276Speter
424166124Srafan    if (!support_cookies && magic_cookie_glitch >= 0) {
425166124Srafan	T(("will disable attributes to work w/o magic cookies"));
426166124Srafan    }
427166124Srafan
428166124Srafan    if (magic_cookie_glitch > 0) {	/* tvi, wyse */
429166124Srafan
430166124Srafan	SP->_xmc_triggers = SP->_ok_attributes & (
431166124Srafan						     A_STANDOUT |
432166124Srafan						     A_UNDERLINE |
433166124Srafan						     A_REVERSE |
434166124Srafan						     A_BLINK |
435166124Srafan						     A_DIM |
436166124Srafan						     A_BOLD |
437166124Srafan						     A_INVIS |
438166124Srafan						     A_PROTECT
43962449Speter	    );
440166124Srafan#if 0
441166124Srafan	/*
442166124Srafan	 * We "should" treat colors as an attribute.  The wyse350 (and its
443166124Srafan	 * clones) appear to be the only ones that have both colors and magic
444166124Srafan	 * cookies.
445166124Srafan	 */
446166124Srafan	if (has_colors()) {
447166124Srafan	    SP->_xmc_triggers |= A_COLOR;
448166124Srafan	}
449166124Srafan#endif
450166124Srafan	SP->_xmc_suppress = SP->_xmc_triggers & (chtype) ~(A_BOLD);
45150276Speter
45262449Speter	T(("magic cookie attributes %s", _traceattr(SP->_xmc_suppress)));
453166124Srafan	/*
454166124Srafan	 * Supporting line-drawing may be possible.  But make the regular
455166124Srafan	 * video attributes work first.
456166124Srafan	 */
457166124Srafan	acs_chars = ABSENT_STRING;
458166124Srafan	ena_acs = ABSENT_STRING;
459166124Srafan	enter_alt_charset_mode = ABSENT_STRING;
460166124Srafan	exit_alt_charset_mode = ABSENT_STRING;
46150276Speter#if USE_XMC_SUPPORT
46262449Speter	/*
463166124Srafan	 * To keep the cookie support simple, suppress all of the optimization
464166124Srafan	 * hooks except for clear_screen and the cursor addressing.
46562449Speter	 */
466166124Srafan	if (support_cookies) {
467166124Srafan	    clr_eol = ABSENT_STRING;
468166124Srafan	    clr_eos = ABSENT_STRING;
469166124Srafan	    set_attributes = ABSENT_STRING;
470166124Srafan	}
47150276Speter#endif
472166124Srafan    } else if (magic_cookie_glitch == 0) {	/* hpterm */
47362449Speter    }
474166124Srafan
475166124Srafan    /*
476166124Srafan     * If magic cookies are not supported, cancel the strings that set
477166124Srafan     * video attributes.
478166124Srafan     */
479166124Srafan    if (!support_cookies && magic_cookie_glitch >= 0) {
480166124Srafan	magic_cookie_glitch = ABSENT_NUMERIC;
481166124Srafan	set_attributes = ABSENT_STRING;
482166124Srafan	enter_blink_mode = ABSENT_STRING;
483166124Srafan	enter_bold_mode = ABSENT_STRING;
484166124Srafan	enter_dim_mode = ABSENT_STRING;
485166124Srafan	enter_reverse_mode = ABSENT_STRING;
486166124Srafan	enter_standout_mode = ABSENT_STRING;
487166124Srafan	enter_underline_mode = ABSENT_STRING;
488166124Srafan    }
489166124Srafan
490166124Srafan    /* initialize normal acs before wide, since we use mapping in the latter */
491174993Srafan#if !USE_WIDEC_SUPPORT
492174993Srafan    if (_nc_unicode_locale() && _nc_locale_breaks_acs()) {
493174993Srafan	acs_chars = NULL;
494174993Srafan	ena_acs = NULL;
495174993Srafan	enter_alt_charset_mode = NULL;
496174993Srafan	exit_alt_charset_mode = NULL;
497174993Srafan	set_attributes = NULL;
498174993Srafan    }
499174993Srafan#endif
500166124Srafan    _nc_init_acs();
50197049Speter#if USE_WIDEC_SUPPORT
50297049Speter    _nc_init_wacs();
503166124Srafan
504166124Srafan    SP->_screen_acs_fix = (_nc_unicode_locale() && _nc_locale_breaks_acs());
50597049Speter#endif
506176187Srafan    env = _nc_get_locale();
507176187Srafan    SP->_legacy_coding = ((env == 0)
508176187Srafan			  || !strcmp(env, "C")
509176187Srafan			  || !strcmp(env, "POSIX"));
510176187Srafan    T(("legacy-coding %d", SP->_legacy_coding));
51150276Speter
51262449Speter    _nc_idcok = TRUE;
51362449Speter    _nc_idlok = FALSE;
51450276Speter
51562449Speter    _nc_windows = 0;		/* no windows yet */
51650276Speter
51762449Speter    SP->oldhash = 0;
51862449Speter    SP->newhash = 0;
51950276Speter
52062449Speter    T(("creating newscr"));
521174993Srafan    if ((SP->_newscr = newwin(slines, scolumns, 0, 0)) == 0)
522166124Srafan	returnCode(ERR);
52350276Speter
52462449Speter    T(("creating curscr"));
525174993Srafan    if ((SP->_curscr = newwin(slines, scolumns, 0, 0)) == 0)
526166124Srafan	returnCode(ERR);
52750276Speter
528174993Srafan#if !USE_REENTRANT
529174993Srafan    newscr = SP->_newscr;
530174993Srafan    curscr = SP->_curscr;
531174993Srafan#endif
53250276Speter#if USE_SIZECHANGE
53362449Speter    SP->_resize = resizeterm;
53450276Speter#endif
53550276Speter
53662449Speter    newscr->_clear = TRUE;
53762449Speter    curscr->_clear = FALSE;
53850276Speter
53997049Speter    def_shell_mode();
54097049Speter    def_prog_mode();
54197049Speter
542174993Srafan    for (rop = ripoff_stack;
543174993Srafan	 rop != ripoff_sp && (rop - ripoff_stack) < N_RIPS;
544174993Srafan	 rop++) {
54550276Speter
546174993Srafan	/* If we must simulate soft labels, grab off the line to be used.
547174993Srafan	   We assume that we must simulate, if it is none of the standard
548174993Srafan	   formats (4-4 or 3-2-3) for which there may be some hardware
549174993Srafan	   support. */
550174993Srafan	if (rop->hook == _nc_slk_initialize)
551174993Srafan	    if (!(num_labels <= 0 || !SLK_STDFMT(slk_format)))
552174993Srafan		continue;
553174993Srafan	if (rop->hook) {
554174993Srafan	    int count;
555174993Srafan	    WINDOW *w;
556174993Srafan
557174993Srafan	    count = (rop->line < 0) ? -rop->line : rop->line;
558174993Srafan	    T(("ripping off %i lines at %s", count,
559174993Srafan	       ((rop->line < 0)
560174993Srafan		? "bottom"
561174993Srafan		: "top")));
562174993Srafan
563174993Srafan	    w = newwin(count, scolumns,
564174993Srafan		       ((rop->line < 0)
565174993Srafan			? SP->_lines_avail - count
566174993Srafan			: 0),
567174993Srafan		       0);
568176187Srafan	    if (w) {
569176187Srafan		rop->win = w;
570174993Srafan		rop->hook(w, scolumns);
571176187Srafan	    } else {
572166124Srafan		returnCode(ERR);
573176187Srafan	    }
574174993Srafan	    if (rop->line < 0)
575166124Srafan		bottom_stolen += count;
576166124Srafan	    else
577166124Srafan		SP->_topstolen += count;
57862449Speter	    SP->_lines_avail -= count;
57950276Speter	}
58062449Speter    }
58162449Speter    /* reset the stack */
582174993Srafan    ripoff_sp = ripoff_stack;
58350276Speter
58462449Speter    T(("creating stdscr"));
58562449Speter    assert((SP->_lines_avail + SP->_topstolen + bottom_stolen) == slines);
586174993Srafan    if ((SP->_stdscr = newwin(SP->_lines_avail, scolumns, 0, 0)) == 0)
587166124Srafan	returnCode(ERR);
58850276Speter
589174993Srafan    SET_LINES(SP->_lines_avail);
590174993Srafan#if !USE_REENTRANT
591174993Srafan    stdscr = SP->_stdscr;
592174993Srafan#endif
593174993Srafan
594166124Srafan    returnCode(OK);
59550276Speter}
59650276Speter
597174993Srafan/*
598174993Srafan * The internal implementation interprets line as the number of lines to rip
599174993Srafan * off from the top or bottom.
600174993Srafan */
60176726SpeterNCURSES_EXPORT(int)
60262449Speter_nc_ripoffline(int line, int (*init) (WINDOW *, int))
60350276Speter{
604166124Srafan    T((T_CALLED("_nc_ripoffline(%d, %p)"), line, init));
60550276Speter
606166124Srafan    if (line != 0) {
60750276Speter
608174993Srafan	if (ripoff_sp == 0)
609174993Srafan	    ripoff_sp = ripoff_stack;
610174993Srafan	if (ripoff_sp >= ripoff_stack + N_RIPS)
611166124Srafan	    returnCode(ERR);
61250276Speter
613174993Srafan	ripoff_sp->line = line;
614174993Srafan	ripoff_sp->hook = init;
615174993Srafan	ripoff_sp++;
616166124Srafan    }
617166124Srafan
618166124Srafan    returnCode(OK);
61950276Speter}
62050276Speter
62176726SpeterNCURSES_EXPORT(int)
62262449Speterripoffline(int line, int (*init) (WINDOW *, int))
62350276Speter{
624176187Srafan    START_TRACE();
62550276Speter    T((T_CALLED("ripoffline(%d,%p)"), line, init));
62650276Speter
62750276Speter    if (line == 0)
62850276Speter	returnCode(OK);
62950276Speter
63062449Speter    returnCode(_nc_ripoffline((line < 0) ? -1 : 1, init));
63150276Speter}
632