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.
12 #include <sys/types.h>
13 #include <sys/ioctl.h>
14 #include <sys/queue.h>
17 #include <bitstring.h>
30 #include "../common/common.h"
33 static int cl_pfmap(SCR *, seq_t, CHAR_T *, size_t, CHAR_T *, size_t);
34 static size_t atoz_or(const char *, size_t);
38 * THIS REQUIRES THAT ALL SCREENS SHARE A TERMINAL TYPE.
40 typedef struct _tklist {
41 char *ts; /* Key's termcap string. */
42 char *output; /* Corresponding vi command. */
43 char *name; /* Name. */
44 u_char value; /* Special value (for lookup). */
46 static TKLIST const c_tklist[] = { /* Command mappings. */
47 {"kil1", "O", "insert line"},
48 {"kdch1", "x", "delete character"},
49 {"kcud1", "j", "cursor down"},
50 {"kel", "D", "delete to eol"},
51 {"kind", "\004", "scroll down"}, /* ^D */
52 {"kll", "$", "go to eol"},
53 {"kend", "$", "go to eol"},
54 {"khome", "^", "go to sol"},
55 {"kich1", "i", "insert at cursor"},
56 {"kdl1", "dd", "delete line"},
57 {"kcub1", "h", "cursor left"},
58 {"knp", "\006", "page down"}, /* ^F */
59 {"kpp", "\002", "page up"}, /* ^B */
60 {"kri", "\025", "scroll up"}, /* ^U */
61 {"ked", "dG", "delete to end of screen"},
62 {"kcuf1", "l", "cursor right"},
63 {"kcuu1", "k", "cursor up"},
66 static TKLIST const m1_tklist[] = { /* Input mappings (lookup). */
69 static TKLIST const m2_tklist[] = { /* Input mappings (set or delete). */
70 {"kcud1", "\033ja", "cursor down"}, /* ^[ja */
71 {"kcub1", "\033ha", "cursor left"}, /* ^[ha */
72 {"kcuu1", "\033ka", "cursor up"}, /* ^[ka */
73 {"kcuf1", "\033la", "cursor right"}, /* ^[la */
79 * Initialize the special keys defined by the termcap/terminfo entry.
81 * PUBLIC: int cl_term_init(SCR *);
96 /* Command mappings. */
97 for (tkp = c_tklist; tkp->name != NULL; ++tkp) {
98 if ((t = tigetstr(tkp->ts)) == NULL || t == (char *)-1)
100 CHAR2INT(sp, tkp->name, strlen(tkp->name), wp, wlen);
101 MEMCPY(name, wp, wlen);
102 CHAR2INT(sp, t, strlen(t), wp, wlen);
103 MEMCPY(ts, wp, wlen);
104 CHAR2INT(sp, tkp->output, strlen(tkp->output), wp, wlen);
105 MEMCPY(output, wp, wlen);
106 if (seq_set(sp, name, strlen(tkp->name), ts, strlen(t),
107 output, strlen(tkp->output), SEQ_COMMAND,
108 SEQ_NOOVERWRITE | SEQ_SCREEN))
112 /* Input mappings needing to be looked up. */
113 for (tkp = m1_tklist; tkp->name != NULL; ++tkp) {
114 if ((t = tigetstr(tkp->ts)) == NULL || t == (char *)-1)
116 for (kp = keylist;; ++kp)
117 if (kp->value == tkp->value)
121 CHAR2INT(sp, tkp->name, strlen(tkp->name), wp, wlen);
122 MEMCPY(name, wp, wlen);
123 CHAR2INT(sp, t, strlen(t), wp, wlen);
124 MEMCPY(ts, wp, wlen);
125 output[0] = (UCHAR_T)kp->ch;
126 if (seq_set(sp, name, strlen(tkp->name), ts, strlen(t),
127 output, 1, SEQ_INPUT, SEQ_NOOVERWRITE | SEQ_SCREEN))
131 /* Input mappings that are already set or are text deletions. */
132 for (tkp = m2_tklist; tkp->name != NULL; ++tkp) {
133 if ((t = tigetstr(tkp->ts)) == NULL || t == (char *)-1)
137 * Some terminals' <cursor_left> keys send single <backspace>
138 * characters. This is okay in command mapping, but not okay
139 * in input mapping. That combination is the only one we'll
140 * ever see, hopefully, so kluge it here for now.
142 if (!strcmp(t, "\b"))
144 if (tkp->output == NULL) {
145 CHAR2INT(sp, tkp->name, strlen(tkp->name), wp, wlen);
146 MEMCPY(name, wp, wlen);
147 CHAR2INT(sp, t, strlen(t), wp, wlen);
148 MEMCPY(ts, wp, wlen);
149 if (seq_set(sp, name, strlen(tkp->name),
150 ts, strlen(t), NULL, 0,
151 SEQ_INPUT, SEQ_NOOVERWRITE | SEQ_SCREEN))
154 CHAR2INT(sp, tkp->name, strlen(tkp->name), wp, wlen);
155 MEMCPY(name, wp, wlen);
156 CHAR2INT(sp, t, strlen(t), wp, wlen);
157 MEMCPY(ts, wp, wlen);
158 CHAR2INT(sp, tkp->output, strlen(tkp->output), wp, wlen);
159 MEMCPY(output, wp, wlen);
160 if (seq_set(sp, name, strlen(tkp->name),
161 ts, strlen(t), output, strlen(tkp->output),
162 SEQ_INPUT, SEQ_NOOVERWRITE | SEQ_SCREEN))
168 * Rework any function key mappings that were set before the
169 * screen was initialized.
171 SLIST_FOREACH(qp, sp->gp->seqq, q)
172 if (F_ISSET(qp, SEQ_FUNCMAP))
173 (void)cl_pfmap(sp, qp->stype,
174 qp->input, qp->ilen, qp->output, qp->olen);
180 * End the special keys defined by the termcap/terminfo entry.
182 * PUBLIC: int cl_term_end(GS *);
187 SEQ *qp, *nqp, *pre_qp = NULL;
189 /* Delete screen specific mappings. */
190 SLIST_FOREACH_MUTABLE(qp, gp->seqq, q, nqp)
191 if (F_ISSET(qp, SEQ_SCREEN)) {
192 if (qp == SLIST_FIRST(gp->seqq))
193 SLIST_REMOVE_HEAD(gp->seqq, q);
195 SLIST_REMOVE_AFTER(pre_qp, q);
204 * Map a function key.
206 * PUBLIC: int cl_fmap(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)
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(keyname,
246 SIZE(keyname), L("function key %d"),
247 (int)STRTOL(from+1,NULL,10));
248 CHAR2INT(sp, p, strlen(p), wp, wlen);
249 MEMCPY(ts, wp, wlen);
250 return (seq_set(sp, keyname, nlen,
251 ts, strlen(p), to, tlen, stype, SEQ_NOOVERWRITE | SEQ_SCREEN));
256 * Curses screen specific "option changed" routine.
258 * PUBLIC: int cl_optchange(SCR *, int, char *, u_long *);
261 cl_optchange(SCR *sp, int opt, char *str, u_long *valp)
269 F_CLR(sp, SC_SCR_EX | SC_SCR_VI);
274 * Changing the terminal type requires that we reinitialize
275 * curses, while resizing does not.
277 F_SET(sp->gp, G_SRESTART);
280 (void)cl_omesg(sp, clp, *valp);
284 F_SET(clp, CL_RENAME_OK);
287 * If the screen is live, i.e. we're not reading the
288 * .exrc file, update the window.
290 if (sp->frp != NULL && sp->frp->name != NULL)
291 (void)cl_rename(sp, sp->frp->name, 1);
293 F_CLR(clp, CL_RENAME_OK);
295 (void)cl_rename(sp, NULL, 0);
304 * Turn the tty write permission on or off.
306 * PUBLIC: int cl_omesg(SCR *, CL_PRIVATE *, int);
309 cl_omesg(SCR *sp, CL_PRIVATE *clp, int on)
314 /* Find the tty, get the current permissions. */
315 if ((tty = ttyname(STDERR_FILENO)) == NULL) {
317 msgq(sp, M_SYSERR, "stderr");
320 if (stat(tty, &sb) < 0) {
322 msgq(sp, M_SYSERR, "%s", tty);
326 /* Save the original status if it's unknown. */
327 if (clp->tgw == TGW_UNKNOWN)
328 clp->tgw = sb.st_mode & S_IWGRP ? TGW_SET : TGW_UNSET;
330 /* Toggle the permissions. */
332 if (chmod(tty, sb.st_mode | S_IWGRP) < 0) {
335 "046|messages not turned on: %s", tty);
339 if (chmod(tty, sb.st_mode & ~S_IWGRP) < 0) {
342 "045|messages not turned off: %s", tty);
350 * Return the terminal size.
352 * PUBLIC: int cl_ssize(SCR *, int, size_t *, size_t *, int *);
355 cl_ssize(SCR *sp, int sigwinch, size_t *rowp, size_t *colp, int *changedp)
362 /* Assume it's changed. */
363 if (changedp != NULL)
370 * Get the screen rows and columns. If the values are wrong, it's
371 * not a big deal -- as soon as the user sets them explicitly the
372 * environment will be set and the screen package will use the new
378 if (ioctl(STDERR_FILENO, TIOCGWINSZ, &win) != -1) {
382 /* If here because of suspend or a signal, only trust TIOCGWINSZ. */
385 * Somebody didn't get TIOCGWINSZ right, or has suspend
386 * without window resizing support. The user just lost,
387 * but there's nothing we can do.
389 if (row == 0 || col == 0) {
390 if (changedp != NULL)
396 * SunOS systems deliver SIGWINCH when windows are uncovered
397 * as well as when they change size. In addition, we call
398 * here when continuing after being suspended since the window
399 * may have changed size. Since we don't want to background
400 * all of the screens just because the window was uncovered,
401 * ignore the signal if there's no change.
404 row == O_VAL(sp, O_LINES) && col == O_VAL(sp, O_COLUMNS)) {
405 if (changedp != NULL)
419 * If TIOCGWINSZ failed, or had entries of 0, try termcap. This
420 * routine is called before any termcap or terminal information
421 * has been set up. If there's no TERM environmental variable set,
422 * let it go, at least ex can run.
424 if (row == 0 || col == 0) {
425 if ((p = getenv("TERM")) == NULL)
428 if ((rval = tigetnum("lines")) < 0)
429 msgq(sp, M_SYSERR, "tigetnum: lines");
433 if ((rval = tigetnum("cols")) < 0)
434 msgq(sp, M_SYSERR, "tigetnum: cols");
439 /* If nothing else, well, it's probably a VT100. */
440 noterm: if (row == 0)
447 * POSIX 1003.2 requires the environment to override everything.
448 * Often, people can get nvi to stop messing up their screen by
449 * deleting the LINES and COLUMNS environment variables from their
452 if ((p = getenv("LINES")) != NULL)
453 row = atoz_or(p, row);
454 if ((p = getenv("COLUMNS")) != NULL)
455 col = atoz_or(p, col);
466 * Parse non-zero positive decimal with a fallback.
469 atoz_or(const char *s, size_t y)
472 long x = strtol(s, &ep, 10);
474 if (*ep == '\0' && (0 < x && x < INT_MAX))
482 * Function version of putchar, for tputs.
484 * PUBLIC: int cl_putchar(int);
489 return (putchar(ch));