[gnome-builder/wip/gtk4-port] libide/foundry: add PTY helper to set stdin/out/err



commit 6bf5ca807248800bac180198436980a7a186e677
Author: Christian Hergert <chergert redhat com>
Date:   Tue Jun 21 17:03:13 2022 -0700

    libide/foundry: add PTY helper to set stdin/out/err

 src/libide/foundry/ide-run-context.c | 53 ++++++++++++++++++++++++++++++++++++
 src/libide/foundry/ide-run-context.h |  3 ++
 2 files changed, 56 insertions(+)
---
diff --git a/src/libide/foundry/ide-run-context.c b/src/libide/foundry/ide-run-context.c
index e0a6dac65..c131fe4f7 100644
--- a/src/libide/foundry/ide-run-context.c
+++ b/src/libide/foundry/ide-run-context.c
@@ -22,7 +22,11 @@
 
 #include "config.h"
 
+#include <errno.h>
 #include <string.h>
+#include <unistd.h>
+
+#include <libide-io.h>
 
 #include "ide-run-context.h"
 
@@ -675,3 +679,52 @@ ide_run_context_merge_unix_fd_map (IdeRunContext  *self,
 
   return ide_unix_fd_map_steal_from (layer->unix_fd_map, unix_fd_map, error);
 }
+
+/**
+ * ide_run_context_set_pty:
+ * @self: an #IdeRunContext
+ * @consumer_fd: the FD of the PTY controller
+ *
+ * Sets up a PTY for the run context that will communicate with the
+ * consumer. It is set for stdin/stdout/stderr.
+ */
+void
+ide_run_context_set_pty (IdeRunContext *self,
+                         int            consumer_fd)
+{
+  int stdin_fd = -1;
+  int stdout_fd = -1;
+  int stderr_fd = -1;
+
+  g_return_if_fail (IDE_IS_RUN_CONTEXT (self));
+
+  if (consumer_fd < 0)
+    return;
+
+  if (-1 == (stdin_fd = ide_pty_intercept_create_producer (consumer_fd, TRUE)))
+    {
+      int errsv = errno;
+      g_critical ("Failed to create PTY device: %s", g_strerror (errsv));
+      return;
+    }
+
+  if (-1 == (stdout_fd = dup (stdin_fd)))
+    {
+      int errsv = errno;
+      g_critical ("Failed to dup stdout FD: %s", g_strerror (errsv));
+    }
+
+  if (-1 == (stderr_fd = dup (stdin_fd)))
+    {
+      int errsv = errno;
+      g_critical ("Failed to dup stderr FD: %s", g_strerror (errsv));
+    }
+
+  g_assert (stdin_fd > -1);
+  g_assert (stdout_fd > -1);
+  g_assert (stderr_fd > -1);
+
+  ide_run_context_take_fd (self, stdin_fd, STDIN_FILENO);
+  ide_run_context_take_fd (self, stdout_fd, STDOUT_FILENO);
+  ide_run_context_take_fd (self, stderr_fd, STDERR_FILENO);
+}
diff --git a/src/libide/foundry/ide-run-context.h b/src/libide/foundry/ide-run-context.h
index d7dbe8640..76810c330 100644
--- a/src/libide/foundry/ide-run-context.h
+++ b/src/libide/foundry/ide-run-context.h
@@ -74,6 +74,9 @@ IDE_AVAILABLE_IN_ALL
 void                   ide_run_context_set_cwd            (IdeRunContext         *self,
                                                            const char            *cwd);
 IDE_AVAILABLE_IN_ALL
+void                   ide_run_context_set_pty            (IdeRunContext         *self,
+                                                           int                    consumer_fd);
+IDE_AVAILABLE_IN_ALL
 void                   ide_run_context_take_fd            (IdeRunContext         *self,
                                                            int                    source_fd,
                                                            int                    dest_fd);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]