1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1995-1999 by Sun Microsystems, Inc.
24*7c478bd9Sstevel@tonic-gate  * All rights reserved.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate /* LINTLIBRARY */
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate /*
30*7c478bd9Sstevel@tonic-gate  * newterm.c
31*7c478bd9Sstevel@tonic-gate  *
32*7c478bd9Sstevel@tonic-gate  * XCurses Library
33*7c478bd9Sstevel@tonic-gate  *
34*7c478bd9Sstevel@tonic-gate  * Copyright 1990, 1995 by Mortice Kern Systems Inc.  All rights reserved.
35*7c478bd9Sstevel@tonic-gate  *
36*7c478bd9Sstevel@tonic-gate  */
37*7c478bd9Sstevel@tonic-gate 
38*7c478bd9Sstevel@tonic-gate #ifdef M_RCSID
39*7c478bd9Sstevel@tonic-gate #ifndef lint
40*7c478bd9Sstevel@tonic-gate static char const rcsID[] =
41*7c478bd9Sstevel@tonic-gate "$Header: /team/ps/sun_xcurses/archive/local_changes/xcurses/src/lib/"
42*7c478bd9Sstevel@tonic-gate "libxcurses/src/libc/xcurses/rcs/newterm.c 1.13 1998/06/04 19:55:52 "
43*7c478bd9Sstevel@tonic-gate "cbates Exp $";
44*7c478bd9Sstevel@tonic-gate #endif
45*7c478bd9Sstevel@tonic-gate #endif
46*7c478bd9Sstevel@tonic-gate 
47*7c478bd9Sstevel@tonic-gate #include <sys/isa_defs.h>
48*7c478bd9Sstevel@tonic-gate #include <private.h>
49*7c478bd9Sstevel@tonic-gate #include <m_wio.h>
50*7c478bd9Sstevel@tonic-gate #include <errno.h>
51*7c478bd9Sstevel@tonic-gate #include <signal.h>
52*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
53*7c478bd9Sstevel@tonic-gate #include <string.h>
54*7c478bd9Sstevel@tonic-gate 
55*7c478bd9Sstevel@tonic-gate int	LINES, COLS;
56*7c478bd9Sstevel@tonic-gate int	COLORS, COLOR_PAIRS;
57*7c478bd9Sstevel@tonic-gate 
58*7c478bd9Sstevel@tonic-gate WINDOW	*curscr;
59*7c478bd9Sstevel@tonic-gate WINDOW	*stdscr;
60*7c478bd9Sstevel@tonic-gate SCREEN	*__m_screen;
61*7c478bd9Sstevel@tonic-gate 
62*7c478bd9Sstevel@tonic-gate static short	assume_one_line = FALSE;
63*7c478bd9Sstevel@tonic-gate 
64*7c478bd9Sstevel@tonic-gate /*
65*7c478bd9Sstevel@tonic-gate  * Assume terminal has only one screen line by restricting those
66*7c478bd9Sstevel@tonic-gate  * capabilities that assume more than one line.  This function must
67*7c478bd9Sstevel@tonic-gate  * be called before initscr() or newterm().
68*7c478bd9Sstevel@tonic-gate  *
69*7c478bd9Sstevel@tonic-gate  * This flag will reset after initscr() or newterm() so that subsequent
70*7c478bd9Sstevel@tonic-gate  * calls to newterm(), without a preceding call to filter(), will load
71*7c478bd9Sstevel@tonic-gate  * an unmodified terminal.  THIS IS NOT HISTORICAL PRACTICE, BUT DEEMED
72*7c478bd9Sstevel@tonic-gate  * USEFUL.
73*7c478bd9Sstevel@tonic-gate  */
74*7c478bd9Sstevel@tonic-gate void
filter(void)75*7c478bd9Sstevel@tonic-gate filter(void)
76*7c478bd9Sstevel@tonic-gate {
77*7c478bd9Sstevel@tonic-gate 	assume_one_line = TRUE;
78*7c478bd9Sstevel@tonic-gate }
79*7c478bd9Sstevel@tonic-gate 
80*7c478bd9Sstevel@tonic-gate /*
81*7c478bd9Sstevel@tonic-gate  * SIGTSTP Handler.
82*7c478bd9Sstevel@tonic-gate  */
83*7c478bd9Sstevel@tonic-gate /* ARGSUSED */
84*7c478bd9Sstevel@tonic-gate void
tstp(int signo)85*7c478bd9Sstevel@tonic-gate tstp(int signo)
86*7c478bd9Sstevel@tonic-gate {
87*7c478bd9Sstevel@tonic-gate #ifdef SIGTSTP
88*7c478bd9Sstevel@tonic-gate 	/*
89*7c478bd9Sstevel@tonic-gate 	 * Only permit SIGTSTP if the curent process is the process
90*7c478bd9Sstevel@tonic-gate 	 * group leader.  If the process is not the current group
91*7c478bd9Sstevel@tonic-gate 	 * leader, then suspending the current process will suspend
92*7c478bd9Sstevel@tonic-gate 	 * other members of the process group, such as the parent
93*7c478bd9Sstevel@tonic-gate 	 * process.
94*7c478bd9Sstevel@tonic-gate 	 */
95*7c478bd9Sstevel@tonic-gate 	if (getpid() == getpgrp()) {
96*7c478bd9Sstevel@tonic-gate 		(void) endwin();
97*7c478bd9Sstevel@tonic-gate 
98*7c478bd9Sstevel@tonic-gate #ifdef SIG_UNBLOCK
99*7c478bd9Sstevel@tonic-gate 		{
100*7c478bd9Sstevel@tonic-gate 			sigset_t unblock;
101*7c478bd9Sstevel@tonic-gate 
102*7c478bd9Sstevel@tonic-gate 			(void) sigemptyset(&unblock);
103*7c478bd9Sstevel@tonic-gate 			(void) sigaddset(&unblock, SIGTSTP);
104*7c478bd9Sstevel@tonic-gate 			(void) sigprocmask(SIG_UNBLOCK, &unblock,
105*7c478bd9Sstevel@tonic-gate 				(sigset_t *) 0);
106*7c478bd9Sstevel@tonic-gate 		}
107*7c478bd9Sstevel@tonic-gate #endif /* SIG_UNBLOCK */
108*7c478bd9Sstevel@tonic-gate 		(void) signal(SIGTSTP, SIG_DFL);
109*7c478bd9Sstevel@tonic-gate 		(void) kill(0, SIGTSTP);
110*7c478bd9Sstevel@tonic-gate 	} else {
111*7c478bd9Sstevel@tonic-gate 		(void) beep();
112*7c478bd9Sstevel@tonic-gate 	}
113*7c478bd9Sstevel@tonic-gate 
114*7c478bd9Sstevel@tonic-gate 	(void) signal(SIGTSTP, tstp);
115*7c478bd9Sstevel@tonic-gate 	(void) wrefresh(curscr);
116*7c478bd9Sstevel@tonic-gate #else /* no SIGTSTP */
117*7c478bd9Sstevel@tonic-gate 	(void) beep();
118*7c478bd9Sstevel@tonic-gate #endif /* SIGTSTP */
119*7c478bd9Sstevel@tonic-gate }
120*7c478bd9Sstevel@tonic-gate 
121*7c478bd9Sstevel@tonic-gate int	__m_slk_format = -1;
122*7c478bd9Sstevel@tonic-gate 
123*7c478bd9Sstevel@tonic-gate /*
124*7c478bd9Sstevel@tonic-gate  * Do real soft label key initialisation once setupterm() have been called
125*7c478bd9Sstevel@tonic-gate  * to load the current terminal.  Determine whether the terminal supplies
126*7c478bd9Sstevel@tonic-gate  * soft label keys, or whether we have to fake it by using the last line
127*7c478bd9Sstevel@tonic-gate  * of a terminal screen.
128*7c478bd9Sstevel@tonic-gate  */
129*7c478bd9Sstevel@tonic-gate /* ARGSUSED */
130*7c478bd9Sstevel@tonic-gate int
__m_slk_init(SCREEN * sp,int style)131*7c478bd9Sstevel@tonic-gate __m_slk_init(SCREEN *sp, int style)
132*7c478bd9Sstevel@tonic-gate {
133*7c478bd9Sstevel@tonic-gate 	int	code;
134*7c478bd9Sstevel@tonic-gate 
135*7c478bd9Sstevel@tonic-gate 	code = ERR;
136*7c478bd9Sstevel@tonic-gate 
137*7c478bd9Sstevel@tonic-gate 	(void) memset(&sp->_slk, 0, sizeof (sp->_slk));
138*7c478bd9Sstevel@tonic-gate 
139*7c478bd9Sstevel@tonic-gate 	/* Does the terminal have a method to program the soft label key? */
140*7c478bd9Sstevel@tonic-gate 	if (plab_norm != NULL || pkey_plab  != NULL) {
141*7c478bd9Sstevel@tonic-gate 		code = OK;
142*7c478bd9Sstevel@tonic-gate 		goto done;
143*7c478bd9Sstevel@tonic-gate 	}
144*7c478bd9Sstevel@tonic-gate 
145*7c478bd9Sstevel@tonic-gate 	/* We have to fake it. */
146*7c478bd9Sstevel@tonic-gate 	if (lines < 2)
147*7c478bd9Sstevel@tonic-gate 		goto done;
148*7c478bd9Sstevel@tonic-gate 
149*7c478bd9Sstevel@tonic-gate 	sp->_slk._w = subwin(sp->_newscr, 1, 0, lines-1, 0);
150*7c478bd9Sstevel@tonic-gate 	if (sp->_slk._w == NULL)
151*7c478bd9Sstevel@tonic-gate 		goto done;
152*7c478bd9Sstevel@tonic-gate 
153*7c478bd9Sstevel@tonic-gate 	/* Test suite seems to expect this */
154*7c478bd9Sstevel@tonic-gate 	(void) wattrset(sp->_slk._w, A_DIM|A_REVERSE);
155*7c478bd9Sstevel@tonic-gate 	(void) ripoffline(-1, 0);
156*7c478bd9Sstevel@tonic-gate 	code = OK;
157*7c478bd9Sstevel@tonic-gate done:
158*7c478bd9Sstevel@tonic-gate 	return (code);
159*7c478bd9Sstevel@tonic-gate }
160*7c478bd9Sstevel@tonic-gate 
161*7c478bd9Sstevel@tonic-gate /*
162*7c478bd9Sstevel@tonic-gate  * The XCurses specification is unclear how ripoffline() would
163*7c478bd9Sstevel@tonic-gate  * affect newterm().  We assume that it can't be used with newterm()
164*7c478bd9Sstevel@tonic-gate  * and that it only affects initscr(), which is responsible for
165*7c478bd9Sstevel@tonic-gate  * creating stdscr.
166*7c478bd9Sstevel@tonic-gate  */
167*7c478bd9Sstevel@tonic-gate t_rip	rip = { 0 };
168*7c478bd9Sstevel@tonic-gate 
169*7c478bd9Sstevel@tonic-gate /*
170*7c478bd9Sstevel@tonic-gate  * If line is positive (1), one line is removed from the beginning of
171*7c478bd9Sstevel@tonic-gate  * stdscr; else if line is negative (-1), one line is removed from the end.
172*7c478bd9Sstevel@tonic-gate  */
173*7c478bd9Sstevel@tonic-gate int
ripoffline(int line,int (* init)(WINDOW *,int))174*7c478bd9Sstevel@tonic-gate ripoffline(int line, int (*init)(WINDOW *, int))
175*7c478bd9Sstevel@tonic-gate {
176*7c478bd9Sstevel@tonic-gate 	int	i;
177*7c478bd9Sstevel@tonic-gate 
178*7c478bd9Sstevel@tonic-gate 	i = rip.top - rip.bottom;
179*7c478bd9Sstevel@tonic-gate 
180*7c478bd9Sstevel@tonic-gate 	if (line != 0 && i < M_CURSES_MAX_RIPOFFLINE) {
181*7c478bd9Sstevel@tonic-gate 		rip.line[i].init = init;
182*7c478bd9Sstevel@tonic-gate 		if (line < 0)
183*7c478bd9Sstevel@tonic-gate 			rip.line[i].dy = --rip.bottom;
184*7c478bd9Sstevel@tonic-gate 		else
185*7c478bd9Sstevel@tonic-gate 			rip.line[i].dy = rip.top++;
186*7c478bd9Sstevel@tonic-gate 	}
187*7c478bd9Sstevel@tonic-gate 
188*7c478bd9Sstevel@tonic-gate 	return (OK);
189*7c478bd9Sstevel@tonic-gate }
190*7c478bd9Sstevel@tonic-gate 
191*7c478bd9Sstevel@tonic-gate /*
192*7c478bd9Sstevel@tonic-gate  * Create a new terminal screen.  Used if a program is going to be sending
193*7c478bd9Sstevel@tonic-gate  * output to more than one terminal.  It returns a SCREEN* for the terminal.
194*7c478bd9Sstevel@tonic-gate  * The parameters are a terminal name, output FILE*, and input FILE*.  If
195*7c478bd9Sstevel@tonic-gate  * the terminal name is null then $TERM is used.  The program must also
196*7c478bd9Sstevel@tonic-gate  * call endwin() for each terminal being used before exiting from curses.
197*7c478bd9Sstevel@tonic-gate  * If newterm() is called more than once for the same terminal, the first
198*7c478bd9Sstevel@tonic-gate  * terminal referred to must be the last one for which endwin() is called.
199*7c478bd9Sstevel@tonic-gate  */
200*7c478bd9Sstevel@tonic-gate SCREEN *
newterm(char * term,FILE * out_fp,FILE * in_fp)201*7c478bd9Sstevel@tonic-gate newterm(char *term, FILE *out_fp, FILE *in_fp)
202*7c478bd9Sstevel@tonic-gate {
203*7c478bd9Sstevel@tonic-gate 	WINDOW	*w;
204*7c478bd9Sstevel@tonic-gate 	t_wide_io	*wio;
205*7c478bd9Sstevel@tonic-gate 	SCREEN	*sp, *osp;
206*7c478bd9Sstevel@tonic-gate 	int	i, n, y, errret;
207*7c478bd9Sstevel@tonic-gate 
208*7c478bd9Sstevel@tonic-gate 	/*
209*7c478bd9Sstevel@tonic-gate 	 * Input stream should be unbuffered so that m_tfgetc() works
210*7c478bd9Sstevel@tonic-gate 	 * correctly on BSD and SUN systems.
211*7c478bd9Sstevel@tonic-gate 	 */
212*7c478bd9Sstevel@tonic-gate 	(void) setvbuf(in_fp, (char *) 0, _IONBF, BUFSIZ);
213*7c478bd9Sstevel@tonic-gate #if 0
214*7c478bd9Sstevel@tonic-gate /*
215*7c478bd9Sstevel@tonic-gate  * Not sure whether we really want to concern ourselves with the output
216*7c478bd9Sstevel@tonic-gate  * buffer scheme.  Might be best to leave it upto the application to
217*7c478bd9Sstevel@tonic-gate  * deal with buffer schemes and when to perform flushes.
218*7c478bd9Sstevel@tonic-gate  *
219*7c478bd9Sstevel@tonic-gate  * MKS Vi uses MKS Curses and so must support the ability to switch in
220*7c478bd9Sstevel@tonic-gate  * and out of Curses mode when switching from Vi to Ex and back.
221*7c478bd9Sstevel@tonic-gate  * Problem is that in Vi mode you would prefer full buffered output to
222*7c478bd9Sstevel@tonic-gate  * give updates a smoother appearance and Ex mode you require line
223*7c478bd9Sstevel@tonic-gate  * buffered in order to see prompts and messages.
224*7c478bd9Sstevel@tonic-gate  */
225*7c478bd9Sstevel@tonic-gate 	(void) setvbuf(out_fp, (char *) 0, _IOLBF, BUFSIZ);
226*7c478bd9Sstevel@tonic-gate #endif
227*7c478bd9Sstevel@tonic-gate 	errno = 0;
228*7c478bd9Sstevel@tonic-gate 
229*7c478bd9Sstevel@tonic-gate 	if (__m_setupterm(term, fileno(in_fp), fileno(out_fp), &errret)
230*7c478bd9Sstevel@tonic-gate 		== ERR) {
231*7c478bd9Sstevel@tonic-gate 		switch (errret) {
232*7c478bd9Sstevel@tonic-gate 		case -1:
233*7c478bd9Sstevel@tonic-gate 			errno = ENOMEM;
234*7c478bd9Sstevel@tonic-gate 			break;
235*7c478bd9Sstevel@tonic-gate 		case 2:
236*7c478bd9Sstevel@tonic-gate 			errno = ENAMETOOLONG;
237*7c478bd9Sstevel@tonic-gate 			break;
238*7c478bd9Sstevel@tonic-gate 		case 0:
239*7c478bd9Sstevel@tonic-gate 		default:
240*7c478bd9Sstevel@tonic-gate 			errno = ENOENT;
241*7c478bd9Sstevel@tonic-gate 			break;
242*7c478bd9Sstevel@tonic-gate 		}
243*7c478bd9Sstevel@tonic-gate 		goto error1;
244*7c478bd9Sstevel@tonic-gate 	}
245*7c478bd9Sstevel@tonic-gate 
246*7c478bd9Sstevel@tonic-gate 	if (__m_doupdate_init())
247*7c478bd9Sstevel@tonic-gate 		goto error1;
248*7c478bd9Sstevel@tonic-gate 
249*7c478bd9Sstevel@tonic-gate 	if ((sp = (SCREEN *) calloc(1, sizeof (*sp))) == NULL)
250*7c478bd9Sstevel@tonic-gate 		goto error1;
251*7c478bd9Sstevel@tonic-gate 
252*7c478bd9Sstevel@tonic-gate 	sp->_kfd = -1;
253*7c478bd9Sstevel@tonic-gate 	sp->_if = in_fp;
254*7c478bd9Sstevel@tonic-gate 	sp->_of = out_fp;
255*7c478bd9Sstevel@tonic-gate 	sp->_term = cur_term;
256*7c478bd9Sstevel@tonic-gate 
257*7c478bd9Sstevel@tonic-gate 	sp->_unget._size = __m_decode_init((t_decode **) &sp->_decode);
258*7c478bd9Sstevel@tonic-gate 
259*7c478bd9Sstevel@tonic-gate 	/*
260*7c478bd9Sstevel@tonic-gate 	 * Maximum length of a multbyte key sequence, including
261*7c478bd9Sstevel@tonic-gate 	 * multibyte characters and terminal function keys.
262*7c478bd9Sstevel@tonic-gate 	 */
263*7c478bd9Sstevel@tonic-gate 	if (sp->_unget._size < (M_TYPEAHEAD_SIZE + MB_LEN_MAX))
264*7c478bd9Sstevel@tonic-gate 		sp->_unget._size = M_TYPEAHEAD_SIZE + MB_LEN_MAX;
265*7c478bd9Sstevel@tonic-gate 
266*7c478bd9Sstevel@tonic-gate 	sp->_unget._stack = calloc((size_t) sp->_unget._size,
267*7c478bd9Sstevel@tonic-gate 		sizeof (*sp->_unget._stack));
268*7c478bd9Sstevel@tonic-gate 	if (sp->_unget._stack == NULL)
269*7c478bd9Sstevel@tonic-gate 		goto error2;
270*7c478bd9Sstevel@tonic-gate 
271*7c478bd9Sstevel@tonic-gate 	if ((wio = (t_wide_io *) calloc(1, sizeof (*wio))) == NULL)
272*7c478bd9Sstevel@tonic-gate 		goto error2;
273*7c478bd9Sstevel@tonic-gate 
274*7c478bd9Sstevel@tonic-gate 	/* Setup wide input for XCurses. */
275*7c478bd9Sstevel@tonic-gate 	wio->get = (int (*)(void *)) wgetch;
276*7c478bd9Sstevel@tonic-gate 	wio->unget = __xc_ungetc;
277*7c478bd9Sstevel@tonic-gate 	wio->reset = __xc_clearerr;
278*7c478bd9Sstevel@tonic-gate 	wio->iserror = __xc_ferror;
279*7c478bd9Sstevel@tonic-gate 	wio->iseof = __xc_feof;
280*7c478bd9Sstevel@tonic-gate 	sp->_in = wio;
281*7c478bd9Sstevel@tonic-gate 
282*7c478bd9Sstevel@tonic-gate 	if (assume_one_line) {
283*7c478bd9Sstevel@tonic-gate 		/* Assume only one line. */
284*7c478bd9Sstevel@tonic-gate 		lines = 1;
285*7c478bd9Sstevel@tonic-gate 
286*7c478bd9Sstevel@tonic-gate 		/* Disable capabilities that assume more than one line. */
287*7c478bd9Sstevel@tonic-gate 		clear_screen = clr_eos = cursor_up = cursor_down = NULL;
288*7c478bd9Sstevel@tonic-gate 		cursor_home = cursor_to_ll = cursor_address = NULL;
289*7c478bd9Sstevel@tonic-gate 		row_address = parm_up_cursor = parm_down_cursor = NULL;
290*7c478bd9Sstevel@tonic-gate 
291*7c478bd9Sstevel@tonic-gate 		/* Re-evaluate the cursor motion costs. */
292*7c478bd9Sstevel@tonic-gate 		__m_mvcur_cost();
293*7c478bd9Sstevel@tonic-gate 
294*7c478bd9Sstevel@tonic-gate 		/* Reset flag for subsequent calls to newterm(). */
295*7c478bd9Sstevel@tonic-gate 		assume_one_line = FALSE;
296*7c478bd9Sstevel@tonic-gate 	}
297*7c478bd9Sstevel@tonic-gate 
298*7c478bd9Sstevel@tonic-gate 	if ((sp->_curscr = newwin(lines, columns, 0, 0)) == NULL)
299*7c478bd9Sstevel@tonic-gate 		goto error2;
300*7c478bd9Sstevel@tonic-gate 
301*7c478bd9Sstevel@tonic-gate 	if ((sp->_newscr = newwin(lines, columns, 0, 0)) == NULL)
302*7c478bd9Sstevel@tonic-gate 		goto error2;
303*7c478bd9Sstevel@tonic-gate 
304*7c478bd9Sstevel@tonic-gate #if defined(_LP64)
305*7c478bd9Sstevel@tonic-gate 	sp->_hash = (unsigned int *) calloc(lines, sizeof (*sp->_hash));
306*7c478bd9Sstevel@tonic-gate #else
307*7c478bd9Sstevel@tonic-gate 	sp->_hash = (unsigned long *) calloc(lines, sizeof (*sp->_hash));
308*7c478bd9Sstevel@tonic-gate #endif
309*7c478bd9Sstevel@tonic-gate 	if (sp->_hash == NULL)
310*7c478bd9Sstevel@tonic-gate 		goto error2;
311*7c478bd9Sstevel@tonic-gate 
312*7c478bd9Sstevel@tonic-gate 	if (0 <= __m_slk_format && __m_slk_init(sp, __m_slk_format) == ERR) {
313*7c478bd9Sstevel@tonic-gate 		goto error2;
314*7c478bd9Sstevel@tonic-gate 	}
315*7c478bd9Sstevel@tonic-gate 
316*7c478bd9Sstevel@tonic-gate 	/*
317*7c478bd9Sstevel@tonic-gate 	 * doupdate() will perform the final screen preparations like
318*7c478bd9Sstevel@tonic-gate 	 * enter_ca_mode, reset_prog_mode() (to assert the termios
319*7c478bd9Sstevel@tonic-gate 	 * changes), etc.
320*7c478bd9Sstevel@tonic-gate 	 */
321*7c478bd9Sstevel@tonic-gate 	sp->_flags |= S_ENDWIN;
322*7c478bd9Sstevel@tonic-gate 
323*7c478bd9Sstevel@tonic-gate #ifdef SIGTSTP
324*7c478bd9Sstevel@tonic-gate 	(void) signal(SIGTSTP, tstp);
325*7c478bd9Sstevel@tonic-gate #endif
326*7c478bd9Sstevel@tonic-gate 	/* Assert that __m_screen is set to the new terminal. */
327*7c478bd9Sstevel@tonic-gate 	osp = set_term(sp);
328*7c478bd9Sstevel@tonic-gate 
329*7c478bd9Sstevel@tonic-gate 	/* Disable echo in tty driver, Curses does software echo. */
330*7c478bd9Sstevel@tonic-gate 	PTERMIOS(_prog)->c_lflag &= ~ECHO;
331*7c478bd9Sstevel@tonic-gate 
332*7c478bd9Sstevel@tonic-gate 	/* Enable mappnig of cr -> nl on input and nl -> crlf on output. */
333*7c478bd9Sstevel@tonic-gate 	PTERMIOS(_prog)->c_iflag |= ICRNL;
334*7c478bd9Sstevel@tonic-gate 	PTERMIOS(_prog)->c_oflag |= OPOST;
335*7c478bd9Sstevel@tonic-gate #ifdef ONLCR
336*7c478bd9Sstevel@tonic-gate 	PTERMIOS(_prog)->c_oflag |= ONLCR;
337*7c478bd9Sstevel@tonic-gate #endif
338*7c478bd9Sstevel@tonic-gate 	cur_term->_flags |= __TERM_NL_IS_CRLF;
339*7c478bd9Sstevel@tonic-gate 
340*7c478bd9Sstevel@tonic-gate #ifdef TAB0
341*7c478bd9Sstevel@tonic-gate 	/* Use real tabs. */
342*7c478bd9Sstevel@tonic-gate 	PTERMIOS(_prog)->c_oflag &= ~(TAB1|TAB2|TAB3);
343*7c478bd9Sstevel@tonic-gate #endif
344*7c478bd9Sstevel@tonic-gate 
345*7c478bd9Sstevel@tonic-gate 	/*
346*7c478bd9Sstevel@tonic-gate 	 * Default to 'cbreak' mode as per
347*7c478bd9Sstevel@tonic-gate 	 * test /tset/CAPIxcurses/fcbreak/fcbreak1{4}
348*7c478bd9Sstevel@tonic-gate 	 */
349*7c478bd9Sstevel@tonic-gate 	cur_term->_flags &= ~__TERM_HALF_DELAY;
350*7c478bd9Sstevel@tonic-gate 
351*7c478bd9Sstevel@tonic-gate 	/*
352*7c478bd9Sstevel@tonic-gate 	 * Default to 'idcok' mode as per
353*7c478bd9Sstevel@tonic-gate 	 * test /tset/CAPIxcurses/fidcok/fidcok1{3}
354*7c478bd9Sstevel@tonic-gate 	 */
355*7c478bd9Sstevel@tonic-gate 	__m_screen->_flags |= S_INS_DEL_CHAR;
356*7c478bd9Sstevel@tonic-gate 
357*7c478bd9Sstevel@tonic-gate 	PTERMIOS(_prog)->c_cc[VMIN] = 1;
358*7c478bd9Sstevel@tonic-gate 	PTERMIOS(_prog)->c_cc[VTIME] = 0;
359*7c478bd9Sstevel@tonic-gate 	PTERMIOS(_prog)->c_lflag &= ~ICANON;
360*7c478bd9Sstevel@tonic-gate 
361*7c478bd9Sstevel@tonic-gate 	(void) __m_tty_set_prog_mode();
362*7c478bd9Sstevel@tonic-gate 	(void) __m_set_echo(1);
363*7c478bd9Sstevel@tonic-gate 	(void) typeahead(fileno(in_fp));
364*7c478bd9Sstevel@tonic-gate 
365*7c478bd9Sstevel@tonic-gate 	(void) __m_slk_clear(1);
366*7c478bd9Sstevel@tonic-gate 
367*7c478bd9Sstevel@tonic-gate 	n = rip.top - rip.bottom;
368*7c478bd9Sstevel@tonic-gate 	if (stdscr == NULL) {
369*7c478bd9Sstevel@tonic-gate 		stdscr = newwin(lines - n, 0, rip.top, 0);
370*7c478bd9Sstevel@tonic-gate 		if (stdscr == NULL)
371*7c478bd9Sstevel@tonic-gate 			goto error3;
372*7c478bd9Sstevel@tonic-gate 	}
373*7c478bd9Sstevel@tonic-gate 	/*
374*7c478bd9Sstevel@tonic-gate 	 * Create and initialise ripped off line windows.
375*7c478bd9Sstevel@tonic-gate 	 * It is the application's responsiblity to free the
376*7c478bd9Sstevel@tonic-gate 	 * windows when the application terminates.
377*7c478bd9Sstevel@tonic-gate 	 */
378*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < n; ++i) {
379*7c478bd9Sstevel@tonic-gate 		if (rip.line[i].created)
380*7c478bd9Sstevel@tonic-gate 			continue;
381*7c478bd9Sstevel@tonic-gate 		y = rip.line[i].dy;
382*7c478bd9Sstevel@tonic-gate 		if (y < 0)
383*7c478bd9Sstevel@tonic-gate 			y += lines;
384*7c478bd9Sstevel@tonic-gate 
385*7c478bd9Sstevel@tonic-gate 		w = newwin(1, 0, y, 0);
386*7c478bd9Sstevel@tonic-gate 		if (rip.line[i].init != (int (*)(WINDOW *, int)) 0)
387*7c478bd9Sstevel@tonic-gate 			(void) (*rip.line[i].init)(w, columns);
388*7c478bd9Sstevel@tonic-gate 		rip.line[i].created = 1;
389*7c478bd9Sstevel@tonic-gate 	}
390*7c478bd9Sstevel@tonic-gate 	LINES = stdscr->_maxy = sp->_curscr->_maxy - n;
391*7c478bd9Sstevel@tonic-gate 
392*7c478bd9Sstevel@tonic-gate 	return (sp);
393*7c478bd9Sstevel@tonic-gate error3:
394*7c478bd9Sstevel@tonic-gate 	(void) set_term(osp);
395*7c478bd9Sstevel@tonic-gate error2:
396*7c478bd9Sstevel@tonic-gate 	delscreen(sp);
397*7c478bd9Sstevel@tonic-gate error1:
398*7c478bd9Sstevel@tonic-gate 	return (NULL);
399*7c478bd9Sstevel@tonic-gate }
400*7c478bd9Sstevel@tonic-gate 
401*7c478bd9Sstevel@tonic-gate /*
402*7c478bd9Sstevel@tonic-gate  * Free storage associated with a screen structure.
403*7c478bd9Sstevel@tonic-gate  * NOTE endwin() does not do this.
404*7c478bd9Sstevel@tonic-gate  */
405*7c478bd9Sstevel@tonic-gate void
delscreen(SCREEN * sp)406*7c478bd9Sstevel@tonic-gate delscreen(SCREEN *sp)
407*7c478bd9Sstevel@tonic-gate {
408*7c478bd9Sstevel@tonic-gate 	if (sp != NULL) {
409*7c478bd9Sstevel@tonic-gate 		if (sp->_slk._w != NULL)
410*7c478bd9Sstevel@tonic-gate 			(void) delwin(sp->_slk._w);
411*7c478bd9Sstevel@tonic-gate 
412*7c478bd9Sstevel@tonic-gate 		(void) delwin(sp->_newscr);
413*7c478bd9Sstevel@tonic-gate 		(void) delwin(sp->_curscr);
414*7c478bd9Sstevel@tonic-gate 		(void) del_curterm(sp->_term);
415*7c478bd9Sstevel@tonic-gate 
416*7c478bd9Sstevel@tonic-gate 		__m_decode_free((t_decode **) &sp->_decode);
417*7c478bd9Sstevel@tonic-gate 
418*7c478bd9Sstevel@tonic-gate 		if (sp->_hash != NULL)
419*7c478bd9Sstevel@tonic-gate 			free(sp->_hash);
420*7c478bd9Sstevel@tonic-gate 
421*7c478bd9Sstevel@tonic-gate 		if (sp->_unget._stack != NULL)
422*7c478bd9Sstevel@tonic-gate 			free(sp->_unget._stack);
423*7c478bd9Sstevel@tonic-gate 
424*7c478bd9Sstevel@tonic-gate 		if (sp->_in != NULL)
425*7c478bd9Sstevel@tonic-gate 			free(sp->_in);
426*7c478bd9Sstevel@tonic-gate 
427*7c478bd9Sstevel@tonic-gate 		free(sp);
428*7c478bd9Sstevel@tonic-gate 	}
429*7c478bd9Sstevel@tonic-gate }
430*7c478bd9Sstevel@tonic-gate 
431*7c478bd9Sstevel@tonic-gate /*
432*7c478bd9Sstevel@tonic-gate  * Switch current terminal for Curses layer.
433*7c478bd9Sstevel@tonic-gate  */
434*7c478bd9Sstevel@tonic-gate SCREEN *
set_term(SCREEN * screen)435*7c478bd9Sstevel@tonic-gate set_term(SCREEN *screen)
436*7c478bd9Sstevel@tonic-gate {
437*7c478bd9Sstevel@tonic-gate 	SCREEN	*osp = __m_screen;
438*7c478bd9Sstevel@tonic-gate 
439*7c478bd9Sstevel@tonic-gate 	if (screen != NULL) {
440*7c478bd9Sstevel@tonic-gate 		(void) set_curterm(screen->_term);
441*7c478bd9Sstevel@tonic-gate 		curscr = screen->_curscr;
442*7c478bd9Sstevel@tonic-gate 		__m_screen = screen;
443*7c478bd9Sstevel@tonic-gate 
444*7c478bd9Sstevel@tonic-gate 		LINES = lines;
445*7c478bd9Sstevel@tonic-gate 		COLS = columns;
446*7c478bd9Sstevel@tonic-gate 		COLORS = max_colors;
447*7c478bd9Sstevel@tonic-gate 		COLOR_PAIRS = max_pairs;
448*7c478bd9Sstevel@tonic-gate 	}
449*7c478bd9Sstevel@tonic-gate 
450*7c478bd9Sstevel@tonic-gate 	return (osp);
451*7c478bd9Sstevel@tonic-gate }
452*7c478bd9Sstevel@tonic-gate 
453*7c478bd9Sstevel@tonic-gate int
typeahead(int fd)454*7c478bd9Sstevel@tonic-gate typeahead(int fd)
455*7c478bd9Sstevel@tonic-gate {
456*7c478bd9Sstevel@tonic-gate 	__m_screen->_flags &= ~S_ISATTY;
457*7c478bd9Sstevel@tonic-gate 	if (fd != -1) {
458*7c478bd9Sstevel@tonic-gate 		if (isatty(fd)) {
459*7c478bd9Sstevel@tonic-gate 			__m_screen->_kfd = fd;
460*7c478bd9Sstevel@tonic-gate 			__m_screen->_flags |= S_ISATTY;
461*7c478bd9Sstevel@tonic-gate 		} else {
462*7c478bd9Sstevel@tonic-gate 			__m_screen->_kfd = -1;
463*7c478bd9Sstevel@tonic-gate 		}
464*7c478bd9Sstevel@tonic-gate 	}
465*7c478bd9Sstevel@tonic-gate 
466*7c478bd9Sstevel@tonic-gate 	return (OK);
467*7c478bd9Sstevel@tonic-gate }
468*7c478bd9Sstevel@tonic-gate 
469*7c478bd9Sstevel@tonic-gate int
__m_set_echo(int bf)470*7c478bd9Sstevel@tonic-gate __m_set_echo(int bf)
471*7c478bd9Sstevel@tonic-gate {
472*7c478bd9Sstevel@tonic-gate 	int	old;
473*7c478bd9Sstevel@tonic-gate 
474*7c478bd9Sstevel@tonic-gate 	old = (__m_screen->_flags & S_ECHO) == S_ECHO;
475*7c478bd9Sstevel@tonic-gate 
476*7c478bd9Sstevel@tonic-gate 	__m_screen->_flags &= ~S_ECHO;
477*7c478bd9Sstevel@tonic-gate 	if (bf)
478*7c478bd9Sstevel@tonic-gate 		__m_screen->_flags |= S_ECHO;
479*7c478bd9Sstevel@tonic-gate 
480*7c478bd9Sstevel@tonic-gate 	return (old);
481*7c478bd9Sstevel@tonic-gate }
482