[2/3] Factor out the code to do the datadir-relocation for gdbinit

Message ID 20190909180830.215313-3-cbiesinger@google.com
State New, archived
Headers

Commit Message

Terekhov, Mikhail via Gdb-patches Sept. 9, 2019, 6:08 p.m. UTC
  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  <cbiesinger@google.com>

	* 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(-)
  

Comments

Tom Tromey Sept. 10, 2019, 3:15 p.m. UTC | #1
>>>>> "Christian" == Christian Biesinger via gdb-patches <gdb-patches@sourceware.org> writes:

Christian> This simplifies get_init_files and makes it possible to reuse
Christian> this code in an upcoming patch for SYSTEM_GDBINIT_DIR.

 
Christian> +static std::string
Christian> +relocate_gdbinit_path_maybe_in_datadir (const std::string& file)
Christian> +{

New functions should have an introductory comment.

Christian> +      for (; IS_DIR_SEPARATOR (file[start]); ++start)
Christian> +	continue;

Just omit the "continue" and leave a bare ";".

Christian> +      relocated_path = std::string (gdb_datadir) + SLASH_STRING +
Christian> +	file.substr(start);

This needs parens on the RHS and should be indented differently.
Also, space before the "(" after "substr".

Christian> +	  std::string relocated_sysgdbinit =
Christian> +	    relocate_gdbinit_path_maybe_in_datadir (SYSTEM_GDBINIT);

"=" at start of continuation line.

Tom
  
Terekhov, Mikhail via Gdb-patches Sept. 10, 2019, 7:14 p.m. UTC | #2
On Tue, Sep 10, 2019 at 10:15 AM Tom Tromey <tom@tromey.com> wrote:
>
> >>>>> "Christian" == Christian Biesinger via gdb-patches <gdb-patches@sourceware.org> writes:
>
> Christian> This simplifies get_init_files and makes it possible to reuse
> Christian> this code in an upcoming patch for SYSTEM_GDBINIT_DIR.
>
>
> Christian> +static std::string
> Christian> +relocate_gdbinit_path_maybe_in_datadir (const std::string& file)
> Christian> +{
>
> New functions should have an introductory comment.

Done.

> Christian> +      for (; IS_DIR_SEPARATOR (file[start]); ++start)
> Christian> +    continue;
>
> Just omit the "continue" and leave a bare ";".

Done.

> Christian> +      relocated_path = std::string (gdb_datadir) + SLASH_STRING +
> Christian> +    file.substr(start);
>
> This needs parens on the RHS and should be indented differently.
> Also, space before the "(" after "substr".

Done.

> Christian> +      std::string relocated_sysgdbinit =
> Christian> +        relocate_gdbinit_path_maybe_in_datadir (SYSTEM_GDBINIT);
>
> "=" at start of continuation line.

Done.
  

Patch

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;