aboutsummaryrefslogtreecommitdiff
path: root/gdb/cli
diff options
context:
space:
mode:
authorTom Tromey <tom@tromey.com>2017-04-26 21:49:55 -0600
committerTom Tromey <tom@tromey.com>2017-08-03 07:58:53 -0600
commited1669453be56d71351c77377acee097aefe83b1 (patch)
tree80ee2b988d2bddb6f52fbf3e1fee2bb3376adab5 /gdb/cli
parentd419f42dd3f3635fc036413258ed530676998191 (diff)
downloadgdb-ed1669453be56d71351c77377acee097aefe83b1.zip
gdb-ed1669453be56d71351c77377acee097aefe83b1.tar.gz
gdb-ed1669453be56d71351c77377acee097aefe83b1.tar.bz2
Change return type of find_and_open_script
This changes find_and_open_script to return a gdb::optional<open_script>, where open_script is a new type encapsulating the two return values. The new type helps avoid cleanups in the callers. ChangeLog 2017-08-03 Tom Tromey <tom@tromey.com> * cli/cli-cmds.c (find_and_open_script): Change return type. Remove "streamp" and "full_path" parameters. (source_script_with_search): Update. * auto-load.c (source_script_file): Update. * cli/cli-cmds.h (find_and_open_script): Change type. (open_script): New struct.
Diffstat (limited to 'gdb/cli')
-rw-r--r--gdb/cli/cli-cmds.c40
-rw-r--r--gdb/cli/cli-cmds.h21
2 files changed, 37 insertions, 24 deletions
diff --git a/gdb/cli/cli-cmds.c b/gdb/cli/cli-cmds.c
index 8f0376e..4650532 100644
--- a/gdb/cli/cli-cmds.c
+++ b/gdb/cli/cli-cmds.c
@@ -506,22 +506,21 @@ 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
-find_and_open_script (const char *script_file, int search_path,
- FILE **streamp, char **full_pathp)
+gdb::optional<open_script>
+find_and_open_script (const char *script_file, int search_path)
{
char *file;
int fd;
struct cleanup *old_cleanups;
int search_flags = OPF_TRY_CWD_FIRST | OPF_RETURN_REALPATH;
+ gdb::optional<open_script> opened;
file = tilde_expand (script_file);
old_cleanups = make_cleanup (xfree, file);
@@ -531,32 +530,33 @@ 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);
+ gdb::unique_xmalloc_ptr<char> full_path (temp_path);
if (fd == -1)
{
int save_errno = errno;
do_cleanups (old_cleanups);
errno = save_errno;
- return 0;
+ return opened;
}
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;
}
+ else
+ opened.emplace (gdb_file_up (result), std::move (full_path));
- return 1;
+ return opened;
}
/* Load script FILE, which has already been opened as STREAM.
@@ -607,14 +607,12 @@ 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::optional<open_script> opened = find_and_open_script (file, search_path);
+ if (!opened)
{
/* The script wasn't found, or was otherwise inaccessible.
If the source command was invoked interactively, throw an
@@ -629,15 +627,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 (opened->stream.get (), file,
+ search_path ? opened->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..1122a97 100644
--- a/gdb/cli/cli-cmds.h
+++ b/gdb/cli/cli-cmds.h
@@ -17,6 +17,9 @@
#if !defined (CLI_CMDS_H)
#define CLI_CMDS_H 1
+#include "common/filestuff.h"
+#include "common/gdb_optional.h"
+
/* Chain containing all defined commands. */
extern struct cmd_list_element *cmdlist;
@@ -117,8 +120,22 @@ 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);
+/* The script that was opened. */
+struct open_script
+{
+ gdb_file_up stream;
+ gdb::unique_xmalloc_ptr<char> full_path;
+
+ open_script (gdb_file_up &&stream_,
+ gdb::unique_xmalloc_ptr<char> &&full_path_)
+ : stream (std::move (stream_)),
+ full_path (std::move (full_path_))
+ {
+ }
+};
+
+extern gdb::optional<open_script>
+ find_and_open_script (const char *file, int search_path);
/* Command tracing state. */