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.35 2015/04/08 02:12:11 zy Exp $";
16 #include <sys/types.h>
17 #include <sys/ioctl.h>
18 #include <sys/queue.h>
21 #include <bitstring.h>
34 #include "../common/common.h"
37 static int cl_pfmap(SCR
*, seq_t
, CHAR_T
*, size_t, CHAR_T
*, size_t);
41 * THIS REQUIRES THAT ALL SCREENS SHARE A TERMINAL TYPE.
43 typedef struct _tklist
{
44 char *ts
; /* Key's termcap string. */
45 char *output
; /* Corresponding vi command. */
46 char *name
; /* Name. */
47 u_char value
; /* Special value (for lookup). */
49 static TKLIST
const c_tklist
[] = { /* Command mappings. */
50 {"kil1", "O", "insert line"},
51 {"kdch1", "x", "delete character"},
52 {"kcud1", "j", "cursor down"},
53 {"kel", "D", "delete to eol"},
54 {"kind", "\004", "scroll down"}, /* ^D */
55 {"kll", "$", "go to eol"},
56 {"kend", "$", "go to eol"},
57 {"khome", "^", "go to sol"},
58 {"kich1", "i", "insert at cursor"},
59 {"kdl1", "dd", "delete line"},
60 {"kcub1", "h", "cursor left"},
61 {"knp", "\006", "page down"}, /* ^F */
62 {"kpp", "\002", "page up"}, /* ^B */
63 {"kri", "\025", "scroll up"}, /* ^U */
64 {"ked", "dG", "delete to end of screen"},
65 {"kcuf1", "l", "cursor right"},
66 {"kcuu1", "k", "cursor up"},
69 static TKLIST
const m1_tklist
[] = { /* Input mappings (lookup). */
72 static TKLIST
const m2_tklist
[] = { /* Input mappings (set or delete). */
73 {"kcud1", "\033ja", "cursor down"}, /* ^[ja */
74 {"kcub1", "\033ha", "cursor left"}, /* ^[ha */
75 {"kcuu1", "\033ka", "cursor up"}, /* ^[ka */
76 {"kcuf1", "\033la", "cursor right"}, /* ^[la */
82 * Initialize the special keys defined by the termcap/terminfo entry.
84 * PUBLIC: int cl_term_init(SCR *);
99 /* Command mappings. */
100 for (tkp
= c_tklist
; tkp
->name
!= NULL
; ++tkp
) {
101 if ((t
= tigetstr(tkp
->ts
)) == NULL
|| t
== (char *)-1)
103 CHAR2INT(sp
, tkp
->name
, strlen(tkp
->name
), wp
, wlen
);
104 MEMCPY(name
, wp
, wlen
);
105 CHAR2INT(sp
, t
, strlen(t
), wp
, wlen
);
106 MEMCPY(ts
, wp
, wlen
);
107 CHAR2INT(sp
, tkp
->output
, strlen(tkp
->output
), wp
, wlen
);
108 MEMCPY(output
, wp
, wlen
);
109 if (seq_set(sp
, name
, strlen(tkp
->name
), ts
, strlen(t
),
110 output
, strlen(tkp
->output
), SEQ_COMMAND
,
111 SEQ_NOOVERWRITE
| SEQ_SCREEN
))
115 /* Input mappings needing to be looked up. */
116 for (tkp
= m1_tklist
; tkp
->name
!= NULL
; ++tkp
) {
117 if ((t
= tigetstr(tkp
->ts
)) == NULL
|| t
== (char *)-1)
119 for (kp
= keylist
;; ++kp
)
120 if (kp
->value
== tkp
->value
)
124 CHAR2INT(sp
, tkp
->name
, strlen(tkp
->name
), wp
, wlen
);
125 MEMCPY(name
, wp
, wlen
);
126 CHAR2INT(sp
, t
, strlen(t
), wp
, wlen
);
127 MEMCPY(ts
, wp
, wlen
);
128 output
[0] = (UCHAR_T
)kp
->ch
;
129 if (seq_set(sp
, name
, strlen(tkp
->name
), ts
, strlen(t
),
130 output
, 1, SEQ_INPUT
, SEQ_NOOVERWRITE
| SEQ_SCREEN
))
134 /* Input mappings that are already set or are text deletions. */
135 for (tkp
= m2_tklist
; tkp
->name
!= NULL
; ++tkp
) {
136 if ((t
= tigetstr(tkp
->ts
)) == NULL
|| t
== (char *)-1)
140 * Some terminals' <cursor_left> keys send single <backspace>
141 * characters. This is okay in command mapping, but not okay
142 * in input mapping. That combination is the only one we'll
143 * ever see, hopefully, so kluge it here for now.
145 if (!strcmp(t
, "\b"))
147 if (tkp
->output
== NULL
) {
148 CHAR2INT(sp
, tkp
->name
, strlen(tkp
->name
), wp
, wlen
);
149 MEMCPY(name
, wp
, wlen
);
150 CHAR2INT(sp
, t
, strlen(t
), wp
, wlen
);
151 MEMCPY(ts
, wp
, wlen
);
152 if (seq_set(sp
, name
, strlen(tkp
->name
),
153 ts
, strlen(t
), NULL
, 0,
154 SEQ_INPUT
, SEQ_NOOVERWRITE
| SEQ_SCREEN
))
157 CHAR2INT(sp
, tkp
->name
, strlen(tkp
->name
), wp
, wlen
);
158 MEMCPY(name
, wp
, wlen
);
159 CHAR2INT(sp
, t
, strlen(t
), wp
, wlen
);
160 MEMCPY(ts
, wp
, wlen
);
161 CHAR2INT(sp
, tkp
->output
, strlen(tkp
->output
), wp
, wlen
);
162 MEMCPY(output
, wp
, wlen
);
163 if (seq_set(sp
, name
, strlen(tkp
->name
),
164 ts
, strlen(t
), output
, strlen(tkp
->output
),
165 SEQ_INPUT
, SEQ_NOOVERWRITE
| SEQ_SCREEN
))
171 * Rework any function key mappings that were set before the
172 * screen was initialized.
174 SLIST_FOREACH(qp
, sp
->gp
->seqq
, q
)
175 if (F_ISSET(qp
, SEQ_FUNCMAP
))
176 (void)cl_pfmap(sp
, qp
->stype
,
177 qp
->input
, qp
->ilen
, qp
->output
, qp
->olen
);
183 * End the special keys defined by the termcap/terminfo entry.
185 * PUBLIC: int cl_term_end(GS *);
190 SEQ
*qp
, *nqp
, *pre_qp
= NULL
;
192 /* Delete screen specific mappings. */
193 SLIST_FOREACH_MUTABLE(qp
, gp
->seqq
, q
, nqp
)
194 if (F_ISSET(qp
, SEQ_SCREEN
)) {
195 if (qp
== SLIST_FIRST(gp
->seqq
))
196 SLIST_REMOVE_HEAD(gp
->seqq
, q
);
198 SLIST_REMOVE_AFTER(pre_qp
, q
);
207 * Map a function key.
209 * PUBLIC: int cl_fmap(SCR *, seq_t, CHAR_T *, size_t, CHAR_T *, size_t);
212 cl_fmap(SCR
*sp
, seq_t stype
, CHAR_T
*from
, size_t flen
, CHAR_T
*to
, size_t tlen
)
214 /* Ignore until the screen is running, do the real work then. */
215 if (F_ISSET(sp
, SC_VI
) && !F_ISSET(sp
, SC_SCR_VI
))
217 if (F_ISSET(sp
, SC_EX
) && !F_ISSET(sp
, SC_SCR_EX
))
220 return (cl_pfmap(sp
, stype
, from
, flen
, to
, tlen
));
225 * Map a function key (private version).
228 cl_pfmap(SCR
*sp
, seq_t stype
, CHAR_T
*from
, size_t flen
, CHAR_T
*to
, size_t tlen
)
238 (void)snprintf(name
, sizeof(name
), "kf%d",
239 (int)STRTOL(from
+1,NULL
,10));
240 if ((p
= tigetstr(name
)) == NULL
||
241 p
== (char *)-1 || strlen(p
) == 0)
244 msgq_wstr(sp
, M_ERR
, from
, "233|This terminal has no %s key");
248 nlen
= SPRINTF(keyname
,
249 SIZE(keyname
), L("function key %d"),
250 (int)STRTOL(from
+1,NULL
,10));
251 CHAR2INT(sp
, p
, strlen(p
), wp
, wlen
);
252 MEMCPY(ts
, wp
, wlen
);
253 return (seq_set(sp
, keyname
, nlen
,
254 ts
, strlen(p
), to
, tlen
, stype
, SEQ_NOOVERWRITE
| SEQ_SCREEN
));
259 * Curses screen specific "option changed" routine.
261 * PUBLIC: int cl_optchange(SCR *, int, char *, u_long *);
264 cl_optchange(SCR
*sp
, int opt
, char *str
, u_long
*valp
)
272 F_CLR(sp
, SC_SCR_EX
| SC_SCR_VI
);
277 * Changing the terminal type requires that we reinitialize
278 * curses, while resizing does not.
280 F_SET(sp
->gp
, G_SRESTART
);
283 (void)cl_omesg(sp
, clp
, *valp
);
287 F_SET(clp
, CL_RENAME_OK
);
290 * If the screen is live, i.e. we're not reading the
291 * .exrc file, update the window.
293 if (sp
->frp
!= NULL
&& sp
->frp
->name
!= NULL
)
294 (void)cl_rename(sp
, sp
->frp
->name
, 1);
296 F_CLR(clp
, CL_RENAME_OK
);
298 (void)cl_rename(sp
, NULL
, 0);
307 * Turn the tty write permission on or off.
309 * PUBLIC: int cl_omesg(SCR *, CL_PRIVATE *, int);
312 cl_omesg(SCR
*sp
, CL_PRIVATE
*clp
, int on
)
317 /* Find the tty, get the current permissions. */
318 if ((tty
= ttyname(STDERR_FILENO
)) == NULL
) {
320 msgq(sp
, M_SYSERR
, "stderr");
323 if (stat(tty
, &sb
) < 0) {
325 msgq(sp
, M_SYSERR
, "%s", tty
);
329 /* Save the original status if it's unknown. */
330 if (clp
->tgw
== TGW_UNKNOWN
)
331 clp
->tgw
= sb
.st_mode
& S_IWGRP
? TGW_SET
: TGW_UNSET
;
333 /* Toggle the permissions. */
335 if (chmod(tty
, sb
.st_mode
| S_IWGRP
) < 0) {
338 "046|messages not turned on: %s", tty
);
342 if (chmod(tty
, sb
.st_mode
& ~S_IWGRP
) < 0) {
345 "045|messages not turned off: %s", tty
);
353 * Return the terminal size.
355 * PUBLIC: int cl_ssize(SCR *, int, size_t *, size_t *, int *);
358 cl_ssize(SCR
*sp
, int sigwinch
, size_t *rowp
, size_t *colp
, int *changedp
)
365 /* Assume it's changed. */
366 if (changedp
!= NULL
)
373 * Get the screen rows and columns. If the values are wrong, it's
374 * not a big deal -- as soon as the user sets them explicitly the
375 * environment will be set and the screen package will use the new
381 if (ioctl(STDERR_FILENO
, TIOCGWINSZ
, &win
) != -1) {
385 /* If here because of suspend or a signal, only trust TIOCGWINSZ. */
388 * Somebody didn't get TIOCGWINSZ right, or has suspend
389 * without window resizing support. The user just lost,
390 * but there's nothing we can do.
392 if (row
== 0 || col
== 0) {
393 if (changedp
!= NULL
)
399 * SunOS systems deliver SIGWINCH when windows are uncovered
400 * as well as when they change size. In addition, we call
401 * here when continuing after being suspended since the window
402 * may have changed size. Since we don't want to background
403 * all of the screens just because the window was uncovered,
404 * ignore the signal if there's no change.
407 row
== O_VAL(sp
, O_LINES
) && col
== O_VAL(sp
, O_COLUMNS
)) {
408 if (changedp
!= NULL
)
422 * If TIOCGWINSZ failed, or had entries of 0, try termcap. This
423 * routine is called before any termcap or terminal information
424 * has been set up. If there's no TERM environmental variable set,
425 * let it go, at least ex can run.
427 if (row
== 0 || col
== 0) {
428 if ((p
= getenv("TERM")) == NULL
)
431 if ((rval
= tigetnum("lines")) < 0)
432 msgq(sp
, M_SYSERR
, "tigetnum: lines");
436 if ((rval
= tigetnum("cols")) < 0)
437 msgq(sp
, M_SYSERR
, "tigetnum: cols");
442 /* If nothing else, well, it's probably a VT100. */
443 noterm
: if (row
== 0)
450 * POSIX 1003.2 requires the environment to override everything.
451 * Often, people can get nvi to stop messing up their screen by
452 * deleting the LINES and COLUMNS environment variables from their
455 if ((p
= getenv("LINES")) != NULL
)
456 row
= strtol(p
, NULL
, 10);
457 if ((p
= getenv("COLUMNS")) != NULL
)
458 col
= strtol(p
, NULL
, 10);
469 * Function version of putchar, for tputs.
471 * PUBLIC: int cl_putchar(int);
476 return (putchar(ch
));