Refactor code to check for terminal sharing

This refactors the code to check for terminal sharing.
is_gdb_terminal is exported, and sharing_input_terminal_1 is renamed,
slightly refactored, and moved to posix-hdep.c.  A new
Windows-specific implementation of this function is added to
mingw-hdep.c.

MSDN has a warning about GetConsoleProcessList

    This API is not recommended and does not have a virtual terminal
    equivalent. [...] Applications remoting via cross-platform
    utilities and transports like SSH may not work as expected if
    using this API.

However, we believe this isn't likely to be an issue for gdb.
This commit is contained in:
Tom Tromey
2022-12-05 11:15:09 -07:00
parent d2f803afd5
commit c1dc47f53c
4 changed files with 63 additions and 26 deletions

View File

@@ -212,10 +212,9 @@ gdb_save_tty_state (void)
}
}
/* Try to determine whether TTY is GDB's input terminal. Returns
TRIBOOL_UNKNOWN if we can't tell. */
/* See inferior.h. */
static tribool
tribool
is_gdb_terminal (const char *tty)
{
struct stat gdb_tty;
@@ -236,26 +235,6 @@ is_gdb_terminal (const char *tty)
: TRIBOOL_FALSE);
}
/* Helper for sharing_input_terminal. Try to determine whether
inferior INF is using the same TTY for input as GDB is. Returns
TRIBOOL_UNKNOWN if we can't tell. */
static tribool
sharing_input_terminal_1 (inferior *inf)
{
/* Using host-dependent code here is fine, because the
child_terminal_foo functions are meant to be used by child/native
targets. */
#if defined (__linux__) || defined (__sun__)
char buf[100];
xsnprintf (buf, sizeof (buf), "/proc/%d/fd/0", inf->pid);
return is_gdb_terminal (buf);
#else
return TRIBOOL_UNKNOWN;
#endif
}
/* Return true if the inferior is using the same TTY for input as GDB
is. If this is true, then we save/restore terminal flags/state.
@@ -287,7 +266,7 @@ sharing_input_terminal (inferior *inf)
{
terminal_info *tinfo = get_inflow_inferior_data (inf);
tribool res = sharing_input_terminal_1 (inf);
tribool res = sharing_input_terminal (inf->pid);
if (res == TRIBOOL_UNKNOWN)
{