Expand PMF_FN_* macros.
[netbsd-mini2440.git] / dist / nvi / cl / cl_term.c
blobe6e6331f5d2acf17a09d61d0b5ca822ed48f1372
1 /* $NetBSD: cl_term.c,v 1.1.1.2 2008/05/18 14:29:38 aymeric Exp $ */
3 /*-
4 * Copyright (c) 1993, 1994
5 * The Regents of the University of California. All rights reserved.
6 * Copyright (c) 1993, 1994, 1995, 1996
7 * Keith Bostic. All rights reserved.
9 * See the LICENSE file for redistribution information.
12 #include "config.h"
14 #ifndef lint
15 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";
16 #endif /* not lint */
18 #include <sys/types.h>
19 #include <sys/ioctl.h>
20 #include <sys/queue.h>
21 #include <sys/stat.h>
23 #include <bitstring.h>
24 #include <errno.h>
25 #include <limits.h>
26 #include <signal.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <termios.h>
31 #include <unistd.h>
33 #include "../common/common.h"
34 #include "cl.h"
36 static int cl_pfmap __P((SCR *, seq_t, CHAR_T *, size_t, CHAR_T *, size_t));
39 * XXX
40 * THIS REQUIRES THAT ALL SCREENS SHARE A TERMINAL TYPE.
42 typedef struct _tklist {
43 const char *ts; /* Key's termcap string. */
44 const char *output; /* Corresponding vi command. */
45 const char *name; /* Name. */
46 u_char value; /* Special value (for lookup). */
47 } TKLIST;
49 #define TKINIT(a, b, c) { a, b, c, 0 }
51 static TKLIST const c_tklist[] = { /* Command mappings. */
52 TKINIT("kil1", "O", "insert line"),
53 TKINIT("kdch1", "x", "delete character"),
54 TKINIT("kcud1", "j", "cursor down"),
55 TKINIT("kel", "D", "delete to eol"),
56 TKINIT("kind", "\004", "scroll down"), /* ^D */
57 TKINIT("kll", "$", "go to eol"),
58 TKINIT("kend", "$", "go to eol"),
59 TKINIT("khome", "^", "go to sol"),
60 TKINIT("kich1", "i", "insert at cursor"),
61 TKINIT("kdl1", "dd", "delete line"),
62 TKINIT("kcub1", "h", "cursor left"),
63 TKINIT("knp", "\006", "page down"), /* ^F */
64 TKINIT("kpp", "\002", "page up"), /* ^B */
65 TKINIT("kri", "\025", "scroll up"), /* ^U */
66 TKINIT("ked", "dG", "delete to end of screen"),
67 TKINIT("kcuf1", "l", "cursor right"),
68 TKINIT("kcuu1", "k", "cursor up"),
69 TKINIT(NULL, NULL, NULL),
71 static TKLIST const m1_tklist[] = { /* Input mappings (lookup). */
72 TKINIT(NULL, NULL, NULL),
74 static TKLIST const m2_tklist[] = { /* Input mappings (set or delete). */
75 TKINIT("kcud1", "\033ja", "cursor down"), /* ^[ja */
76 TKINIT("kcub1", "\033ha", "cursor left"), /* ^[ha */
77 TKINIT("kcuu1", "\033ka", "cursor up"), /* ^[ka */
78 TKINIT("kcuf1", "\033la", "cursor right"), /* ^[la */
79 TKINIT(NULL, NULL, NULL),
83 * cl_term_init --
84 * Initialize the special keys defined by the termcap/terminfo entry.
86 * PUBLIC: int cl_term_init __P((SCR *));
88 int
89 cl_term_init(SCR *sp)
91 KEYLIST *kp;
92 SEQ *qp;
93 TKLIST const *tkp;
94 char *t;
95 CHAR_T name[60];
96 CHAR_T output[5];
97 CHAR_T ts[20];
98 const CHAR_T *wp;
99 size_t wlen;
101 /* Command mappings. */
102 for (tkp = c_tklist; tkp->name != NULL; ++tkp) {
103 if ((t = tigetstr(tkp->ts)) == NULL || t == (char *)-1)
104 continue;
105 CHAR2INT(sp, tkp->name, strlen(tkp->name), wp, wlen);
106 MEMCPYW(name, wp, wlen);
107 CHAR2INT(sp, t, strlen(t), wp, wlen);
108 MEMCPYW(ts, wp, wlen);
109 CHAR2INT(sp, tkp->output, strlen(tkp->output), wp, wlen);
110 MEMCPYW(output, wp, wlen);
111 if (seq_set(sp, name, strlen(tkp->name), ts, strlen(t),
112 output, strlen(tkp->output), SEQ_COMMAND,
113 SEQ_NOOVERWRITE | SEQ_SCREEN))
114 return (1);
117 /* Input mappings needing to be looked up. */
118 for (tkp = m1_tklist; tkp->name != NULL; ++tkp) {
119 if ((t = tigetstr(tkp->ts)) == NULL || t == (char *)-1)
120 continue;
121 for (kp = keylist;; ++kp)
122 if (kp->value == tkp->value)
123 break;
124 if (kp == NULL)
125 continue;
126 CHAR2INT(sp, tkp->name, strlen(tkp->name), wp, wlen);
127 MEMCPYW(name, wp, wlen);
128 CHAR2INT(sp, t, strlen(t), wp, wlen);
129 MEMCPYW(ts, wp, wlen);
130 if (seq_set(sp, name, strlen(tkp->name), ts, strlen(t),
131 &kp->ch, 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 for (qp = sp->gp->seqq.lh_first; qp != NULL; qp = qp->q.le_next)
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 for (qp = gp->seqq.lh_first; qp != NULL; qp = nqp) {
195 nqp = qp->q.le_next;
196 if (F_ISSET(qp, SEQ_SCREEN))
197 (void)seq_mdel(qp);
199 return (0);
203 * cl_fmap --
204 * Map a function key.
206 * PUBLIC: int cl_fmap __P((SCR *, seq_t, CHAR_T *, size_t, CHAR_T *, size_t));
209 cl_fmap(SCR *sp, seq_t stype, CHAR_T *from, size_t flen, CHAR_T *to, size_t 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(SCR *sp, seq_t stype, CHAR_T *from, size_t flen, CHAR_T *to, size_t tlen)
227 size_t nlen;
228 char *p;
229 char name[64];
230 CHAR_T mykeyname[64];
231 CHAR_T ts[20];
232 const CHAR_T *wp;
233 size_t wlen;
235 (void)snprintf(name, sizeof(name), "kf%d",
236 (int)STRTOL(from+1,NULL,10));
237 if ((p = tigetstr(name)) == NULL ||
238 p == (char *)-1 || strlen(p) == 0)
239 p = NULL;
240 if (p == NULL) {
241 msgq_wstr(sp, M_ERR, from, "233|This terminal has no %s key");
242 return (1);
245 nlen = SPRINTF(mykeyname,
246 SIZE(mykeyname), L("function key %d"),
247 (int)STRTOL(from+1,NULL,10));
248 CHAR2INT(sp, p, strlen(p), wp, wlen);
249 MEMCPYW(ts, wp, wlen);
250 return (seq_set(sp, mykeyname, nlen,
251 ts, strlen(p), to, tlen, stype, SEQ_NOOVERWRITE | SEQ_SCREEN));
255 * cl_optchange --
256 * Curses screen specific "option changed" routine.
258 * PUBLIC: int cl_optchange __P((SCR *, int, const char *, u_long *));
261 cl_optchange(SCR *sp, int opt, const char *str, u_long *valp)
263 CL_PRIVATE *clp;
265 clp = CLP(sp);
267 switch (opt) {
268 case O_COLUMNS:
269 case O_LINES:
270 case O_TERM:
272 * Changing the columns, lines or terminal require that
273 * we restart the screen.
275 F_SET(sp->gp, G_SRESTART);
276 F_CLR(sp, SC_SCR_EX | SC_SCR_VI);
277 break;
278 case O_MESG:
279 (void)cl_omesg(sp, clp, *valp);
280 break;
281 case O_WINDOWNAME:
282 if (*valp) {
283 F_SET(clp, CL_RENAME_OK);
286 * If the screen is live, i.e. we're not reading the
287 * .exrc file, update the window.
289 if (sp->frp != NULL && sp->frp->name != NULL)
290 (void)cl_rename(sp, sp->frp->name, 1);
291 } else {
292 F_CLR(clp, CL_RENAME_OK);
294 (void)cl_rename(sp, NULL, 0);
296 break;
298 return (0);
302 * cl_omesg --
303 * Turn the tty write permission on or off.
305 * PUBLIC: int cl_omesg __P((SCR *, CL_PRIVATE *, int));
308 cl_omesg(SCR *sp, CL_PRIVATE *clp, int on)
310 struct stat sb;
311 char *tty;
313 /* Find the tty, get the current permissions. */
314 if ((tty = ttyname(STDERR_FILENO)) == NULL) {
315 if (sp != NULL)
316 msgq(sp, M_SYSERR, "stderr");
317 return (1);
319 if (stat(tty, &sb) < 0) {
320 if (sp != NULL)
321 msgq(sp, M_SYSERR, "%s", tty);
322 return (1);
325 /* Save the original status if it's unknown. */
326 if (clp->tgw == TGW_UNKNOWN)
327 clp->tgw = sb.st_mode & S_IWGRP ? TGW_SET : TGW_UNSET;
329 /* Toggle the permissions. */
330 if (on) {
331 if (chmod(tty, sb.st_mode | S_IWGRP) < 0) {
332 if (sp != NULL)
333 msgq(sp, M_SYSERR,
334 "046|messages not turned on: %s", tty);
335 return (1);
337 } else
338 if (chmod(tty, sb.st_mode & ~S_IWGRP) < 0) {
339 if (sp != NULL)
340 msgq(sp, M_SYSERR,
341 "045|messages not turned off: %s", tty);
342 return (1);
344 return (0);
348 * cl_ssize --
349 * Return the terminal size.
351 * PUBLIC: int cl_ssize __P((SCR *, int, size_t *, size_t *, int *));
354 cl_ssize(SCR *sp, int sigwinch, size_t *rowp, size_t *colp, int *changedp)
356 #ifdef TIOCGWINSZ
357 struct winsize win;
358 #endif
359 size_t col, row;
360 int rval;
361 char *p;
363 /* Assume it's changed. */
364 if (changedp != NULL)
365 *changedp = 1;
368 * !!!
369 * sp may be NULL.
371 * Get the screen rows and columns. If the values are wrong, it's
372 * not a big deal -- as soon as the user sets them explicitly the
373 * environment will be set and the screen package will use the new
374 * values.
376 * Try TIOCGWINSZ.
378 row = col = 0;
379 #ifdef TIOCGWINSZ
380 if (ioctl(STDERR_FILENO, TIOCGWINSZ, &win) != -1) {
381 row = win.ws_row;
382 col = win.ws_col;
384 #endif
385 /* If here because of suspend or a signal, only trust TIOCGWINSZ. */
386 if (sigwinch) {
388 * Somebody didn't get TIOCGWINSZ right, or has suspend
389 * without window resizing support. The user just lost,
390 * but there's nothing we can do.
392 if (row == 0 || col == 0) {
393 if (changedp != NULL)
394 *changedp = 0;
395 return (0);
399 * SunOS systems deliver SIGWINCH when windows are uncovered
400 * as well as when they change size. In addition, we call
401 * here when continuing after being suspended since the window
402 * may have changed size. Since we don't want to background
403 * all of the screens just because the window was uncovered,
404 * ignore the signal if there's no change.
406 if (sp != NULL &&
407 row == O_VAL(sp, O_LINES) && col == O_VAL(sp, O_COLUMNS)) {
408 if (changedp != NULL)
409 *changedp = 0;
410 return (0);
413 if (rowp != NULL)
414 *rowp = row;
415 if (colp != NULL)
416 *colp = col;
417 return (0);
421 * !!!
422 * If TIOCGWINSZ failed, or had entries of 0, try termcap. This
423 * routine is called before any termcap or terminal information
424 * has been set up. If there's no TERM environmental variable set,
425 * let it go, at least ex can run.
427 if (row == 0 || col == 0) {
428 if ((p = getenv("TERM")) == NULL)
429 goto noterm;
430 if (row == 0) {
431 if ((rval = tigetnum("lines")) < 0)
432 msgq(sp, M_SYSERR, "tigetnum: lines");
433 else
434 row = rval;
436 if (col == 0) {
437 if ((rval = tigetnum("cols")) < 0)
438 msgq(sp, M_SYSERR, "tigetnum: cols");
439 else
440 col = rval;
444 /* If nothing else, well, it's probably a VT100. */
445 noterm: if (row == 0)
446 row = 24;
447 if (col == 0)
448 col = 80;
451 * !!!
452 * POSIX 1003.2 requires the environment to override everything.
453 * Often, people can get nvi to stop messing up their screen by
454 * deleting the LINES and COLUMNS environment variables from their
455 * dot-files.
457 if ((p = getenv("LINES")) != NULL)
458 row = strtol(p, NULL, 10);
459 if ((p = getenv("COLUMNS")) != NULL)
460 col = strtol(p, NULL, 10);
462 if (rowp != NULL)
463 *rowp = row;
464 if (colp != NULL)
465 *colp = col;
466 return (0);
470 * cl_putchar --
471 * Function version of putchar, for tputs.
473 * PUBLIC: int cl_putchar __P((int));
476 cl_putchar(int ch)
478 return (putchar(ch));