From patchwork Mon Sep 9 18:08:29 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Terekhov, Mikhail via Gdb-patches" X-Patchwork-Id: 34458 Received: (qmail 71408 invoked by alias); 9 Sep 2019 18:08: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 71338 invoked by uid 89); 9 Sep 2019 18:08:43 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-22.7 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=ham version=3.3.1 spammy=upcoming, U*cbiesinger, sk:cbiesin, cbiesinger@google.com X-HELO: mail-yb1-f202.google.com Received: from mail-yb1-f202.google.com (HELO mail-yb1-f202.google.com) (209.85.219.202) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Mon, 09 Sep 2019 18:08:42 +0000 Received: by mail-yb1-f202.google.com with SMTP id o123so11290507ybo.7 for ; Mon, 09 Sep 2019 11:08:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=IkMjD6mfLOcEApxmhGm3MdPhv0hi/HwCfgJW30f2aok=; b=hxn9sDCw9myUKoPpbLkQo3t/7CG1m4iWw7WsGepww0DPKpPX2KLUBTlFyUKjwjTbA8 xPi9XDOJL5wvLRp48BW/UAdakjhk+tHR3vZ/ig/IVKe5VxcnNV33wgb2iaWT8YW5Gvhx BaaymNuPde3XcA+R3sMOx/AZwDdhl49HFDeqO8sW/GFIU5ocABHdug+bPkME6t2IqlGC 2+ws3snC4zcyLI3fS8kfc0HfeHPCWXSEzNRuvfzX3bNu/Fw0w14ov8DqAmherODZPV97 8UG5pZXHqCWoFJn0UdDTzfIg/cxBb0EuqKKOcc5qEo0pw59HACr21meYKrGi6aTzElGB kcxw== Date: Mon, 9 Sep 2019 13:08:29 -0500 In-Reply-To: <20190909180830.215313-1-cbiesinger@google.com> Message-Id: <20190909180830.215313-3-cbiesinger@google.com> Mime-Version: 1.0 References: <20190909180830.215313-1-cbiesinger@google.com> Subject: [PATCH 2/3] Factor out the code to do the datadir-relocation for gdbinit X-Patchwork-Original-From: "Christian Biesinger via gdb-patches" From: "Terekhov, Mikhail via Gdb-patches" Reply-To: Christian Biesinger To: gdb-patches@sourceware.org Cc: Christian Biesinger X-IsSubscribed: yes This simplifies get_init_files and makes it possible to reuse this code in an upcoming patch for SYSTEM_GDBINIT_DIR. gdb/ChangeLog: 2019-08-20 Christian Biesinger * main.c (relocate_gdbinit_path_maybe_in_datadir): Factor this code out of get_init_files. (get_init_files): Update. --- gdb/main.c | 70 ++++++++++++++++++++++++++++++------------------------ 1 file changed, 39 insertions(+), 31 deletions(-) diff --git a/gdb/main.c b/gdb/main.c index d27b124fa3..24aad0ca5a 100644 --- a/gdb/main.c +++ b/gdb/main.c @@ -191,6 +191,43 @@ relocate_gdb_directory (const char *initial, bool relocatable) return dir; } +static std::string +relocate_gdbinit_path_maybe_in_datadir (const std::string& file) +{ + size_t datadir_len = strlen (GDB_DATADIR); + + std::string relocated_path; + + /* If SYSTEM_GDBINIT lives in data-directory, and data-directory + has been provided, search for SYSTEM_GDBINIT there. */ + if (gdb_datadir_provided + && datadir_len < file.length () + && filename_ncmp (file.c_str (), GDB_DATADIR, datadir_len) == 0 + && IS_DIR_SEPARATOR (file[datadir_len])) + { + /* Append the part of SYSTEM_GDBINIT that follows GDB_DATADIR + to gdb_datadir. */ + + size_t start = datadir_len; + for (; IS_DIR_SEPARATOR (file[start]); ++start) + continue; + relocated_path = std::string (gdb_datadir) + SLASH_STRING + + file.substr(start); + } + else + { + char *relocated = relocate_path (gdb_program_name, + file.c_str (), + SYSTEM_GDBINIT_RELOCATABLE); + if (relocated != nullptr) + { + relocated_path = relocated; + xfree (relocated); + } + } + return relocated_path; +} + /* Compute the locations of init files that GDB should source and return them in SYSTEM_GDBINIT, HOME_GDBINIT, LOCAL_GDBINIT. If there is no system gdbinit (resp. home gdbinit and local gdbinit) @@ -212,37 +249,8 @@ get_init_files (std::string *system_gdbinit, if (SYSTEM_GDBINIT[0]) { - size_t datadir_len = strlen (GDB_DATADIR); - size_t sys_gdbinit_len = strlen (SYSTEM_GDBINIT); - std::string relocated_sysgdbinit; - - /* If SYSTEM_GDBINIT lives in data-directory, and data-directory - has been provided, search for SYSTEM_GDBINIT there. */ - if (gdb_datadir_provided - && datadir_len < sys_gdbinit_len - && filename_ncmp (SYSTEM_GDBINIT, GDB_DATADIR, datadir_len) == 0 - && IS_DIR_SEPARATOR (SYSTEM_GDBINIT[datadir_len])) - { - /* Append the part of SYSTEM_GDBINIT that follows GDB_DATADIR - to gdb_datadir. */ - - size_t start = datadir_len; - for (; IS_DIR_SEPARATOR (SYSTEM_GDBINIT[start]); ++start) - continue; - relocated_sysgdbinit = std::string (gdb_datadir) + SLASH_STRING + - &SYSTEM_GDBINIT[start]; - } - else - { - char *relocated = relocate_path (gdb_program_name, - SYSTEM_GDBINIT, - SYSTEM_GDBINIT_RELOCATABLE); - if (relocated != nullptr) - { - relocated_sysgdbinit = relocated; - xfree (relocated); - } - } + std::string relocated_sysgdbinit = + relocate_gdbinit_path_maybe_in_datadir (SYSTEM_GDBINIT); if (!relocated_sysgdbinit.empty () && stat (relocated_sysgdbinit.c_str (), &s) == 0) sysgdbinit = relocated_sysgdbinit;