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.
13 static const char sccsid
[] = "$Id: cl_screen.c,v 10.49 1996/09/24 20:48:04 bostic Exp $ (Berkeley) $Date: 1996/09/24 20:48:04 $";
16 #include <sys/types.h>
17 #include <sys/queue.h>
19 #include <bitstring.h>
29 #include "../common/common.h"
32 static int cl_ex_end
__P((GS
*));
33 static int cl_ex_init
__P((SCR
*));
34 static void cl_freecap
__P((CL_PRIVATE
*));
35 static int cl_vi_end
__P((GS
*));
36 static int cl_vi_init
__P((SCR
*));
37 static int cl_putenv
__P((char *, char *, u_long
));
41 * Switch screen types.
43 * PUBLIC: int cl_screen __P((SCR *, u_int32_t));
56 /* See if the current information is incorrect. */
57 if (F_ISSET(gp
, G_SRESTART
)) {
60 F_CLR(gp
, G_SRESTART
);
63 /* See if we're already in the right mode. */
64 if (LF_ISSET(SC_EX
) && F_ISSET(sp
, SC_SCR_EX
) ||
65 LF_ISSET(SC_VI
) && F_ISSET(sp
, SC_SCR_VI
))
69 * Fake leaving ex mode.
71 * We don't actually exit ex or vi mode unless forced (e.g. by a window
72 * size change). This is because many curses implementations can't be
73 * called twice in a single program. Plus, it's faster. If the editor
74 * "leaves" vi to enter ex, when it exits ex we'll just fall back into
77 if (F_ISSET(sp
, SC_SCR_EX
))
81 * Fake leaving vi mode.
83 * Clear out the rest of the screen if we're in the middle of a split
84 * screen. Move to the last line in the current screen -- this makes
85 * terminal scrolling happen naturally. Note: *don't* move past the
86 * end of the screen, as there are ex commands (e.g., :read ! cat file)
87 * that don't want to. Don't clear the info line, its contents may be
88 * valid, e.g. :file|append.
90 if (F_ISSET(sp
, SC_SCR_VI
)) {
93 if (sp
->q
.cqe_next
!= (void *)&gp
->dq
) {
94 (void)move(RLNO(sp
, sp
->rows
), 0);
97 (void)move(RLNO(sp
, sp
->rows
) - 1, 0);
101 /* Enter the requested mode. */
102 if (LF_ISSET(SC_EX
)) {
105 F_SET(clp
, CL_IN_EX
| CL_SCR_EX_INIT
);
108 * If doing an ex screen for ex mode, move to the last line
111 if (F_ISSET(sp
, SC_EX
) && clp
->cup
!= NULL
)
112 tputs(tgoto(clp
->cup
,
113 0, O_VAL(sp
, O_LINES
) - 1), 1, cl_putchar
);
117 F_CLR(clp
, CL_IN_EX
);
118 F_SET(clp
, CL_SCR_VI_INIT
);
125 * Shutdown the screens.
127 * PUBLIC: int cl_quit __P((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.
186 char *o_cols
, *o_lines
, *o_term
, *ttype
;
191 /* If already initialized, just set the terminal modes. */
192 if (F_ISSET(clp
, CL_SCR_VI_INIT
))
195 /* Curses vi always reads from (and writes to) a terminal. */
196 if (!F_ISSET(clp
, CL_STDIN_TTY
) || !isatty(STDOUT_FILENO
)) {
198 "016|Vi's standard input and output must be a terminal");
202 /* We'll need a terminal type. */
203 if (opts_empty(sp
, O_TERM
, 0))
205 ttype
= O_STR(sp
, O_TERM
);
209 * Changing the row/column and terminal values is done by putting them
210 * into the environment, which is then read by curses. What this loses
211 * in ugliness, it makes up for in stupidity. We can't simply put the
212 * values into the environment ourselves, because in the presence of a
213 * kernel mechanism for returning the window size, entering values into
214 * the environment will screw up future screen resizing events, e.g. if
215 * the user enters a :shell command and then resizes their window. So,
216 * if they weren't already in the environment, we make sure to delete
217 * them immediately after setting them.
220 * Putting the TERM variable into the environment is necessary, even
221 * though we're using newterm() here. We may be using initscr() as
222 * the underlying function.
224 o_term
= getenv("TERM");
225 cl_putenv("TERM", ttype
, 0);
226 o_lines
= getenv("LINES");
227 cl_putenv("LINES", NULL
, (u_long
)O_VAL(sp
, O_LINES
));
228 o_cols
= getenv("COLUMNS");
229 cl_putenv("COLUMNS", NULL
, (u_long
)O_VAL(sp
, O_COLUMNS
));
232 * We don't care about the SCREEN reference returned by newterm, we
233 * never have more than one SCREEN at a time.
236 * The SunOS initscr() can't be called twice. Don't even think about
237 * using it. It fails in subtle ways (e.g. select(2) on fileno(stdin)
238 * stops working). (The SVID notes that applications should only call
242 * The HP/UX newterm doesn't support the NULL first argument, so we
243 * have to specify the terminal type.
246 if (newterm(ttype
, stdout
, stdin
) == NULL
) {
248 msgq(sp
, M_SYSERR
, "%s", ttype
);
250 msgq(sp
, M_ERR
, "%s: unknown terminal type", ttype
);
263 * Someone got let out alone without adult supervision -- the SunOS
264 * newterm resets the signal handlers. There's a race, but it's not
267 (void)sig_init(sp
->gp
, sp
);
270 * We use raw mode. What we want is 8-bit clean, however, signals
271 * and flow control should continue to work. Admittedly, it sounds
272 * like cbreak, but it isn't. Using cbreak() can get you additional
273 * things like IEXTEN, which turns on flags like DISCARD and LNEXT.
276 * If raw isn't turning off echo and newlines, something's wrong.
277 * However, it shouldn't hurt.
279 noecho(); /* No character echo. */
280 nonl(); /* No CR/NL translation. */
281 raw(); /* 8-bit clean. */
282 idlok(stdscr
, 1); /* Use hardware insert/delete line. */
284 /* Put the cursor keys into application mode. */
285 (void)keypad(stdscr
, TRUE
);
289 * The screen TI sequence just got sent. See the comment in
290 * cl_funcs.c:cl_attr().
292 clp
->ti_te
= TI_SENT
;
296 * Historic implementations of curses handled SIGTSTP signals
297 * in one of three ways. They either:
299 * 1: Set their own handler, regardless.
300 * 2: Did not set a handler if a handler was already installed.
301 * 3: Set their own handler, but then called any previously set
302 * handler after completing their own cleanup.
304 * We don't try and figure out which behavior is in place, we force
305 * it to SIG_DFL after initializing the curses interface, which means
306 * that curses isn't going to take the signal. Since curses isn't
307 * reentrant (i.e., the whole curses SIGTSTP interface is a fantasy),
308 * we're doing The Right Thing.
310 (void)signal(SIGTSTP
, SIG_DFL
);
313 * If flow control was on, turn it back on. Turn signals on. ISIG
314 * turns on VINTR, VQUIT, VDSUSP and VSUSP. The main curses code
315 * already installed a handler for VINTR. We're going to disable the
319 * We want to use ^Y as a vi scrolling command. If the user has the
320 * DSUSP character set to ^Y (common practice) clean it up. As it's
321 * equally possible that the user has VDSUSP set to 'a', we disable
322 * it regardless. It doesn't make much sense to suspend vi at read,
323 * so I don't think anyone will care. Alternatively, we could look
324 * it up in the table of legal command characters and turn it off if
325 * it matches one. VDSUSP wasn't in POSIX 1003.1-1990, so we test for
329 * We don't check to see if the user had signals enabled originally.
330 * If they didn't, it's unclear what we're supposed to do here, but
331 * it's also pretty unlikely.
333 if (tcgetattr(STDIN_FILENO
, &clp
->vi_enter
)) {
334 msgq(sp
, M_SYSERR
, "tcgetattr");
337 if (clp
->orig
.c_iflag
& IXON
)
338 clp
->vi_enter
.c_iflag
|= IXON
;
339 if (clp
->orig
.c_iflag
& IXOFF
)
340 clp
->vi_enter
.c_iflag
|= IXOFF
;
342 clp
->vi_enter
.c_lflag
|= ISIG
;
344 clp
->vi_enter
.c_cc
[VDSUSP
] = _POSIX_VDISABLE
;
346 clp
->vi_enter
.c_cc
[VQUIT
] = _POSIX_VDISABLE
;
347 clp
->vi_enter
.c_cc
[VSUSP
] = _POSIX_VDISABLE
;
351 * OSF/1 doesn't turn off the <discard>, <literal-next> or <status>
352 * characters when curses switches into raw mode. It should be OK
353 * to do it explicitly for everyone.
356 clp
->vi_enter
.c_cc
[VDISCARD
] = _POSIX_VDISABLE
;
359 clp
->vi_enter
.c_cc
[VLNEXT
] = _POSIX_VDISABLE
;
362 clp
->vi_enter
.c_cc
[VSTATUS
] = _POSIX_VDISABLE
;
365 /* Initialize terminal based information. */
366 if (cl_term_init(sp
))
369 fast
: /* Set the terminal modes. */
370 if (tcsetattr(STDIN_FILENO
, TCSASOFT
| TCSADRAIN
, &clp
->vi_enter
)) {
371 msgq(sp
, M_SYSERR
, "tcsetattr");
372 err
: (void)cl_vi_end(sp
->gp
);
380 * Shutdown the vi screen.
390 /* Restore the cursor keys to normal mode. */
391 (void)keypad(stdscr
, FALSE
);
394 * If we were running vi when we quit, scroll the screen up a single
395 * line so we don't lose any information.
397 * Move to the bottom of the window (some endwin implementations don't
400 if (!F_ISSET(clp
, CL_IN_EX
)) {
403 (void)move(LINES
- 1, 0);
409 /* End curses window. */
414 * The screen TE sequence just got sent. See the comment in
415 * cl_funcs.c:cl_attr().
417 clp
->ti_te
= TE_SENT
;
424 * Initialize the ex screen.
434 /* If already initialized, just set the terminal modes. */
435 if (F_ISSET(clp
, CL_SCR_EX_INIT
))
438 /* If not reading from a file, we're done. */
439 if (!F_ISSET(clp
, CL_STDIN_TTY
))
442 /* Get the ex termcap/terminfo strings. */
443 (void)cl_getcap(sp
, "cup", &clp
->cup
);
444 (void)cl_getcap(sp
, "smso", &clp
->smso
);
445 (void)cl_getcap(sp
, "rmso", &clp
->rmso
);
446 (void)cl_getcap(sp
, "el", &clp
->el
);
447 (void)cl_getcap(sp
, "cuu1", &clp
->cuu1
);
449 /* Enter_standout_mode and exit_standout_mode are paired. */
450 if (clp
->smso
== NULL
|| clp
->rmso
== NULL
) {
451 if (clp
->smso
!= NULL
) {
455 if (clp
->rmso
!= NULL
) {
462 * Turn on canonical mode, with normal input and output processing.
463 * Start with the original terminal settings as the user probably
464 * had them (including any local extensions) set correctly for the
468 * We can't get everything that we need portably; for example, ONLCR,
469 * mapping <newline> to <carriage-return> on output isn't required
470 * by POSIX 1003.1b-1993. If this turns out to be a problem, then
471 * we'll either have to play some games on the mapping, or we'll have
472 * to make all ex printf's output \r\n instead of \n.
474 clp
->ex_enter
= clp
->orig
;
475 clp
->ex_enter
.c_lflag
|= ECHO
| ECHOE
| ECHOK
| ICANON
| IEXTEN
| ISIG
;
477 clp
->ex_enter
.c_lflag
|= ECHOCTL
;
480 clp
->ex_enter
.c_lflag
|= ECHOKE
;
482 clp
->ex_enter
.c_iflag
|= ICRNL
;
483 clp
->ex_enter
.c_oflag
|= OPOST
;
485 clp
->ex_enter
.c_oflag
|= ONLCR
;
488 fast
: if (tcsetattr(STDIN_FILENO
, TCSADRAIN
| TCSASOFT
, &clp
->ex_enter
)) {
489 msgq(sp
, M_SYSERR
, "tcsetattr");
497 * Shutdown the ex screen.
514 * Retrieve termcap/terminfo strings.
516 * PUBLIC: int cl_getcap __P((SCR *, char *, char **));
519 cl_getcap(sp
, name
, elementp
)
521 char *name
, **elementp
;
526 if ((t
= tigetstr(name
)) != NULL
&&
527 t
!= (char *)-1 && (len
= strlen(t
)) != 0) {
528 MALLOC_RET(sp
, *elementp
, char *, len
+ 1);
529 memmove(*elementp
, t
, len
+ 1);
536 * Free any allocated termcap/terminfo strings.
542 if (clp
->el
!= NULL
) {
546 if (clp
->cup
!= NULL
) {
550 if (clp
->cuu1
!= NULL
) {
554 if (clp
->rmso
!= NULL
) {
558 if (clp
->smso
!= NULL
) {
566 * Put a value into the environment.
569 cl_putenv(name
, str
, value
)
577 (void)snprintf(buf
, sizeof(buf
), "%lu", value
);
578 return (setenv(name
, buf
, 1));
580 return (setenv(name
, str
, 1));