Another CHAR_T patch.
[nvi.git] / cl / cl_term.c
blob0b7da26afe3ca4b979ff62252b293aad84fa3397
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.24 2000/07/14 14:29:14 skimo Exp $ (Berkeley) $Date: 2000/07/14 14:29:14 $";
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 <curses.h>
23 #include <errno.h>
24 #include <limits.h>
25 #include <signal.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <termios.h>
30 #include <unistd.h>
32 #include "../common/common.h"
33 #include "cl.h"
35 static int cl_pfmap __P((SCR *, seq_t, CHAR_T *, size_t, CHAR_T *, size_t));
38 * XXX
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). */
46 } TKLIST;
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 {"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(sp)
85 SCR *sp;
87 KEYLIST *kp;
88 SEQ *qp;
89 TKLIST const *tkp;
90 char *t;
91 CHAR_T name[60];
92 CHAR_T output[5];
93 CHAR_T ts[20];
94 CHAR_T *wp;
95 size_t wlen;
97 /* Command mappings. */
98 for (tkp = c_tklist; tkp->name != NULL; ++tkp) {
99 if ((t = tigetstr(tkp->ts)) == NULL || t == (char *)-1)
100 continue;
101 CHAR2INT(sp, tkp->name, strlen(tkp->name), wp, wlen);
102 memcpy(name, wp, wlen * sizeof(CHAR_T));
103 CHAR2INT(sp, t, strlen(t), wp, wlen);
104 memcpy(ts, wp, wlen * sizeof(CHAR_T));
105 CHAR2INT(sp, tkp->output, strlen(tkp->output), wp, wlen);
106 memcpy(output, wp, wlen * sizeof(CHAR_T));
107 if (seq_set(sp, name, strlen(tkp->name), ts, strlen(t),
108 output, strlen(tkp->output), SEQ_COMMAND,
109 SEQ_NOOVERWRITE | SEQ_SCREEN))
110 return (1);
113 /* Input mappings needing to be looked up. */
114 for (tkp = m1_tklist; tkp->name != NULL; ++tkp) {
115 if ((t = tigetstr(tkp->ts)) == NULL || t == (char *)-1)
116 continue;
117 for (kp = keylist;; ++kp)
118 if (kp->value == tkp->value)
119 break;
120 if (kp == NULL)
121 continue;
122 CHAR2INT(sp, tkp->name, strlen(tkp->name), wp, wlen);
123 memcpy(name, wp, wlen * sizeof(CHAR_T));
124 CHAR2INT(sp, t, strlen(t), wp, wlen);
125 memcpy(ts, wp, wlen * sizeof(CHAR_T));
126 if (seq_set(sp, name, strlen(tkp->name), ts, strlen(t),
127 &kp->ch, 1, SEQ_INPUT, SEQ_NOOVERWRITE | SEQ_SCREEN))
128 return (1);
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)
134 continue;
136 * !!!
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"))
143 continue;
144 if (tkp->output == NULL) {
145 if (seq_set(sp, tkp->name, strlen(tkp->name),
146 t, strlen(t), NULL, 0,
147 SEQ_INPUT, SEQ_NOOVERWRITE | SEQ_SCREEN))
148 return (1);
149 } else {
150 CHAR2INT(sp, tkp->name, strlen(tkp->name), wp, wlen);
151 memcpy(name, wp, wlen * sizeof(CHAR_T));
152 CHAR2INT(sp, t, strlen(t), wp, wlen);
153 memcpy(ts, wp, wlen * sizeof(CHAR_T));
154 CHAR2INT(sp, tkp->output, strlen(tkp->output), wp, wlen);
155 memcpy(output, wp, wlen * sizeof(CHAR_T));
156 if (seq_set(sp, name, strlen(tkp->name),
157 ts, strlen(t), output, strlen(tkp->output),
158 SEQ_INPUT, SEQ_NOOVERWRITE | SEQ_SCREEN))
159 return (1);
164 * Rework any function key mappings that were set before the
165 * screen was initialized.
167 for (qp = sp->gp->seqq.lh_first; qp != NULL; qp = qp->q.le_next)
168 if (F_ISSET(qp, SEQ_FUNCMAP))
169 (void)cl_pfmap(sp, qp->stype,
170 qp->input, qp->ilen, qp->output, qp->olen);
171 return (0);
175 * cl_term_end --
176 * End the special keys defined by the termcap/terminfo entry.
178 * PUBLIC: int cl_term_end __P((GS *));
181 cl_term_end(gp)
182 GS *gp;
184 SEQ *qp, *nqp;
186 /* Delete screen specific mappings. */
187 for (qp = gp->seqq.lh_first; qp != NULL; qp = nqp) {
188 nqp = qp->q.le_next;
189 if (F_ISSET(qp, SEQ_SCREEN))
190 (void)seq_mdel(qp);
192 return (0);
196 * cl_fmap --
197 * Map a function key.
199 * PUBLIC: int cl_fmap __P((SCR *, seq_t, CHAR_T *, size_t, CHAR_T *, size_t));
202 cl_fmap(sp, stype, from, flen, to, tlen)
203 SCR *sp;
204 seq_t stype;
205 CHAR_T *from, *to;
206 size_t flen, tlen;
208 /* Ignore until the screen is running, do the real work then. */
209 if (F_ISSET(sp, SC_VI) && !F_ISSET(sp, SC_SCR_VI))
210 return (0);
211 if (F_ISSET(sp, SC_EX) && !F_ISSET(sp, SC_SCR_EX))
212 return (0);
214 return (cl_pfmap(sp, stype, from, flen, to, tlen));
218 * cl_pfmap --
219 * Map a function key (private version).
221 static int
222 cl_pfmap(sp, stype, from, flen, to, tlen)
223 SCR *sp;
224 seq_t stype;
225 CHAR_T *from, *to;
226 size_t flen, tlen;
228 size_t nlen;
229 char *p, keyname[64];
231 (void)snprintf(keyname, sizeof(keyname), "kf%d", atoi(from + 1));
232 if ((p = tigetstr(keyname)) == NULL ||
233 p == (char *)-1 || strlen(p) == 0)
234 p = NULL;
235 if (p == NULL) {
236 msgq_str(sp, M_ERR, from, "233|This terminal has no %s key");
237 return (1);
240 nlen = snprintf(keyname,
241 sizeof(keyname), "function key %d", atoi(from + 1));
242 return (seq_set(sp, keyname, nlen,
243 p, strlen(p), to, tlen, stype, SEQ_NOOVERWRITE | SEQ_SCREEN));
247 * cl_optchange --
248 * Curses screen specific "option changed" routine.
250 * PUBLIC: int cl_optchange __P((SCR *, int, char *, u_long *));
253 cl_optchange(sp, opt, str, valp)
254 SCR *sp;
255 int opt;
256 char *str;
257 u_long *valp;
259 CL_PRIVATE *clp;
261 clp = CLP(sp);
263 switch (opt) {
264 case O_COLUMNS:
265 case O_LINES:
266 case O_TERM:
268 * Changing the columns, lines or terminal require that
269 * we restart the screen.
271 F_SET(sp->gp, G_SRESTART);
272 F_CLR(sp, SC_SCR_EX | SC_SCR_VI);
273 break;
274 case O_MESG:
275 (void)cl_omesg(sp, clp, *valp);
276 break;
277 case O_WINDOWNAME:
278 if (*valp) {
279 F_SET(clp, CL_RENAME_OK);
282 * If the screen is live, i.e. we're not reading the
283 * .exrc file, update the window.
285 if (sp->frp != NULL && sp->frp->name != NULL)
286 (void)cl_rename(sp, sp->frp->name, 1);
287 } else {
288 F_CLR(clp, CL_RENAME_OK);
290 (void)cl_rename(sp, NULL, 0);
292 break;
294 return (0);
298 * cl_omesg --
299 * Turn the tty write permission on or off.
301 * PUBLIC: int cl_omesg __P((SCR *, CL_PRIVATE *, int));
304 cl_omesg(sp, clp, on)
305 SCR *sp;
306 CL_PRIVATE *clp;
307 int on;
309 struct stat sb;
310 char *tty;
312 /* Find the tty, get the current permissions. */
313 if ((tty = ttyname(STDERR_FILENO)) == NULL) {
314 if (sp != NULL)
315 msgq(sp, M_SYSERR, "stderr");
316 return (1);
318 if (stat(tty, &sb) < 0) {
319 if (sp != NULL)
320 msgq(sp, M_SYSERR, "%s", tty);
321 return (1);
324 /* Save the original status if it's unknown. */
325 if (clp->tgw == TGW_UNKNOWN)
326 clp->tgw = sb.st_mode & S_IWGRP ? TGW_SET : TGW_UNSET;
328 /* Toggle the permissions. */
329 if (on) {
330 if (chmod(tty, sb.st_mode | S_IWGRP) < 0) {
331 if (sp != NULL)
332 msgq(sp, M_SYSERR,
333 "046|messages not turned on: %s", tty);
334 return (1);
336 } else
337 if (chmod(tty, sb.st_mode & ~S_IWGRP) < 0) {
338 if (sp != NULL)
339 msgq(sp, M_SYSERR,
340 "045|messages not turned off: %s", tty);
341 return (1);
343 return (0);
347 * cl_ssize --
348 * Return the terminal size.
350 * PUBLIC: int cl_ssize __P((SCR *, int, size_t *, size_t *, int *));
353 cl_ssize(sp, sigwinch, rowp, colp, changedp)
354 SCR *sp;
355 int sigwinch;
356 size_t *rowp, *colp;
357 int *changedp;
359 #ifdef TIOCGWINSZ
360 struct winsize win;
361 #endif
362 size_t col, row;
363 int rval;
364 char *p;
366 /* Assume it's changed. */
367 if (changedp != NULL)
368 *changedp = 1;
371 * !!!
372 * sp may be 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
377 * values.
379 * Try TIOCGWINSZ.
381 row = col = 0;
382 #ifdef TIOCGWINSZ
383 if (ioctl(STDERR_FILENO, TIOCGWINSZ, &win) != -1) {
384 row = win.ws_row;
385 col = win.ws_col;
387 #endif
388 /* If here because of suspend or a signal, only trust TIOCGWINSZ. */
389 if (sigwinch) {
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)
397 *changedp = 0;
398 return (0);
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.
409 if (sp != NULL &&
410 row == O_VAL(sp, O_LINES) && col == O_VAL(sp, O_COLUMNS)) {
411 if (changedp != NULL)
412 *changedp = 0;
413 return (0);
416 if (rowp != NULL)
417 *rowp = row;
418 if (colp != NULL)
419 *colp = col;
420 return (0);
424 * !!!
425 * If TIOCGWINSZ failed, or had entries of 0, try termcap. This
426 * routine is called before any termcap or terminal information
427 * has been set up. If there's no TERM environmental variable set,
428 * let it go, at least ex can run.
430 if (row == 0 || col == 0) {
431 if ((p = getenv("TERM")) == NULL)
432 goto noterm;
433 if (row == 0)
434 if ((rval = tigetnum("lines")) < 0)
435 msgq(sp, M_SYSERR, "tigetnum: lines");
436 else
437 row = rval;
438 if (col == 0)
439 if ((rval = tigetnum("cols")) < 0)
440 msgq(sp, M_SYSERR, "tigetnum: cols");
441 else
442 col = rval;
445 /* If nothing else, well, it's probably a VT100. */
446 noterm: if (row == 0)
447 row = 24;
448 if (col == 0)
449 col = 80;
452 * !!!
453 * POSIX 1003.2 requires the environment to override everything.
454 * Often, people can get nvi to stop messing up their screen by
455 * deleting the LINES and COLUMNS environment variables from their
456 * dot-files.
458 if ((p = getenv("LINES")) != NULL)
459 row = strtol(p, NULL, 10);
460 if ((p = getenv("COLUMNS")) != NULL)
461 col = strtol(p, NULL, 10);
463 if (rowp != NULL)
464 *rowp = row;
465 if (colp != NULL)
466 *colp = col;
467 return (0);
471 * cl_putchar --
472 * Function version of putchar, for tputs.
474 * PUBLIC: int cl_putchar __P((int));
477 cl_putchar(ch)
478 int ch;
480 return (putchar(ch));