1 /* $OpenBSD: cl_term.c,v 1.16 2009/10/27 23:59:47 deraadt Exp $ */
2
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.
8 *
9 * See the LICENSE file for redistribution information.
10 */
11
12 #include "config.h"
13
14 #include <sys/types.h>
15 #include <sys/ioctl.h>
16 #include <sys/queue.h>
17 #include <sys/stat.h>
18
19 #include <bitstring.h>
20 #include <curses.h>
21 #include <errno.h>
22 #include <limits.h>
23 #include <signal.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <termios.h>
28 #include <unistd.h>
29
30 #include "../common/common.h"
31 #include "cl.h"
32
33 static int cl_pfmap(SCR *, seq_t, CHAR_T *, size_t, CHAR_T *, size_t);
34
35 /*
36 * XXX
37 * THIS REQUIRES THAT ALL SCREENS SHARE A TERMINAL TYPE.
38 */
39 typedef struct _tklist {
40 char *ts; /* Key's termcap string. */
41 char *output; /* Corresponding vi command. */
42 char *name; /* Name. */
43 u_char value; /* Special value (for lookup). */
44 } TKLIST;
45 static TKLIST const c_tklist[] = { /* Command mappings. */
46 {"kil1", "O", "insert line"},
47 {"kdch1", "x", "delete character"},
48 {"kcud1", "j", "cursor down"},
49 {"kel", "D", "delete to eol"},
50 {"kind", "\004", "scroll down"}, /* ^D */
51 {"kll", "$", "go to eol"},
52 {"khome", "^", "go to sol"},
53 {"kich1", "i", "insert at cursor"},
54 {"kdl1", "dd", "delete line"},
55 {"kcub1", "h", "cursor left"},
56 {"knp", "\006", "page down"}, /* ^F */
57 {"kpp", "\002", "page up"}, /* ^B */
58 {"kri", "\025", "scroll up"}, /* ^U */
59 {"ked", "dG", "delete to end of screen"},
60 {"kcuf1", "l", "cursor right"},
61 {"kcuu1", "k", "cursor up"},
62 {NULL},
63 };
64 static TKLIST const m1_tklist[] = { /* Input mappings (lookup). */
65 {NULL},
66 };
67 static TKLIST const m2_tklist[] = { /* Input mappings (set or delete). */
68 {"kcud1", "\033ja", "cursor down"}, /* ^[ja */
69 {"kcub1", "\033ha", "cursor left"}, /* ^[ha */
70 {"kcuu1", "\033ka", "cursor up"}, /* ^[ka */
71 {"kcuf1", "\033la", "cursor right"}, /* ^[la */
72 {NULL},
73 };
74
75 /*
76 * cl_term_init --
77 * Initialize the special keys defined by the termcap/terminfo entry.
78 *
79 * PUBLIC: int cl_term_init(SCR *);
80 */
81 int
cl_term_init(sp)82 cl_term_init(sp)
83 SCR *sp;
84 {
85 KEYLIST *kp;
86 SEQ *qp;
87 TKLIST const *tkp;
88 char *t;
89
90 /* Command mappings. */
91 for (tkp = c_tklist; tkp->name != NULL; ++tkp) {
92 if ((t = tigetstr(tkp->ts)) == NULL || t == (char *)-1)
93 continue;
94 if (seq_set(sp, tkp->name, strlen(tkp->name), t, strlen(t),
95 tkp->output, strlen(tkp->output), SEQ_COMMAND,
96 SEQ_NOOVERWRITE | SEQ_SCREEN))
97 return (1);
98 }
99
100 /* Input mappings needing to be looked up. */
101 for (tkp = m1_tklist; tkp->name != NULL; ++tkp) {
102 if ((t = tigetstr(tkp->ts)) == NULL || t == (char *)-1)
103 continue;
104 for (kp = keylist;; ++kp)
105 if (kp->value == tkp->value)
106 break;
107 if (kp == NULL)
108 continue;
109 if (seq_set(sp, tkp->name, strlen(tkp->name), t, strlen(t),
110 &kp->ch, 1, SEQ_INPUT, SEQ_NOOVERWRITE | SEQ_SCREEN))
111 return (1);
112 }
113
114 /* Input mappings that are already set or are text deletions. */
115 for (tkp = m2_tklist; tkp->name != NULL; ++tkp) {
116 if ((t = tigetstr(tkp->ts)) == NULL || t == (char *)-1)
117 continue;
118 /*
119 * !!!
120 * Some terminals' <cursor_left> keys send single <backspace>
121 * characters. This is okay in command mapping, but not okay
122 * in input mapping. That combination is the only one we'll
123 * ever see, hopefully, so kluge it here for now.
124 */
125 if (!strcmp(t, "\b"))
126 continue;
127 if (tkp->output == NULL) {
128 if (seq_set(sp, tkp->name, strlen(tkp->name),
129 t, strlen(t), NULL, 0,
130 SEQ_INPUT, SEQ_NOOVERWRITE | SEQ_SCREEN))
131 return (1);
132 } else
133 if (seq_set(sp, tkp->name, strlen(tkp->name),
134 t, strlen(t), tkp->output, strlen(tkp->output),
135 SEQ_INPUT, SEQ_NOOVERWRITE | SEQ_SCREEN))
136 return (1);
137 }
138
139 /*
140 * Rework any function key mappings that were set before the
141 * screen was initialized.
142 */
143 LIST_FOREACH(qp, & sp->gp->seqq, q)
144 if (F_ISSET(qp, SEQ_FUNCMAP))
145 (void)cl_pfmap(sp, qp->stype,
146 qp->input, qp->ilen, qp->output, qp->olen);
147 return (0);
148 }
149
150 /*
151 * cl_term_end --
152 * End the special keys defined by the termcap/terminfo entry.
153 *
154 * PUBLIC: int cl_term_end(GS *);
155 */
156 int
cl_term_end(gp)157 cl_term_end(gp)
158 GS *gp;
159 {
160 SEQ *qp, *nqp;
161
162 /* Delete screen specific mappings. */
163 for (qp = LIST_FIRST(&gp->seqq); qp != NULL; qp = nqp) {
164 nqp = LIST_NEXT(qp, q);
165 if (F_ISSET(qp, SEQ_SCREEN))
166 (void)seq_mdel(qp);
167 }
168 return (0);
169 }
170
171 /*
172 * cl_fmap --
173 * Map a function key.
174 *
175 * PUBLIC: int cl_fmap(SCR *, seq_t, CHAR_T *, size_t, CHAR_T *, size_t);
176 */
177 int
cl_fmap(sp,stype,from,flen,to,tlen)178 cl_fmap(sp, stype, from, flen, to, tlen)
179 SCR *sp;
180 seq_t stype;
181 CHAR_T *from, *to;
182 size_t flen, tlen;
183 {
184 /* Ignore until the screen is running, do the real work then. */
185 if (F_ISSET(sp, SC_VI) && !F_ISSET(sp, SC_SCR_VI))
186 return (0);
187 if (F_ISSET(sp, SC_EX) && !F_ISSET(sp, SC_SCR_EX))
188 return (0);
189
190 return (cl_pfmap(sp, stype, from, flen, to, tlen));
191 }
192
193 /*
194 * cl_pfmap --
195 * Map a function key (private version).
196 */
197 static int
cl_pfmap(sp,stype,from,flen,to,tlen)198 cl_pfmap(sp, stype, from, flen, to, tlen)
199 SCR *sp;
200 seq_t stype;
201 CHAR_T *from, *to;
202 size_t flen, tlen;
203 {
204 size_t nlen;
205 char *p, key_name[64];
206
207 (void)snprintf(key_name, sizeof(key_name), "kf%d", atoi(from + 1));
208 if ((p = tigetstr(key_name)) == NULL ||
209 p == (char *)-1 || strlen(p) == 0)
210 p = NULL;
211 if (p == NULL) {
212 msgq_str(sp, M_ERR, from, "233|This terminal has no %s key");
213 return (1);
214 }
215
216 nlen = snprintf(key_name,
217 sizeof(key_name), "function key %d", atoi(from + 1));
218 if (nlen >= sizeof(key_name))
219 nlen = sizeof(key_name) - 1;
220 return (seq_set(sp, key_name, nlen,
221 p, strlen(p), to, tlen, stype, SEQ_NOOVERWRITE | SEQ_SCREEN));
222 }
223
224 /*
225 * cl_optchange --
226 * Curses screen specific "option changed" routine.
227 *
228 * PUBLIC: int cl_optchange(SCR *, int, char *, u_long *);
229 */
230 int
cl_optchange(sp,opt,str,valp)231 cl_optchange(sp, opt, str, valp)
232 SCR *sp;
233 int opt;
234 char *str;
235 u_long *valp;
236 {
237 CL_PRIVATE *clp;
238
239 clp = CLP(sp);
240
241 switch (opt) {
242 case O_COLUMNS:
243 case O_LINES:
244 case O_TERM:
245 /*
246 * Changing the columns, lines or terminal require that
247 * we restart the screen.
248 */
249 F_SET(sp->gp, G_SRESTART);
250 F_CLR(sp, SC_SCR_EX | SC_SCR_VI);
251 break;
252 case O_MESG:
253 (void)cl_omesg(sp, clp, !*valp);
254 break;
255 case O_WINDOWNAME:
256 if (*valp) {
257 F_CLR(clp, CL_RENAME_OK);
258
259 (void)cl_rename(sp, NULL, 0);
260 } else {
261 F_SET(clp, CL_RENAME_OK);
262
263 /*
264 * If the screen is live, i.e. we're not reading the
265 * .exrc file, update the window.
266 */
267 if (sp->frp != NULL && sp->frp->name != NULL)
268 (void)cl_rename(sp, sp->frp->name, 1);
269 }
270 break;
271 }
272 return (0);
273 }
274
275 /*
276 * cl_omesg --
277 * Turn the tty write permission on or off.
278 *
279 * PUBLIC: int cl_omesg(SCR *, CL_PRIVATE *, int);
280 */
281 int
cl_omesg(sp,clp,on)282 cl_omesg(sp, clp, on)
283 SCR *sp;
284 CL_PRIVATE *clp;
285 int on;
286 {
287 struct stat sb;
288 char *tty;
289
290 /* Find the tty, get the current permissions. */
291 if ((tty = ttyname(STDERR_FILENO)) == NULL) {
292 if (sp != NULL)
293 msgq(sp, M_SYSERR, "stderr");
294 return (1);
295 }
296 if (stat(tty, &sb) < 0) {
297 if (sp != NULL)
298 msgq(sp, M_SYSERR, "%s", tty);
299 return (1);
300 }
301
302 /* Save the original status if it's unknown. */
303 if (clp->tgw == TGW_UNKNOWN)
304 clp->tgw = sb.st_mode & S_IWGRP ? TGW_SET : TGW_UNSET;
305
306 /* Toggle the permissions. */
307 if (on) {
308 if (chmod(tty, sb.st_mode | S_IWGRP) < 0) {
309 if (sp != NULL)
310 msgq(sp, M_SYSERR,
311 "046|messages not turned on: %s", tty);
312 return (1);
313 }
314 } else
315 if (chmod(tty, sb.st_mode & ~S_IWGRP) < 0) {
316 if (sp != NULL)
317 msgq(sp, M_SYSERR,
318 "045|messages not turned off: %s", tty);
319 return (1);
320 }
321 return (0);
322 }
323
324 /*
325 * cl_ssize --
326 * Return the terminal size.
327 *
328 * PUBLIC: int cl_ssize(SCR *, int, size_t *, size_t *, int *);
329 */
330 int
cl_ssize(sp,sigwinch,rowp,colp,changedp)331 cl_ssize(sp, sigwinch, rowp, colp, changedp)
332 SCR *sp;
333 int sigwinch;
334 size_t *rowp, *colp;
335 int *changedp;
336 {
337 #ifdef TIOCGWINSZ
338 struct winsize win;
339 #endif
340 size_t col, row;
341 int rval;
342 char *p;
343
344 /* Assume it's changed. */
345 if (changedp != NULL)
346 *changedp = 1;
347
348 /*
349 * !!!
350 * sp may be NULL.
351 *
352 * Get the screen rows and columns. If the values are wrong, it's
353 * not a big deal -- as soon as the user sets them explicitly the
354 * environment will be set and the screen package will use the new
355 * values.
356 *
357 * Try TIOCGWINSZ.
358 */
359 row = col = 0;
360 #ifdef TIOCGWINSZ
361 if (ioctl(STDERR_FILENO, TIOCGWINSZ, &win) != -1) {
362 row = win.ws_row;
363 col = win.ws_col;
364 }
365 #endif
366 /* If here because of suspend or a signal, only trust TIOCGWINSZ. */
367 if (sigwinch) {
368 /*
369 * Somebody didn't get TIOCGWINSZ right, or has suspend
370 * without window resizing support. The user just lost,
371 * but there's nothing we can do.
372 */
373 if (row == 0 || col == 0) {
374 if (changedp != NULL)
375 *changedp = 0;
376 return (0);
377 }
378
379 /*
380 * SunOS systems deliver SIGWINCH when windows are uncovered
381 * as well as when they change size. In addition, we call
382 * here when continuing after being suspended since the window
383 * may have changed size. Since we don't want to background
384 * all of the screens just because the window was uncovered,
385 * ignore the signal if there's no change.
386 */
387 if (sp != NULL &&
388 row == O_VAL(sp, O_LINES) && col == O_VAL(sp, O_COLUMNS)) {
389 if (changedp != NULL)
390 *changedp = 0;
391 return (0);
392 }
393
394 if (rowp != NULL)
395 *rowp = row;
396 if (colp != NULL)
397 *colp = col;
398 return (0);
399 }
400
401 /*
402 * !!!
403 * If TIOCGWINSZ failed, or had entries of 0, try termcap. This
404 * routine is called before any termcap or terminal information
405 * has been set up. If there's no TERM environmental variable set,
406 * let it go, at least ex can run.
407 */
408 if (row == 0 || col == 0) {
409 if ((p = getenv("TERM")) == NULL)
410 goto noterm;
411 if (row == 0) {
412 if ((rval = tigetnum("lines")) < 0)
413 msgq(sp, M_SYSERR, "tigetnum: lines");
414 else
415 row = rval;
416 }
417 if (col == 0) {
418 if ((rval = tigetnum("cols")) < 0)
419 msgq(sp, M_SYSERR, "tigetnum: cols");
420 else
421 col = rval;
422 }
423 }
424
425 /* If nothing else, well, it's probably a VT100. */
426 noterm: if (row == 0)
427 row = 24;
428 if (col == 0)
429 col = 80;
430
431 /*
432 * !!!
433 * POSIX 1003.2 requires the environment to override everything.
434 * Often, people can get nvi to stop messing up their screen by
435 * deleting the LINES and COLUMNS environment variables from their
436 * dot-files.
437 */
438 if ((p = getenv("LINES")) != NULL)
439 row = strtol(p, NULL, 10);
440 if ((p = getenv("COLUMNS")) != NULL)
441 col = strtol(p, NULL, 10);
442
443 if (rowp != NULL)
444 *rowp = row;
445 if (colp != NULL)
446 *colp = col;
447 return (0);
448 }
449
450 #ifdef _USE_OLD_CURSES_
451 /*
452 * cl_putchar --
453 * Function version of putchar, for tputs.
454 *
455 * PUBLIC: int cl_putchar(int);
456 */
457 void
cl_putchar(ch)458 cl_putchar(ch)
459 int ch;
460 {
461 (void)putchar(ch);
462 }
463 #else
464 /*
465 * cl_putchar --
466 * Function version of putchar, for tputs.
467 *
468 * PUBLIC: int cl_putchar(int);
469 */
470 int
cl_putchar(ch)471 cl_putchar(ch)
472 int ch;
473 {
474 return (putchar(ch));
475 }
476 #endif
477