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_term.c,v 10.22 1996/09/15 15:56:26 bostic Exp $ (Berkeley) $Date: 1996/09/15 15:56:26 $";
16 #include <sys/types.h>
17 #include <sys/ioctl.h>
18 #include <sys/queue.h>
21 #include <bitstring.h>
32 #include "../common/common.h"
35 static int cl_pfmap
__P((SCR
*, seq_t
, CHAR_T
*, size_t, CHAR_T
*, size_t));
39 * THIS REQUIRES THAT ALL SCREENS SHARE A TERMINAL TYPE.
41 typedef struct _tklist
{
42 char *ts
; /* Key's termcap string. */
43 char *output
; /* Corresponding vi command. */
44 char *name
; /* Name. */
45 u_char value
; /* Special value (for lookup). */
47 static TKLIST
const c_tklist
[] = { /* Command mappings. */
48 {"kil1", "O", "insert line"},
49 {"kdch1", "x", "delete character"},
50 {"kcud1", "j", "cursor down"},
51 {"kel", "D", "delete to eol"},
52 {"kind", "\004", "scroll down"}, /* ^D */
53 {"kll", "$", "go to eol"},
54 {"khome", "^", "go to sol"},
55 {"kich1", "i", "insert at cursor"},
56 {"kdl1", "dd", "delete line"},
57 {"kcub1", "h", "cursor left"},
58 {"knp", "\006", "page down"}, /* ^F */
59 {"kpp", "\002", "page up"}, /* ^B */
60 {"kri", "\025", "scroll up"}, /* ^U */
61 {"ked", "dG", "delete to end of screen"},
62 {"kcuf1", "l", "cursor right"},
63 {"kcuu1", "k", "cursor up"},
66 static TKLIST
const m1_tklist
[] = { /* Input mappings (lookup). */
69 static TKLIST
const m2_tklist
[] = { /* Input mappings (set or delete). */
70 {"kcud1", "\033ja", "cursor down"}, /* ^[ja */
71 {"kcub1", "\033ha", "cursor left"}, /* ^[ha */
72 {"kcuu1", "\033ka", "cursor up"}, /* ^[ka */
73 {"kcuf1", "\033la", "cursor right"}, /* ^[la */
79 * Initialize the special keys defined by the termcap/terminfo entry.
81 * PUBLIC: int cl_term_init __P((SCR *));
92 /* Command mappings. */
93 for (tkp
= c_tklist
; tkp
->name
!= NULL
; ++tkp
) {
94 if ((t
= tigetstr(tkp
->ts
)) == NULL
|| t
== (char *)-1)
96 if (seq_set(sp
, tkp
->name
, strlen(tkp
->name
), t
, strlen(t
),
97 tkp
->output
, strlen(tkp
->output
), SEQ_COMMAND
,
98 SEQ_NOOVERWRITE
| SEQ_SCREEN
))
102 /* Input mappings needing to be looked up. */
103 for (tkp
= m1_tklist
; tkp
->name
!= NULL
; ++tkp
) {
104 if ((t
= tigetstr(tkp
->ts
)) == NULL
|| t
== (char *)-1)
106 for (kp
= keylist
;; ++kp
)
107 if (kp
->value
== tkp
->value
)
111 if (seq_set(sp
, tkp
->name
, strlen(tkp
->name
), t
, strlen(t
),
112 &kp
->ch
, 1, SEQ_INPUT
, SEQ_NOOVERWRITE
| SEQ_SCREEN
))
116 /* Input mappings that are already set or are text deletions. */
117 for (tkp
= m2_tklist
; tkp
->name
!= NULL
; ++tkp
) {
118 if ((t
= tigetstr(tkp
->ts
)) == NULL
|| t
== (char *)-1)
122 * Some terminals' <cursor_left> keys send single <backspace>
123 * characters. This is okay in command mapping, but not okay
124 * in input mapping. That combination is the only one we'll
125 * ever see, hopefully, so kluge it here for now.
127 if (!strcmp(t
, "\b"))
129 if (tkp
->output
== NULL
) {
130 if (seq_set(sp
, tkp
->name
, strlen(tkp
->name
),
131 t
, strlen(t
), NULL
, 0,
132 SEQ_INPUT
, SEQ_NOOVERWRITE
| SEQ_SCREEN
))
135 if (seq_set(sp
, tkp
->name
, strlen(tkp
->name
),
136 t
, strlen(t
), tkp
->output
, strlen(tkp
->output
),
137 SEQ_INPUT
, SEQ_NOOVERWRITE
| SEQ_SCREEN
))
142 * Rework any function key mappings that were set before the
143 * screen was initialized.
145 for (qp
= sp
->gp
->seqq
.lh_first
; qp
!= NULL
; qp
= qp
->q
.le_next
)
146 if (F_ISSET(qp
, SEQ_FUNCMAP
))
147 (void)cl_pfmap(sp
, qp
->stype
,
148 qp
->input
, qp
->ilen
, qp
->output
, qp
->olen
);
154 * End the special keys defined by the termcap/terminfo entry.
156 * PUBLIC: int cl_term_end __P((GS *));
164 /* Delete screen specific mappings. */
165 for (qp
= gp
->seqq
.lh_first
; qp
!= NULL
; qp
= nqp
) {
167 if (F_ISSET(qp
, SEQ_SCREEN
))
175 * Map a function key.
177 * PUBLIC: int cl_fmap __P((SCR *, seq_t, CHAR_T *, size_t, CHAR_T *, size_t));
180 cl_fmap(sp
, stype
, from
, flen
, to
, tlen
)
186 /* Ignore until the screen is running, do the real work then. */
187 if (F_ISSET(sp
, SC_VI
) && !F_ISSET(sp
, SC_SCR_VI
))
189 if (F_ISSET(sp
, SC_EX
) && !F_ISSET(sp
, SC_SCR_EX
))
192 return (cl_pfmap(sp
, stype
, from
, flen
, to
, tlen
));
197 * Map a function key (private version).
200 cl_pfmap(sp
, stype
, from
, flen
, to
, tlen
)
207 char *p
, keyname
[64];
209 (void)snprintf(keyname
, sizeof(keyname
), "kf%d", atoi(from
+ 1));
210 if ((p
= tigetstr(keyname
)) == NULL
||
211 p
== (char *)-1 || strlen(p
) == 0)
214 msgq_str(sp
, M_ERR
, from
, "233|This terminal has no %s key");
218 nlen
= snprintf(keyname
,
219 sizeof(keyname
), "function key %d", atoi(from
+ 1));
220 return (seq_set(sp
, keyname
, nlen
,
221 p
, strlen(p
), to
, tlen
, stype
, SEQ_NOOVERWRITE
| SEQ_SCREEN
));
226 * Curses screen specific "option changed" routine.
228 * PUBLIC: int cl_optchange __P((SCR *, int, char *, u_long *));
231 cl_optchange(sp
, opt
, str
, valp
)
246 * Changing the columns, lines or terminal require that
247 * we restart the screen.
249 F_SET(sp
->gp
, G_SRESTART
);
250 F_CLR(sp
, SC_SCR_EX
| SC_SCR_VI
);
253 (void)cl_omesg(sp
, clp
, !*valp
);
257 F_CLR(clp
, CL_RENAME_OK
);
259 (void)cl_rename(sp
, NULL
, 0);
261 F_SET(clp
, CL_RENAME_OK
);
264 * If the screen is live, i.e. we're not reading the
265 * .exrc file, update the window.
267 if (sp
->frp
!= NULL
&& sp
->frp
->name
!= NULL
)
268 (void)cl_rename(sp
, sp
->frp
->name
, 1);
277 * Turn the tty write permission on or off.
279 * PUBLIC: int cl_omesg __P((SCR *, CL_PRIVATE *, int));
282 cl_omesg(sp
, clp
, on
)
290 /* Find the tty, get the current permissions. */
291 if ((tty
= ttyname(STDERR_FILENO
)) == NULL
) {
293 msgq(sp
, M_SYSERR
, "stderr");
296 if (stat(tty
, &sb
) < 0) {
298 msgq(sp
, M_SYSERR
, "%s", tty
);
302 /* Save the original status if it's unknown. */
303 if (clp
->tgw
== TGW_UNKNOWN
)
304 clp
->tgw
= sb
.st_mode
& S_IWGRP
? TGW_SET
: TGW_UNSET
;
306 /* Toggle the permissions. */
308 if (chmod(tty
, sb
.st_mode
| S_IWGRP
) < 0) {
311 "046|messages not turned on: %s", tty
);
315 if (chmod(tty
, sb
.st_mode
& ~S_IWGRP
) < 0) {
318 "045|messages not turned off: %s", tty
);
326 * Return the terminal size.
328 * PUBLIC: int cl_ssize __P((SCR *, int, size_t *, size_t *, int *));
331 cl_ssize(sp
, sigwinch
, rowp
, colp
, changedp
)
344 /* Assume it's changed. */
345 if (changedp
!= NULL
)
352 * Get the screen rows and columns. If the values are wrong, it's
353 * not a big deal -- as soon as the user sets them explicitly the
354 * environment will be set and the screen package will use the new
361 if (ioctl(STDERR_FILENO
, TIOCGWINSZ
, &win
) != -1) {
366 /* If here because of suspend or a signal, only trust TIOCGWINSZ. */
369 * Somebody didn't get TIOCGWINSZ right, or has suspend
370 * without window resizing support. The user just lost,
371 * but there's nothing we can do.
373 if (row
== 0 || col
== 0) {
374 if (changedp
!= NULL
)
380 * SunOS systems deliver SIGWINCH when windows are uncovered
381 * as well as when they change size. In addition, we call
382 * here when continuing after being suspended since the window
383 * may have changed size. Since we don't want to background
384 * all of the screens just because the window was uncovered,
385 * ignore the signal if there's no change.
388 row
== O_VAL(sp
, O_LINES
) && col
== O_VAL(sp
, O_COLUMNS
)) {
389 if (changedp
!= NULL
)
403 * If TIOCGWINSZ failed, or had entries of 0, try termcap. This
404 * routine is called before any termcap or terminal information
405 * has been set up. If there's no TERM environmental variable set,
406 * let it go, at least ex can run.
408 if (row
== 0 || col
== 0) {
409 if ((p
= getenv("TERM")) == NULL
)
412 if ((rval
= tigetnum("lines")) < 0)
413 msgq(sp
, M_SYSERR
, "tigetnum: lines");
417 if ((rval
= tigetnum("cols")) < 0)
418 msgq(sp
, M_SYSERR
, "tigetnum: cols");
423 /* If nothing else, well, it's probably a VT100. */
424 noterm
: if (row
== 0)
431 * POSIX 1003.2 requires the environment to override everything.
432 * Often, people can get nvi to stop messing up their screen by
433 * deleting the LINES and COLUMNS environment variables from their
436 if ((p
= getenv("LINES")) != NULL
)
437 row
= strtol(p
, NULL
, 10);
438 if ((p
= getenv("COLUMNS")) != NULL
)
439 col
= strtol(p
, NULL
, 10);
450 * Function version of putchar, for tputs.
452 * PUBLIC: int cl_putchar __P((int));
458 return (putchar(ch
));