public inbox for gdb-cvs@sourceware.org
help / color / mirror / Atom feed
* [binutils-gdb] Factor out the code to do the datadir-relocation for gdbinit
@ 2019-09-11 21:35 Christian Biesinger
  0 siblings, 0 replies; only message in thread
From: Christian Biesinger @ 2019-09-11 21:35 UTC (permalink / raw)
  To: gdb-cvs

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=9224a01377868604ce4a7eb9c0d97736f7349dcd

commit 9224a01377868604ce4a7eb9c0d97736f7349dcd
Author: Christian Biesinger <cbiesinger@google.com>
Date:   Tue Aug 20 16:38:29 2019 -0500

    Factor out the code to do the datadir-relocation for gdbinit
    
    This simplifies get_init_files and makes it possible to reuse
    this code in an upcoming patch for SYSTEM_GDBINIT_DIR.
    
    gdb/ChangeLog:
    
    2019-09-11  Christian Biesinger  <cbiesinger@google.com>
    
    	* main.c (relocate_gdbinit_path_maybe_in_datadir): Factor this code
    	out of get_init_files.
    	(get_init_files): Update.

Diff:
---
 gdb/ChangeLog |  6 +++++
 gdb/main.c    | 74 ++++++++++++++++++++++++++++++++++-------------------------
 2 files changed, 49 insertions(+), 31 deletions(-)

diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index 27eae1d..91b860c 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,5 +1,11 @@
 2019-09-11  Christian Biesinger  <cbiesinger@google.com>
 
+	* main.c (relocate_gdbinit_path_maybe_in_datadir): Factor this code
+	out of get_init_files.
+	(get_init_files): Update.
+
+2019-09-11  Christian Biesinger  <cbiesinger@google.com>
+
 	* main.c (get_init_files): Change to use std::string.
 	(captured_main_1): Update.
 	(print_gdb_help): Update.
diff --git a/gdb/main.c b/gdb/main.c
index e32ed62..9e22889 100644
--- a/gdb/main.c
+++ b/gdb/main.c
@@ -191,6 +191,47 @@ relocate_gdb_directory (const char *initial, bool relocatable)
   return dir;
 }
 
+/* Given a gdbinit path in FILE, adjusts it according to the gdb_datadir
+   parameter if it is in the data dir, or passes it through relocate_path
+   otherwise.  */
+
+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)
+	;
+      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 +253,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)
-		;
-	      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;


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2019-09-11 21:35 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-09-11 21:35 [binutils-gdb] Factor out the code to do the datadir-relocation for gdbinit Christian Biesinger

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