AMD64 - Fix just enough of the missing prototypes to help vi not crash
[dragonfly.git] / contrib / nvi / cl / cl_screen.c
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.
6  *
7  * See the LICENSE file for redistribution information.
8  *
9  * $FreeBSD: src/contrib/nvi/cl/cl_screen.c,v 1.1.1.1.8.1 2001/11/24 00:02:11 rwatson Exp $
10  * $DragonFly: src/contrib/nvi/cl/cl_screen.c,v 1.2 2003/06/17 04:24:04 dillon Exp $
11  */
12
13 #include "config.h"
14
15 #ifndef lint
16 static const char sccsid[] = "@(#)cl_screen.c   10.49 (Berkeley) 9/24/96";
17 #endif /* not lint */
18
19 #include <sys/types.h>
20 #include <sys/queue.h>
21
22 #include <bitstring.h>
23 #include <curses.h>
24 #include <term.h>
25 #include <errno.h>
26 #include <signal.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <termios.h>
31 #include <unistd.h>
32
33 #include "../common/common.h"
34 #include "cl.h"
35
36 static int      cl_ex_end __P((GS *));
37 static int      cl_ex_init __P((SCR *));
38 static void     cl_freecap __P((CL_PRIVATE *));
39 static int      cl_vi_end __P((GS *));
40 static int      cl_vi_init __P((SCR *));
41 static int      cl_putenv __P((char *, char *, u_long));
42
43 /*
44  * cl_screen --
45  *      Switch screen types.
46  *
47  * PUBLIC: int cl_screen __P((SCR *, u_int32_t));
48  */
49 int
50 cl_screen(sp, flags)
51         SCR *sp;
52         u_int32_t flags;
53 {
54         CL_PRIVATE *clp;
55         GS *gp;
56
57         gp = sp->gp;
58         clp = CLP(sp);
59
60         /* See if the current information is incorrect. */
61         if (F_ISSET(gp, G_SRESTART)) {
62                 if (cl_quit(gp))
63                         return (1);
64                 F_CLR(gp, G_SRESTART);
65         }
66         
67         /* See if we're already in the right mode. */
68         if (LF_ISSET(SC_EX) && F_ISSET(sp, SC_SCR_EX) ||
69             LF_ISSET(SC_VI) && F_ISSET(sp, SC_SCR_VI))
70                 return (0);
71
72         /*
73          * Fake leaving ex mode.
74          *
75          * We don't actually exit ex or vi mode unless forced (e.g. by a window
76          * size change).  This is because many curses implementations can't be
77          * called twice in a single program.  Plus, it's faster.  If the editor
78          * "leaves" vi to enter ex, when it exits ex we'll just fall back into
79          * vi.
80          */
81         if (F_ISSET(sp, SC_SCR_EX))
82                 F_CLR(sp, SC_SCR_EX);
83
84         /*
85          * Fake leaving vi mode.
86          *
87          * Clear out the rest of the screen if we're in the middle of a split
88          * screen.  Move to the last line in the current screen -- this makes
89          * terminal scrolling happen naturally.  Note: *don't* move past the
90          * end of the screen, as there are ex commands (e.g., :read ! cat file)
91          * that don't want to.  Don't clear the info line, its contents may be
92          * valid, e.g. :file|append.
93          */
94         if (F_ISSET(sp, SC_SCR_VI)) {
95                 F_CLR(sp, SC_SCR_VI);
96
97                 if (sp->q.cqe_next != (void *)&gp->dq) {
98                         (void)move(RLNO(sp, sp->rows), 0);
99                         clrtobot();
100                 }
101                 (void)move(RLNO(sp, sp->rows) - 1, 0);
102                 refresh();
103         }
104
105         /* Enter the requested mode. */
106         if (LF_ISSET(SC_EX)) {
107                 if (cl_ex_init(sp))
108                         return (1);
109                 F_SET(clp, CL_IN_EX | CL_SCR_EX_INIT);
110
111                 /*
112                  * If doing an ex screen for ex mode, move to the last line
113                  * on the screen.
114                  */
115                 if (F_ISSET(sp, SC_EX) && clp->cup != NULL)
116                         tputs(tgoto(clp->cup,
117                             0, O_VAL(sp, O_LINES) - 1), 1, cl_putchar);
118         } else {
119                 if (cl_vi_init(sp))
120                         return (1);
121                 F_CLR(clp, CL_IN_EX);
122                 F_SET(clp, CL_SCR_VI_INIT);
123         }
124         return (0);
125 }
126
127 /*
128  * cl_quit --
129  *      Shutdown the screens.
130  *
131  * PUBLIC: int cl_quit __P((GS *));
132  */
133 int
134 cl_quit(gp)
135         GS *gp;
136 {
137         CL_PRIVATE *clp;
138         int rval;
139
140         rval = 0;
141         clp = GCLP(gp);
142
143         /*
144          * If we weren't really running, ignore it.  This happens if the
145          * screen changes size before we've called curses.
146          */
147         if (!F_ISSET(clp, CL_SCR_EX_INIT | CL_SCR_VI_INIT))
148                 return (0);
149
150         /* Clean up the terminal mappings. */
151         if (cl_term_end(gp))
152                 rval = 1;
153
154         /* Really leave vi mode. */
155         if (F_ISSET(clp, CL_STDIN_TTY) &&
156             F_ISSET(clp, CL_SCR_VI_INIT) && cl_vi_end(gp))
157                 rval = 1;
158
159         /* Really leave ex mode. */
160         if (F_ISSET(clp, CL_STDIN_TTY) &&
161             F_ISSET(clp, CL_SCR_EX_INIT) && cl_ex_end(gp))
162                 rval = 1;
163
164         /*
165          * If we were running ex when we quit, or we're using an implementation
166          * of curses where endwin() doesn't get this right, restore the original
167          * terminal modes.
168          *
169          * XXX
170          * We always do this because it's too hard to figure out what curses
171          * implementations get it wrong.  It may discard type-ahead characters
172          * from the tty queue.
173          */
174         (void)tcsetattr(STDIN_FILENO, TCSADRAIN | TCSASOFT, &clp->orig);
175
176         F_CLR(clp, CL_SCR_EX_INIT | CL_SCR_VI_INIT);
177         return (rval);
178 }
179
180 /*
181  * cl_vi_init --
182  *      Initialize the curses vi screen.
183  */
184 static int
185 cl_vi_init(sp)
186         SCR *sp;
187 {
188         CL_PRIVATE *clp;
189         GS *gp;
190         char *o_cols, *o_lines, *o_term, *ttype;
191
192         gp = sp->gp;
193         clp = CLP(sp);
194
195         /* If already initialized, just set the terminal modes. */
196         if (F_ISSET(clp, CL_SCR_VI_INIT))
197                 goto fast;
198
199         /* Curses vi always reads from (and writes to) a terminal. */
200         if (!F_ISSET(clp, CL_STDIN_TTY) || !isatty(STDOUT_FILENO)) {
201                 msgq(sp, M_ERR,
202                     "016|Vi's standard input and output must be a terminal");
203                 return (1);
204         }
205
206         /* We'll need a terminal type. */
207         if (opts_empty(sp, O_TERM, 0))
208                 return (1);
209         ttype = O_STR(sp, O_TERM);
210
211         /*
212          * XXX
213          * Changing the row/column and terminal values is done by putting them
214          * into the environment, which is then read by curses.  What this loses
215          * in ugliness, it makes up for in stupidity.  We can't simply put the
216          * values into the environment ourselves, because in the presence of a
217          * kernel mechanism for returning the window size, entering values into
218          * the environment will screw up future screen resizing events, e.g. if
219          * the user enters a :shell command and then resizes their window.  So,
220          * if they weren't already in the environment, we make sure to delete
221          * them immediately after setting them.
222          *
223          * XXX
224          * Putting the TERM variable into the environment is necessary, even
225          * though we're using newterm() here.  We may be using initscr() as
226          * the underlying function.
227          */
228         o_term = getenv("TERM");
229         cl_putenv("TERM", ttype, 0);
230         o_lines = getenv("LINES");
231         cl_putenv("LINES", NULL, (u_long)O_VAL(sp, O_LINES));
232         o_cols = getenv("COLUMNS");
233         cl_putenv("COLUMNS", NULL, (u_long)O_VAL(sp, O_COLUMNS));
234
235         /*
236          * We don't care about the SCREEN reference returned by newterm, we
237          * never have more than one SCREEN at a time.
238          *
239          * XXX
240          * The SunOS initscr() can't be called twice.  Don't even think about
241          * using it.  It fails in subtle ways (e.g. select(2) on fileno(stdin)
242          * stops working).  (The SVID notes that applications should only call
243          * initscr() once.)
244          *
245          * XXX
246          * The HP/UX newterm doesn't support the NULL first argument, so we
247          * have to specify the terminal type.
248          */
249         errno = 0;
250         if (newterm(ttype, stdout, stdin) == NULL) {
251                 if (errno)
252                         msgq(sp, M_SYSERR, "%s", ttype);
253                 else
254                         msgq(sp, M_ERR, "%s: unknown terminal type", ttype);
255                 return (1);
256         }
257
258         if (o_term == NULL)
259                 unsetenv("TERM");
260         if (o_lines == NULL)
261                 unsetenv("LINES");
262         if (o_cols == NULL)
263                 unsetenv("COLUMNS");
264
265         /*
266          * XXX
267          * Someone got let out alone without adult supervision -- the SunOS
268          * newterm resets the signal handlers.  There's a race, but it's not
269          * worth closing.
270          */
271         (void)sig_init(sp->gp, sp);
272
273         /*
274          * We use raw mode.  What we want is 8-bit clean, however, signals
275          * and flow control should continue to work.  Admittedly, it sounds
276          * like cbreak, but it isn't.  Using cbreak() can get you additional
277          * things like IEXTEN, which turns on flags like DISCARD and LNEXT.
278          *
279          * !!!
280          * If raw isn't turning off echo and newlines, something's wrong.
281          * However, it shouldn't hurt.
282          */
283         noecho();                       /* No character echo. */
284         nonl();                         /* No CR/NL translation. */
285         raw();                          /* 8-bit clean. */
286         idlok(stdscr, 1);               /* Use hardware insert/delete line. */
287
288         /* Put the cursor keys into application mode. */
289         (void)keypad(stdscr, TRUE);
290
291         /*
292          * XXX
293          * The screen TI sequence just got sent.  See the comment in
294          * cl_funcs.c:cl_attr().
295          */
296         clp->ti_te = TI_SENT;
297
298         /*
299          * XXX
300          * Historic implementations of curses handled SIGTSTP signals
301          * in one of three ways.  They either:
302          *
303          *      1: Set their own handler, regardless.
304          *      2: Did not set a handler if a handler was already installed.
305          *      3: Set their own handler, but then called any previously set
306          *         handler after completing their own cleanup.
307          *
308          * We don't try and figure out which behavior is in place, we force
309          * it to SIG_DFL after initializing the curses interface, which means
310          * that curses isn't going to take the signal.  Since curses isn't
311          * reentrant (i.e., the whole curses SIGTSTP interface is a fantasy),
312          * we're doing The Right Thing.
313          */
314         (void)signal(SIGTSTP, SIG_DFL);
315
316         /*
317          * If flow control was on, turn it back on.  Turn signals on.  ISIG
318          * turns on VINTR, VQUIT, VDSUSP and VSUSP.   The main curses code
319          * already installed a handler for VINTR.  We're going to disable the
320          * other three.
321          *
322          * XXX
323          * We want to use ^Y as a vi scrolling command.  If the user has the
324          * DSUSP character set to ^Y (common practice) clean it up.  As it's
325          * equally possible that the user has VDSUSP set to 'a', we disable
326          * it regardless.  It doesn't make much sense to suspend vi at read,
327          * so I don't think anyone will care.  Alternatively, we could look
328          * it up in the table of legal command characters and turn it off if
329          * it matches one.  VDSUSP wasn't in POSIX 1003.1-1990, so we test for
330          * it.
331          *
332          * XXX
333          * We don't check to see if the user had signals enabled originally.
334          * If they didn't, it's unclear what we're supposed to do here, but
335          * it's also pretty unlikely.
336          */
337         if (tcgetattr(STDIN_FILENO, &clp->vi_enter)) {
338                 msgq(sp, M_SYSERR, "tcgetattr");
339                 goto err;
340         }
341         if (clp->orig.c_iflag & IXON)
342                 clp->vi_enter.c_iflag |= IXON;
343         if (clp->orig.c_iflag & IXOFF)
344                 clp->vi_enter.c_iflag |= IXOFF;
345
346         clp->vi_enter.c_lflag |= ISIG;
347 #ifdef VDSUSP
348         clp->vi_enter.c_cc[VDSUSP] = _POSIX_VDISABLE;
349 #endif
350         clp->vi_enter.c_cc[VQUIT] = _POSIX_VDISABLE;
351         clp->vi_enter.c_cc[VSUSP] = _POSIX_VDISABLE;
352
353         /*
354          * XXX
355          * OSF/1 doesn't turn off the <discard>, <literal-next> or <status>
356          * characters when curses switches into raw mode.  It should be OK
357          * to do it explicitly for everyone.
358          */
359 #ifdef VDISCARD
360         clp->vi_enter.c_cc[VDISCARD] = _POSIX_VDISABLE;
361 #endif
362 #ifdef VLNEXT
363         clp->vi_enter.c_cc[VLNEXT] = _POSIX_VDISABLE;
364 #endif
365 #ifdef VSTATUS
366         clp->vi_enter.c_cc[VSTATUS] = _POSIX_VDISABLE;
367 #endif
368
369         /* Initialize terminal based information. */
370         if (cl_term_init(sp))
371                 goto err;
372
373 fast:   /* Set the terminal modes. */
374         if (tcsetattr(STDIN_FILENO, TCSASOFT | TCSADRAIN, &clp->vi_enter)) {
375                 if (errno == EINTR)
376                         goto fast;
377                 msgq(sp, M_SYSERR, "tcsetattr");
378 err:            (void)cl_vi_end(sp->gp);
379                 return (1);
380         }
381         return (0);
382 }
383
384 /*
385  * cl_vi_end --
386  *      Shutdown the vi screen.
387  */
388 static int
389 cl_vi_end(gp)
390         GS *gp;
391 {
392         CL_PRIVATE *clp;
393
394         clp = GCLP(gp);
395
396         /* Restore the cursor keys to normal mode. */
397         (void)keypad(stdscr, FALSE);
398
399         /*
400          * If we were running vi when we quit, scroll the screen up a single
401          * line so we don't lose any information.
402          *
403          * Move to the bottom of the window (some endwin implementations don't
404          * do this for you).
405          */
406         if (!F_ISSET(clp, CL_IN_EX)) {
407                 (void)move(0, 0);
408                 (void)deleteln();
409                 (void)move(LINES - 1, 0);
410                 (void)refresh();
411         }
412
413         cl_freecap(clp);
414
415         /* End curses window. */
416         (void)endwin();
417
418         /*
419          * XXX
420          * The screen TE sequence just got sent.  See the comment in
421          * cl_funcs.c:cl_attr().
422          */
423         clp->ti_te = TE_SENT;
424
425         return (0);
426 }
427
428 /*
429  * cl_ex_init --
430  *      Initialize the ex screen.
431  */
432 static int
433 cl_ex_init(sp)
434         SCR *sp;
435 {
436         CL_PRIVATE *clp;
437
438         clp = CLP(sp);
439
440         /* If already initialized, just set the terminal modes. */
441         if (F_ISSET(clp, CL_SCR_EX_INIT))
442                 goto fast;
443
444         /* If not reading from a file, we're done. */
445         if (!F_ISSET(clp, CL_STDIN_TTY))
446                 return (0);
447
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);
454
455         /* Enter_standout_mode and exit_standout_mode are paired. */
456         if (clp->smso == NULL || clp->rmso == NULL) {
457                 if (clp->smso != NULL) {
458                         free(clp->smso);
459                         clp->smso = NULL;
460                 }
461                 if (clp->rmso != NULL) {
462                         free(clp->rmso);
463                         clp->rmso = NULL;
464                 }
465         }
466
467         /*
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
471          * current terminal.
472          *
473          * !!!
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.
479          */
480         clp->ex_enter = clp->orig;
481         clp->ex_enter.c_lflag  |= ECHO | ECHOE | ECHOK | ICANON | IEXTEN | ISIG;
482 #ifdef ECHOCTL
483         clp->ex_enter.c_lflag |= ECHOCTL;
484 #endif
485 #ifdef ECHOKE
486         clp->ex_enter.c_lflag |= ECHOKE;
487 #endif
488         clp->ex_enter.c_iflag |= ICRNL;
489         clp->ex_enter.c_oflag |= OPOST;
490 #ifdef ONLCR
491         clp->ex_enter.c_oflag |= ONLCR;
492 #endif
493
494 fast:   if (tcsetattr(STDIN_FILENO, TCSADRAIN | TCSASOFT, &clp->ex_enter)) {
495                 if (errno == EINTR)
496                         goto fast;
497                 msgq(sp, M_SYSERR, "tcsetattr");
498                 return (1);
499         }
500         return (0);
501 }
502
503 /*
504  * cl_ex_end --
505  *      Shutdown the ex screen.
506  */
507 static int
508 cl_ex_end(gp)
509         GS *gp;
510 {
511         CL_PRIVATE *clp;
512
513         clp = GCLP(gp);
514
515         cl_freecap(clp);
516
517         return (0);
518 }
519
520 /*
521  * cl_getcap --
522  *      Retrieve termcap/terminfo strings.
523  *
524  * PUBLIC: int cl_getcap __P((SCR *, char *, char **));
525  */
526 int
527 cl_getcap(sp, name, elementp)
528         SCR *sp;
529         char *name, **elementp;
530 {
531         size_t len;
532         char *t;
533
534         if ((t = tigetstr(name)) != NULL &&
535             t != (char *)-1 && (len = strlen(t)) != 0) {
536                 MALLOC_RET(sp, *elementp, char *, len + 1);
537                 memmove(*elementp, t, len + 1);
538         }
539         return (0);
540 }
541
542 /*
543  * cl_freecap --
544  *      Free any allocated termcap/terminfo strings.
545  */
546 static void
547 cl_freecap(clp)
548         CL_PRIVATE *clp;
549 {
550         if (clp->el != NULL) {
551                 free(clp->el);
552                 clp->el = NULL;
553         }
554         if (clp->cup != NULL) {
555                 free(clp->cup);
556                 clp->cup = NULL;
557         }
558         if (clp->cuu1 != NULL) {
559                 free(clp->cuu1);
560                 clp->cuu1 = NULL;
561         }
562         if (clp->rmso != NULL) {
563                 free(clp->rmso);
564                 clp->rmso = NULL;
565         }
566         if (clp->smso != NULL) {
567                 free(clp->smso);
568                 clp->smso = NULL;
569         }
570 }
571
572 /*
573  * cl_putenv --
574  *      Put a value into the environment.
575  */
576 static int
577 cl_putenv(name, str, value)
578         char *name, *str;
579         u_long value;
580
581 {
582         char buf[40];
583
584         if (str == NULL) {
585                 (void)snprintf(buf, sizeof(buf), "%lu", value);
586                 return (setenv(name, buf, 1));
587         } else
588                 return (setenv(name, str, 1));
589 }