xref: /minix/external/bsd/tmux/dist/tty.c (revision 0a6a1f1d)
1 /* Id */
2 
3 /*
4  * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15  * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 #include <sys/types.h>
20 #include <sys/ioctl.h>
21 
22 #include <netinet/in.h>
23 
24 #include <errno.h>
25 #include <fcntl.h>
26 #include <resolv.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <termios.h>
30 #include <unistd.h>
31 
32 #include "tmux.h"
33 
34 void	tty_read_callback(struct bufferevent *, void *);
35 void	tty_error_callback(struct bufferevent *, short, void *);
36 
37 int	tty_try_256(struct tty *, u_char, const char *);
38 
39 void	tty_colours(struct tty *, const struct grid_cell *);
40 void	tty_check_fg(struct tty *, struct grid_cell *);
41 void	tty_check_bg(struct tty *, struct grid_cell *);
42 void	tty_colours_fg(struct tty *, const struct grid_cell *);
43 void	tty_colours_bg(struct tty *, const struct grid_cell *);
44 
45 int	tty_large_region(struct tty *, const struct tty_ctx *);
46 void	tty_redraw_region(struct tty *, const struct tty_ctx *);
47 void	tty_emulate_repeat(
48 	    struct tty *, enum tty_code_code, enum tty_code_code, u_int);
49 void	tty_repeat_space(struct tty *, u_int);
50 void	tty_cell(struct tty *, const struct grid_cell *);
51 
52 #define tty_use_acs(tty) \
53 	(tty_term_has((tty)->term, TTYC_ACSC) && !((tty)->flags & TTY_UTF8))
54 
55 #define tty_pane_full_width(tty, ctx) \
56 	((ctx)->xoff == 0 && screen_size_x((ctx)->wp->screen) >= (tty)->sx)
57 
58 void
tty_init(struct tty * tty,struct client * c,int fd,char * term)59 tty_init(struct tty *tty, struct client *c, int fd, char *term)
60 {
61 	char	*path;
62 
63 	memset(tty, 0, sizeof *tty);
64 	tty->log_fd = -1;
65 
66 	if (term == NULL || *term == '\0')
67 		tty->termname = xstrdup("unknown");
68 	else
69 		tty->termname = xstrdup(term);
70 	tty->fd = fd;
71 	tty->client = c;
72 
73 	if ((path = ttyname(fd)) == NULL)
74 		fatalx("ttyname failed");
75 	tty->path = xstrdup(path);
76 	tty->cstyle = 0;
77 	tty->ccolour = xstrdup("");
78 
79 	tty->flags = 0;
80 	tty->term_flags = 0;
81 }
82 
83 int
tty_resize(struct tty * tty)84 tty_resize(struct tty *tty)
85 {
86 	struct winsize	ws;
87 	u_int		sx, sy;
88 
89 	if (ioctl(tty->fd, TIOCGWINSZ, &ws) != -1) {
90 		sx = ws.ws_col;
91 		if (sx == 0)
92 			sx = 80;
93 		sy = ws.ws_row;
94 		if (sy == 0)
95 			sy = 24;
96 	} else {
97 		sx = 80;
98 		sy = 24;
99 	}
100 	if (!tty_set_size(tty, sx, sy))
101 		return (0);
102 
103 	tty->cx = UINT_MAX;
104 	tty->cy = UINT_MAX;
105 
106 	tty->rupper = UINT_MAX;
107 	tty->rlower = UINT_MAX;
108 
109 	/*
110 	 * If the terminal has been started, reset the actual scroll region and
111 	 * cursor position, as this may not have happened.
112 	 */
113 	if (tty->flags & TTY_STARTED) {
114 		tty_cursor(tty, 0, 0);
115 		tty_region(tty, 0, tty->sy - 1);
116 	}
117 
118 	return (1);
119 }
120 
121 int
tty_set_size(struct tty * tty,u_int sx,u_int sy)122 tty_set_size(struct tty *tty, u_int sx, u_int sy) {
123 	if (sx == tty->sx && sy == tty->sy)
124 		return (0);
125 	tty->sx = sx;
126 	tty->sy = sy;
127 	return (1);
128 }
129 
130 int
tty_open(struct tty * tty,const char * overrides,char ** cause)131 tty_open(struct tty *tty, const char *overrides, char **cause)
132 {
133 	char	out[64];
134 	int	fd;
135 
136 	if (debug_level > 3) {
137 		xsnprintf(out, sizeof out, "tmux-out-%ld.log", (long) getpid());
138 		fd = open(out, O_WRONLY|O_CREAT|O_TRUNC, 0644);
139 		if (fd != -1 && fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)
140 			fatal("fcntl failed");
141 		tty->log_fd = fd;
142 	}
143 
144 	tty->term = tty_term_find(tty->termname, tty->fd, overrides, cause);
145 	if (tty->term == NULL) {
146 		tty_close(tty);
147 		return (-1);
148 	}
149 	tty->flags |= TTY_OPENED;
150 
151 	tty->flags &= ~(TTY_NOCURSOR|TTY_FREEZE|TTY_TIMER);
152 
153 	tty->event = bufferevent_new(
154 	    tty->fd, tty_read_callback, NULL, tty_error_callback, tty);
155 
156 	tty_start_tty(tty);
157 
158 	tty_keys_build(tty);
159 
160 	return (0);
161 }
162 
163 void
tty_read_callback(unused struct bufferevent * bufev,void * data)164 tty_read_callback(unused struct bufferevent *bufev, void *data)
165 {
166 	struct tty	*tty = data;
167 
168 	while (tty_keys_next(tty))
169 		;
170 }
171 
172 void
tty_error_callback(unused struct bufferevent * bufev,unused short what,unused void * data)173 tty_error_callback(
174     unused struct bufferevent *bufev, unused short what, unused void *data)
175 {
176 }
177 
178 void
tty_init_termios(int fd,struct termios * orig_tio,struct bufferevent * bufev)179 tty_init_termios(int fd, struct termios *orig_tio, struct bufferevent *bufev)
180 {
181 	struct termios	tio;
182 
183 	if (fd == -1 || tcgetattr(fd, orig_tio) != 0)
184 		return;
185 
186 	setblocking(fd, 0);
187 
188 	if (bufev != NULL)
189 		bufferevent_enable(bufev, EV_READ|EV_WRITE);
190 
191 	memcpy(&tio, orig_tio, sizeof tio);
192 	tio.c_iflag &= ~(IXON|IXOFF|ICRNL|INLCR|IGNCR|IMAXBEL|ISTRIP);
193 	tio.c_iflag |= IGNBRK;
194 	tio.c_oflag &= ~(OPOST|ONLCR|OCRNL|ONLRET);
195 	tio.c_lflag &= ~(IEXTEN|ICANON|ECHO|ECHOE|ECHONL|ECHOCTL|
196 	    ECHOPRT|ECHOKE|ECHOCTL|ISIG);
197 	tio.c_cc[VMIN] = 1;
198 	tio.c_cc[VTIME] = 0;
199 	if (tcsetattr(fd, TCSANOW, &tio) == 0)
200 		tcflush(fd, TCIOFLUSH);
201 }
202 
203 void
tty_start_tty(struct tty * tty)204 tty_start_tty(struct tty *tty)
205 {
206 	tty_init_termios(tty->fd, &tty->tio, tty->event);
207 
208 	tty_putcode(tty, TTYC_SMCUP);
209 
210 	tty_putcode(tty, TTYC_SGR0);
211 	memcpy(&tty->cell, &grid_default_cell, sizeof tty->cell);
212 
213 	tty_putcode(tty, TTYC_RMKX);
214 	if (tty_use_acs(tty))
215 		tty_putcode(tty, TTYC_ENACS);
216 	tty_putcode(tty, TTYC_CLEAR);
217 
218 	tty_putcode(tty, TTYC_CNORM);
219 	if (tty_term_has(tty->term, TTYC_KMOUS))
220 		tty_puts(tty, "\033[?1000l\033[?1006l\033[?1005l");
221 
222 	if (tty_term_has(tty->term, TTYC_XT)) {
223 		if (options_get_number(&global_options, "focus-events")) {
224 			tty->flags |= TTY_FOCUS;
225 			tty_puts(tty, "\033[?1004h");
226 		}
227 		tty_puts(tty, "\033[c");
228 	}
229 
230 	tty->cx = UINT_MAX;
231 	tty->cy = UINT_MAX;
232 
233 	tty->rlower = UINT_MAX;
234 	tty->rupper = UINT_MAX;
235 
236 	tty->mode = MODE_CURSOR;
237 
238 	tty->flags |= TTY_STARTED;
239 
240 	tty_force_cursor_colour(tty, "");
241 }
242 
243 void
tty_set_class(struct tty * tty,u_int class)244 tty_set_class(struct tty *tty, u_int class)
245 {
246 	if (tty->class != 0)
247 		return;
248 	tty->class = class;
249 }
250 
251 void
tty_stop_tty(struct tty * tty)252 tty_stop_tty(struct tty *tty)
253 {
254 	struct winsize	ws;
255 
256 	if (!(tty->flags & TTY_STARTED))
257 		return;
258 	tty->flags &= ~TTY_STARTED;
259 
260 	bufferevent_disable(tty->event, EV_READ|EV_WRITE);
261 
262 	/*
263 	 * Be flexible about error handling and try not kill the server just
264 	 * because the fd is invalid. Things like ssh -t can easily leave us
265 	 * with a dead tty.
266 	 */
267 	if (ioctl(tty->fd, TIOCGWINSZ, &ws) == -1)
268 		return;
269 	if (tcsetattr(tty->fd, TCSANOW, &tty->tio) == -1)
270 		return;
271 
272 	tty_raw(tty, tty_term_string2(tty->term, TTYC_CSR, 0, ws.ws_row - 1));
273 	if (tty_use_acs(tty))
274 		tty_raw(tty, tty_term_string(tty->term, TTYC_RMACS));
275 	tty_raw(tty, tty_term_string(tty->term, TTYC_SGR0));
276 	tty_raw(tty, tty_term_string(tty->term, TTYC_RMKX));
277 	tty_raw(tty, tty_term_string(tty->term, TTYC_CLEAR));
278 	if (tty_term_has(tty->term, TTYC_SS) && tty->cstyle != 0) {
279 		if (tty_term_has(tty->term, TTYC_SE))
280 			tty_raw(tty, tty_term_string(tty->term, TTYC_SE));
281 		else
282 			tty_raw(tty, tty_term_string1(tty->term, TTYC_SS, 0));
283 	}
284 	tty_raw(tty, tty_term_string(tty->term, TTYC_CR));
285 
286 	tty_raw(tty, tty_term_string(tty->term, TTYC_CNORM));
287 	if (tty_term_has(tty->term, TTYC_KMOUS))
288 		tty_raw(tty, "\033[?1000l\033[?1006l\033[?1005l");
289 
290 	if (tty_term_has(tty->term, TTYC_XT)) {
291 		if (tty->flags & TTY_FOCUS) {
292 			tty->flags &= ~TTY_FOCUS;
293 			tty_puts(tty, "\033[?1004l");
294 		}
295 	}
296 
297 	tty_raw(tty, tty_term_string(tty->term, TTYC_RMCUP));
298 
299 	setblocking(tty->fd, 1);
300 }
301 
302 void
tty_close(struct tty * tty)303 tty_close(struct tty *tty)
304 {
305 	if (tty->log_fd != -1) {
306 		close(tty->log_fd);
307 		tty->log_fd = -1;
308 	}
309 
310 	if (event_initialized(&tty->key_timer))
311 		evtimer_del(&tty->key_timer);
312 	tty_stop_tty(tty);
313 
314 	if (tty->flags & TTY_OPENED) {
315 		bufferevent_free(tty->event);
316 
317 		tty_term_free(tty->term);
318 		tty_keys_free(tty);
319 
320 		tty->flags &= ~TTY_OPENED;
321 	}
322 
323 	if (tty->fd != -1) {
324 		close(tty->fd);
325 		tty->fd = -1;
326 	}
327 }
328 
329 void
tty_free(struct tty * tty)330 tty_free(struct tty *tty)
331 {
332 	tty_close(tty);
333 
334 	free(tty->ccolour);
335 	if (tty->path != NULL)
336 		free(tty->path);
337 	if (tty->termname != NULL)
338 		free(tty->termname);
339 }
340 
341 void
tty_raw(struct tty * tty,const char * s)342 tty_raw(struct tty *tty, const char *s)
343 {
344 	ssize_t	n, slen;
345 	u_int	i;
346 
347 	slen = strlen(s);
348 	for (i = 0; i < 5; i++) {
349 		n = write(tty->fd, s, slen);
350 		if (n >= 0) {
351 			s += n;
352 			slen -= n;
353 			if (slen == 0)
354 				break;
355 		} else if (n == -1 && errno != EAGAIN)
356 			break;
357 		usleep(100);
358 	}
359 }
360 
361 void
tty_putcode(struct tty * tty,enum tty_code_code code)362 tty_putcode(struct tty *tty, enum tty_code_code code)
363 {
364 	tty_puts(tty, tty_term_string(tty->term, code));
365 }
366 
367 void
tty_putcode1(struct tty * tty,enum tty_code_code code,int a)368 tty_putcode1(struct tty *tty, enum tty_code_code code, int a)
369 {
370 	if (a < 0)
371 		return;
372 	tty_puts(tty, tty_term_string1(tty->term, code, a));
373 }
374 
375 void
tty_putcode2(struct tty * tty,enum tty_code_code code,int a,int b)376 tty_putcode2(struct tty *tty, enum tty_code_code code, int a, int b)
377 {
378 	if (a < 0 || b < 0)
379 		return;
380 	tty_puts(tty, tty_term_string2(tty->term, code, a, b));
381 }
382 
383 void
tty_putcode_ptr1(struct tty * tty,enum tty_code_code code,const void * a)384 tty_putcode_ptr1(struct tty *tty, enum tty_code_code code, const void *a)
385 {
386 	if (a != NULL)
387 		tty_puts(tty, tty_term_ptr1(tty->term, code, a));
388 }
389 
390 void
tty_putcode_ptr2(struct tty * tty,enum tty_code_code code,const void * a,const void * b)391 tty_putcode_ptr2(struct tty *tty, enum tty_code_code code, const void *a, const void *b)
392 {
393 	if (a != NULL && b != NULL)
394 		tty_puts(tty, tty_term_ptr2(tty->term, code, a, b));
395 }
396 
397 void
tty_puts(struct tty * tty,const char * s)398 tty_puts(struct tty *tty, const char *s)
399 {
400 	if (*s == '\0')
401 		return;
402 	bufferevent_write(tty->event, s, strlen(s));
403 
404 	if (tty->log_fd != -1)
405 		write(tty->log_fd, s, strlen(s));
406 }
407 
408 void
tty_putc(struct tty * tty,u_char ch)409 tty_putc(struct tty *tty, u_char ch)
410 {
411 	const char	*acs;
412 	u_int		 sx;
413 
414 	if (tty->cell.attr & GRID_ATTR_CHARSET) {
415 		acs = tty_acs_get(tty, ch);
416 		if (acs != NULL)
417 			bufferevent_write(tty->event, acs, strlen(acs));
418 		else
419 			bufferevent_write(tty->event, &ch, 1);
420 	} else
421 		bufferevent_write(tty->event, &ch, 1);
422 
423 	if (ch >= 0x20 && ch != 0x7f) {
424 		sx = tty->sx;
425 		if (tty->term->flags & TERM_EARLYWRAP)
426 			sx--;
427 
428 		if (tty->cx >= sx) {
429 			tty->cx = 1;
430 			if (tty->cy != tty->rlower)
431 				tty->cy++;
432 		} else
433 			tty->cx++;
434 	}
435 
436 	if (tty->log_fd != -1)
437 		write(tty->log_fd, &ch, 1);
438 }
439 
440 void
tty_putn(struct tty * tty,const void * buf,size_t len,u_int width)441 tty_putn(struct tty *tty, const void *buf, size_t len, u_int width)
442 {
443 	bufferevent_write(tty->event, buf, len);
444 	if (tty->log_fd != -1)
445 		write(tty->log_fd, buf, len);
446 	tty->cx += width;
447 }
448 
449 void
tty_set_title(struct tty * tty,const char * title)450 tty_set_title(struct tty *tty, const char *title)
451 {
452 	if (!tty_term_has(tty->term, TTYC_TSL) ||
453 	    !tty_term_has(tty->term, TTYC_FSL))
454 		return;
455 
456 	tty_putcode(tty, TTYC_TSL);
457 	tty_puts(tty, title);
458 	tty_putcode(tty, TTYC_FSL);
459 }
460 
461 void
tty_force_cursor_colour(struct tty * tty,const char * ccolour)462 tty_force_cursor_colour(struct tty *tty, const char *ccolour)
463 {
464 	if (*ccolour == '\0')
465 		tty_putcode(tty, TTYC_CR);
466 	else
467 		tty_putcode_ptr1(tty, TTYC_CS, ccolour);
468 	free(tty->ccolour);
469 	tty->ccolour = xstrdup(ccolour);
470 }
471 
472 void
tty_update_mode(struct tty * tty,int mode,struct screen * s)473 tty_update_mode(struct tty *tty, int mode, struct screen *s)
474 {
475 	int	changed;
476 
477 	if (strcmp(s->ccolour, tty->ccolour))
478 		tty_force_cursor_colour(tty, s->ccolour);
479 
480 	if (tty->flags & TTY_NOCURSOR)
481 		mode &= ~MODE_CURSOR;
482 
483 	changed = mode ^ tty->mode;
484 	if (changed & MODE_CURSOR) {
485 		if (mode & MODE_CURSOR)
486 			tty_putcode(tty, TTYC_CNORM);
487 		else
488 			tty_putcode(tty, TTYC_CIVIS);
489 	}
490 	if (tty->cstyle != s->cstyle) {
491 		if (tty_term_has(tty->term, TTYC_SS)) {
492 			if (s->cstyle == 0 &&
493 			    tty_term_has(tty->term, TTYC_SE))
494 				tty_putcode(tty, TTYC_SE);
495 			else
496 				tty_putcode1(tty, TTYC_SS, s->cstyle);
497 		}
498 		tty->cstyle = s->cstyle;
499 	}
500 	if (changed & (ALL_MOUSE_MODES|MODE_MOUSE_UTF8)) {
501 		if (mode & ALL_MOUSE_MODES) {
502 			/*
503 			 * Enable the UTF-8 (1005) extension if configured to.
504 			 * Enable the SGR (1006) extension unconditionally, as
505 			 * this is safe from misinterpretation. Do it in this
506 			 * order, because in some terminals it's the last one
507 			 * that takes effect and SGR is the preferred one.
508 			 */
509 			if (mode & MODE_MOUSE_UTF8)
510 				tty_puts(tty, "\033[?1005h");
511 			else
512 				tty_puts(tty, "\033[?1005l");
513 			tty_puts(tty, "\033[?1006h");
514 
515 			if (mode & MODE_MOUSE_ANY)
516 				tty_puts(tty, "\033[?1003h");
517 			else if (mode & MODE_MOUSE_BUTTON)
518 				tty_puts(tty, "\033[?1002h");
519 			else if (mode & MODE_MOUSE_STANDARD)
520 				tty_puts(tty, "\033[?1000h");
521 		} else {
522 			if (tty->mode & MODE_MOUSE_ANY)
523 				tty_puts(tty, "\033[?1003l");
524 			else if (tty->mode & MODE_MOUSE_BUTTON)
525 				tty_puts(tty, "\033[?1002l");
526 			else if (tty->mode & MODE_MOUSE_STANDARD)
527 				tty_puts(tty, "\033[?1000l");
528 
529 			tty_puts(tty, "\033[?1006l");
530 			if (tty->mode & MODE_MOUSE_UTF8)
531 				tty_puts(tty, "\033[?1005l");
532 		}
533 	}
534 	if (changed & MODE_KKEYPAD) {
535 		if (mode & MODE_KKEYPAD)
536 			tty_putcode(tty, TTYC_SMKX);
537 		else
538 			tty_putcode(tty, TTYC_RMKX);
539 	}
540 	if (changed & MODE_BRACKETPASTE) {
541 		if (mode & MODE_BRACKETPASTE)
542 			tty_puts(tty, "\033[?2004h");
543 		else
544 			tty_puts(tty, "\033[?2004l");
545 	}
546 	tty->mode = mode;
547 }
548 
549 void
tty_emulate_repeat(struct tty * tty,enum tty_code_code code,enum tty_code_code code1,u_int n)550 tty_emulate_repeat(
551     struct tty *tty, enum tty_code_code code, enum tty_code_code code1, u_int n)
552 {
553 	if (tty_term_has(tty->term, code))
554 		tty_putcode1(tty, code, n);
555 	else {
556 		while (n-- > 0)
557 			tty_putcode(tty, code1);
558 	}
559 }
560 
561 void
tty_repeat_space(struct tty * tty,u_int n)562 tty_repeat_space(struct tty *tty, u_int n)
563 {
564 	while (n-- > 0)
565 		tty_putc(tty, ' ');
566 }
567 
568 /*
569  * Is the region large enough to be worth redrawing once later rather than
570  * probably several times now? Currently yes if it is more than 50% of the
571  * pane.
572  */
573 int
tty_large_region(unused struct tty * tty,const struct tty_ctx * ctx)574 tty_large_region(unused struct tty *tty, const struct tty_ctx *ctx)
575 {
576 	struct window_pane	*wp = ctx->wp;
577 
578 	return (ctx->orlower - ctx->orupper >= screen_size_y(wp->screen) / 2);
579 }
580 
581 /*
582  * Redraw scroll region using data from screen (already updated). Used when
583  * CSR not supported, or window is a pane that doesn't take up the full
584  * width of the terminal.
585  */
586 void
tty_redraw_region(struct tty * tty,const struct tty_ctx * ctx)587 tty_redraw_region(struct tty *tty, const struct tty_ctx *ctx)
588 {
589 	struct window_pane	*wp = ctx->wp;
590 	struct screen		*s = wp->screen;
591 	u_int		 	 i;
592 
593 	/*
594 	 * If region is large, schedule a window redraw. In most cases this is
595 	 * likely to be followed by some more scrolling.
596 	 */
597 	if (tty_large_region(tty, ctx)) {
598 		wp->flags |= PANE_REDRAW;
599 		return;
600 	}
601 
602 	if (ctx->ocy < ctx->orupper || ctx->ocy > ctx->orlower) {
603 		for (i = ctx->ocy; i < screen_size_y(s); i++)
604 			tty_draw_line(tty, s, i, ctx->xoff, ctx->yoff);
605 	} else {
606 		for (i = ctx->orupper; i <= ctx->orlower; i++)
607 			tty_draw_line(tty, s, i, ctx->xoff, ctx->yoff);
608 	}
609 }
610 
611 void
tty_draw_line(struct tty * tty,struct screen * s,u_int py,u_int ox,u_int oy)612 tty_draw_line(struct tty *tty, struct screen *s, u_int py, u_int ox, u_int oy)
613 {
614 	const struct grid_cell	*gc;
615 	struct grid_line	*gl;
616 	struct grid_cell	 tmpgc;
617 	struct utf8_data	 ud;
618 	u_int			 i, sx;
619 
620 	tty_update_mode(tty, tty->mode & ~MODE_CURSOR, s);
621 
622 	sx = screen_size_x(s);
623 	if (sx > s->grid->linedata[s->grid->hsize + py].cellsize)
624 		sx = s->grid->linedata[s->grid->hsize + py].cellsize;
625 	if (sx > tty->sx)
626 		sx = tty->sx;
627 
628 	/*
629 	 * Don't move the cursor to the start permission if it will wrap there
630 	 * itself.
631 	 */
632 	gl = NULL;
633 	if (py != 0)
634 		gl = &s->grid->linedata[s->grid->hsize + py - 1];
635 	if (oy + py == 0 || gl == NULL || !(gl->flags & GRID_LINE_WRAPPED) ||
636 	    tty->cx < tty->sx || ox != 0 ||
637 	    (oy + py != tty->cy + 1 && tty->cy != s->rlower + oy))
638 		tty_cursor(tty, ox, oy + py);
639 
640 	for (i = 0; i < sx; i++) {
641 		gc = grid_view_peek_cell(s->grid, i, py);
642 		if (screen_check_selection(s, i, py)) {
643 			memcpy(&tmpgc, &s->sel.cell, sizeof tmpgc);
644 			grid_cell_get(gc, &ud);
645 			grid_cell_set(&tmpgc, &ud);
646 			tmpgc.flags = gc->flags &
647 			    ~(GRID_FLAG_FG256|GRID_FLAG_BG256);
648 			tmpgc.flags |= s->sel.cell.flags &
649 			    (GRID_FLAG_FG256|GRID_FLAG_BG256);
650 			tty_cell(tty, &tmpgc);
651 		} else
652 			tty_cell(tty, gc);
653 	}
654 
655 	if (sx >= tty->sx) {
656 		tty_update_mode(tty, tty->mode, s);
657 		return;
658 	}
659 	tty_reset(tty);
660 
661 	tty_cursor(tty, ox + sx, oy + py);
662 	if (sx != screen_size_x(s) && ox + screen_size_x(s) >= tty->sx &&
663 	    tty_term_has(tty->term, TTYC_EL))
664 		tty_putcode(tty, TTYC_EL);
665 	else
666 		tty_repeat_space(tty, screen_size_x(s) - sx);
667 	tty_update_mode(tty, tty->mode, s);
668 }
669 
670 void
tty_write(void (* cmdfn)(struct tty *,const struct tty_ctx *),struct tty_ctx * ctx)671 tty_write(
672     void (*cmdfn)(struct tty *, const struct tty_ctx *), struct tty_ctx *ctx)
673 {
674 	struct window_pane	*wp = ctx->wp;
675 	struct client		*c;
676 	u_int		 	 i;
677 
678 	/* wp can be NULL if updating the screen but not the terminal. */
679 	if (wp == NULL)
680 		return;
681 
682 	if (wp->window->flags & WINDOW_REDRAW || wp->flags & PANE_REDRAW)
683 		return;
684 	if (!window_pane_visible(wp) || wp->flags & PANE_DROP)
685 		return;
686 
687 	for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
688 		c = ARRAY_ITEM(&clients, i);
689 		if (c == NULL || c->session == NULL || c->tty.term == NULL)
690 			continue;
691 		if (c->flags & CLIENT_SUSPENDED)
692 			continue;
693 		if (c->tty.flags & TTY_FREEZE)
694 			continue;
695 		if (c->session->curw->window != wp->window)
696 			continue;
697 
698 		ctx->xoff = wp->xoff;
699 		ctx->yoff = wp->yoff;
700 		if (status_at_line(c) == 0)
701 			ctx->yoff++;
702 
703 		cmdfn(&c->tty, ctx);
704 	}
705 }
706 
707 void
tty_cmd_insertcharacter(struct tty * tty,const struct tty_ctx * ctx)708 tty_cmd_insertcharacter(struct tty *tty, const struct tty_ctx *ctx)
709 {
710 	struct window_pane	*wp = ctx->wp;
711 
712 	if (!tty_pane_full_width(tty, ctx)) {
713 		tty_draw_line(tty, wp->screen, ctx->ocy, ctx->xoff, ctx->yoff);
714 		return;
715 	}
716 
717 	tty_reset(tty);
718 
719 	tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
720 
721 	if (tty_term_has(tty->term, TTYC_ICH) ||
722 	    tty_term_has(tty->term, TTYC_ICH1))
723 		tty_emulate_repeat(tty, TTYC_ICH, TTYC_ICH1, ctx->num);
724 	else
725 		tty_draw_line(tty, wp->screen, ctx->ocy, ctx->xoff, ctx->yoff);
726 }
727 
728 void
tty_cmd_deletecharacter(struct tty * tty,const struct tty_ctx * ctx)729 tty_cmd_deletecharacter(struct tty *tty, const struct tty_ctx *ctx)
730 {
731 	struct window_pane	*wp = ctx->wp;
732 
733 	if (!tty_pane_full_width(tty, ctx) ||
734 	    (!tty_term_has(tty->term, TTYC_DCH) &&
735 	    !tty_term_has(tty->term, TTYC_DCH1))) {
736 		tty_draw_line(tty, wp->screen, ctx->ocy, ctx->xoff, ctx->yoff);
737 		return;
738 	}
739 
740 	tty_reset(tty);
741 
742 	tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
743 
744 	if (tty_term_has(tty->term, TTYC_DCH) ||
745 	    tty_term_has(tty->term, TTYC_DCH1))
746 		tty_emulate_repeat(tty, TTYC_DCH, TTYC_DCH1, ctx->num);
747 }
748 
749 void
tty_cmd_clearcharacter(struct tty * tty,const struct tty_ctx * ctx)750 tty_cmd_clearcharacter(struct tty *tty, const struct tty_ctx *ctx)
751 {
752 	u_int	i;
753 
754 	tty_reset(tty);
755 
756 	tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
757 
758 	if (tty_term_has(tty->term, TTYC_ECH))
759 		tty_putcode1(tty, TTYC_ECH, ctx->num);
760 	else {
761 		for (i = 0; i < ctx->num; i++)
762 			tty_putc(tty, ' ');
763 	}
764 }
765 
766 void
tty_cmd_insertline(struct tty * tty,const struct tty_ctx * ctx)767 tty_cmd_insertline(struct tty *tty, const struct tty_ctx *ctx)
768 {
769 	if (!tty_pane_full_width(tty, ctx) ||
770 	    !tty_term_has(tty->term, TTYC_CSR) ||
771 	    !tty_term_has(tty->term, TTYC_IL1)) {
772 		tty_redraw_region(tty, ctx);
773 		return;
774 	}
775 
776 	tty_reset(tty);
777 
778 	tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
779 	tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
780 
781 	tty_emulate_repeat(tty, TTYC_IL, TTYC_IL1, ctx->num);
782 }
783 
784 void
tty_cmd_deleteline(struct tty * tty,const struct tty_ctx * ctx)785 tty_cmd_deleteline(struct tty *tty, const struct tty_ctx *ctx)
786 {
787 	if (!tty_pane_full_width(tty, ctx) ||
788 	    !tty_term_has(tty->term, TTYC_CSR) ||
789 	    !tty_term_has(tty->term, TTYC_DL1)) {
790 		tty_redraw_region(tty, ctx);
791 		return;
792 	}
793 
794 	tty_reset(tty);
795 
796 	tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
797 	tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
798 
799 	tty_emulate_repeat(tty, TTYC_DL, TTYC_DL1, ctx->num);
800 }
801 
802 void
tty_cmd_clearline(struct tty * tty,const struct tty_ctx * ctx)803 tty_cmd_clearline(struct tty *tty, const struct tty_ctx *ctx)
804 {
805 	struct window_pane	*wp = ctx->wp;
806 	struct screen		*s = wp->screen;
807 
808 	tty_reset(tty);
809 
810 	tty_cursor_pane(tty, ctx, 0, ctx->ocy);
811 
812 	if (tty_pane_full_width(tty, ctx) && tty_term_has(tty->term, TTYC_EL))
813 		tty_putcode(tty, TTYC_EL);
814 	else
815 		tty_repeat_space(tty, screen_size_x(s));
816 }
817 
818 void
tty_cmd_clearendofline(struct tty * tty,const struct tty_ctx * ctx)819 tty_cmd_clearendofline(struct tty *tty, const struct tty_ctx *ctx)
820 {
821 	struct window_pane	*wp = ctx->wp;
822 	struct screen		*s = wp->screen;
823 
824 	tty_reset(tty);
825 
826 	tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
827 
828 	if (tty_pane_full_width(tty, ctx) && tty_term_has(tty->term, TTYC_EL))
829 		tty_putcode(tty, TTYC_EL);
830 	else
831 		tty_repeat_space(tty, screen_size_x(s) - ctx->ocx);
832 }
833 
834 void
tty_cmd_clearstartofline(struct tty * tty,const struct tty_ctx * ctx)835 tty_cmd_clearstartofline(struct tty *tty, const struct tty_ctx *ctx)
836 {
837 	tty_reset(tty);
838 
839 	if (ctx->xoff == 0 && tty_term_has(tty->term, TTYC_EL1)) {
840 		tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
841 		tty_putcode(tty, TTYC_EL1);
842 	} else {
843 		tty_cursor_pane(tty, ctx, 0, ctx->ocy);
844 		tty_repeat_space(tty, ctx->ocx + 1);
845 	}
846 }
847 
848 void
tty_cmd_reverseindex(struct tty * tty,const struct tty_ctx * ctx)849 tty_cmd_reverseindex(struct tty *tty, const struct tty_ctx *ctx)
850 {
851 	if (ctx->ocy != ctx->orupper)
852 		return;
853 
854 	if (!tty_pane_full_width(tty, ctx) ||
855 	    !tty_term_has(tty->term, TTYC_CSR) ||
856 	    !tty_term_has(tty->term, TTYC_RI)) {
857 		tty_redraw_region(tty, ctx);
858 		return;
859 	}
860 
861 	tty_reset(tty);
862 
863 	tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
864 	tty_cursor_pane(tty, ctx, ctx->ocx, ctx->orupper);
865 
866 	tty_putcode(tty, TTYC_RI);
867 }
868 
869 void
tty_cmd_linefeed(struct tty * tty,const struct tty_ctx * ctx)870 tty_cmd_linefeed(struct tty *tty, const struct tty_ctx *ctx)
871 {
872 	struct window_pane	*wp = ctx->wp;
873 
874 	if (ctx->ocy != ctx->orlower)
875 		return;
876 
877 	if (!tty_pane_full_width(tty, ctx) ||
878 	    !tty_term_has(tty->term, TTYC_CSR)) {
879 		if (tty_large_region(tty, ctx))
880 			wp->flags |= PANE_REDRAW;
881 		else
882 			tty_redraw_region(tty, ctx);
883 		return;
884 	}
885 
886 	/*
887 	 * If this line wrapped naturally (ctx->num is nonzero), don't do
888 	 * anything - the cursor can just be moved to the last cell and wrap
889 	 * naturally.
890 	 */
891 	if (ctx->num && !(tty->term->flags & TERM_EARLYWRAP))
892 		return;
893 
894 	tty_reset(tty);
895 
896 	tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
897 	tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
898 
899 	tty_putc(tty, '\n');
900 }
901 
902 void
tty_cmd_clearendofscreen(struct tty * tty,const struct tty_ctx * ctx)903 tty_cmd_clearendofscreen(struct tty *tty, const struct tty_ctx *ctx)
904 {
905 	struct window_pane	*wp = ctx->wp;
906 	struct screen		*s = wp->screen;
907 	u_int		 	 i, j;
908 
909 	tty_reset(tty);
910 
911 	tty_region_pane(tty, ctx, 0, screen_size_y(s) - 1);
912 	tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
913 
914 	if (tty_pane_full_width(tty, ctx) && tty_term_has(tty->term, TTYC_EL)) {
915 		tty_putcode(tty, TTYC_EL);
916 		if (ctx->ocy != screen_size_y(s) - 1) {
917 			tty_cursor_pane(tty, ctx, 0, ctx->ocy + 1);
918 			for (i = ctx->ocy + 1; i < screen_size_y(s); i++) {
919 				tty_putcode(tty, TTYC_EL);
920 				if (i == screen_size_y(s) - 1)
921 					continue;
922 				tty_emulate_repeat(tty, TTYC_CUD, TTYC_CUD1, 1);
923 				tty->cy++;
924 			}
925 		}
926 	} else {
927 		tty_repeat_space(tty, screen_size_x(s) - ctx->ocx);
928 		for (j = ctx->ocy + 1; j < screen_size_y(s); j++) {
929 			tty_cursor_pane(tty, ctx, 0, j);
930 			tty_repeat_space(tty, screen_size_x(s));
931 		}
932 	}
933 }
934 
935 void
tty_cmd_clearstartofscreen(struct tty * tty,const struct tty_ctx * ctx)936 tty_cmd_clearstartofscreen(struct tty *tty, const struct tty_ctx *ctx)
937 {
938 	struct window_pane	*wp = ctx->wp;
939 	struct screen		*s = wp->screen;
940 	u_int		 	 i, j;
941 
942 	tty_reset(tty);
943 
944 	tty_region_pane(tty, ctx, 0, screen_size_y(s) - 1);
945 	tty_cursor_pane(tty, ctx, 0, 0);
946 
947 	if (tty_pane_full_width(tty, ctx) && tty_term_has(tty->term, TTYC_EL)) {
948 		for (i = 0; i < ctx->ocy; i++) {
949 			tty_putcode(tty, TTYC_EL);
950 			tty_emulate_repeat(tty, TTYC_CUD, TTYC_CUD1, 1);
951 			tty->cy++;
952 		}
953 	} else {
954 		for (j = 0; j < ctx->ocy; j++) {
955 			tty_cursor_pane(tty, ctx, 0, j);
956 			tty_repeat_space(tty, screen_size_x(s));
957 		}
958 	}
959 	tty_repeat_space(tty, ctx->ocx + 1);
960 }
961 
962 void
tty_cmd_clearscreen(struct tty * tty,const struct tty_ctx * ctx)963 tty_cmd_clearscreen(struct tty *tty, const struct tty_ctx *ctx)
964 {
965 	struct window_pane	*wp = ctx->wp;
966 	struct screen		*s = wp->screen;
967 	u_int		 	 i, j;
968 
969 	tty_reset(tty);
970 
971 	tty_region_pane(tty, ctx, 0, screen_size_y(s) - 1);
972 	tty_cursor_pane(tty, ctx, 0, 0);
973 
974 	if (tty_pane_full_width(tty, ctx) && tty_term_has(tty->term, TTYC_EL)) {
975 		for (i = 0; i < screen_size_y(s); i++) {
976 			tty_putcode(tty, TTYC_EL);
977 			if (i != screen_size_y(s) - 1) {
978 				tty_emulate_repeat(tty, TTYC_CUD, TTYC_CUD1, 1);
979 				tty->cy++;
980 			}
981 		}
982 	} else {
983 		for (j = 0; j < screen_size_y(s); j++) {
984 			tty_cursor_pane(tty, ctx, 0, j);
985 			tty_repeat_space(tty, screen_size_x(s));
986 		}
987 	}
988 }
989 
990 void
tty_cmd_alignmenttest(struct tty * tty,const struct tty_ctx * ctx)991 tty_cmd_alignmenttest(struct tty *tty, const struct tty_ctx *ctx)
992 {
993 	struct window_pane	*wp = ctx->wp;
994 	struct screen		*s = wp->screen;
995 	u_int			 i, j;
996 
997 	tty_reset(tty);
998 
999 	tty_region_pane(tty, ctx, 0, screen_size_y(s) - 1);
1000 
1001 	for (j = 0; j < screen_size_y(s); j++) {
1002 		tty_cursor_pane(tty, ctx, 0, j);
1003 		for (i = 0; i < screen_size_x(s); i++)
1004 			tty_putc(tty, 'E');
1005 	}
1006 }
1007 
1008 void
tty_cmd_cell(struct tty * tty,const struct tty_ctx * ctx)1009 tty_cmd_cell(struct tty *tty, const struct tty_ctx *ctx)
1010 {
1011 	struct window_pane	*wp = ctx->wp;
1012 	struct screen		*s = wp->screen;
1013 	u_int			 cx;
1014 	u_int			 width;
1015 
1016 	tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
1017 
1018 	/* Is the cursor in the very last position? */
1019 	width = grid_cell_width(ctx->cell);
1020 	if (ctx->ocx > wp->sx - width) {
1021 		if (ctx->xoff != 0 || wp->sx != tty->sx) {
1022 			/*
1023 			 * The pane doesn't fill the entire line, the linefeed
1024 			 * will already have happened, so just move the cursor.
1025 			 */
1026 			if (ctx->ocy != wp->yoff + wp->screen->rlower)
1027 				tty_cursor_pane(tty, ctx, 0, ctx->ocy + 1);
1028 			else
1029 				tty_cursor_pane(tty, ctx, 0, ctx->ocy);
1030 		} else if (tty->cx < tty->sx) {
1031 			/*
1032 			 * The cursor isn't in the last position already, so
1033 			 * move as far left as possible and redraw the last
1034 			 * cell to move into the last position.
1035 			 */
1036 			cx = screen_size_x(s) - grid_cell_width(&ctx->last_cell);
1037 			tty_cursor_pane(tty, ctx, cx, ctx->ocy);
1038 			tty_cell(tty, &ctx->last_cell);
1039 		}
1040 	} else
1041 		tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1042 
1043 	tty_cell(tty, ctx->cell);
1044 }
1045 
1046 void
tty_cmd_utf8character(struct tty * tty,const struct tty_ctx * ctx)1047 tty_cmd_utf8character(struct tty *tty, const struct tty_ctx *ctx)
1048 {
1049 	struct window_pane	*wp = ctx->wp;
1050 
1051 	/*
1052 	 * Cannot rely on not being a partial character, so just redraw the
1053 	 * whole line.
1054 	 */
1055 	tty_draw_line(tty, wp->screen, ctx->ocy, ctx->xoff, ctx->yoff);
1056 }
1057 
1058 void
tty_cmd_setselection(struct tty * tty,const struct tty_ctx * ctx)1059 tty_cmd_setselection(struct tty *tty, const struct tty_ctx *ctx)
1060 {
1061 	char	*buf;
1062 	size_t	 off;
1063 
1064 	if (!tty_term_has(tty->term, TTYC_MS))
1065 		return;
1066 
1067 	off = 4 * ((ctx->num + 2) / 3) + 1; /* storage for base64 */
1068 	buf = xmalloc(off);
1069 
1070 	b64_ntop(ctx->ptr, ctx->num, buf, off);
1071 	tty_putcode_ptr2(tty, TTYC_MS, "", buf);
1072 
1073 	free(buf);
1074 }
1075 
1076 void
tty_cmd_rawstring(struct tty * tty,const struct tty_ctx * ctx)1077 tty_cmd_rawstring(struct tty *tty, const struct tty_ctx *ctx)
1078 {
1079 	u_int	 i;
1080 	u_char	*str = ctx->ptr;
1081 
1082 	for (i = 0; i < ctx->num; i++)
1083 		tty_putc(tty, str[i]);
1084 
1085 	tty->cx = tty->cy = UINT_MAX;
1086 	tty->rupper = tty->rlower = UINT_MAX;
1087 
1088 	tty_reset(tty);
1089 	tty_cursor(tty, 0, 0);
1090 }
1091 
1092 void
tty_cell(struct tty * tty,const struct grid_cell * gc)1093 tty_cell(struct tty *tty, const struct grid_cell *gc)
1094 {
1095 	struct utf8_data	ud;
1096 	u_int			i;
1097 
1098 	/* Skip last character if terminal is stupid. */
1099 	if (tty->term->flags & TERM_EARLYWRAP &&
1100 	    tty->cy == tty->sy - 1 && tty->cx == tty->sx - 1)
1101 		return;
1102 
1103 	/* If this is a padding character, do nothing. */
1104 	if (gc->flags & GRID_FLAG_PADDING)
1105 		return;
1106 
1107 	/* Set the attributes. */
1108 	tty_attributes(tty, gc);
1109 
1110 	/* Get the cell and if ASCII write with putc to do ACS translation. */
1111 	grid_cell_get(gc, &ud);
1112 	if (ud.size == 1) {
1113 		if (*ud.data < 0x20 || *ud.data == 0x7f)
1114 			return;
1115 		tty_putc(tty, *ud.data);
1116 		return;
1117 	}
1118 
1119 	/* If not UTF-8, write _. */
1120 	if (!(tty->flags & TTY_UTF8)) {
1121 		for (i = 0; i < ud.width; i++)
1122 			tty_putc(tty, '_');
1123 		return;
1124 	}
1125 
1126 	/* Write the data. */
1127 	tty_putn(tty, ud.data, ud.size, ud.width);
1128 }
1129 
1130 void
tty_reset(struct tty * tty)1131 tty_reset(struct tty *tty)
1132 {
1133 	struct grid_cell	*gc = &tty->cell;
1134 
1135 	if (memcmp(gc, &grid_default_cell, sizeof *gc) == 0)
1136 		return;
1137 
1138 	if ((gc->attr & GRID_ATTR_CHARSET) && tty_use_acs(tty))
1139 		tty_putcode(tty, TTYC_RMACS);
1140 	tty_putcode(tty, TTYC_SGR0);
1141 	memcpy(gc, &grid_default_cell, sizeof *gc);
1142 }
1143 
1144 /* Set region inside pane. */
1145 void
tty_region_pane(struct tty * tty,const struct tty_ctx * ctx,u_int rupper,u_int rlower)1146 tty_region_pane(
1147     struct tty *tty, const struct tty_ctx *ctx, u_int rupper, u_int rlower)
1148 {
1149 	tty_region(tty, ctx->yoff + rupper, ctx->yoff + rlower);
1150 }
1151 
1152 /* Set region at absolute position. */
1153 void
tty_region(struct tty * tty,u_int rupper,u_int rlower)1154 tty_region(struct tty *tty, u_int rupper, u_int rlower)
1155 {
1156 	if (tty->rlower == rlower && tty->rupper == rupper)
1157 		return;
1158 	if (!tty_term_has(tty->term, TTYC_CSR))
1159 		return;
1160 
1161 	tty->rupper = rupper;
1162 	tty->rlower = rlower;
1163 
1164 	/*
1165 	 * Some terminals (such as PuTTY) do not correctly reset the cursor to
1166 	 * 0,0 if it is beyond the last column (they do not reset their wrap
1167 	 * flag so further output causes a line feed). As a workaround, do an
1168 	 * explicit move to 0 first.
1169 	 */
1170 	if (tty->cx >= tty->sx)
1171 		tty_cursor(tty, 0, tty->cy);
1172 
1173 	tty_putcode2(tty, TTYC_CSR, tty->rupper, tty->rlower);
1174 	tty_cursor(tty, 0, 0);
1175 }
1176 
1177 /* Move cursor inside pane. */
1178 void
tty_cursor_pane(struct tty * tty,const struct tty_ctx * ctx,u_int cx,u_int cy)1179 tty_cursor_pane(struct tty *tty, const struct tty_ctx *ctx, u_int cx, u_int cy)
1180 {
1181 	tty_cursor(tty, ctx->xoff + cx, ctx->yoff + cy);
1182 }
1183 
1184 /* Move cursor to absolute position. */
1185 void
tty_cursor(struct tty * tty,u_int cx,u_int cy)1186 tty_cursor(struct tty *tty, u_int cx, u_int cy)
1187 {
1188 	struct tty_term	*term = tty->term;
1189 	u_int		 thisx, thisy;
1190 	int		 change;
1191 
1192 	if (cx > tty->sx - 1)
1193 		cx = tty->sx - 1;
1194 
1195 	thisx = tty->cx;
1196 	thisy = tty->cy;
1197 
1198 	/* No change. */
1199 	if (cx == thisx && cy == thisy)
1200 		return;
1201 
1202 	/* Very end of the line, just use absolute movement. */
1203 	if (thisx > tty->sx - 1)
1204 		goto absolute;
1205 
1206 	/* Move to home position (0, 0). */
1207 	if (cx == 0 && cy == 0 && tty_term_has(term, TTYC_HOME)) {
1208 		tty_putcode(tty, TTYC_HOME);
1209 		goto out;
1210 	}
1211 
1212 	/* Zero on the next line. */
1213 	if (cx == 0 && cy == thisy + 1 && thisy != tty->rlower) {
1214 		tty_putc(tty, '\r');
1215 		tty_putc(tty, '\n');
1216 		goto out;
1217 	}
1218 
1219 	/* Moving column or row. */
1220 	if (cy == thisy) {
1221 		/*
1222 		 * Moving column only, row staying the same.
1223 		 */
1224 
1225 		/* To left edge. */
1226 		if (cx == 0)	{
1227 			tty_putc(tty, '\r');
1228 			goto out;
1229 		}
1230 
1231 		/* One to the left. */
1232 		if (cx == thisx - 1 && tty_term_has(term, TTYC_CUB1)) {
1233 			tty_putcode(tty, TTYC_CUB1);
1234 			goto out;
1235 		}
1236 
1237 		/* One to the right. */
1238 		if (cx == thisx + 1 && tty_term_has(term, TTYC_CUF1)) {
1239 			tty_putcode(tty, TTYC_CUF1);
1240 			goto out;
1241 		}
1242 
1243 		/* Calculate difference. */
1244 		change = thisx - cx;	/* +ve left, -ve right */
1245 
1246 		/*
1247 		 * Use HPA if change is larger than absolute, otherwise move
1248 		 * the cursor with CUB/CUF.
1249 		 */
1250 		if ((u_int) abs(change) > cx && tty_term_has(term, TTYC_HPA)) {
1251 			tty_putcode1(tty, TTYC_HPA, cx);
1252 			goto out;
1253 		} else if (change > 0 && tty_term_has(term, TTYC_CUB)) {
1254 			tty_putcode1(tty, TTYC_CUB, change);
1255 			goto out;
1256 		} else if (change < 0 && tty_term_has(term, TTYC_CUF)) {
1257 			tty_putcode1(tty, TTYC_CUF, -change);
1258 			goto out;
1259 		}
1260 	} else if (cx == thisx) {
1261 		/*
1262 		 * Moving row only, column staying the same.
1263 		 */
1264 
1265 		/* One above. */
1266 		if (thisy != tty->rupper &&
1267 		    cy == thisy - 1 && tty_term_has(term, TTYC_CUU1)) {
1268 			tty_putcode(tty, TTYC_CUU1);
1269 			goto out;
1270 		}
1271 
1272 		/* One below. */
1273 		if (thisy != tty->rlower &&
1274 		    cy == thisy + 1 && tty_term_has(term, TTYC_CUD1)) {
1275 			tty_putcode(tty, TTYC_CUD1);
1276 			goto out;
1277 		}
1278 
1279 		/* Calculate difference. */
1280 		change = thisy - cy;	/* +ve up, -ve down */
1281 
1282 		/*
1283 		 * Try to use VPA if change is larger than absolute or if this
1284 		 * change would cross the scroll region, otherwise use CUU/CUD.
1285 		 */
1286 		if ((u_int) abs(change) > cy ||
1287 		    (change < 0 && cy - change > tty->rlower) ||
1288 		    (change > 0 && cy - change < tty->rupper)) {
1289 			    if (tty_term_has(term, TTYC_VPA)) {
1290 				    tty_putcode1(tty, TTYC_VPA, cy);
1291 				    goto out;
1292 			    }
1293 		} else if (change > 0 && tty_term_has(term, TTYC_CUU)) {
1294 			tty_putcode1(tty, TTYC_CUU, change);
1295 			goto out;
1296 		} else if (change < 0 && tty_term_has(term, TTYC_CUD)) {
1297 			tty_putcode1(tty, TTYC_CUD, -change);
1298 			goto out;
1299 		}
1300 	}
1301 
1302 absolute:
1303 	/* Absolute movement. */
1304 	tty_putcode2(tty, TTYC_CUP, cy, cx);
1305 
1306 out:
1307 	tty->cx = cx;
1308 	tty->cy = cy;
1309 }
1310 
1311 void
tty_attributes(struct tty * tty,const struct grid_cell * gc)1312 tty_attributes(struct tty *tty, const struct grid_cell *gc)
1313 {
1314 	struct grid_cell	*tc = &tty->cell, gc2;
1315 	u_char			 changed;
1316 
1317 	memcpy(&gc2, gc, sizeof gc2);
1318 
1319 	/*
1320 	 * If no setab, try to use the reverse attribute as a best-effort for a
1321 	 * non-default background. This is a bit of a hack but it doesn't do
1322 	 * any serious harm and makes a couple of applications happier.
1323 	 */
1324 	if (!tty_term_has(tty->term, TTYC_SETAB)) {
1325 		if (gc2.attr & GRID_ATTR_REVERSE) {
1326 			if (gc2.fg != 7 && gc2.fg != 8)
1327 				gc2.attr &= ~GRID_ATTR_REVERSE;
1328 		} else {
1329 			if (gc2.bg != 0 && gc2.bg != 8)
1330 				gc2.attr |= GRID_ATTR_REVERSE;
1331 		}
1332 	}
1333 
1334 	/* Fix up the colours if necessary. */
1335 	tty_check_fg(tty, &gc2);
1336 	tty_check_bg(tty, &gc2);
1337 
1338 	/* If any bits are being cleared, reset everything. */
1339 	if (tc->attr & ~gc2.attr)
1340 		tty_reset(tty);
1341 
1342 	/*
1343 	 * Set the colours. This may call tty_reset() (so it comes next) and
1344 	 * may add to (NOT remove) the desired attributes by changing new_attr.
1345 	 */
1346 	tty_colours(tty, &gc2);
1347 
1348 	/* Filter out attribute bits already set. */
1349 	changed = gc2.attr & ~tc->attr;
1350 	tc->attr = gc2.attr;
1351 
1352 	/* Set the attributes. */
1353 	if (changed & GRID_ATTR_BRIGHT)
1354 		tty_putcode(tty, TTYC_BOLD);
1355 	if (changed & GRID_ATTR_DIM)
1356 		tty_putcode(tty, TTYC_DIM);
1357 	if (changed & GRID_ATTR_ITALICS) {
1358 		if (tty_term_has(tty->term, TTYC_SITM))
1359 			tty_putcode(tty, TTYC_SITM);
1360 		else
1361 			tty_putcode(tty, TTYC_SMSO);
1362 	}
1363 	if (changed & GRID_ATTR_UNDERSCORE)
1364 		tty_putcode(tty, TTYC_SMUL);
1365 	if (changed & GRID_ATTR_BLINK)
1366 		tty_putcode(tty, TTYC_BLINK);
1367 	if (changed & GRID_ATTR_REVERSE) {
1368 		if (tty_term_has(tty->term, TTYC_REV))
1369 			tty_putcode(tty, TTYC_REV);
1370 		else if (tty_term_has(tty->term, TTYC_SMSO))
1371 			tty_putcode(tty, TTYC_SMSO);
1372 	}
1373 	if (changed & GRID_ATTR_HIDDEN)
1374 		tty_putcode(tty, TTYC_INVIS);
1375 	if ((changed & GRID_ATTR_CHARSET) && tty_use_acs(tty))
1376 		tty_putcode(tty, TTYC_SMACS);
1377 }
1378 
1379 void
tty_colours(struct tty * tty,const struct grid_cell * gc)1380 tty_colours(struct tty *tty, const struct grid_cell *gc)
1381 {
1382 	struct grid_cell	*tc = &tty->cell;
1383 	u_char			 fg = gc->fg, bg = gc->bg, flags = gc->flags;
1384 	int			 have_ax, fg_default, bg_default;
1385 
1386 	/* No changes? Nothing is necessary. */
1387 	if (fg == tc->fg && bg == tc->bg &&
1388 	    ((flags ^ tc->flags) & (GRID_FLAG_FG256|GRID_FLAG_BG256)) == 0)
1389 		return;
1390 
1391 	/*
1392 	 * Is either the default colour? This is handled specially because the
1393 	 * best solution might be to reset both colours to default, in which
1394 	 * case if only one is default need to fall onward to set the other
1395 	 * colour.
1396 	 */
1397 	fg_default = (fg == 8 && !(flags & GRID_FLAG_FG256));
1398 	bg_default = (bg == 8 && !(flags & GRID_FLAG_BG256));
1399 	if (fg_default || bg_default) {
1400 		/*
1401 		 * If don't have AX but do have op, send sgr0 (op can't
1402 		 * actually be used because it is sometimes the same as sgr0
1403 		 * and sometimes isn't). This resets both colours to default.
1404 		 *
1405 		 * Otherwise, try to set the default colour only as needed.
1406 		 */
1407 		have_ax = tty_term_has(tty->term, TTYC_AX);
1408 		if (!have_ax && tty_term_has(tty->term, TTYC_OP))
1409 			tty_reset(tty);
1410 		else {
1411 			if (fg_default &&
1412 			    (tc->fg != 8 || tc->flags & GRID_FLAG_FG256)) {
1413 				if (have_ax)
1414 					tty_puts(tty, "\033[39m");
1415 				else if (tc->fg != 7 ||
1416 				    tc->flags & GRID_FLAG_FG256)
1417 					tty_putcode1(tty, TTYC_SETAF, 7);
1418 				tc->fg = 8;
1419 				tc->flags &= ~GRID_FLAG_FG256;
1420 			}
1421 			if (bg_default &&
1422 			    (tc->bg != 8 || tc->flags & GRID_FLAG_BG256)) {
1423 				if (have_ax)
1424 					tty_puts(tty, "\033[49m");
1425 				else if (tc->bg != 0 ||
1426 				    tc->flags & GRID_FLAG_BG256)
1427 					tty_putcode1(tty, TTYC_SETAB, 0);
1428 				tc->bg = 8;
1429 				tc->flags &= ~GRID_FLAG_BG256;
1430 			}
1431 		}
1432 	}
1433 
1434 	/* Set the foreground colour. */
1435 	if (!fg_default && (fg != tc->fg ||
1436 	    ((flags & GRID_FLAG_FG256) != (tc->flags & GRID_FLAG_FG256))))
1437 		tty_colours_fg(tty, gc);
1438 
1439 	/*
1440 	 * Set the background colour. This must come after the foreground as
1441 	 * tty_colour_fg() can call tty_reset().
1442 	 */
1443 	if (!bg_default && (bg != tc->bg ||
1444 	    ((flags & GRID_FLAG_BG256) != (tc->flags & GRID_FLAG_BG256))))
1445 		tty_colours_bg(tty, gc);
1446 }
1447 
1448 void
tty_check_fg(struct tty * tty,struct grid_cell * gc)1449 tty_check_fg(struct tty *tty, struct grid_cell *gc)
1450 {
1451 	u_int	colours;
1452 
1453 	/* Is this a 256-colour colour? */
1454 	if (gc->flags & GRID_FLAG_FG256) {
1455 		/* And not a 256 colour mode? */
1456 		if (!(tty->term->flags & TERM_256COLOURS) &&
1457 		    !(tty->term_flags & TERM_256COLOURS)) {
1458 			gc->fg = colour_256to16(gc->fg);
1459 			if (gc->fg & 8) {
1460 				gc->fg &= 7;
1461 				gc->attr |= GRID_ATTR_BRIGHT;
1462 			} else
1463 				gc->attr &= ~GRID_ATTR_BRIGHT;
1464 			gc->flags &= ~GRID_FLAG_FG256;
1465 		}
1466 		return;
1467 	}
1468 
1469 	/* Is this an aixterm colour? */
1470 	colours = tty_term_number(tty->term, TTYC_COLORS);
1471 	if (gc->fg >= 90 && gc->fg <= 97 && colours < 16) {
1472 		gc->fg -= 90;
1473 		gc->attr |= GRID_ATTR_BRIGHT;
1474 	}
1475 }
1476 
1477 void
tty_check_bg(struct tty * tty,struct grid_cell * gc)1478 tty_check_bg(struct tty *tty, struct grid_cell *gc)
1479 {
1480 	u_int	colours;
1481 
1482 	/* Is this a 256-colour colour? */
1483 	if (gc->flags & GRID_FLAG_BG256) {
1484 		/*
1485 		 * And not a 256 colour mode? Translate to 16-colour
1486 		 * palette. Bold background doesn't exist portably, so just
1487 		 * discard the bold bit if set.
1488 		 */
1489 		if (!(tty->term->flags & TERM_256COLOURS) &&
1490 		    !(tty->term_flags & TERM_256COLOURS)) {
1491 			gc->bg = colour_256to16(gc->bg);
1492 			if (gc->bg & 8)
1493 				gc->bg &= 7;
1494 			gc->attr &= ~GRID_ATTR_BRIGHT;
1495 			gc->flags &= ~GRID_FLAG_BG256;
1496 		}
1497 		return;
1498 	}
1499 
1500 	/* Is this an aixterm colour? */
1501 	colours = tty_term_number(tty->term, TTYC_COLORS);
1502 	if (gc->bg >= 90 && gc->bg <= 97 && colours < 16) {
1503 		gc->bg -= 90;
1504 		gc->attr |= GRID_ATTR_BRIGHT;
1505 	}
1506 }
1507 
1508 void
tty_colours_fg(struct tty * tty,const struct grid_cell * gc)1509 tty_colours_fg(struct tty *tty, const struct grid_cell *gc)
1510 {
1511 	struct grid_cell	*tc = &tty->cell;
1512 	u_char			 fg = gc->fg;
1513 	char			 s[32];
1514 
1515 	/* Is this a 256-colour colour? */
1516 	if (gc->flags & GRID_FLAG_FG256) {
1517 		/* Try as 256 colours. */
1518 		if (tty_try_256(tty, fg, "38") == 0)
1519 			goto save_fg;
1520 		/* Else already handled by tty_check_fg. */
1521 		return;
1522 	}
1523 
1524 	/* Is this an aixterm bright colour? */
1525 	if (fg >= 90 && fg <= 97) {
1526 		xsnprintf(s, sizeof s, "\033[%dm", fg);
1527 		tty_puts(tty, s);
1528 		goto save_fg;
1529 	}
1530 
1531 	/* Otherwise set the foreground colour. */
1532 	tty_putcode1(tty, TTYC_SETAF, fg);
1533 
1534 save_fg:
1535 	/* Save the new values in the terminal current cell. */
1536 	tc->fg = fg;
1537 	tc->flags &= ~GRID_FLAG_FG256;
1538 	tc->flags |= gc->flags & GRID_FLAG_FG256;
1539 }
1540 
1541 void
tty_colours_bg(struct tty * tty,const struct grid_cell * gc)1542 tty_colours_bg(struct tty *tty, const struct grid_cell *gc)
1543 {
1544 	struct grid_cell	*tc = &tty->cell;
1545 	u_char			 bg = gc->bg;
1546 	char			 s[32];
1547 
1548 	/* Is this a 256-colour colour? */
1549 	if (gc->flags & GRID_FLAG_BG256) {
1550 		/* Try as 256 colours. */
1551 		if (tty_try_256(tty, bg, "48") == 0)
1552 			goto save_bg;
1553 		/* Else already handled by tty_check_bg. */
1554 		return;
1555 	}
1556 
1557 	/* Is this an aixterm bright colour? */
1558 	if (bg >= 90 && bg <= 97) {
1559 		/* 16 colour terminals or above only. */
1560 		if (tty_term_number(tty->term, TTYC_COLORS) >= 16) {
1561 			xsnprintf(s, sizeof s, "\033[%dm", bg + 10);
1562 			tty_puts(tty, s);
1563 			goto save_bg;
1564 		}
1565 		bg -= 90;
1566 		/* no such thing as a bold background */
1567 	}
1568 
1569 	/* Otherwise set the background colour. */
1570 	tty_putcode1(tty, TTYC_SETAB, bg);
1571 
1572 save_bg:
1573 	/* Save the new values in the terminal current cell. */
1574 	tc->bg = bg;
1575 	tc->flags &= ~GRID_FLAG_BG256;
1576 	tc->flags |= gc->flags & GRID_FLAG_BG256;
1577 }
1578 
1579 int
tty_try_256(struct tty * tty,u_char colour,const char * type)1580 tty_try_256(struct tty *tty, u_char colour, const char *type)
1581 {
1582 	char	s[32];
1583 
1584 	/*
1585 	 * If the terminfo entry has 256 colours, assume that setaf and setab
1586 	 * work correctly.
1587 	 */
1588 	if (tty->term->flags & TERM_256COLOURS) {
1589 		if (*type == '3')
1590 			tty_putcode1(tty, TTYC_SETAF, colour);
1591 		else
1592 			tty_putcode1(tty, TTYC_SETAB, colour);
1593 		return (0);
1594 	}
1595 
1596 	/*
1597 	 * If the user has specified -2 to the client, setaf and setab may not
1598 	 * work, so send the usual sequence.
1599 	 */
1600 	if (tty->term_flags & TERM_256COLOURS) {
1601 		xsnprintf(s, sizeof s, "\033[%s;5;%hhum", type, colour);
1602 		tty_puts(tty, s);
1603 		return (0);
1604 	}
1605 
1606 	return (-1);
1607 }
1608 
1609 void
tty_bell(struct tty * tty)1610 tty_bell(struct tty *tty)
1611 {
1612 	tty_putcode(tty, TTYC_BEL);
1613 }
1614