[07/16] gdbsupport: split escape_shell_characters in two

Message ID ef0da483dc21b26edd918cf3decba17152c77075.1704809585.git.aburgess@redhat.com
State New
Headers
Series Inferior argument (inc for remote targets) changes |

Commit Message

Andrew Burgess Jan. 9, 2024, 2:26 p.m. UTC
  Building on the previous commit, this commit performs further
refactoring.

The escape_shell_characters function is split into two.  We have a
worker core (the escape_characters function) which takes a set of
"special" characters that need escaping, and applies that escaping.

Then we have escape_shell_characters, which just calls
escape_characters with the correct set of special characters.

There should be no user visible changes after this commit.

This commit is similar to some of the changes made in this series:

  https://inbox.sourceware.org/gdb-patches/20211022071933.3478427-1-m.weghorn@posteo.de/

Though I don't think there's one commit that is exactly the same as
this one.  But I've listed the author of the original series as a
Co-Author, because I feel the work is similar enough.

Co-Authored-By: Michael Weghorn <m.weghorn@posteo.de>
---
 gdbsupport/common-inferior.cc | 49 +++++++++++++++++++++++++----------
 1 file changed, 36 insertions(+), 13 deletions(-)
  

Patch

diff --git a/gdbsupport/common-inferior.cc b/gdbsupport/common-inferior.cc
index f5620ec89aa..6717f7d5c08 100644
--- a/gdbsupport/common-inferior.cc
+++ b/gdbsupport/common-inferior.cc
@@ -44,23 +44,23 @@  construct_inferior_arguments (gdb::array_view<char * const> argv,
   return result;
 }
 
-/* See common-inferior.h.  */
-
-std::string
-escape_shell_characters (const char *arg)
+/* Escape characters in ARG and return an updated string.  The string
+   SPECIAL contains the set of characters that must be escaped.  SPECIAL
+   must not be nullptr, and it is assumed that SPECIAL contains the newline
+   '\n' character.  It is assumed that ARG is not nullptr, but ARG can
+   be the empty string.  */
+
+static std::string
+escape_characters (const char *arg, const char *special)
 {
+  gdb_assert (special != nullptr);
+  gdb_assert (arg != nullptr);
+
   std::string result;
 
 #ifdef __MINGW32__
-  /* This holds all the characters considered special to the
-     Windows shells.  */
-  static const char special[] = "\"!&*|[]{}<>?`~^=;, \t\n";
   static const char quote = '"';
 #else
-  /* This holds all the characters considered special to the
-     typical Unix shells.  We include `^' because the SunOS
-     /bin/sh treats it as a synonym for `|'.  */
-  static const char special[] = "\"!#$&*()\\|[]{}<>?'`~^; \t\n";
   static const char quote = '\'';
 #endif
 
@@ -70,12 +70,16 @@  escape_shell_characters (const char *arg)
       result += quote;
       result += quote;
     }
+  /* The special character handling code here assumes that if SPECIAL is
+     not nullptr, then SPECIAL will contain '\n'.  This is true for all our
+     current usages, but if this ever changes in the future the following
+     might need reworking.  */
   else
     {
 #ifdef __MINGW32__
       bool quoted = false;
 
-      if (strpbrk (arg, special))
+      if (strpbrk (argv[i], special))
 	{
 	  quoted = true;
 	  result += quote;
@@ -96,7 +100,7 @@  escape_shell_characters (const char *arg)
 #ifdef __MINGW32__
 	      if (*cp == quote)
 #else
-	      if (strchr (special, *cp) != NULL)
+	      if (strchr (special, *cp) != nullptr)
 #endif
 		result += '\\';
 	      result += *cp;
@@ -110,3 +114,22 @@  escape_shell_characters (const char *arg)
 
   return result;
 }
+
+/* See common-inferior.h.  */
+
+std::string
+escape_shell_characters (const char *arg)
+{
+#ifdef __MINGW32__
+  /* This holds all the characters considered special to the
+     Windows shells.  */
+  static const char special[] = "\"!&*|[]{}<>?`~^=;, \t\n";
+#else
+  /* This holds all the characters considered special to the
+     typical Unix shells.  We include `^' because the SunOS
+     /bin/sh treats it as a synonym for `|'.  */
+  static const char special[] = "\"!#$&*()\\|[]{}<>?'`~^; \t\n";
+#endif
+
+  return escape_characters (arg, special);
+}