terminal-util: add new helper terminal_reset_defensive() that combines reset-by-ioctl and reset-by-sequence reasonably

This commit is contained in:
Lennart Poettering
2024-07-11 09:26:07 +02:00
parent ce3a1593bc
commit cfac09083b
8 changed files with 36 additions and 8 deletions

View File

@@ -1575,6 +1575,27 @@ int terminal_reset_ansi_seq(int fd) {
return k < 0 ? k : r;
}
int terminal_reset_defensive(int fd, bool switch_to_text) {
int r = 0;
assert(fd >= 0);
/* Resets the terminal comprehensively, but defensively. i.e. both resets the tty via ioctl()s and
* via ANSI sequences, but avoids the latter in case we are talking to a pty. That's a safety measure
* because ptys might be connected to shell pipelines where we cannot expect such ansi sequences to
* work. Given that ptys are generally short-lived (and not recycled) this restriction shouldn't hurt
* much.
*
* The specified fd should be open for *writing*! */
RET_GATHER(r, reset_terminal_fd(fd, switch_to_text));
if (terminal_is_pty_fd(fd) == 0)
RET_GATHER(r, terminal_reset_ansi_seq(fd));
return r;
}
void termios_disable_echo(struct termios *termios) {
assert(termios);

View File

@@ -98,6 +98,7 @@ bool isatty_safe(int fd);
int reset_terminal_fd(int fd, bool switch_to_text);
int reset_terminal(const char *name);
int terminal_reset_ansi_seq(int fd);
int terminal_reset_defensive(int fd, bool switch_to_text);
int terminal_set_cursor_position(int fd, unsigned row, unsigned column);