1 /* $NetBSD: cl_screen.c,v 1.4 2014/01/26 21:43:45 christos Exp $ */
3 * Copyright (c) 1993, 1994
4 * The Regents of the University of California. All rights reserved.
5 * Copyright (c) 1993, 1994, 1995, 1996
6 * Keith Bostic. All rights reserved.
8 * See the LICENSE file for redistribution information.
13 #include <sys/cdefs.h>
16 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 ";
19 __RCSID("$NetBSD: cl_screen.c,v 1.4 2014/01/26 21:43:45 christos Exp $");
22 #include <sys/types.h>
23 #include <sys/queue.h>
25 #include <bitstring.h>
34 #include "../common/common.h"
37 static int cl_ex_end
__P((GS
*));
38 static int cl_ex_init
__P((SCR
*));
39 static void cl_freecap
__P((CL_PRIVATE
*));
40 static int cl_vi_end
__P((GS
*));
41 static int cl_vi_init
__P((SCR
*));
42 static int cl_putenv
__P((SCR
*, const char *, const char *, u_long
));
46 * Switch screen types.
48 * PUBLIC: int cl_screen __P((SCR *, u_int32_t));
51 cl_screen(SCR
*sp
, u_int32_t flags
)
59 win
= CLSP(sp
) ? CLSP(sp
) : stdscr
;
61 /* See if the current information is incorrect. */
62 if (F_ISSET(gp
, G_SRESTART
)) {
65 sp
->cl_private
= NULL
;
69 F_CLR(gp
, G_SRESTART
);
72 /* See if we're already in the right mode. */
73 if ((LF_ISSET(SC_EX
) && F_ISSET(sp
, SC_SCR_EX
)) ||
74 (LF_ISSET(SC_VI
) && F_ISSET(sp
, SC_SCR_VI
)))
78 * Fake leaving ex mode.
80 * We don't actually exit ex or vi mode unless forced (e.g. by a window
81 * size change). This is because many curses implementations can't be
82 * called twice in a single program. Plus, it's faster. If the editor
83 * "leaves" vi to enter ex, when it exits ex we'll just fall back into
86 if (F_ISSET(sp
, SC_SCR_EX
))
90 * Fake leaving vi mode.
92 * Clear out the rest of the screen if we're in the middle of a split
93 * screen. Move to the last line in the current screen -- this makes
94 * terminal scrolling happen naturally. Note: *don't* move past the
95 * end of the screen, as there are ex commands (e.g., :read ! cat file)
96 * that don't want to. Don't clear the info line, its contents may be
97 * valid, e.g. :file|append.
99 if (F_ISSET(sp
, SC_SCR_VI
)) {
100 F_CLR(sp
, SC_SCR_VI
);
102 if (TAILQ_NEXT(sp
, q
) != NULL
) {
103 (void)wmove(win
, RLNO(sp
, sp
->rows
), 0);
106 (void)wmove(win
, RLNO(sp
, sp
->rows
) - 1, 0);
110 /* Enter the requested mode. */
111 if (LF_ISSET(SC_EX
)) {
114 F_SET(clp
, CL_IN_EX
| CL_SCR_EX_INIT
);
117 * If doing an ex screen for ex mode, move to the last line
120 if (F_ISSET(sp
, SC_EX
) && clp
->cup
!= NULL
)
121 tputs(tgoto(clp
->cup
,
122 0, O_VAL(sp
, O_LINES
) - 1), 1, cl_putchar
);
126 F_CLR(clp
, CL_IN_EX
);
127 F_SET(clp
, CL_SCR_VI_INIT
);
134 * Shutdown the screens.
136 * PUBLIC: int cl_quit __P((GS *));
148 * If we weren't really running, ignore it. This happens if the
149 * screen changes size before we've called curses.
151 if (!F_ISSET(clp
, CL_SCR_EX_INIT
| CL_SCR_VI_INIT
))
154 /* Clean up the terminal mappings. */
158 /* Really leave vi mode. */
159 if (F_ISSET(clp
, CL_STDIN_TTY
) &&
160 F_ISSET(clp
, CL_SCR_VI_INIT
) && cl_vi_end(gp
))
163 /* Really leave ex mode. */
164 if (F_ISSET(clp
, CL_STDIN_TTY
) &&
165 F_ISSET(clp
, CL_SCR_EX_INIT
) && cl_ex_end(gp
))
169 * If we were running ex when we quit, or we're using an implementation
170 * of curses where endwin() doesn't get this right, restore the original
174 * We always do this because it's too hard to figure out what curses
175 * implementations get it wrong. It may discard type-ahead characters
176 * from the tty queue.
178 (void)tcsetattr(STDIN_FILENO
, TCSADRAIN
| TCSASOFT
, &clp
->orig
);
180 F_CLR(clp
, CL_SCR_EX_INIT
| CL_SCR_VI_INIT
);
186 * Initialize the curses vi screen.
192 char *o_cols
, *o_lines
, *o_term
;
197 /* If already initialized, just set the terminal modes. */
198 if (F_ISSET(clp
, CL_SCR_VI_INIT
))
201 /* Curses vi always reads from (and writes to) a terminal. */
202 if (!F_ISSET(clp
, CL_STDIN_TTY
) || !isatty(STDOUT_FILENO
)) {
204 "016|Vi's standard input and output must be a terminal");
208 /* We'll need a terminal type. */
209 if (opts_empty(sp
, O_TERM
, 0))
211 ttype
= O_STR(sp
, O_TERM
);
215 * Changing the row/column and terminal values is done by putting them
216 * into the environment, which is then read by curses. What this loses
217 * in ugliness, it makes up for in stupidity. We can't simply put the
218 * values into the environment ourselves, because in the presence of a
219 * kernel mechanism for returning the window size, entering values into
220 * the environment will screw up future screen resizing events, e.g. if
221 * the user enters a :shell command and then resizes their window. So,
222 * if they weren't already in the environment, we make sure to delete
223 * them immediately after setting them.
226 * Putting the TERM variable into the environment is necessary, even
227 * though we're using newterm() here. We may be using initscr() as
228 * the underlying function.
230 o_term
= getenv("TERM");
231 cl_putenv(sp
, "TERM", ttype
, 0);
232 o_lines
= getenv("LINES");
233 cl_putenv(sp
, "LINES", NULL
, (u_long
)O_VAL(sp
, O_LINES
));
234 o_cols
= getenv("COLUMNS");
235 cl_putenv(sp
, "COLUMNS", NULL
, (u_long
)O_VAL(sp
, O_COLUMNS
));
238 * We don't care about the SCREEN reference returned by newterm, we
239 * never have more than one SCREEN at a time.
242 * The SunOS initscr() can't be called twice. Don't even think about
243 * using it. It fails in subtle ways (e.g. select(2) on fileno(stdin)
244 * stops working). (The SVID notes that applications should only call
248 * The HP/UX newterm doesn't support the NULL first argument, so we
249 * have to specify the terminal type.
252 if (newterm(__UNCONST(ttype
), stdout
, stdin
) == NULL
) {
254 msgq(sp
, M_SYSERR
, "%s", ttype
);
256 msgq(sp
, M_ERR
, "%s: unknown terminal type", ttype
);
261 cl_unsetenv(sp
, "TERM");
263 cl_unsetenv(sp
, "LINES");
265 cl_unsetenv(sp
, "COLUMNS");
269 * Someone got let out alone without adult supervision -- the SunOS
270 * newterm resets the signal handlers. There's a race, but it's not
273 (void)sig_init(sp
->gp
, sp
);
276 * We use raw mode. What we want is 8-bit clean, however, signals
277 * and flow control should continue to work. Admittedly, it sounds
278 * like cbreak, but it isn't. Using cbreak() can get you additional
279 * things like IEXTEN, which turns on flags like DISCARD and LNEXT.
282 * If raw isn't turning off echo and newlines, something's wrong.
283 * However, it shouldn't hurt.
285 noecho(); /* No character echo. */
286 nonl(); /* No CR/NL translation. */
287 raw(); /* 8-bit clean. */
288 idlok(stdscr
, 1); /* Use hardware insert/delete line. */
290 /* Put the cursor keys into application mode. */
291 (void)keypad(stdscr
, TRUE
);
295 * The screen TI sequence just got sent. See the comment in
296 * cl_funcs.c:cl_attr().
298 clp
->ti_te
= TI_SENT
;
302 * Historic implementations of curses handled SIGTSTP signals
303 * in one of three ways. They either:
305 * 1: Set their own handler, regardless.
306 * 2: Did not set a handler if a handler was already installed.
307 * 3: Set their own handler, but then called any previously set
308 * handler after completing their own cleanup.
310 * We don't try and figure out which behavior is in place, we force
311 * it to SIG_DFL after initializing the curses interface, which means
312 * that curses isn't going to take the signal. Since curses isn't
313 * reentrant (i.e., the whole curses SIGTSTP interface is a fantasy),
314 * we're doing The Right Thing.
316 (void)signal(SIGTSTP
, SIG_DFL
);
319 * If flow control was on, turn it back on. Turn signals on. ISIG
320 * turns on VINTR, VQUIT, VDSUSP and VSUSP. The main curses code
321 * already installed a handler for VINTR. We're going to disable the
325 * We want to use ^Y as a vi scrolling command. If the user has the
326 * DSUSP character set to ^Y (common practice) clean it up. As it's
327 * equally possible that the user has VDSUSP set to 'a', we disable
328 * it regardless. It doesn't make much sense to suspend vi at read,
329 * so I don't think anyone will care. Alternatively, we could look
330 * it up in the table of legal command characters and turn it off if
331 * it matches one. VDSUSP wasn't in POSIX 1003.1-1990, so we test for
335 * We don't check to see if the user had signals enabled originally.
336 * If they didn't, it's unclear what we're supposed to do here, but
337 * it's also pretty unlikely.
339 if (tcgetattr(STDIN_FILENO
, &clp
->vi_enter
)) {
340 msgq(sp
, M_SYSERR
, "tcgetattr");
343 if (clp
->orig
.c_iflag
& IXON
)
344 clp
->vi_enter
.c_iflag
|= IXON
;
345 if (clp
->orig
.c_iflag
& IXOFF
)
346 clp
->vi_enter
.c_iflag
|= IXOFF
;
348 clp
->vi_enter
.c_lflag
|= ISIG
;
350 clp
->vi_enter
.c_cc
[VDSUSP
] = _POSIX_VDISABLE
;
352 clp
->vi_enter
.c_cc
[VQUIT
] = _POSIX_VDISABLE
;
353 clp
->vi_enter
.c_cc
[VSUSP
] = _POSIX_VDISABLE
;
357 * OSF/1 doesn't turn off the <discard>, <literal-next> or <status>
358 * characters when curses switches into raw mode. It should be OK
359 * to do it explicitly for everyone.
362 clp
->vi_enter
.c_cc
[VDISCARD
] = _POSIX_VDISABLE
;
365 clp
->vi_enter
.c_cc
[VLNEXT
] = _POSIX_VDISABLE
;
368 clp
->vi_enter
.c_cc
[VSTATUS
] = _POSIX_VDISABLE
;
371 /* Initialize terminal based information. */
372 if (cl_term_init(sp
))
375 fast
: /* Set the terminal modes. */
376 if (tcsetattr(STDIN_FILENO
, TCSASOFT
| TCSADRAIN
, &clp
->vi_enter
)) {
379 msgq(sp
, M_SYSERR
, "tcsetattr");
380 err
: (void)cl_vi_end(sp
->gp
);
388 * Shutdown the vi screen.
397 /* Restore the cursor keys to normal mode. */
398 (void)keypad(stdscr
, FALSE
);
401 * If we were running vi when we quit, scroll the screen up a single
402 * line so we don't lose any information.
404 * Move to the bottom of the window (some endwin implementations don't
407 if (!F_ISSET(clp
, CL_IN_EX
)) {
410 (void)move(LINES
- 1, 0);
416 /* End curses window. */
421 * The screen TE sequence just got sent. See the comment in
422 * cl_funcs.c:cl_attr().
424 clp
->ti_te
= TE_SENT
;
431 * Initialize the ex screen.
440 /* If already initialized, just set the terminal modes. */
441 if (F_ISSET(clp
, CL_SCR_EX_INIT
))
444 /* If not reading from a file, we're done. */
445 if (!F_ISSET(clp
, CL_STDIN_TTY
))
448 /* Get the ex termcap/terminfo strings. */
449 (void)cl_getcap(sp
, "cup", &clp
->cup
);
450 (void)cl_getcap(sp
, "smso", &clp
->smso
);
451 (void)cl_getcap(sp
, "rmso", &clp
->rmso
);
452 (void)cl_getcap(sp
, "el", &clp
->el
);
453 (void)cl_getcap(sp
, "cuu1", &clp
->cuu1
);
455 /* Enter_standout_mode and exit_standout_mode are paired. */
456 if (clp
->smso
== NULL
|| clp
->rmso
== NULL
) {
457 if (clp
->smso
!= NULL
) {
461 if (clp
->rmso
!= NULL
) {
468 * Turn on canonical mode, with normal input and output processing.
469 * Start with the original terminal settings as the user probably
470 * had them (including any local extensions) set correctly for the
474 * We can't get everything that we need portably; for example, ONLCR,
475 * mapping <newline> to <carriage-return> on output isn't required
476 * by POSIX 1003.1b-1993. If this turns out to be a problem, then
477 * we'll either have to play some games on the mapping, or we'll have
478 * to make all ex printf's output \r\n instead of \n.
480 clp
->ex_enter
= clp
->orig
;
481 clp
->ex_enter
.c_lflag
|= ECHO
| ECHOE
| ECHOK
| ICANON
| IEXTEN
| ISIG
;
483 clp
->ex_enter
.c_lflag
|= ECHOCTL
;
486 clp
->ex_enter
.c_lflag
|= ECHOKE
;
488 clp
->ex_enter
.c_iflag
|= ICRNL
;
489 clp
->ex_enter
.c_oflag
|= OPOST
;
491 clp
->ex_enter
.c_oflag
|= ONLCR
;
494 fast
: if (tcsetattr(STDIN_FILENO
, TCSADRAIN
| TCSASOFT
, &clp
->ex_enter
)) {
497 msgq(sp
, M_SYSERR
, "tcsetattr");
505 * Shutdown the ex screen.
521 * Retrieve termcap/terminfo strings.
523 * PUBLIC: int cl_getcap __P((SCR *, const char *, char **));
526 cl_getcap(SCR
*sp
, const char *name
, char **elementp
)
531 if ((t
= tigetstr(name
)) != NULL
&&
532 t
!= (char *)-1 && (len
= strlen(t
)) != 0) {
533 MALLOC_RET(sp
, *elementp
, char *, len
+ 1);
534 memmove(*elementp
, t
, len
+ 1);
541 * Free any allocated termcap/terminfo strings.
544 cl_freecap(CL_PRIVATE
*clp
)
546 if (clp
->el
!= NULL
) {
550 if (clp
->cup
!= NULL
) {
554 if (clp
->cuu1
!= NULL
) {
558 if (clp
->rmso
!= NULL
) {
562 if (clp
->smso
!= NULL
) {
570 * Put a value into the environment.
573 cl_putenv(SCR
*sp
, const char *name
, const char *str
, u_long value
)
578 (void)snprintf(buf
, sizeof(buf
), "%lu", value
);
579 return (cl_setenv(sp
, name
, buf
));
581 return (cl_setenv(sp
, name
, str
));