From b038600562c1a3ccc6e63eca145db06cc521c13d Mon Sep 17 00:00:00 2001 From: nicm Date: Tue, 15 Feb 2022 13:03:02 +0000 Subject: [PATCH] Do not create a buffer from an OSC 52 response if we have not sent a query. --- usr.bin/tmux/cmd-refresh-client.c | 4 ++-- usr.bin/tmux/tmux.h | 6 ++++-- usr.bin/tmux/tty-keys.c | 7 ++++++- usr.bin/tmux/tty.c | 29 +++++++++++++++++++++++++++-- 4 files changed, 39 insertions(+), 7 deletions(-) diff --git a/usr.bin/tmux/cmd-refresh-client.c b/usr.bin/tmux/cmd-refresh-client.c index dada29cc8aa..696d5717e26 100644 --- a/usr.bin/tmux/cmd-refresh-client.c +++ b/usr.bin/tmux/cmd-refresh-client.c @@ -1,4 +1,4 @@ -/* $OpenBSD: cmd-refresh-client.c,v 1.45 2021/08/27 17:15:57 nicm Exp $ */ +/* $OpenBSD: cmd-refresh-client.c,v 1.46 2022/02/15 13:03:02 nicm Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott @@ -225,7 +225,7 @@ cmd_refresh_client_exec(struct cmd *self, struct cmdq_item *item) } if (args_has(args, 'l')) { - tty_putcode_ptr2(&tc->tty, TTYC_MS, "", "?"); + tty_send_osc52_query(&tc->tty); return (CMD_RETURN_NORMAL); } diff --git a/usr.bin/tmux/tmux.h b/usr.bin/tmux/tmux.h index 618d530a7d2..98887d1b1aa 100644 --- a/usr.bin/tmux/tmux.h +++ b/usr.bin/tmux/tmux.h @@ -1,4 +1,4 @@ -/* $OpenBSD: tmux.h,v 1.1158 2022/02/01 14:46:42 nicm Exp $ */ +/* $OpenBSD: tmux.h,v 1.1159 2022/02/15 13:03:02 nicm Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott @@ -1277,6 +1277,7 @@ LIST_HEAD(tty_terms, tty_term); struct tty { struct client *client; struct event start_timer; + struct event query_timer; u_int sx; u_int sy; @@ -1320,7 +1321,7 @@ struct tty { #define TTY_NOBLOCK 0x8 #define TTY_STARTED 0x10 #define TTY_OPENED 0x20 -/* 0x40 unused */ +#define TTY_OSC52QUERY 0x40 #define TTY_BLOCK 0x80 #define TTY_HAVEDA 0x100 #define TTY_HAVEXDA 0x200 @@ -2173,6 +2174,7 @@ void tty_reset(struct tty *); void tty_region_off(struct tty *); void tty_margin_off(struct tty *); void tty_cursor(struct tty *, u_int, u_int); +void tty_send_osc52_query(struct tty *); void tty_putcode(struct tty *, enum tty_code_code); void tty_putcode1(struct tty *, enum tty_code_code, int); void tty_putcode2(struct tty *, enum tty_code_code, int, int); diff --git a/usr.bin/tmux/tty-keys.c b/usr.bin/tmux/tty-keys.c index f01ab9a2407..9519b2ab16a 100644 --- a/usr.bin/tmux/tty-keys.c +++ b/usr.bin/tmux/tty-keys.c @@ -1,4 +1,4 @@ -/* $OpenBSD: tty-keys.c,v 1.151 2021/10/21 08:36:51 nicm Exp $ */ +/* $OpenBSD: tty-keys.c,v 1.152 2022/02/15 13:03:02 nicm Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott @@ -1217,6 +1217,11 @@ tty_keys_clipboard(__unused struct tty *tty, const char *buf, size_t len, buf++; end--; + /* If we did not request this, ignore it. */ + if (~tty->flags & TTY_OSC52QUERY) + return (0); + tty->flags &= ~TTY_OSC52QUERY; + /* It has to be a string so copy it. */ copy = xmalloc(end + 1); memcpy(copy, buf, end); diff --git a/usr.bin/tmux/tty.c b/usr.bin/tmux/tty.c index 63274052d00..253fc10e562 100644 --- a/usr.bin/tmux/tty.c +++ b/usr.bin/tmux/tty.c @@ -1,4 +1,4 @@ -/* $OpenBSD: tty.c,v 1.415 2021/12/31 11:35:49 nicm Exp $ */ +/* $OpenBSD: tty.c,v 1.416 2022/02/15 13:03:02 nicm Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott @@ -83,6 +83,8 @@ static void tty_check_overlay_range(struct tty *, u_int, u_int, u_int, #define TTY_BLOCK_START(tty) (1 + ((tty)->sx * (tty)->sy) * 8) #define TTY_BLOCK_STOP(tty) (1 + ((tty)->sx * (tty)->sy) / 8) +#define TTY_QUERY_TIMEOUT 5 + void tty_create_log(void) { @@ -307,7 +309,7 @@ tty_start_tty(struct tty *tty) { struct client *c = tty->client; struct termios tio; - struct timeval tv = { .tv_sec = 3 }; + struct timeval tv = { .tv_sec = TTY_QUERY_TIMEOUT }; setblocking(c->fd, 0); event_add(&tty->event_in, NULL); @@ -2917,3 +2919,26 @@ tty_default_attributes(struct tty *tty, const struct grid_cell *defaults, gc.bg = bg; tty_attributes(tty, &gc, defaults, palette); } + +static void +tty_query_timer_callback(__unused int fd, __unused short events, void *data) +{ + struct tty *tty = data; + + tty->flags &= ~TTY_OSC52QUERY; +} + +void +tty_send_osc52_query(struct tty *tty) +{ + struct timeval tv = { .tv_sec = TTY_QUERY_TIMEOUT }; + + if ((~tty->flags & TTY_STARTED) || (tty->flags & TTY_OSC52QUERY)) + return; + tty_putcode_ptr2(tty, TTYC_MS, "", "?"); + tty->flags |= TTY_OSC52QUERY; + + evtimer_set(&tty->query_timer, tty_query_timer_callback, tty); + evtimer_add(&tty->query_timer, &tv); +} + -- 2.20.1