public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Andrew Burgess <aburgess@redhat.com>
To: gdb-patches@sourceware.org
Cc: Andrew Burgess <aburgess@redhat.com>,
	Michael Weghorn <m.weghorn@posteo.de>
Subject: [PATCH 07/16] gdbsupport: split escape_shell_characters in two
Date: Tue,  9 Jan 2024 14:26:30 +0000	[thread overview]
Message-ID: <ef0da483dc21b26edd918cf3decba17152c77075.1704809585.git.aburgess@redhat.com> (raw)
In-Reply-To: <cover.1704809585.git.aburgess@redhat.com>

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(-)

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);
+}
-- 
2.25.4


  parent reply	other threads:[~2024-01-09 14:26 UTC|newest]

Thread overview: 37+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-01-09 14:26 [PATCH 00/16] Inferior argument (inc for remote targets) changes Andrew Burgess
2024-01-09 14:26 ` [PATCH 01/16] libiberty/buildargv: POSIX behaviour for backslash handling Andrew Burgess
2024-01-09 14:26 ` [PATCH 02/16] gdb/testsuite: add some xfail in gdb.base/startup-with-shell.exp Andrew Burgess
2024-01-09 14:26 ` [PATCH 03/16] gdb: Support some escaping of args with startup-with-shell being off Andrew Burgess
2024-01-09 14:26 ` [PATCH 04/16] gdb: remove the !startup_with_shell path from construct_inferior_arguments Andrew Burgess
2024-01-21  3:56   ` Keith Seitz
2024-01-09 14:26 ` [PATCH 05/16] gdbserver: convert program_args to a single string Andrew Burgess
2024-01-09 14:26 ` [PATCH 06/16] gdbsupport: have construct_inferior_arguments take an escape function Andrew Burgess
2024-01-09 14:26 ` Andrew Burgess [this message]
2024-01-09 14:26 ` [PATCH 08/16] gdb: move remote arg splitting and joining into gdbsupport/ Andrew Burgess
2024-01-21  3:57   ` Keith Seitz
2024-01-09 14:26 ` [PATCH 09/16] gdb/python: change escaping rules when setting arguments Andrew Burgess
2024-01-09 16:30   ` Eli Zaretskii
2024-01-09 14:26 ` [PATCH 10/16] gdb: add remote argument passing self tests Andrew Burgess
2024-01-21  3:57   ` Keith Seitz
2024-01-09 14:26 ` [PATCH 11/16] gdb/gdbserver: pass inferior arguments as a single string Andrew Burgess
2024-01-09 16:34   ` Eli Zaretskii
2024-01-09 16:35   ` Eli Zaretskii
2024-01-09 14:26 ` [PATCH 12/16] gdb/gdbserver: add a '--no-escape-args' command line option Andrew Burgess
2024-01-09 16:43   ` Eli Zaretskii
2024-01-21  3:57   ` Keith Seitz
2024-01-09 14:26 ` [PATCH 13/16] gdb: allow 'set args' and run commands to contain newlines Andrew Burgess
2024-01-09 16:44   ` Eli Zaretskii
2024-01-21  3:57   ` Keith Seitz
2024-01-09 14:26 ` [PATCH 14/16] gdb/gdbserver: remove some uses of free_vector_argv Andrew Burgess
2024-01-09 14:26 ` [PATCH 15/16] gdb: new maintenance command to help debug remote argument issues Andrew Burgess
2024-01-09 16:32   ` Eli Zaretskii
2024-01-21  3:57   ` Keith Seitz
2024-01-09 14:26 ` [PATCH 16/16] gdb/gdbserver: rework argument splitting and joining Andrew Burgess
2024-01-09 16:37   ` Eli Zaretskii
2024-01-21  3:57   ` Keith Seitz
2024-01-09 16:58 ` [PATCH 00/16] Inferior argument (inc for remote targets) changes Eli Zaretskii
2024-01-20 22:46   ` Andrew Burgess
2024-01-21 10:22     ` Eli Zaretskii
2024-01-22 10:29       ` Andrew Burgess
2024-01-10  8:28 ` Michael Weghorn
2024-01-21  3:56 ` Keith Seitz

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ef0da483dc21b26edd918cf3decba17152c77075.1704809585.git.aburgess@redhat.com \
    --to=aburgess@redhat.com \
    --cc=gdb-patches@sourceware.org \
    --cc=m.weghorn@posteo.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).