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[] = "@(#)cl_term.c 10.22 (Berkeley) 9/15/96";
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 {"kend", "$", "go to eol"},
55 {"khome", "^", "go to sol"},
56 {"kich1", "i", "insert at cursor"},
57 {"kdl1", "dd", "delete line"},
58 {"kcub1", "h", "cursor left"},
59 {"knp", "\006", "page down"}, /* ^F */
60 {"kpp", "\002", "page up"}, /* ^B */
61 {"kri", "\025", "scroll up"}, /* ^U */
62 {"ked", "dG", "delete to end of screen"},
63 {"kcuf1", "l", "cursor right"},
64 {"kcuu1", "k", "cursor up"},
67 static TKLIST const m1_tklist[] = { /* Input mappings (lookup). */
70 static TKLIST const m2_tklist[] = { /* Input mappings (set or delete). */
71 {"kcud1", "\033ja", "cursor down"}, /* ^[ja */
72 {"kcub1", "\033ha", "cursor left"}, /* ^[ha */
73 {"kcuu1", "\033ka", "cursor up"}, /* ^[ka */
74 {"kcuf1", "\033la", "cursor right"}, /* ^[la */
80 * Initialize the special keys defined by the termcap/terminfo entry.
82 * PUBLIC: int cl_term_init __P((SCR *));
93 /* Command mappings. */
94 for (tkp = c_tklist; tkp->name != NULL; ++tkp) {
95 if ((t = tigetstr(tkp->ts)) == NULL || t == (char *)-1)
97 if (seq_set(sp, tkp->name, strlen(tkp->name), t, strlen(t),
98 tkp->output, strlen(tkp->output), SEQ_COMMAND,
99 SEQ_NOOVERWRITE | SEQ_SCREEN))
103 /* Input mappings needing to be looked up. */
104 for (tkp = m1_tklist; tkp->name != NULL; ++tkp) {
105 if ((t = tigetstr(tkp->ts)) == NULL || t == (char *)-1)
107 for (kp = keylist;; ++kp)
108 if (kp->value == tkp->value)
112 if (seq_set(sp, tkp->name, strlen(tkp->name), t, strlen(t),
113 &kp->ch, 1, SEQ_INPUT, SEQ_NOOVERWRITE | SEQ_SCREEN))
117 /* Input mappings that are already set or are text deletions. */
118 for (tkp = m2_tklist; tkp->name != NULL; ++tkp) {
119 if ((t = tigetstr(tkp->ts)) == NULL || t == (char *)-1)
123 * Some terminals' <cursor_left> keys send single <backspace>
124 * characters. This is okay in command mapping, but not okay
125 * in input mapping. That combination is the only one we'll
126 * ever see, hopefully, so kluge it here for now.
128 if (!strcmp(t, "\b"))
130 if (tkp->output == NULL) {
131 if (seq_set(sp, tkp->name, strlen(tkp->name),
132 t, strlen(t), NULL, 0,
133 SEQ_INPUT, SEQ_NOOVERWRITE | SEQ_SCREEN))
136 if (seq_set(sp, tkp->name, strlen(tkp->name),
137 t, strlen(t), tkp->output, strlen(tkp->output),
138 SEQ_INPUT, SEQ_NOOVERWRITE | SEQ_SCREEN))
143 * Rework any function key mappings that were set before the
144 * screen was initialized.
146 for (qp = sp->gp->seqq.lh_first; qp != NULL; qp = qp->q.le_next)
147 if (F_ISSET(qp, SEQ_FUNCMAP))
148 (void)cl_pfmap(sp, qp->stype,
149 qp->input, qp->ilen, qp->output, qp->olen);
155 * End the special keys defined by the termcap/terminfo entry.
157 * PUBLIC: int cl_term_end __P((GS *));
165 /* Delete screen specific mappings. */
166 for (qp = gp->seqq.lh_first; qp != NULL; qp = nqp) {
168 if (F_ISSET(qp, SEQ_SCREEN))
176 * Map a function key.
178 * PUBLIC: int cl_fmap __P((SCR *, seq_t, CHAR_T *, size_t, CHAR_T *, size_t));
181 cl_fmap(sp, stype, from, flen, to, tlen)
187 /* Ignore until the screen is running, do the real work then. */
188 if (F_ISSET(sp, SC_VI) && !F_ISSET(sp, SC_SCR_VI))
190 if (F_ISSET(sp, SC_EX) && !F_ISSET(sp, SC_SCR_EX))
193 return (cl_pfmap(sp, stype, from, flen, to, tlen));
198 * Map a function key (private version).
201 cl_pfmap(sp, stype, from, flen, to, tlen)
208 char *p, keyname[64];
210 (void)snprintf(keyname, sizeof(keyname), "kf%d", atoi(from + 1));
211 if ((p = tigetstr(keyname)) == NULL ||
212 p == (char *)-1 || strlen(p) == 0)
215 msgq_str(sp, M_ERR, from, "233|This terminal has no %s key");
219 nlen = snprintf(keyname,
220 sizeof(keyname), "function key %d", atoi(from + 1));
221 return (seq_set(sp, keyname, nlen,
222 p, strlen(p), to, tlen, stype, SEQ_NOOVERWRITE | SEQ_SCREEN));
227 * Curses screen specific "option changed" routine.
229 * PUBLIC: int cl_optchange __P((SCR *, int, char *, u_long *));
232 cl_optchange(sp, opt, str, valp)
247 * Changing the columns, lines or terminal require that
248 * we restart the screen.
250 F_SET(sp->gp, G_SRESTART);
251 F_CLR(sp, SC_SCR_EX | SC_SCR_VI);
254 (void)cl_omesg(sp, clp, !*valp);
258 F_CLR(clp, CL_RENAME_OK);
260 (void)cl_rename(sp, NULL, 0);
262 F_SET(clp, CL_RENAME_OK);
265 * If the screen is live, i.e. we're not reading the
266 * .exrc file, update the window.
268 if (sp->frp != NULL && sp->frp->name != NULL)
269 (void)cl_rename(sp, sp->frp->name, 1);
278 * Turn the tty write permission on or off.
280 * PUBLIC: int cl_omesg __P((SCR *, CL_PRIVATE *, int));
283 cl_omesg(sp, clp, on)
291 /* Find the tty, get the current permissions. */
292 if ((tty = ttyname(STDERR_FILENO)) == NULL) {
294 msgq(sp, M_SYSERR, "stderr");
297 if (stat(tty, &sb) < 0) {
299 msgq(sp, M_SYSERR, "%s", tty);
303 /* Save the original status if it's unknown. */
304 if (clp->tgw == TGW_UNKNOWN)
305 clp->tgw = sb.st_mode & S_IWGRP ? TGW_SET : TGW_UNSET;
307 /* Toggle the permissions. */
309 if (chmod(tty, sb.st_mode | S_IWGRP) < 0) {
312 "046|messages not turned on: %s", tty);
316 if (chmod(tty, sb.st_mode & ~S_IWGRP) < 0) {
319 "045|messages not turned off: %s", tty);
327 * Return the terminal size.
329 * PUBLIC: int cl_ssize __P((SCR *, int, size_t *, size_t *, int *));
332 cl_ssize(sp, sigwinch, rowp, colp, changedp)
345 /* Assume it's changed. */
346 if (changedp != NULL)
353 * Get the screen rows and columns. If the values are wrong, it's
354 * not a big deal -- as soon as the user sets them explicitly the
355 * environment will be set and the screen package will use the new
362 if (ioctl(STDERR_FILENO, TIOCGWINSZ, &win) != -1) {
367 /* If here because of suspend or a signal, only trust TIOCGWINSZ. */
370 * Somebody didn't get TIOCGWINSZ right, or has suspend
371 * without window resizing support. The user just lost,
372 * but there's nothing we can do.
374 if (row == 0 || col == 0) {
375 if (changedp != NULL)
381 * SunOS systems deliver SIGWINCH when windows are uncovered
382 * as well as when they change size. In addition, we call
383 * here when continuing after being suspended since the window
384 * may have changed size. Since we don't want to background
385 * all of the screens just because the window was uncovered,
386 * ignore the signal if there's no change.
389 row == O_VAL(sp, O_LINES) && col == O_VAL(sp, O_COLUMNS)) {
390 if (changedp != NULL)
404 * If TIOCGWINSZ failed, or had entries of 0, try termcap. This
405 * routine is called before any termcap or terminal information
406 * has been set up. If there's no TERM environmental variable set,
407 * let it go, at least ex can run.
409 if (row == 0 || col == 0) {
410 if ((p = getenv("TERM")) == NULL)
413 if ((rval = tigetnum("lines")) < 0)
414 msgq(sp, M_SYSERR, "tigetnum: lines");
418 if ((rval = tigetnum("cols")) < 0)
419 msgq(sp, M_SYSERR, "tigetnum: cols");
424 /* If nothing else, well, it's probably a VT100. */
425 noterm: if (row == 0)
432 * POSIX 1003.2 requires the environment to override everything.
433 * Often, people can get nvi to stop messing up their screen by
434 * deleting the LINES and COLUMNS environment variables from their
437 if ((p = getenv("LINES")) != NULL)
438 row = strtol(p, NULL, 10);
439 if ((p = getenv("COLUMNS")) != NULL)
440 col = strtol(p, NULL, 10);
451 * Function version of putchar, for tputs.
453 * PUBLIC: int cl_putchar __P((int));
459 return (putchar(ch));