Implement write_async_safe for mi_console_file (PR 22299)
[external/binutils.git] / gdb / mi / mi-console.c
index 43d5388..248d070 100644 (file)
@@ -48,6 +48,34 @@ mi_console_file::write (const char *buf, long length_buf)
     this->flush ();
 }
 
+/* Write C to STREAM's in an async-safe way.  */
+
+static int
+do_fputc_async_safe (int c, ui_file *stream)
+{
+  char ch = c;
+  stream->write_async_safe (&ch, 1);
+  return c;
+}
+
+void
+mi_console_file::write_async_safe (const char *buf, long length_buf)
+{
+  m_raw->write_async_safe (m_prefix, strlen (m_prefix));
+  if (m_quote)
+    {
+      m_raw->write_async_safe (&m_quote, 1);
+      fputstrn_unfiltered (buf, length_buf, m_quote, do_fputc_async_safe,
+                          m_raw);
+      m_raw->write_async_safe (&m_quote, 1);
+    }
+  else
+    fputstrn_unfiltered (buf, length_buf, 0, do_fputc_async_safe, m_raw);
+
+  char nl = '\n';
+  m_raw->write_async_safe (&nl, 1);
+}
+
 void
 mi_console_file::flush ()
 {
@@ -63,13 +91,14 @@ mi_console_file::flush ()
       if (m_quote)
        {
          fputc_unfiltered (m_quote, m_raw);
-         fputstrn_unfiltered (buf, length_buf, m_quote, m_raw);
+         fputstrn_unfiltered (buf, length_buf, m_quote, fputc_unfiltered,
+                              m_raw);
          fputc_unfiltered (m_quote, m_raw);
          fputc_unfiltered ('\n', m_raw);
        }
       else
        {
-         fputstrn_unfiltered (buf, length_buf, 0, m_raw);
+         fputstrn_unfiltered (buf, length_buf, 0, fputc_unfiltered, m_raw);
          fputc_unfiltered ('\n', m_raw);
        }
       gdb_flush (m_raw);