]> git.lyx.org Git - lyx.git/blobdiff - src/support/package.C.in
* src/text2.C: deleteEmptyParagraphMechanism(): fix a crash in
[lyx.git] / src / support / package.C.in
index c205d05c91c085dff6c3ab85a5171a61958fa4f1..efe19c62727696173219dfe66d18032765d3f468 100644 (file)
 #include "support/environment.h"
 #include "support/filetools.h"
 #include "support/lstrings.h"
+#include "support/ExceptionMessage.h"
 #include "support/os.h"
 
-#include <boost/assert.hpp>
+#if defined (USE_WINDOWS_PACKAGING)
+# include "support/os_win32.h"
+#endif
+
 #include <boost/filesystem/operations.hpp>
 #include <boost/tuple/tuple.hpp>
 
 #error USE_FOO_PACKAGING must be defined for FOO = WINDOWS, MACOSX or POSIX.
 #endif
 
-#if defined (USE_WINDOWS_PACKAGING)
-
-/*
- * MinGW's version of winver.h contains this comment:
- *
- * If you need Win32 API features newer the Win95 and WinNT then you must
- * define WINVER before including windows.h or any other method of including
- * the windef.h header.
- *
- * GetLongPathNameA requires WINVER == 0x0500.
- *
- * It doesn't matter if the Windows version is older than this because the
- * function will compile but will fail at run time. See
- * http://msdn.microsoft.com/library/en-us/mslu/winprog/microsoft_layer_for_unicode_apis_with_limited_support.asp
- */
-# if defined(__MINGW32__)
-#  define WINVER 0x0500
-# endif
-
-# include <windows.h>
-# include <shlobj.h>  // SHGetFolderPath
-
-  // Needed for MinGW:
-# ifndef SHGFP_TYPE_CURRENT
-#  define SHGFP_TYPE_CURRENT 0
-# endif
-
-#elif defined (USE_MACOSX_PACKAGING)
+#if defined (USE_MACOSX_PACKAGING)
 # include <CoreServices/CoreServices.h> // FSFindFolder, FSRefMakePath
 #endif
 
@@ -135,6 +112,9 @@ std::pair<string, bool> const
 get_user_support_dir(string const & default_user_support_dir,
                     string const & command_line_user_support_dir);
 
+
+string const & with_version_suffix();
+
 } // namespace anon
 
 
@@ -145,11 +125,12 @@ Package::Package(string const & command_line_arg0,
        : explicit_user_support_dir_(false)
 {
        home_dir_ = get_home_dir();
-       temp_dir_ = get_temp_dir();
+       system_temp_dir_ = get_temp_dir();
+       temp_dir_ = system_temp_dir_;
        document_dir_ = get_document_dir(home_dir_);
 
        string const abs_binary = abs_path_from_binary_name(command_line_arg0);
-       binary_dir_ = OnlyPath(abs_binary);
+       binary_dir_ = onlyPath(abs_binary);
 
        // Is LyX being run in-place from the build tree?
        boost::tie(build_support_dir_, system_support_dir_) =
@@ -168,6 +149,10 @@ Package::Package(string const & command_line_arg0,
                get_user_support_dir(default_user_support_dir,
                                     command_line_user_support_dir);
 
+       string const configure_script = addName(system_support(), "configure.py");
+       configure_command_ = os::python() + ' ' + quoteName(configure_script)
+                                      + with_version_suffix();
+
        lyxerr[Debug::INIT]
                << "<package>\n"
                << "\tbinary_dir " << binary_dir() << '\n'
@@ -188,20 +173,29 @@ namespace {
 // configuration-time.
 string const top_srcdir()
 {
-       static string const dir("%TOP_SRCDIR%");
+       static string const dir("@TOP_SRCDIR@");
        return dir;
 }
 
 
 string const hardcoded_localedir()
 {
-       return string("%LOCALEDIR%");
+       return string("@LOCALEDIR@");
 }
 
 
 string const hardcoded_system_support_dir()
 {
-       return string("%LYX_DIR%");
+       return string("@LYX_DIR@");
+}
+
+
+string const & with_version_suffix()
+{
+       static string const program_suffix("@PROGRAM_SUFFIX@");
+       static string const
+               with_version_suffix(" --with-version-suffix=@PROGRAM_SUFFIX@");
+       return program_suffix.empty() ? program_suffix : with_version_suffix;
 }
 
 } // namespace anon
@@ -209,7 +203,7 @@ string const hardcoded_system_support_dir()
 
 string const & Package::top_srcdir() const
 {
-       static string const dir("%TOP_SRCDIR%");
+       static string const dir("@TOP_SRCDIR@");
        return dir;
 }
 
@@ -234,20 +228,6 @@ string const relative_locale_dir();
 string const relative_system_support_dir();
 
 
-#if defined (USE_WINDOWS_PACKAGING)
-// Given a folder ID, returns the folder name (in unix-style format).
-// Eg CSIDL_PERSONAL -> "C:/Documents and Settings/USERNAME/My Documents"
-string const win32_folder_path(int folder_id)
-{
-       char folder_path[PATH_MAX + 1];
-       if (SUCCEEDED(SHGetFolderPath(0, folder_id, 0,
-                                     SHGFP_TYPE_CURRENT, folder_path)))
-               return os::internal_path(folder_path);
-       return string();
-}
-#endif
-
-
 std::string const
 get_build_support_dir(std::string const & binary_dir,
                      exe_build_dir_to_top_build_dir top_build_dir_location)
@@ -262,7 +242,7 @@ get_build_support_dir(std::string const & binary_dir,
                break;
        }
 
-       return NormalizePath(AddPath(binary_dir, indirection));
+       return normalizePath(addPath(binary_dir, indirection));
 }
 
 
@@ -272,10 +252,10 @@ get_build_dirs(string const & abs_binary,
 {
        string const check_text = "Checking whether LyX is run in place...";
 
-       // We're looking for "lyxrc.defaults" in a directory
+       // We're looking for "Makefile" in a directory
        //   binary_dir/../lib
        // We're also looking for "chkconfig.ltx" in a directory
-       //   binary_dir/top_srcdir()/lib
+       //   top_srcdir()/lib
        // If both are found, then we're running LyX in-place.
 
        // Note that the name of the lyx binary may be a symbolic link.
@@ -283,17 +263,16 @@ get_build_dirs(string const & abs_binary,
        string binary = abs_binary;
        while (true) {
                // Try and find "lyxrc.defaults".
-               string const binary_dir = OnlyPath(binary);
+               string const binary_dir = onlyPath(binary);
                string const build_support_dir =
                        get_build_support_dir(binary_dir, top_build_dir_location);
 
-               if (!FileSearch(build_support_dir, "lyxrc.defaults").empty()) {
+               if (!fileSearch(build_support_dir, "Makefile").empty()) {
                        // Try and find "chkconfig.ltx".
                        string const system_support_dir =
-                               MakeAbsPath(AddPath(top_srcdir(), "lib"),
-                                           AddPath(binary_dir, "support"));
+                               addPath(top_srcdir(), "lib");
 
-                       if (!FileSearch(system_support_dir, "chkconfig.ltx").empty()) {
+                       if (!fileSearch(system_support_dir, "chkconfig.ltx").empty()) {
                                lyxerr[Debug::INIT] << check_text << " yes"
                                                    << std::endl;
                                return std::make_pair(build_support_dir, system_support_dir);
@@ -302,11 +281,11 @@ get_build_dirs(string const & abs_binary,
 
                // Check whether binary is a symbolic link.
                // If so, resolve it and repeat the exercise.
-               if (!fs::symbolic_link_exists(binary))
+               if (!fs::symbolic_link_exists(FileName(binary).toFilesystemEncoding()))
                        break;
 
                string link;
-               if (LyXReadLink(binary, link, true)) {
+               if (readLink(binary, link, true)) {
                        binary = link;
                } else {
                        // Unable to resolve the link.
@@ -325,7 +304,8 @@ string const get_document_dir(string const & home_dir)
 {
 #if defined (USE_WINDOWS_PACKAGING)
        (void)home_dir; // Silence warning about unused variable.
-       return win32_folder_path(CSIDL_PERSONAL);
+       os::GetFolderPath win32_folder_path;
+       return win32_folder_path(os::GetFolderPath::PERSONAL);
 #else // Posix-like.
        return home_dir;
 #endif
@@ -352,22 +332,24 @@ string const get_home_dir()
 string const get_locale_dir(string const & system_support_dir)
 {
        // 1. Use the "LYX_LOCALEDIR" environment variable.
-       string path = extract_env_var_dir("LYX_LOCALEDIR");
-       if (!path.empty() && check_env_var_dir(path, "LYX_LOCALEDIR"))
-               return path;
+       string const path_env = extract_env_var_dir("LYX_LOCALEDIR");
+       if (!path_env.empty() && check_env_var_dir(path_env, "LYX_LOCALEDIR"))
+               return path_env;
 
        // 2. Search for system_support_dir / <relative locale dir>
        // The <relative locale dir> is OS-dependent. (On Unix, it will
        // be "../locale/".)
-       path = NormalizePath(AddPath(system_support_dir, relative_locale_dir()));
+       FileName path(normalizePath(addPath(system_support_dir, relative_locale_dir())));
 
-       if (fs::exists(path) && fs::is_directory(path))
-               return path;
+       if (fs::exists(path.toFilesystemEncoding()) &&
+           fs::is_directory(path.toFilesystemEncoding()))
+               return path.absFilename();
 
        // 3. Fall back to the hard-coded LOCALEDIR.
-       path = hardcoded_localedir();
-       if (fs::exists(path) && fs::is_directory(path))
-               return path;
+       path = FileName(hardcoded_localedir());
+       if (fs::exists(path.toFilesystemEncoding()) &&
+           fs::is_directory(path.toFilesystemEncoding()))
+               return path.absFilename();
 
        return string();
 }
@@ -379,9 +361,8 @@ string const get_temp_dir()
 {
 #if defined (USE_WINDOWS_PACKAGING)
        // Typical example: C:/TEMP/.
-       char path[PATH_MAX + 1];
-       GetTempPath(PATH_MAX, path);
-       GetLongPathName(path, path, PATH_MAX + 1);
+       char path[MAX_PATH];
+       GetTempPath(MAX_PATH, path);
        return os::internal_path(path);
 #else // Posix-like.
        return "/tmp";
@@ -389,17 +370,6 @@ string const get_temp_dir()
 }
 
 
-// If we use a 'lazy' lyxerr in the hope of setting the locale before
-// printing any messages, then we should ensure that it is flushed first.
-void bail_out()
-{
-#ifndef CXX_GLOBAL_CSTD
-       using std::exit;
-#endif
-       exit(1);
-}
-
-
 // Extracts the absolute path from the foo of "-sysdir foo" or "-userdir foo"
 string const abs_path_from_command_line(string const & command_line)
 {
@@ -407,7 +377,7 @@ string const abs_path_from_command_line(string const & command_line)
                return string();
 
        string const path = os::internal_path(command_line);
-       return os::is_absolute_path(path) ? path : MakeAbsPath(path);
+       return os::is_absolute_path(path) ? path : makeAbsPath(path).absFilename();
 }
 
 
@@ -429,13 +399,13 @@ string const get_binary_path(string const & exe)
 
        // Two possibilities present themselves.
        // 1. The binary is relative to the CWD.
-       string const abs_exe_path = MakeAbsPath(exe_path);
-       if (fs::exists(abs_exe_path))
+       string const abs_exe_path = makeAbsPath(exe_path).absFilename();
+       if (fs::exists(FileName(abs_exe_path).toFilesystemEncoding()))
                return abs_exe_path;
 
        // 2. exe must be the name of the binary only and it
        // can be found on the PATH.
-       string const exe_name = OnlyFilename(exe_path);
+       string const exe_name = onlyFilename(exe_path);
        if (exe_name != exe_path)
                return string();
 
@@ -444,10 +414,10 @@ string const get_binary_path(string const & exe)
        std::vector<string>::const_iterator const end = path.end();
        for (; it != end; ++it) {
                // This will do nothing if *it is already absolute.
-               string const exe_dir = MakeAbsPath(*it);
+               string const exe_dir = makeAbsPath(*it).absFilename();
 
-               string const exe_path = AddName(exe_dir, exe_name);
-               if (fs::exists(exe_path))
+               string const exe_path = addName(exe_dir, exe_name);
+               if (fs::exists(FileName(exe_path).toFilesystemEncoding()))
                        return exe_path;
        }
 
@@ -461,11 +431,11 @@ string const abs_path_from_binary_name(string const & exe)
 {
        string const abs_binary = get_binary_path(exe);
        if (abs_binary.empty()) {
-               lyxerr << bformat(_("Unable to determine the path to the "
-                                   "LyX binary from the command line %1$s"),
-                                 exe)
-                      << std::endl;
-               bail_out();
+               // FIXME UNICODE
+               throw ExceptionMessage(ErrorException,
+                       _("LyX binary not found"),
+                       bformat(_("Unable to determine the path to the LyX binary from the command line %1$s"),
+                               lyx::from_utf8(exe)));
        }
        return abs_binary;
 }
@@ -492,11 +462,11 @@ get_system_support_dir(string const & abs_binary,
                        return path;
        }
 
-       // 2. Use the "LYX_DIR_14x" environment variable.
-       path = extract_env_var_dir("LYX_DIR_14x");
+       // 2. Use the "LYX_DIR_15x" environment variable.
+       path = extract_env_var_dir("LYX_DIR_15x");
        if (!path.empty()) {
                searched_dirs.push_back(path);
-               if (check_env_var_dir(path, chkconfig_ltx, "LYX_DIR_14x"))
+               if (check_env_var_dir(path, chkconfig_ltx, "LYX_DIR_15x"))
                        return path;
        }
 
@@ -512,24 +482,24 @@ get_system_support_dir(string const & abs_binary,
        string binary = abs_binary;
        while (true) {
                // Try and find "chkconfig.ltx".
-               string const binary_dir = OnlyPath(binary);
+               string const binary_dir = onlyPath(binary);
 
                string const lyxdir =
-                       NormalizePath(AddPath(binary_dir, relative_lyxdir));
+                       normalizePath(addPath(binary_dir, relative_lyxdir));
                searched_dirs.push_back(lyxdir);
 
-               if (!FileSearch(lyxdir, chkconfig_ltx).empty()) {
+               if (!fileSearch(lyxdir, chkconfig_ltx).empty()) {
                        // Success! "chkconfig.ltx" has been found.
                        return lyxdir;
                }
 
                // Check whether binary is a symbolic link.
                // If so, resolve it and repeat the exercise.
-               if (!fs::symbolic_link_exists(binary))
+               if (!fs::symbolic_link_exists(FileName(binary).toFilesystemEncoding()))
                        break;
 
                string link;
-               if (LyXReadLink(binary, link, true)) {
+               if (readLink(binary, link, true)) {
                        binary = link;
                } else {
                        // Unable to resolve the link.
@@ -538,16 +508,16 @@ get_system_support_dir(string const & abs_binary,
        }
 
        // 4. Repeat the exercise on the directory itself.
-       string binary_dir = OnlyPath(abs_binary);
+       string binary_dir = onlyPath(abs_binary);
        while (true) {
                // This time test whether the directory is a symbolic link
                // *before* looking for "chkconfig.ltx".
                // (We've looked relative to the original already.)
-               if (!fs::symbolic_link_exists(binary))
+               if (!fs::symbolic_link_exists(FileName(binary).toFilesystemEncoding()))
                        break;
 
                string link;
-               if (LyXReadLink(binary_dir, link, true)) {
+               if (readLink(binary_dir, link, true)) {
                        binary_dir = link;
                } else {
                        // Unable to resolve the link.
@@ -556,10 +526,10 @@ get_system_support_dir(string const & abs_binary,
 
                // Try and find "chkconfig.ltx".
                string const lyxdir =
-                       NormalizePath(AddPath(binary_dir, relative_lyxdir));
+                       normalizePath(addPath(binary_dir, relative_lyxdir));
                searched_dirs.push_back(lyxdir);
 
-               if (!FileSearch(lyxdir, chkconfig_ltx).empty()) {
+               if (!fileSearch(lyxdir, chkconfig_ltx).empty()) {
                        // Success! "chkconfig.ltx" has been found.
                        return lyxdir;
                }
@@ -567,7 +537,7 @@ get_system_support_dir(string const & abs_binary,
 
        // 5. In desparation, try the hard-coded system support dir.
        path = hardcoded_system_support_dir();
-       if (!FileSearch(path, chkconfig_ltx).empty())
+       if (!fileSearch(path, chkconfig_ltx).empty())
                return path;
 
        // Everything has failed :-(
@@ -582,17 +552,17 @@ get_system_support_dir(string const & abs_binary,
                searched_dirs_str += *it;
        }
 
-       lyxerr << bformat(_("Unable to determine the system directory "
-                           "having searched\n"
-                           "\t%1$s\n"
-                           "Try the '-sysdir' command line parameter or "
-                           "set the environment variable LYX_DIR_14x to "
-                           "the LyX system directory containing the file "
-                           "`chkconfig.ltx'."),
-                         searched_dirs_str)
-              << std::endl;
-
-       bail_out();
+       // FIXME UNICODE
+       throw ExceptionMessage(ErrorException, _("No system directory"),
+               bformat(_("Unable to determine the system directory "
+                                        "having searched\n"
+                                        "\t%1$s\n"
+                                        "Use the '-sysdir' command line parameter or "
+                                        "set the environment variable LYX_DIR_15x to "
+                                        "the LyX system directory containing the file "
+                                        "`chkconfig.ltx'."),
+                         lyx::from_utf8(searched_dirs_str)));
+
        // Keep the compiler happy.
        return string();
 }
@@ -613,8 +583,8 @@ get_user_support_dir(string const & default_user_support_dir,
        if (!path.empty())
                return std::make_pair(path, explicit_userdir);
 
-       // 2. Use the LYX_USERDIR_14x environment variable.
-       path = extract_env_var_dir("LYX_USERDIR_14x");
+       // 2. Use the LYX_USERDIR_15x environment variable.
+       path = extract_env_var_dir("LYX_USERDIR_15x");
        if (!path.empty())
                return std::make_pair(path, explicit_userdir);
 
@@ -624,14 +594,15 @@ get_user_support_dir(string const & default_user_support_dir,
 }
 
 
-// $HOME/.lyx on all platforms but Win32 where it will be something like
-// "C:/Documents and Settings/USERNAME/Application Data/lyx"
+// $HOME/.lyx on POSIX but on Win32 it will be something like
+// "C:/Documents and Settings/USERNAME/Application Data/LyX"
 string const get_default_user_support_dir(string const & home_dir)
 {
 #if defined (USE_WINDOWS_PACKAGING)
        (void)home_dir; // Silence warning about unused variable.
 
-       return AddPath(win32_folder_path(CSIDL_APPDATA), PACKAGE);
+       os::GetFolderPath win32_folder_path;
+       return addPath(win32_folder_path(os::GetFolderPath::APPDATA), PACKAGE);
 
 #elif defined (USE_MACOSX_PACKAGING)
        (void)home_dir; // Silence warning about unused variable.
@@ -650,10 +621,10 @@ string const get_default_user_support_dir(string const & home_dir)
        if (status_code != 0)
                return string();
 
-       return AddPath(reinterpret_cast<char const *>(store), PACKAGE);
+       return addPath(reinterpret_cast<char const *>(store), PACKAGE);
 
 #else // USE_POSIX_PACKAGING
-       return AddPath(home_dir, string(".") + PACKAGE);
+       return addPath(home_dir, string(".") + PACKAGE);
 #endif
 }
 
@@ -664,12 +635,13 @@ bool check_command_line_dir(string const & dir,
                            string const & file,
                            string const & command_line_switch)
 {
-       string const abs_path = FileSearch(dir, file);
+       FileName const abs_path = fileSearch(dir, file);
        if (abs_path.empty()) {
-               lyxerr << bformat(_("Invalid %1$s switch.\n"
-                                   "Directory %2$s does not contain %3$s."),
-                                 command_line_switch, dir, file)
-                      << std::endl;
+               // FIXME UNICODE
+               throw ExceptionMessage(WarningException, _("File not found"), bformat(
+                       _("Invalid %1$s switch.\nDirectory %2$s does not contain %3$s."),
+                       lyx::from_utf8(command_line_switch), lyx::from_utf8(dir),
+                       lyx::from_utf8(file)));
        }
 
        return !abs_path.empty();
@@ -680,7 +652,7 @@ bool check_command_line_dir(string const & dir,
 string const extract_env_var_dir(string const & env_var)
 {
        string const dir = os::internal_path(getEnv(env_var));
-       return dir.empty() ? dir : MakeAbsPath(dir);
+       return dir.empty() ? dir : makeAbsPath(dir).absFilename();
 }
 
 
@@ -690,12 +662,14 @@ bool check_env_var_dir(string const & dir,
                       string const & file,
                       string const & env_var)
 {
-       string const abs_path = FileSearch(dir, file);
+       FileName const abs_path = fileSearch(dir, file);
        if (abs_path.empty()) {
-               lyxerr << bformat(_("Invalid %1$s environment variable.\n"
-                                   "Directory %2$s does not contain %3$s."),
-                                 env_var, dir, file)
-                      << std::endl;
+               // FIXME UNICODE
+               throw ExceptionMessage(WarningException, _("File not found"), bformat(
+                       _("Invalid %1$s environment variable.\n"
+                               "Directory %2$s does not contain %3$s."),
+                       lyx::from_utf8(env_var), lyx::from_utf8(dir),
+                       lyx::from_utf8(file)));
        }
 
        return !abs_path.empty();
@@ -707,18 +681,20 @@ bool check_env_var_dir(string const & dir,
 bool check_env_var_dir(string const & dir,
                       string const & env_var)
 {
-       bool const success = (fs::exists(dir) && fs::is_directory(dir));
+       string const encoded(FileName(dir).toFilesystemEncoding());
+       bool const success = (fs::exists(encoded) && fs::is_directory(encoded));
 
        if (!success) {
                // Put this string on a single line so that the gettext
                // search mechanism in po/Makefile.in.in will register
                // package.C.in as a file containing strings that need
                // translation.
-               string const fmt =
-               _("Invalid %1$s environment variable.\n%2$s is not a directory.");
+               // FIXME UNICODE
+               docstring const fmt =
+                       _("Invalid %1$s environment variable.\n%2$s is not a directory.");
 
-               lyxerr << bformat(fmt, env_var, dir)
-                      << std::endl;
+               throw ExceptionMessage(WarningException, _("Directory not found"), bformat(
+                       fmt, lyx::from_utf8(env_var), lyx::from_utf8(dir)));
        }
 
        return success;
@@ -728,7 +704,11 @@ bool check_env_var_dir(string const & dir,
 // The locale directory relative to the LyX system directory.
 string const relative_locale_dir()
 {
+#if defined (USE_WINDOWS_PACKAGING) || defined (USE_MACOSX_PACKAGING)
+       return "locale/";
+#else
        return "../locale/";
+#endif
 }
 
 
@@ -738,9 +718,9 @@ string const relative_system_support_dir()
        string result;
 
 #if defined (USE_WINDOWS_PACKAGING) || defined (USE_MACOSX_PACKAGING)
-       result = AddPath("../Resources/", PACKAGE);
+       result = "../Resources/";
 #else // Posix-like.
-       result = AddPath("../share/", PACKAGE);
+       result = addPath("../share/", PACKAGE);
 #endif
 
        return result;