[gnome-builder/wip/gtk4-port: 155/1774] libide/io: move shell helper to libide-io




commit 9a90e1f802ba558f767d9799cde903e123531fe9
Author: Christian Hergert <chergert redhat com>
Date:   Tue Mar 29 03:33:02 2022 -0700

    libide/io: move shell helper to libide-io
    
    This gives us access to it at a lower level, with the only cost being that
    we can't take the Vte value from here. But since we were only using that
    in the race condition anyway and we get the value at startup, it shouldn't
    really impact things.

 .../ide-shell-private.h}                           |   4 +-
 src/libide/io/ide-shell.c                          |  97 +++++++++++++++++-
 src/libide/io/ide-shell.h                          |  10 +-
 src/libide/io/meson.build                          |   1 +
 src/libide/terminal/ide-terminal-launcher.c        |   2 +-
 src/libide/terminal/ide-terminal-surface.c         | 108 ---------------------
 src/libide/terminal/ide-terminal-surface.h         |  39 --------
 src/libide/terminal/ide-terminal-surface.ui        |  10 --
 src/libide/terminal/ide-terminal-util.c            | 103 +-------------------
 src/libide/terminal/ide-terminal-util.h            |   4 +-
 src/libide/terminal/meson.build                    |   1 -
 11 files changed, 108 insertions(+), 271 deletions(-)
---
diff --git a/src/libide/terminal/ide-terminal-private.h b/src/libide/io/ide-shell-private.h
similarity index 94%
rename from src/libide/terminal/ide-terminal-private.h
rename to src/libide/io/ide-shell-private.h
index 9b53f3b25..39b511ffd 100644
--- a/src/libide/terminal/ide-terminal-private.h
+++ b/src/libide/io/ide-shell-private.h
@@ -1,4 +1,4 @@
-/* ide-terminal-private.h
+/* ide-shell-private.h
  *
  * Copyright 2018-2019 Christian Hergert <chergert redhat com>
  *
@@ -20,7 +20,7 @@
 
 #pragma once
 
-#include <glib.h>
+#include "ide-shell.h"
 
 G_BEGIN_DECLS
 
diff --git a/src/libide/io/ide-shell.c b/src/libide/io/ide-shell.c
index 12f9ecb9a..d7625f031 100644
--- a/src/libide/io/ide-shell.c
+++ b/src/libide/io/ide-shell.c
@@ -18,9 +18,15 @@
  * SPDX-License-Identifier: GPL-3.0-or-later
  */
 
+#define G_LOG_DOMAIN "ide-shell"
+
 #include "config.h"
 
-#include "ide-shell.h"
+#include <libide-threading.h>
+
+#include "ide-shell-private.h"
+
+static const char *user_shell = "/bin/sh";
 
 gboolean
 ide_shell_supports_dash_c (const char *shell)
@@ -45,3 +51,92 @@ ide_shell_supports_dash_login (const char *shell)
          strcmp (shell, "zsh") == 0 || g_str_has_suffix (shell, "/zsh") ||
          strcmp (shell, "sh") == 0 || g_str_has_suffix (shell, "/sh");
 }
+
+static void
+ide_guess_shell_communicate_cb (GObject      *object,
+                                GAsyncResult *result,
+                                gpointer      user_data)
+{
+  IdeSubprocess *subprocess = (IdeSubprocess *)object;
+  g_autoptr(GError) error = NULL;
+  g_autofree gchar *stdout_buf = NULL;
+
+  g_assert (IDE_IS_SUBPROCESS (subprocess));
+  g_assert (G_IS_ASYNC_RESULT (result));
+  g_assert (user_data == NULL);
+
+  if (!ide_subprocess_communicate_utf8_finish (subprocess, result, &stdout_buf, NULL, &error))
+    {
+      g_warning ("Failed to parse result from getent: %s", error->message);
+      return;
+    }
+
+  if (stdout_buf != NULL)
+    {
+      g_strstrip (stdout_buf);
+
+      if (stdout_buf[0] == '/')
+        user_shell = g_steal_pointer (&stdout_buf);
+    }
+}
+
+void
+_ide_guess_shell (void)
+{
+  g_autoptr(IdeSubprocessLauncher) launcher = NULL;
+  g_autoptr(IdeSubprocess) subprocess = NULL;
+  g_autofree gchar *command = NULL;
+  g_autoptr(GError) error = NULL;
+  g_auto(GStrv) argv = NULL;
+  g_autofree gchar *shell = NULL;
+
+  command = g_strdup_printf ("sh -c 'getent passwd %s | head -n1 | cut -f 7 -d :'",
+                             g_get_user_name ());
+
+  if (!g_shell_parse_argv (command, NULL, &argv, &error))
+    {
+      g_warning ("Failed to parse command into argv: %s",
+                 error ? error->message : "unknown error");
+      return;
+    }
+
+  /*
+   * We don't use the runtime shell here, because we want to know
+   * what the host thinks the user shell should be.
+   */
+  launcher = ide_subprocess_launcher_new (G_SUBPROCESS_FLAGS_STDOUT_PIPE);
+
+  ide_subprocess_launcher_set_run_on_host (launcher, TRUE);
+  ide_subprocess_launcher_set_clear_env (launcher, FALSE);
+  ide_subprocess_launcher_set_cwd (launcher, g_get_home_dir ());
+  ide_subprocess_launcher_push_args (launcher, (const gchar * const *)argv);
+
+  if (!(subprocess = ide_subprocess_launcher_spawn (launcher, NULL, &error)))
+    {
+      g_warning ("Failed to spawn getent: %s", error->message);
+      return;
+    }
+
+  ide_subprocess_communicate_utf8_async (subprocess,
+                                         NULL,
+                                         NULL,
+                                         ide_guess_shell_communicate_cb,
+                                         NULL);
+}
+
+/**
+ * ide_get_user_shell:
+ *
+ * Gets the user preferred shell on the host.
+ *
+ * If the background shell discovery has not yet finished due to
+ * slow or misconfigured getent on the host, this will provide a
+ * sensible fallback.
+ *
+ * Returns: (not nullable): a shell such as "/bin/sh"
+ */
+const gchar *
+ide_get_user_shell (void)
+{
+  return user_shell;
+}
diff --git a/src/libide/io/ide-shell.h b/src/libide/io/ide-shell.h
index ac086bae5..b47d49d6a 100644
--- a/src/libide/io/ide-shell.h
+++ b/src/libide/io/ide-shell.h
@@ -24,9 +24,11 @@
 
 G_BEGIN_DECLS
 
-IDE_AVAILABLE_IN_42
-gboolean ide_shell_supports_dash_c     (const char *shell);
-IDE_AVAILABLE_IN_42
-gboolean ide_shell_supports_dash_login (const char *shell);
+IDE_AVAILABLE_IN_ALL
+gboolean    ide_shell_supports_dash_c     (const char *shell);
+IDE_AVAILABLE_IN_ALL
+gboolean    ide_shell_supports_dash_login (const char *shell);
+IDE_AVAILABLE_IN_ALL
+const char *ide_get_user_shell            (void);
 
 G_END_DECLS
diff --git a/src/libide/io/meson.build b/src/libide/io/meson.build
index 4e2bb166d..dfaa4eddc 100644
--- a/src/libide/io/meson.build
+++ b/src/libide/io/meson.build
@@ -23,6 +23,7 @@ libide_io_public_headers = [
 
 libide_io_private_headers = [
   'ide-gfile-private.h',
+  'ide-shell-private.h',
 ]
 
 install_headers(libide_io_public_headers, subdir: libide_io_header_subdir)
diff --git a/src/libide/terminal/ide-terminal-launcher.c b/src/libide/terminal/ide-terminal-launcher.c
index 7c8e76c26..c311d9a7b 100644
--- a/src/libide/terminal/ide-terminal-launcher.c
+++ b/src/libide/terminal/ide-terminal-launcher.c
@@ -504,7 +504,7 @@ ide_terminal_launcher_spawn_async (IdeTerminalLauncher *self,
   task = ide_task_new (self, cancellable, callback, user_data);
   ide_task_set_source_tag (task, ide_terminal_launcher_spawn_async);
 
-  if ((pty_fd = ide_vte_pty_create_slave (pty)) == -1)
+  if ((pty_fd = ide_vte_pty_create_producer (pty)) == -1)
     {
       int errsv = errno;
       ide_task_return_new_error (task,
diff --git a/src/libide/terminal/ide-terminal-util.c b/src/libide/terminal/ide-terminal-util.c
index 1f2370eed..fba13af4d 100644
--- a/src/libide/terminal/ide-terminal-util.c
+++ b/src/libide/terminal/ide-terminal-util.c
@@ -29,13 +29,10 @@
 #include <unistd.h>
 #include <vte/vte.h>
 
-#include "ide-terminal-private.h"
 #include "ide-terminal-util.h"
 
-static const gchar *user_shell = "/bin/sh";
-
 gint
-ide_vte_pty_create_slave (VtePty *pty)
+ide_vte_pty_create_producer (VtePty *pty)
 {
   gint master_fd;
 
@@ -47,101 +44,3 @@ ide_vte_pty_create_slave (VtePty *pty)
 
   return ide_pty_intercept_create_slave (master_fd, TRUE);
 }
-
-/**
- * ide_get_user_shell:
- *
- * Gets the user preferred shell on the host.
- *
- * If the background shell discovery has not yet finished due to
- * slow or misconfigured getent on the host, this will provide a
- * sensible fallback.
- *
- * Returns: (not nullable): a shell such as "/bin/sh"
- *
- * Since: 3.32
- */
-const gchar *
-ide_get_user_shell (void)
-{
-  return user_shell;
-}
-
-static void
-ide_guess_shell_communicate_cb (GObject      *object,
-                                GAsyncResult *result,
-                                gpointer      user_data)
-{
-  IdeSubprocess *subprocess = (IdeSubprocess *)object;
-  g_autoptr(GError) error = NULL;
-  g_autofree gchar *stdout_buf = NULL;
-
-  g_assert (IDE_IS_SUBPROCESS (subprocess));
-  g_assert (G_IS_ASYNC_RESULT (result));
-  g_assert (user_data == NULL);
-
-  if (!ide_subprocess_communicate_utf8_finish (subprocess, result, &stdout_buf, NULL, &error))
-    {
-      g_warning ("Failed to parse result from getent: %s", error->message);
-      return;
-    }
-
-  if (stdout_buf != NULL)
-    {
-      g_strstrip (stdout_buf);
-
-      if (stdout_buf[0] == '/')
-        user_shell = g_steal_pointer (&stdout_buf);
-    }
-}
-
-void
-_ide_guess_shell (void)
-{
-  g_autoptr(IdeSubprocessLauncher) launcher = NULL;
-  g_autoptr(IdeSubprocess) subprocess = NULL;
-  g_autofree gchar *command = NULL;
-  g_autoptr(GError) error = NULL;
-  g_auto(GStrv) argv = NULL;
-  g_autofree gchar *shell = NULL;
-
-  /*
-   * First ask VTE to guess, so we can use that while we discover
-   * the real shell asynchronously (and possibly outside the container).
-   */
-  if ((shell = vte_get_user_shell ()))
-    user_shell = g_strdup (shell);
-
-  command = g_strdup_printf ("sh -c 'getent passwd %s | head -n1 | cut -f 7 -d :'",
-                             g_get_user_name ());
-
-  if (!g_shell_parse_argv (command, NULL, &argv, &error))
-    {
-      g_warning ("Failed to parse command into argv: %s",
-                 error ? error->message : "unknown error");
-      return;
-    }
-
-  /*
-   * We don't use the runtime shell here, because we want to know
-   * what the host thinks the user shell should be.
-   */
-  launcher = ide_subprocess_launcher_new (G_SUBPROCESS_FLAGS_STDOUT_PIPE);
-
-  ide_subprocess_launcher_set_run_on_host (launcher, TRUE);
-  ide_subprocess_launcher_set_clear_env (launcher, FALSE);
-  ide_subprocess_launcher_set_cwd (launcher, g_get_home_dir ());
-  ide_subprocess_launcher_push_args (launcher, (const gchar * const *)argv);
-
-  if (!(subprocess = ide_subprocess_launcher_spawn (launcher, NULL, &error)))
-    {
-      g_warning ("Failed to spawn getent: %s", error->message);
-      return;
-    }
-
-  ide_subprocess_communicate_utf8_async (subprocess,
-                                         NULL,
-                                         NULL,
-                                         ide_guess_shell_communicate_cb,
-                                         NULL);
-}
diff --git a/src/libide/terminal/ide-terminal-util.h b/src/libide/terminal/ide-terminal-util.h
index a07b35efc..d3b55be22 100644
--- a/src/libide/terminal/ide-terminal-util.h
+++ b/src/libide/terminal/ide-terminal-util.h
@@ -30,8 +30,6 @@
 G_BEGIN_DECLS
 
 IDE_AVAILABLE_IN_3_32
-int          ide_vte_pty_create_slave (VtePty *pty);
-IDE_AVAILABLE_IN_3_32
-const gchar *ide_get_user_shell       (void);
+int ide_vte_pty_create_producer (VtePty *pty);
 
 G_END_DECLS
diff --git a/src/libide/terminal/meson.build b/src/libide/terminal/meson.build
index 8caa0bb94..e1282707f 100644
--- a/src/libide/terminal/meson.build
+++ b/src/libide/terminal/meson.build
@@ -32,7 +32,6 @@ libide_terminal_private_headers = [
   'ide-terminal-page-actions.h',
   'ide-terminal-page-private.h',
   'ide-terminal-popover-row.h',
-  'ide-terminal-private.h',
   'ide-terminal-search-private.h',
 ]
 


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