1*0a6a1f1dSLionel Sambuc /* $NetBSD: cl_screen.c,v 1.4 2014/01/26 21:43:45 christos Exp $ */
284d9c625SLionel Sambuc /*-
384d9c625SLionel Sambuc * Copyright (c) 1993, 1994
484d9c625SLionel Sambuc * The Regents of the University of California. All rights reserved.
584d9c625SLionel Sambuc * Copyright (c) 1993, 1994, 1995, 1996
684d9c625SLionel Sambuc * Keith Bostic. All rights reserved.
784d9c625SLionel Sambuc *
884d9c625SLionel Sambuc * See the LICENSE file for redistribution information.
984d9c625SLionel Sambuc */
1084d9c625SLionel Sambuc
1184d9c625SLionel Sambuc #include "config.h"
1284d9c625SLionel Sambuc
13*0a6a1f1dSLionel Sambuc #include <sys/cdefs.h>
14*0a6a1f1dSLionel Sambuc #if 0
1584d9c625SLionel Sambuc #ifndef lint
1684d9c625SLionel Sambuc static const char sccsid[] = "Id: cl_screen.c,v 10.56 2002/05/03 19:59:44 skimo Exp (Berkeley) Date: 2002/05/03 19:59:44 ";
1784d9c625SLionel Sambuc #endif /* not lint */
18*0a6a1f1dSLionel Sambuc #else
19*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: cl_screen.c,v 1.4 2014/01/26 21:43:45 christos Exp $");
20*0a6a1f1dSLionel Sambuc #endif
2184d9c625SLionel Sambuc
2284d9c625SLionel Sambuc #include <sys/types.h>
2384d9c625SLionel Sambuc #include <sys/queue.h>
2484d9c625SLionel Sambuc
2584d9c625SLionel Sambuc #include <bitstring.h>
2684d9c625SLionel Sambuc #include <errno.h>
2784d9c625SLionel Sambuc #include <signal.h>
2884d9c625SLionel Sambuc #include <stdio.h>
2984d9c625SLionel Sambuc #include <stdlib.h>
3084d9c625SLionel Sambuc #include <string.h>
3184d9c625SLionel Sambuc #include <termios.h>
3284d9c625SLionel Sambuc #include <unistd.h>
3384d9c625SLionel Sambuc
3484d9c625SLionel Sambuc #include "../common/common.h"
3584d9c625SLionel Sambuc #include "cl.h"
3684d9c625SLionel Sambuc
3784d9c625SLionel Sambuc static int cl_ex_end __P((GS *));
3884d9c625SLionel Sambuc static int cl_ex_init __P((SCR *));
3984d9c625SLionel Sambuc static void cl_freecap __P((CL_PRIVATE *));
4084d9c625SLionel Sambuc static int cl_vi_end __P((GS *));
4184d9c625SLionel Sambuc static int cl_vi_init __P((SCR *));
4284d9c625SLionel Sambuc static int cl_putenv __P((SCR *, const char *, const char *, u_long));
4384d9c625SLionel Sambuc
4484d9c625SLionel Sambuc /*
4584d9c625SLionel Sambuc * cl_screen --
4684d9c625SLionel Sambuc * Switch screen types.
4784d9c625SLionel Sambuc *
4884d9c625SLionel Sambuc * PUBLIC: int cl_screen __P((SCR *, u_int32_t));
4984d9c625SLionel Sambuc */
5084d9c625SLionel Sambuc int
cl_screen(SCR * sp,u_int32_t flags)5184d9c625SLionel Sambuc cl_screen(SCR *sp, u_int32_t flags)
5284d9c625SLionel Sambuc {
5384d9c625SLionel Sambuc CL_PRIVATE *clp;
5484d9c625SLionel Sambuc WINDOW *win;
5584d9c625SLionel Sambuc GS *gp;
5684d9c625SLionel Sambuc
5784d9c625SLionel Sambuc gp = sp->gp;
5884d9c625SLionel Sambuc clp = CLP(sp);
5984d9c625SLionel Sambuc win = CLSP(sp) ? CLSP(sp) : stdscr;
6084d9c625SLionel Sambuc
6184d9c625SLionel Sambuc /* See if the current information is incorrect. */
6284d9c625SLionel Sambuc if (F_ISSET(gp, G_SRESTART)) {
6384d9c625SLionel Sambuc if (CLSP(sp)) {
6484d9c625SLionel Sambuc delwin(CLSP(sp));
6584d9c625SLionel Sambuc sp->cl_private = NULL;
6684d9c625SLionel Sambuc }
6784d9c625SLionel Sambuc if (cl_quit(gp))
6884d9c625SLionel Sambuc return (1);
6984d9c625SLionel Sambuc F_CLR(gp, G_SRESTART);
7084d9c625SLionel Sambuc }
7184d9c625SLionel Sambuc
7284d9c625SLionel Sambuc /* See if we're already in the right mode. */
7384d9c625SLionel Sambuc if ((LF_ISSET(SC_EX) && F_ISSET(sp, SC_SCR_EX)) ||
7484d9c625SLionel Sambuc (LF_ISSET(SC_VI) && F_ISSET(sp, SC_SCR_VI)))
7584d9c625SLionel Sambuc return (0);
7684d9c625SLionel Sambuc
7784d9c625SLionel Sambuc /*
7884d9c625SLionel Sambuc * Fake leaving ex mode.
7984d9c625SLionel Sambuc *
8084d9c625SLionel Sambuc * We don't actually exit ex or vi mode unless forced (e.g. by a window
8184d9c625SLionel Sambuc * size change). This is because many curses implementations can't be
8284d9c625SLionel Sambuc * called twice in a single program. Plus, it's faster. If the editor
8384d9c625SLionel Sambuc * "leaves" vi to enter ex, when it exits ex we'll just fall back into
8484d9c625SLionel Sambuc * vi.
8584d9c625SLionel Sambuc */
8684d9c625SLionel Sambuc if (F_ISSET(sp, SC_SCR_EX))
8784d9c625SLionel Sambuc F_CLR(sp, SC_SCR_EX);
8884d9c625SLionel Sambuc
8984d9c625SLionel Sambuc /*
9084d9c625SLionel Sambuc * Fake leaving vi mode.
9184d9c625SLionel Sambuc *
9284d9c625SLionel Sambuc * Clear out the rest of the screen if we're in the middle of a split
9384d9c625SLionel Sambuc * screen. Move to the last line in the current screen -- this makes
9484d9c625SLionel Sambuc * terminal scrolling happen naturally. Note: *don't* move past the
9584d9c625SLionel Sambuc * end of the screen, as there are ex commands (e.g., :read ! cat file)
9684d9c625SLionel Sambuc * that don't want to. Don't clear the info line, its contents may be
9784d9c625SLionel Sambuc * valid, e.g. :file|append.
9884d9c625SLionel Sambuc */
9984d9c625SLionel Sambuc if (F_ISSET(sp, SC_SCR_VI)) {
10084d9c625SLionel Sambuc F_CLR(sp, SC_SCR_VI);
10184d9c625SLionel Sambuc
10284d9c625SLionel Sambuc if (TAILQ_NEXT(sp, q) != NULL) {
10384d9c625SLionel Sambuc (void)wmove(win, RLNO(sp, sp->rows), 0);
10484d9c625SLionel Sambuc wclrtobot(win);
10584d9c625SLionel Sambuc }
10684d9c625SLionel Sambuc (void)wmove(win, RLNO(sp, sp->rows) - 1, 0);
10784d9c625SLionel Sambuc wrefresh(win);
10884d9c625SLionel Sambuc }
10984d9c625SLionel Sambuc
11084d9c625SLionel Sambuc /* Enter the requested mode. */
11184d9c625SLionel Sambuc if (LF_ISSET(SC_EX)) {
11284d9c625SLionel Sambuc if (cl_ex_init(sp))
11384d9c625SLionel Sambuc return (1);
11484d9c625SLionel Sambuc F_SET(clp, CL_IN_EX | CL_SCR_EX_INIT);
11584d9c625SLionel Sambuc
11684d9c625SLionel Sambuc /*
11784d9c625SLionel Sambuc * If doing an ex screen for ex mode, move to the last line
11884d9c625SLionel Sambuc * on the screen.
11984d9c625SLionel Sambuc */
12084d9c625SLionel Sambuc if (F_ISSET(sp, SC_EX) && clp->cup != NULL)
12184d9c625SLionel Sambuc tputs(tgoto(clp->cup,
12284d9c625SLionel Sambuc 0, O_VAL(sp, O_LINES) - 1), 1, cl_putchar);
12384d9c625SLionel Sambuc } else {
12484d9c625SLionel Sambuc if (cl_vi_init(sp))
12584d9c625SLionel Sambuc return (1);
12684d9c625SLionel Sambuc F_CLR(clp, CL_IN_EX);
12784d9c625SLionel Sambuc F_SET(clp, CL_SCR_VI_INIT);
12884d9c625SLionel Sambuc }
12984d9c625SLionel Sambuc return (0);
13084d9c625SLionel Sambuc }
13184d9c625SLionel Sambuc
13284d9c625SLionel Sambuc /*
13384d9c625SLionel Sambuc * cl_quit --
13484d9c625SLionel Sambuc * Shutdown the screens.
13584d9c625SLionel Sambuc *
13684d9c625SLionel Sambuc * PUBLIC: int cl_quit __P((GS *));
13784d9c625SLionel Sambuc */
13884d9c625SLionel Sambuc int
cl_quit(GS * gp)13984d9c625SLionel Sambuc cl_quit(GS *gp)
14084d9c625SLionel Sambuc {
14184d9c625SLionel Sambuc CL_PRIVATE *clp;
14284d9c625SLionel Sambuc int rval;
14384d9c625SLionel Sambuc
14484d9c625SLionel Sambuc rval = 0;
14584d9c625SLionel Sambuc clp = GCLP(gp);
14684d9c625SLionel Sambuc
14784d9c625SLionel Sambuc /*
14884d9c625SLionel Sambuc * If we weren't really running, ignore it. This happens if the
14984d9c625SLionel Sambuc * screen changes size before we've called curses.
15084d9c625SLionel Sambuc */
15184d9c625SLionel Sambuc if (!F_ISSET(clp, CL_SCR_EX_INIT | CL_SCR_VI_INIT))
15284d9c625SLionel Sambuc return (0);
15384d9c625SLionel Sambuc
15484d9c625SLionel Sambuc /* Clean up the terminal mappings. */
15584d9c625SLionel Sambuc if (cl_term_end(gp))
15684d9c625SLionel Sambuc rval = 1;
15784d9c625SLionel Sambuc
15884d9c625SLionel Sambuc /* Really leave vi mode. */
15984d9c625SLionel Sambuc if (F_ISSET(clp, CL_STDIN_TTY) &&
16084d9c625SLionel Sambuc F_ISSET(clp, CL_SCR_VI_INIT) && cl_vi_end(gp))
16184d9c625SLionel Sambuc rval = 1;
16284d9c625SLionel Sambuc
16384d9c625SLionel Sambuc /* Really leave ex mode. */
16484d9c625SLionel Sambuc if (F_ISSET(clp, CL_STDIN_TTY) &&
16584d9c625SLionel Sambuc F_ISSET(clp, CL_SCR_EX_INIT) && cl_ex_end(gp))
16684d9c625SLionel Sambuc rval = 1;
16784d9c625SLionel Sambuc
16884d9c625SLionel Sambuc /*
16984d9c625SLionel Sambuc * If we were running ex when we quit, or we're using an implementation
17084d9c625SLionel Sambuc * of curses where endwin() doesn't get this right, restore the original
17184d9c625SLionel Sambuc * terminal modes.
17284d9c625SLionel Sambuc *
17384d9c625SLionel Sambuc * XXX
17484d9c625SLionel Sambuc * We always do this because it's too hard to figure out what curses
17584d9c625SLionel Sambuc * implementations get it wrong. It may discard type-ahead characters
17684d9c625SLionel Sambuc * from the tty queue.
17784d9c625SLionel Sambuc */
17884d9c625SLionel Sambuc (void)tcsetattr(STDIN_FILENO, TCSADRAIN | TCSASOFT, &clp->orig);
17984d9c625SLionel Sambuc
18084d9c625SLionel Sambuc F_CLR(clp, CL_SCR_EX_INIT | CL_SCR_VI_INIT);
18184d9c625SLionel Sambuc return (rval);
18284d9c625SLionel Sambuc }
18384d9c625SLionel Sambuc
18484d9c625SLionel Sambuc /*
18584d9c625SLionel Sambuc * cl_vi_init --
18684d9c625SLionel Sambuc * Initialize the curses vi screen.
18784d9c625SLionel Sambuc */
18884d9c625SLionel Sambuc static int
cl_vi_init(SCR * sp)18984d9c625SLionel Sambuc cl_vi_init(SCR *sp)
19084d9c625SLionel Sambuc {
19184d9c625SLionel Sambuc CL_PRIVATE *clp;
19284d9c625SLionel Sambuc char *o_cols, *o_lines, *o_term;
19384d9c625SLionel Sambuc const char *ttype;
19484d9c625SLionel Sambuc
19584d9c625SLionel Sambuc clp = CLP(sp);
19684d9c625SLionel Sambuc
19784d9c625SLionel Sambuc /* If already initialized, just set the terminal modes. */
19884d9c625SLionel Sambuc if (F_ISSET(clp, CL_SCR_VI_INIT))
19984d9c625SLionel Sambuc goto fast;
20084d9c625SLionel Sambuc
20184d9c625SLionel Sambuc /* Curses vi always reads from (and writes to) a terminal. */
20284d9c625SLionel Sambuc if (!F_ISSET(clp, CL_STDIN_TTY) || !isatty(STDOUT_FILENO)) {
20384d9c625SLionel Sambuc msgq(sp, M_ERR,
20484d9c625SLionel Sambuc "016|Vi's standard input and output must be a terminal");
20584d9c625SLionel Sambuc return (1);
20684d9c625SLionel Sambuc }
20784d9c625SLionel Sambuc
20884d9c625SLionel Sambuc /* We'll need a terminal type. */
20984d9c625SLionel Sambuc if (opts_empty(sp, O_TERM, 0))
21084d9c625SLionel Sambuc return (1);
21184d9c625SLionel Sambuc ttype = O_STR(sp, O_TERM);
21284d9c625SLionel Sambuc
21384d9c625SLionel Sambuc /*
21484d9c625SLionel Sambuc * XXX
21584d9c625SLionel Sambuc * Changing the row/column and terminal values is done by putting them
21684d9c625SLionel Sambuc * into the environment, which is then read by curses. What this loses
21784d9c625SLionel Sambuc * in ugliness, it makes up for in stupidity. We can't simply put the
21884d9c625SLionel Sambuc * values into the environment ourselves, because in the presence of a
21984d9c625SLionel Sambuc * kernel mechanism for returning the window size, entering values into
22084d9c625SLionel Sambuc * the environment will screw up future screen resizing events, e.g. if
22184d9c625SLionel Sambuc * the user enters a :shell command and then resizes their window. So,
22284d9c625SLionel Sambuc * if they weren't already in the environment, we make sure to delete
22384d9c625SLionel Sambuc * them immediately after setting them.
22484d9c625SLionel Sambuc *
22584d9c625SLionel Sambuc * XXX
22684d9c625SLionel Sambuc * Putting the TERM variable into the environment is necessary, even
22784d9c625SLionel Sambuc * though we're using newterm() here. We may be using initscr() as
22884d9c625SLionel Sambuc * the underlying function.
22984d9c625SLionel Sambuc */
23084d9c625SLionel Sambuc o_term = getenv("TERM");
23184d9c625SLionel Sambuc cl_putenv(sp, "TERM", ttype, 0);
23284d9c625SLionel Sambuc o_lines = getenv("LINES");
23384d9c625SLionel Sambuc cl_putenv(sp, "LINES", NULL, (u_long)O_VAL(sp, O_LINES));
23484d9c625SLionel Sambuc o_cols = getenv("COLUMNS");
23584d9c625SLionel Sambuc cl_putenv(sp, "COLUMNS", NULL, (u_long)O_VAL(sp, O_COLUMNS));
23684d9c625SLionel Sambuc
23784d9c625SLionel Sambuc /*
23884d9c625SLionel Sambuc * We don't care about the SCREEN reference returned by newterm, we
23984d9c625SLionel Sambuc * never have more than one SCREEN at a time.
24084d9c625SLionel Sambuc *
24184d9c625SLionel Sambuc * XXX
24284d9c625SLionel Sambuc * The SunOS initscr() can't be called twice. Don't even think about
24384d9c625SLionel Sambuc * using it. It fails in subtle ways (e.g. select(2) on fileno(stdin)
24484d9c625SLionel Sambuc * stops working). (The SVID notes that applications should only call
24584d9c625SLionel Sambuc * initscr() once.)
24684d9c625SLionel Sambuc *
24784d9c625SLionel Sambuc * XXX
24884d9c625SLionel Sambuc * The HP/UX newterm doesn't support the NULL first argument, so we
24984d9c625SLionel Sambuc * have to specify the terminal type.
25084d9c625SLionel Sambuc */
25184d9c625SLionel Sambuc errno = 0;
25284d9c625SLionel Sambuc if (newterm(__UNCONST(ttype), stdout, stdin) == NULL) {
25384d9c625SLionel Sambuc if (errno)
25484d9c625SLionel Sambuc msgq(sp, M_SYSERR, "%s", ttype);
25584d9c625SLionel Sambuc else
25684d9c625SLionel Sambuc msgq(sp, M_ERR, "%s: unknown terminal type", ttype);
25784d9c625SLionel Sambuc return (1);
25884d9c625SLionel Sambuc }
25984d9c625SLionel Sambuc
26084d9c625SLionel Sambuc if (o_term == NULL)
26184d9c625SLionel Sambuc cl_unsetenv(sp, "TERM");
26284d9c625SLionel Sambuc if (o_lines == NULL)
26384d9c625SLionel Sambuc cl_unsetenv(sp, "LINES");
26484d9c625SLionel Sambuc if (o_cols == NULL)
26584d9c625SLionel Sambuc cl_unsetenv(sp, "COLUMNS");
26684d9c625SLionel Sambuc
26784d9c625SLionel Sambuc /*
26884d9c625SLionel Sambuc * XXX
26984d9c625SLionel Sambuc * Someone got let out alone without adult supervision -- the SunOS
27084d9c625SLionel Sambuc * newterm resets the signal handlers. There's a race, but it's not
27184d9c625SLionel Sambuc * worth closing.
27284d9c625SLionel Sambuc */
27384d9c625SLionel Sambuc (void)sig_init(sp->gp, sp);
27484d9c625SLionel Sambuc
27584d9c625SLionel Sambuc /*
27684d9c625SLionel Sambuc * We use raw mode. What we want is 8-bit clean, however, signals
27784d9c625SLionel Sambuc * and flow control should continue to work. Admittedly, it sounds
27884d9c625SLionel Sambuc * like cbreak, but it isn't. Using cbreak() can get you additional
27984d9c625SLionel Sambuc * things like IEXTEN, which turns on flags like DISCARD and LNEXT.
28084d9c625SLionel Sambuc *
28184d9c625SLionel Sambuc * !!!
28284d9c625SLionel Sambuc * If raw isn't turning off echo and newlines, something's wrong.
28384d9c625SLionel Sambuc * However, it shouldn't hurt.
28484d9c625SLionel Sambuc */
28584d9c625SLionel Sambuc noecho(); /* No character echo. */
28684d9c625SLionel Sambuc nonl(); /* No CR/NL translation. */
28784d9c625SLionel Sambuc raw(); /* 8-bit clean. */
28884d9c625SLionel Sambuc idlok(stdscr, 1); /* Use hardware insert/delete line. */
28984d9c625SLionel Sambuc
29084d9c625SLionel Sambuc /* Put the cursor keys into application mode. */
29184d9c625SLionel Sambuc (void)keypad(stdscr, TRUE);
29284d9c625SLionel Sambuc
29384d9c625SLionel Sambuc /*
29484d9c625SLionel Sambuc * XXX
29584d9c625SLionel Sambuc * The screen TI sequence just got sent. See the comment in
29684d9c625SLionel Sambuc * cl_funcs.c:cl_attr().
29784d9c625SLionel Sambuc */
29884d9c625SLionel Sambuc clp->ti_te = TI_SENT;
29984d9c625SLionel Sambuc
30084d9c625SLionel Sambuc /*
30184d9c625SLionel Sambuc * XXX
30284d9c625SLionel Sambuc * Historic implementations of curses handled SIGTSTP signals
30384d9c625SLionel Sambuc * in one of three ways. They either:
30484d9c625SLionel Sambuc *
30584d9c625SLionel Sambuc * 1: Set their own handler, regardless.
30684d9c625SLionel Sambuc * 2: Did not set a handler if a handler was already installed.
30784d9c625SLionel Sambuc * 3: Set their own handler, but then called any previously set
30884d9c625SLionel Sambuc * handler after completing their own cleanup.
30984d9c625SLionel Sambuc *
31084d9c625SLionel Sambuc * We don't try and figure out which behavior is in place, we force
31184d9c625SLionel Sambuc * it to SIG_DFL after initializing the curses interface, which means
31284d9c625SLionel Sambuc * that curses isn't going to take the signal. Since curses isn't
31384d9c625SLionel Sambuc * reentrant (i.e., the whole curses SIGTSTP interface is a fantasy),
31484d9c625SLionel Sambuc * we're doing The Right Thing.
31584d9c625SLionel Sambuc */
31684d9c625SLionel Sambuc (void)signal(SIGTSTP, SIG_DFL);
31784d9c625SLionel Sambuc
31884d9c625SLionel Sambuc /*
31984d9c625SLionel Sambuc * If flow control was on, turn it back on. Turn signals on. ISIG
32084d9c625SLionel Sambuc * turns on VINTR, VQUIT, VDSUSP and VSUSP. The main curses code
32184d9c625SLionel Sambuc * already installed a handler for VINTR. We're going to disable the
32284d9c625SLionel Sambuc * other three.
32384d9c625SLionel Sambuc *
32484d9c625SLionel Sambuc * XXX
32584d9c625SLionel Sambuc * We want to use ^Y as a vi scrolling command. If the user has the
32684d9c625SLionel Sambuc * DSUSP character set to ^Y (common practice) clean it up. As it's
32784d9c625SLionel Sambuc * equally possible that the user has VDSUSP set to 'a', we disable
32884d9c625SLionel Sambuc * it regardless. It doesn't make much sense to suspend vi at read,
32984d9c625SLionel Sambuc * so I don't think anyone will care. Alternatively, we could look
33084d9c625SLionel Sambuc * it up in the table of legal command characters and turn it off if
33184d9c625SLionel Sambuc * it matches one. VDSUSP wasn't in POSIX 1003.1-1990, so we test for
33284d9c625SLionel Sambuc * it.
33384d9c625SLionel Sambuc *
33484d9c625SLionel Sambuc * XXX
33584d9c625SLionel Sambuc * We don't check to see if the user had signals enabled originally.
33684d9c625SLionel Sambuc * If they didn't, it's unclear what we're supposed to do here, but
33784d9c625SLionel Sambuc * it's also pretty unlikely.
33884d9c625SLionel Sambuc */
33984d9c625SLionel Sambuc if (tcgetattr(STDIN_FILENO, &clp->vi_enter)) {
34084d9c625SLionel Sambuc msgq(sp, M_SYSERR, "tcgetattr");
34184d9c625SLionel Sambuc goto err;
34284d9c625SLionel Sambuc }
34384d9c625SLionel Sambuc if (clp->orig.c_iflag & IXON)
34484d9c625SLionel Sambuc clp->vi_enter.c_iflag |= IXON;
34584d9c625SLionel Sambuc if (clp->orig.c_iflag & IXOFF)
34684d9c625SLionel Sambuc clp->vi_enter.c_iflag |= IXOFF;
34784d9c625SLionel Sambuc
34884d9c625SLionel Sambuc clp->vi_enter.c_lflag |= ISIG;
34984d9c625SLionel Sambuc #ifdef VDSUSP
35084d9c625SLionel Sambuc clp->vi_enter.c_cc[VDSUSP] = _POSIX_VDISABLE;
35184d9c625SLionel Sambuc #endif
35284d9c625SLionel Sambuc clp->vi_enter.c_cc[VQUIT] = _POSIX_VDISABLE;
35384d9c625SLionel Sambuc clp->vi_enter.c_cc[VSUSP] = _POSIX_VDISABLE;
35484d9c625SLionel Sambuc
35584d9c625SLionel Sambuc /*
35684d9c625SLionel Sambuc * XXX
35784d9c625SLionel Sambuc * OSF/1 doesn't turn off the <discard>, <literal-next> or <status>
35884d9c625SLionel Sambuc * characters when curses switches into raw mode. It should be OK
35984d9c625SLionel Sambuc * to do it explicitly for everyone.
36084d9c625SLionel Sambuc */
36184d9c625SLionel Sambuc #ifdef VDISCARD
36284d9c625SLionel Sambuc clp->vi_enter.c_cc[VDISCARD] = _POSIX_VDISABLE;
36384d9c625SLionel Sambuc #endif
36484d9c625SLionel Sambuc #ifdef VLNEXT
36584d9c625SLionel Sambuc clp->vi_enter.c_cc[VLNEXT] = _POSIX_VDISABLE;
36684d9c625SLionel Sambuc #endif
36784d9c625SLionel Sambuc #ifdef VSTATUS
36884d9c625SLionel Sambuc clp->vi_enter.c_cc[VSTATUS] = _POSIX_VDISABLE;
36984d9c625SLionel Sambuc #endif
37084d9c625SLionel Sambuc
37184d9c625SLionel Sambuc /* Initialize terminal based information. */
37284d9c625SLionel Sambuc if (cl_term_init(sp))
37384d9c625SLionel Sambuc goto err;
37484d9c625SLionel Sambuc
37584d9c625SLionel Sambuc fast: /* Set the terminal modes. */
37684d9c625SLionel Sambuc if (tcsetattr(STDIN_FILENO, TCSASOFT | TCSADRAIN, &clp->vi_enter)) {
37784d9c625SLionel Sambuc if (errno == EINTR)
37884d9c625SLionel Sambuc goto fast;
37984d9c625SLionel Sambuc msgq(sp, M_SYSERR, "tcsetattr");
38084d9c625SLionel Sambuc err: (void)cl_vi_end(sp->gp);
38184d9c625SLionel Sambuc return (1);
38284d9c625SLionel Sambuc }
38384d9c625SLionel Sambuc return (0);
38484d9c625SLionel Sambuc }
38584d9c625SLionel Sambuc
38684d9c625SLionel Sambuc /*
38784d9c625SLionel Sambuc * cl_vi_end --
38884d9c625SLionel Sambuc * Shutdown the vi screen.
38984d9c625SLionel Sambuc */
39084d9c625SLionel Sambuc static int
cl_vi_end(GS * gp)39184d9c625SLionel Sambuc cl_vi_end(GS *gp)
39284d9c625SLionel Sambuc {
39384d9c625SLionel Sambuc CL_PRIVATE *clp;
39484d9c625SLionel Sambuc
39584d9c625SLionel Sambuc clp = GCLP(gp);
39684d9c625SLionel Sambuc
39784d9c625SLionel Sambuc /* Restore the cursor keys to normal mode. */
39884d9c625SLionel Sambuc (void)keypad(stdscr, FALSE);
39984d9c625SLionel Sambuc
40084d9c625SLionel Sambuc /*
40184d9c625SLionel Sambuc * If we were running vi when we quit, scroll the screen up a single
40284d9c625SLionel Sambuc * line so we don't lose any information.
40384d9c625SLionel Sambuc *
40484d9c625SLionel Sambuc * Move to the bottom of the window (some endwin implementations don't
40584d9c625SLionel Sambuc * do this for you).
40684d9c625SLionel Sambuc */
40784d9c625SLionel Sambuc if (!F_ISSET(clp, CL_IN_EX)) {
40884d9c625SLionel Sambuc (void)move(0, 0);
40984d9c625SLionel Sambuc (void)deleteln();
41084d9c625SLionel Sambuc (void)move(LINES - 1, 0);
41184d9c625SLionel Sambuc (void)refresh();
41284d9c625SLionel Sambuc }
41384d9c625SLionel Sambuc
41484d9c625SLionel Sambuc cl_freecap(clp);
41584d9c625SLionel Sambuc
41684d9c625SLionel Sambuc /* End curses window. */
41784d9c625SLionel Sambuc (void)endwin();
41884d9c625SLionel Sambuc
41984d9c625SLionel Sambuc /*
42084d9c625SLionel Sambuc * XXX
42184d9c625SLionel Sambuc * The screen TE sequence just got sent. See the comment in
42284d9c625SLionel Sambuc * cl_funcs.c:cl_attr().
42384d9c625SLionel Sambuc */
42484d9c625SLionel Sambuc clp->ti_te = TE_SENT;
42584d9c625SLionel Sambuc
42684d9c625SLionel Sambuc return (0);
42784d9c625SLionel Sambuc }
42884d9c625SLionel Sambuc
42984d9c625SLionel Sambuc /*
43084d9c625SLionel Sambuc * cl_ex_init --
43184d9c625SLionel Sambuc * Initialize the ex screen.
43284d9c625SLionel Sambuc */
43384d9c625SLionel Sambuc static int
cl_ex_init(SCR * sp)43484d9c625SLionel Sambuc cl_ex_init(SCR *sp)
43584d9c625SLionel Sambuc {
43684d9c625SLionel Sambuc CL_PRIVATE *clp;
43784d9c625SLionel Sambuc
43884d9c625SLionel Sambuc clp = CLP(sp);
43984d9c625SLionel Sambuc
44084d9c625SLionel Sambuc /* If already initialized, just set the terminal modes. */
44184d9c625SLionel Sambuc if (F_ISSET(clp, CL_SCR_EX_INIT))
44284d9c625SLionel Sambuc goto fast;
44384d9c625SLionel Sambuc
44484d9c625SLionel Sambuc /* If not reading from a file, we're done. */
44584d9c625SLionel Sambuc if (!F_ISSET(clp, CL_STDIN_TTY))
44684d9c625SLionel Sambuc return (0);
44784d9c625SLionel Sambuc
44884d9c625SLionel Sambuc /* Get the ex termcap/terminfo strings. */
44984d9c625SLionel Sambuc (void)cl_getcap(sp, "cup", &clp->cup);
45084d9c625SLionel Sambuc (void)cl_getcap(sp, "smso", &clp->smso);
45184d9c625SLionel Sambuc (void)cl_getcap(sp, "rmso", &clp->rmso);
45284d9c625SLionel Sambuc (void)cl_getcap(sp, "el", &clp->el);
45384d9c625SLionel Sambuc (void)cl_getcap(sp, "cuu1", &clp->cuu1);
45484d9c625SLionel Sambuc
45584d9c625SLionel Sambuc /* Enter_standout_mode and exit_standout_mode are paired. */
45684d9c625SLionel Sambuc if (clp->smso == NULL || clp->rmso == NULL) {
45784d9c625SLionel Sambuc if (clp->smso != NULL) {
45884d9c625SLionel Sambuc free(clp->smso);
45984d9c625SLionel Sambuc clp->smso = NULL;
46084d9c625SLionel Sambuc }
46184d9c625SLionel Sambuc if (clp->rmso != NULL) {
46284d9c625SLionel Sambuc free(clp->rmso);
46384d9c625SLionel Sambuc clp->rmso = NULL;
46484d9c625SLionel Sambuc }
46584d9c625SLionel Sambuc }
46684d9c625SLionel Sambuc
46784d9c625SLionel Sambuc /*
46884d9c625SLionel Sambuc * Turn on canonical mode, with normal input and output processing.
46984d9c625SLionel Sambuc * Start with the original terminal settings as the user probably
47084d9c625SLionel Sambuc * had them (including any local extensions) set correctly for the
47184d9c625SLionel Sambuc * current terminal.
47284d9c625SLionel Sambuc *
47384d9c625SLionel Sambuc * !!!
47484d9c625SLionel Sambuc * We can't get everything that we need portably; for example, ONLCR,
47584d9c625SLionel Sambuc * mapping <newline> to <carriage-return> on output isn't required
47684d9c625SLionel Sambuc * by POSIX 1003.1b-1993. If this turns out to be a problem, then
47784d9c625SLionel Sambuc * we'll either have to play some games on the mapping, or we'll have
47884d9c625SLionel Sambuc * to make all ex printf's output \r\n instead of \n.
47984d9c625SLionel Sambuc */
48084d9c625SLionel Sambuc clp->ex_enter = clp->orig;
48184d9c625SLionel Sambuc clp->ex_enter.c_lflag |= ECHO | ECHOE | ECHOK | ICANON | IEXTEN | ISIG;
48284d9c625SLionel Sambuc #ifdef ECHOCTL
48384d9c625SLionel Sambuc clp->ex_enter.c_lflag |= ECHOCTL;
48484d9c625SLionel Sambuc #endif
48584d9c625SLionel Sambuc #ifdef ECHOKE
48684d9c625SLionel Sambuc clp->ex_enter.c_lflag |= ECHOKE;
48784d9c625SLionel Sambuc #endif
48884d9c625SLionel Sambuc clp->ex_enter.c_iflag |= ICRNL;
48984d9c625SLionel Sambuc clp->ex_enter.c_oflag |= OPOST;
49084d9c625SLionel Sambuc #ifdef ONLCR
49184d9c625SLionel Sambuc clp->ex_enter.c_oflag |= ONLCR;
49284d9c625SLionel Sambuc #endif
49384d9c625SLionel Sambuc
49484d9c625SLionel Sambuc fast: if (tcsetattr(STDIN_FILENO, TCSADRAIN | TCSASOFT, &clp->ex_enter)) {
49584d9c625SLionel Sambuc if (errno == EINTR)
49684d9c625SLionel Sambuc goto fast;
49784d9c625SLionel Sambuc msgq(sp, M_SYSERR, "tcsetattr");
49884d9c625SLionel Sambuc return (1);
49984d9c625SLionel Sambuc }
50084d9c625SLionel Sambuc return (0);
50184d9c625SLionel Sambuc }
50284d9c625SLionel Sambuc
50384d9c625SLionel Sambuc /*
50484d9c625SLionel Sambuc * cl_ex_end --
50584d9c625SLionel Sambuc * Shutdown the ex screen.
50684d9c625SLionel Sambuc */
50784d9c625SLionel Sambuc static int
cl_ex_end(GS * gp)50884d9c625SLionel Sambuc cl_ex_end(GS *gp)
50984d9c625SLionel Sambuc {
51084d9c625SLionel Sambuc CL_PRIVATE *clp;
51184d9c625SLionel Sambuc
51284d9c625SLionel Sambuc clp = GCLP(gp);
51384d9c625SLionel Sambuc
51484d9c625SLionel Sambuc cl_freecap(clp);
51584d9c625SLionel Sambuc
51684d9c625SLionel Sambuc return (0);
51784d9c625SLionel Sambuc }
51884d9c625SLionel Sambuc
51984d9c625SLionel Sambuc /*
52084d9c625SLionel Sambuc * cl_getcap --
52184d9c625SLionel Sambuc * Retrieve termcap/terminfo strings.
52284d9c625SLionel Sambuc *
52384d9c625SLionel Sambuc * PUBLIC: int cl_getcap __P((SCR *, const char *, char **));
52484d9c625SLionel Sambuc */
52584d9c625SLionel Sambuc int
cl_getcap(SCR * sp,const char * name,char ** elementp)52684d9c625SLionel Sambuc cl_getcap(SCR *sp, const char *name, char **elementp)
52784d9c625SLionel Sambuc {
52884d9c625SLionel Sambuc size_t len;
52984d9c625SLionel Sambuc char *t;
53084d9c625SLionel Sambuc
53184d9c625SLionel Sambuc if ((t = tigetstr(name)) != NULL &&
53284d9c625SLionel Sambuc t != (char *)-1 && (len = strlen(t)) != 0) {
53384d9c625SLionel Sambuc MALLOC_RET(sp, *elementp, char *, len + 1);
53484d9c625SLionel Sambuc memmove(*elementp, t, len + 1);
53584d9c625SLionel Sambuc }
53684d9c625SLionel Sambuc return (0);
53784d9c625SLionel Sambuc }
53884d9c625SLionel Sambuc
53984d9c625SLionel Sambuc /*
54084d9c625SLionel Sambuc * cl_freecap --
54184d9c625SLionel Sambuc * Free any allocated termcap/terminfo strings.
54284d9c625SLionel Sambuc */
54384d9c625SLionel Sambuc static void
cl_freecap(CL_PRIVATE * clp)54484d9c625SLionel Sambuc cl_freecap(CL_PRIVATE *clp)
54584d9c625SLionel Sambuc {
54684d9c625SLionel Sambuc if (clp->el != NULL) {
54784d9c625SLionel Sambuc free(clp->el);
54884d9c625SLionel Sambuc clp->el = NULL;
54984d9c625SLionel Sambuc }
55084d9c625SLionel Sambuc if (clp->cup != NULL) {
55184d9c625SLionel Sambuc free(clp->cup);
55284d9c625SLionel Sambuc clp->cup = NULL;
55384d9c625SLionel Sambuc }
55484d9c625SLionel Sambuc if (clp->cuu1 != NULL) {
55584d9c625SLionel Sambuc free(clp->cuu1);
55684d9c625SLionel Sambuc clp->cuu1 = NULL;
55784d9c625SLionel Sambuc }
55884d9c625SLionel Sambuc if (clp->rmso != NULL) {
55984d9c625SLionel Sambuc free(clp->rmso);
56084d9c625SLionel Sambuc clp->rmso = NULL;
56184d9c625SLionel Sambuc }
56284d9c625SLionel Sambuc if (clp->smso != NULL) {
56384d9c625SLionel Sambuc free(clp->smso);
56484d9c625SLionel Sambuc clp->smso = NULL;
56584d9c625SLionel Sambuc }
56684d9c625SLionel Sambuc }
56784d9c625SLionel Sambuc
56884d9c625SLionel Sambuc /*
56984d9c625SLionel Sambuc * cl_putenv --
57084d9c625SLionel Sambuc * Put a value into the environment.
57184d9c625SLionel Sambuc */
57284d9c625SLionel Sambuc static int
cl_putenv(SCR * sp,const char * name,const char * str,u_long value)57384d9c625SLionel Sambuc cl_putenv(SCR *sp, const char *name, const char *str, u_long value)
57484d9c625SLionel Sambuc {
57584d9c625SLionel Sambuc char buf[40];
57684d9c625SLionel Sambuc
57784d9c625SLionel Sambuc if (str == NULL) {
57884d9c625SLionel Sambuc (void)snprintf(buf, sizeof(buf), "%lu", value);
57984d9c625SLionel Sambuc return (cl_setenv(sp, name, buf));
58084d9c625SLionel Sambuc } else
58184d9c625SLionel Sambuc return (cl_setenv(sp, name, str));
58284d9c625SLionel Sambuc }
583