From 8b136dd58ac520ebb21a98f54d262f1c12ea5457 Mon Sep 17 00:00:00 2001
From: "Avi Halachmi (:avih)" <avihpit@yahoo.com>
Date: Thu, 9 Apr 2015 06:53:29 +0300
Subject: [PATCH] fix datadir search - include user settings

at fileutils.cc:
- removed redundant logic when searching data files.
- now using start_path as fallback data-dir for all cases instead of only for
  the case where path is not given and input_file_name doesn't have a path.

at source-highlight.cc
- refined the logic of data-dir search. now it starts with retrieveDataDir(),
  overridden with --data-dir, and fallback to <bin-dir>../share/source-highlight/
  (previously it was effectively never using retrieveDataDir(), so it was
  ignoring user settings within $HOME, env vars, etc)
---
 lib/srchilite/fileutil.cc | 48 ++++++++++++-----------------------------------
 src/source-highlight.cc   | 41 ++++++++++++++++++++--------------------
 2 files changed, 33 insertions(+), 56 deletions(-)

diff --git a/lib/srchilite/fileutil.cc b/lib/srchilite/fileutil.cc
index b4b95a6..59a6d64 100644
--- a/lib/srchilite/fileutil.cc
+++ b/lib/srchilite/fileutil.cc
@@ -206,29 +206,17 @@ istream *open_data_file_istream(const string &path,
         throw IOException("empty file name", input_file_name);
 
     istream *in = 0;
-    if (input_file_name.size() && contains_path(input_file_name)) {
+    if (contains_path(input_file_name)) {
         in = _open_data_file_istream("", input_file_name);
-        if (!in) {
-            throw IOException("cannot open", input_file_name);
-        }
-    } else if (path.size() && input_file_name.size()) {
-        const string file = (path.size() ? path + "/" : "") + input_file_name;
+    } else if (path.size()) {
         in = _open_data_file_istream(path, input_file_name);
-        if (!in) {
-            throw IOException("cannot open", file);
-        }
     } else {
-        string _path = path;
-        string _file = input_file_name;
-        bool has_path = contains_path(input_file_name);
-        if (!path.size() && !has_path)
-            _path = ".";
-
-        in = _open_data_file_istream(_path, _file);
-        if (!in && !path.size() && !has_path)
-            in = _open_data_file_istream(start, _file);
+        in = _open_data_file_istream(".", input_file_name);
     }
 
+    if (!in)  // falback if all else failed, start defaults to start_path
+        in = _open_data_file_istream(start, input_file_name);
+
     if (!in)
         throw IOException("cannot find input file anywhere", input_file_name);
 
@@ -248,29 +236,17 @@ FILE *open_data_file_stream(const string &path, const string &input_file_name,
         throw IOException("empty file name", input_file_name);
 
     FILE *in = 0;
-    if (input_file_name.size() && contains_path(input_file_name)) {
+    if (contains_path(input_file_name)) {
         in = _open_data_file_stream("", input_file_name);
-        if (!in) {
-            throw IOException("cannot open", input_file_name);
-        }
-    } else if (path.size() && input_file_name.size()) {
-        const string file = (path.size() ? path + "/" : "") + input_file_name;
+    } else if (path.size()) {
         in = _open_data_file_stream(path, input_file_name);
-        if (!in) {
-            throw IOException("cannot open", file);
-        }
     } else {
-        string _path = path;
-        string _file = input_file_name;
-        bool has_path = contains_path(input_file_name);
-        if (!path.size() && !has_path)
-            _path = ".";
-
-        in = _open_data_file_stream(_path, _file);
-        if (!in && !path.size() && !has_path)
-            in = _open_data_file_stream(start, _file);
+        in = _open_data_file_stream(".", input_file_name);
     }
 
+    if (!in)  // falback if all else failed, start defaults to start_path
+        in = _open_data_file_stream(start, input_file_name);
+
     if (!in)
         throw IOException("cannot find input file anywhere", input_file_name);
 
diff --git a/src/source-highlight.cc b/src/source-highlight.cc
index 4f813e6..4feba30 100644
--- a/src/source-highlight.cc
+++ b/src/source-highlight.cc
@@ -203,34 +203,35 @@ int main(int argc, char * argv[]) {
     // the default for output format is html
     outputFormat = args_info.out_format_arg;
 
-    if (args_info.data_dir_given) {
-        dataDir = args_info.data_dir_arg;
-    }
-
     if (args_info.output_dir_given) {
         outputDir = args_info.output_dir_arg;
     }
 
     /*
-     the starting default path to search for files is computed at
-     run-time: it is
-     the path of the binary + ".." + RELATIVEDATADIR
-     this should make the package relocable (i.e., not stuck
-     with a fixed installation directory).
+     The starting default path is from Settings::retrieveDataDir() which
+     does the heavy lifting of finding the data dir by looking at config-time
+     values, environment variables, user settings at $HOME, etc. See settings.h
+
+     From here, invoking with --data-dir=<my-datadir> overrides the above value.
+
+     We also use a fallback dir which is calculated in runtime relative to this
+     executable (by default <executable-dir>/../share/source-highlight/).
+
+     this should make the package relocatable (i.e., not stuck with a fixed
+     installation directory).
      Of course, the GNU standards for installation directories
      should be followed, but this is not a problem if you use
      configure and make install features.
-     If no path is specified in the running program we go back to
-     the absolute datadir.
-     */
-    // this is defined in fileutil.cc
-    string prefix_dir = get_file_path(argv[0]);
-    if (prefix_dir.size())
-        start_path = get_file_path(argv[0]) + RELATIVEDATADIR;
-    else
-        start_path = Settings::retrieveDataDir();
-
-    // if datadir is not specified, we rely on start_path?
+    */
+
+    dataDir = Settings::retrieveDataDir();
+    if (args_info.data_dir_given)
+        dataDir = args_info.data_dir_arg;
+
+    // start_path is global fallback for dataDir - used at fileutil.h
+    string executable_dir = get_file_path(argv[0]);
+    if (executable_dir.size())
+        start_path = executable_dir + RELATIVEDATADIR;
 
     try {
         // initialize map files
-- 
1.9.1

