tools/llvm: Do not build with symbols
[minix3.git] / external / bsd / nvi / dist / cl / cl_term.c
blob1a0fba9cccb5c4ab129c993ac4175c25c7e3dda0
1 /* $NetBSD: cl_term.c,v 1.3 2013/11/25 22:43:46 christos Exp $ */
2 /*-
3 * Copyright (c) 1993, 1994
4 * The Regents of the University of California. All rights reserved.
5 * Copyright (c) 1993, 1994, 1995, 1996
6 * Keith Bostic. All rights reserved.
8 * See the LICENSE file for redistribution information.
9 */
11 #include "config.h"
13 #ifndef lint
14 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 ";
15 #endif /* not lint */
17 #include <sys/types.h>
18 #include <sys/ioctl.h>
19 #include <sys/queue.h>
20 #include <sys/stat.h>
22 #include <bitstring.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 const char *ts; /* Key's termcap string. */
43 const char *output; /* Corresponding vi command. */
44 const char *name; /* Name. */
45 u_char value; /* Special value (for lookup). */
46 } TKLIST;
48 #define TKINIT(a, b, c) { a, b, c, 0 }
50 static TKLIST const c_tklist[] = { /* Command mappings. */
51 TKINIT("kil1", "O", "insert line"),
52 TKINIT("kdch1", "x", "delete character"),
53 TKINIT("kcud1", "j", "cursor down"),
54 TKINIT("kel", "D", "delete to eol"),
55 TKINIT("kind", "\004", "scroll down"), /* ^D */
56 TKINIT("kll", "$", "go to eol"),
57 TKINIT("kend", "$", "go to eol"),
58 TKINIT("khome", "^", "go to sol"),
59 TKINIT("kich1", "i", "insert at cursor"),
60 TKINIT("kdl1", "dd", "delete line"),
61 TKINIT("kcub1", "h", "cursor left"),
62 TKINIT("knp", "\006", "page down"), /* ^F */
63 TKINIT("kpp", "\002", "page up"), /* ^B */
64 TKINIT("kri", "\025", "scroll up"), /* ^U */
65 TKINIT("ked", "dG", "delete to end of screen"),
66 TKINIT("kcuf1", "l", "cursor right"),
67 TKINIT("kcuu1", "k", "cursor up"),
68 TKINIT(NULL, NULL, NULL),
70 static TKLIST const m1_tklist[] = { /* Input mappings (lookup). */
71 TKINIT(NULL, NULL, NULL),
73 static TKLIST const m2_tklist[] = { /* Input mappings (set or delete). */
74 TKINIT("kcud1", "\033ja", "cursor down"), /* ^[ja */
75 TKINIT("kcub1", "\033ha", "cursor left"), /* ^[ha */
76 TKINIT("kcuu1", "\033ka", "cursor up"), /* ^[ka */
77 TKINIT("kcuf1", "\033la", "cursor right"), /* ^[la */
78 TKINIT(NULL, NULL, NULL),
82 * cl_term_init --
83 * Initialize the special keys defined by the termcap/terminfo entry.
85 * PUBLIC: int cl_term_init __P((SCR *));
87 int
88 cl_term_init(SCR *sp)
90 KEYLIST *kp;
91 SEQ *qp;
92 TKLIST const *tkp;
93 char *t;
94 CHAR_T name[60];
95 CHAR_T output[5];
96 CHAR_T ts[20];
97 const CHAR_T *wp;
98 size_t wlen;
100 /* Command mappings. */
101 for (tkp = c_tklist; tkp->name != NULL; ++tkp) {
102 if ((t = tigetstr(tkp->ts)) == NULL || t == (char *)-1)
103 continue;
104 CHAR2INT(sp, tkp->name, strlen(tkp->name), wp, wlen);
105 MEMCPYW(name, wp, wlen);
106 CHAR2INT(sp, t, strlen(t), wp, wlen);
107 MEMCPYW(ts, wp, wlen);
108 CHAR2INT(sp, tkp->output, strlen(tkp->output), wp, wlen);
109 MEMCPYW(output, wp, wlen);
110 if (seq_set(sp, name, strlen(tkp->name), ts, strlen(t),
111 output, strlen(tkp->output), SEQ_COMMAND,
112 SEQ_NOOVERWRITE | SEQ_SCREEN))
113 return (1);
116 /* Input mappings needing to be looked up. */
117 for (tkp = m1_tklist; tkp->name != NULL; ++tkp) {
118 if ((t = tigetstr(tkp->ts)) == NULL || t == (char *)-1)
119 continue;
120 for (kp = keylist;; ++kp)
121 if (kp->value == tkp->value)
122 break;
123 if (kp == NULL)
124 continue;
125 CHAR2INT(sp, tkp->name, strlen(tkp->name), wp, wlen);
126 MEMCPYW(name, wp, wlen);
127 CHAR2INT(sp, t, strlen(t), wp, wlen);
128 MEMCPYW(ts, wp, wlen);
129 output[0] = (UCHAR_T)kp->ch;
130 if (seq_set(sp, name, strlen(tkp->name), ts, strlen(t),
131 output, 1, SEQ_INPUT, SEQ_NOOVERWRITE | SEQ_SCREEN))
132 return (1);
135 /* Input mappings that are already set or are text deletions. */
136 for (tkp = m2_tklist; tkp->name != NULL; ++tkp) {
137 if ((t = tigetstr(tkp->ts)) == NULL || t == (char *)-1)
138 continue;
140 * !!!
141 * Some terminals' <cursor_left> keys send single <backspace>
142 * characters. This is okay in command mapping, but not okay
143 * in input mapping. That combination is the only one we'll
144 * ever see, hopefully, so kluge it here for now.
146 if (!strcmp(t, "\b"))
147 continue;
148 if (tkp->output == NULL) {
149 CHAR2INT(sp, tkp->name, strlen(tkp->name), wp, wlen);
150 MEMCPYW(name, wp, wlen);
151 CHAR2INT(sp, t, strlen(t), wp, wlen);
152 MEMCPYW(ts, wp, wlen);
153 if (seq_set(sp, name, strlen(tkp->name),
154 ts, strlen(t), NULL, 0,
155 SEQ_INPUT, SEQ_NOOVERWRITE | SEQ_SCREEN))
156 return (1);
157 } else {
158 CHAR2INT(sp, tkp->name, strlen(tkp->name), wp, wlen);
159 MEMCPYW(name, wp, wlen);
160 CHAR2INT(sp, t, strlen(t), wp, wlen);
161 MEMCPYW(ts, wp, wlen);
162 CHAR2INT(sp, tkp->output, strlen(tkp->output), wp, wlen);
163 MEMCPYW(output, wp, wlen);
164 if (seq_set(sp, name, strlen(tkp->name),
165 ts, strlen(t), output, strlen(tkp->output),
166 SEQ_INPUT, SEQ_NOOVERWRITE | SEQ_SCREEN))
167 return (1);
172 * Rework any function key mappings that were set before the
173 * screen was initialized.
175 LIST_FOREACH(qp, &sp->gp->seqq, q)
176 if (F_ISSET(qp, SEQ_FUNCMAP))
177 (void)cl_pfmap(sp, qp->stype,
178 qp->input, qp->ilen, qp->output, qp->olen);
179 return (0);
183 * cl_term_end --
184 * End the special keys defined by the termcap/terminfo entry.
186 * PUBLIC: int cl_term_end __P((GS *));
189 cl_term_end(GS *gp)
191 SEQ *qp, *nqp;
193 /* Delete screen specific mappings. */
194 LIST_FOREACH_SAFE(qp, &gp->seqq, q, nqp)
195 if (F_ISSET(qp, SEQ_SCREEN))
196 (void)seq_mdel(qp);
197 return (0);
201 * cl_fmap --
202 * Map a function key.
204 * PUBLIC: int cl_fmap __P((SCR *, seq_t, CHAR_T *, size_t, CHAR_T *, size_t));
207 cl_fmap(SCR *sp, seq_t stype, CHAR_T *from, size_t flen, CHAR_T *to, size_t tlen)
209 /* Ignore until the screen is running, do the real work then. */
210 if (F_ISSET(sp, SC_VI) && !F_ISSET(sp, SC_SCR_VI))
211 return (0);
212 if (F_ISSET(sp, SC_EX) && !F_ISSET(sp, SC_SCR_EX))
213 return (0);
215 return (cl_pfmap(sp, stype, from, flen, to, tlen));
219 * cl_pfmap --
220 * Map a function key (private version).
222 static int
223 cl_pfmap(SCR *sp, seq_t stype, CHAR_T *from, size_t flen, CHAR_T *to, size_t tlen)
225 size_t nlen;
226 char *p;
227 char name[64];
228 CHAR_T mykeyname[64];
229 CHAR_T ts[20];
230 const CHAR_T *wp;
231 size_t wlen;
233 (void)snprintf(name, sizeof(name), "kf%d",
234 (int)STRTOL(from+1,NULL,10));
235 if ((p = tigetstr(name)) == NULL ||
236 p == (char *)-1 || strlen(p) == 0)
237 p = NULL;
238 if (p == NULL) {
239 msgq_wstr(sp, M_ERR, from, "233|This terminal has no %s key");
240 return (1);
243 nlen = SPRINTF(mykeyname,
244 SIZE(mykeyname), L("function key %d"),
245 (int)STRTOL(from+1,NULL,10));
246 CHAR2INT(sp, p, strlen(p), wp, wlen);
247 MEMCPYW(ts, wp, wlen);
248 return (seq_set(sp, mykeyname, nlen,
249 ts, strlen(p), to, tlen, stype, SEQ_NOOVERWRITE | SEQ_SCREEN));
253 * cl_optchange --
254 * Curses screen specific "option changed" routine.
256 * PUBLIC: int cl_optchange __P((SCR *, int, const char *, u_long *));
259 cl_optchange(SCR *sp, int opt, const char *str, u_long *valp)
261 CL_PRIVATE *clp;
263 clp = CLP(sp);
265 switch (opt) {
266 case O_COLUMNS:
267 case O_LINES:
268 case O_TERM:
270 * Changing the columns, lines or terminal require that
271 * we restart the screen.
273 F_SET(sp->gp, G_SRESTART);
274 F_CLR(sp, SC_SCR_EX | SC_SCR_VI);
275 break;
276 case O_MESG:
277 (void)cl_omesg(sp, clp, *valp);
278 break;
279 case O_WINDOWNAME:
280 if (*valp) {
281 F_SET(clp, CL_RENAME_OK);
284 * If the screen is live, i.e. we're not reading the
285 * .exrc file, update the window.
287 if (sp->frp != NULL && sp->frp->name != NULL)
288 (void)cl_rename(sp, sp->frp->name, 1);
289 } else {
290 F_CLR(clp, CL_RENAME_OK);
292 (void)cl_rename(sp, NULL, 0);
294 break;
296 return (0);
300 * cl_omesg --
301 * Turn the tty write permission on or off.
303 * PUBLIC: int cl_omesg __P((SCR *, CL_PRIVATE *, int));
306 cl_omesg(SCR *sp, CL_PRIVATE *clp, int on)
308 struct stat sb;
309 char *tty;
311 /* Find the tty, get the current permissions. */
312 if ((tty = ttyname(STDERR_FILENO)) == NULL) {
313 if (sp != NULL)
314 msgq(sp, M_SYSERR, "stderr");
315 return (1);
317 if (stat(tty, &sb) < 0) {
318 if (sp != NULL)
319 msgq(sp, M_SYSERR, "%s", tty);
320 return (1);
323 /* Save the original status if it's unknown. */
324 if (clp->tgw == TGW_UNKNOWN)
325 clp->tgw = sb.st_mode & S_IWGRP ? TGW_SET : TGW_UNSET;
327 /* Toggle the permissions. */
328 if (on) {
329 if (chmod(tty, sb.st_mode | S_IWGRP) < 0) {
330 if (sp != NULL)
331 msgq(sp, M_SYSERR,
332 "046|messages not turned on: %s", tty);
333 return (1);
335 } else
336 if (chmod(tty, sb.st_mode & ~S_IWGRP) < 0) {
337 if (sp != NULL)
338 msgq(sp, M_SYSERR,
339 "045|messages not turned off: %s", tty);
340 return (1);
342 return (0);
346 * cl_ssize --
347 * Return the terminal size.
349 * PUBLIC: int cl_ssize __P((SCR *, int, size_t *, size_t *, int *));
352 cl_ssize(SCR *sp, int sigwinch, size_t *rowp, size_t *colp, int *changedp)
354 #ifdef TIOCGWINSZ
355 struct winsize win;
356 #endif
357 size_t col, row;
358 int rval;
359 char *p;
361 /* Assume it's changed. */
362 if (changedp != NULL)
363 *changedp = 1;
366 * !!!
367 * sp may be NULL.
369 * Get the screen rows and columns. If the values are wrong, it's
370 * not a big deal -- as soon as the user sets them explicitly the
371 * environment will be set and the screen package will use the new
372 * values.
374 * Try TIOCGWINSZ.
376 row = col = 0;
377 #ifdef TIOCGWINSZ
378 if (ioctl(STDERR_FILENO, TIOCGWINSZ, &win) != -1) {
379 row = win.ws_row;
380 col = win.ws_col;
382 #endif
383 /* If here because of suspend or a signal, only trust TIOCGWINSZ. */
384 if (sigwinch) {
386 * Somebody didn't get TIOCGWINSZ right, or has suspend
387 * without window resizing support. The user just lost,
388 * but there's nothing we can do.
390 if (row == 0 || col == 0) {
391 if (changedp != NULL)
392 *changedp = 0;
393 return (0);
397 * SunOS systems deliver SIGWINCH when windows are uncovered
398 * as well as when they change size. In addition, we call
399 * here when continuing after being suspended since the window
400 * may have changed size. Since we don't want to background
401 * all of the screens just because the window was uncovered,
402 * ignore the signal if there's no change.
404 if (sp != NULL &&
405 row == O_VAL(sp, O_LINES) && col == O_VAL(sp, O_COLUMNS)) {
406 if (changedp != NULL)
407 *changedp = 0;
408 return (0);
411 if (rowp != NULL)
412 *rowp = row;
413 if (colp != NULL)
414 *colp = col;
415 resizeterm(row, col);
416 return (0);
420 * !!!
421 * If TIOCGWINSZ failed, or had entries of 0, try termcap. This
422 * routine is called before any termcap or terminal information
423 * has been set up. If there's no TERM environmental variable set,
424 * let it go, at least ex can run.
426 if (row == 0 || col == 0) {
427 if ((p = getenv("TERM")) == NULL)
428 goto noterm;
429 if (row == 0) {
430 if ((rval = tigetnum("lines")) < 0)
431 msgq(sp, M_SYSERR, "tigetnum: lines");
432 else
433 row = rval;
435 if (col == 0) {
436 if ((rval = tigetnum("cols")) < 0)
437 msgq(sp, M_SYSERR, "tigetnum: cols");
438 else
439 col = rval;
443 /* If nothing else, well, it's probably a VT100. */
444 noterm: if (row == 0)
445 row = 24;
446 if (col == 0)
447 col = 80;
450 * !!!
451 * POSIX 1003.2 requires the environment to override everything.
452 * Often, people can get nvi to stop messing up their screen by
453 * deleting the LINES and COLUMNS environment variables from their
454 * dot-files.
456 if ((p = getenv("LINES")) != NULL)
457 row = strtol(p, NULL, 10);
458 if ((p = getenv("COLUMNS")) != NULL)
459 col = strtol(p, NULL, 10);
461 if (rowp != NULL)
462 *rowp = row;
463 if (colp != NULL)
464 *colp = col;
465 return (0);
469 * cl_putchar --
470 * Function version of putchar, for tputs.
472 * PUBLIC: int cl_putchar __P((int));
475 cl_putchar(int ch)
477 return (putchar(ch));