xref: /openbsd-src/lib/libcurses/base/lib_newterm.c (revision bcc05361ef768670d0c5fa5affad379a9b45d34d)
1 /*	$OpenBSD: lib_newterm.c,v 1.3 1999/03/11 21:03:55 millert Exp $	*/
2 
3 /****************************************************************************
4  * Copyright (c) 1998 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 
38 /*
39 **	lib_newterm.c
40 **
41 **	The newterm() function.
42 **
43 */
44 
45 #include <curses.priv.h>
46 
47 #if defined(SVR4_TERMIO) && !defined(_POSIX_SOURCE)
48 #define _POSIX_SOURCE
49 #endif
50 
51 #include <term.h>	/* clear_screen, cup & friends, cur_term */
52 
53 MODULE_ID("$From: lib_newterm.c,v 1.39 1999/03/03 23:44:22 juergen Exp $")
54 
55 #ifndef ONLCR		/* Allows compilation under the QNX 4.2 OS */
56 #define ONLCR 0
57 #endif
58 
59 /*
60  * SVr4/XSI Curses specify that hardware echo is turned off in initscr, and not
61  * restored during the curses session.  The library simulates echo in software.
62  * (The behavior is unspecified if the application enables hardware echo).
63  *
64  * The newterm function also initializes terminal settings, and since initscr
65  * is supposed to behave as if it calls newterm, we do it here.
66  */
67 static inline int _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 filter(void)
92 {
93     filter_mode = TRUE;
94 }
95 
96 SCREEN * newterm(NCURSES_CONST char *term, 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)"), term, ofp, ifp));
109 
110 	/* this loads the capability entry, then sets LINES and COLS */
111 	if (setupterm(term, fileno(ofp), &errret) == ERR)
112 		return 0;
113 
114 	/*
115 	 * Check for mismatched graphic-rendition capabilities.  Most SVr4
116 	 * terminfo trees contain entries that have rmul or rmso equated to
117 	 * sgr0 (Solaris curses copes with those entries).  We do this only for
118 	 * curses, since many termcap applications assume that smso/rmso and
119 	 * smul/rmul are paired, and will not function properly if we remove
120 	 * rmso or rmul.  Curses applications shouldn't be looking at this
121 	 * detail.
122 	 */
123 	if (exit_attribute_mode) {
124 #define SGR0_FIX(mode) if (mode != 0 && !strcmp(mode, exit_attribute_mode)) \
125 			mode = 0
126 		SGR0_FIX(exit_underline_mode);
127 		SGR0_FIX(exit_standout_mode);
128 	}
129 
130 	/* implement filter mode */
131 	if (filter_mode) {
132 		LINES = 1;
133 
134 		if (init_tabs != -1)
135 			TABSIZE = init_tabs;
136 		else
137 			TABSIZE = 8;
138 
139 		T(("TABSIZE = %d", TABSIZE));
140 
141 		clear_screen = 0;
142 		cursor_down = parm_down_cursor = 0;
143 		cursor_address = 0;
144 		cursor_up = parm_up_cursor = 0;
145 		row_address = 0;
146 
147 		cursor_home = carriage_return;
148 	}
149 
150 	/* If we must simulate soft labels, grab off the line to be used.
151 	   We assume that we must simulate, if it is none of the standard
152 	   formats (4-4  or 3-2-3) for which there may be some hardware
153 	   support. */
154 	if (num_labels <= 0 || !SLK_STDFMT(slk_format))
155 	    if (slk_format)
156 	      {
157 		if (ERR==_nc_ripoffline(-SLK_LINES(slk_format),
158 					_nc_slk_initialize))
159 		  return 0;
160 	      }
161 	/* this actually allocates the screen structure, and saves the
162 	 * original terminal settings.
163 	 */
164 	current = SP;
165 	_nc_set_screen(0);
166 	if (_nc_setupscreen(LINES, COLS, ofp) == ERR) {
167 	        _nc_set_screen(current);
168 		return 0;
169 	}
170 
171 	/* if the terminal type has real soft labels, set those up */
172 	if (slk_format && num_labels > 0 && SLK_STDFMT(slk_format))
173 	    _nc_slk_initialize(stdscr, COLS);
174 
175 	SP->_ifd        = fileno(ifp);
176 	SP->_checkfd	= fileno(ifp);
177 	typeahead(fileno(ifp));
178 #ifdef TERMIOS
179 	SP->_use_meta   = ((cur_term->Ottyb.c_cflag & CSIZE) == CS8 &&
180 			    !(cur_term->Ottyb.c_iflag & ISTRIP));
181 #else
182 	SP->_use_meta   = FALSE;
183 #endif
184 	SP->_endwin	= FALSE;
185 
186 	/* Check whether we can optimize scrolling under dumb terminals in case
187 	 * we do not have any of these capabilities, scrolling optimization
188 	 * will be useless.
189 	 */
190 	SP->_scrolling = ((scroll_forward && scroll_reverse) ||
191 			  ((parm_rindex || parm_insert_line || insert_line) &&
192 			   (parm_index  || parm_delete_line || delete_line)));
193 
194 	baudrate();	/* sets a field in the SP structure */
195 
196 	SP->_keytry = 0;
197 
198 	/* compute movement costs so we can do better move optimization */
199 	_nc_mvcur_init();
200 
201 	/* initialize terminal to a sane state */
202 	_nc_screen_init();
203 
204 	/* Initialize the terminal line settings. */
205 	_nc_initscr();
206 
207 	_nc_signal_handler(TRUE);
208 
209 	T((T_RETURN("%p"), SP));
210 	return(SP);
211 }
212