xref: /openbsd-src/lib/libcurses/base/lib_newterm.c (revision a22e14e348d0d1fd6025ad1e36987aea1a2f9ed5)
1 /*	$OpenBSD: lib_newterm.c,v 1.4 1999/06/14 02:29:15 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.40 1999/06/12 21:30:39 tom 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 	/* implement filter mode */
115 	if (filter_mode) {
116 		LINES = 1;
117 
118 		if (init_tabs != -1)
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 	      {
141 		if (ERR==_nc_ripoffline(-SLK_LINES(slk_format),
142 					_nc_slk_initialize))
143 		  return 0;
144 	      }
145 	/* this actually allocates the screen structure, and saves the
146 	 * original terminal settings.
147 	 */
148 	current = SP;
149 	_nc_set_screen(0);
150 	if (_nc_setupscreen(LINES, COLS, ofp) == ERR) {
151 	        _nc_set_screen(current);
152 		return 0;
153 	}
154 
155 	/* if the terminal type has real soft labels, set those up */
156 	if (slk_format && num_labels > 0 && SLK_STDFMT(slk_format))
157 	    _nc_slk_initialize(stdscr, COLS);
158 
159 	SP->_ifd        = fileno(ifp);
160 	SP->_checkfd	= fileno(ifp);
161 	typeahead(fileno(ifp));
162 #ifdef TERMIOS
163 	SP->_use_meta   = ((cur_term->Ottyb.c_cflag & CSIZE) == CS8 &&
164 			    !(cur_term->Ottyb.c_iflag & ISTRIP));
165 #else
166 	SP->_use_meta   = FALSE;
167 #endif
168 	SP->_endwin	= FALSE;
169 
170 	/* Check whether we can optimize scrolling under dumb terminals in case
171 	 * we do not have any of these capabilities, scrolling optimization
172 	 * will be useless.
173 	 */
174 	SP->_scrolling = ((scroll_forward && scroll_reverse) ||
175 			  ((parm_rindex || parm_insert_line || insert_line) &&
176 			   (parm_index  || parm_delete_line || delete_line)));
177 
178 	baudrate();	/* sets a field in the SP structure */
179 
180 	SP->_keytry = 0;
181 
182 	/*
183 	 * Check for mismatched graphic-rendition capabilities.  Most SVr4
184 	 * terminfo trees contain entries that have rmul or rmso equated to
185 	 * sgr0 (Solaris curses copes with those entries).  We do this only for
186 	 * curses, since many termcap applications assume that smso/rmso and
187 	 * smul/rmul are paired, and will not function properly if we remove
188 	 * rmso or rmul.  Curses applications shouldn't be looking at this
189 	 * detail.
190 	 */
191 #define SGR0_TEST(mode) (mode != 0) && (exit_attribute_mode == 0 || strcmp(mode, exit_attribute_mode))
192 	SP->_use_rmso = SGR0_TEST(exit_standout_mode);
193 	SP->_use_rmul = SGR0_TEST(exit_underline_mode);
194 
195 	/* compute movement costs so we can do better move optimization */
196 	_nc_mvcur_init();
197 
198 	/* initialize terminal to a sane state */
199 	_nc_screen_init();
200 
201 	/* Initialize the terminal line settings. */
202 	_nc_initscr();
203 
204 	_nc_signal_handler(TRUE);
205 
206 	T((T_RETURN("%p"), SP));
207 	return(SP);
208 }
209