1 /* $NetBSD: cl_term.c,v 1.1.1.2 2008/05/18 14:29:38 aymeric Exp $ */
4 * Copyright (c) 1993, 1994
5 * The Regents of the University of California. All rights reserved.
6 * Copyright (c) 1993, 1994, 1995, 1996
7 * Keith Bostic. All rights reserved.
9 * See the LICENSE file for redistribution information.
15 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";
18 #include <sys/types.h>
19 #include <sys/ioctl.h>
20 #include <sys/queue.h>
23 #include <bitstring.h>
33 #include "../common/common.h"
36 static int cl_pfmap
__P((SCR
*, seq_t
, CHAR_T
*, size_t, CHAR_T
*, size_t));
40 * THIS REQUIRES THAT ALL SCREENS SHARE A TERMINAL TYPE.
42 typedef struct _tklist
{
43 const char *ts
; /* Key's termcap string. */
44 const char *output
; /* Corresponding vi command. */
45 const char *name
; /* Name. */
46 u_char value
; /* Special value (for lookup). */
49 #define TKINIT(a, b, c) { a, b, c, 0 }
51 static TKLIST
const c_tklist
[] = { /* Command mappings. */
52 TKINIT("kil1", "O", "insert line"),
53 TKINIT("kdch1", "x", "delete character"),
54 TKINIT("kcud1", "j", "cursor down"),
55 TKINIT("kel", "D", "delete to eol"),
56 TKINIT("kind", "\004", "scroll down"), /* ^D */
57 TKINIT("kll", "$", "go to eol"),
58 TKINIT("kend", "$", "go to eol"),
59 TKINIT("khome", "^", "go to sol"),
60 TKINIT("kich1", "i", "insert at cursor"),
61 TKINIT("kdl1", "dd", "delete line"),
62 TKINIT("kcub1", "h", "cursor left"),
63 TKINIT("knp", "\006", "page down"), /* ^F */
64 TKINIT("kpp", "\002", "page up"), /* ^B */
65 TKINIT("kri", "\025", "scroll up"), /* ^U */
66 TKINIT("ked", "dG", "delete to end of screen"),
67 TKINIT("kcuf1", "l", "cursor right"),
68 TKINIT("kcuu1", "k", "cursor up"),
69 TKINIT(NULL
, NULL
, NULL
),
71 static TKLIST
const m1_tklist
[] = { /* Input mappings (lookup). */
72 TKINIT(NULL
, NULL
, NULL
),
74 static TKLIST
const m2_tklist
[] = { /* Input mappings (set or delete). */
75 TKINIT("kcud1", "\033ja", "cursor down"), /* ^[ja */
76 TKINIT("kcub1", "\033ha", "cursor left"), /* ^[ha */
77 TKINIT("kcuu1", "\033ka", "cursor up"), /* ^[ka */
78 TKINIT("kcuf1", "\033la", "cursor right"), /* ^[la */
79 TKINIT(NULL
, NULL
, NULL
),
84 * Initialize the special keys defined by the termcap/terminfo entry.
86 * PUBLIC: int cl_term_init __P((SCR *));
101 /* Command mappings. */
102 for (tkp
= c_tklist
; tkp
->name
!= NULL
; ++tkp
) {
103 if ((t
= tigetstr(tkp
->ts
)) == NULL
|| t
== (char *)-1)
105 CHAR2INT(sp
, tkp
->name
, strlen(tkp
->name
), wp
, wlen
);
106 MEMCPYW(name
, wp
, wlen
);
107 CHAR2INT(sp
, t
, strlen(t
), wp
, wlen
);
108 MEMCPYW(ts
, wp
, wlen
);
109 CHAR2INT(sp
, tkp
->output
, strlen(tkp
->output
), wp
, wlen
);
110 MEMCPYW(output
, wp
, wlen
);
111 if (seq_set(sp
, name
, strlen(tkp
->name
), ts
, strlen(t
),
112 output
, strlen(tkp
->output
), SEQ_COMMAND
,
113 SEQ_NOOVERWRITE
| SEQ_SCREEN
))
117 /* Input mappings needing to be looked up. */
118 for (tkp
= m1_tklist
; tkp
->name
!= NULL
; ++tkp
) {
119 if ((t
= tigetstr(tkp
->ts
)) == NULL
|| t
== (char *)-1)
121 for (kp
= keylist
;; ++kp
)
122 if (kp
->value
== tkp
->value
)
126 CHAR2INT(sp
, tkp
->name
, strlen(tkp
->name
), wp
, wlen
);
127 MEMCPYW(name
, wp
, wlen
);
128 CHAR2INT(sp
, t
, strlen(t
), wp
, wlen
);
129 MEMCPYW(ts
, wp
, wlen
);
130 if (seq_set(sp
, name
, strlen(tkp
->name
), ts
, strlen(t
),
131 &kp
->ch
, 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 for (qp
= sp
->gp
->seqq
.lh_first
; qp
!= NULL
; qp
= qp
->q
.le_next
)
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 for (qp
= gp
->seqq
.lh_first
; qp
!= NULL
; qp
= nqp
) {
196 if (F_ISSET(qp
, SEQ_SCREEN
))
204 * Map a function key.
206 * PUBLIC: int cl_fmap __P((SCR *, seq_t, CHAR_T *, size_t, CHAR_T *, size_t));
209 cl_fmap(SCR
*sp
, seq_t stype
, CHAR_T
*from
, size_t flen
, CHAR_T
*to
, size_t tlen
)
211 /* Ignore until the screen is running, do the real work then. */
212 if (F_ISSET(sp
, SC_VI
) && !F_ISSET(sp
, SC_SCR_VI
))
214 if (F_ISSET(sp
, SC_EX
) && !F_ISSET(sp
, SC_SCR_EX
))
217 return (cl_pfmap(sp
, stype
, from
, flen
, to
, tlen
));
222 * Map a function key (private version).
225 cl_pfmap(SCR
*sp
, seq_t stype
, CHAR_T
*from
, size_t flen
, CHAR_T
*to
, size_t tlen
)
230 CHAR_T mykeyname
[64];
235 (void)snprintf(name
, sizeof(name
), "kf%d",
236 (int)STRTOL(from
+1,NULL
,10));
237 if ((p
= tigetstr(name
)) == NULL
||
238 p
== (char *)-1 || strlen(p
) == 0)
241 msgq_wstr(sp
, M_ERR
, from
, "233|This terminal has no %s key");
245 nlen
= SPRINTF(mykeyname
,
246 SIZE(mykeyname
), L("function key %d"),
247 (int)STRTOL(from
+1,NULL
,10));
248 CHAR2INT(sp
, p
, strlen(p
), wp
, wlen
);
249 MEMCPYW(ts
, wp
, wlen
);
250 return (seq_set(sp
, mykeyname
, nlen
,
251 ts
, strlen(p
), to
, tlen
, stype
, SEQ_NOOVERWRITE
| SEQ_SCREEN
));
256 * Curses screen specific "option changed" routine.
258 * PUBLIC: int cl_optchange __P((SCR *, int, const char *, u_long *));
261 cl_optchange(SCR
*sp
, int opt
, const char *str
, u_long
*valp
)
272 * Changing the columns, lines or terminal require that
273 * we restart the screen.
275 F_SET(sp
->gp
, G_SRESTART
);
276 F_CLR(sp
, SC_SCR_EX
| SC_SCR_VI
);
279 (void)cl_omesg(sp
, clp
, *valp
);
283 F_SET(clp
, CL_RENAME_OK
);
286 * If the screen is live, i.e. we're not reading the
287 * .exrc file, update the window.
289 if (sp
->frp
!= NULL
&& sp
->frp
->name
!= NULL
)
290 (void)cl_rename(sp
, sp
->frp
->name
, 1);
292 F_CLR(clp
, CL_RENAME_OK
);
294 (void)cl_rename(sp
, NULL
, 0);
303 * Turn the tty write permission on or off.
305 * PUBLIC: int cl_omesg __P((SCR *, CL_PRIVATE *, int));
308 cl_omesg(SCR
*sp
, CL_PRIVATE
*clp
, int on
)
313 /* Find the tty, get the current permissions. */
314 if ((tty
= ttyname(STDERR_FILENO
)) == NULL
) {
316 msgq(sp
, M_SYSERR
, "stderr");
319 if (stat(tty
, &sb
) < 0) {
321 msgq(sp
, M_SYSERR
, "%s", tty
);
325 /* Save the original status if it's unknown. */
326 if (clp
->tgw
== TGW_UNKNOWN
)
327 clp
->tgw
= sb
.st_mode
& S_IWGRP
? TGW_SET
: TGW_UNSET
;
329 /* Toggle the permissions. */
331 if (chmod(tty
, sb
.st_mode
| S_IWGRP
) < 0) {
334 "046|messages not turned on: %s", tty
);
338 if (chmod(tty
, sb
.st_mode
& ~S_IWGRP
) < 0) {
341 "045|messages not turned off: %s", tty
);
349 * Return the terminal size.
351 * PUBLIC: int cl_ssize __P((SCR *, int, size_t *, size_t *, int *));
354 cl_ssize(SCR
*sp
, int sigwinch
, size_t *rowp
, size_t *colp
, int *changedp
)
363 /* Assume it's changed. */
364 if (changedp
!= NULL
)
371 * Get the screen rows and columns. If the values are wrong, it's
372 * not a big deal -- as soon as the user sets them explicitly the
373 * environment will be set and the screen package will use the new
380 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");
437 if ((rval
= tigetnum("cols")) < 0)
438 msgq(sp
, M_SYSERR
, "tigetnum: cols");
444 /* If nothing else, well, it's probably a VT100. */
445 noterm
: if (row
== 0)
452 * POSIX 1003.2 requires the environment to override everything.
453 * Often, people can get nvi to stop messing up their screen by
454 * deleting the LINES and COLUMNS environment variables from their
457 if ((p
= getenv("LINES")) != NULL
)
458 row
= strtol(p
, NULL
, 10);
459 if ((p
= getenv("COLUMNS")) != NULL
)
460 col
= strtol(p
, NULL
, 10);
471 * Function version of putchar, for tputs.
473 * PUBLIC: int cl_putchar __P((int));
478 return (putchar(ch
));