xref: /openbsd-src/lib/libcurses/base/lib_newterm.c (revision 1fe33145a0a9b1310a0413297a8deb871918b27f)
1 /*	$OpenBSD: lib_newterm.c,v 1.8 2000/06/19 03:53:43 millert Exp $	*/
2 
3 /****************************************************************************
4  * Copyright (c) 1998,1999,2000 Free Software Foundation, Inc.              *
5  *                                                                          *
6  * Permission is hereby granted, free of charge, to any person obtaining a  *
7  * copy of this software and associated documentation files (the            *
8  * "Software"), to deal in the Software without restriction, including      *
9  * without limitation the rights to use, copy, modify, merge, publish,      *
10  * distribute, distribute with modifications, sublicense, and/or sell       *
11  * copies of the Software, and to permit persons to whom the Software is    *
12  * furnished to do so, subject to the following conditions:                 *
13  *                                                                          *
14  * The above copyright notice and this permission notice shall be included  *
15  * in all copies or substantial portions of the Software.                   *
16  *                                                                          *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
18  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
20  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
21  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
22  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
23  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
24  *                                                                          *
25  * Except as contained in this notice, the name(s) of the above copyright   *
26  * holders shall not be used in advertising or otherwise to promote the     *
27  * sale, use or other dealings in this Software without prior written       *
28  * authorization.                                                           *
29  ****************************************************************************/
30 
31 /****************************************************************************
32  *  Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
33  *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
34  ****************************************************************************/
35 
36 /*
37 **	lib_newterm.c
38 **
39 **	The newterm() function.
40 **
41 */
42 
43 #include <curses.priv.h>
44 
45 #if defined(SVR4_TERMIO) && !defined(_POSIX_SOURCE)
46 #define _POSIX_SOURCE
47 #endif
48 
49 #include <term.h>		/* clear_screen, cup & friends, cur_term */
50 #include <tic.h>
51 
52 MODULE_ID("$From: lib_newterm.c,v 1.45 2000/05/20 23:45:57 tom Exp $");
53 
54 #ifndef ONLCR			/* Allows compilation under the QNX 4.2 OS */
55 #define ONLCR 0
56 #endif
57 
58 /*
59  * SVr4/XSI Curses specify that hardware echo is turned off in initscr, and not
60  * restored during the curses session.  The library simulates echo in software.
61  * (The behavior is unspecified if the application enables hardware echo).
62  *
63  * The newterm function also initializes terminal settings, and since initscr
64  * is supposed to behave as if it calls newterm, we do it here.
65  */
66 static inline int
67 _nc_initscr(void)
68 {
69     /* for extended XPG4 conformance requires cbreak() at this point */
70     /* (SVr4 curses does this anyway) */
71     cbreak();
72 
73 #ifdef TERMIOS
74     cur_term->Nttyb.c_lflag &= ~(ECHO | ECHONL);
75     cur_term->Nttyb.c_iflag &= ~(ICRNL | INLCR | IGNCR);
76     cur_term->Nttyb.c_oflag &= ~(ONLCR);
77 #else
78     cur_term->Nttyb.sg_flags &= ~(ECHO | CRMOD);
79 #endif
80     return _nc_set_tty_mode(&cur_term->Nttyb);
81 }
82 
83 /*
84  * filter() has to be called before either initscr() or newterm(), so there is
85  * apparently no way to make this flag apply to some terminals and not others,
86  * aside from possibly delaying a filter() call until some terminals have been
87  * initialized.
88  */
89 static int filter_mode = FALSE;
90 
91 void
92 filter(void)
93 {
94     filter_mode = TRUE;
95 }
96 
97 SCREEN *
98 newterm(NCURSES_CONST char *name, FILE * ofp, FILE * ifp)
99 {
100     int errret;
101     int slk_format = _nc_slk_format;
102     SCREEN *current;
103 #ifdef TRACE
104     int t = _nc_getenv_num("NCURSES_TRACE");
105 
106     if (t >= 0)
107 	trace(t);
108 #endif
109 
110     T((T_CALLED("newterm(\"%s\",%p,%p)"), name, ofp, ifp));
111 
112     /* this loads the capability entry, then sets LINES and COLS */
113     if (setupterm(name, fileno(ofp), &errret) == ERR)
114 	return 0;
115 
116     /* implement filter mode */
117     if (filter_mode) {
118 	LINES = 1;
119 
120 	if (VALID_NUMERIC(init_tabs))
121 	    TABSIZE = init_tabs;
122 	else
123 	    TABSIZE = 8;
124 
125 	T(("TABSIZE = %d", TABSIZE));
126 
127 	clear_screen = 0;
128 	cursor_down = parm_down_cursor = 0;
129 	cursor_address = 0;
130 	cursor_up = parm_up_cursor = 0;
131 	row_address = 0;
132 
133 	cursor_home = carriage_return;
134     }
135 
136     /* If we must simulate soft labels, grab off the line to be used.
137        We assume that we must simulate, if it is none of the standard
138        formats (4-4  or 3-2-3) for which there may be some hardware
139        support. */
140     if (num_labels <= 0 || !SLK_STDFMT(slk_format))
141 	if (slk_format) {
142 	    if (ERR == _nc_ripoffline(-SLK_LINES(slk_format),
143 		    _nc_slk_initialize))
144 		return 0;
145 	}
146     /* this actually allocates the screen structure, and saves the
147      * original terminal settings.
148      */
149     current = SP;
150     _nc_set_screen(0);
151     if (_nc_setupscreen(LINES, COLS, ofp) == ERR) {
152 	_nc_set_screen(current);
153 	return 0;
154     }
155 
156     /* if the terminal type has real soft labels, set those up */
157     if (slk_format && num_labels > 0 && SLK_STDFMT(slk_format))
158 	_nc_slk_initialize(stdscr, COLS);
159 
160     SP->_ifd = fileno(ifp);
161     SP->_checkfd = fileno(ifp);
162     typeahead(fileno(ifp));
163 #ifdef TERMIOS
164     SP->_use_meta = ((cur_term->Ottyb.c_cflag & CSIZE) == CS8 &&
165 	!(cur_term->Ottyb.c_iflag & ISTRIP));
166 #else
167     SP->_use_meta = FALSE;
168 #endif
169     SP->_endwin = FALSE;
170 
171     /* Check whether we can optimize scrolling under dumb terminals in case
172      * we do not have any of these capabilities, scrolling optimization
173      * will be useless.
174      */
175     SP->_scrolling = ((scroll_forward && scroll_reverse) ||
176 	((parm_rindex || parm_insert_line || insert_line) &&
177 	    (parm_index || parm_delete_line || delete_line)));
178 
179     baudrate();			/* sets a field in the SP structure */
180 
181     SP->_keytry = 0;
182 
183     /*
184      * Check for mismatched graphic-rendition capabilities.  Most SVr4
185      * terminfo trees contain entries that have rmul or rmso equated to
186      * sgr0 (Solaris curses copes with those entries).  We do this only for
187      * curses, since many termcap applications assume that smso/rmso and
188      * smul/rmul are paired, and will not function properly if we remove
189      * rmso or rmul.  Curses applications shouldn't be looking at this
190      * detail.
191      */
192 #define SGR0_TEST(mode) (mode != 0) && (exit_attribute_mode == 0 || strcmp(mode, exit_attribute_mode))
193     SP->_use_rmso = SGR0_TEST(exit_standout_mode);
194     SP->_use_rmul = SGR0_TEST(exit_underline_mode);
195 
196 #ifdef USE_WIDEC_SUPPORT
197     /*
198      * XFree86 xterm can be configured to support UTF-8 based on environment
199      * variable settings.
200      */
201     {
202 	char *s;
203 	if (((s = getenv("LC_ALL")) != 0
204 		|| (s = getenv("LC_CTYPE")) != 0
205 		|| (s = getenv("LANG")) != 0)
206 	    && strstr(s, "UTF-8") != 0) {
207 	    SP->_outch = _nc_utf8_outch;
208 	}
209     }
210 #endif
211 
212     /* compute movement costs so we can do better move optimization */
213     _nc_mvcur_init();
214 
215     /* initialize terminal to a sane state */
216     _nc_screen_init();
217 
218     /* Initialize the terminal line settings. */
219     _nc_initscr();
220 
221     _nc_signal_handler(TRUE);
222 
223     T((T_RETURN("%p"), SP));
224     return (SP);
225 }
226