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