[Github-comments] [geany/geany] Incorrect search command printed into grep results (#2424)

Chris Graham notifications at xxxxx
Tue Jan 21 04:15:14 UTC 2020


When you do a "Find in Files" search, it will show the search command in the first line of the results.

However, this is quite badly broken. It does not include the directory specifier part of the command, and it doesn't provide escaping (e.g. quoting) of search terms.

I have fixed it in the patch below. I don't know if people are going to like my patch, because I re-used `spawn_append_argument` from the Windows code to build up the arguments, which is probably messy and not 100% perfect. However the Unix code is trusting the command line build up to something deeper in GLib (`g_spawn_async_with_pipes`) so I can't get at the final command line built up made with that code.

IMO What I have is an improvement if not perfect, but there may be a better solution so I'll leave that to wiser minds.

```diff
diff --git a/src/search.c b/src/search.c
index 934d06f5..0b85f0d8 100644
--- a/src/search.c
+++ b/src/search.c
@@ -1694,11 +1694,17 @@ search_find_in_files(const gchar *utf8_search_text, const gchar *utf8_dir, const
 		NULL, &error))
  	{
 		gchar *utf8_str;
+		GString *argv_escaped_together = g_string_new("");
+		gchar **argv_tmp = argv;
+
+		while (argv_tmp && *argv_tmp)
+			spawn_append_argument(argv_escaped_together, *argv_tmp++);
 
  		ui_progress_bar_start(_("Searching..."));
  		msgwin_set_messages_dir(dir);
-		utf8_str = g_strdup_printf(_("%s %s -- %s (in directory: %s)"),
-			tool_prefs.grep_cmd, opts, utf8_search_text, utf8_dir);
+		utf8_str = g_strdup_printf(_("%s %s (in directory: %s)"),
+			command_line, argv_escaped_together->str, utf8_dir);
  		msgwin_msg_add_string(COLOR_BLUE, -1, NULL, utf8_str);
 		g_free(utf8_str);
+		g_string_free(argv_escaped_together, TRUE);
  		ret = TRUE;
diff --git a/src/spawn.c b/src/spawn.c
index 1d4e4292..7e390977 100644
--- a/src/spawn.c
+++ b/src/spawn.c
@@ -52,8 +52,9 @@
 
 #include "spawn.h"
 
+#include <ctype.h>    /* isspace() */
+
 #ifdef G_OS_WIN32
-# include <ctype.h>    /* isspace() */
 # include <fcntl.h>    /* _O_RDONLY, _O_WRONLY */
 # include <io.h>       /* _open_osfhandle, _close */
 # include <windows.h>
@@ -436,8 +437,9 @@ leave:
 	return failure;
 }
 
+#endif /* G_OS_WIN32 */
 
-static void spawn_append_argument(GString *command, const char *text)
+void spawn_append_argument(GString *command, const char *text)
 {
 	const char *s;
 
@@ -487,7 +489,6 @@ static void spawn_append_argument(GString *command, const char *text)
 		g_string_append_c(command, '"');
 	}
 }
-#endif /* G_OS_WIN32 */
 
 
 /*
diff --git a/src/spawn.h b/src/spawn.h
index ccfca269..04337054 100644
--- a/src/spawn.h
+++ b/src/spawn.h
@@ -45,6 +45,8 @@ gboolean spawn_kill_process(GPid pid, GError **error);
 gboolean spawn_async(const gchar *working_directory, const gchar *command_line, gchar **argv,
 	gchar **envp, GPid *child_pid, GError **error);
 
+void spawn_append_argument(GString *command, const char *text);
+
 /** Flags passed to @c spawn_with_callbacks(), which see. */
 typedef enum
 {
```


-- 
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
https://github.com/geany/geany/issues/2424
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.geany.org/pipermail/github-comments/attachments/20200120/44bae68f/attachment.htm>


More information about the Github-comments mailing list