use iconv for non-codeset fileencodings
[nvi.git] / cl / cl_screen.c
blobaff4a3aa8e49c130612cd35ba8031fb19f36e7fc
1 /*-
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.
8 */
10 #include "config.h"
12 #ifndef lint
13 static const char sccsid[] = "$Id: cl_screen.c,v 10.53 2000/07/23 11:14:45 skimo Exp $ (Berkeley) $Date: 2000/07/23 11:14:45 $";
14 #endif /* not lint */
16 #include <sys/types.h>
17 #include <sys/queue.h>
19 #include <bitstring.h>
20 #include <errno.h>
21 #include <signal.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <termios.h>
26 #include <unistd.h>
28 #include "../common/common.h"
29 #include "cl.h"
31 static int cl_ex_end __P((GS *));
32 static int cl_ex_init __P((SCR *));
33 static void cl_freecap __P((CL_PRIVATE *));
34 static int cl_vi_end __P((GS *));
35 static int cl_vi_init __P((SCR *));
36 static int cl_putenv __P((char *, char *, u_long));
39 * cl_screen --
40 * Switch screen types.
42 * PUBLIC: int cl_screen __P((SCR *, u_int32_t));
44 int
45 cl_screen(sp, flags)
46 SCR *sp;
47 u_int32_t flags;
49 CL_PRIVATE *clp;
50 WINDOW *win;
51 GS *gp;
53 gp = sp->gp;
54 clp = CLP(sp);
55 win = CLSP(sp) ? CLSP(sp) : stdscr;
57 /* See if the current information is incorrect. */
58 if (F_ISSET(gp, G_SRESTART)) {
59 if (CLSP(sp)) {
60 delwin(CLSP(sp));
61 CLSP(sp) = NULL;
63 if (cl_quit(gp))
64 return (1);
65 F_CLR(gp, G_SRESTART);
68 /* See if we're already in the right mode. */
69 if (LF_ISSET(SC_EX) && F_ISSET(sp, SC_SCR_EX) ||
70 LF_ISSET(SC_VI) && F_ISSET(sp, SC_SCR_VI))
71 return (0);
74 * Fake leaving ex mode.
76 * We don't actually exit ex or vi mode unless forced (e.g. by a window
77 * size change). This is because many curses implementations can't be
78 * called twice in a single program. Plus, it's faster. If the editor
79 * "leaves" vi to enter ex, when it exits ex we'll just fall back into
80 * vi.
82 if (F_ISSET(sp, SC_SCR_EX))
83 F_CLR(sp, SC_SCR_EX);
86 * Fake leaving vi mode.
88 * Clear out the rest of the screen if we're in the middle of a split
89 * screen. Move to the last line in the current screen -- this makes
90 * terminal scrolling happen naturally. Note: *don't* move past the
91 * end of the screen, as there are ex commands (e.g., :read ! cat file)
92 * that don't want to. Don't clear the info line, its contents may be
93 * valid, e.g. :file|append.
95 if (F_ISSET(sp, SC_SCR_VI)) {
96 F_CLR(sp, SC_SCR_VI);
98 if (sp->q.cqe_next != (void *)&sp->wp->scrq) {
99 (void)wmove(win, RLNO(sp, sp->rows), 0);
100 wclrtobot(win);
102 (void)wmove(win, RLNO(sp, sp->rows) - 1, 0);
103 wrefresh(win);
106 /* Enter the requested mode. */
107 if (LF_ISSET(SC_EX)) {
108 if (cl_ex_init(sp))
109 return (1);
110 F_SET(clp, CL_IN_EX | CL_SCR_EX_INIT);
113 * If doing an ex screen for ex mode, move to the last line
114 * on the screen.
116 if (F_ISSET(sp, SC_EX) && clp->cup != NULL)
117 tputs(tgoto(clp->cup,
118 0, O_VAL(sp, O_LINES) - 1), 1, cl_putchar);
119 } else {
120 if (cl_vi_init(sp))
121 return (1);
122 F_CLR(clp, CL_IN_EX);
123 F_SET(clp, CL_SCR_VI_INIT);
125 return (0);
129 * cl_quit --
130 * Shutdown the screens.
132 * PUBLIC: int cl_quit __P((GS *));
135 cl_quit(gp)
136 GS *gp;
138 CL_PRIVATE *clp;
139 int rval;
141 rval = 0;
142 clp = GCLP(gp);
145 * If we weren't really running, ignore it. This happens if the
146 * screen changes size before we've called curses.
148 if (!F_ISSET(clp, CL_SCR_EX_INIT | CL_SCR_VI_INIT))
149 return (0);
151 /* Clean up the terminal mappings. */
152 if (cl_term_end(gp))
153 rval = 1;
155 /* Really leave vi mode. */
156 if (F_ISSET(clp, CL_STDIN_TTY) &&
157 F_ISSET(clp, CL_SCR_VI_INIT) && cl_vi_end(gp))
158 rval = 1;
160 /* Really leave ex mode. */
161 if (F_ISSET(clp, CL_STDIN_TTY) &&
162 F_ISSET(clp, CL_SCR_EX_INIT) && cl_ex_end(gp))
163 rval = 1;
166 * If we were running ex when we quit, or we're using an implementation
167 * of curses where endwin() doesn't get this right, restore the original
168 * terminal modes.
170 * XXX
171 * We always do this because it's too hard to figure out what curses
172 * implementations get it wrong. It may discard type-ahead characters
173 * from the tty queue.
175 (void)tcsetattr(STDIN_FILENO, TCSADRAIN | TCSASOFT, &clp->orig);
177 F_CLR(clp, CL_SCR_EX_INIT | CL_SCR_VI_INIT);
178 return (rval);
182 * cl_vi_init --
183 * Initialize the curses vi screen.
185 static int
186 cl_vi_init(sp)
187 SCR *sp;
189 CL_PRIVATE *clp;
190 GS *gp;
191 char *o_cols, *o_lines, *o_term, *ttype;
193 gp = sp->gp;
194 clp = CLP(sp);
196 /* If already initialized, just set the terminal modes. */
197 if (F_ISSET(clp, CL_SCR_VI_INIT))
198 goto fast;
200 /* Curses vi always reads from (and writes to) a terminal. */
201 if (!F_ISSET(clp, CL_STDIN_TTY) || !isatty(STDOUT_FILENO)) {
202 msgq(sp, M_ERR,
203 "016|Vi's standard input and output must be a terminal");
204 return (1);
207 /* We'll need a terminal type. */
208 if (opts_empty(sp, O_TERM, 0))
209 return (1);
210 ttype = O_STR(sp, O_TERM);
213 * XXX
214 * Changing the row/column and terminal values is done by putting them
215 * into the environment, which is then read by curses. What this loses
216 * in ugliness, it makes up for in stupidity. We can't simply put the
217 * values into the environment ourselves, because in the presence of a
218 * kernel mechanism for returning the window size, entering values into
219 * the environment will screw up future screen resizing events, e.g. if
220 * the user enters a :shell command and then resizes their window. So,
221 * if they weren't already in the environment, we make sure to delete
222 * them immediately after setting them.
224 * XXX
225 * Putting the TERM variable into the environment is necessary, even
226 * though we're using newterm() here. We may be using initscr() as
227 * the underlying function.
229 o_term = getenv("TERM");
230 cl_putenv("TERM", ttype, 0);
231 o_lines = getenv("LINES");
232 cl_putenv("LINES", NULL, (u_long)O_VAL(sp, O_LINES));
233 o_cols = getenv("COLUMNS");
234 cl_putenv("COLUMNS", NULL, (u_long)O_VAL(sp, O_COLUMNS));
237 * We don't care about the SCREEN reference returned by newterm, we
238 * never have more than one SCREEN at a time.
240 * XXX
241 * The SunOS initscr() can't be called twice. Don't even think about
242 * using it. It fails in subtle ways (e.g. select(2) on fileno(stdin)
243 * stops working). (The SVID notes that applications should only call
244 * initscr() once.)
246 * XXX
247 * The HP/UX newterm doesn't support the NULL first argument, so we
248 * have to specify the terminal type.
250 errno = 0;
251 if (newterm(ttype, stdout, stdin) == NULL) {
252 if (errno)
253 msgq(sp, M_SYSERR, "%s", ttype);
254 else
255 msgq(sp, M_ERR, "%s: unknown terminal type", ttype);
256 return (1);
259 if (o_term == NULL)
260 unsetenv("TERM");
261 if (o_lines == NULL)
262 unsetenv("LINES");
263 if (o_cols == NULL)
264 unsetenv("COLUMNS");
267 * XXX
268 * Someone got let out alone without adult supervision -- the SunOS
269 * newterm resets the signal handlers. There's a race, but it's not
270 * worth closing.
272 (void)sig_init(sp->gp, sp);
275 * We use raw mode. What we want is 8-bit clean, however, signals
276 * and flow control should continue to work. Admittedly, it sounds
277 * like cbreak, but it isn't. Using cbreak() can get you additional
278 * things like IEXTEN, which turns on flags like DISCARD and LNEXT.
280 * !!!
281 * If raw isn't turning off echo and newlines, something's wrong.
282 * However, it shouldn't hurt.
284 noecho(); /* No character echo. */
285 nonl(); /* No CR/NL translation. */
286 raw(); /* 8-bit clean. */
287 idlok(stdscr, 1); /* Use hardware insert/delete line. */
289 /* Put the cursor keys into application mode. */
290 (void)keypad(stdscr, TRUE);
293 * XXX
294 * The screen TI sequence just got sent. See the comment in
295 * cl_funcs.c:cl_attr().
297 clp->ti_te = TI_SENT;
300 * XXX
301 * Historic implementations of curses handled SIGTSTP signals
302 * in one of three ways. They either:
304 * 1: Set their own handler, regardless.
305 * 2: Did not set a handler if a handler was already installed.
306 * 3: Set their own handler, but then called any previously set
307 * handler after completing their own cleanup.
309 * We don't try and figure out which behavior is in place, we force
310 * it to SIG_DFL after initializing the curses interface, which means
311 * that curses isn't going to take the signal. Since curses isn't
312 * reentrant (i.e., the whole curses SIGTSTP interface is a fantasy),
313 * we're doing The Right Thing.
315 (void)signal(SIGTSTP, SIG_DFL);
318 * If flow control was on, turn it back on. Turn signals on. ISIG
319 * turns on VINTR, VQUIT, VDSUSP and VSUSP. The main curses code
320 * already installed a handler for VINTR. We're going to disable the
321 * other three.
323 * XXX
324 * We want to use ^Y as a vi scrolling command. If the user has the
325 * DSUSP character set to ^Y (common practice) clean it up. As it's
326 * equally possible that the user has VDSUSP set to 'a', we disable
327 * it regardless. It doesn't make much sense to suspend vi at read,
328 * so I don't think anyone will care. Alternatively, we could look
329 * it up in the table of legal command characters and turn it off if
330 * it matches one. VDSUSP wasn't in POSIX 1003.1-1990, so we test for
331 * it.
333 * XXX
334 * We don't check to see if the user had signals enabled originally.
335 * If they didn't, it's unclear what we're supposed to do here, but
336 * it's also pretty unlikely.
338 if (tcgetattr(STDIN_FILENO, &clp->vi_enter)) {
339 msgq(sp, M_SYSERR, "tcgetattr");
340 goto err;
342 if (clp->orig.c_iflag & IXON)
343 clp->vi_enter.c_iflag |= IXON;
344 if (clp->orig.c_iflag & IXOFF)
345 clp->vi_enter.c_iflag |= IXOFF;
347 clp->vi_enter.c_lflag |= ISIG;
348 #ifdef VDSUSP
349 clp->vi_enter.c_cc[VDSUSP] = _POSIX_VDISABLE;
350 #endif
351 clp->vi_enter.c_cc[VQUIT] = _POSIX_VDISABLE;
352 clp->vi_enter.c_cc[VSUSP] = _POSIX_VDISABLE;
355 * XXX
356 * OSF/1 doesn't turn off the <discard>, <literal-next> or <status>
357 * characters when curses switches into raw mode. It should be OK
358 * to do it explicitly for everyone.
360 #ifdef VDISCARD
361 clp->vi_enter.c_cc[VDISCARD] = _POSIX_VDISABLE;
362 #endif
363 #ifdef VLNEXT
364 clp->vi_enter.c_cc[VLNEXT] = _POSIX_VDISABLE;
365 #endif
366 #ifdef VSTATUS
367 clp->vi_enter.c_cc[VSTATUS] = _POSIX_VDISABLE;
368 #endif
370 /* Initialize terminal based information. */
371 if (cl_term_init(sp))
372 goto err;
374 fast: /* Set the terminal modes. */
375 if (tcsetattr(STDIN_FILENO, TCSASOFT | TCSADRAIN, &clp->vi_enter)) {
376 msgq(sp, M_SYSERR, "tcsetattr");
377 err: (void)cl_vi_end(sp->gp);
378 return (1);
380 return (0);
384 * cl_vi_end --
385 * Shutdown the vi screen.
387 static int
388 cl_vi_end(gp)
389 GS *gp;
391 CL_PRIVATE *clp;
393 clp = GCLP(gp);
395 /* Restore the cursor keys to normal mode. */
396 (void)keypad(stdscr, FALSE);
399 * If we were running vi when we quit, scroll the screen up a single
400 * line so we don't lose any information.
402 * Move to the bottom of the window (some endwin implementations don't
403 * do this for you).
405 if (!F_ISSET(clp, CL_IN_EX)) {
406 (void)move(0, 0);
407 (void)deleteln();
408 (void)move(LINES - 1, 0);
409 (void)refresh();
412 cl_freecap(clp);
414 /* End curses window. */
415 (void)endwin();
418 * XXX
419 * The screen TE sequence just got sent. See the comment in
420 * cl_funcs.c:cl_attr().
422 clp->ti_te = TE_SENT;
424 return (0);
428 * cl_ex_init --
429 * Initialize the ex screen.
431 static int
432 cl_ex_init(sp)
433 SCR *sp;
435 CL_PRIVATE *clp;
437 clp = CLP(sp);
439 /* If already initialized, just set the terminal modes. */
440 if (F_ISSET(clp, CL_SCR_EX_INIT))
441 goto fast;
443 /* If not reading from a file, we're done. */
444 if (!F_ISSET(clp, CL_STDIN_TTY))
445 return (0);
447 /* Get the ex termcap/terminfo strings. */
448 (void)cl_getcap(sp, "cup", &clp->cup);
449 (void)cl_getcap(sp, "smso", &clp->smso);
450 (void)cl_getcap(sp, "rmso", &clp->rmso);
451 (void)cl_getcap(sp, "el", &clp->el);
452 (void)cl_getcap(sp, "cuu1", &clp->cuu1);
454 /* Enter_standout_mode and exit_standout_mode are paired. */
455 if (clp->smso == NULL || clp->rmso == NULL) {
456 if (clp->smso != NULL) {
457 free(clp->smso);
458 clp->smso = NULL;
460 if (clp->rmso != NULL) {
461 free(clp->rmso);
462 clp->rmso = NULL;
467 * Turn on canonical mode, with normal input and output processing.
468 * Start with the original terminal settings as the user probably
469 * had them (including any local extensions) set correctly for the
470 * current terminal.
472 * !!!
473 * We can't get everything that we need portably; for example, ONLCR,
474 * mapping <newline> to <carriage-return> on output isn't required
475 * by POSIX 1003.1b-1993. If this turns out to be a problem, then
476 * we'll either have to play some games on the mapping, or we'll have
477 * to make all ex printf's output \r\n instead of \n.
479 clp->ex_enter = clp->orig;
480 clp->ex_enter.c_lflag |= ECHO | ECHOE | ECHOK | ICANON | IEXTEN | ISIG;
481 #ifdef ECHOCTL
482 clp->ex_enter.c_lflag |= ECHOCTL;
483 #endif
484 #ifdef ECHOKE
485 clp->ex_enter.c_lflag |= ECHOKE;
486 #endif
487 clp->ex_enter.c_iflag |= ICRNL;
488 clp->ex_enter.c_oflag |= OPOST;
489 #ifdef ONLCR
490 clp->ex_enter.c_oflag |= ONLCR;
491 #endif
493 fast: if (tcsetattr(STDIN_FILENO, TCSADRAIN | TCSASOFT, &clp->ex_enter)) {
494 msgq(sp, M_SYSERR, "tcsetattr");
495 return (1);
497 return (0);
501 * cl_ex_end --
502 * Shutdown the ex screen.
504 static int
505 cl_ex_end(gp)
506 GS *gp;
508 CL_PRIVATE *clp;
510 clp = GCLP(gp);
512 cl_freecap(clp);
514 return (0);
518 * cl_getcap --
519 * Retrieve termcap/terminfo strings.
521 * PUBLIC: int cl_getcap __P((SCR *, char *, char **));
524 cl_getcap(sp, name, elementp)
525 SCR *sp;
526 char *name, **elementp;
528 size_t len;
529 char *t;
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);
536 return (0);
540 * cl_freecap --
541 * Free any allocated termcap/terminfo strings.
543 static void
544 cl_freecap(clp)
545 CL_PRIVATE *clp;
547 if (clp->el != NULL) {
548 free(clp->el);
549 clp->el = NULL;
551 if (clp->cup != NULL) {
552 free(clp->cup);
553 clp->cup = NULL;
555 if (clp->cuu1 != NULL) {
556 free(clp->cuu1);
557 clp->cuu1 = NULL;
559 if (clp->rmso != NULL) {
560 free(clp->rmso);
561 clp->rmso = NULL;
563 if (clp->smso != NULL) {
564 free(clp->smso);
565 clp->smso = NULL;
570 * cl_putenv --
571 * Put a value into the environment.
573 static int
574 cl_putenv(name, str, value)
575 char *name, *str;
576 u_long value;
579 char buf[40];
581 if (str == NULL) {
582 (void)snprintf(buf, sizeof(buf), "%lu", value);
583 return (setenv(name, buf, 1));
584 } else
585 return (setenv(name, str, 1));