]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
gdb: new extract_single_filename_arg helper function
authorAndrew Burgess <aburgess@redhat.com>
Wed, 19 Jun 2024 10:13:46 +0000 (11:13 +0100)
committerAndrew Burgess <aburgess@redhat.com>
Sat, 7 Sep 2024 19:28:58 +0000 (20:28 +0100)
This commit is in preparation for the next few commits, this commit
adds a new function extract_single_filename_arg.

This new function will be used to convert GDB commands that expect a
single filename argument to have these commands take a possibly quoted
or escaped string.

There's no use of the new function in this commit, for that see the
following commits.

gdb/utils.c
gdb/utils.h

index 94310300fb5a1311fc91cd0b3931f8d277925547..07c5d3c5f5b0c45407f8a5f9569fcb548f16f90c 100644 (file)
@@ -80,6 +80,7 @@
 #include "gdbsupport/buildargv.h"
 #include "pager.h"
 #include "run-on-main-thread.h"
+#include "gdbsupport/gdb_tilde_expand.h"
 
 void (*deprecated_error_begin_hook) (void);
 
@@ -3667,6 +3668,23 @@ copy_bitwise (gdb_byte *dest, ULONGEST dest_offset,
     }
 }
 
+/* See utils.h.  */
+
+std::string
+extract_single_filename_arg (const char *args)
+{
+  if (args == nullptr)
+    return {};
+
+  std::string filename = extract_string_maybe_quoted (&args);
+  args = skip_spaces (args);
+  if (*args != '\0')
+    error (_("Junk after filename \"%s\": %s"), filename.c_str (), args);
+  if (!filename.empty ())
+    filename = gdb_tilde_expand (filename.c_str ());
+  return filename;
+}
+
 #if GDB_SELF_TEST
 static void
 test_assign_set_return_if_changed ()
index 90c8012e73b59c0634cb48ca22254ccb3fbeb1fc..d69c81ca834f32b7b44d2a2719bab5f801e76ac7 100644 (file)
@@ -371,6 +371,20 @@ assign_return_if_changed (T &lval, const T &val)
   return true;
 }
 
+/* ARG is an argument string as passed to a GDB command which is expected
+   to contain a single, possibly quoted, filename argument.  Extract the
+   filename and return it as a string.  If the filename is quoted then the
+   quotes will have been removed.  If the filename is not quoted then any
+   escaping within the filename will have been removed.
+
+   If there is any content in ARG after the filename then an error will be
+   thrown complaining about the extra content.
+
+   If there is no filename in ARG, or if ARG is nullptr, then an empty
+   string will be returned.  */
+
+extern std::string extract_single_filename_arg (const char *arg);
+
 /* A class that can be used to intercept warnings.  A class is used
    here, rather than a gdb::function_view because it proved difficult
    to use a function view in conjunction with ATTRIBUTE_PRINTF in a