xref: /dflybsd-src/contrib/nvi2/cl/cl_screen.c (revision 07bc39c2f4bbca56f12568e06d89da17f2eeb965)
1e0b8e63eSJohn Marino /*-
2e0b8e63eSJohn Marino  * Copyright (c) 1993, 1994
3e0b8e63eSJohn Marino  *	The Regents of the University of California.  All rights reserved.
4e0b8e63eSJohn Marino  * Copyright (c) 1993, 1994, 1995, 1996
5e0b8e63eSJohn Marino  *	Keith Bostic.  All rights reserved.
6e0b8e63eSJohn Marino  *
7e0b8e63eSJohn Marino  * See the LICENSE file for redistribution information.
8e0b8e63eSJohn Marino  */
9e0b8e63eSJohn Marino 
10e0b8e63eSJohn Marino #include "config.h"
11e0b8e63eSJohn Marino 
12e0b8e63eSJohn Marino #include <sys/types.h>
13e0b8e63eSJohn Marino #include <sys/queue.h>
14e0b8e63eSJohn Marino #include <sys/time.h>
15e0b8e63eSJohn Marino 
16e0b8e63eSJohn Marino #include <bitstring.h>
17e0b8e63eSJohn Marino #include <errno.h>
18e0b8e63eSJohn Marino #include <signal.h>
19e0b8e63eSJohn Marino #include <stdio.h>
20e0b8e63eSJohn Marino #include <stdlib.h>
21e0b8e63eSJohn Marino #include <string.h>
22e0b8e63eSJohn Marino #ifdef HAVE_TERM_H
23e0b8e63eSJohn Marino #include <term.h>
24e0b8e63eSJohn Marino #endif
25e0b8e63eSJohn Marino #include <termios.h>
26e0b8e63eSJohn Marino #include <unistd.h>
27e0b8e63eSJohn Marino 
28e0b8e63eSJohn Marino #include "../common/common.h"
29e0b8e63eSJohn Marino #include "cl.h"
30e0b8e63eSJohn Marino 
31e0b8e63eSJohn Marino static int	cl_ex_end(GS *);
32e0b8e63eSJohn Marino static int	cl_ex_init(SCR *);
33e0b8e63eSJohn Marino static void	cl_freecap(CL_PRIVATE *);
34e0b8e63eSJohn Marino static int	cl_vi_end(GS *);
35e0b8e63eSJohn Marino static int	cl_vi_init(SCR *);
36e0b8e63eSJohn Marino static int	cl_putenv(char *, char *, u_long);
37e0b8e63eSJohn Marino 
38e0b8e63eSJohn Marino /*
39e0b8e63eSJohn Marino  * cl_screen --
40e0b8e63eSJohn Marino  *	Switch screen types.
41e0b8e63eSJohn Marino  *
42e0b8e63eSJohn Marino  * PUBLIC: int cl_screen(SCR *, u_int32_t);
43e0b8e63eSJohn Marino  */
44e0b8e63eSJohn Marino int
cl_screen(SCR * sp,u_int32_t flags)45e0b8e63eSJohn Marino cl_screen(SCR *sp, u_int32_t flags)
46e0b8e63eSJohn Marino {
47e0b8e63eSJohn Marino 	CL_PRIVATE *clp;
48e0b8e63eSJohn Marino 	WINDOW *win;
49e0b8e63eSJohn Marino 	GS *gp;
50e0b8e63eSJohn Marino 
51e0b8e63eSJohn Marino 	gp = sp->gp;
52e0b8e63eSJohn Marino 	clp = CLP(sp);
53e0b8e63eSJohn Marino 	win = CLSP(sp) ? CLSP(sp) : stdscr;
54e0b8e63eSJohn Marino 
55e0b8e63eSJohn Marino 	/* See if the current information is incorrect. */
56e0b8e63eSJohn Marino 	if (F_ISSET(gp, G_SRESTART)) {
57e0b8e63eSJohn Marino 		if ((!F_ISSET(sp, SC_SCR_EX | SC_SCR_VI) ||
58e0b8e63eSJohn Marino 		     resizeterm(O_VAL(sp, O_LINES), O_VAL(sp, O_COLUMNS))) &&
59e0b8e63eSJohn Marino 		    cl_quit(gp))
60e0b8e63eSJohn Marino 			return (1);
61e0b8e63eSJohn Marino 		F_CLR(gp, G_SRESTART);
62e0b8e63eSJohn Marino 	}
63e0b8e63eSJohn Marino 
64e0b8e63eSJohn Marino 	/* See if we're already in the right mode. */
65e0b8e63eSJohn Marino 	if ((LF_ISSET(SC_EX) && F_ISSET(sp, SC_SCR_EX)) ||
66e0b8e63eSJohn Marino 	    (LF_ISSET(SC_VI) && F_ISSET(sp, SC_SCR_VI)))
67e0b8e63eSJohn Marino 		return (0);
68e0b8e63eSJohn Marino 
69e0b8e63eSJohn Marino 	/*
70e0b8e63eSJohn Marino 	 * Fake leaving ex mode.
71e0b8e63eSJohn Marino 	 *
72e0b8e63eSJohn Marino 	 * We don't actually exit ex or vi mode unless forced (e.g. by a window
73e0b8e63eSJohn Marino 	 * size change).  This is because many curses implementations can't be
74e0b8e63eSJohn Marino 	 * called twice in a single program.  Plus, it's faster.  If the editor
75e0b8e63eSJohn Marino 	 * "leaves" vi to enter ex, when it exits ex we'll just fall back into
76e0b8e63eSJohn Marino 	 * vi.
77e0b8e63eSJohn Marino 	 */
78e0b8e63eSJohn Marino 	if (F_ISSET(sp, SC_SCR_EX))
79e0b8e63eSJohn Marino 		F_CLR(sp, SC_SCR_EX);
80e0b8e63eSJohn Marino 
81e0b8e63eSJohn Marino 	/*
82e0b8e63eSJohn Marino 	 * Fake leaving vi mode.
83e0b8e63eSJohn Marino 	 *
84e0b8e63eSJohn Marino 	 * Clear out the rest of the screen if we're in the middle of a split
85e0b8e63eSJohn Marino 	 * screen.  Move to the last line in the current screen -- this makes
86e0b8e63eSJohn Marino 	 * terminal scrolling happen naturally.  Note: *don't* move past the
87e0b8e63eSJohn Marino 	 * end of the screen, as there are ex commands (e.g., :read ! cat file)
88e0b8e63eSJohn Marino 	 * that don't want to.  Don't clear the info line, its contents may be
89e0b8e63eSJohn Marino 	 * valid, e.g. :file|append.
90e0b8e63eSJohn Marino 	 */
91e0b8e63eSJohn Marino 	if (F_ISSET(sp, SC_SCR_VI)) {
92e0b8e63eSJohn Marino 		F_CLR(sp, SC_SCR_VI);
93e0b8e63eSJohn Marino 
94e0b8e63eSJohn Marino 		if (TAILQ_NEXT(sp, q) != NULL) {
95e0b8e63eSJohn Marino 			(void)wmove(win, RLNO(sp, sp->rows), 0);
96e0b8e63eSJohn Marino 			wclrtobot(win);
97e0b8e63eSJohn Marino 		}
98e0b8e63eSJohn Marino 		(void)wmove(win, RLNO(sp, sp->rows) - 1, 0);
99e0b8e63eSJohn Marino 		wrefresh(win);
100e0b8e63eSJohn Marino 	}
101e0b8e63eSJohn Marino 
102e0b8e63eSJohn Marino 	/* Enter the requested mode. */
103e0b8e63eSJohn Marino 	if (LF_ISSET(SC_EX)) {
104e0b8e63eSJohn Marino 		if (cl_ex_init(sp))
105e0b8e63eSJohn Marino 			return (1);
106e0b8e63eSJohn Marino 		F_SET(clp, CL_IN_EX | CL_SCR_EX_INIT);
107e0b8e63eSJohn Marino 
108e0b8e63eSJohn Marino 		/*
109e0b8e63eSJohn Marino 		 * If doing an ex screen for ex mode, move to the last line
110e0b8e63eSJohn Marino 		 * on the screen.
111e0b8e63eSJohn Marino 		 */
112e0b8e63eSJohn Marino 		if (F_ISSET(sp, SC_EX) && clp->cup != NULL)
113e0b8e63eSJohn Marino 			tputs(tgoto(clp->cup,
114e0b8e63eSJohn Marino 			    0, O_VAL(sp, O_LINES) - 1), 1, cl_putchar);
115e0b8e63eSJohn Marino 	} else {
116e0b8e63eSJohn Marino 		if (cl_vi_init(sp))
117e0b8e63eSJohn Marino 			return (1);
118e0b8e63eSJohn Marino 		F_CLR(clp, CL_IN_EX);
119e0b8e63eSJohn Marino 		F_SET(clp, CL_SCR_VI_INIT);
120e0b8e63eSJohn Marino 	}
121e0b8e63eSJohn Marino 	return (0);
122e0b8e63eSJohn Marino }
123e0b8e63eSJohn Marino 
124e0b8e63eSJohn Marino /*
125e0b8e63eSJohn Marino  * cl_quit --
126e0b8e63eSJohn Marino  *	Shutdown the screens.
127e0b8e63eSJohn Marino  *
128e0b8e63eSJohn Marino  * PUBLIC: int cl_quit(GS *);
129e0b8e63eSJohn Marino  */
130e0b8e63eSJohn Marino int
cl_quit(GS * gp)131e0b8e63eSJohn Marino cl_quit(GS *gp)
132e0b8e63eSJohn Marino {
133e0b8e63eSJohn Marino 	CL_PRIVATE *clp;
134e0b8e63eSJohn Marino 	int rval;
135e0b8e63eSJohn Marino 
136e0b8e63eSJohn Marino 	rval = 0;
137e0b8e63eSJohn Marino 	clp = GCLP(gp);
138e0b8e63eSJohn Marino 
139e0b8e63eSJohn Marino 	/*
140e0b8e63eSJohn Marino 	 * If we weren't really running, ignore it.  This happens if the
141e0b8e63eSJohn Marino 	 * screen changes size before we've called curses.
142e0b8e63eSJohn Marino 	 */
143e0b8e63eSJohn Marino 	if (!F_ISSET(clp, CL_SCR_EX_INIT | CL_SCR_VI_INIT))
144e0b8e63eSJohn Marino 		return (0);
145e0b8e63eSJohn Marino 
146e0b8e63eSJohn Marino 	/* Clean up the terminal mappings. */
147e0b8e63eSJohn Marino 	if (cl_term_end(gp))
148e0b8e63eSJohn Marino 		rval = 1;
149e0b8e63eSJohn Marino 
150e0b8e63eSJohn Marino 	/* Really leave vi mode. */
151e0b8e63eSJohn Marino 	if (F_ISSET(clp, CL_STDIN_TTY) &&
152e0b8e63eSJohn Marino 	    F_ISSET(clp, CL_SCR_VI_INIT) && cl_vi_end(gp))
153e0b8e63eSJohn Marino 		rval = 1;
154e0b8e63eSJohn Marino 
155e0b8e63eSJohn Marino 	/* Really leave ex mode. */
156e0b8e63eSJohn Marino 	if (F_ISSET(clp, CL_STDIN_TTY) &&
157e0b8e63eSJohn Marino 	    F_ISSET(clp, CL_SCR_EX_INIT) && cl_ex_end(gp))
158e0b8e63eSJohn Marino 		rval = 1;
159e0b8e63eSJohn Marino 
160e0b8e63eSJohn Marino 	/*
161e0b8e63eSJohn Marino 	 * If we were running ex when we quit, or we're using an implementation
162e0b8e63eSJohn Marino 	 * of curses where endwin() doesn't get this right, restore the original
163e0b8e63eSJohn Marino 	 * terminal modes.
164e0b8e63eSJohn Marino 	 *
165e0b8e63eSJohn Marino 	 * XXX
166e0b8e63eSJohn Marino 	 * We always do this because it's too hard to figure out what curses
167e0b8e63eSJohn Marino 	 * implementations get it wrong.  It may discard type-ahead characters
168e0b8e63eSJohn Marino 	 * from the tty queue.
169e0b8e63eSJohn Marino 	 */
170e0b8e63eSJohn Marino 	(void)tcsetattr(STDIN_FILENO, TCSADRAIN | TCSASOFT, &clp->orig);
171e0b8e63eSJohn Marino 
172e0b8e63eSJohn Marino 	F_CLR(clp, CL_SCR_EX_INIT | CL_SCR_VI_INIT);
173e0b8e63eSJohn Marino 	return (rval);
174e0b8e63eSJohn Marino }
175e0b8e63eSJohn Marino 
176e0b8e63eSJohn Marino /*
177e0b8e63eSJohn Marino  * cl_vi_init --
178e0b8e63eSJohn Marino  *	Initialize the curses vi screen.
179e0b8e63eSJohn Marino  */
180e0b8e63eSJohn Marino static int
cl_vi_init(SCR * sp)181e0b8e63eSJohn Marino cl_vi_init(SCR *sp)
182e0b8e63eSJohn Marino {
183e0b8e63eSJohn Marino 	CL_PRIVATE *clp;
184e0b8e63eSJohn Marino 	GS *gp;
185e0b8e63eSJohn Marino 	char *o_cols, *o_lines, *o_term, *ttype;
186e0b8e63eSJohn Marino 
187e0b8e63eSJohn Marino 	gp = sp->gp;
188e0b8e63eSJohn Marino 	clp = CLP(sp);
189e0b8e63eSJohn Marino 
190e0b8e63eSJohn Marino 	/* If already initialized, just set the terminal modes. */
191e0b8e63eSJohn Marino 	if (F_ISSET(clp, CL_SCR_VI_INIT))
192e0b8e63eSJohn Marino 		goto fast;
193e0b8e63eSJohn Marino 
194e0b8e63eSJohn Marino 	/* Curses vi always reads from (and writes to) a terminal. */
195e0b8e63eSJohn Marino 	if (!F_ISSET(clp, CL_STDIN_TTY) || !isatty(STDOUT_FILENO)) {
196e0b8e63eSJohn Marino 		msgq(sp, M_ERR,
197e0b8e63eSJohn Marino 		    "016|Vi's standard input and output must be a terminal");
198e0b8e63eSJohn Marino 		return (1);
199e0b8e63eSJohn Marino 	}
200e0b8e63eSJohn Marino 
201e0b8e63eSJohn Marino 	/* We'll need a terminal type. */
202e0b8e63eSJohn Marino 	if (opts_empty(sp, O_TERM, 0))
203e0b8e63eSJohn Marino 		return (1);
204e0b8e63eSJohn Marino 	ttype = O_STR(sp, O_TERM);
205e0b8e63eSJohn Marino 
206e0b8e63eSJohn Marino 	/*
207e0b8e63eSJohn Marino 	 * XXX
208e0b8e63eSJohn Marino 	 * Changing the row/column and terminal values is done by putting them
209e0b8e63eSJohn Marino 	 * into the environment, which is then read by curses.  What this loses
210e0b8e63eSJohn Marino 	 * in ugliness, it makes up for in stupidity.  We can't simply put the
211e0b8e63eSJohn Marino 	 * values into the environment ourselves, because in the presence of a
212e0b8e63eSJohn Marino 	 * kernel mechanism for returning the window size, entering values into
213e0b8e63eSJohn Marino 	 * the environment will screw up future screen resizing events, e.g. if
214e0b8e63eSJohn Marino 	 * the user enters a :shell command and then resizes their window.  So,
215e0b8e63eSJohn Marino 	 * if they weren't already in the environment, we make sure to delete
216e0b8e63eSJohn Marino 	 * them immediately after setting them.
217e0b8e63eSJohn Marino 	 *
218e0b8e63eSJohn Marino 	 * XXX
219e0b8e63eSJohn Marino 	 * Putting the TERM variable into the environment is necessary, even
220e0b8e63eSJohn Marino 	 * though we're using newterm() here.  We may be using initscr() as
221e0b8e63eSJohn Marino 	 * the underlying function.
222e0b8e63eSJohn Marino 	 */
223e0b8e63eSJohn Marino 	o_term = getenv("TERM");
224e0b8e63eSJohn Marino 	cl_putenv("TERM", ttype, 0);
225e0b8e63eSJohn Marino 	o_lines = getenv("LINES");
226e0b8e63eSJohn Marino 	cl_putenv("LINES", NULL, (u_long)O_VAL(sp, O_LINES));
227e0b8e63eSJohn Marino 	o_cols = getenv("COLUMNS");
228e0b8e63eSJohn Marino 	cl_putenv("COLUMNS", NULL, (u_long)O_VAL(sp, O_COLUMNS));
229e0b8e63eSJohn Marino 
230e0b8e63eSJohn Marino 	/*
231e0b8e63eSJohn Marino 	 * The terminal is aways initialized, either in `main`, or by a
232e0b8e63eSJohn Marino 	 * previous call to newterm(3X).
233e0b8e63eSJohn Marino 	 */
234e0b8e63eSJohn Marino 	(void)del_curterm(cur_term);
235e0b8e63eSJohn Marino 
236e0b8e63eSJohn Marino 	/*
237e0b8e63eSJohn Marino 	 * We never have more than one SCREEN at a time, so set_term(NULL) will
238e0b8e63eSJohn Marino 	 * give us the last SCREEN.
239e0b8e63eSJohn Marino 	 */
240e0b8e63eSJohn Marino 	errno = 0;
241e0b8e63eSJohn Marino 	if (newterm(ttype, stdout, stdin) == NULL) {
242e0b8e63eSJohn Marino 		if (errno)
243e0b8e63eSJohn Marino 			msgq(sp, M_SYSERR, "%s", ttype);
244e0b8e63eSJohn Marino 		else
245e0b8e63eSJohn Marino 			msgq(sp, M_ERR, "%s: unknown terminal type", ttype);
246e0b8e63eSJohn Marino 		return (1);
247e0b8e63eSJohn Marino 	}
248e0b8e63eSJohn Marino 
249e0b8e63eSJohn Marino 	if (o_term == NULL)
250e0b8e63eSJohn Marino 		unsetenv("TERM");
251e0b8e63eSJohn Marino 	if (o_lines == NULL)
252e0b8e63eSJohn Marino 		unsetenv("LINES");
253e0b8e63eSJohn Marino 	if (o_cols == NULL)
254e0b8e63eSJohn Marino 		unsetenv("COLUMNS");
255e0b8e63eSJohn Marino 
256e0b8e63eSJohn Marino 	/*
257e0b8e63eSJohn Marino 	 * XXX
258e0b8e63eSJohn Marino 	 * Someone got let out alone without adult supervision -- the SunOS
259e0b8e63eSJohn Marino 	 * newterm resets the signal handlers.  There's a race, but it's not
260e0b8e63eSJohn Marino 	 * worth closing.
261e0b8e63eSJohn Marino 	 */
262e0b8e63eSJohn Marino 	(void)sig_init(sp->gp, sp);
263e0b8e63eSJohn Marino 
264e0b8e63eSJohn Marino 	/*
265e0b8e63eSJohn Marino 	 * We use raw mode.  What we want is 8-bit clean, however, signals
266e0b8e63eSJohn Marino 	 * and flow control should continue to work.  Admittedly, it sounds
267e0b8e63eSJohn Marino 	 * like cbreak, but it isn't.  Using cbreak() can get you additional
268e0b8e63eSJohn Marino 	 * things like IEXTEN, which turns on flags like DISCARD and LNEXT.
269e0b8e63eSJohn Marino 	 *
270e0b8e63eSJohn Marino 	 * !!!
271e0b8e63eSJohn Marino 	 * If raw isn't turning off echo and newlines, something's wrong.
272e0b8e63eSJohn Marino 	 * However, it shouldn't hurt.
273e0b8e63eSJohn Marino 	 */
274e0b8e63eSJohn Marino 	noecho();			/* No character echo. */
275e0b8e63eSJohn Marino 	nonl();				/* No CR/NL translation. */
276e0b8e63eSJohn Marino 	raw();				/* 8-bit clean. */
277e0b8e63eSJohn Marino 	idlok(stdscr, 1);		/* Use hardware insert/delete line. */
278e0b8e63eSJohn Marino 
279e0b8e63eSJohn Marino 	/* Put the cursor keys into application mode. */
280e0b8e63eSJohn Marino 	(void)keypad(stdscr, TRUE);
281e0b8e63eSJohn Marino 
282e0b8e63eSJohn Marino 	/*
283e0b8e63eSJohn Marino 	 * XXX
284e0b8e63eSJohn Marino 	 * The screen TI sequence just got sent.  See the comment in
285e0b8e63eSJohn Marino 	 * cl_funcs.c:cl_attr().
286e0b8e63eSJohn Marino 	 */
287e0b8e63eSJohn Marino 	clp->ti_te = TI_SENT;
288e0b8e63eSJohn Marino 
289e0b8e63eSJohn Marino 	/*
290e0b8e63eSJohn Marino 	 * XXX
291e0b8e63eSJohn Marino 	 * Historic implementations of curses handled SIGTSTP signals
292e0b8e63eSJohn Marino 	 * in one of three ways.  They either:
293e0b8e63eSJohn Marino 	 *
294e0b8e63eSJohn Marino 	 *	1: Set their own handler, regardless.
295e0b8e63eSJohn Marino 	 *	2: Did not set a handler if a handler was already installed.
296e0b8e63eSJohn Marino 	 *	3: Set their own handler, but then called any previously set
297e0b8e63eSJohn Marino 	 *	   handler after completing their own cleanup.
298e0b8e63eSJohn Marino 	 *
299e0b8e63eSJohn Marino 	 * We don't try and figure out which behavior is in place, we force
300e0b8e63eSJohn Marino 	 * it to SIG_DFL after initializing the curses interface, which means
301e0b8e63eSJohn Marino 	 * that curses isn't going to take the signal.  Since curses isn't
302e0b8e63eSJohn Marino 	 * reentrant (i.e., the whole curses SIGTSTP interface is a fantasy),
303e0b8e63eSJohn Marino 	 * we're doing The Right Thing.
304e0b8e63eSJohn Marino 	 */
305e0b8e63eSJohn Marino 	(void)signal(SIGTSTP, SIG_DFL);
306e0b8e63eSJohn Marino 
307e0b8e63eSJohn Marino 	/*
308e0b8e63eSJohn Marino 	 * If flow control was on, turn it back on.  Turn signals on.  ISIG
309e0b8e63eSJohn Marino 	 * turns on VINTR, VQUIT, VDSUSP and VSUSP.   The main curses code
310e0b8e63eSJohn Marino 	 * already installed a handler for VINTR.  We're going to disable the
311e0b8e63eSJohn Marino 	 * other three.
312e0b8e63eSJohn Marino 	 *
313e0b8e63eSJohn Marino 	 * XXX
314e0b8e63eSJohn Marino 	 * We want to use ^Y as a vi scrolling command.  If the user has the
315e0b8e63eSJohn Marino 	 * DSUSP character set to ^Y (common practice) clean it up.  As it's
316e0b8e63eSJohn Marino 	 * equally possible that the user has VDSUSP set to 'a', we disable
317e0b8e63eSJohn Marino 	 * it regardless.  It doesn't make much sense to suspend vi at read,
318e0b8e63eSJohn Marino 	 * so I don't think anyone will care.  Alternatively, we could look
319e0b8e63eSJohn Marino 	 * it up in the table of legal command characters and turn it off if
320e0b8e63eSJohn Marino 	 * it matches one.  VDSUSP wasn't in POSIX 1003.1-1990, so we test for
321e0b8e63eSJohn Marino 	 * it.
322e0b8e63eSJohn Marino 	 *
323e0b8e63eSJohn Marino 	 * XXX
324e0b8e63eSJohn Marino 	 * We don't check to see if the user had signals enabled originally.
325e0b8e63eSJohn Marino 	 * If they didn't, it's unclear what we're supposed to do here, but
326e0b8e63eSJohn Marino 	 * it's also pretty unlikely.
327e0b8e63eSJohn Marino 	 */
328e0b8e63eSJohn Marino 	if (tcgetattr(STDIN_FILENO, &clp->vi_enter)) {
329e0b8e63eSJohn Marino 		msgq(sp, M_SYSERR, "tcgetattr");
330e0b8e63eSJohn Marino 		goto err;
331e0b8e63eSJohn Marino 	}
332e0b8e63eSJohn Marino 	if (clp->orig.c_iflag & IXON)
333e0b8e63eSJohn Marino 		clp->vi_enter.c_iflag |= IXON;
334e0b8e63eSJohn Marino 	if (clp->orig.c_iflag & IXOFF)
335e0b8e63eSJohn Marino 		clp->vi_enter.c_iflag |= IXOFF;
336e0b8e63eSJohn Marino 
337e0b8e63eSJohn Marino 	clp->vi_enter.c_lflag |= ISIG;
338e0b8e63eSJohn Marino #ifdef VDSUSP
339e0b8e63eSJohn Marino 	clp->vi_enter.c_cc[VDSUSP] = _POSIX_VDISABLE;
340e0b8e63eSJohn Marino #endif
341e0b8e63eSJohn Marino 	clp->vi_enter.c_cc[VQUIT] = _POSIX_VDISABLE;
342e0b8e63eSJohn Marino 	clp->vi_enter.c_cc[VSUSP] = _POSIX_VDISABLE;
343e0b8e63eSJohn Marino 
344e0b8e63eSJohn Marino 	/*
345e0b8e63eSJohn Marino 	 * XXX
346e0b8e63eSJohn Marino 	 * OSF/1 doesn't turn off the <discard>, <literal-next> or <status>
347e0b8e63eSJohn Marino 	 * characters when curses switches into raw mode.  It should be OK
348e0b8e63eSJohn Marino 	 * to do it explicitly for everyone.
349e0b8e63eSJohn Marino 	 */
350e0b8e63eSJohn Marino #ifdef VDISCARD
351e0b8e63eSJohn Marino 	clp->vi_enter.c_cc[VDISCARD] = _POSIX_VDISABLE;
352e0b8e63eSJohn Marino #endif
353e0b8e63eSJohn Marino #ifdef VLNEXT
354e0b8e63eSJohn Marino 	clp->vi_enter.c_cc[VLNEXT] = _POSIX_VDISABLE;
355e0b8e63eSJohn Marino #endif
356e0b8e63eSJohn Marino #ifdef VSTATUS
357e0b8e63eSJohn Marino 	clp->vi_enter.c_cc[VSTATUS] = _POSIX_VDISABLE;
358e0b8e63eSJohn Marino #endif
359e0b8e63eSJohn Marino 
360e0b8e63eSJohn Marino 	/* Initialize terminal based information. */
361e0b8e63eSJohn Marino 	if (cl_term_init(sp))
362e0b8e63eSJohn Marino 		goto err;
363e0b8e63eSJohn Marino 
364e0b8e63eSJohn Marino fast:	/* Set the terminal modes. */
365e0b8e63eSJohn Marino 	if (tcsetattr(STDIN_FILENO, TCSASOFT | TCSADRAIN, &clp->vi_enter)) {
366e0b8e63eSJohn Marino 		if (errno == EINTR)
367e0b8e63eSJohn Marino 			goto fast;
368e0b8e63eSJohn Marino 		msgq(sp, M_SYSERR, "tcsetattr");
369e0b8e63eSJohn Marino err:		(void)cl_vi_end(sp->gp);
370e0b8e63eSJohn Marino 		return (1);
371e0b8e63eSJohn Marino 	}
372e0b8e63eSJohn Marino 	return (0);
373e0b8e63eSJohn Marino }
374e0b8e63eSJohn Marino 
375e0b8e63eSJohn Marino /*
376e0b8e63eSJohn Marino  * cl_vi_end --
377e0b8e63eSJohn Marino  *	Shutdown the vi screen.
378e0b8e63eSJohn Marino  */
379e0b8e63eSJohn Marino static int
cl_vi_end(GS * gp)380e0b8e63eSJohn Marino cl_vi_end(GS *gp)
381e0b8e63eSJohn Marino {
382e0b8e63eSJohn Marino 	CL_PRIVATE *clp;
383e0b8e63eSJohn Marino 
384e0b8e63eSJohn Marino 	clp = GCLP(gp);
385e0b8e63eSJohn Marino 
386e0b8e63eSJohn Marino 	/* Restore the cursor keys to normal mode. */
387e0b8e63eSJohn Marino 	(void)keypad(stdscr, FALSE);
388e0b8e63eSJohn Marino 
389e0b8e63eSJohn Marino 	/*
390e0b8e63eSJohn Marino 	 * If we were running vi when we quit, scroll the screen up a single
391e0b8e63eSJohn Marino 	 * line so we don't lose any information.
392e0b8e63eSJohn Marino 	 *
393e0b8e63eSJohn Marino 	 * Move to the bottom of the window (some endwin implementations don't
394e0b8e63eSJohn Marino 	 * do this for you).
395e0b8e63eSJohn Marino 	 */
396e0b8e63eSJohn Marino 	if (!F_ISSET(clp, CL_IN_EX)) {
397e0b8e63eSJohn Marino 		(void)move(0, 0);
398e0b8e63eSJohn Marino 		(void)deleteln();
399e0b8e63eSJohn Marino 		(void)move(LINES - 1, 0);
400e0b8e63eSJohn Marino 		(void)refresh();
401e0b8e63eSJohn Marino 	}
402e0b8e63eSJohn Marino 
403e0b8e63eSJohn Marino 	cl_freecap(clp);
404e0b8e63eSJohn Marino 
405e0b8e63eSJohn Marino 	/* End curses window. */
406e0b8e63eSJohn Marino 	(void)endwin();
407e0b8e63eSJohn Marino 
408e0b8e63eSJohn Marino 	/* Free the SCREEN created by newterm(3X). */
409e0b8e63eSJohn Marino 	delscreen(set_term(NULL));
410e0b8e63eSJohn Marino 
411e0b8e63eSJohn Marino 	/*
412e0b8e63eSJohn Marino 	 * XXX
413e0b8e63eSJohn Marino 	 * The screen TE sequence just got sent.  See the comment in
414e0b8e63eSJohn Marino 	 * cl_funcs.c:cl_attr().
415e0b8e63eSJohn Marino 	 */
416e0b8e63eSJohn Marino 	clp->ti_te = TE_SENT;
417e0b8e63eSJohn Marino 
418e0b8e63eSJohn Marino 	return (0);
419e0b8e63eSJohn Marino }
420e0b8e63eSJohn Marino 
421e0b8e63eSJohn Marino /*
422e0b8e63eSJohn Marino  * cl_ex_init --
423e0b8e63eSJohn Marino  *	Initialize the ex screen.
424e0b8e63eSJohn Marino  */
425e0b8e63eSJohn Marino static int
cl_ex_init(SCR * sp)426e0b8e63eSJohn Marino cl_ex_init(SCR *sp)
427e0b8e63eSJohn Marino {
428e0b8e63eSJohn Marino 	CL_PRIVATE *clp;
429e0b8e63eSJohn Marino 
430e0b8e63eSJohn Marino 	clp = CLP(sp);
431e0b8e63eSJohn Marino 
432e0b8e63eSJohn Marino 	/* If already initialized, just set the terminal modes. */
433e0b8e63eSJohn Marino 	if (F_ISSET(clp, CL_SCR_EX_INIT))
434e0b8e63eSJohn Marino 		goto fast;
435e0b8e63eSJohn Marino 
436e0b8e63eSJohn Marino 	/* If not reading from a file, we're done. */
437e0b8e63eSJohn Marino 	if (!F_ISSET(clp, CL_STDIN_TTY))
438e0b8e63eSJohn Marino 		return (0);
439e0b8e63eSJohn Marino 
440e0b8e63eSJohn Marino 	/* Get the ex termcap/terminfo strings. */
441e0b8e63eSJohn Marino 	(void)cl_getcap(sp, "cup", &clp->cup);
442e0b8e63eSJohn Marino 	(void)cl_getcap(sp, "smso", &clp->smso);
443e0b8e63eSJohn Marino 	(void)cl_getcap(sp, "rmso", &clp->rmso);
444e0b8e63eSJohn Marino 	(void)cl_getcap(sp, "el", &clp->el);
445e0b8e63eSJohn Marino 	(void)cl_getcap(sp, "cuu1", &clp->cuu1);
446e0b8e63eSJohn Marino 
447e0b8e63eSJohn Marino 	/* Enter_standout_mode and exit_standout_mode are paired. */
448e0b8e63eSJohn Marino 	if (clp->smso == NULL || clp->rmso == NULL) {
449e0b8e63eSJohn Marino 		free(clp->smso);
450e0b8e63eSJohn Marino 		clp->smso = NULL;
451*b1ac2ebbSDaniel Fojt 
452e0b8e63eSJohn Marino 		free(clp->rmso);
453e0b8e63eSJohn Marino 		clp->rmso = NULL;
454e0b8e63eSJohn Marino 	}
455e0b8e63eSJohn Marino 
456e0b8e63eSJohn Marino 	/*
457e0b8e63eSJohn Marino 	 * Turn on canonical mode, with normal input and output processing.
458e0b8e63eSJohn Marino 	 * Start with the original terminal settings as the user probably
459e0b8e63eSJohn Marino 	 * had them (including any local extensions) set correctly for the
460e0b8e63eSJohn Marino 	 * current terminal.
461e0b8e63eSJohn Marino 	 *
462e0b8e63eSJohn Marino 	 * !!!
463e0b8e63eSJohn Marino 	 * We can't get everything that we need portably; for example, ONLCR,
464e0b8e63eSJohn Marino 	 * mapping <newline> to <carriage-return> on output isn't required
465e0b8e63eSJohn Marino 	 * by POSIX 1003.1b-1993.  If this turns out to be a problem, then
466e0b8e63eSJohn Marino 	 * we'll either have to play some games on the mapping, or we'll have
467e0b8e63eSJohn Marino 	 * to make all ex printf's output \r\n instead of \n.
468e0b8e63eSJohn Marino 	 */
469e0b8e63eSJohn Marino 	clp->ex_enter = clp->orig;
470e0b8e63eSJohn Marino 	clp->ex_enter.c_lflag  |= ECHO | ECHOE | ECHOK | ICANON | IEXTEN | ISIG;
471e0b8e63eSJohn Marino #ifdef ECHOCTL
472e0b8e63eSJohn Marino 	clp->ex_enter.c_lflag |= ECHOCTL;
473e0b8e63eSJohn Marino #endif
474e0b8e63eSJohn Marino #ifdef ECHOKE
475e0b8e63eSJohn Marino 	clp->ex_enter.c_lflag |= ECHOKE;
476e0b8e63eSJohn Marino #endif
477e0b8e63eSJohn Marino 	clp->ex_enter.c_iflag |= ICRNL;
478e0b8e63eSJohn Marino 	clp->ex_enter.c_oflag |= OPOST;
479e0b8e63eSJohn Marino #ifdef ONLCR
480e0b8e63eSJohn Marino 	clp->ex_enter.c_oflag |= ONLCR;
481e0b8e63eSJohn Marino #endif
482e0b8e63eSJohn Marino 
483e0b8e63eSJohn Marino fast:	if (tcsetattr(STDIN_FILENO, TCSADRAIN | TCSASOFT, &clp->ex_enter)) {
484e0b8e63eSJohn Marino 		if (errno == EINTR)
485e0b8e63eSJohn Marino 			goto fast;
486e0b8e63eSJohn Marino 		msgq(sp, M_SYSERR, "tcsetattr");
487e0b8e63eSJohn Marino 		return (1);
488e0b8e63eSJohn Marino 	}
489e0b8e63eSJohn Marino 	return (0);
490e0b8e63eSJohn Marino }
491e0b8e63eSJohn Marino 
492e0b8e63eSJohn Marino /*
493e0b8e63eSJohn Marino  * cl_ex_end --
494e0b8e63eSJohn Marino  *	Shutdown the ex screen.
495e0b8e63eSJohn Marino  */
496e0b8e63eSJohn Marino static int
cl_ex_end(GS * gp)497e0b8e63eSJohn Marino cl_ex_end(GS *gp)
498e0b8e63eSJohn Marino {
499e0b8e63eSJohn Marino 	CL_PRIVATE *clp;
500e0b8e63eSJohn Marino 
501e0b8e63eSJohn Marino 	clp = GCLP(gp);
502e0b8e63eSJohn Marino 
503e0b8e63eSJohn Marino 	cl_freecap(clp);
504e0b8e63eSJohn Marino 
505e0b8e63eSJohn Marino 	return (0);
506e0b8e63eSJohn Marino }
507e0b8e63eSJohn Marino 
508e0b8e63eSJohn Marino /*
509e0b8e63eSJohn Marino  * cl_getcap --
510e0b8e63eSJohn Marino  *	Retrieve termcap/terminfo strings.
511e0b8e63eSJohn Marino  *
512e0b8e63eSJohn Marino  * PUBLIC: int cl_getcap(SCR *, char *, char **);
513e0b8e63eSJohn Marino  */
514e0b8e63eSJohn Marino int
cl_getcap(SCR * sp,char * name,char ** elementp)515e0b8e63eSJohn Marino cl_getcap(SCR *sp, char *name, char **elementp)
516e0b8e63eSJohn Marino {
517e0b8e63eSJohn Marino 	size_t len;
518e0b8e63eSJohn Marino 	char *t;
519e0b8e63eSJohn Marino 
520e0b8e63eSJohn Marino 	if ((t = tigetstr(name)) != NULL &&
521e0b8e63eSJohn Marino 	    t != (char *)-1 && (len = strlen(t)) != 0) {
522*b1ac2ebbSDaniel Fojt 		MALLOC_RET(sp, *elementp, len + 1);
523e0b8e63eSJohn Marino 		memmove(*elementp, t, len + 1);
524e0b8e63eSJohn Marino 	}
525e0b8e63eSJohn Marino 	return (0);
526e0b8e63eSJohn Marino }
527e0b8e63eSJohn Marino 
528e0b8e63eSJohn Marino /*
529e0b8e63eSJohn Marino  * cl_freecap --
530e0b8e63eSJohn Marino  *	Free any allocated termcap/terminfo strings.
531e0b8e63eSJohn Marino  */
532e0b8e63eSJohn Marino static void
cl_freecap(CL_PRIVATE * clp)533e0b8e63eSJohn Marino cl_freecap(CL_PRIVATE *clp)
534e0b8e63eSJohn Marino {
535e0b8e63eSJohn Marino 	free(clp->el);
536e0b8e63eSJohn Marino 	clp->el = NULL;
537*b1ac2ebbSDaniel Fojt 
538e0b8e63eSJohn Marino 	free(clp->cup);
539e0b8e63eSJohn Marino 	clp->cup = NULL;
540*b1ac2ebbSDaniel Fojt 
541e0b8e63eSJohn Marino 	free(clp->cuu1);
542e0b8e63eSJohn Marino 	clp->cuu1 = NULL;
543*b1ac2ebbSDaniel Fojt 
544e0b8e63eSJohn Marino 	free(clp->rmso);
545e0b8e63eSJohn Marino 	clp->rmso = NULL;
546*b1ac2ebbSDaniel Fojt 
547e0b8e63eSJohn Marino 	free(clp->smso);
548e0b8e63eSJohn Marino 	clp->smso = NULL;
549*b1ac2ebbSDaniel Fojt 
550e0b8e63eSJohn Marino 	/* Required by libcursesw :) */
551e0b8e63eSJohn Marino 	free(clp->cw.bp1.c);
552e0b8e63eSJohn Marino 	clp->cw.bp1.c = NULL;
553e0b8e63eSJohn Marino 	clp->cw.blen1 = 0;
554e0b8e63eSJohn Marino }
555e0b8e63eSJohn Marino 
556e0b8e63eSJohn Marino /*
557e0b8e63eSJohn Marino  * cl_putenv --
558e0b8e63eSJohn Marino  *	Put a value into the environment.
559e0b8e63eSJohn Marino  */
560e0b8e63eSJohn Marino static int
cl_putenv(char * name,char * str,u_long value)561e0b8e63eSJohn Marino cl_putenv(char *name, char *str, u_long value)
562e0b8e63eSJohn Marino {
563e0b8e63eSJohn Marino 	char buf[40];
564e0b8e63eSJohn Marino 
565e0b8e63eSJohn Marino 	if (str == NULL) {
566e0b8e63eSJohn Marino 		(void)snprintf(buf, sizeof(buf), "%lu", value);
567e0b8e63eSJohn Marino 		return (setenv(name, buf, 1));
568e0b8e63eSJohn Marino 	} else
569e0b8e63eSJohn Marino 		return (setenv(name, str, 1));
570e0b8e63eSJohn Marino }
571