distrib: run libtoolize
[nvi.git] / cl / cl_term.c
blob94e8dddb9658c6dcb9057bb8b72482efd8a11c61
1 /*-
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.
8 */
10 #include "config.h"
12 #ifndef lint
13 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 $";
14 #endif /* not lint */
16 #include <sys/types.h>
17 #include <sys/ioctl.h>
18 #include <sys/queue.h>
19 #include <sys/stat.h>
21 #include <bitstring.h>
22 #include <errno.h>
23 #include <limits.h>
24 #include <signal.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <termios.h>
29 #include <unistd.h>
31 #include "../common/common.h"
32 #include "cl.h"
34 static int cl_pfmap __P((SCR *, seq_t, CHAR_T *, size_t, CHAR_T *, size_t));
37 * XXX
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). */
45 } TKLIST;
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"},
64 {NULL},
66 static TKLIST const m1_tklist[] = { /* Input mappings (lookup). */
67 {NULL},
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 */
74 {NULL},
78 * cl_term_init --
79 * Initialize the special keys defined by the termcap/terminfo entry.
81 * PUBLIC: int cl_term_init __P((SCR *));
83 int
84 cl_term_init(SCR *sp)
86 KEYLIST *kp;
87 SEQ *qp;
88 TKLIST const *tkp;
89 char *t;
90 CHAR_T name[60];
91 CHAR_T output[5];
92 CHAR_T ts[20];
93 CHAR_T *wp;
94 size_t wlen;
96 /* Command mappings. */
97 for (tkp = c_tklist; tkp->name != NULL; ++tkp) {
98 if ((t = tigetstr(tkp->ts)) == NULL || t == (char *)-1)
99 continue;
100 CHAR2INT(sp, tkp->name, strlen(tkp->name), wp, wlen);
101 MEMCPYW(name, wp, wlen);
102 CHAR2INT(sp, t, strlen(t), wp, wlen);
103 MEMCPYW(ts, wp, wlen);
104 CHAR2INT(sp, tkp->output, strlen(tkp->output), wp, wlen);
105 MEMCPYW(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))
109 return (1);
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)
115 continue;
116 for (kp = keylist;; ++kp)
117 if (kp->value == tkp->value)
118 break;
119 if (kp == NULL)
120 continue;
121 CHAR2INT(sp, tkp->name, strlen(tkp->name), wp, wlen);
122 MEMCPYW(name, wp, wlen);
123 CHAR2INT(sp, t, strlen(t), wp, wlen);
124 MEMCPYW(ts, wp, wlen);
125 if (seq_set(sp, name, strlen(tkp->name), ts, strlen(t),
126 &kp->ch, 1, SEQ_INPUT, SEQ_NOOVERWRITE | SEQ_SCREEN))
127 return (1);
130 /* Input mappings that are already set or are text deletions. */
131 for (tkp = m2_tklist; tkp->name != NULL; ++tkp) {
132 if ((t = tigetstr(tkp->ts)) == NULL || t == (char *)-1)
133 continue;
135 * !!!
136 * Some terminals' <cursor_left> keys send single <backspace>
137 * characters. This is okay in command mapping, but not okay
138 * in input mapping. That combination is the only one we'll
139 * ever see, hopefully, so kluge it here for now.
141 if (!strcmp(t, "\b"))
142 continue;
143 if (tkp->output == NULL) {
144 CHAR2INT(sp, tkp->name, strlen(tkp->name), wp, wlen);
145 MEMCPYW(name, wp, wlen);
146 CHAR2INT(sp, t, strlen(t), wp, wlen);
147 MEMCPYW(ts, wp, wlen);
148 if (seq_set(sp, name, strlen(tkp->name),
149 ts, strlen(t), NULL, 0,
150 SEQ_INPUT, SEQ_NOOVERWRITE | SEQ_SCREEN))
151 return (1);
152 } else {
153 CHAR2INT(sp, tkp->name, strlen(tkp->name), wp, wlen);
154 MEMCPYW(name, wp, wlen);
155 CHAR2INT(sp, t, strlen(t), wp, wlen);
156 MEMCPYW(ts, wp, wlen);
157 CHAR2INT(sp, tkp->output, strlen(tkp->output), wp, wlen);
158 MEMCPYW(output, wp, wlen);
159 if (seq_set(sp, name, strlen(tkp->name),
160 ts, strlen(t), output, strlen(tkp->output),
161 SEQ_INPUT, SEQ_NOOVERWRITE | SEQ_SCREEN))
162 return (1);
167 * Rework any function key mappings that were set before the
168 * screen was initialized.
170 for (qp = sp->gp->seqq.lh_first; qp != NULL; qp = qp->q.le_next)
171 if (F_ISSET(qp, SEQ_FUNCMAP))
172 (void)cl_pfmap(sp, qp->stype,
173 qp->input, qp->ilen, qp->output, qp->olen);
174 return (0);
178 * cl_term_end --
179 * End the special keys defined by the termcap/terminfo entry.
181 * PUBLIC: int cl_term_end __P((GS *));
184 cl_term_end(GS *gp)
186 SEQ *qp, *nqp;
188 /* Delete screen specific mappings. */
189 for (qp = gp->seqq.lh_first; qp != NULL; qp = nqp) {
190 nqp = qp->q.le_next;
191 if (F_ISSET(qp, SEQ_SCREEN))
192 (void)seq_mdel(qp);
194 return (0);
198 * cl_fmap --
199 * Map a function key.
201 * PUBLIC: int cl_fmap __P((SCR *, seq_t, CHAR_T *, size_t, CHAR_T *, size_t));
204 cl_fmap(SCR *sp, seq_t stype, CHAR_T *from, size_t flen, CHAR_T *to, size_t tlen)
206 /* Ignore until the screen is running, do the real work then. */
207 if (F_ISSET(sp, SC_VI) && !F_ISSET(sp, SC_SCR_VI))
208 return (0);
209 if (F_ISSET(sp, SC_EX) && !F_ISSET(sp, SC_SCR_EX))
210 return (0);
212 return (cl_pfmap(sp, stype, from, flen, to, tlen));
216 * cl_pfmap --
217 * Map a function key (private version).
219 static int
220 cl_pfmap(SCR *sp, seq_t stype, CHAR_T *from, size_t flen, CHAR_T *to, size_t tlen)
222 size_t nlen;
223 char *p;
224 char name[64];
225 CHAR_T keyname[64];
226 CHAR_T ts[20];
227 CHAR_T *wp;
228 size_t wlen;
230 (void)snprintf(name, sizeof(name), "kf%d",
231 (int)STRTOL(from+1,NULL,10));
232 if ((p = tigetstr(name)) == NULL ||
233 p == (char *)-1 || strlen(p) == 0)
234 p = NULL;
235 if (p == NULL) {
236 msgq_wstr(sp, M_ERR, from, "233|This terminal has no %s key");
237 return (1);
240 nlen = SPRINTF(keyname,
241 SIZE(keyname), L("function key %d"),
242 (int)STRTOL(from+1,NULL,10));
243 CHAR2INT(sp, p, strlen(p), wp, wlen);
244 MEMCPYW(ts, wp, wlen);
245 return (seq_set(sp, keyname, nlen,
246 ts, strlen(p), to, tlen, stype, SEQ_NOOVERWRITE | SEQ_SCREEN));
250 * cl_optchange --
251 * Curses screen specific "option changed" routine.
253 * PUBLIC: int cl_optchange __P((SCR *, int, char *, u_long *));
256 cl_optchange(SCR *sp, int opt, char *str, u_long *valp)
258 CL_PRIVATE *clp;
260 clp = CLP(sp);
262 switch (opt) {
263 case O_COLUMNS:
264 case O_LINES:
265 case O_TERM:
267 * Changing the columns, lines or terminal require that
268 * we restart the screen.
270 F_SET(sp->gp, G_SRESTART);
271 F_CLR(sp, SC_SCR_EX | SC_SCR_VI);
272 break;
273 case O_MESG:
274 (void)cl_omesg(sp, clp, *valp);
275 break;
276 case O_WINDOWNAME:
277 if (*valp) {
278 F_SET(clp, CL_RENAME_OK);
281 * If the screen is live, i.e. we're not reading the
282 * .exrc file, update the window.
284 if (sp->frp != NULL && sp->frp->name != NULL)
285 (void)cl_rename(sp, sp->frp->name, 1);
286 } else {
287 F_CLR(clp, CL_RENAME_OK);
289 (void)cl_rename(sp, NULL, 0);
291 break;
293 return (0);
297 * cl_omesg --
298 * Turn the tty write permission on or off.
300 * PUBLIC: int cl_omesg __P((SCR *, CL_PRIVATE *, int));
303 cl_omesg(SCR *sp, CL_PRIVATE *clp, int on)
305 struct stat sb;
306 char *tty;
308 /* Find the tty, get the current permissions. */
309 if ((tty = ttyname(STDERR_FILENO)) == NULL) {
310 if (sp != NULL)
311 msgq(sp, M_SYSERR, "stderr");
312 return (1);
314 if (stat(tty, &sb) < 0) {
315 if (sp != NULL)
316 msgq(sp, M_SYSERR, "%s", tty);
317 return (1);
320 /* Save the original status if it's unknown. */
321 if (clp->tgw == TGW_UNKNOWN)
322 clp->tgw = sb.st_mode & S_IWGRP ? TGW_SET : TGW_UNSET;
324 /* Toggle the permissions. */
325 if (on) {
326 if (chmod(tty, sb.st_mode | S_IWGRP) < 0) {
327 if (sp != NULL)
328 msgq(sp, M_SYSERR,
329 "046|messages not turned on: %s", tty);
330 return (1);
332 } else
333 if (chmod(tty, sb.st_mode & ~S_IWGRP) < 0) {
334 if (sp != NULL)
335 msgq(sp, M_SYSERR,
336 "045|messages not turned off: %s", tty);
337 return (1);
339 return (0);
343 * cl_ssize --
344 * Return the terminal size.
346 * PUBLIC: int cl_ssize __P((SCR *, int, size_t *, size_t *, int *));
349 cl_ssize(SCR *sp, int sigwinch, size_t *rowp, size_t *colp, int *changedp)
351 #ifdef TIOCGWINSZ
352 struct winsize win;
353 #endif
354 size_t col, row;
355 int rval;
356 char *p;
358 /* Assume it's changed. */
359 if (changedp != NULL)
360 *changedp = 1;
363 * !!!
364 * sp may be NULL.
366 * Get the screen rows and columns. If the values are wrong, it's
367 * not a big deal -- as soon as the user sets them explicitly the
368 * environment will be set and the screen package will use the new
369 * values.
371 * Try TIOCGWINSZ.
373 row = col = 0;
374 #ifdef TIOCGWINSZ
375 if (ioctl(STDERR_FILENO, TIOCGWINSZ, &win) != -1) {
376 row = win.ws_row;
377 col = win.ws_col;
379 #endif
380 /* If here because of suspend or a signal, only trust TIOCGWINSZ. */
381 if (sigwinch) {
383 * Somebody didn't get TIOCGWINSZ right, or has suspend
384 * without window resizing support. The user just lost,
385 * but there's nothing we can do.
387 if (row == 0 || col == 0) {
388 if (changedp != NULL)
389 *changedp = 0;
390 return (0);
394 * SunOS systems deliver SIGWINCH when windows are uncovered
395 * as well as when they change size. In addition, we call
396 * here when continuing after being suspended since the window
397 * may have changed size. Since we don't want to background
398 * all of the screens just because the window was uncovered,
399 * ignore the signal if there's no change.
401 if (sp != NULL &&
402 row == O_VAL(sp, O_LINES) && col == O_VAL(sp, O_COLUMNS)) {
403 if (changedp != NULL)
404 *changedp = 0;
405 return (0);
408 if (rowp != NULL)
409 *rowp = row;
410 if (colp != NULL)
411 *colp = col;
412 return (0);
416 * !!!
417 * If TIOCGWINSZ failed, or had entries of 0, try termcap. This
418 * routine is called before any termcap or terminal information
419 * has been set up. If there's no TERM environmental variable set,
420 * let it go, at least ex can run.
422 if (row == 0 || col == 0) {
423 if ((p = getenv("TERM")) == NULL)
424 goto noterm;
425 if (row == 0)
426 if ((rval = tigetnum("lines")) < 0)
427 msgq(sp, M_SYSERR, "tigetnum: lines");
428 else
429 row = rval;
430 if (col == 0)
431 if ((rval = tigetnum("cols")) < 0)
432 msgq(sp, M_SYSERR, "tigetnum: cols");
433 else
434 col = rval;
437 /* If nothing else, well, it's probably a VT100. */
438 noterm: if (row == 0)
439 row = 24;
440 if (col == 0)
441 col = 80;
444 * !!!
445 * POSIX 1003.2 requires the environment to override everything.
446 * Often, people can get nvi to stop messing up their screen by
447 * deleting the LINES and COLUMNS environment variables from their
448 * dot-files.
450 if ((p = getenv("LINES")) != NULL)
451 row = strtol(p, NULL, 10);
452 if ((p = getenv("COLUMNS")) != NULL)
453 col = strtol(p, NULL, 10);
455 if (rowp != NULL)
456 *rowp = row;
457 if (colp != NULL)
458 *colp = col;
459 return (0);
463 * cl_putchar --
464 * Function version of putchar, for tputs.
466 * PUBLIC: int cl_putchar __P((int));
469 cl_putchar(int ch)
471 return (putchar(ch));