Display single column Unicode characters in one column.
[nvi.git] / cl / cl_term.c
blob0fb1e4b44da5788cc24073134c4f28d083c17028
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.27 2000/07/23 11:14:45 skimo Exp $ (Berkeley) $Date: 2000/07/23 11:14:45 $";
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 {"khome", "^", "go to sol"},
54 {"kich1", "i", "insert at cursor"},
55 {"kdl1", "dd", "delete line"},
56 {"kcub1", "h", "cursor left"},
57 {"knp", "\006", "page down"}, /* ^F */
58 {"kpp", "\002", "page up"}, /* ^B */
59 {"kri", "\025", "scroll up"}, /* ^U */
60 {"ked", "dG", "delete to end of screen"},
61 {"kcuf1", "l", "cursor right"},
62 {"kcuu1", "k", "cursor up"},
63 {NULL},
65 static TKLIST const m1_tklist[] = { /* Input mappings (lookup). */
66 {NULL},
68 static TKLIST const m2_tklist[] = { /* Input mappings (set or delete). */
69 {"kcud1", "\033ja", "cursor down"}, /* ^[ja */
70 {"kcub1", "\033ha", "cursor left"}, /* ^[ha */
71 {"kcuu1", "\033ka", "cursor up"}, /* ^[ka */
72 {"kcuf1", "\033la", "cursor right"}, /* ^[la */
73 {NULL},
77 * cl_term_init --
78 * Initialize the special keys defined by the termcap/terminfo entry.
80 * PUBLIC: int cl_term_init __P((SCR *));
82 int
83 cl_term_init(sp)
84 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(gp)
185 GS *gp;
187 SEQ *qp, *nqp;
189 /* Delete screen specific mappings. */
190 for (qp = gp->seqq.lh_first; qp != NULL; qp = nqp) {
191 nqp = qp->q.le_next;
192 if (F_ISSET(qp, SEQ_SCREEN))
193 (void)seq_mdel(qp);
195 return (0);
199 * cl_fmap --
200 * Map a function key.
202 * PUBLIC: int cl_fmap __P((SCR *, seq_t, CHAR_T *, size_t, CHAR_T *, size_t));
205 cl_fmap(sp, stype, from, flen, to, tlen)
206 SCR *sp;
207 seq_t stype;
208 CHAR_T *from, *to;
209 size_t flen, 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))
213 return (0);
214 if (F_ISSET(sp, SC_EX) && !F_ISSET(sp, SC_SCR_EX))
215 return (0);
217 return (cl_pfmap(sp, stype, from, flen, to, tlen));
221 * cl_pfmap --
222 * Map a function key (private version).
224 static int
225 cl_pfmap(sp, stype, from, flen, to, tlen)
226 SCR *sp;
227 seq_t stype;
228 CHAR_T *from, *to;
229 size_t flen, tlen;
231 size_t nlen;
232 char *p, keyname[64];
233 char *np;
234 size_t nplen;
235 CHAR_T name[60];
236 CHAR_T ts[20];
237 CHAR_T *wp;
238 size_t wlen;
240 INT2CHAR(sp, from+1, v_strlen(from+1)+1, np, nplen);
241 (void)snprintf(keyname, sizeof(keyname), "kf%d", atoi(np));
242 if ((p = tigetstr(keyname)) == NULL ||
243 p == (char *)-1 || strlen(p) == 0)
244 p = NULL;
245 if (p == NULL) {
246 msgq_wstr(sp, M_ERR, from, "233|This terminal has no %s key");
247 return (1);
250 INT2CHAR(sp, from+1, v_strlen(from+1)+1, np, nplen);
251 nlen = snprintf(keyname,
252 sizeof(keyname), "function key %d", atoi(np));
253 CHAR2INT(sp, keyname, nlen, wp, wlen);
254 MEMCPYW(name, wp, wlen);
255 CHAR2INT(sp, p, strlen(p), wp, wlen);
256 MEMCPYW(ts, wp, wlen);
257 return (seq_set(sp, name, nlen,
258 ts, strlen(p), to, tlen, stype, SEQ_NOOVERWRITE | SEQ_SCREEN));
262 * cl_optchange --
263 * Curses screen specific "option changed" routine.
265 * PUBLIC: int cl_optchange __P((SCR *, int, char *, u_long *));
268 cl_optchange(sp, opt, str, valp)
269 SCR *sp;
270 int opt;
271 char *str;
272 u_long *valp;
274 CL_PRIVATE *clp;
276 clp = CLP(sp);
278 switch (opt) {
279 case O_COLUMNS:
280 case O_LINES:
281 case O_TERM:
283 * Changing the columns, lines or terminal require that
284 * we restart the screen.
286 F_SET(sp->gp, G_SRESTART);
287 F_CLR(sp, SC_SCR_EX | SC_SCR_VI);
288 break;
289 case O_MESG:
290 (void)cl_omesg(sp, clp, *valp);
291 break;
292 case O_WINDOWNAME:
293 if (*valp) {
294 F_SET(clp, CL_RENAME_OK);
297 * If the screen is live, i.e. we're not reading the
298 * .exrc file, update the window.
300 if (sp->frp != NULL && sp->frp->name != NULL)
301 (void)cl_rename(sp, sp->frp->name, 1);
302 } else {
303 F_CLR(clp, CL_RENAME_OK);
305 (void)cl_rename(sp, NULL, 0);
307 break;
309 return (0);
313 * cl_omesg --
314 * Turn the tty write permission on or off.
316 * PUBLIC: int cl_omesg __P((SCR *, CL_PRIVATE *, int));
319 cl_omesg(sp, clp, on)
320 SCR *sp;
321 CL_PRIVATE *clp;
322 int on;
324 struct stat sb;
325 char *tty;
327 /* Find the tty, get the current permissions. */
328 if ((tty = ttyname(STDERR_FILENO)) == NULL) {
329 if (sp != NULL)
330 msgq(sp, M_SYSERR, "stderr");
331 return (1);
333 if (stat(tty, &sb) < 0) {
334 if (sp != NULL)
335 msgq(sp, M_SYSERR, "%s", tty);
336 return (1);
339 /* Save the original status if it's unknown. */
340 if (clp->tgw == TGW_UNKNOWN)
341 clp->tgw = sb.st_mode & S_IWGRP ? TGW_SET : TGW_UNSET;
343 /* Toggle the permissions. */
344 if (on) {
345 if (chmod(tty, sb.st_mode | S_IWGRP) < 0) {
346 if (sp != NULL)
347 msgq(sp, M_SYSERR,
348 "046|messages not turned on: %s", tty);
349 return (1);
351 } else
352 if (chmod(tty, sb.st_mode & ~S_IWGRP) < 0) {
353 if (sp != NULL)
354 msgq(sp, M_SYSERR,
355 "045|messages not turned off: %s", tty);
356 return (1);
358 return (0);
362 * cl_ssize --
363 * Return the terminal size.
365 * PUBLIC: int cl_ssize __P((SCR *, int, size_t *, size_t *, int *));
368 cl_ssize(sp, sigwinch, rowp, colp, changedp)
369 SCR *sp;
370 int sigwinch;
371 size_t *rowp, *colp;
372 int *changedp;
374 #ifdef TIOCGWINSZ
375 struct winsize win;
376 #endif
377 size_t col, row;
378 int rval;
379 char *p;
381 /* Assume it's changed. */
382 if (changedp != NULL)
383 *changedp = 1;
386 * !!!
387 * sp may be NULL.
389 * Get the screen rows and columns. If the values are wrong, it's
390 * not a big deal -- as soon as the user sets them explicitly the
391 * environment will be set and the screen package will use the new
392 * values.
394 * Try TIOCGWINSZ.
396 row = col = 0;
397 #ifdef TIOCGWINSZ
398 if (ioctl(STDERR_FILENO, TIOCGWINSZ, &win) != -1) {
399 row = win.ws_row;
400 col = win.ws_col;
402 #endif
403 /* If here because of suspend or a signal, only trust TIOCGWINSZ. */
404 if (sigwinch) {
406 * Somebody didn't get TIOCGWINSZ right, or has suspend
407 * without window resizing support. The user just lost,
408 * but there's nothing we can do.
410 if (row == 0 || col == 0) {
411 if (changedp != NULL)
412 *changedp = 0;
413 return (0);
417 * SunOS systems deliver SIGWINCH when windows are uncovered
418 * as well as when they change size. In addition, we call
419 * here when continuing after being suspended since the window
420 * may have changed size. Since we don't want to background
421 * all of the screens just because the window was uncovered,
422 * ignore the signal if there's no change.
424 if (sp != NULL &&
425 row == O_VAL(sp, O_LINES) && col == O_VAL(sp, O_COLUMNS)) {
426 if (changedp != NULL)
427 *changedp = 0;
428 return (0);
431 if (rowp != NULL)
432 *rowp = row;
433 if (colp != NULL)
434 *colp = col;
435 return (0);
439 * !!!
440 * If TIOCGWINSZ failed, or had entries of 0, try termcap. This
441 * routine is called before any termcap or terminal information
442 * has been set up. If there's no TERM environmental variable set,
443 * let it go, at least ex can run.
445 if (row == 0 || col == 0) {
446 if ((p = getenv("TERM")) == NULL)
447 goto noterm;
448 if (row == 0)
449 if ((rval = tigetnum("lines")) < 0)
450 msgq(sp, M_SYSERR, "tigetnum: lines");
451 else
452 row = rval;
453 if (col == 0)
454 if ((rval = tigetnum("cols")) < 0)
455 msgq(sp, M_SYSERR, "tigetnum: cols");
456 else
457 col = rval;
460 /* If nothing else, well, it's probably a VT100. */
461 noterm: if (row == 0)
462 row = 24;
463 if (col == 0)
464 col = 80;
467 * !!!
468 * POSIX 1003.2 requires the environment to override everything.
469 * Often, people can get nvi to stop messing up their screen by
470 * deleting the LINES and COLUMNS environment variables from their
471 * dot-files.
473 if ((p = getenv("LINES")) != NULL)
474 row = strtol(p, NULL, 10);
475 if ((p = getenv("COLUMNS")) != NULL)
476 col = strtol(p, NULL, 10);
478 if (rowp != NULL)
479 *rowp = row;
480 if (colp != NULL)
481 *colp = col;
482 return (0);
486 * cl_putchar --
487 * Function version of putchar, for tputs.
489 * PUBLIC: int cl_putchar __P((int));
492 cl_putchar(ch)
493 int ch;
495 return (putchar(ch));