xref: /openbsd-src/lib/libcurses/base/lib_newterm.c (revision a28daedfc357b214be5c701aa8ba8adb29a7f1c2)
1 /*	$OpenBSD: lib_newterm.c,v 1.12 2001/01/22 18:01:42 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 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.50 2000/12/10 02:43:27 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 NCURSES_EXPORT(void)
92 filter(void)
93 {
94     filter_mode = TRUE;
95 }
96 
97 NCURSES_EXPORT(SCREEN *)
98 newterm
99 (NCURSES_CONST char *name, FILE * ofp, FILE * ifp)
100 {
101     int errret;
102     int slk_format = _nc_slk_format;
103     SCREEN *current;
104 #ifdef TRACE
105     int t = _nc_getenv_num("NCURSES_TRACE");
106 
107     if (t >= 0)
108 	trace(t);
109 #endif
110 
111     T((T_CALLED("newterm(\"%s\",%p,%p)"), name, ofp, ifp));
112 
113     /* this loads the capability entry, then sets LINES and COLS */
114     if (setupterm(name, fileno(ofp), &errret) == ERR)
115 	return 0;
116 
117     /* implement filter mode */
118     if (filter_mode) {
119 	LINES = 1;
120 
121 	if (VALID_NUMERIC(init_tabs))
122 	    TABSIZE = init_tabs;
123 	else
124 	    TABSIZE = 8;
125 
126 	T(("TABSIZE = %d", TABSIZE));
127 
128 	clear_screen = 0;
129 	cursor_down = parm_down_cursor = 0;
130 	cursor_address = 0;
131 	cursor_up = parm_up_cursor = 0;
132 	row_address = 0;
133 
134 	cursor_home = carriage_return;
135     }
136 
137     /* If we must simulate soft labels, grab off the line to be used.
138        We assume that we must simulate, if it is none of the standard
139        formats (4-4  or 3-2-3) for which there may be some hardware
140        support. */
141     if (num_labels <= 0 || !SLK_STDFMT(slk_format))
142 	if (slk_format) {
143 	    if (ERR == _nc_ripoffline(-SLK_LINES(slk_format),
144 				      _nc_slk_initialize))
145 		return 0;
146 	}
147     /* this actually allocates the screen structure, and saves the
148      * original terminal settings.
149      */
150     current = SP;
151     _nc_set_screen(0);
152     if (_nc_setupscreen(LINES, COLS, ofp) == ERR) {
153 	_nc_set_screen(current);
154 	return 0;
155     }
156 
157     /* if the terminal type has real soft labels, set those up */
158     if (slk_format && num_labels > 0 && SLK_STDFMT(slk_format))
159 	_nc_slk_initialize(stdscr, COLS);
160 
161     SP->_ifd = fileno(ifp);
162     SP->_checkfd = fileno(ifp);
163     typeahead(fileno(ifp));
164 #ifdef TERMIOS
165     SP->_use_meta = ((cur_term->Ottyb.c_cflag & CSIZE) == CS8 &&
166 		     !(cur_term->Ottyb.c_iflag & ISTRIP));
167 #else
168     SP->_use_meta = FALSE;
169 #endif
170     SP->_endwin = FALSE;
171 
172     /* Check whether we can optimize scrolling under dumb terminals in case
173      * we do not have any of these capabilities, scrolling optimization
174      * will be useless.
175      */
176     SP->_scrolling = ((scroll_forward && scroll_reverse) ||
177 		      ((parm_rindex || parm_insert_line || insert_line) &&
178 		       (parm_index || parm_delete_line || delete_line)));
179 
180     baudrate();			/* sets a field in the SP structure */
181 
182     SP->_keytry = 0;
183 
184     /*
185      * Check for mismatched graphic-rendition capabilities.  Most SVr4
186      * terminfo trees contain entries that have rmul or rmso equated to
187      * sgr0 (Solaris curses copes with those entries).  We do this only for
188      * curses, since many termcap applications assume that smso/rmso and
189      * smul/rmul are paired, and will not function properly if we remove
190      * rmso or rmul.  Curses applications shouldn't be looking at this
191      * detail.
192      */
193 #define SGR0_TEST(mode) (mode != 0) && (exit_attribute_mode == 0 || strcmp(mode, exit_attribute_mode))
194     SP->_use_rmso = SGR0_TEST(exit_standout_mode);
195     SP->_use_rmul = SGR0_TEST(exit_underline_mode);
196 
197 #if USE_WIDEC_SUPPORT
198     /*
199      * XFree86 xterm can be configured to support UTF-8 based on environment
200      * variable settings.
201      */
202     {
203 	char *s;
204 	s = getenv("LC_ALL");
205 	if (s == NULL || *s == '\0') {
206 	    s = getenv("LC_CTYPE");
207 	    if (s == NULL || *s == '\0') {
208 		s = getenv("LANG");
209 	    }
210 	}
211 	if (s != NULL && *s != '\0' && strstr(s, "UTF-8") != NULL) {
212 	    SP->_outch = _nc_utf8_outch;
213 	}
214     }
215 #endif
216 
217     /* compute movement costs so we can do better move optimization */
218     _nc_mvcur_init();
219 
220     /* initialize terminal to a sane state */
221     _nc_screen_init();
222 
223     /* Initialize the terminal line settings. */
224     _nc_initscr();
225 
226     _nc_signal_handler(TRUE);
227 
228     T((T_RETURN("%p"), SP));
229     return (SP);
230 }
231