lib_set_term.c revision 176187
1285SN/A/****************************************************************************
2367SN/A * Copyright (c) 1998-2007,2008 Free Software Foundation, Inc.              *
3285SN/A *                                                                          *
4285SN/A * Permission is hereby granted, free of charge, to any person obtaining a  *
5285SN/A * copy of this software and associated documentation files (the            *
6285SN/A * "Software"), to deal in the Software without restriction, including      *
7285SN/A * without limitation the rights to use, copy, modify, merge, publish,      *
8285SN/A * distribute, distribute with modifications, sublicense, and/or sell       *
9285SN/A * copies of the Software, and to permit persons to whom the Software is    *
10285SN/A * furnished to do so, subject to the following conditions:                 *
11285SN/A *                                                                          *
12285SN/A * The above copyright notice and this permission notice shall be included  *
13285SN/A * in all copies or substantial portions of the Software.                   *
14285SN/A *                                                                          *
15285SN/A * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
16285SN/A * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
17285SN/A * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
18285SN/A * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
19285SN/A * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
20285SN/A * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
21285SN/A * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
22285SN/A *                                                                          *
23285SN/A * Except as contained in this notice, the name(s) of the above copyright   *
24285SN/A * holders shall not be used in advertising or otherwise to promote the     *
25285SN/A * sale, use or other dealings in this Software without prior written       *
26285SN/A * authorization.                                                           *
27285SN/A ****************************************************************************/
28285SN/A
29285SN/A/****************************************************************************
30285SN/A *  Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
31285SN/A *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
32285SN/A *     and: Thomas E. Dickey                        1996-on                 *
33285SN/A ****************************************************************************/
34285SN/A
35285SN/A/*
36285SN/A**	lib_set_term.c
37285SN/A**
38367SN/A**	The routine set_term().
39285SN/A**
40285SN/A*/
41285SN/A
42285SN/A#include <curses.priv.h>
43285SN/A
44285SN/A#include <term.h>		/* cur_term */
45285SN/A#include <tic.h>
46285SN/A
47285SN/AMODULE_ID("$Id: lib_set_term.c,v 1.103 2008/02/03 20:31:08 tom Exp $")
48285SN/A
49285SN/ANCURSES_EXPORT(SCREEN *)
50285SN/Aset_term(SCREEN *screenp)
51285SN/A{
52285SN/A    SCREEN *oldSP;
53285SN/A
54285SN/A    T((T_CALLED("set_term(%p)"), screenp));
55285SN/A
56285SN/A    _nc_lock_global(set_SP);
57285SN/A
58285SN/A    oldSP = SP;
59285SN/A    _nc_set_screen(screenp);
60285SN/A
61285SN/A    set_curterm(SP->_term);
62285SN/A#if !USE_REENTRANT
63285SN/A    curscr = SP->_curscr;
64285SN/A    newscr = SP->_newscr;
65285SN/A    stdscr = SP->_stdscr;
66285SN/A    COLORS = SP->_color_count;
67285SN/A    COLOR_PAIRS = SP->_pair_count;
68285SN/A#endif
69285SN/A
70285SN/A    _nc_unlock_global(set_SP);
71285SN/A
72285SN/A    T((T_RETURN("%p"), oldSP));
73285SN/A    return (oldSP);
74285SN/A}
75285SN/A
76285SN/Astatic void
77285SN/A_nc_free_keytry(TRIES * kt)
78285SN/A{
79285SN/A    if (kt != 0) {
80285SN/A	_nc_free_keytry(kt->child);
81285SN/A	_nc_free_keytry(kt->sibling);
82285SN/A	free(kt);
83285SN/A    }
84285SN/A}
85285SN/A
86285SN/A/*
87285SN/A * Free the storage associated with the given SCREEN sp.
88285SN/A */
89285SN/ANCURSES_EXPORT(void)
90285SN/Adelscreen(SCREEN *sp)
91285SN/A{
92285SN/A    SCREEN **scan = &_nc_screen_chain;
93285SN/A    int i;
94285SN/A
95285SN/A    T((T_CALLED("delscreen(%p)"), sp));
96285SN/A
97285SN/A    _nc_lock_global(set_SP);
98285SN/A    while (*scan) {
99367SN/A	if (*scan == sp) {
100367SN/A	    *scan = sp->_next_screen;
101367SN/A	    break;
102367SN/A	}
103367SN/A	scan = &(*scan)->_next_screen;
104367SN/A    }
105367SN/A
106367SN/A    (void) _nc_freewin(sp->_curscr);
107367SN/A    (void) _nc_freewin(sp->_newscr);
108367SN/A    (void) _nc_freewin(sp->_stdscr);
109367SN/A
110367SN/A    if (sp->_slk != 0) {
111367SN/A	if (sp->_slk->ent != 0) {
112367SN/A	    for (i = 0; i < sp->_slk->labcnt; ++i) {
113367SN/A		FreeIfNeeded(sp->_slk->ent[i].ent_text);
114367SN/A		FreeIfNeeded(sp->_slk->ent[i].form_text);
115367SN/A	    }
116367SN/A	    free(sp->_slk->ent);
117367SN/A	}
118367SN/A	free(sp->_slk);
119367SN/A	sp->_slk = 0;
120367SN/A    }
121367SN/A
122367SN/A    _nc_free_keytry(sp->_keytry);
123367SN/A    sp->_keytry = 0;
124367SN/A
125367SN/A    _nc_free_keytry(sp->_key_ok);
126367SN/A    sp->_key_ok = 0;
127367SN/A
128367SN/A    FreeIfNeeded(sp->_current_attr);
129367SN/A
130367SN/A    FreeIfNeeded(sp->_color_table);
131367SN/A    FreeIfNeeded(sp->_color_pairs);
132367SN/A
133367SN/A    FreeIfNeeded(sp->oldhash);
134367SN/A    FreeIfNeeded(sp->newhash);
135367SN/A    FreeIfNeeded(sp->hashtab);
136367SN/A
137367SN/A    FreeIfNeeded(sp->_acs_map);
138367SN/A    FreeIfNeeded(sp->_screen_acs_map);
139367SN/A
140367SN/A    del_curterm(sp->_term);
141367SN/A
142367SN/A    /*
143367SN/A     * If the associated output stream has been closed, we can discard the
144367SN/A     * set-buffer.  Limit the error check to EBADF, since fflush may fail
145367SN/A     * for other reasons than trying to operate upon a closed stream.
146367SN/A     */
147367SN/A    if (sp->_ofp != 0
148367SN/A	&& sp->_setbuf != 0
149367SN/A	&& fflush(sp->_ofp) != 0
150367SN/A	&& errno == EBADF) {
151367SN/A	free(sp->_setbuf);
152367SN/A    }
153367SN/A
154367SN/A    free(sp);
155367SN/A
156367SN/A    /*
157367SN/A     * If this was the current screen, reset everything that the
158367SN/A     * application might try to use (except cur_term, which may have
159367SN/A     * multiple references in different screens).
160367SN/A     */
161367SN/A    if (sp == SP) {
162367SN/A#if !USE_REENTRANT
163367SN/A	curscr = 0;
164367SN/A	newscr = 0;
165367SN/A	stdscr = 0;
166367SN/A	COLORS = 0;
167367SN/A	COLOR_PAIRS = 0;
168367SN/A#endif
169285SN/A	_nc_set_screen(0);
170285SN/A    }
171285SN/A    _nc_unlock_global(set_SP);
172367SN/A
173285SN/A    returnVoid;
174285SN/A}
175285SN/A
176285SN/Astatic bool
177285SN/Ano_mouse_event(SCREEN *sp GCC_UNUSED)
178285SN/A{
179285SN/A    return FALSE;
180285SN/A}
181285SN/A
182285SN/Astatic bool
183367SN/Ano_mouse_inline(SCREEN *sp GCC_UNUSED)
184285SN/A{
185285SN/A    return FALSE;
186285SN/A}
187285SN/A
188285SN/Astatic bool
189285SN/Ano_mouse_parse(int code GCC_UNUSED)
190285SN/A{
191367SN/A    return TRUE;
192285SN/A}
193285SN/A
194285SN/Astatic void
195285SN/Ano_mouse_resume(SCREEN *sp GCC_UNUSED)
196285SN/A{
197285SN/A}
198285SN/A
199285SN/Astatic void
200285SN/Ano_mouse_wrap(SCREEN *sp GCC_UNUSED)
201285SN/A{
202285SN/A}
203285SN/A
204285SN/A#if NCURSES_EXT_FUNCS && USE_COLORFGBG
205285SN/Astatic char *
206285SN/Aextract_fgbg(char *src, int *result)
207285SN/A{
208285SN/A    char *dst = 0;
209285SN/A    long value = strtol(src, &dst, 0);
210285SN/A
211285SN/A    if (dst == 0) {
212285SN/A	dst = src;
213285SN/A    } else if (value >= 0) {
214285SN/A	*result = value;
215285SN/A    }
216285SN/A    while (*dst != 0 && *dst != ';')
217285SN/A	dst++;
218285SN/A    if (*dst == ';')
219285SN/A	dst++;
220285SN/A    return dst;
221285SN/A}
222285SN/A#endif
223367SN/A
224285SN/A/* OS-independent screen initializations */
225285SN/ANCURSES_EXPORT(int)
226285SN/A_nc_setupscreen(int slines GCC_UNUSED,
227285SN/A		int scolumns GCC_UNUSED,
228285SN/A		FILE *output,
229285SN/A		bool filtered,
230285SN/A		int slk_format)
231285SN/A{
232285SN/A    char *env;
233285SN/A    int bottom_stolen = 0;
234285SN/A    bool support_cookies = USE_XMC_SUPPORT;
235285SN/A    ripoff_t *rop;
236285SN/A
237285SN/A    T((T_CALLED("_nc_setupscreen(%d, %d, %p, %d, %d)"),
238285SN/A       slines, scolumns, output, filtered, slk_format));
239285SN/A
240285SN/A    assert(SP == 0);		/* has been reset in newterm() ! */
241285SN/A    if (!_nc_alloc_screen()
242285SN/A	|| ((SP->_acs_map = typeCalloc(chtype, ACS_LEN)) == 0)
243285SN/A	|| ((SP->_screen_acs_map = typeCalloc(bool, ACS_LEN)) == 0)) {
244285SN/A	returnCode(ERR);
245285SN/A    }
246285SN/A
247285SN/A    T(("created SP %p", SP));
248285SN/A    SP->_next_screen = _nc_screen_chain;
249285SN/A    _nc_screen_chain = SP;
250285SN/A
251285SN/A    if ((SP->_current_attr = typeCalloc(NCURSES_CH_T, 1)) == 0)
252285SN/A	returnCode(ERR);
253285SN/A
254285SN/A    /*
255367SN/A     * We should always check the screensize, just in case.
256285SN/A     */
257285SN/A    _nc_get_screensize(&slines, &scolumns);
258285SN/A    SET_LINES(slines);
259285SN/A    SET_COLS(scolumns);
260285SN/A    T((T_CREATE("screen %s %dx%d"), termname(), LINES, COLS));
261285SN/A
262285SN/A    SP->_filtered = filtered;
263285SN/A
264285SN/A    /* implement filter mode */
265285SN/A    if (filtered) {
266285SN/A	slines = 1;
267285SN/A	SET_LINES(slines);
268285SN/A	clear_screen = 0;
269285SN/A	cursor_down = parm_down_cursor = 0;
270285SN/A	cursor_address = 0;
271285SN/A	cursor_up = parm_up_cursor = 0;
272285SN/A	row_address = 0;
273285SN/A
274285SN/A	cursor_home = carriage_return;
275285SN/A	T(("filter screensize %dx%d", LINES, COLS));
276285SN/A    }
277285SN/A#ifdef __DJGPP__
278285SN/A    T(("setting output mode to binary"));
279285SN/A    fflush(output);
280285SN/A    setmode(output, O_BINARY);
281285SN/A#endif
282285SN/A    _nc_set_buffer(output, TRUE);
283285SN/A    SP->_term = cur_term;
284285SN/A    SP->_lines = slines;
285285SN/A    SP->_lines_avail = slines;
286285SN/A    SP->_columns = scolumns;
287285SN/A    SP->_cursrow = -1;
288285SN/A    SP->_curscol = -1;
289285SN/A    SP->_nl = TRUE;
290285SN/A    SP->_raw = FALSE;
291285SN/A    SP->_cbreak = 0;
292285SN/A    SP->_echo = TRUE;
293285SN/A    SP->_fifohead = -1;
294285SN/A    SP->_endwin = TRUE;
295285SN/A    SP->_ofp = output;
296285SN/A    SP->_cursor = -1;		/* cannot know real cursor shape */
297285SN/A
298285SN/A#if NCURSES_NO_PADDING
299285SN/A    SP->_no_padding = getenv("NCURSES_NO_PADDING") != 0;
300285SN/A    TR(TRACE_CHARPUT | TRACE_MOVE, ("padding will%s be used",
301285SN/A				    SP->_no_padding ? " not" : ""));
302285SN/A#endif
303285SN/A
304285SN/A#if NCURSES_EXT_FUNCS
305285SN/A    SP->_default_color = FALSE;
306285SN/A    SP->_has_sgr_39_49 = FALSE;
307285SN/A
308285SN/A    /*
309285SN/A     * Set our assumption of the terminal's default foreground and background
310285SN/A     * colors.  The curs_color man-page states that we can assume that the
311285SN/A     * background is black.  The origin of this assumption appears to be
312285SN/A     * terminals that displayed colored text, but no colored backgrounds, e.g.,
313285SN/A     * the first colored terminals around 1980.  More recent ones with better
314285SN/A     * technology can display not only colored backgrounds, but all
315285SN/A     * combinations.  So a terminal might be something other than "white" on
316367SN/A     * black (green/black looks monochrome too), but black on white or even
317285SN/A     * on ivory.
318285SN/A     *
319285SN/A     * White-on-black is the simplest thing to use for monochrome.  Almost
320285SN/A     * all applications that use color paint both text and background, so
321285SN/A     * the distinction is moot.  But a few do not - which is why we leave this
322285SN/A     * configurable (a better solution is to use assume_default_colors() for
323285SN/A     * the rare applications that do require that sort of appearance, since
324285SN/A     * is appears that more users expect to be able to make a white-on-black
325285SN/A     * or black-on-white display under control of the application than not).
326285SN/A     */
327285SN/A#ifdef USE_ASSUMED_COLOR
328285SN/A    SP->_default_fg = COLOR_WHITE;
329285SN/A    SP->_default_bg = COLOR_BLACK;
330285SN/A#else
331285SN/A    SP->_default_fg = C_MASK;
332285SN/A    SP->_default_bg = C_MASK;
333285SN/A#endif
334285SN/A
335285SN/A    /*
336285SN/A     * Allow those assumed/default color assumptions to be overridden at
337285SN/A     * runtime:
338285SN/A     */
339367SN/A    if (getenv("NCURSES_ASSUMED_COLORS") != 0) {
340285SN/A	char *p = getenv("NCURSES_ASSUMED_COLORS");
341285SN/A	int fg, bg;
342285SN/A	char sep1, sep2;
343285SN/A	int count = sscanf(p, "%d%c%d%c", &fg, &sep1, &bg, &sep2);
344285SN/A	if (count >= 1) {
345285SN/A	    SP->_default_fg = (fg >= 0 && fg < max_colors) ? fg : C_MASK;
346285SN/A	    if (count >= 3) {
347285SN/A		SP->_default_bg = (bg >= 0 && bg < max_colors) ? bg : C_MASK;
348285SN/A	    }
349285SN/A	    TR(TRACE_CHARPUT | TRACE_MOVE,
350367SN/A	       ("from environment assumed fg=%d, bg=%d",
351285SN/A		SP->_default_fg,
352285SN/A		SP->_default_bg));
353285SN/A	}
354285SN/A    }
355285SN/A#if USE_COLORFGBG
356285SN/A    /*
357285SN/A     * If rxvt's $COLORFGBG variable is set, use it to specify the assumed
358285SN/A     * default colors.  Note that rxvt (mis)uses bold colors, equating a bold
359285SN/A     * color to that value plus 8.  We'll only use the non-bold color for now -
360285SN/A     * decide later if it is worth having default attributes as well.
361285SN/A     */
362285SN/A    if (getenv("COLORFGBG") != 0) {
363285SN/A	char *p = getenv("COLORFGBG");
364285SN/A	TR(TRACE_CHARPUT | TRACE_MOVE, ("decoding COLORFGBG %s", p));
365285SN/A	p = extract_fgbg(p, &(SP->_default_fg));
366285SN/A	p = extract_fgbg(p, &(SP->_default_bg));
367285SN/A	if (*p)			/* assume rxvt was compiled with xpm support */
368285SN/A	    p = extract_fgbg(p, &(SP->_default_bg));
369367SN/A	TR(TRACE_CHARPUT | TRACE_MOVE, ("decoded fg=%d, bg=%d",
370285SN/A					SP->_default_fg, SP->_default_bg));
371285SN/A	if (SP->_default_fg >= max_colors) {
372285SN/A	    if (set_a_foreground != ABSENT_STRING
373285SN/A		&& !strcmp(set_a_foreground, "\033[3%p1%dm")) {
374285SN/A		set_a_foreground = "\033[3%?%p1%{8}%>%t9%e%p1%d%;m";
375285SN/A	    } else {
376285SN/A		SP->_default_fg %= max_colors;
377285SN/A	    }
378285SN/A	}
379285SN/A	if (SP->_default_bg >= max_colors) {
380367SN/A	    if (set_a_background != ABSENT_STRING
381285SN/A		&& !strcmp(set_a_background, "\033[4%p1%dm")) {
382285SN/A		set_a_background = "\033[4%?%p1%{8}%>%t9%e%p1%d%;m";
383285SN/A	    } else {
384285SN/A		SP->_default_bg %= max_colors;
385285SN/A	    }
386285SN/A	}
387285SN/A    }
388285SN/A#endif
389285SN/A#endif /* NCURSES_EXT_FUNCS */
390285SN/A
391367SN/A    SP->_maxclick = DEFAULT_MAXCLICK;
392285SN/A    SP->_mouse_event = no_mouse_event;
393285SN/A    SP->_mouse_inline = no_mouse_inline;
394285SN/A    SP->_mouse_parse = no_mouse_parse;
395285SN/A    SP->_mouse_resume = no_mouse_resume;
396285SN/A    SP->_mouse_wrap = no_mouse_wrap;
397285SN/A    SP->_mouse_fd = -1;
398285SN/A
399285SN/A    /* initialize the panel hooks */
400285SN/A    SP->_panelHook.top_panel = (struct panel *) 0;
401285SN/A    SP->_panelHook.bottom_panel = (struct panel *) 0;
402285SN/A    SP->_panelHook.stdscr_pseudo_panel = (struct panel *) 0;
403285SN/A
404285SN/A    /*
405285SN/A     * If we've no magic cookie support, we suppress attributes that xmc would
406285SN/A     * affect, i.e., the attributes that affect the rendition of a space.
407285SN/A     */
408285SN/A    SP->_ok_attributes = termattrs();
409285SN/A    if (has_colors()) {
410285SN/A	SP->_ok_attributes |= A_COLOR;
411285SN/A    }
412285SN/A#if USE_XMC_SUPPORT
413285SN/A    /*
414285SN/A     * If we have no magic-cookie support compiled-in, or if it is suppressed
415285SN/A     * in the environment, reset the support-flag.
416285SN/A     */
417285SN/A    if (magic_cookie_glitch >= 0) {
418367SN/A	if (getenv("NCURSES_NO_MAGIC_COOKIE") != 0) {
419285SN/A	    support_cookies = FALSE;
420285SN/A	}
421285SN/A    }
422285SN/A#endif
423285SN/A
424285SN/A    if (!support_cookies && magic_cookie_glitch >= 0) {
425285SN/A	T(("will disable attributes to work w/o magic cookies"));
426285SN/A    }
427285SN/A
428285SN/A    if (magic_cookie_glitch > 0) {	/* tvi, wyse */
429367SN/A
430285SN/A	SP->_xmc_triggers = SP->_ok_attributes & (
431285SN/A						     A_STANDOUT |
432285SN/A						     A_UNDERLINE |
433285SN/A						     A_REVERSE |
434285SN/A						     A_BLINK |
435285SN/A						     A_DIM |
436285SN/A						     A_BOLD |
437285SN/A						     A_INVIS |
438285SN/A						     A_PROTECT
439285SN/A	    );
440285SN/A#if 0
441285SN/A	/*
442285SN/A	 * We "should" treat colors as an attribute.  The wyse350 (and its
443285SN/A	 * clones) appear to be the only ones that have both colors and magic
444285SN/A	 * cookies.
445285SN/A	 */
446285SN/A	if (has_colors()) {
447285SN/A	    SP->_xmc_triggers |= A_COLOR;
448285SN/A	}
449285SN/A#endif
450285SN/A	SP->_xmc_suppress = SP->_xmc_triggers & (chtype) ~(A_BOLD);
451285SN/A
452285SN/A	T(("magic cookie attributes %s", _traceattr(SP->_xmc_suppress)));
453285SN/A	/*
454285SN/A	 * Supporting line-drawing may be possible.  But make the regular
455285SN/A	 * video attributes work first.
456285SN/A	 */
457285SN/A	acs_chars = ABSENT_STRING;
458285SN/A	ena_acs = ABSENT_STRING;
459285SN/A	enter_alt_charset_mode = ABSENT_STRING;
460285SN/A	exit_alt_charset_mode = ABSENT_STRING;
461367SN/A#if USE_XMC_SUPPORT
462285SN/A	/*
463285SN/A	 * To keep the cookie support simple, suppress all of the optimization
464285SN/A	 * hooks except for clear_screen and the cursor addressing.
465285SN/A	 */
466285SN/A	if (support_cookies) {
467285SN/A	    clr_eol = ABSENT_STRING;
468285SN/A	    clr_eos = ABSENT_STRING;
469285SN/A	    set_attributes = ABSENT_STRING;
470285SN/A	}
471285SN/A#endif
472285SN/A    } else if (magic_cookie_glitch == 0) {	/* hpterm */
473285SN/A    }
474285SN/A
475285SN/A    /*
476285SN/A     * If magic cookies are not supported, cancel the strings that set
477285SN/A     * video attributes.
478285SN/A     */
479285SN/A    if (!support_cookies && magic_cookie_glitch >= 0) {
480285SN/A	magic_cookie_glitch = ABSENT_NUMERIC;
481285SN/A	set_attributes = ABSENT_STRING;
482285SN/A	enter_blink_mode = ABSENT_STRING;
483285SN/A	enter_bold_mode = ABSENT_STRING;
484285SN/A	enter_dim_mode = ABSENT_STRING;
485285SN/A	enter_reverse_mode = ABSENT_STRING;
486285SN/A	enter_standout_mode = ABSENT_STRING;
487285SN/A	enter_underline_mode = ABSENT_STRING;
488285SN/A    }
489285SN/A
490285SN/A    /* initialize normal acs before wide, since we use mapping in the latter */
491285SN/A#if !USE_WIDEC_SUPPORT
492285SN/A    if (_nc_unicode_locale() && _nc_locale_breaks_acs()) {
493285SN/A	acs_chars = NULL;
494285SN/A	ena_acs = NULL;
495285SN/A	enter_alt_charset_mode = NULL;
496285SN/A	exit_alt_charset_mode = NULL;
497285SN/A	set_attributes = NULL;
498285SN/A    }
499285SN/A#endif
500285SN/A    _nc_init_acs();
501285SN/A#if USE_WIDEC_SUPPORT
502285SN/A    _nc_init_wacs();
503285SN/A
504285SN/A    SP->_screen_acs_fix = (_nc_unicode_locale() && _nc_locale_breaks_acs());
505285SN/A#endif
506285SN/A    env = _nc_get_locale();
507285SN/A    SP->_legacy_coding = ((env == 0)
508285SN/A			  || !strcmp(env, "C")
509285SN/A			  || !strcmp(env, "POSIX"));
510285SN/A    T(("legacy-coding %d", SP->_legacy_coding));
511285SN/A
512285SN/A    _nc_idcok = TRUE;
513285SN/A    _nc_idlok = FALSE;
514285SN/A
515285SN/A    _nc_windows = 0;		/* no windows yet */
516285SN/A
517285SN/A    SP->oldhash = 0;
518285SN/A    SP->newhash = 0;
519285SN/A
520285SN/A    T(("creating newscr"));
521285SN/A    if ((SP->_newscr = newwin(slines, scolumns, 0, 0)) == 0)
522285SN/A	returnCode(ERR);
523285SN/A
524285SN/A    T(("creating curscr"));
525285SN/A    if ((SP->_curscr = newwin(slines, scolumns, 0, 0)) == 0)
526285SN/A	returnCode(ERR);
527367SN/A
528285SN/A#if !USE_REENTRANT
529285SN/A    newscr = SP->_newscr;
530285SN/A    curscr = SP->_curscr;
531285SN/A#endif
532285SN/A#if USE_SIZECHANGE
533285SN/A    SP->_resize = resizeterm;
534285SN/A#endif
535285SN/A
536285SN/A    newscr->_clear = TRUE;
537285SN/A    curscr->_clear = FALSE;
538285SN/A
539285SN/A    def_shell_mode();
540285SN/A    def_prog_mode();
541285SN/A
542285SN/A    for (rop = ripoff_stack;
543285SN/A	 rop != ripoff_sp && (rop - ripoff_stack) < N_RIPS;
544285SN/A	 rop++) {
545285SN/A
546285SN/A	/* If we must simulate soft labels, grab off the line to be used.
547285SN/A	   We assume that we must simulate, if it is none of the standard
548285SN/A	   formats (4-4 or 3-2-3) for which there may be some hardware
549285SN/A	   support. */
550285SN/A	if (rop->hook == _nc_slk_initialize)
551285SN/A	    if (!(num_labels <= 0 || !SLK_STDFMT(slk_format)))
552285SN/A		continue;
553285SN/A	if (rop->hook) {
554285SN/A	    int count;
555285SN/A	    WINDOW *w;
556285SN/A
557285SN/A	    count = (rop->line < 0) ? -rop->line : rop->line;
558285SN/A	    T(("ripping off %i lines at %s", count,
559285SN/A	       ((rop->line < 0)
560285SN/A		? "bottom"
561285SN/A		: "top")));
562285SN/A
563285SN/A	    w = newwin(count, scolumns,
564285SN/A		       ((rop->line < 0)
565367SN/A			? SP->_lines_avail - count
566285SN/A			: 0),
567285SN/A		       0);
568285SN/A	    if (w) {
569285SN/A		rop->win = w;
570285SN/A		rop->hook(w, scolumns);
571285SN/A	    } else {
572285SN/A		returnCode(ERR);
573285SN/A	    }
574285SN/A	    if (rop->line < 0)
575285SN/A		bottom_stolen += count;
576285SN/A	    else
577285SN/A		SP->_topstolen += count;
578285SN/A	    SP->_lines_avail -= count;
579285SN/A	}
580285SN/A    }
581285SN/A    /* reset the stack */
582285SN/A    ripoff_sp = ripoff_stack;
583367SN/A
584285SN/A    T(("creating stdscr"));
585285SN/A    assert((SP->_lines_avail + SP->_topstolen + bottom_stolen) == slines);
586285SN/A    if ((SP->_stdscr = newwin(SP->_lines_avail, scolumns, 0, 0)) == 0)
587285SN/A	returnCode(ERR);
588285SN/A
589367SN/A    SET_LINES(SP->_lines_avail);
590285SN/A#if !USE_REENTRANT
591367SN/A    stdscr = SP->_stdscr;
592285SN/A#endif
593285SN/A
594285SN/A    returnCode(OK);
595285SN/A}
596285SN/A
597285SN/A/*
598367SN/A * The internal implementation interprets line as the number of lines to rip
599367SN/A * off from the top or bottom.
600367SN/A */
601367SN/ANCURSES_EXPORT(int)
602367SN/A_nc_ripoffline(int line, int (*init) (WINDOW *, int))
603367SN/A{
604367SN/A    T((T_CALLED("_nc_ripoffline(%d, %p)"), line, init));
605367SN/A
606367SN/A    if (line != 0) {
607367SN/A
608367SN/A	if (ripoff_sp == 0)
609367SN/A	    ripoff_sp = ripoff_stack;
610367SN/A	if (ripoff_sp >= ripoff_stack + N_RIPS)
611367SN/A	    returnCode(ERR);
612367SN/A
613367SN/A	ripoff_sp->line = line;
614367SN/A	ripoff_sp->hook = init;
615367SN/A	ripoff_sp++;
616367SN/A    }
617367SN/A
618367SN/A    returnCode(OK);
619285SN/A}
620285SN/A
621367SN/ANCURSES_EXPORT(int)
622285SN/Aripoffline(int line, int (*init) (WINDOW *, int))
623285SN/A{
624367SN/A    START_TRACE();
625285SN/A    T((T_CALLED("ripoffline(%d,%p)"), line, init));
626367SN/A
627285SN/A    if (line == 0)
628285SN/A	returnCode(OK);
629285SN/A
630285SN/A    returnCode(_nc_ripoffline((line < 0) ? -1 : 1, init));
631285SN/A}
632285SN/A