public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] gdb/guile: perform tilde expansion when sourcing guile scripts
@ 2021-05-05 17:40 Andrew Burgess
  2021-05-05 19:11 ` Tom Tromey
  0 siblings, 1 reply; 12+ messages in thread
From: Andrew Burgess @ 2021-05-05 17:40 UTC (permalink / raw)
  To: gdb-patches

Before this patch:

  (gdb) source ~/script.scm
  ERROR: In procedure apply-smob/1:
  ERROR: In procedure primitive-load-path: Unable to find file "~/script.scm" in load path
  Error while executing Scheme code.
  (gdb)

This is because the path is not tilde expanded.  In contrast, when
sourcing a .py or .gdb script the path is tilde expanded.

This commit fixes this oversight, and allows the above source command
to work as expected.

While I was working on the function that performs the sourcing I added
a use of gdb::unique_xmalloc_ptr<char>, this allows me to remove some
manual memory management.

gdb/ChangeLog:

	* guile/guile.c: Add 'gdbsupport/gdb_tilde_expand.h' include.
	(gdbscm_source_script): Expand filename using gdb_tilde_expand,
	and wrap error message in gdb::unique_xmalloc_ptr<char> to avoid
	manual memory management.

gdb/testsuite/ChangeLog:

	* gdb.guile/guile.exp: Add an extra test.
---
 gdb/ChangeLog                     |  7 +++++++
 gdb/guile/guile.c                 | 11 ++++++-----
 gdb/testsuite/ChangeLog           |  4 ++++
 gdb/testsuite/gdb.guile/guile.exp |  8 ++++++++
 4 files changed, 25 insertions(+), 5 deletions(-)

diff --git a/gdb/guile/guile.c b/gdb/guile/guile.c
index c6959f5b713..d91427f4536 100644
--- a/gdb/guile/guile.c
+++ b/gdb/guile/guile.c
@@ -37,6 +37,7 @@
 #endif
 #include <signal.h>
 #include "gdbsupport/block-signals.h"
+#include "gdbsupport/gdb_tilde_expand.h"
 
 /* The Guile version we're using.
    We *could* use the macros in libguile/version.h but that would preclude
@@ -270,13 +271,13 @@ static void
 gdbscm_source_script (const struct extension_language_defn *extlang,
 		      FILE *file, const char *filename)
 {
-  char *msg = gdbscm_safe_source_script (filename);
+  std::string full_path = gdb_tilde_expand (filename);
+
+  gdb::unique_xmalloc_ptr<char> msg
+    (gdbscm_safe_source_script (full_path.c_str ()));
 
   if (msg != NULL)
-    {
-      fprintf_filtered (gdb_stderr, "%s\n", msg);
-      xfree (msg);
-    }
+    fprintf_filtered (gdb_stderr, "%s\n", msg.get ());
 }
 \f
 /* (execute string [#:from-tty boolean] [#:to-string boolean])
diff --git a/gdb/testsuite/gdb.guile/guile.exp b/gdb/testsuite/gdb.guile/guile.exp
index 6e464cc0e77..06b7c006ee6 100644
--- a/gdb/testsuite/gdb.guile/guile.exp
+++ b/gdb/testsuite/gdb.guile/guile.exp
@@ -82,3 +82,11 @@ gdb_test_no_output "guile (define a (execute \"help\" #:to-string #t))" \
 
 gdb_test "guile (print a)" "= .*aliases -- User-defined aliases of other commands.*" \
     "verify help to uiout"
+
+# Verify that we can source a guile script using ~ for the HOME directory.
+
+save_vars { env(HOME) } {
+    set env(HOME) $srcdir/$subdir
+    clean_restart
+    gdb_test "source ~/source2.scm" "yes"
+}
-- 
2.25.4


^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2021-05-07 21:23 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-05-05 17:40 [PATCH] gdb/guile: perform tilde expansion when sourcing guile scripts Andrew Burgess
2021-05-05 19:11 ` Tom Tromey
2021-05-05 21:01   ` [PATCHv2 0/2] " Andrew Burgess
2021-05-05 21:01     ` [PATCHv2 1/2] " Andrew Burgess
2021-05-06  2:23       ` Simon Marchi
2021-05-07 10:29         ` Andrew Burgess
2021-05-07 14:55           ` Simon Marchi
2021-05-07 21:23             ` Andrew Burgess
2021-05-05 21:01     ` [PATCHv2 2/2] gdb/guile: Have gdbscm_safe_source_script return a unique_ptr Andrew Burgess
2021-05-06  2:28       ` Simon Marchi
2021-05-06  2:32         ` Simon Marchi
2021-05-07  8:55           ` Andrew Burgess

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