[gnome-terminal/wip/search: 1/2] screen: Allow getting the cmdline of the current foreground process



commit cb24616ef2610087aadc8d7ff03e73be38217ca4
Author: Debarshi Ray <debarshir gnome org>
Date:   Tue Dec 10 17:14:29 2013 +0100

    screen: Allow getting the cmdline of the current foreground process
    
    This would further enhance our ability to search based on the current
    foreground process.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=711075

 src/terminal-screen.c |   25 +++++++++++++++++++++----
 src/terminal-screen.h |    3 ++-
 src/terminal-window.c |    4 ++--
 3 files changed, 25 insertions(+), 7 deletions(-)
---
diff --git a/src/terminal-screen.c b/src/terminal-screen.c
index a2c7dad..9c30271 100644
--- a/src/terminal-screen.c
+++ b/src/terminal-screen.c
@@ -1952,6 +1952,7 @@ terminal_screen_check_match (TerminalScreen *screen,
  * terminal_screen_has_foreground_process:
  * @screen:
  * @process_name: (out) (allow-none): the basename of the program, or %NULL
+ * @cmdline: (out) (allow-none): the full command line, or %NULL
  *
  * Checks whether there's a foreground process running in
  * this terminal.
@@ -1960,13 +1961,16 @@ terminal_screen_check_match (TerminalScreen *screen,
  */
 gboolean
 terminal_screen_has_foreground_process (TerminalScreen *screen,
-                                        char           **process_name)
+                                        char           **process_name,
+                                        char           **cmdline)
 {
   TerminalScreenPrivate *priv = screen->priv;
-  gs_free char *cmdline = NULL;
+  gs_free char *command = NULL;
+  gs_free char *data = NULL;
   gs_free char *basename = NULL;
   gs_free char *name = NULL;
   char filename[64];
+  gsize i;
   gsize len;
   int fgpid;
 
@@ -1978,10 +1982,10 @@ terminal_screen_has_foreground_process (TerminalScreen *screen,
     return FALSE;
 
   g_snprintf (filename, sizeof (filename), "/proc/%d/cmdline", fgpid);
-  if (!g_file_get_contents (filename, &cmdline, &len, NULL))
+  if (!g_file_get_contents (filename, &data, &len, NULL))
     return TRUE;
 
-  basename = g_path_get_basename (cmdline);
+  basename = g_path_get_basename (data);
   if (!basename)
     return TRUE;
 
@@ -1992,6 +1996,19 @@ terminal_screen_has_foreground_process (TerminalScreen *screen,
   if (process_name)
     gs_transfer_out_value (process_name, &name);
 
+  for (i = 0; i < len - 1; i++)
+    {
+      if (data[i] == '\0')
+        data[i] = ' ';
+    }
+
+  command = g_filename_to_utf8 (data, -1, NULL, NULL, NULL);
+  if (!command)
+    return TRUE;
+
+  if (cmdline)
+    gs_transfer_out_value (cmdline, &command);
+
   return TRUE;
 }
 
diff --git a/src/terminal-screen.h b/src/terminal-screen.h
index 03f6e99..5c8207c 100644
--- a/src/terminal-screen.h
+++ b/src/terminal-screen.h
@@ -134,7 +134,8 @@ void terminal_screen_save_config (TerminalScreen *screen,
 void terminal_screen_update_style (TerminalScreen *screen);
 
 gboolean terminal_screen_has_foreground_process (TerminalScreen *screen,
-                                                 char           **process_name);
+                                                 char           **process_name,
+                                                 char           **cmdline);
 
 /* Allow scales a bit smaller and a bit larger than the usual pango ranges */
 #define TERMINAL_SCALE_XXX_SMALL   (PANGO_SCALE_XX_SMALL/1.2)
diff --git a/src/terminal-window.c b/src/terminal-window.c
index 4176f86..59ba30e 100644
--- a/src/terminal-window.c
+++ b/src/terminal-window.c
@@ -3532,7 +3532,7 @@ confirm_close_window_or_tab (TerminalWindow *window,
 
   if (screen)
     {
-      do_confirm = terminal_screen_has_foreground_process (screen, NULL);
+      do_confirm = terminal_screen_has_foreground_process (screen, NULL, NULL);
       n_tabs = 1;
     }
   else
@@ -3549,7 +3549,7 @@ confirm_close_window_or_tab (TerminalWindow *window,
           TerminalScreen *terminal_screen;
 
           terminal_screen = terminal_screen_container_get_screen (TERMINAL_SCREEN_CONTAINER (t->data));
-          if (terminal_screen_has_foreground_process (terminal_screen, NULL))
+          if (terminal_screen_has_foreground_process (terminal_screen, NULL, NULL))
             {
               do_confirm = TRUE;
               break;


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