1 /* $NetBSD: cl_term.c,v 1.3 2013/11/25 22:43:46 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.
14 static const char sccsid
[] = "Id: cl_term.c,v 10.31 2001/07/08 13:06:56 skimo Exp (Berkeley) Date: 2001/07/08 13:06:56 ";
17 #include <sys/types.h>
18 #include <sys/ioctl.h>
19 #include <sys/queue.h>
22 #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 const char *ts
; /* Key's termcap string. */
43 const char *output
; /* Corresponding vi command. */
44 const char *name
; /* Name. */
45 u_char value
; /* Special value (for lookup). */
48 #define TKINIT(a, b, c) { a, b, c, 0 }
50 static TKLIST
const c_tklist
[] = { /* Command mappings. */
51 TKINIT("kil1", "O", "insert line"),
52 TKINIT("kdch1", "x", "delete character"),
53 TKINIT("kcud1", "j", "cursor down"),
54 TKINIT("kel", "D", "delete to eol"),
55 TKINIT("kind", "\004", "scroll down"), /* ^D */
56 TKINIT("kll", "$", "go to eol"),
57 TKINIT("kend", "$", "go to eol"),
58 TKINIT("khome", "^", "go to sol"),
59 TKINIT("kich1", "i", "insert at cursor"),
60 TKINIT("kdl1", "dd", "delete line"),
61 TKINIT("kcub1", "h", "cursor left"),
62 TKINIT("knp", "\006", "page down"), /* ^F */
63 TKINIT("kpp", "\002", "page up"), /* ^B */
64 TKINIT("kri", "\025", "scroll up"), /* ^U */
65 TKINIT("ked", "dG", "delete to end of screen"),
66 TKINIT("kcuf1", "l", "cursor right"),
67 TKINIT("kcuu1", "k", "cursor up"),
68 TKINIT(NULL
, NULL
, NULL
),
70 static TKLIST
const m1_tklist
[] = { /* Input mappings (lookup). */
71 TKINIT(NULL
, NULL
, NULL
),
73 static TKLIST
const m2_tklist
[] = { /* Input mappings (set or delete). */
74 TKINIT("kcud1", "\033ja", "cursor down"), /* ^[ja */
75 TKINIT("kcub1", "\033ha", "cursor left"), /* ^[ha */
76 TKINIT("kcuu1", "\033ka", "cursor up"), /* ^[ka */
77 TKINIT("kcuf1", "\033la", "cursor right"), /* ^[la */
78 TKINIT(NULL
, NULL
, NULL
),
83 * Initialize the special keys defined by the termcap/terminfo entry.
85 * PUBLIC: int cl_term_init __P((SCR *));
100 /* Command mappings. */
101 for (tkp
= c_tklist
; tkp
->name
!= NULL
; ++tkp
) {
102 if ((t
= tigetstr(tkp
->ts
)) == NULL
|| t
== (char *)-1)
104 CHAR2INT(sp
, tkp
->name
, strlen(tkp
->name
), wp
, wlen
);
105 MEMCPYW(name
, wp
, wlen
);
106 CHAR2INT(sp
, t
, strlen(t
), wp
, wlen
);
107 MEMCPYW(ts
, wp
, wlen
);
108 CHAR2INT(sp
, tkp
->output
, strlen(tkp
->output
), wp
, wlen
);
109 MEMCPYW(output
, wp
, wlen
);
110 if (seq_set(sp
, name
, strlen(tkp
->name
), ts
, strlen(t
),
111 output
, strlen(tkp
->output
), SEQ_COMMAND
,
112 SEQ_NOOVERWRITE
| SEQ_SCREEN
))
116 /* Input mappings needing to be looked up. */
117 for (tkp
= m1_tklist
; tkp
->name
!= NULL
; ++tkp
) {
118 if ((t
= tigetstr(tkp
->ts
)) == NULL
|| t
== (char *)-1)
120 for (kp
= keylist
;; ++kp
)
121 if (kp
->value
== tkp
->value
)
125 CHAR2INT(sp
, tkp
->name
, strlen(tkp
->name
), wp
, wlen
);
126 MEMCPYW(name
, wp
, wlen
);
127 CHAR2INT(sp
, t
, strlen(t
), wp
, wlen
);
128 MEMCPYW(ts
, wp
, wlen
);
129 output
[0] = (UCHAR_T
)kp
->ch
;
130 if (seq_set(sp
, name
, strlen(tkp
->name
), ts
, strlen(t
),
131 output
, 1, SEQ_INPUT
, SEQ_NOOVERWRITE
| SEQ_SCREEN
))
135 /* Input mappings that are already set or are text deletions. */
136 for (tkp
= m2_tklist
; tkp
->name
!= NULL
; ++tkp
) {
137 if ((t
= tigetstr(tkp
->ts
)) == NULL
|| t
== (char *)-1)
141 * Some terminals' <cursor_left> keys send single <backspace>
142 * characters. This is okay in command mapping, but not okay
143 * in input mapping. That combination is the only one we'll
144 * ever see, hopefully, so kluge it here for now.
146 if (!strcmp(t
, "\b"))
148 if (tkp
->output
== NULL
) {
149 CHAR2INT(sp
, tkp
->name
, strlen(tkp
->name
), wp
, wlen
);
150 MEMCPYW(name
, wp
, wlen
);
151 CHAR2INT(sp
, t
, strlen(t
), wp
, wlen
);
152 MEMCPYW(ts
, wp
, wlen
);
153 if (seq_set(sp
, name
, strlen(tkp
->name
),
154 ts
, strlen(t
), NULL
, 0,
155 SEQ_INPUT
, SEQ_NOOVERWRITE
| SEQ_SCREEN
))
158 CHAR2INT(sp
, tkp
->name
, strlen(tkp
->name
), wp
, wlen
);
159 MEMCPYW(name
, wp
, wlen
);
160 CHAR2INT(sp
, t
, strlen(t
), wp
, wlen
);
161 MEMCPYW(ts
, wp
, wlen
);
162 CHAR2INT(sp
, tkp
->output
, strlen(tkp
->output
), wp
, wlen
);
163 MEMCPYW(output
, wp
, wlen
);
164 if (seq_set(sp
, name
, strlen(tkp
->name
),
165 ts
, strlen(t
), output
, strlen(tkp
->output
),
166 SEQ_INPUT
, SEQ_NOOVERWRITE
| SEQ_SCREEN
))
172 * Rework any function key mappings that were set before the
173 * screen was initialized.
175 LIST_FOREACH(qp
, &sp
->gp
->seqq
, q
)
176 if (F_ISSET(qp
, SEQ_FUNCMAP
))
177 (void)cl_pfmap(sp
, qp
->stype
,
178 qp
->input
, qp
->ilen
, qp
->output
, qp
->olen
);
184 * End the special keys defined by the termcap/terminfo entry.
186 * PUBLIC: int cl_term_end __P((GS *));
193 /* Delete screen specific mappings. */
194 LIST_FOREACH_SAFE(qp
, &gp
->seqq
, q
, nqp
)
195 if (F_ISSET(qp
, SEQ_SCREEN
))
202 * Map a function key.
204 * PUBLIC: int cl_fmap __P((SCR *, seq_t, CHAR_T *, size_t, CHAR_T *, size_t));
207 cl_fmap(SCR
*sp
, seq_t stype
, CHAR_T
*from
, size_t flen
, CHAR_T
*to
, size_t tlen
)
209 /* Ignore until the screen is running, do the real work then. */
210 if (F_ISSET(sp
, SC_VI
) && !F_ISSET(sp
, SC_SCR_VI
))
212 if (F_ISSET(sp
, SC_EX
) && !F_ISSET(sp
, SC_SCR_EX
))
215 return (cl_pfmap(sp
, stype
, from
, flen
, to
, tlen
));
220 * Map a function key (private version).
223 cl_pfmap(SCR
*sp
, seq_t stype
, CHAR_T
*from
, size_t flen
, CHAR_T
*to
, size_t tlen
)
228 CHAR_T mykeyname
[64];
233 (void)snprintf(name
, sizeof(name
), "kf%d",
234 (int)STRTOL(from
+1,NULL
,10));
235 if ((p
= tigetstr(name
)) == NULL
||
236 p
== (char *)-1 || strlen(p
) == 0)
239 msgq_wstr(sp
, M_ERR
, from
, "233|This terminal has no %s key");
243 nlen
= SPRINTF(mykeyname
,
244 SIZE(mykeyname
), L("function key %d"),
245 (int)STRTOL(from
+1,NULL
,10));
246 CHAR2INT(sp
, p
, strlen(p
), wp
, wlen
);
247 MEMCPYW(ts
, wp
, wlen
);
248 return (seq_set(sp
, mykeyname
, nlen
,
249 ts
, strlen(p
), to
, tlen
, stype
, SEQ_NOOVERWRITE
| SEQ_SCREEN
));
254 * Curses screen specific "option changed" routine.
256 * PUBLIC: int cl_optchange __P((SCR *, int, const char *, u_long *));
259 cl_optchange(SCR
*sp
, int opt
, const char *str
, u_long
*valp
)
270 * Changing the columns, lines or terminal require that
271 * we restart the screen.
273 F_SET(sp
->gp
, G_SRESTART
);
274 F_CLR(sp
, SC_SCR_EX
| SC_SCR_VI
);
277 (void)cl_omesg(sp
, clp
, *valp
);
281 F_SET(clp
, CL_RENAME_OK
);
284 * If the screen is live, i.e. we're not reading the
285 * .exrc file, update the window.
287 if (sp
->frp
!= NULL
&& sp
->frp
->name
!= NULL
)
288 (void)cl_rename(sp
, sp
->frp
->name
, 1);
290 F_CLR(clp
, CL_RENAME_OK
);
292 (void)cl_rename(sp
, NULL
, 0);
301 * Turn the tty write permission on or off.
303 * PUBLIC: int cl_omesg __P((SCR *, CL_PRIVATE *, int));
306 cl_omesg(SCR
*sp
, CL_PRIVATE
*clp
, int on
)
311 /* Find the tty, get the current permissions. */
312 if ((tty
= ttyname(STDERR_FILENO
)) == NULL
) {
314 msgq(sp
, M_SYSERR
, "stderr");
317 if (stat(tty
, &sb
) < 0) {
319 msgq(sp
, M_SYSERR
, "%s", tty
);
323 /* Save the original status if it's unknown. */
324 if (clp
->tgw
== TGW_UNKNOWN
)
325 clp
->tgw
= sb
.st_mode
& S_IWGRP
? TGW_SET
: TGW_UNSET
;
327 /* Toggle the permissions. */
329 if (chmod(tty
, sb
.st_mode
| S_IWGRP
) < 0) {
332 "046|messages not turned on: %s", tty
);
336 if (chmod(tty
, sb
.st_mode
& ~S_IWGRP
) < 0) {
339 "045|messages not turned off: %s", tty
);
347 * Return the terminal size.
349 * PUBLIC: int cl_ssize __P((SCR *, int, size_t *, size_t *, int *));
352 cl_ssize(SCR
*sp
, int sigwinch
, size_t *rowp
, size_t *colp
, int *changedp
)
361 /* Assume it's changed. */
362 if (changedp
!= NULL
)
369 * Get the screen rows and columns. If the values are wrong, it's
370 * not a big deal -- as soon as the user sets them explicitly the
371 * environment will be set and the screen package will use the new
378 if (ioctl(STDERR_FILENO
, TIOCGWINSZ
, &win
) != -1) {
383 /* If here because of suspend or a signal, only trust TIOCGWINSZ. */
386 * Somebody didn't get TIOCGWINSZ right, or has suspend
387 * without window resizing support. The user just lost,
388 * but there's nothing we can do.
390 if (row
== 0 || col
== 0) {
391 if (changedp
!= NULL
)
397 * SunOS systems deliver SIGWINCH when windows are uncovered
398 * as well as when they change size. In addition, we call
399 * here when continuing after being suspended since the window
400 * may have changed size. Since we don't want to background
401 * all of the screens just because the window was uncovered,
402 * ignore the signal if there's no change.
405 row
== O_VAL(sp
, O_LINES
) && col
== O_VAL(sp
, O_COLUMNS
)) {
406 if (changedp
!= NULL
)
415 resizeterm(row
, col
);
421 * If TIOCGWINSZ failed, or had entries of 0, try termcap. This
422 * routine is called before any termcap or terminal information
423 * has been set up. If there's no TERM environmental variable set,
424 * let it go, at least ex can run.
426 if (row
== 0 || col
== 0) {
427 if ((p
= getenv("TERM")) == NULL
)
430 if ((rval
= tigetnum("lines")) < 0)
431 msgq(sp
, M_SYSERR
, "tigetnum: lines");
436 if ((rval
= tigetnum("cols")) < 0)
437 msgq(sp
, M_SYSERR
, "tigetnum: cols");
443 /* If nothing else, well, it's probably a VT100. */
444 noterm
: if (row
== 0)
451 * POSIX 1003.2 requires the environment to override everything.
452 * Often, people can get nvi to stop messing up their screen by
453 * deleting the LINES and COLUMNS environment variables from their
456 if ((p
= getenv("LINES")) != NULL
)
457 row
= strtol(p
, NULL
, 10);
458 if ((p
= getenv("COLUMNS")) != NULL
)
459 col
= strtol(p
, NULL
, 10);
470 * Function version of putchar, for tputs.
472 * PUBLIC: int cl_putchar __P((int));
477 return (putchar(ch
));