1 /****************************************************************************
2  * Copyright (c) 1998-2004,2005 Free Software Foundation, Inc.              *
3  *                                                                          *
4  * Permission is hereby granted, free of charge, to any person obtaining a  *
5  * copy of this software and associated documentation files (the            *
6  * "Software"), to deal in the Software without restriction, including      *
7  * without limitation the rights to use, copy, modify, merge, publish,      *
8  * distribute, distribute with modifications, sublicense, and/or sell       *
9  * copies of the Software, and to permit persons to whom the Software is    *
10  * furnished to do so, subject to the following conditions:                 *
11  *                                                                          *
12  * The above copyright notice and this permission notice shall be included  *
13  * in all copies or substantial portions of the Software.                   *
14  *                                                                          *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
16  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
18  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
19  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
20  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
21  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
22  *                                                                          *
23  * Except as contained in this notice, the name(s) of the above copyright   *
24  * holders shall not be used in advertising or otherwise to promote the     *
25  * sale, use or other dealings in this Software without prior written       *
26  * authorization.                                                           *
27  ****************************************************************************/
28 
29 /****************************************************************************
30  *  Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
31  *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
32  *     and: Thomas E. Dickey                        1996-on                 *
33  ****************************************************************************/
34 
35 /*
36 **	lib_set_term.c
37 **
38 **	The routine set_term().
39 **
40 */
41 
42 #include <curses.priv.h>
43 
44 #include <term.h>		/* cur_term */
45 #include <tic.h>
46 
47 MODULE_ID("$Id: lib_set_term.c,v 1.85 2005/01/22 17:36:01 tom Exp $")
48 #ifdef __MirBSD__
49 __RCSID("$MirOS: src/lib/libncurses/src/ncurses/base/lib_set_term.c,v 1.5 2009/09/06 13:46:35 tg Exp $");
50 #endif
51 
52 NCURSES_EXPORT(SCREEN *)
set_term(SCREEN * screenp)53 set_term(SCREEN *screenp)
54 {
55     SCREEN *oldSP;
56 
57     T((T_CALLED("set_term(%p)"), screenp));
58 
59     oldSP = SP;
60     _nc_set_screen(screenp);
61 
62     set_curterm(SP->_term);
63     curscr = SP->_curscr;
64     newscr = SP->_newscr;
65     stdscr = SP->_stdscr;
66     COLORS = SP->_color_count;
67     COLOR_PAIRS = SP->_pair_count;
68 
69     T((T_RETURN("%p"), oldSP));
70     return (oldSP);
71 }
72 
73 static void
_nc_free_keytry(struct tries * kt)74 _nc_free_keytry(struct tries *kt)
75 {
76     if (kt != 0) {
77 	_nc_free_keytry(kt->child);
78 	_nc_free_keytry(kt->sibling);
79 	free(kt);
80     }
81 }
82 
83 /*
84  * Free the storage associated with the given SCREEN sp.
85  */
86 NCURSES_EXPORT(void)
delscreen(SCREEN * sp)87 delscreen(SCREEN *sp)
88 {
89     SCREEN **scan = &_nc_screen_chain;
90     int i;
91 
92     T((T_CALLED("delscreen(%p)"), sp));
93 
94     while (*scan) {
95 	if (*scan == sp) {
96 	    *scan = sp->_next_screen;
97 	    break;
98 	}
99 	scan = &(*scan)->_next_screen;
100     }
101 
102     (void) _nc_freewin(sp->_curscr);
103     (void) _nc_freewin(sp->_newscr);
104     (void) _nc_freewin(sp->_stdscr);
105 
106     if (sp->_slk != 0) {
107 	if (sp->_slk->ent != 0) {
108 	    for (i = 0; i < sp->_slk->labcnt; ++i) {
109 		FreeIfNeeded(sp->_slk->ent[i].ent_text);
110 		FreeIfNeeded(sp->_slk->ent[i].form_text);
111 	    }
112 	    free(sp->_slk->ent);
113 	}
114 	free(sp->_slk);
115 	sp->_slk = 0;
116     }
117 
118     _nc_free_keytry(sp->_keytry);
119     sp->_keytry = 0;
120 
121     _nc_free_keytry(sp->_key_ok);
122     sp->_key_ok = 0;
123 
124     FreeIfNeeded(sp->_current_attr);
125 
126     FreeIfNeeded(sp->_color_table);
127     FreeIfNeeded(sp->_color_pairs);
128 
129     FreeIfNeeded(sp->oldhash);
130     FreeIfNeeded(sp->newhash);
131     FreeIfNeeded(sp->hashtab);
132 
133     del_curterm(sp->_term);
134 
135     /*
136      * If the associated output stream has been closed, we can discard the
137      * set-buffer.  Limit the error check to EBADF, since fflush may fail
138      * for other reasons than trying to operate upon a closed stream.
139      */
140     if (sp->_ofp != 0
141 	&& sp->_setbuf != 0
142 	&& fflush(sp->_ofp) != 0
143 	&& errno == EBADF) {
144 	free(sp->_setbuf);
145     }
146 
147     free(sp);
148 
149     /*
150      * If this was the current screen, reset everything that the
151      * application might try to use (except cur_term, which may have
152      * multiple references in different screens).
153      */
154     if (sp == SP) {
155 	curscr = 0;
156 	newscr = 0;
157 	stdscr = 0;
158 	COLORS = 0;
159 	COLOR_PAIRS = 0;
160 	_nc_set_screen(0);
161     }
162     returnVoid;
163 }
164 
165 static ripoff_t rippedoff[5];
166 static ripoff_t *rsp = rippedoff;
167 #define N_RIPS SIZEOF(SP->_rippedoff)
168 
169 static bool
no_mouse_event(SCREEN * sp GCC_UNUSED)170 no_mouse_event(SCREEN *sp GCC_UNUSED)
171 {
172     return FALSE;
173 }
174 
175 static bool
no_mouse_inline(SCREEN * sp GCC_UNUSED)176 no_mouse_inline(SCREEN *sp GCC_UNUSED)
177 {
178     return FALSE;
179 }
180 
181 static bool
no_mouse_parse(int code GCC_UNUSED)182 no_mouse_parse(int code GCC_UNUSED)
183 {
184     return TRUE;
185 }
186 
187 static void
no_mouse_resume(SCREEN * sp GCC_UNUSED)188 no_mouse_resume(SCREEN *sp GCC_UNUSED)
189 {
190 }
191 
192 static void
no_mouse_wrap(SCREEN * sp GCC_UNUSED)193 no_mouse_wrap(SCREEN *sp GCC_UNUSED)
194 {
195 }
196 
197 #if NCURSES_EXT_FUNCS && USE_COLORFGBG
198 static char *
extract_fgbg(char * src,int * result)199 extract_fgbg(char *src, int *result)
200 {
201     char *dst = 0;
202     long value = strtol(src, &dst, 0);
203 
204     if (dst == 0) {
205 	dst = src;
206     } else if (value >= 0) {
207 	*result = value;
208     }
209     while (*dst != 0 && *dst != ';')
210 	dst++;
211     if (*dst == ';')
212 	dst++;
213     return dst;
214 }
215 #endif
216 
217 NCURSES_EXPORT(int)
_nc_setupscreen(short slines,short const scolumns,FILE * output)218 _nc_setupscreen(short slines, short const scolumns, FILE *output)
219 /* OS-independent screen initializations */
220 {
221     int bottom_stolen = 0;
222     int i;
223 
224     T((T_CALLED("_nc_setupscreen(%d, %d, %p)"), slines, scolumns, output));
225     assert(SP == 0);		/* has been reset in newterm() ! */
226     if (!_nc_alloc_screen())
227 	returnCode(ERR);
228 
229     T(("created SP %p", SP));
230     SP->_next_screen = _nc_screen_chain;
231     _nc_screen_chain = SP;
232 
233     if ((SP->_current_attr = typeCalloc(NCURSES_CH_T, 1)) == 0)
234 	returnCode(ERR);
235 
236 #ifdef __DJGPP__
237     T(("setting output mode to binary"));
238     fflush(output);
239     setmode(output, O_BINARY);
240 #endif
241     _nc_set_buffer(output, TRUE);
242     SP->_term = cur_term;
243     SP->_lines = slines;
244     SP->_lines_avail = slines;
245     SP->_columns = scolumns;
246     SP->_cursrow = -1;
247     SP->_curscol = -1;
248     SP->_nl = TRUE;
249     SP->_raw = FALSE;
250     SP->_cbreak = 0;
251     SP->_echo = TRUE;
252     SP->_fifohead = -1;
253     SP->_endwin = TRUE;
254     SP->_ofp = output;
255     SP->_cursor = -1;		/* cannot know real cursor shape */
256 
257 #if NCURSES_NO_PADDING
258     SP->_no_padding = getenv("NCURSES_NO_PADDING") != 0;
259     TR(TRACE_CHARPUT | TRACE_MOVE, ("padding will%s be used",
260 				    SP->_no_padding ? " not" : ""));
261 #endif
262 
263 #if NCURSES_EXT_FUNCS
264     SP->_default_color = FALSE;
265     SP->_has_sgr_39_49 = FALSE;
266 
267     /*
268      * Set our assumption of the terminal's default foreground and background
269      * colors.  The curs_color man-page states that we can assume that the
270      * background is black.  The origin of this assumption appears to be
271      * terminals that displayed colored text, but no colored backgrounds, e.g.,
272      * the first colored terminals around 1980.  More recent ones with better
273      * technology can display not only colored backgrounds, but all
274      * combinations.  So a terminal might be something other than "white" on
275      * black (green/black looks monochrome too), but black on white or even
276      * on ivory.
277      *
278      * White-on-black is the simplest thing to use for monochrome.  Almost
279      * all applications that use color paint both text and background, so
280      * the distinction is moot.  But a few do not - which is why we leave this
281      * configurable (a better solution is to use assume_default_colors() for
282      * the rare applications that do require that sort of appearance, since
283      * is appears that more users expect to be able to make a white-on-black
284      * or black-on-white display under control of the application than not).
285      */
286 #ifdef USE_ASSUMED_COLOR
287     SP->_default_fg = COLOR_WHITE;
288     SP->_default_bg = COLOR_BLACK;
289 #else
290     SP->_default_fg = C_MASK;
291     SP->_default_bg = C_MASK;
292 #endif
293 
294     /*
295      * Allow those assumed/default color assumptions to be overridden at
296      * runtime:
297      */
298     if (getenv("NCURSES_ASSUMED_COLORS") != 0) {
299 	char *p = getenv("NCURSES_ASSUMED_COLORS");
300 	int fg, bg;
301 	char sep1, sep2;
302 	int count = sscanf(p, "%d%c%d%c", &fg, &sep1, &bg, &sep2);
303 	if (count >= 1) {
304 	    SP->_default_fg = (fg >= 0 && fg < max_colors) ? fg : C_MASK;
305 	    if (count >= 3) {
306 		SP->_default_bg = (bg >= 0 && bg < max_colors) ? bg : C_MASK;
307 	    }
308 	    TR(TRACE_CHARPUT | TRACE_MOVE,
309 	       ("from environment assumed fg=%d, bg=%d",
310 		SP->_default_fg,
311 		SP->_default_bg));
312 	}
313     }
314 #if USE_COLORFGBG
315     /*
316      * If rxvt's $COLORFGBG variable is set, use it to specify the assumed
317      * default colors.  Note that rxvt (mis)uses bold colors, equating a bold
318      * color to that value plus 8.  We'll only use the non-bold color for now -
319      * decide later if it is worth having default attributes as well.
320      */
321     if (getenv("COLORFGBG") != 0) {
322 	char *p = getenv("COLORFGBG");
323 	TR(TRACE_CHARPUT | TRACE_MOVE, ("decoding COLORFGBG %s", p));
324 	p = extract_fgbg(p, &(SP->_default_fg));
325 	p = extract_fgbg(p, &(SP->_default_bg));
326 	if (*p)			/* assume rxvt was compiled with xpm support */
327 	    p = extract_fgbg(p, &(SP->_default_bg));
328 	TR(TRACE_CHARPUT | TRACE_MOVE, ("decoded fg=%d, bg=%d",
329 					SP->_default_fg, SP->_default_bg));
330 	if (SP->_default_fg >= max_colors) {
331 	    if (set_a_foreground != ABSENT_STRING
332 		&& !strcmp(set_a_foreground, "\033[3%p1%dm")) {
333 		static char _nc_set_a_foreground[] = "\033[3%?%p1%{8}%>%t9%e%p1%d%;m";
334 		set_a_foreground = _nc_set_a_foreground;
335 	    } else {
336 		SP->_default_fg %= max_colors;
337 	    }
338 	}
339 	if (SP->_default_bg >= max_colors) {
340 	    if (set_a_background != ABSENT_STRING
341 		&& !strcmp(set_a_background, "\033[4%p1%dm")) {
342 		static char _nc_set_a_background[] = "\033[4%?%p1%{8}%>%t9%e%p1%d%;m";
343 		set_a_background = _nc_set_a_background;
344 	    } else {
345 		SP->_default_bg %= max_colors;
346 	    }
347 	}
348     }
349 #endif
350 #endif /* NCURSES_EXT_FUNCS */
351 
352     SP->_maxclick = DEFAULT_MAXCLICK;
353     SP->_mouse_event = no_mouse_event;
354     SP->_mouse_inline = no_mouse_inline;
355     SP->_mouse_parse = no_mouse_parse;
356     SP->_mouse_resume = no_mouse_resume;
357     SP->_mouse_wrap = no_mouse_wrap;
358     SP->_mouse_fd = -1;
359 
360     /* initialize the panel hooks */
361     SP->_panelHook.top_panel = (struct panel *) 0;
362     SP->_panelHook.bottom_panel = (struct panel *) 0;
363     SP->_panelHook.stdscr_pseudo_panel = (struct panel *) 0;
364 
365     /*
366      * If we've no magic cookie support, we suppress attributes that xmc
367      * would affect, i.e., the attributes that affect the rendition of a
368      * space.  Note that this impacts the alternate character set mapping
369      * as well.
370      */
371     if (magic_cookie_glitch > 0) {
372 
373 	SP->_xmc_triggers = termattrs() & (
374 					      A_ALTCHARSET |
375 					      A_BLINK |
376 					      A_BOLD |
377 					      A_REVERSE |
378 					      A_STANDOUT |
379 					      A_UNDERLINE
380 	    );
381 	SP->_xmc_suppress = SP->_xmc_triggers & (chtype) ~(A_BOLD);
382 
383 	T(("magic cookie attributes %s", _traceattr(SP->_xmc_suppress)));
384 #if USE_XMC_SUPPORT
385 	/*
386 	 * To keep this simple, suppress all of the optimization hooks
387 	 * except for clear_screen and the cursor addressing.
388 	 */
389 	clr_eol = 0;
390 	clr_eos = 0;
391 	set_attributes = 0;
392 #else
393 	magic_cookie_glitch = ABSENT_NUMERIC;
394 	acs_chars = 0;
395 #endif
396     }
397 
398     /* initialize normal acs before wide, since we use mapping in the latter */
399     _nc_init_acs();
400 #if USE_WIDEC_SUPPORT
401     _nc_init_wacs();
402 
403     SP->_screen_acs_fix = (_nc_unicode_locale() && _nc_locale_breaks_acs());
404     {
405 	char *env = _nc_get_locale();
406 	SP->_legacy_coding = ((env == 0)
407 			      || (_nc_overridden_locale() && !_nc_unicode_locale())
408 			      || !strcmp(env, "C")
409 			      || !strcmp(env, "POSIX"));
410     }
411 #endif
412 
413     _nc_idcok = TRUE;
414     _nc_idlok = FALSE;
415 
416     _nc_windows = 0;		/* no windows yet */
417 
418     SP->oldhash = 0;
419     SP->newhash = 0;
420 
421     T(("creating newscr"));
422     if ((newscr = newwin(slines, scolumns, 0, 0)) == 0)
423 	returnCode(ERR);
424 
425     T(("creating curscr"));
426     if ((curscr = newwin(slines, scolumns, 0, 0)) == 0)
427 	returnCode(ERR);
428 
429     SP->_newscr = newscr;
430     SP->_curscr = curscr;
431 #if USE_SIZECHANGE
432     SP->_resize = resizeterm;
433 #endif
434 
435     newscr->_clear = TRUE;
436     curscr->_clear = FALSE;
437 
438     def_shell_mode();
439     def_prog_mode();
440 
441     for (i = 0, rsp = rippedoff; rsp->line && (i < (int) N_RIPS); rsp++, i++) {
442 	T(("ripping off line %d at %s", i, rsp->line < 0 ? "bottom" : "top"));
443 	SP->_rippedoff[i] = rippedoff[i];
444 	if (rsp->hook) {
445 	    int count = (rsp->line < 0) ? -rsp->line : rsp->line;
446 
447 	    SP->_rippedoff[i].w = newwin(count,
448 					 scolumns,
449 					 ((rsp->line < 0)
450 					  ? SP->_lines_avail - count
451 					  : 0),
452 					 0);
453 	    if (SP->_rippedoff[i].w != 0)
454 		SP->_rippedoff[i].hook(SP->_rippedoff[i].w, scolumns);
455 	    else
456 		returnCode(ERR);
457 	    if (rsp->line < 0)
458 		bottom_stolen += count;
459 	    else
460 		SP->_topstolen += count;
461 	    SP->_lines_avail -= count;
462 	}
463 	rsp->line = 0;
464     }
465     SP->_rip_count = i;
466     /* reset the stack */
467     rsp = rippedoff;
468 
469     T(("creating stdscr"));
470     assert((SP->_lines_avail + SP->_topstolen + bottom_stolen) == slines);
471     if ((stdscr = newwin(LINES = SP->_lines_avail, scolumns, 0, 0)) == 0)
472 	returnCode(ERR);
473     SP->_stdscr = stdscr;
474 
475     returnCode(OK);
476 }
477 
478 /* The internal implementation interprets line as the number of
479    lines to rip off from the top or bottom.
480    */
481 NCURSES_EXPORT(int)
_nc_ripoffline(int line,int (* init)(WINDOW *,int))482 _nc_ripoffline(int line, int (*init) (WINDOW *, int))
483 {
484     T((T_CALLED("_nc_ripoffline(%d, %p)"), line, init));
485 
486     if (line != 0) {
487 
488 	if (rsp >= rippedoff + N_RIPS)
489 	    returnCode(ERR);
490 
491 	rsp->line = line;
492 	rsp->hook = init;
493 	rsp->w = 0;
494 	rsp++;
495     }
496 
497     returnCode(OK);
498 }
499 
500 NCURSES_EXPORT(int)
ripoffline(int line,int (* init)(WINDOW *,int))501 ripoffline(int line, int (*init) (WINDOW *, int))
502 {
503     T((T_CALLED("ripoffline(%d,%p)"), line, init));
504 
505     if (line == 0)
506 	returnCode(OK);
507 
508     returnCode(_nc_ripoffline((line < 0) ? -1 : 1, init));
509 }
510