Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Tom Tromey <tom@tromey.com>
To: gdb-patches@sourceware.org
Cc: Tom Tromey <tom@tromey.com>
Subject: [RFA 03/23] Use gdb_file_up in find_and_open_script
Date: Wed, 03 May 2017 22:46:00 -0000	[thread overview]
Message-ID: <20170503224626.2818-4-tom@tromey.com> (raw)
In-Reply-To: <20170503224626.2818-1-tom@tromey.com>

This changes find_and_open_script to return a gdb_file_up.  The result
is also used to indicate whether the function succeeded, so the patch
also removes the "streamp" argument; and finally, the type of the
"full_path" argument is changed to remove more cleanups from the
callers.

ChangeLog
2017-05-02  Tom Tromey  <tom@tromey.com>

	* cli/cli-cmds.c (find_and_open_script): Return gdb_file_p.
	Remove "streamp" argument.  Change "full_path" to a
	unique_xmalloc_ptr.
	(source_script_with_search): Update.
	* auto-load.c (source_script_file): Update.
	* cli/cli-cmds.h (find_and_open_script): Change type.
---
 gdb/ChangeLog      |  9 +++++++++
 gdb/auto-load.c    | 32 ++++++++++++++------------------
 gdb/cli/cli-cmds.c | 35 +++++++++++++++--------------------
 gdb/cli/cli-cmds.h |  7 +++++--
 4 files changed, 43 insertions(+), 40 deletions(-)

diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index 2bda6ff..76c53be 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,5 +1,14 @@
 2017-05-02  Tom Tromey  <tom@tromey.com>
 
+	* cli/cli-cmds.c (find_and_open_script): Return gdb_file_p.
+	Remove "streamp" argument.  Change "full_path" to a
+	unique_xmalloc_ptr.
+	(source_script_with_search): Update.
+	* auto-load.c (source_script_file): Update.
+	* cli/cli-cmds.h (find_and_open_script): Change type.
+
+2017-05-02  Tom Tromey  <tom@tromey.com>
+
 	* xml-support.c (xml_fetch_content_from_file): Update.
 	* ui-file.c (stdio_file::open): Update.
 	* tracefile-tfile.c (tfile_start): Update.
diff --git a/gdb/auto-load.c b/gdb/auto-load.c
index 292f2ae..dc1bb5d 100644
--- a/gdb/auto-load.c
+++ b/gdb/auto-load.c
@@ -928,10 +928,7 @@ source_script_file (struct auto_load_pspace_info *pspace_info,
 		    const char *section_name, unsigned int offset,
 		    const char *file)
 {
-  FILE *stream;
-  char *full_path;
-  int opened, in_hash_table;
-  struct cleanup *cleanups;
+  int opened = 0, in_hash_table;
   objfile_script_sourcer_func *sourcer;
 
   /* Skip this script if support is not compiled in.  */
@@ -953,26 +950,27 @@ source_script_file (struct auto_load_pspace_info *pspace_info,
       return;
     }
 
-  opened = find_and_open_script (file, 1 /*search_path*/,
-				 &stream, &full_path);
+  gdb::unique_xmalloc_ptr<char> full_path;
+  gdb_file_up stream = find_and_open_script (file, 1 /*search_path*/,
+					     &full_path);
+  const char *path_ptr = full_path.get ();
 
-  cleanups = make_cleanup (null_cleanup, NULL);
-  if (opened)
+  if (stream)
     {
-      make_cleanup_fclose (stream);
-      make_cleanup (xfree, full_path);
-
-      if (!file_is_auto_load_safe (full_path,
+      if (!file_is_auto_load_safe (full_path.get (),
 				   _("auto-load: Loading %s script "
 				     "\"%s\" from section \"%s\" of "
 				     "objfile \"%s\".\n"),
-				   ext_lang_name (language), full_path,
+				   ext_lang_name (language),
+				   full_path.get (),
 				   section_name, objfile_name (objfile)))
 	opened = 0;
+      else
+	opened = 1;
     }
   else
     {
-      full_path = NULL;
+      path_ptr = NULL;
 
       /* If one script isn't found it's not uncommon for more to not be
 	 found either.  We don't want to print a message for each script,
@@ -986,14 +984,12 @@ source_script_file (struct auto_load_pspace_info *pspace_info,
 					    section_name, offset);
     }
 
-  in_hash_table = maybe_add_script_file (pspace_info, opened, file, full_path,
+  in_hash_table = maybe_add_script_file (pspace_info, opened, file, path_ptr,
 					 language);
 
   /* If this file is not currently loaded, load it.  */
   if (opened && !in_hash_table)
-    sourcer (language, objfile, stream, full_path);
-
-  do_cleanups (cleanups);
+    sourcer (language, objfile, stream.get (), path_ptr);
 }
 
 /* Subroutine of source_section_scripts to simplify it.
diff --git a/gdb/cli/cli-cmds.c b/gdb/cli/cli-cmds.c
index 2a5b128..c75dd16 100644
--- a/gdb/cli/cli-cmds.c
+++ b/gdb/cli/cli-cmds.c
@@ -495,17 +495,16 @@ show_script_ext_mode (struct ui_file *file, int from_tty,
 
 /* Try to open SCRIPT_FILE.
    If successful, the full path name is stored in *FULL_PATHP,
-   the stream is stored in *STREAMP, and return 1.
-   The caller is responsible for freeing *FULL_PATHP.
-   If not successful, return 0; errno is set for the last file
+   and the stream is returned.
+   If not successful, return NULL; errno is set for the last file
    we tried to open.
 
    If SEARCH_PATH is non-zero, and the file isn't found in cwd,
    search for it in the source search path.  */
 
-int
+gdb_file_up
 find_and_open_script (const char *script_file, int search_path,
-		      FILE **streamp, char **full_pathp)
+		      gdb::unique_xmalloc_ptr<char> *full_pathp)
 {
   char *file;
   int fd;
@@ -520,8 +519,10 @@ find_and_open_script (const char *script_file, int search_path,
 
   /* Search for and open 'file' on the search path used for source
      files.  Put the full location in *FULL_PATHP.  */
+  char *temp_path;
   fd = openp (source_path, search_flags,
-	      file, O_RDONLY, full_pathp);
+	      file, O_RDONLY, &temp_path);
+  full_pathp->reset (temp_path);
 
   if (fd == -1)
     {
@@ -533,19 +534,16 @@ find_and_open_script (const char *script_file, int search_path,
 
   do_cleanups (old_cleanups);
 
-  *streamp = fdopen (fd, FOPEN_RT);
-  if (*streamp == NULL)
+  FILE *result = fdopen (fd, FOPEN_RT);
+  if (result == NULL)
     {
       int save_errno = errno;
 
       close (fd);
-      if (full_pathp)
-	xfree (*full_pathp);
       errno = save_errno;
-      return 0;
     }
 
-  return 1;
+  return gdb_file_up (result);
 }
 
 /* Load script FILE, which has already been opened as STREAM.
@@ -596,14 +594,13 @@ source_script_from_stream (FILE *stream, const char *file,
 static void
 source_script_with_search (const char *file, int from_tty, int search_path)
 {
-  FILE *stream;
-  char *full_path;
-  struct cleanup *old_cleanups;
 
   if (file == NULL || *file == 0)
     error (_("source command requires file name of file to source."));
 
-  if (!find_and_open_script (file, search_path, &stream, &full_path))
+  gdb::unique_xmalloc_ptr<char> full_path;
+  gdb_file_up stream = find_and_open_script (file, search_path, &full_path);
+  if (!stream)
     {
       /* The script wasn't found, or was otherwise inaccessible.
          If the source command was invoked interactively, throw an
@@ -618,15 +615,13 @@ source_script_with_search (const char *file, int from_tty, int search_path)
 	}
     }
 
-  old_cleanups = make_cleanup (xfree, full_path);
-  make_cleanup_fclose (stream);
   /* The python support reopens the file, so we need to pass full_path here
      in case the file was found on the search path.  It's useful to do this
      anyway so that error messages show the actual file used.  But only do
      this if we (may have) used search_path, as printing the full path in
      errors for the non-search case can be more noise than signal.  */
-  source_script_from_stream (stream, file, search_path ? full_path : file);
-  do_cleanups (old_cleanups);
+  source_script_from_stream (stream.get (), file,
+			     search_path ? full_path.get () : file);
 }
 
 /* Wrapper around source_script_with_search to export it to main.c
diff --git a/gdb/cli/cli-cmds.h b/gdb/cli/cli-cmds.h
index 7ff1fca..6e3b3d9 100644
--- a/gdb/cli/cli-cmds.h
+++ b/gdb/cli/cli-cmds.h
@@ -17,6 +17,8 @@
 #if !defined (CLI_CMDS_H)
 #define CLI_CMDS_H 1
 
+#include "common/filestuff.h"
+
 /* Chain containing all defined commands.  */
 
 extern struct cmd_list_element *cmdlist;
@@ -117,8 +119,9 @@ extern void source_script (const char *, int);
 
 /* Exported to objfiles.c.  */
 
-extern int find_and_open_script (const char *file, int search_path,
-				 FILE **streamp, char **full_path);
+extern gdb_file_up
+    find_and_open_script (const char *file, int search_path,
+			  gdb::unique_xmalloc_ptr<char> *full_path);
 
 /* Command tracing state.  */
 
-- 
2.9.3


  reply	other threads:[~2017-05-03 22:46 UTC|newest]

Thread overview: 58+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-05-03 22:46 [RFA 00/23] More miscellaneous C++-ification Tom Tromey
2017-05-03 22:46 ` Tom Tromey [this message]
2017-06-02 17:24   ` [RFA 03/23] Use gdb_file_up in find_and_open_script Pedro Alves
2017-07-23 16:08     ` Tom Tromey
2017-05-03 22:46 ` [RFA 21/23] Remove a cleanup in Python Tom Tromey
2017-06-05 13:56   ` Pedro Alves
2017-05-03 22:46 ` [RFA 15/23] Use std::vector to avoid cleanups Tom Tromey
2017-06-02 19:22   ` Pedro Alves
2017-07-23 16:06     ` Tom Tromey
2017-05-03 22:46 ` [RFA 01/23] Introduce and use ui_out_emit_table Tom Tromey
2017-06-02 17:04   ` Pedro Alves
2017-05-03 22:46 ` [RFA 18/23] Use a scoped_restore for command_nest_depth Tom Tromey
2017-06-05 13:38   ` Pedro Alves
2017-05-03 22:46 ` [RFA 11/23] Remove make_cleanup_free_so Tom Tromey
2017-06-02 18:25   ` Pedro Alves
2017-05-03 22:46 ` [RFA 12/23] More uses of scoped_restore Tom Tromey
2017-06-02 18:31   ` Pedro Alves
2017-05-03 22:46 ` [RFA 17/23] Use a scoped_restore for user_call_depth Tom Tromey
2017-06-05 13:32   ` Pedro Alves
2017-06-05 16:56     ` Tom Tromey
2017-05-03 22:46 ` [RFA 10/23] Remove make_cleanup_restore_current_language Tom Tromey
2017-06-02 18:18   ` Pedro Alves
2017-06-05 13:09     ` Tom Tromey
2017-05-03 22:46 ` [RFA 23/23] Use gdb_argv_up in Python Tom Tromey
     [not found]   ` <32dc20d7-52ef-1599-d1fa-1ec299fe291c@redhat.com>
2017-07-19 22:52     ` Tom Tromey
2017-07-19 22:51       ` Tom Tromey
2017-05-03 22:46 ` [RFA 06/23] Change open_terminal_stream to return a gdb_file_up Tom Tromey
2017-06-02 17:31   ` Pedro Alves
2017-05-03 22:46 ` [RFA 02/23] Introduce and use gdb_file_up Tom Tromey
2017-06-02 17:08   ` Pedro Alves
2017-05-03 22:46 ` [RFA 22/23] Make gdb_buildargv return a unique pointer Tom Tromey
2017-06-05 16:21   ` Pedro Alves
2017-07-23 17:26     ` Tom Tromey
2017-05-03 22:46 ` [RFA 19/23] Replace do_restore_instream_cleanup with scoped_restore Tom Tromey
2017-06-05 13:49   ` Pedro Alves
2017-05-03 22:46 ` [RFA 08/23] Remove an unlink cleanup Tom Tromey
2017-06-02 17:37   ` Pedro Alves
2017-05-03 22:46 ` [RFA 14/23] Use unique_xmalloc_ptr in jit.c Tom Tromey
2017-06-02 18:42   ` Pedro Alves
2017-06-05 13:09     ` Tom Tromey
2017-05-03 22:46 ` [RFA 09/23] Remove close cleanup Tom Tromey
2017-06-02 18:08   ` Pedro Alves
2017-07-19 22:52     ` Tom Tromey
2017-07-31 19:08       ` Simon Marchi
2017-08-01 21:52         ` Tom Tromey
2017-05-03 22:46 ` [RFA 20/23] Avoid some manual memory management in Python Tom Tromey
2017-06-05 13:55   ` Pedro Alves
2017-05-03 22:46 ` [RFA 16/23] Remove in_user_command Tom Tromey
2017-06-05 13:27   ` Pedro Alves
2017-05-03 22:46 ` [RFA 13/23] Replace tui_restore_gdbout with scoped_restore Tom Tromey
2017-06-02 18:34   ` Pedro Alves
2017-05-03 22:46 ` [RFA 05/23] Use gdb_file_up in source.c Tom Tromey
2017-06-02 17:27   ` Pedro Alves
2017-05-03 22:46 ` [RFA 04/23] Use gdb_file_up in fbsd-nat.c Tom Tromey
2017-05-03 23:52   ` John Baldwin
2017-05-03 22:46 ` [RFA 07/23] Remove make_cleanup_fclose Tom Tromey
2017-06-02 17:32   ` Pedro Alves
2017-05-29 17:31 ` [RFA 00/23] More miscellaneous C++-ification Tom Tromey

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=20170503224626.2818-4-tom@tromey.com \
    --to=tom@tromey.com \
    --cc=gdb-patches@sourceware.org \
    /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