4 * This file is part of LyX, the document processor.
5 * Licence details can be found in the file COPYING.
7 * \author Angus Leeming
9 * Full author contact details are available in file CREDITS.
11 * Warning! This file is autogenerated from package.C.in.
12 * All changes to this file will be lost.
17 #include "support/package.h"
22 #include "support/environment.h"
23 #include "support/filetools.h"
24 #include "support/lstrings.h"
25 #include "support/os.h"
27 #include <boost/assert.hpp>
28 #include <boost/filesystem/operations.hpp>
29 #include <boost/tuple/tuple.hpp>
34 #if !defined (USE_WINDOWS_PACKAGING) && \
35 !defined (USE_MACOSX_PACKAGING) && \
36 !defined (USE_POSIX_PACKAGING)
37 #error USE_FOO_PACKAGING must be defined for FOO = WINDOWS, MACOSX or POSIX.
40 #if defined (USE_WINDOWS_PACKAGING)
42 # include <shlobj.h> // SHGetFolderPath
45 # ifndef SHGFP_TYPE_CURRENT
46 # define SHGFP_TYPE_CURRENT 0
49 #elif defined (USE_MACOSX_PACKAGING)
50 # include <CoreServices/CoreServices.h> // FSFindFolder, FSRefMakePath
55 namespace fs = boost::filesystem;
63 bool initialised_ = false;
68 void init_package(string const & command_line_arg0,
69 string const & command_line_system_support_dir,
70 string const & command_line_user_support_dir,
71 exe_build_dir_to_top_build_dir top_build_dir_location)
73 // Can do so only once.
77 package_ = Package(command_line_arg0,
78 command_line_system_support_dir,
79 command_line_user_support_dir,
80 top_build_dir_location);
85 Package const & package()
87 // Commented out because package().locale_dir() can be called
88 // from the message translation code in messages.C before
89 // init_package() is called. Lars is on the case...
90 // BOOST_ASSERT(initialised_);
97 string const abs_path_from_binary_name(string const & exe);
99 std::pair<string, string> const
100 get_build_dirs(string const & abs_binary,
101 exe_build_dir_to_top_build_dir top_build_dir_location);
103 string const get_document_dir(string const & home_dir);
105 string const get_home_dir();
107 string const get_locale_dir(string const & system_support_dir);
109 string const get_system_support_dir(string const & abs_binary,
110 string const & command_line_system_support_dir);
112 string const get_temp_dir();
114 string const get_default_user_support_dir(string const & home_dir);
116 std::pair<string, bool> const
117 get_user_support_dir(string const & default_user_support_dir,
118 string const & command_line_user_support_dir);
123 Package::Package(string const & command_line_arg0,
124 string const & command_line_system_support_dir,
125 string const & command_line_user_support_dir,
126 exe_build_dir_to_top_build_dir top_build_dir_location)
127 : explicit_user_support_dir_(false)
129 home_dir_ = get_home_dir();
130 temp_dir_ = get_temp_dir();
131 document_dir_ = get_document_dir(home_dir_);
133 string const abs_binary = abs_path_from_binary_name(command_line_arg0);
134 binary_dir_ = OnlyPath(abs_binary);
136 // Is LyX being run in-place from the build tree?
137 boost::tie(build_support_dir_, system_support_dir_) =
138 get_build_dirs(abs_binary, top_build_dir_location);
140 if (build_support_dir_.empty())
141 system_support_dir_ =
142 get_system_support_dir(abs_binary,
143 command_line_system_support_dir);
145 locale_dir_ = get_locale_dir(system_support_dir_);
147 string const default_user_support_dir =
148 get_default_user_support_dir(home_dir_);
149 boost::tie(user_support_dir_, explicit_user_support_dir_) =
150 get_user_support_dir(default_user_support_dir,
151 command_line_user_support_dir);
155 << "\tbinary_dir " << binary_dir() << '\n'
156 << "\tsystem_support " << system_support() << '\n'
157 << "\tbuild_support " << build_support() << '\n'
158 << "\tuser_support " << user_support() << '\n'
159 << "\tlocale_dir " << locale_dir() << '\n'
160 << "\tdocument_dir " << document_dir() << '\n'
161 << "\ttemp_dir " << temp_dir() << '\n'
162 << "\thome_dir " << home_dir() << '\n'
163 << "</package>\n" << std::endl;
169 // These next three functions contain the stuff that is substituted at
170 // configuration-time.
171 string const top_srcdir()
173 static string const dir("%TOP_SRCDIR%");
178 string const hardcoded_localedir()
180 return string("%LOCALEDIR%");
184 string const hardcoded_system_support_dir()
186 return string("%LYX_DIR%");
192 string const & Package::top_srcdir() const
194 static string const dir("%TOP_SRCDIR%");
201 bool check_command_line_dir(string const & dir,
203 string const & command_line_switch);
205 string const extract_env_var_dir(string const & env_var);
207 bool check_env_var_dir(string const & dir,
208 string const & env_var);
210 bool check_env_var_dir(string const & dir,
212 string const & env_var);
214 string const relative_locale_dir();
216 string const relative_system_support_dir();
219 #if defined (USE_WINDOWS_PACKAGING)
220 // Given a folder ID, returns the folder name (in unix-style format).
221 // Eg CSIDL_PERSONAL -> "C:/Documents and Settings/USERNAME/My Documents"
222 string const win32_folder_path(int folder_id)
224 char folder_path[PATH_MAX + 1];
225 if (SUCCEEDED(SHGetFolderPath(0, folder_id, 0,
226 SHGFP_TYPE_CURRENT, folder_path)))
227 return os::internal_path(folder_path);
234 get_build_support_dir(std::string const & binary_dir,
235 exe_build_dir_to_top_build_dir top_build_dir_location)
238 switch (top_build_dir_location) {
239 case top_build_dir_is_one_level_up:
240 indirection = "../lib";
242 case top_build_dir_is_two_levels_up:
243 indirection = "../../lib";
247 return NormalizePath(AddPath(binary_dir, indirection));
251 std::pair<string, string> const
252 get_build_dirs(string const & abs_binary,
253 exe_build_dir_to_top_build_dir top_build_dir_location)
255 string const check_text = "Checking whether LyX is run in place...";
257 // We're looking for "lyxrc.defaults" in a directory
259 // We're also looking for "chkconfig.ltx" in a directory
260 // binary_dir/top_srcdir()/lib
261 // If both are found, then we're running LyX in-place.
263 // Note that the name of the lyx binary may be a symbolic link.
264 // If that is the case, then we follow the links too.
265 string binary = abs_binary;
267 // Try and find "lyxrc.defaults".
268 string const binary_dir = OnlyPath(binary);
269 string const build_support_dir =
270 get_build_support_dir(binary_dir, top_build_dir_location);
272 if (!FileSearch(build_support_dir, "lyxrc.defaults").empty()) {
273 // Try and find "chkconfig.ltx".
274 string const system_support_dir =
275 MakeAbsPath(AddPath(top_srcdir(), "lib"),
276 AddPath(binary_dir, "support"));
278 if (!FileSearch(system_support_dir, "chkconfig.ltx").empty()) {
279 lyxerr[Debug::INIT] << check_text << " yes"
281 return std::make_pair(build_support_dir, system_support_dir);
285 // Check whether binary is a symbolic link.
286 // If so, resolve it and repeat the exercise.
287 if (!fs::symbolic_link_exists(binary))
291 if (LyXReadLink(binary, link, true)) {
294 // Unable to resolve the link.
299 lyxerr[Debug::INIT] << check_text << " no" << std::endl;
300 return std::make_pair(string(), string());
304 // Specification of document_dir_ may be reset by LyXRC,
305 // but the default is fixed for a given OS.
306 string const get_document_dir(string const & home_dir)
308 #if defined (USE_WINDOWS_PACKAGING)
309 (void)home_dir; // Silence warning about unused variable.
310 return win32_folder_path(CSIDL_PERSONAL);
317 // The specification of home_dir_ is fixed for a given OS.
318 // A typical example on Windows: "C:/Documents and Settings/USERNAME"
319 // and on a Posix-like machine: "/home/USERNAME".
320 string const get_home_dir()
322 #if defined (USE_WINDOWS_PACKAGING)
323 string const home_dir = getEnv("USERPROFILE");
325 string const home_dir = getEnv("HOME");
328 return os::internal_path(home_dir);
332 // Several sources are probed to ascertain the locale directory.
333 // The only requirement is that the result is indeed a directory.
334 string const get_locale_dir(string const & system_support_dir)
336 // 1. Use the "LYX_LOCALEDIR" environment variable.
337 string path = extract_env_var_dir("LYX_LOCALEDIR");
338 if (!path.empty() && check_env_var_dir(path, "LYX_LOCALEDIR"))
341 // 2. Search for system_support_dir / <relative locale dir>
342 // The <relative locale dir> is OS-dependent. (On Unix, it will
344 path = NormalizePath(AddPath(system_support_dir, relative_locale_dir()));
346 if (fs::exists(path) && fs::is_directory(path))
349 // 3. Fall back to the hard-coded LOCALEDIR.
350 path = hardcoded_localedir();
351 if (fs::exists(path) && fs::is_directory(path))
358 // Specification of temp_dir_ may be reset by LyXRC,
359 // but the default is fixed for a given OS.
360 string const get_temp_dir()
362 #if defined (USE_WINDOWS_PACKAGING)
363 // Typical example: C:/TEMP/.
364 char path[PATH_MAX + 1];
365 GetTempPath(PATH_MAX, path);
366 return os::internal_path(path);
373 // If we use a 'lazy' lyxerr in the hope of setting the locale before
374 // printing any messages, then we should ensure that it is flushed first.
377 #ifndef CXX_GLOBAL_CSTD
384 // Extracts the absolute path from the foo of "-sysdir foo" or "-userdir foo"
385 string const abs_path_from_command_line(string const & command_line)
387 if (command_line.empty())
390 string const path = os::internal_path(command_line);
391 return os::is_absolute_path(path) ? path : MakeAbsPath(path);
395 // Does the grunt work for abs_path_from_binary_name()
396 string const get_binary_path(string const & exe)
398 string const exe_path = os::internal_path(exe);
399 if (os::is_absolute_path(exe_path))
402 // Two possibilities present themselves.
403 // 1. The binary is relative to the CWD.
404 string const abs_exe_path = MakeAbsPath(exe_path);
405 if (fs::exists(abs_exe_path))
408 // 2. exe must be the name of the binary only and it
409 // can be found on the PATH.
410 string const exe_name = OnlyFilename(exe_path);
411 if (exe_name != exe_path)
414 std::vector<string> const path = getEnvPath("PATH");
415 std::vector<string>::const_iterator it = path.begin();
416 std::vector<string>::const_iterator const end = path.end();
417 for (; it != end; ++it) {
418 // This will do nothing if *it is already absolute.
419 string const exe_dir = MakeAbsPath(*it);
421 string const exe_path = AddName(exe_dir, exe_name);
422 if (fs::exists(exe_path))
426 // Didn't find anything.
431 // Extracts the absolute path to the binary name received as argv[0].
432 string const abs_path_from_binary_name(string const & exe)
434 string const abs_binary = get_binary_path(exe);
435 if (abs_binary.empty()) {
436 lyxerr << bformat(_("Unable to determine the path to the "
437 "LyX binary from the command line %1$s"),
446 // A plethora of directories is searched to ascertain the system
447 // lyxdir which is defined as the first directory to contain
450 get_system_support_dir(string const & abs_binary,
451 string const & command_line_system_support_dir)
453 string const chkconfig_ltx = "chkconfig.ltx";
455 // searched_dirs is used for diagnostic purposes only in the case
456 // that "chkconfig.ltx" is not found.
457 std::list<string> searched_dirs;
459 // 1. Use the -sysdir command line parameter.
460 string path = abs_path_from_command_line(command_line_system_support_dir);
462 searched_dirs.push_back(path);
463 if (check_command_line_dir(path, chkconfig_ltx, "-sysdir"))
467 // 2. Use the "LYX_DIR_13x" environment variable.
468 path = extract_env_var_dir("LYX_DIR_13x");
470 searched_dirs.push_back(path);
471 if (check_env_var_dir(path, chkconfig_ltx, "LYX_DIR_13x"))
475 // 3. Search relative to the lyx binary.
476 // We're looking for "chkconfig.ltx" in a directory
477 // OnlyPath(abs_binary) / <relative dir> / PACKAGE /
478 // PACKAGE is hardcoded in config.h. Eg "lyx" or "lyx-1.3.6cvs".
479 // <relative dir> is OS-dependent; on Unix, it will be "../share/".
480 string const relative_lyxdir = relative_system_support_dir();
482 // One subtlety to be aware of. The name of the lyx binary may be
483 // a symbolic link. If that is the case, then we follow the links too.
484 string binary = abs_binary;
486 // Try and find "chkconfig.ltx".
487 string const binary_dir = OnlyPath(binary);
489 string const lyxdir =
490 NormalizePath(AddPath(binary_dir, relative_lyxdir));
491 searched_dirs.push_back(lyxdir);
493 if (!FileSearch(lyxdir, chkconfig_ltx).empty()) {
494 // Success! "chkconfig.ltx" has been found.
498 // Check whether binary is a symbolic link.
499 // If so, resolve it and repeat the exercise.
500 if (!fs::symbolic_link_exists(binary))
504 if (LyXReadLink(binary, link, true)) {
507 // Unable to resolve the link.
512 // 4. Repeat the exercise on the directory itself.
513 string binary_dir = OnlyPath(abs_binary);
515 // This time test whether the directory is a symbolic link
516 // *before* looking for "chkconfig.ltx".
517 // (We've looked relative to the original already.)
518 if (!fs::symbolic_link_exists(binary))
522 if (LyXReadLink(binary_dir, link, true)) {
525 // Unable to resolve the link.
529 // Try and find "chkconfig.ltx".
530 string const lyxdir =
531 NormalizePath(AddPath(binary_dir, relative_lyxdir));
532 searched_dirs.push_back(lyxdir);
534 if (!FileSearch(lyxdir, chkconfig_ltx).empty()) {
535 // Success! "chkconfig.ltx" has been found.
540 // 5. In desparation, try the hard-coded system support dir.
541 path = hardcoded_system_support_dir();
542 if (!FileSearch(path, chkconfig_ltx).empty())
545 // Everything has failed :-(
546 // So inform the user and exit.
547 string searched_dirs_str;
548 typedef std::list<string>::const_iterator iterator;
549 iterator const begin = searched_dirs.begin();
550 iterator const end = searched_dirs.end();
551 for (iterator it = begin; it != end; ++it) {
553 searched_dirs_str += "\n\t";
554 searched_dirs_str += *it;
557 lyxerr << bformat(_("Unable to determine the system directory "
560 "Try the '-sysdir' command line parameter or "
561 "set the environment variable LYX_DIR_13x to "
562 "the LyX system directory containing the file "
568 // Keep the compiler happy.
573 // Returns the absolute path to the user lyxdir, together with a flag
574 // indicating whether this directory was specified explicitly (as -userdir
575 // or through an environment variable) or whether it was deduced.
576 std::pair<string, bool> const
577 get_user_support_dir(string const & default_user_support_dir,
578 string const & command_line_user_support_dir)
580 bool explicit_userdir = true;
582 // 1. Use the -userdir command line parameter.
584 abs_path_from_command_line(command_line_user_support_dir);
586 return std::make_pair(path, explicit_userdir);
588 // 2. Use the LYX_USERDIR_13x environment variable.
589 path = extract_env_var_dir("LYX_USERDIR_13x");
591 return std::make_pair(path, explicit_userdir);
593 // 3. Use the OS-dependent default_user_support_dir
594 explicit_userdir = false;
595 return std::make_pair(default_user_support_dir, explicit_userdir);
599 // $HOME/.lyx on all platforms but Win32 where it will be something like
600 // "C:/Documents and Settings/USERNAME/Application Data/lyx"
601 string const get_default_user_support_dir(string const & home_dir)
603 #if defined (USE_WINDOWS_PACKAGING)
604 (void)home_dir; // Silence warning about unused variable.
606 return AddPath(win32_folder_path(CSIDL_APPDATA), PACKAGE);
608 #elif defined (USE_MACOSX_PACKAGING)
609 (void)home_dir; // Silence warning about unused variable.
612 OSErr const error_code =
613 FSFindFolder(kUserDomain, kApplicationSupportFolderType,
614 kDontCreateFolder, &fsref);
618 char store[PATH_MAX + 1];
619 OSStatus const status_code =
620 FSRefMakePath(&fsref,
621 reinterpret_cast<UInt8*>(store), PATH_MAX);
622 if (status_code != 0)
625 return AddPath(reinterpret_cast<char const *>(store), PACKAGE);
627 #else // USE_POSIX_PACKAGING
628 return AddPath(home_dir, string(".") + PACKAGE);
633 // Check that directory @c dir contains @c file.
634 // Else emit a warning about an invalid @c command_line_switch.
635 bool check_command_line_dir(string const & dir,
637 string const & command_line_switch)
639 string const abs_path = FileSearch(dir, file);
640 if (abs_path.empty()) {
641 lyxerr << bformat(_("Invalid %1$s switch.\n"
642 "Directory %2$s does not contain %3$s."),
643 command_line_switch, dir, file)
647 return !abs_path.empty();
651 // The environment variable @c env_var expands to a (single) file path.
652 string const extract_env_var_dir(string const & env_var)
654 string const dir = os::internal_path(getEnv(env_var));
655 return dir.empty() ? dir : MakeAbsPath(dir);
659 // Check that directory @c dir contains @c file.
660 // Else emit a warning about an invalid @c env_var.
661 bool check_env_var_dir(string const & dir,
663 string const & env_var)
665 string const abs_path = FileSearch(dir, file);
666 if (abs_path.empty()) {
667 lyxerr << bformat(_("Invalid %1$s environment variable.\n"
668 "Directory %2$s does not contain %3$s."),
673 return !abs_path.empty();
677 // Check that directory @c dir is indeed a directory.
678 // Else emit a warning about an invalid @c env_var.
679 bool check_env_var_dir(string const & dir,
680 string const & env_var)
682 bool const success = (fs::exists(dir) && fs::is_directory(dir));
685 // Put this string on a single line so that the gettext
686 // search mechanism in po/Makefile.in.in will register
687 // package.C.in as a file containing strings that need
690 _("Invalid %1$s environment variable.\n%2$s is not a directory.");
692 lyxerr << bformat(fmt, env_var, dir)
700 // The locale directory relative to the LyX system directory.
701 string const relative_locale_dir()
707 // The system lyxdir is relative to the directory containing the LyX binary.
708 string const relative_system_support_dir()
712 #if defined (USE_WINDOWS_PACKAGING) || defined (USE_MACOSX_PACKAGING)
713 result = AddPath("../Resources/", PACKAGE);
715 result = AddPath("../share/", PACKAGE);
723 } // namespace support