gdb: bool-ify ext_lang_auto_load_enabled and friends
[deliverable/binutils-gdb.git] / gdb / auto-load.c
index 9a51d2f3dc6c5f9024a26c5c1dbe64f66ff9cfe4..0d5532b4882e4789de39a92a2cb8f990b24e6550 100644 (file)
@@ -1,6 +1,6 @@
 /* GDB routines for supporting auto-loaded scripts.
 
-   Copyright (C) 2012-2020 Free Software Foundation, Inc.
+   Copyright (C) 2012-2021 Free Software Foundation, Inc.
 
    This file is part of GDB.
 
@@ -93,9 +93,9 @@ show_auto_load_gdb_scripts (struct ui_file *file, int from_tty,
                    value);
 }
 
-/* Return non-zero if auto-loading gdb scripts is enabled.  */
+/* See auto-load.h.  */
 
-int
+bool
 auto_load_gdb_scripts_enabled (const struct extension_language_defn *extlang)
 {
   return auto_load_gdb_scripts;
@@ -364,7 +364,7 @@ filename_is_in_pattern_1 (char *filename, char *pattern)
   for (;;)
     {
       /* Trim trailing slashes ("/").  PATTERN also has slashes trimmed the
-         same way so they will match.  */
+        same way so they will match.  */
       while (filename_len && IS_DIR_SEPARATOR (filename[filename_len - 1]))
        filename_len--;
       filename[filename_len] = '\0';
@@ -461,19 +461,13 @@ filename_is_in_auto_load_safe_path_vec (const char *filename,
   return 0;
 }
 
-/* Return 1 if FILENAME is located in one of the directories of
-   AUTO_LOAD_SAFE_PATH.  Otherwise call warning and return 0.  FILENAME does
-   not have to be an absolute path.
+/* See auto-load.h.  */
 
-   Existence of FILENAME is not checked.  Function will still give a warning
-   even if the caller would quietly skip non-existing file in unsafe
-   directory.  */
-
-int
+bool
 file_is_auto_load_safe (const char *filename, const char *debug_fmt, ...)
 {
   gdb::unique_xmalloc_ptr<char> filename_real;
-  static int advice_printed = 0;
+  static bool advice_printed = false;
 
   if (debug_auto_load)
     {
@@ -485,11 +479,11 @@ file_is_auto_load_safe (const char *filename, const char *debug_fmt, ...)
     }
 
   if (filename_is_in_auto_load_safe_path_vec (filename, &filename_real))
-    return 1;
+    return true;
 
   auto_load_safe_path_vec_update ();
   if (filename_is_in_auto_load_safe_path_vec (filename, &filename_real))
-    return 1;
+    return true;
 
   warning (_("File \"%ps\" auto-loading has been declined by your "
             "`auto-load safe-path' set to \"%s\"."),
@@ -498,11 +492,26 @@ file_is_auto_load_safe (const char *filename, const char *debug_fmt, ...)
 
   if (!advice_printed)
     {
-      const char *homedir = getenv ("HOME");
-
-      if (homedir == NULL)
-       homedir = "$HOME";
-      std::string homeinit = string_printf ("%s/%s", homedir, GDBINIT);
+      /* Find the existing home directory config file.  */
+      struct stat buf;
+      std::string home_config = find_gdb_home_config_file (GDBINIT, &buf);
+      if (home_config.empty ())
+       {
+         /* The user doesn't have an existing home directory config file,
+            so we should suggest a suitable path for them to use.  */
+         std::string config_dir_file
+           = get_standard_config_filename (GDBINIT);
+         if (!config_dir_file.empty ())
+           home_config = config_dir_file;
+         else
+           {
+             const char *homedir = getenv ("HOME");
+             if (homedir == nullptr)
+               homedir = "$HOME";
+             home_config = (std::string (homedir) + SLASH_STRING
+                            + std::string (GDBINIT));
+           }
+       }
 
       printf_filtered (_("\
 To enable execution of this file add\n\
@@ -515,11 +524,11 @@ For more information about this security protection see the\n\
 \"Auto-loading safe path\" section in the GDB manual.  E.g., run from the shell:\n\
 \tinfo \"(gdb)Auto-loading safe path\"\n"),
                       filename_real.get (),
-                      homeinit.c_str (), homeinit.c_str ());
-      advice_printed = 1;
+                      home_config.c_str (), home_config.c_str ());
+      advice_printed = true;
     }
 
-  return 0;
+  return false;
 }
 
 /* For scripts specified in .debug_gdb_scripts, multiple objfiles may load
@@ -777,10 +786,8 @@ auto_load_objfile_script_1 (struct objfile *objfile, const char *realname,
 
       /* Convert Windows file name from c:/dir/file to /c/dir/file.  */
       if (HAS_DRIVE_SPEC (debugfile))
-       {
-         debugfile_holder = STRIP_DRIVE_SPEC (debugfile);
-         filename = std::string("\\") + debugfile[0] + debugfile_holder;
-       }
+       filename = (std::string("\\") + debugfile[0]
+                   + STRIP_DRIVE_SPEC (debugfile));
 
       for (const gdb::unique_xmalloc_ptr<char> &dir : vec)
        {
This page took 0.029194 seconds and 4 git commands to generate.