1 /* $NetBSD: cl_term.c,v 1.4 2014/01/26 21:43:45 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.
13 #include <sys/cdefs.h>
16 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 ";
19 __RCSID("$NetBSD: cl_term.c,v 1.4 2014/01/26 21:43:45 christos Exp $");
22 #include <sys/types.h>
23 #include <sys/ioctl.h>
24 #include <sys/queue.h>
27 #include <bitstring.h>
37 #include "../common/common.h"
40 static int cl_pfmap
__P((SCR
*, seq_t
, CHAR_T
*, size_t, CHAR_T
*, size_t));
44 * THIS REQUIRES THAT ALL SCREENS SHARE A TERMINAL TYPE.
46 typedef struct _tklist
{
47 const char *ts
; /* Key's termcap string. */
48 const char *output
; /* Corresponding vi command. */
49 const char *name
; /* Name. */
50 u_char value
; /* Special value (for lookup). */
53 #define TKINIT(a, b, c) { a, b, c, 0 }
55 static TKLIST
const c_tklist
[] = { /* Command mappings. */
56 TKINIT("kil1", "O", "insert line"),
57 TKINIT("kdch1", "x", "delete character"),
58 TKINIT("kcud1", "j", "cursor down"),
59 TKINIT("kel", "D", "delete to eol"),
60 TKINIT("kind", "\004", "scroll down"), /* ^D */
61 TKINIT("kll", "$", "go to eol"),
62 TKINIT("kend", "$", "go to eol"),
63 TKINIT("khome", "^", "go to sol"),
64 TKINIT("kich1", "i", "insert at cursor"),
65 TKINIT("kdl1", "dd", "delete line"),
66 TKINIT("kcub1", "h", "cursor left"),
67 TKINIT("knp", "\006", "page down"), /* ^F */
68 TKINIT("kpp", "\002", "page up"), /* ^B */
69 TKINIT("kri", "\025", "scroll up"), /* ^U */
70 TKINIT("ked", "dG", "delete to end of screen"),
71 TKINIT("kcuf1", "l", "cursor right"),
72 TKINIT("kcuu1", "k", "cursor up"),
73 TKINIT(NULL
, NULL
, NULL
),
75 static TKLIST
const m1_tklist
[] = { /* Input mappings (lookup). */
76 TKINIT(NULL
, NULL
, NULL
),
78 static TKLIST
const m2_tklist
[] = { /* Input mappings (set or delete). */
79 TKINIT("kcud1", "\033ja", "cursor down"), /* ^[ja */
80 TKINIT("kcub1", "\033ha", "cursor left"), /* ^[ha */
81 TKINIT("kcuu1", "\033ka", "cursor up"), /* ^[ka */
82 TKINIT("kcuf1", "\033la", "cursor right"), /* ^[la */
83 TKINIT(NULL
, NULL
, NULL
),
88 * Initialize the special keys defined by the termcap/terminfo entry.
90 * PUBLIC: int cl_term_init __P((SCR *));
105 /* Command mappings. */
106 for (tkp
= c_tklist
; tkp
->name
!= NULL
; ++tkp
) {
107 if ((t
= tigetstr(tkp
->ts
)) == NULL
|| t
== (char *)-1)
109 CHAR2INT(sp
, tkp
->name
, strlen(tkp
->name
), wp
, wlen
);
110 MEMCPYW(name
, wp
, wlen
);
111 CHAR2INT(sp
, t
, strlen(t
), wp
, wlen
);
112 MEMCPYW(ts
, wp
, wlen
);
113 CHAR2INT(sp
, tkp
->output
, strlen(tkp
->output
), wp
, wlen
);
114 MEMCPYW(output
, wp
, wlen
);
115 if (seq_set(sp
, name
, strlen(tkp
->name
), ts
, strlen(t
),
116 output
, strlen(tkp
->output
), SEQ_COMMAND
,
117 SEQ_NOOVERWRITE
| SEQ_SCREEN
))
121 /* Input mappings needing to be looked up. */
122 for (tkp
= m1_tklist
; tkp
->name
!= NULL
; ++tkp
) {
123 if ((t
= tigetstr(tkp
->ts
)) == NULL
|| t
== (char *)-1)
125 for (kp
= keylist
;; ++kp
)
126 if (kp
->value
== tkp
->value
)
130 CHAR2INT(sp
, tkp
->name
, strlen(tkp
->name
), wp
, wlen
);
131 MEMCPYW(name
, wp
, wlen
);
132 CHAR2INT(sp
, t
, strlen(t
), wp
, wlen
);
133 MEMCPYW(ts
, wp
, wlen
);
134 output
[0] = (UCHAR_T
)kp
->ch
;
135 if (seq_set(sp
, name
, strlen(tkp
->name
), ts
, strlen(t
),
136 output
, 1, SEQ_INPUT
, SEQ_NOOVERWRITE
| SEQ_SCREEN
))
140 /* Input mappings that are already set or are text deletions. */
141 for (tkp
= m2_tklist
; tkp
->name
!= NULL
; ++tkp
) {
142 if ((t
= tigetstr(tkp
->ts
)) == NULL
|| t
== (char *)-1)
146 * Some terminals' <cursor_left> keys send single <backspace>
147 * characters. This is okay in command mapping, but not okay
148 * in input mapping. That combination is the only one we'll
149 * ever see, hopefully, so kluge it here for now.
151 if (!strcmp(t
, "\b"))
153 if (tkp
->output
== NULL
) {
154 CHAR2INT(sp
, tkp
->name
, strlen(tkp
->name
), wp
, wlen
);
155 MEMCPYW(name
, wp
, wlen
);
156 CHAR2INT(sp
, t
, strlen(t
), wp
, wlen
);
157 MEMCPYW(ts
, wp
, wlen
);
158 if (seq_set(sp
, name
, strlen(tkp
->name
),
159 ts
, strlen(t
), NULL
, 0,
160 SEQ_INPUT
, SEQ_NOOVERWRITE
| SEQ_SCREEN
))
163 CHAR2INT(sp
, tkp
->name
, strlen(tkp
->name
), wp
, wlen
);
164 MEMCPYW(name
, wp
, wlen
);
165 CHAR2INT(sp
, t
, strlen(t
), wp
, wlen
);
166 MEMCPYW(ts
, wp
, wlen
);
167 CHAR2INT(sp
, tkp
->output
, strlen(tkp
->output
), wp
, wlen
);
168 MEMCPYW(output
, wp
, wlen
);
169 if (seq_set(sp
, name
, strlen(tkp
->name
),
170 ts
, strlen(t
), output
, strlen(tkp
->output
),
171 SEQ_INPUT
, SEQ_NOOVERWRITE
| SEQ_SCREEN
))
177 * Rework any function key mappings that were set before the
178 * screen was initialized.
180 LIST_FOREACH(qp
, &sp
->gp
->seqq
, q
)
181 if (F_ISSET(qp
, SEQ_FUNCMAP
))
182 (void)cl_pfmap(sp
, qp
->stype
,
183 qp
->input
, qp
->ilen
, qp
->output
, qp
->olen
);
189 * End the special keys defined by the termcap/terminfo entry.
191 * PUBLIC: int cl_term_end __P((GS *));
198 /* Delete screen specific mappings. */
199 LIST_FOREACH_SAFE(qp
, &gp
->seqq
, q
, nqp
)
200 if (F_ISSET(qp
, SEQ_SCREEN
))
207 * Map a function key.
209 * PUBLIC: int cl_fmap __P((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
)
233 CHAR_T mykeyname
[64];
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(mykeyname
,
249 SIZE(mykeyname
), L("function key %d"),
250 (int)STRTOL(from
+1,NULL
,10));
251 CHAR2INT(sp
, p
, strlen(p
), wp
, wlen
);
252 MEMCPYW(ts
, wp
, wlen
);
253 return (seq_set(sp
, mykeyname
, nlen
,
254 ts
, strlen(p
), to
, tlen
, stype
, SEQ_NOOVERWRITE
| SEQ_SCREEN
));
259 * Curses screen specific "option changed" routine.
261 * PUBLIC: int cl_optchange __P((SCR *, int, const char *, u_long *));
264 cl_optchange(SCR
*sp
, int opt
, const char *str
, u_long
*valp
)
275 * Changing the columns, lines or terminal require that
276 * we restart the screen.
278 F_SET(sp
->gp
, G_SRESTART
);
279 F_CLR(sp
, SC_SCR_EX
| SC_SCR_VI
);
282 (void)cl_omesg(sp
, clp
, *valp
);
286 F_SET(clp
, CL_RENAME_OK
);
289 * If the screen is live, i.e. we're not reading the
290 * .exrc file, update the window.
292 if (sp
->frp
!= NULL
&& sp
->frp
->name
!= NULL
)
293 (void)cl_rename(sp
, sp
->frp
->name
, 1);
295 F_CLR(clp
, CL_RENAME_OK
);
297 (void)cl_rename(sp
, NULL
, 0);
306 * Turn the tty write permission on or off.
308 * PUBLIC: int cl_omesg __P((SCR *, CL_PRIVATE *, int));
311 cl_omesg(SCR
*sp
, CL_PRIVATE
*clp
, int on
)
316 /* Find the tty, get the current permissions. */
317 if ((tty
= ttyname(STDERR_FILENO
)) == NULL
) {
319 msgq(sp
, M_SYSERR
, "stderr");
322 if (stat(tty
, &sb
) < 0) {
324 msgq(sp
, M_SYSERR
, "%s", tty
);
328 /* Save the original status if it's unknown. */
329 if (clp
->tgw
== TGW_UNKNOWN
)
330 clp
->tgw
= sb
.st_mode
& S_IWGRP
? TGW_SET
: TGW_UNSET
;
332 /* Toggle the permissions. */
334 if (chmod(tty
, sb
.st_mode
| S_IWGRP
) < 0) {
337 "046|messages not turned on: %s", tty
);
341 if (chmod(tty
, sb
.st_mode
& ~S_IWGRP
) < 0) {
344 "045|messages not turned off: %s", tty
);
352 * Return the terminal size.
354 * PUBLIC: int cl_ssize __P((SCR *, int, size_t *, size_t *, int *));
357 cl_ssize(SCR
*sp
, int sigwinch
, size_t *rowp
, size_t *colp
, int *changedp
)
366 /* Assume it's changed. */
367 if (changedp
!= NULL
)
374 * Get the screen rows and columns. If the values are wrong, it's
375 * not a big deal -- as soon as the user sets them explicitly the
376 * environment will be set and the screen package will use the new
383 if (ioctl(STDERR_FILENO
, TIOCGWINSZ
, &win
) != -1) {
388 /* If here because of suspend or a signal, only trust TIOCGWINSZ. */
391 * Somebody didn't get TIOCGWINSZ right, or has suspend
392 * without window resizing support. The user just lost,
393 * but there's nothing we can do.
395 if (row
== 0 || col
== 0) {
396 if (changedp
!= NULL
)
402 * SunOS systems deliver SIGWINCH when windows are uncovered
403 * as well as when they change size. In addition, we call
404 * here when continuing after being suspended since the window
405 * may have changed size. Since we don't want to background
406 * all of the screens just because the window was uncovered,
407 * ignore the signal if there's no change.
410 row
== O_VAL(sp
, O_LINES
) && col
== O_VAL(sp
, O_COLUMNS
)) {
411 if (changedp
!= NULL
)
420 resizeterm(row
, col
);
426 * If TIOCGWINSZ failed, or had entries of 0, try termcap. This
427 * routine is called before any termcap or terminal information
428 * has been set up. If there's no TERM environmental variable set,
429 * let it go, at least ex can run.
431 if (row
== 0 || col
== 0) {
432 if ((p
= getenv("TERM")) == NULL
)
435 if ((rval
= tigetnum("lines")) < 0)
436 msgq(sp
, M_SYSERR
, "tigetnum: lines");
441 if ((rval
= tigetnum("cols")) < 0)
442 msgq(sp
, M_SYSERR
, "tigetnum: cols");
448 /* If nothing else, well, it's probably a VT100. */
449 noterm
: if (row
== 0)
456 * POSIX 1003.2 requires the environment to override everything.
457 * Often, people can get nvi to stop messing up their screen by
458 * deleting the LINES and COLUMNS environment variables from their
461 if ((p
= getenv("LINES")) != NULL
)
462 row
= strtol(p
, NULL
, 10);
463 if ((p
= getenv("COLUMNS")) != NULL
)
464 col
= strtol(p
, NULL
, 10);
475 * Function version of putchar, for tputs.
477 * PUBLIC: int cl_putchar __P((int));
482 return (putchar(ch
));