Refactor code to check for terminal sharing
[binutils-gdb.git] / gdb / mingw-hdep.c
index 651e795216dfc32f601b4500bedbf5444d9e56e8..9d0337cf4efa3066855b1bab3bb591254e485616 100644 (file)
@@ -21,8 +21,8 @@
 #include "main.h"
 #include "serial.h"
 #include "gdbsupport/event-loop.h"
-
 #include "gdbsupport/gdb_select.h"
+#include "inferior.h"
 
 #include <windows.h>
 
@@ -371,3 +371,32 @@ gdb_console_fputs (const char *linebuf, FILE *fstream)
   last_style = style;
   return 1;
 }
+
+/* See inferior.h.  */
+
+tribool
+sharing_input_terminal (int pid)
+{
+  std::vector<DWORD> results (10);
+  DWORD len = 0;
+  while (true)
+    {
+      len = GetConsoleProcessList (results.data (), results.size ());
+      /* Note that LEN == 0 is a failure, but we can treat it the same
+        as a "no".  */
+      if (len < results.size ())
+       break;
+
+      results.resize (len);
+    }
+  /* In case the vector was too big.  */
+  results.resize (len);
+  if (std::find (results.begin (), results.end (), pid) != results.end ())
+    {
+      /* The pid is in the list sharing the console, so don't
+        interrupt the inferior -- it will get the signal itself.  */
+      return TRIBOOL_TRUE;
+    }
+
+  return TRIBOOL_FALSE;
+}