# HG changeset patch
# User Mike Miller <mtmiller@octave.org>
# Date 1551912878 28800
#      Wed Mar 06 14:54:38 2019 -0800
# Node ID 0050d00a8fc411e4d0841aa56cd17c27d986e370
# Parent  9590067f59dd9096782d12e9eac344b428ac52e7
[WIP] read startup files from XDG_CONFIG_HOME or LOCALAPPDATA (bug #36477)

diff -r 9590067f59dd -r 0050d00a8fc4 libinterp/corefcn/interpreter.cc
--- a/libinterp/corefcn/interpreter.cc	Sun Mar 10 09:58:58 2019 +0100
+++ b/libinterp/corefcn/interpreter.cc	Wed Mar 06 14:54:38 2019 -0800
@@ -30,6 +30,7 @@
 #include "cmd-edit.h"
 #include "cmd-hist.h"
 #include "file-stat.h"
+#include "file-ops.h"
 #include "fpucw-wrappers.h"
 #include "lo-blas-proto.h"
 #include "lo-error.h"
@@ -792,6 +793,29 @@
         // anywhere in the load path when exiting Octave.
         add_atexit_function ("__finish__");
 
+        // Try to execute commands from $CONFIG/octave/octaverc, where
+        // $CONFIG is the platform-dependent location for user local
+        // configuration files.
+
+        std::string user_config_dir = sys::env::get_user_config_directory ();
+
+        std::string cfg_dir = user_config_dir + sys::file_ops::dir_sep_str ()
+                            + "octave";
+
+        std::string cfg_rc = sys::env::make_absolute ("octaverc", cfg_dir);
+
+        if (! cfg_rc.empty ())
+          {
+            // FIXME: delete this debug
+            std::cerr << "debug: load new startup file: " << cfg_rc << std::endl;
+
+            int status = safe_source_file (cfg_rc, context, verbose,
+                                           require_file);
+
+            if (status)
+              exit_status = status;
+          }
+
         // Try to execute commands from $HOME/$OCTAVE_INITFILE and
         // $OCTAVE_INITFILE.  If $OCTAVE_INITFILE is not set,
         // .octaverc is assumed.
diff -r 9590067f59dd -r 0050d00a8fc4 liboctave/system/oct-env.cc
--- a/liboctave/system/oct-env.cc	Sun Mar 10 09:58:58 2019 +0100
+++ b/liboctave/system/oct-env.cc	Wed Mar 06 14:54:38 2019 -0800
@@ -55,8 +55,14 @@
 #include "oct-syscalls.h"
 #include "set-program-name-wrapper.h"
 #include "singleton-cleanup.h"
+#include "uniconv-wrappers.h"
 #include "unistd-wrappers.h"
 
+#if defined (OCTAVE_USE_WINDOWS_API)
+#  include <windows.h>
+#  include <shlobj.h>
+#endif
+
 namespace octave
 {
   namespace sys
@@ -148,6 +154,13 @@
     }
 
     std::string
+    env::get_user_config_directory ()
+    {
+      return (instance_ok ())
+        ? instance->do_get_user_config_directory () : "";
+    }
+
+    std::string
     env::get_program_name (void)
     {
       return (instance_ok ())
@@ -221,6 +234,30 @@
       return tempd;
     }
 
+    std::string
+    env::do_get_user_config_directory (void) const
+    {
+      std::string cfgd;
+
+#if defined (OCTAVE_HAVE_WINDOWS_FILESYSTEM) && defined (OCTAVE_USE_WINDOWS_API)
+      wchar_t path[MAX_PATH];
+      if (SHGetFolderPathW (nullptr, CSIDL_APPDATA, nullptr,
+                            SHGFP_TYPE_CURRENT, path) == S_OK)
+        {
+          char *local_app_data = u8_from_wchar (path);
+          cfgd = local_app_data;
+        }
+#else
+      cfgd = do_getenv ("XDG_CONFIG_HOME");
+
+      if (cfgd.empty ())
+        cfgd = do_get_home_directory () + sys::file_ops::dir_sep_str ()
+             + ".config";
+#endif
+
+      return cfgd;
+    }
+
     // FIXME: this leaves no way to distinguish between a
     // variable that is not set and one that is set to the empty string.
     // Is this a problem?
diff -r 9590067f59dd -r 0050d00a8fc4 liboctave/system/oct-env.h
--- a/liboctave/system/oct-env.h	Sun Mar 10 09:58:58 2019 +0100
+++ b/liboctave/system/oct-env.h	Wed Mar 06 14:54:38 2019 -0800
@@ -65,6 +65,8 @@
 
       static std::string get_temp_directory (void);
 
+      static std::string get_user_config_directory (void);
+
       static std::string get_program_name (void);
 
       static std::string get_program_invocation_name (void);
@@ -104,6 +106,8 @@
 
       std::string do_get_temp_directory (void) const;
 
+      std::string do_get_user_config_directory (void) const;
+
       std::string do_get_user_name (void) const;
 
       std::string do_get_host_name (void) const;
