From patchwork Tue Jul 25 17:20:46 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 21776 Received: (qmail 46484 invoked by alias); 25 Jul 2017 17:27:44 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Delivered-To: mailing list gdb-patches@sourceware.org Received: (qmail 46396 invoked by uid 89); 25 Jul 2017 17:27:43 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-25.4 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE, RCVD_IN_SORBS_SPAM, SPF_PASS autolearn=ham version=3.3.2 spammy=Hx-languages-length:7998 X-HELO: gproxy10-pub.mail.unifiedlayer.com Received: from gproxy10-pub.mail.unifiedlayer.com (HELO gproxy10-pub.mail.unifiedlayer.com) (69.89.20.226) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Tue, 25 Jul 2017 17:27:40 +0000 Received: from CMOut01 (unknown [10.0.90.82]) by gproxy10.mail.unifiedlayer.com (Postfix) with ESMTP id 6D5001408C7 for ; Tue, 25 Jul 2017 11:21:17 -0600 (MDT) Received: from box522.bluehost.com ([74.220.219.122]) by CMOut01 with id p5ME1v00H2f2jeq015MHXx; Tue, 25 Jul 2017 11:21:17 -0600 X-Authority-Analysis: v=2.2 cv=INByMknG c=1 sm=1 tr=0 a=GsOEXm/OWkKvwdLVJsfwcA==:117 a=GsOEXm/OWkKvwdLVJsfwcA==:17 a=G3gG6ho9WtcA:10 a=zstS-IiYAAAA:8 a=sWDeUkjReEb06Dyb3pYA:9 a=4G6NA9xxw8l3yy4pmD5M:22 Received: from 174-29-39-24.hlrn.qwest.net ([174.29.39.24]:55470 helo=bapiya.Home) by box522.bluehost.com with esmtpsa (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.87) (envelope-from ) id 1da3WE-0015gW-0T; Tue, 25 Jul 2017 11:21:14 -0600 From: Tom Tromey To: gdb-patches@sourceware.org Cc: Tom Tromey Subject: [RFA v2 03/24] Change return type of find_and_open_script Date: Tue, 25 Jul 2017 11:20:46 -0600 Message-Id: <20170725172107.9799-4-tom@tromey.com> In-Reply-To: <20170725172107.9799-1-tom@tromey.com> References: <20170725172107.9799-1-tom@tromey.com> X-BWhitelist: no X-Exim-ID: 1da3WE-0015gW-0T X-Source-Sender: 174-29-39-24.hlrn.qwest.net (bapiya.Home) [174.29.39.24]:55470 X-Source-Auth: tom+tromey.com X-Email-Count: 4 X-Source-Cap: ZWx5bnJvYmk7ZWx5bnJvYmk7Ym94NTIyLmJsdWVob3N0LmNvbQ== X-Local-Domain: yes This changes find_and_open_script to return a gdb::optional, where open_script is a new type encapsulating the two return values. The new type helps avoid cleanups in the callers. ChangeLog 2017-07-25 Tom Tromey * 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. --- gdb/ChangeLog | 9 +++++++++ gdb/auto-load.c | 32 +++++++++++++------------------- gdb/cli/cli-cmds.c | 40 ++++++++++++++++++---------------------- gdb/cli/cli-cmds.h | 21 +++++++++++++++++++-- 4 files changed, 59 insertions(+), 43 deletions(-) diff --git a/gdb/ChangeLog b/gdb/ChangeLog index cebc5c8..fc9d184 100644 --- a/gdb/ChangeLog +++ b/gdb/ChangeLog @@ -1,5 +1,14 @@ 2017-07-25 Tom Tromey + * 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. + +2017-07-25 Tom Tromey + * 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..da38d32 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 in_hash_table; objfile_script_sourcer_func *sourcer; /* Skip this script if support is not compiled in. */ @@ -953,26 +950,25 @@ source_script_file (struct auto_load_pspace_info *pspace_info, return; } - opened = find_and_open_script (file, 1 /*search_path*/, - &stream, &full_path); + gdb::optional opened = find_and_open_script (file, + 1 /*search_path*/); + const char *path_ptr; - cleanups = make_cleanup (null_cleanup, NULL); if (opened) { - make_cleanup_fclose (stream); - make_cleanup (xfree, full_path); - - if (!file_is_auto_load_safe (full_path, + path_ptr = opened->full_path.get (); + if (!file_is_auto_load_safe (opened->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), + opened->full_path.get (), section_name, objfile_name (objfile))) - opened = 0; + opened.reset (); } 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 +982,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, - language); + in_hash_table = maybe_add_script_file (pspace_info, bool (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, opened->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 af750d3..c33ddbe 100644 --- a/gdb/cli/cli-cmds.c +++ b/gdb/cli/cli-cmds.c @@ -502,22 +502,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 +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 opened; file = tilde_expand (script_file); old_cleanups = make_cleanup (xfree, file); @@ -527,32 +526,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 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. @@ -603,14 +603,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 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 @@ -625,15 +623,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 full_path; + + open_script (gdb_file_up &&stream_, + gdb::unique_xmalloc_ptr &&full_path_) + : stream (std::move (stream_)), + full_path (std::move (full_path_)) + { + } +}; + +extern gdb::optional + find_and_open_script (const char *file, int search_path); /* Command tracing state. */