align CHAR_T string in log
[nvi.git] / cl / cl_term.c
blob3502f14edcb62d09aaf71013803780d85f2eecf9
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.28 2000/12/01 13:56:18 skimo Exp $ (Berkeley) $Date: 2000/12/01 13:56:18 $";
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(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 MEMCPYW(name, wp, wlen);
103 CHAR2INT(sp, t, strlen(t), wp, wlen);
104 MEMCPYW(ts, wp, wlen);
105 CHAR2INT(sp, tkp->output, strlen(tkp->output), wp, wlen);
106 MEMCPYW(output, wp, wlen);
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 MEMCPYW(name, wp, wlen);
124 CHAR2INT(sp, t, strlen(t), wp, wlen);
125 MEMCPYW(ts, wp, wlen);
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 CHAR2INT(sp, tkp->name, strlen(tkp->name), wp, wlen);
146 MEMCPYW(name, wp, wlen);
147 CHAR2INT(sp, t, strlen(t), wp, wlen);
148 MEMCPYW(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))
152 return (1);
153 } else {
154 CHAR2INT(sp, tkp->name, strlen(tkp->name), wp, wlen);
155 MEMCPYW(name, wp, wlen);
156 CHAR2INT(sp, t, strlen(t), wp, wlen);
157 MEMCPYW(ts, wp, wlen);
158 CHAR2INT(sp, tkp->output, strlen(tkp->output), wp, wlen);
159 MEMCPYW(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))
163 return (1);
168 * Rework any function key mappings that were set before the
169 * screen was initialized.
171 for (qp = sp->gp->seqq.lh_first; qp != NULL; qp = qp->q.le_next)
172 if (F_ISSET(qp, SEQ_FUNCMAP))
173 (void)cl_pfmap(sp, qp->stype,
174 qp->input, qp->ilen, qp->output, qp->olen);
175 return (0);
179 * cl_term_end --
180 * End the special keys defined by the termcap/terminfo entry.
182 * PUBLIC: int cl_term_end __P((GS *));
185 cl_term_end(gp)
186 GS *gp;
188 SEQ *qp, *nqp;
190 /* Delete screen specific mappings. */
191 for (qp = gp->seqq.lh_first; qp != NULL; qp = nqp) {
192 nqp = qp->q.le_next;
193 if (F_ISSET(qp, SEQ_SCREEN))
194 (void)seq_mdel(qp);
196 return (0);
200 * cl_fmap --
201 * Map a function key.
203 * PUBLIC: int cl_fmap __P((SCR *, seq_t, CHAR_T *, size_t, CHAR_T *, size_t));
206 cl_fmap(sp, stype, from, flen, to, tlen)
207 SCR *sp;
208 seq_t stype;
209 CHAR_T *from, *to;
210 size_t flen, tlen;
212 /* Ignore until the screen is running, do the real work then. */
213 if (F_ISSET(sp, SC_VI) && !F_ISSET(sp, SC_SCR_VI))
214 return (0);
215 if (F_ISSET(sp, SC_EX) && !F_ISSET(sp, SC_SCR_EX))
216 return (0);
218 return (cl_pfmap(sp, stype, from, flen, to, tlen));
222 * cl_pfmap --
223 * Map a function key (private version).
225 static int
226 cl_pfmap(sp, stype, from, flen, to, tlen)
227 SCR *sp;
228 seq_t stype;
229 CHAR_T *from, *to;
230 size_t flen, tlen;
232 size_t nlen;
233 char *p, keyname[64];
234 char *np;
235 size_t nplen;
236 CHAR_T name[60];
237 CHAR_T ts[20];
238 CHAR_T *wp;
239 size_t wlen;
241 INT2CHAR(sp, from+1, v_strlen(from+1)+1, np, nplen);
242 (void)snprintf(keyname, sizeof(keyname), "kf%d", atoi(np));
243 if ((p = tigetstr(keyname)) == NULL ||
244 p == (char *)-1 || strlen(p) == 0)
245 p = NULL;
246 if (p == NULL) {
247 msgq_wstr(sp, M_ERR, from, "233|This terminal has no %s key");
248 return (1);
251 INT2CHAR(sp, from+1, v_strlen(from+1)+1, np, nplen);
252 nlen = snprintf(keyname,
253 sizeof(keyname), "function key %d", atoi(np));
254 CHAR2INT(sp, keyname, nlen, wp, wlen);
255 MEMCPYW(name, wp, wlen);
256 CHAR2INT(sp, p, strlen(p), wp, wlen);
257 MEMCPYW(ts, wp, wlen);
258 return (seq_set(sp, name, nlen,
259 ts, strlen(p), to, tlen, stype, SEQ_NOOVERWRITE | SEQ_SCREEN));
263 * cl_optchange --
264 * Curses screen specific "option changed" routine.
266 * PUBLIC: int cl_optchange __P((SCR *, int, char *, u_long *));
269 cl_optchange(sp, opt, str, valp)
270 SCR *sp;
271 int opt;
272 char *str;
273 u_long *valp;
275 CL_PRIVATE *clp;
277 clp = CLP(sp);
279 switch (opt) {
280 case O_COLUMNS:
281 case O_LINES:
282 case O_TERM:
284 * Changing the columns, lines or terminal require that
285 * we restart the screen.
287 F_SET(sp->gp, G_SRESTART);
288 F_CLR(sp, SC_SCR_EX | SC_SCR_VI);
289 break;
290 case O_MESG:
291 (void)cl_omesg(sp, clp, *valp);
292 break;
293 case O_WINDOWNAME:
294 if (*valp) {
295 F_SET(clp, CL_RENAME_OK);
298 * If the screen is live, i.e. we're not reading the
299 * .exrc file, update the window.
301 if (sp->frp != NULL && sp->frp->name != NULL)
302 (void)cl_rename(sp, sp->frp->name, 1);
303 } else {
304 F_CLR(clp, CL_RENAME_OK);
306 (void)cl_rename(sp, NULL, 0);
308 break;
310 return (0);
314 * cl_omesg --
315 * Turn the tty write permission on or off.
317 * PUBLIC: int cl_omesg __P((SCR *, CL_PRIVATE *, int));
320 cl_omesg(sp, clp, on)
321 SCR *sp;
322 CL_PRIVATE *clp;
323 int on;
325 struct stat sb;
326 char *tty;
328 /* Find the tty, get the current permissions. */
329 if ((tty = ttyname(STDERR_FILENO)) == NULL) {
330 if (sp != NULL)
331 msgq(sp, M_SYSERR, "stderr");
332 return (1);
334 if (stat(tty, &sb) < 0) {
335 if (sp != NULL)
336 msgq(sp, M_SYSERR, "%s", tty);
337 return (1);
340 /* Save the original status if it's unknown. */
341 if (clp->tgw == TGW_UNKNOWN)
342 clp->tgw = sb.st_mode & S_IWGRP ? TGW_SET : TGW_UNSET;
344 /* Toggle the permissions. */
345 if (on) {
346 if (chmod(tty, sb.st_mode | S_IWGRP) < 0) {
347 if (sp != NULL)
348 msgq(sp, M_SYSERR,
349 "046|messages not turned on: %s", tty);
350 return (1);
352 } else
353 if (chmod(tty, sb.st_mode & ~S_IWGRP) < 0) {
354 if (sp != NULL)
355 msgq(sp, M_SYSERR,
356 "045|messages not turned off: %s", tty);
357 return (1);
359 return (0);
363 * cl_ssize --
364 * Return the terminal size.
366 * PUBLIC: int cl_ssize __P((SCR *, int, size_t *, size_t *, int *));
369 cl_ssize(sp, sigwinch, rowp, colp, changedp)
370 SCR *sp;
371 int sigwinch;
372 size_t *rowp, *colp;
373 int *changedp;
375 #ifdef TIOCGWINSZ
376 struct winsize win;
377 #endif
378 size_t col, row;
379 int rval;
380 char *p;
382 /* Assume it's changed. */
383 if (changedp != NULL)
384 *changedp = 1;
387 * !!!
388 * sp may be NULL.
390 * Get the screen rows and columns. If the values are wrong, it's
391 * not a big deal -- as soon as the user sets them explicitly the
392 * environment will be set and the screen package will use the new
393 * values.
395 * Try TIOCGWINSZ.
397 row = col = 0;
398 #ifdef TIOCGWINSZ
399 if (ioctl(STDERR_FILENO, TIOCGWINSZ, &win) != -1) {
400 row = win.ws_row;
401 col = win.ws_col;
403 #endif
404 /* If here because of suspend or a signal, only trust TIOCGWINSZ. */
405 if (sigwinch) {
407 * Somebody didn't get TIOCGWINSZ right, or has suspend
408 * without window resizing support. The user just lost,
409 * but there's nothing we can do.
411 if (row == 0 || col == 0) {
412 if (changedp != NULL)
413 *changedp = 0;
414 return (0);
418 * SunOS systems deliver SIGWINCH when windows are uncovered
419 * as well as when they change size. In addition, we call
420 * here when continuing after being suspended since the window
421 * may have changed size. Since we don't want to background
422 * all of the screens just because the window was uncovered,
423 * ignore the signal if there's no change.
425 if (sp != NULL &&
426 row == O_VAL(sp, O_LINES) && col == O_VAL(sp, O_COLUMNS)) {
427 if (changedp != NULL)
428 *changedp = 0;
429 return (0);
432 if (rowp != NULL)
433 *rowp = row;
434 if (colp != NULL)
435 *colp = col;
436 return (0);
440 * !!!
441 * If TIOCGWINSZ failed, or had entries of 0, try termcap. This
442 * routine is called before any termcap or terminal information
443 * has been set up. If there's no TERM environmental variable set,
444 * let it go, at least ex can run.
446 if (row == 0 || col == 0) {
447 if ((p = getenv("TERM")) == NULL)
448 goto noterm;
449 if (row == 0)
450 if ((rval = tigetnum("lines")) < 0)
451 msgq(sp, M_SYSERR, "tigetnum: lines");
452 else
453 row = rval;
454 if (col == 0)
455 if ((rval = tigetnum("cols")) < 0)
456 msgq(sp, M_SYSERR, "tigetnum: cols");
457 else
458 col = rval;
461 /* If nothing else, well, it's probably a VT100. */
462 noterm: if (row == 0)
463 row = 24;
464 if (col == 0)
465 col = 80;
468 * !!!
469 * POSIX 1003.2 requires the environment to override everything.
470 * Often, people can get nvi to stop messing up their screen by
471 * deleting the LINES and COLUMNS environment variables from their
472 * dot-files.
474 if ((p = getenv("LINES")) != NULL)
475 row = strtol(p, NULL, 10);
476 if ((p = getenv("COLUMNS")) != NULL)
477 col = strtol(p, NULL, 10);
479 if (rowp != NULL)
480 *rowp = row;
481 if (colp != NULL)
482 *colp = col;
483 return (0);
487 * cl_putchar --
488 * Function version of putchar, for tputs.
490 * PUBLIC: int cl_putchar __P((int));
493 cl_putchar(ch)
494 int ch;
496 return (putchar(ch));