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
47184989SrafanMODULE_ID("$Id: lib_set_term.c,v 1.117 2008/08/04 18:11:12 tom Exp $")
4850276Speter
4976726SpeterNCURSES_EXPORT(SCREEN *)
50166124Srafanset_term(SCREEN *screenp)
5150276Speter{
5262449Speter    SCREEN *oldSP;
53184989Srafan    SCREEN *newSP;
5450276Speter
5562449Speter    T((T_CALLED("set_term(%p)"), screenp));
5650276Speter
57184989Srafan    _nc_lock_global(curses);
58174993Srafan
5962449Speter    oldSP = SP;
6062449Speter    _nc_set_screen(screenp);
61184989Srafan    newSP = SP;
6250276Speter
63184989Srafan    if (newSP != 0) {
64184989Srafan	set_curterm(newSP->_term);
65174993Srafan#if !USE_REENTRANT
66184989Srafan	curscr = newSP->_curscr;
67184989Srafan	newscr = newSP->_newscr;
68184989Srafan	stdscr = newSP->_stdscr;
69184989Srafan	COLORS = newSP->_color_count;
70184989Srafan	COLOR_PAIRS = newSP->_pair_count;
71174993Srafan#endif
72178866Srafan    } else {
73178866Srafan	set_curterm(0);
74178866Srafan#if !USE_REENTRANT
75178866Srafan	curscr = 0;
76178866Srafan	newscr = 0;
77178866Srafan	stdscr = 0;
78178866Srafan	COLORS = 0;
79178866Srafan	COLOR_PAIRS = 0;
80178866Srafan#endif
81178866Srafan    }
8250276Speter
83184989Srafan    _nc_unlock_global(curses);
84174993Srafan
8562449Speter    T((T_RETURN("%p"), oldSP));
8662449Speter    return (oldSP);
8750276Speter}
8850276Speter
8962449Speterstatic void
90174993Srafan_nc_free_keytry(TRIES * kt)
9150276Speter{
9262449Speter    if (kt != 0) {
9362449Speter	_nc_free_keytry(kt->child);
9462449Speter	_nc_free_keytry(kt->sibling);
9562449Speter	free(kt);
9662449Speter    }
9750276Speter}
9850276Speter
99178866Srafanstatic bool
100178866Srafandelink_screen(SCREEN *sp)
101178866Srafan{
102178866Srafan    SCREEN *last = 0;
103178866Srafan    SCREEN *temp;
104178866Srafan    bool result = FALSE;
105178866Srafan
106178866Srafan    for (each_screen(temp)) {
107178866Srafan	if (temp == sp) {
108178866Srafan	    if (last)
109178866Srafan		last = sp->_next_screen;
110178866Srafan	    else
111178866Srafan		_nc_screen_chain = sp->_next_screen;
112178866Srafan	    result = TRUE;
113178866Srafan	    break;
114178866Srafan	}
115178866Srafan	last = temp;
116178866Srafan    }
117178866Srafan    return result;
118178866Srafan}
119178866Srafan
12050276Speter/*
12150276Speter * Free the storage associated with the given SCREEN sp.
12250276Speter */
12376726SpeterNCURSES_EXPORT(void)
124166124Srafandelscreen(SCREEN *sp)
12550276Speter{
126166124Srafan    int i;
12750276Speter
12862449Speter    T((T_CALLED("delscreen(%p)"), sp));
12950276Speter
130184989Srafan    _nc_lock_global(curses);
131178866Srafan    if (delink_screen(sp)) {
13250276Speter
133178866Srafan	(void) _nc_freewin(sp->_curscr);
134178866Srafan	(void) _nc_freewin(sp->_newscr);
135178866Srafan	(void) _nc_freewin(sp->_stdscr);
136166124Srafan
137178866Srafan	if (sp->_slk != 0) {
138178866Srafan	    if (sp->_slk->ent != 0) {
139178866Srafan		for (i = 0; i < sp->_slk->labcnt; ++i) {
140178866Srafan		    FreeIfNeeded(sp->_slk->ent[i].ent_text);
141178866Srafan		    FreeIfNeeded(sp->_slk->ent[i].form_text);
142178866Srafan		}
143178866Srafan		free(sp->_slk->ent);
144166124Srafan	    }
145178866Srafan	    free(sp->_slk);
146178866Srafan	    sp->_slk = 0;
147166124Srafan	}
148166124Srafan
149178866Srafan	_nc_free_keytry(sp->_keytry);
150178866Srafan	sp->_keytry = 0;
151166124Srafan
152178866Srafan	_nc_free_keytry(sp->_key_ok);
153178866Srafan	sp->_key_ok = 0;
15450276Speter
155178866Srafan	FreeIfNeeded(sp->_current_attr);
156166124Srafan
157178866Srafan	FreeIfNeeded(sp->_color_table);
158178866Srafan	FreeIfNeeded(sp->_color_pairs);
15950276Speter
160178866Srafan	FreeIfNeeded(sp->oldhash);
161178866Srafan	FreeIfNeeded(sp->newhash);
162178866Srafan	FreeIfNeeded(sp->hashtab);
16350276Speter
164178866Srafan	FreeIfNeeded(sp->_acs_map);
165178866Srafan	FreeIfNeeded(sp->_screen_acs_map);
166166124Srafan
167178866Srafan	/*
168178866Srafan	 * If the associated output stream has been closed, we can discard the
169178866Srafan	 * set-buffer.  Limit the error check to EBADF, since fflush may fail
170178866Srafan	 * for other reasons than trying to operate upon a closed stream.
171178866Srafan	 */
172178866Srafan	if (sp->_ofp != 0
173178866Srafan	    && sp->_setbuf != 0
174178866Srafan	    && fflush(sp->_ofp) != 0
175178866Srafan	    && errno == EBADF) {
176178866Srafan	    free(sp->_setbuf);
177178866Srafan	}
17850276Speter
179184989Srafan	del_curterm(sp->_term);
180178866Srafan	free(sp);
18162449Speter
182178866Srafan	/*
183178866Srafan	 * If this was the current screen, reset everything that the
184178866Srafan	 * application might try to use (except cur_term, which may have
185178866Srafan	 * multiple references in different screens).
186178866Srafan	 */
187178866Srafan	if (sp == SP) {
188174993Srafan#if !USE_REENTRANT
189178866Srafan	    curscr = 0;
190178866Srafan	    newscr = 0;
191178866Srafan	    stdscr = 0;
192178866Srafan	    COLORS = 0;
193178866Srafan	    COLOR_PAIRS = 0;
194174993Srafan#endif
195178866Srafan	    _nc_set_screen(0);
196178866Srafan	}
19762449Speter    }
198184989Srafan    _nc_unlock_global(curses);
199174993Srafan
20062449Speter    returnVoid;
20150276Speter}
20250276Speter
20362449Speterstatic bool
204166124Srafanno_mouse_event(SCREEN *sp GCC_UNUSED)
20562449Speter{
20662449Speter    return FALSE;
20762449Speter}
20850276Speter
20962449Speterstatic bool
210166124Srafanno_mouse_inline(SCREEN *sp GCC_UNUSED)
21162449Speter{
21262449Speter    return FALSE;
21362449Speter}
21462449Speter
21562449Speterstatic bool
216184989Srafanno_mouse_parse(SCREEN *sp GCC_UNUSED, int code GCC_UNUSED)
21762449Speter{
21862449Speter    return TRUE;
21962449Speter}
22062449Speter
22162449Speterstatic void
222166124Srafanno_mouse_resume(SCREEN *sp GCC_UNUSED)
22362449Speter{
22462449Speter}
22562449Speter
22662449Speterstatic void
227166124Srafanno_mouse_wrap(SCREEN *sp GCC_UNUSED)
22862449Speter{
22962449Speter}
23062449Speter
23166963Speter#if NCURSES_EXT_FUNCS && USE_COLORFGBG
23262449Speterstatic char *
23362449Speterextract_fgbg(char *src, int *result)
23462449Speter{
23562449Speter    char *dst = 0;
23662449Speter    long value = strtol(src, &dst, 0);
23762449Speter
23862449Speter    if (dst == 0) {
23962449Speter	dst = src;
24062449Speter    } else if (value >= 0) {
24198503Speter	*result = value;
24262449Speter    }
24362449Speter    while (*dst != 0 && *dst != ';')
24462449Speter	dst++;
24562449Speter    if (*dst == ';')
24662449Speter	dst++;
24762449Speter    return dst;
24862449Speter}
24962449Speter#endif
25062449Speter
251166124Srafan/* OS-independent screen initializations */
25276726SpeterNCURSES_EXPORT(int)
253174993Srafan_nc_setupscreen(int slines GCC_UNUSED,
254174993Srafan		int scolumns GCC_UNUSED,
255166124Srafan		FILE *output,
256166124Srafan		bool filtered,
257166124Srafan		int slk_format)
25850276Speter{
259176187Srafan    char *env;
26062449Speter    int bottom_stolen = 0;
261166124Srafan    bool support_cookies = USE_XMC_SUPPORT;
262174993Srafan    ripoff_t *rop;
26350276Speter
264166124Srafan    T((T_CALLED("_nc_setupscreen(%d, %d, %p, %d, %d)"),
265166124Srafan       slines, scolumns, output, filtered, slk_format));
266166124Srafan
26762449Speter    assert(SP == 0);		/* has been reset in newterm() ! */
268166124Srafan    if (!_nc_alloc_screen()
269166124Srafan	|| ((SP->_acs_map = typeCalloc(chtype, ACS_LEN)) == 0)
270166124Srafan	|| ((SP->_screen_acs_map = typeCalloc(bool, ACS_LEN)) == 0)) {
271166124Srafan	returnCode(ERR);
272166124Srafan    }
27350276Speter
274166124Srafan    T(("created SP %p", SP));
27562449Speter    SP->_next_screen = _nc_screen_chain;
27662449Speter    _nc_screen_chain = SP;
27750276Speter
278166124Srafan    if ((SP->_current_attr = typeCalloc(NCURSES_CH_T, 1)) == 0)
279166124Srafan	returnCode(ERR);
280166124Srafan
281174993Srafan    /*
282174993Srafan     * We should always check the screensize, just in case.
283174993Srafan     */
284178866Srafan    _nc_get_screensize(SP, &slines, &scolumns);
285174993Srafan    SET_LINES(slines);
286174993Srafan    SET_COLS(scolumns);
287174993Srafan    T((T_CREATE("screen %s %dx%d"), termname(), LINES, COLS));
288174993Srafan
289166124Srafan    SP->_filtered = filtered;
290166124Srafan
291166124Srafan    /* implement filter mode */
292166124Srafan    if (filtered) {
293174993Srafan	slines = 1;
294174993Srafan	SET_LINES(slines);
295166124Srafan	clear_screen = 0;
296166124Srafan	cursor_down = parm_down_cursor = 0;
297166124Srafan	cursor_address = 0;
298166124Srafan	cursor_up = parm_up_cursor = 0;
299166124Srafan	row_address = 0;
300166124Srafan
301166124Srafan	cursor_home = carriage_return;
302166124Srafan	T(("filter screensize %dx%d", LINES, COLS));
303166124Srafan    }
304166124Srafan#ifdef __DJGPP__
305166124Srafan    T(("setting output mode to binary"));
306166124Srafan    fflush(output);
307166124Srafan    setmode(output, O_BINARY);
308166124Srafan#endif
30962449Speter    _nc_set_buffer(output, TRUE);
31062449Speter    SP->_term = cur_term;
31162449Speter    SP->_lines = slines;
31262449Speter    SP->_lines_avail = slines;
31362449Speter    SP->_columns = scolumns;
31462449Speter    SP->_cursrow = -1;
31562449Speter    SP->_curscol = -1;
31662449Speter    SP->_nl = TRUE;
31762449Speter    SP->_raw = FALSE;
31862449Speter    SP->_cbreak = 0;
31962449Speter    SP->_echo = TRUE;
32062449Speter    SP->_fifohead = -1;
32162449Speter    SP->_endwin = TRUE;
32262449Speter    SP->_ofp = output;
32362449Speter    SP->_cursor = -1;		/* cannot know real cursor shape */
32466963Speter
325184989Srafan    SetNoPadding(SP);
32666963Speter
32766963Speter#if NCURSES_EXT_FUNCS
32862449Speter    SP->_default_color = FALSE;
32962449Speter    SP->_has_sgr_39_49 = FALSE;
33066963Speter
33166963Speter    /*
33266963Speter     * Set our assumption of the terminal's default foreground and background
33366963Speter     * colors.  The curs_color man-page states that we can assume that the
33466963Speter     * background is black.  The origin of this assumption appears to be
33566963Speter     * terminals that displayed colored text, but no colored backgrounds, e.g.,
33666963Speter     * the first colored terminals around 1980.  More recent ones with better
33766963Speter     * technology can display not only colored backgrounds, but all
33866963Speter     * combinations.  So a terminal might be something other than "white" on
33966963Speter     * black (green/black looks monochrome too), but black on white or even
34066963Speter     * on ivory.
34166963Speter     *
34266963Speter     * White-on-black is the simplest thing to use for monochrome.  Almost
34366963Speter     * all applications that use color paint both text and background, so
34466963Speter     * the distinction is moot.  But a few do not - which is why we leave this
34566963Speter     * configurable (a better solution is to use assume_default_colors() for
34666963Speter     * the rare applications that do require that sort of appearance, since
34766963Speter     * is appears that more users expect to be able to make a white-on-black
34866963Speter     * or black-on-white display under control of the application than not).
34966963Speter     */
35066963Speter#ifdef USE_ASSUMED_COLOR
35162449Speter    SP->_default_fg = COLOR_WHITE;
35262449Speter    SP->_default_bg = COLOR_BLACK;
35366963Speter#else
35466963Speter    SP->_default_fg = C_MASK;
35566963Speter    SP->_default_bg = C_MASK;
35666963Speter#endif
35766963Speter
358166124Srafan    /*
359166124Srafan     * Allow those assumed/default color assumptions to be overridden at
360166124Srafan     * runtime:
361166124Srafan     */
362184989Srafan    if ((env = getenv("NCURSES_ASSUMED_COLORS")) != 0) {
363166124Srafan	int fg, bg;
364166124Srafan	char sep1, sep2;
365184989Srafan	int count = sscanf(env, "%d%c%d%c", &fg, &sep1, &bg, &sep2);
366166124Srafan	if (count >= 1) {
367166124Srafan	    SP->_default_fg = (fg >= 0 && fg < max_colors) ? fg : C_MASK;
368166124Srafan	    if (count >= 3) {
369166124Srafan		SP->_default_bg = (bg >= 0 && bg < max_colors) ? bg : C_MASK;
370166124Srafan	    }
371166124Srafan	    TR(TRACE_CHARPUT | TRACE_MOVE,
372166124Srafan	       ("from environment assumed fg=%d, bg=%d",
373166124Srafan		SP->_default_fg,
374166124Srafan		SP->_default_bg));
375166124Srafan	}
376166124Srafan    }
37766963Speter#if USE_COLORFGBG
37862449Speter    /*
37962449Speter     * If rxvt's $COLORFGBG variable is set, use it to specify the assumed
38062449Speter     * default colors.  Note that rxvt (mis)uses bold colors, equating a bold
38162449Speter     * color to that value plus 8.  We'll only use the non-bold color for now -
38262449Speter     * decide later if it is worth having default attributes as well.
38362449Speter     */
38462449Speter    if (getenv("COLORFGBG") != 0) {
38562449Speter	char *p = getenv("COLORFGBG");
38697049Speter	TR(TRACE_CHARPUT | TRACE_MOVE, ("decoding COLORFGBG %s", p));
38762449Speter	p = extract_fgbg(p, &(SP->_default_fg));
38862449Speter	p = extract_fgbg(p, &(SP->_default_bg));
38997049Speter	if (*p)			/* assume rxvt was compiled with xpm support */
39097049Speter	    p = extract_fgbg(p, &(SP->_default_bg));
39197049Speter	TR(TRACE_CHARPUT | TRACE_MOVE, ("decoded fg=%d, bg=%d",
39297049Speter					SP->_default_fg, SP->_default_bg));
393166124Srafan	if (SP->_default_fg >= max_colors) {
39498503Speter	    if (set_a_foreground != ABSENT_STRING
39598503Speter		&& !strcmp(set_a_foreground, "\033[3%p1%dm")) {
39698503Speter		set_a_foreground = "\033[3%?%p1%{8}%>%t9%e%p1%d%;m";
39798503Speter	    } else {
39898503Speter		SP->_default_fg %= max_colors;
39998503Speter	    }
40098503Speter	}
401166124Srafan	if (SP->_default_bg >= max_colors) {
40298503Speter	    if (set_a_background != ABSENT_STRING
40398503Speter		&& !strcmp(set_a_background, "\033[4%p1%dm")) {
40498503Speter		set_a_background = "\033[4%?%p1%{8}%>%t9%e%p1%d%;m";
40598503Speter	    } else {
40698503Speter		SP->_default_bg %= max_colors;
40798503Speter	    }
40898503Speter	}
40962449Speter    }
41062449Speter#endif
41162449Speter#endif /* NCURSES_EXT_FUNCS */
41250276Speter
41362449Speter    SP->_maxclick = DEFAULT_MAXCLICK;
41462449Speter    SP->_mouse_event = no_mouse_event;
41562449Speter    SP->_mouse_inline = no_mouse_inline;
41662449Speter    SP->_mouse_parse = no_mouse_parse;
41762449Speter    SP->_mouse_resume = no_mouse_resume;
41862449Speter    SP->_mouse_wrap = no_mouse_wrap;
41962449Speter    SP->_mouse_fd = -1;
42050276Speter
42162449Speter    /*
422166124Srafan     * If we've no magic cookie support, we suppress attributes that xmc would
423166124Srafan     * affect, i.e., the attributes that affect the rendition of a space.
42462449Speter     */
425166124Srafan    SP->_ok_attributes = termattrs();
426166124Srafan    if (has_colors()) {
427166124Srafan	SP->_ok_attributes |= A_COLOR;
428166124Srafan    }
429166124Srafan#if USE_XMC_SUPPORT
430166124Srafan    /*
431166124Srafan     * If we have no magic-cookie support compiled-in, or if it is suppressed
432166124Srafan     * in the environment, reset the support-flag.
433166124Srafan     */
434166124Srafan    if (magic_cookie_glitch >= 0) {
435166124Srafan	if (getenv("NCURSES_NO_MAGIC_COOKIE") != 0) {
436166124Srafan	    support_cookies = FALSE;
437166124Srafan	}
438166124Srafan    }
439166124Srafan#endif
44050276Speter
441166124Srafan    if (!support_cookies && magic_cookie_glitch >= 0) {
442166124Srafan	T(("will disable attributes to work w/o magic cookies"));
443166124Srafan    }
444166124Srafan
445166124Srafan    if (magic_cookie_glitch > 0) {	/* tvi, wyse */
446166124Srafan
447166124Srafan	SP->_xmc_triggers = SP->_ok_attributes & (
448166124Srafan						     A_STANDOUT |
449166124Srafan						     A_UNDERLINE |
450166124Srafan						     A_REVERSE |
451166124Srafan						     A_BLINK |
452166124Srafan						     A_DIM |
453166124Srafan						     A_BOLD |
454166124Srafan						     A_INVIS |
455166124Srafan						     A_PROTECT
45662449Speter	    );
457166124Srafan#if 0
458166124Srafan	/*
459166124Srafan	 * We "should" treat colors as an attribute.  The wyse350 (and its
460166124Srafan	 * clones) appear to be the only ones that have both colors and magic
461166124Srafan	 * cookies.
462166124Srafan	 */
463166124Srafan	if (has_colors()) {
464166124Srafan	    SP->_xmc_triggers |= A_COLOR;
465166124Srafan	}
466166124Srafan#endif
467166124Srafan	SP->_xmc_suppress = SP->_xmc_triggers & (chtype) ~(A_BOLD);
46850276Speter
46962449Speter	T(("magic cookie attributes %s", _traceattr(SP->_xmc_suppress)));
470166124Srafan	/*
471166124Srafan	 * Supporting line-drawing may be possible.  But make the regular
472166124Srafan	 * video attributes work first.
473166124Srafan	 */
474166124Srafan	acs_chars = ABSENT_STRING;
475166124Srafan	ena_acs = ABSENT_STRING;
476166124Srafan	enter_alt_charset_mode = ABSENT_STRING;
477166124Srafan	exit_alt_charset_mode = ABSENT_STRING;
47850276Speter#if USE_XMC_SUPPORT
47962449Speter	/*
480166124Srafan	 * To keep the cookie support simple, suppress all of the optimization
481166124Srafan	 * hooks except for clear_screen and the cursor addressing.
48262449Speter	 */
483166124Srafan	if (support_cookies) {
484166124Srafan	    clr_eol = ABSENT_STRING;
485166124Srafan	    clr_eos = ABSENT_STRING;
486166124Srafan	    set_attributes = ABSENT_STRING;
487166124Srafan	}
48850276Speter#endif
489166124Srafan    } else if (magic_cookie_glitch == 0) {	/* hpterm */
49062449Speter    }
491166124Srafan
492166124Srafan    /*
493166124Srafan     * If magic cookies are not supported, cancel the strings that set
494166124Srafan     * video attributes.
495166124Srafan     */
496166124Srafan    if (!support_cookies && magic_cookie_glitch >= 0) {
497166124Srafan	magic_cookie_glitch = ABSENT_NUMERIC;
498166124Srafan	set_attributes = ABSENT_STRING;
499166124Srafan	enter_blink_mode = ABSENT_STRING;
500166124Srafan	enter_bold_mode = ABSENT_STRING;
501166124Srafan	enter_dim_mode = ABSENT_STRING;
502166124Srafan	enter_reverse_mode = ABSENT_STRING;
503166124Srafan	enter_standout_mode = ABSENT_STRING;
504166124Srafan	enter_underline_mode = ABSENT_STRING;
505166124Srafan    }
506166124Srafan
507166124Srafan    /* initialize normal acs before wide, since we use mapping in the latter */
508174993Srafan#if !USE_WIDEC_SUPPORT
509184989Srafan    if (_nc_unicode_locale() && _nc_locale_breaks_acs(cur_term)) {
510174993Srafan	acs_chars = NULL;
511174993Srafan	ena_acs = NULL;
512174993Srafan	enter_alt_charset_mode = NULL;
513174993Srafan	exit_alt_charset_mode = NULL;
514174993Srafan	set_attributes = NULL;
515174993Srafan    }
516174993Srafan#endif
517166124Srafan    _nc_init_acs();
51897049Speter#if USE_WIDEC_SUPPORT
51997049Speter    _nc_init_wacs();
520166124Srafan
521184989Srafan    SP->_screen_acs_fix = (_nc_unicode_locale()
522184989Srafan			   && _nc_locale_breaks_acs(cur_term));
52397049Speter#endif
524176187Srafan    env = _nc_get_locale();
525176187Srafan    SP->_legacy_coding = ((env == 0)
526176187Srafan			  || !strcmp(env, "C")
527176187Srafan			  || !strcmp(env, "POSIX"));
528176187Srafan    T(("legacy-coding %d", SP->_legacy_coding));
52950276Speter
53062449Speter    _nc_idcok = TRUE;
53162449Speter    _nc_idlok = FALSE;
53250276Speter
53362449Speter    SP->oldhash = 0;
53462449Speter    SP->newhash = 0;
53550276Speter
53662449Speter    T(("creating newscr"));
537174993Srafan    if ((SP->_newscr = newwin(slines, scolumns, 0, 0)) == 0)
538166124Srafan	returnCode(ERR);
53950276Speter
54062449Speter    T(("creating curscr"));
541174993Srafan    if ((SP->_curscr = newwin(slines, scolumns, 0, 0)) == 0)
542166124Srafan	returnCode(ERR);
54350276Speter
544174993Srafan#if !USE_REENTRANT
545174993Srafan    newscr = SP->_newscr;
546174993Srafan    curscr = SP->_curscr;
547174993Srafan#endif
54850276Speter#if USE_SIZECHANGE
54962449Speter    SP->_resize = resizeterm;
55050276Speter#endif
55150276Speter
55262449Speter    newscr->_clear = TRUE;
55362449Speter    curscr->_clear = FALSE;
55450276Speter
55597049Speter    def_shell_mode();
55697049Speter    def_prog_mode();
55797049Speter
558174993Srafan    for (rop = ripoff_stack;
559174993Srafan	 rop != ripoff_sp && (rop - ripoff_stack) < N_RIPS;
560174993Srafan	 rop++) {
56150276Speter
562174993Srafan	/* If we must simulate soft labels, grab off the line to be used.
563174993Srafan	   We assume that we must simulate, if it is none of the standard
564174993Srafan	   formats (4-4 or 3-2-3) for which there may be some hardware
565174993Srafan	   support. */
566174993Srafan	if (rop->hook == _nc_slk_initialize)
567174993Srafan	    if (!(num_labels <= 0 || !SLK_STDFMT(slk_format)))
568174993Srafan		continue;
569174993Srafan	if (rop->hook) {
570174993Srafan	    int count;
571174993Srafan	    WINDOW *w;
572174993Srafan
573174993Srafan	    count = (rop->line < 0) ? -rop->line : rop->line;
574174993Srafan	    T(("ripping off %i lines at %s", count,
575174993Srafan	       ((rop->line < 0)
576174993Srafan		? "bottom"
577174993Srafan		: "top")));
578174993Srafan
579174993Srafan	    w = newwin(count, scolumns,
580174993Srafan		       ((rop->line < 0)
581174993Srafan			? SP->_lines_avail - count
582174993Srafan			: 0),
583174993Srafan		       0);
584176187Srafan	    if (w) {
585176187Srafan		rop->win = w;
586174993Srafan		rop->hook(w, scolumns);
587176187Srafan	    } else {
588166124Srafan		returnCode(ERR);
589176187Srafan	    }
590174993Srafan	    if (rop->line < 0)
591166124Srafan		bottom_stolen += count;
592166124Srafan	    else
593166124Srafan		SP->_topstolen += count;
59462449Speter	    SP->_lines_avail -= count;
59550276Speter	}
59662449Speter    }
59762449Speter    /* reset the stack */
598174993Srafan    ripoff_sp = ripoff_stack;
59950276Speter
60062449Speter    T(("creating stdscr"));
60162449Speter    assert((SP->_lines_avail + SP->_topstolen + bottom_stolen) == slines);
602174993Srafan    if ((SP->_stdscr = newwin(SP->_lines_avail, scolumns, 0, 0)) == 0)
603166124Srafan	returnCode(ERR);
60450276Speter
605174993Srafan    SET_LINES(SP->_lines_avail);
606174993Srafan#if !USE_REENTRANT
607174993Srafan    stdscr = SP->_stdscr;
608174993Srafan#endif
609174993Srafan
610166124Srafan    returnCode(OK);
61150276Speter}
61250276Speter
613174993Srafan/*
614174993Srafan * The internal implementation interprets line as the number of lines to rip
615174993Srafan * off from the top or bottom.
616174993Srafan */
61776726SpeterNCURSES_EXPORT(int)
61862449Speter_nc_ripoffline(int line, int (*init) (WINDOW *, int))
61950276Speter{
620166124Srafan    T((T_CALLED("_nc_ripoffline(%d, %p)"), line, init));
62150276Speter
622166124Srafan    if (line != 0) {
62350276Speter
624174993Srafan	if (ripoff_sp == 0)
625174993Srafan	    ripoff_sp = ripoff_stack;
626174993Srafan	if (ripoff_sp >= ripoff_stack + N_RIPS)
627166124Srafan	    returnCode(ERR);
62850276Speter
629174993Srafan	ripoff_sp->line = line;
630174993Srafan	ripoff_sp->hook = init;
631174993Srafan	ripoff_sp++;
632166124Srafan    }
633166124Srafan
634166124Srafan    returnCode(OK);
63550276Speter}
63650276Speter
63776726SpeterNCURSES_EXPORT(int)
63862449Speterripoffline(int line, int (*init) (WINDOW *, int))
63950276Speter{
640176187Srafan    START_TRACE();
64150276Speter    T((T_CALLED("ripoffline(%d,%p)"), line, init));
64250276Speter
64350276Speter    if (line == 0)
64450276Speter	returnCode(OK);
64550276Speter
64662449Speter    returnCode(_nc_ripoffline((line < 0) ? -1 : 1, init));
64750276Speter}
648