2 * Copyright (c) 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 * Copyright (c) 1993, 1994, 1995, 1996
5 * Keith Bostic. All rights reserved.
7 * See the LICENSE file for redistribution information.
12 #include <sys/types.h>
13 #include <sys/queue.h>
16 #include <bitstring.h>
28 #include "../common/common.h"
31 static int cl_ex_end(GS *);
32 static int cl_ex_init(SCR *);
33 static void cl_freecap(CL_PRIVATE *);
34 static int cl_vi_end(GS *);
35 static int cl_vi_init(SCR *);
36 static int cl_putenv(char *, char *, u_long);
40 * Switch screen types.
42 * PUBLIC: int cl_screen(SCR *, u_int32_t);
45 cl_screen(SCR *sp, u_int32_t flags)
53 win = CLSP(sp) ? CLSP(sp) : stdscr;
55 /* See if the current information is incorrect. */
56 if (F_ISSET(gp, G_SRESTART)) {
57 if ((!F_ISSET(sp, SC_SCR_EX | SC_SCR_VI) ||
58 resizeterm(O_VAL(sp, O_LINES), O_VAL(sp, O_COLUMNS))) &&
61 F_CLR(gp, G_SRESTART);
64 /* See if we're already in the right mode. */
65 if ((LF_ISSET(SC_EX) && F_ISSET(sp, SC_SCR_EX)) ||
66 (LF_ISSET(SC_VI) && F_ISSET(sp, SC_SCR_VI)))
70 * Fake leaving ex mode.
72 * We don't actually exit ex or vi mode unless forced (e.g. by a window
73 * size change). This is because many curses implementations can't be
74 * called twice in a single program. Plus, it's faster. If the editor
75 * "leaves" vi to enter ex, when it exits ex we'll just fall back into
78 if (F_ISSET(sp, SC_SCR_EX))
82 * Fake leaving vi mode.
84 * Clear out the rest of the screen if we're in the middle of a split
85 * screen. Move to the last line in the current screen -- this makes
86 * terminal scrolling happen naturally. Note: *don't* move past the
87 * end of the screen, as there are ex commands (e.g., :read ! cat file)
88 * that don't want to. Don't clear the info line, its contents may be
89 * valid, e.g. :file|append.
91 if (F_ISSET(sp, SC_SCR_VI)) {
94 if (TAILQ_NEXT(sp, q) != NULL) {
95 (void)wmove(win, RLNO(sp, sp->rows), 0);
98 (void)wmove(win, RLNO(sp, sp->rows) - 1, 0);
102 /* Enter the requested mode. */
103 if (LF_ISSET(SC_EX)) {
106 F_SET(clp, CL_IN_EX | CL_SCR_EX_INIT);
109 * If doing an ex screen for ex mode, move to the last line
112 if (F_ISSET(sp, SC_EX) && clp->cup != NULL)
113 tputs(tgoto(clp->cup,
114 0, O_VAL(sp, O_LINES) - 1), 1, cl_putchar);
118 F_CLR(clp, CL_IN_EX);
119 F_SET(clp, CL_SCR_VI_INIT);
126 * Shutdown the screens.
128 * PUBLIC: int cl_quit(GS *);
140 * If we weren't really running, ignore it. This happens if the
141 * screen changes size before we've called curses.
143 if (!F_ISSET(clp, CL_SCR_EX_INIT | CL_SCR_VI_INIT))
146 /* Clean up the terminal mappings. */
150 /* Really leave vi mode. */
151 if (F_ISSET(clp, CL_STDIN_TTY) &&
152 F_ISSET(clp, CL_SCR_VI_INIT) && cl_vi_end(gp))
155 /* Really leave ex mode. */
156 if (F_ISSET(clp, CL_STDIN_TTY) &&
157 F_ISSET(clp, CL_SCR_EX_INIT) && cl_ex_end(gp))
161 * If we were running ex when we quit, or we're using an implementation
162 * of curses where endwin() doesn't get this right, restore the original
166 * We always do this because it's too hard to figure out what curses
167 * implementations get it wrong. It may discard type-ahead characters
168 * from the tty queue.
170 (void)tcsetattr(STDIN_FILENO, TCSADRAIN | TCSASOFT, &clp->orig);
172 F_CLR(clp, CL_SCR_EX_INIT | CL_SCR_VI_INIT);
178 * Initialize the curses vi screen.
185 char *o_cols, *o_lines, *o_term, *ttype;
190 /* If already initialized, just set the terminal modes. */
191 if (F_ISSET(clp, CL_SCR_VI_INIT))
194 /* Curses vi always reads from (and writes to) a terminal. */
195 if (!F_ISSET(clp, CL_STDIN_TTY) || !isatty(STDOUT_FILENO)) {
197 "016|Vi's standard input and output must be a terminal");
201 /* We'll need a terminal type. */
202 if (opts_empty(sp, O_TERM, 0))
204 ttype = O_STR(sp, O_TERM);
208 * Changing the row/column and terminal values is done by putting them
209 * into the environment, which is then read by curses. What this loses
210 * in ugliness, it makes up for in stupidity. We can't simply put the
211 * values into the environment ourselves, because in the presence of a
212 * kernel mechanism for returning the window size, entering values into
213 * the environment will screw up future screen resizing events, e.g. if
214 * the user enters a :shell command and then resizes their window. So,
215 * if they weren't already in the environment, we make sure to delete
216 * them immediately after setting them.
219 * Putting the TERM variable into the environment is necessary, even
220 * though we're using newterm() here. We may be using initscr() as
221 * the underlying function.
223 o_term = getenv("TERM");
224 cl_putenv("TERM", ttype, 0);
225 o_lines = getenv("LINES");
226 cl_putenv("LINES", NULL, (u_long)O_VAL(sp, O_LINES));
227 o_cols = getenv("COLUMNS");
228 cl_putenv("COLUMNS", NULL, (u_long)O_VAL(sp, O_COLUMNS));
231 * The terminal is aways initialized, either in `main`, or by a
232 * previous call to newterm(3X).
234 (void)del_curterm(cur_term);
237 * We never have more than one SCREEN at a time, so set_term(NULL) will
238 * give us the last SCREEN.
241 if (newterm(ttype, stdout, stdin) == NULL) {
243 msgq(sp, M_SYSERR, "%s", ttype);
245 msgq(sp, M_ERR, "%s: unknown terminal type", ttype);
258 * Someone got let out alone without adult supervision -- the SunOS
259 * newterm resets the signal handlers. There's a race, but it's not
262 (void)sig_init(sp->gp, sp);
265 * We use raw mode. What we want is 8-bit clean, however, signals
266 * and flow control should continue to work. Admittedly, it sounds
267 * like cbreak, but it isn't. Using cbreak() can get you additional
268 * things like IEXTEN, which turns on flags like DISCARD and LNEXT.
271 * If raw isn't turning off echo and newlines, something's wrong.
272 * However, it shouldn't hurt.
274 noecho(); /* No character echo. */
275 nonl(); /* No CR/NL translation. */
276 raw(); /* 8-bit clean. */
277 idlok(stdscr, 1); /* Use hardware insert/delete line. */
279 /* Put the cursor keys into application mode. */
280 (void)keypad(stdscr, TRUE);
284 * The screen TI sequence just got sent. See the comment in
285 * cl_funcs.c:cl_attr().
287 clp->ti_te = TI_SENT;
291 * Historic implementations of curses handled SIGTSTP signals
292 * in one of three ways. They either:
294 * 1: Set their own handler, regardless.
295 * 2: Did not set a handler if a handler was already installed.
296 * 3: Set their own handler, but then called any previously set
297 * handler after completing their own cleanup.
299 * We don't try and figure out which behavior is in place, we force
300 * it to SIG_DFL after initializing the curses interface, which means
301 * that curses isn't going to take the signal. Since curses isn't
302 * reentrant (i.e., the whole curses SIGTSTP interface is a fantasy),
303 * we're doing The Right Thing.
305 (void)signal(SIGTSTP, SIG_DFL);
308 * If flow control was on, turn it back on. Turn signals on. ISIG
309 * turns on VINTR, VQUIT, VDSUSP and VSUSP. The main curses code
310 * already installed a handler for VINTR. We're going to disable the
314 * We want to use ^Y as a vi scrolling command. If the user has the
315 * DSUSP character set to ^Y (common practice) clean it up. As it's
316 * equally possible that the user has VDSUSP set to 'a', we disable
317 * it regardless. It doesn't make much sense to suspend vi at read,
318 * so I don't think anyone will care. Alternatively, we could look
319 * it up in the table of legal command characters and turn it off if
320 * it matches one. VDSUSP wasn't in POSIX 1003.1-1990, so we test for
324 * We don't check to see if the user had signals enabled originally.
325 * If they didn't, it's unclear what we're supposed to do here, but
326 * it's also pretty unlikely.
328 if (tcgetattr(STDIN_FILENO, &clp->vi_enter)) {
329 msgq(sp, M_SYSERR, "tcgetattr");
332 if (clp->orig.c_iflag & IXON)
333 clp->vi_enter.c_iflag |= IXON;
334 if (clp->orig.c_iflag & IXOFF)
335 clp->vi_enter.c_iflag |= IXOFF;
337 clp->vi_enter.c_lflag |= ISIG;
339 clp->vi_enter.c_cc[VDSUSP] = _POSIX_VDISABLE;
341 clp->vi_enter.c_cc[VQUIT] = _POSIX_VDISABLE;
342 clp->vi_enter.c_cc[VSUSP] = _POSIX_VDISABLE;
346 * OSF/1 doesn't turn off the <discard>, <literal-next> or <status>
347 * characters when curses switches into raw mode. It should be OK
348 * to do it explicitly for everyone.
351 clp->vi_enter.c_cc[VDISCARD] = _POSIX_VDISABLE;
354 clp->vi_enter.c_cc[VLNEXT] = _POSIX_VDISABLE;
357 clp->vi_enter.c_cc[VSTATUS] = _POSIX_VDISABLE;
360 /* Initialize terminal based information. */
361 if (cl_term_init(sp))
364 fast: /* Set the terminal modes. */
365 if (tcsetattr(STDIN_FILENO, TCSASOFT | TCSADRAIN, &clp->vi_enter)) {
368 msgq(sp, M_SYSERR, "tcsetattr");
369 err: (void)cl_vi_end(sp->gp);
377 * Shutdown the vi screen.
386 /* Restore the cursor keys to normal mode. */
387 (void)keypad(stdscr, FALSE);
390 * If we were running vi when we quit, scroll the screen up a single
391 * line so we don't lose any information.
393 * Move to the bottom of the window (some endwin implementations don't
396 if (!F_ISSET(clp, CL_IN_EX)) {
399 (void)move(LINES - 1, 0);
405 /* End curses window. */
408 /* Free the SCREEN created by newterm(3X). */
409 delscreen(set_term(NULL));
413 * The screen TE sequence just got sent. See the comment in
414 * cl_funcs.c:cl_attr().
416 clp->ti_te = TE_SENT;
423 * Initialize the ex screen.
432 /* If already initialized, just set the terminal modes. */
433 if (F_ISSET(clp, CL_SCR_EX_INIT))
436 /* If not reading from a file, we're done. */
437 if (!F_ISSET(clp, CL_STDIN_TTY))
440 /* Get the ex termcap/terminfo strings. */
441 (void)cl_getcap(sp, "cup", &clp->cup);
442 (void)cl_getcap(sp, "smso", &clp->smso);
443 (void)cl_getcap(sp, "rmso", &clp->rmso);
444 (void)cl_getcap(sp, "el", &clp->el);
445 (void)cl_getcap(sp, "cuu1", &clp->cuu1);
447 /* Enter_standout_mode and exit_standout_mode are paired. */
448 if (clp->smso == NULL || clp->rmso == NULL) {
457 * Turn on canonical mode, with normal input and output processing.
458 * Start with the original terminal settings as the user probably
459 * had them (including any local extensions) set correctly for the
463 * We can't get everything that we need portably; for example, ONLCR,
464 * mapping <newline> to <carriage-return> on output isn't required
465 * by POSIX 1003.1b-1993. If this turns out to be a problem, then
466 * we'll either have to play some games on the mapping, or we'll have
467 * to make all ex printf's output \r\n instead of \n.
469 clp->ex_enter = clp->orig;
470 clp->ex_enter.c_lflag |= ECHO | ECHOE | ECHOK | ICANON | IEXTEN | ISIG;
472 clp->ex_enter.c_lflag |= ECHOCTL;
475 clp->ex_enter.c_lflag |= ECHOKE;
477 clp->ex_enter.c_iflag |= ICRNL;
478 clp->ex_enter.c_oflag |= OPOST;
480 clp->ex_enter.c_oflag |= ONLCR;
483 fast: if (tcsetattr(STDIN_FILENO, TCSADRAIN | TCSASOFT, &clp->ex_enter)) {
486 msgq(sp, M_SYSERR, "tcsetattr");
494 * Shutdown the ex screen.
510 * Retrieve termcap/terminfo strings.
512 * PUBLIC: int cl_getcap(SCR *, char *, char **);
515 cl_getcap(SCR *sp, char *name, char **elementp)
520 if ((t = tigetstr(name)) != NULL &&
521 t != (char *)-1 && (len = strlen(t)) != 0) {
522 MALLOC_RET(sp, *elementp, len + 1);
523 memmove(*elementp, t, len + 1);
530 * Free any allocated termcap/terminfo strings.
533 cl_freecap(CL_PRIVATE *clp)
550 /* Required by libcursesw :) */
552 clp->cw.bp1.c = NULL;
558 * Put a value into the environment.
561 cl_putenv(char *name, char *str, u_long value)
566 (void)snprintf(buf, sizeof(buf), "%lu", value);
567 return (setenv(name, buf, 1));
569 return (setenv(name, str, 1));