lib_newterm.c revision 66963
1/****************************************************************************
2 * Copyright (c) 1998,1999,2000 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 ****************************************************************************/
33
34/*
35**	lib_newterm.c
36**
37**	The newterm() function.
38**
39*/
40
41#include <curses.priv.h>
42
43#if SVR4_TERMIO && !defined(_POSIX_SOURCE)
44#define _POSIX_SOURCE
45#endif
46
47#include <term.h>		/* clear_screen, cup & friends, cur_term */
48#include <tic.h>
49
50MODULE_ID("$Id: lib_newterm.c,v 1.48 2000/09/02 18:11:42 tom Exp $")
51
52#ifndef ONLCR			/* Allows compilation under the QNX 4.2 OS */
53#define ONLCR 0
54#endif
55
56/*
57 * SVr4/XSI Curses specify that hardware echo is turned off in initscr, and not
58 * restored during the curses session.  The library simulates echo in software.
59 * (The behavior is unspecified if the application enables hardware echo).
60 *
61 * The newterm function also initializes terminal settings, and since initscr
62 * is supposed to behave as if it calls newterm, we do it here.
63 */
64static inline int
65_nc_initscr(void)
66{
67    /* for extended XPG4 conformance requires cbreak() at this point */
68    /* (SVr4 curses does this anyway) */
69    cbreak();
70
71#ifdef TERMIOS
72    cur_term->Nttyb.c_lflag &= ~(ECHO | ECHONL);
73    cur_term->Nttyb.c_iflag &= ~(ICRNL | INLCR | IGNCR);
74    cur_term->Nttyb.c_oflag &= ~(ONLCR);
75#else
76    cur_term->Nttyb.sg_flags &= ~(ECHO | CRMOD);
77#endif
78    return _nc_set_tty_mode(&cur_term->Nttyb);
79}
80
81/*
82 * filter() has to be called before either initscr() or newterm(), so there is
83 * apparently no way to make this flag apply to some terminals and not others,
84 * aside from possibly delaying a filter() call until some terminals have been
85 * initialized.
86 */
87static int filter_mode = FALSE;
88
89void
90filter(void)
91{
92    filter_mode = TRUE;
93}
94
95SCREEN *
96newterm(NCURSES_CONST char *name, FILE * ofp, FILE * ifp)
97{
98    int errret;
99    int slk_format = _nc_slk_format;
100    SCREEN *current;
101#ifdef TRACE
102    int t = _nc_getenv_num("NCURSES_TRACE");
103
104    if (t >= 0)
105	trace(t);
106#endif
107
108    T((T_CALLED("newterm(\"%s\",%p,%p)"), name, ofp, ifp));
109
110    /* this loads the capability entry, then sets LINES and COLS */
111    if (setupterm(name, fileno(ofp), &errret) == ERR)
112	return 0;
113
114    /* implement filter mode */
115    if (filter_mode) {
116	LINES = 1;
117
118	if (VALID_NUMERIC(init_tabs))
119	    TABSIZE = init_tabs;
120	else
121	    TABSIZE = 8;
122
123	T(("TABSIZE = %d", TABSIZE));
124
125	clear_screen = 0;
126	cursor_down = parm_down_cursor = 0;
127	cursor_address = 0;
128	cursor_up = parm_up_cursor = 0;
129	row_address = 0;
130
131	cursor_home = carriage_return;
132    }
133
134    /* If we must simulate soft labels, grab off the line to be used.
135       We assume that we must simulate, if it is none of the standard
136       formats (4-4  or 3-2-3) for which there may be some hardware
137       support. */
138    if (num_labels <= 0 || !SLK_STDFMT(slk_format))
139	if (slk_format) {
140	    if (ERR == _nc_ripoffline(-SLK_LINES(slk_format),
141		    _nc_slk_initialize))
142		return 0;
143	}
144    /* this actually allocates the screen structure, and saves the
145     * original terminal settings.
146     */
147    current = SP;
148    _nc_set_screen(0);
149    if (_nc_setupscreen(LINES, COLS, ofp) == ERR) {
150	_nc_set_screen(current);
151	return 0;
152    }
153
154    /* if the terminal type has real soft labels, set those up */
155    if (slk_format && num_labels > 0 && SLK_STDFMT(slk_format))
156	_nc_slk_initialize(stdscr, COLS);
157
158    SP->_ifd = fileno(ifp);
159    SP->_checkfd = fileno(ifp);
160    typeahead(fileno(ifp));
161#ifdef TERMIOS
162    SP->_use_meta = ((cur_term->Ottyb.c_cflag & CSIZE) == CS8 &&
163	!(cur_term->Ottyb.c_iflag & ISTRIP));
164#else
165    SP->_use_meta = FALSE;
166#endif
167    SP->_endwin = FALSE;
168
169    /* Check whether we can optimize scrolling under dumb terminals in case
170     * we do not have any of these capabilities, scrolling optimization
171     * will be useless.
172     */
173    SP->_scrolling = ((scroll_forward && scroll_reverse) ||
174	((parm_rindex || parm_insert_line || insert_line) &&
175	    (parm_index || parm_delete_line || delete_line)));
176
177    baudrate();			/* sets a field in the SP structure */
178
179    SP->_keytry = 0;
180
181    /*
182     * Check for mismatched graphic-rendition capabilities.  Most SVr4
183     * terminfo trees contain entries that have rmul or rmso equated to
184     * sgr0 (Solaris curses copes with those entries).  We do this only for
185     * curses, since many termcap applications assume that smso/rmso and
186     * smul/rmul are paired, and will not function properly if we remove
187     * rmso or rmul.  Curses applications shouldn't be looking at this
188     * detail.
189     */
190#define SGR0_TEST(mode) (mode != 0) && (exit_attribute_mode == 0 || strcmp(mode, exit_attribute_mode))
191    SP->_use_rmso = SGR0_TEST(exit_standout_mode);
192    SP->_use_rmul = SGR0_TEST(exit_underline_mode);
193
194#if USE_WIDEC_SUPPORT
195    /*
196     * XFree86 xterm can be configured to support UTF-8 based on environment
197     * variable settings.
198     */
199    {
200	char *s;
201	s = getenv("LC_ALL");
202	if (s == NULL || *s == '\0') {
203	    s = getenv("LC_CTYPE");
204	    if (s == NULL || *s == '\0') {
205		s = getenv("LANG");
206	    }
207	}
208	if (s != NULL && *s != '\0' && strstr(s, "UTF-8") != NULL) {
209	    SP->_outch = _nc_utf8_outch;
210	}
211    }
212#endif
213
214    /* compute movement costs so we can do better move optimization */
215    _nc_mvcur_init();
216
217    /* initialize terminal to a sane state */
218    _nc_screen_init();
219
220    /* Initialize the terminal line settings. */
221    _nc_initscr();
222
223    _nc_signal_handler(TRUE);
224
225    T((T_RETURN("%p"), SP));
226    return (SP);
227}
228