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/filetools.h"
23 #include "support/lstrings.h"
24 #include "support/os.h"
26 #include <boost/assert.hpp>
27 #include <boost/filesystem/operations.hpp>
28 #include <boost/tuple/tuple.hpp>
33 #if !defined (USE_WINDOWS_PACKAGING) && \
34 !defined (USE_MACOSX_PACKAGING) && \
35 !defined (USE_POSIX_PACKAGING)
36 #error USE_FOO_PACKAGING must be defined for FOO = WINDOWS, MACOSX or POSIX.
39 #if defined (USE_WINDOWS_PACKAGING)
41 # include <shlobj.h> // SHGetFolderPath
44 # ifndef SHGFP_TYPE_CURRENT
45 # define SHGFP_TYPE_CURRENT 0
48 #elif defined (USE_MACOSX_PACKAGING)
49 # include <CoreServices/CoreServices.h> // FSFindFolder, FSRefMakePath
54 namespace fs = boost::filesystem;
62 bool initialised_ = false;
67 void init_package(string const & command_line_arg0,
68 string const & command_line_system_support_dir,
69 string const & command_line_user_support_dir)
71 // Can do so only once.
75 package_ = Package(command_line_arg0,
76 command_line_system_support_dir,
77 command_line_user_support_dir);
82 Package const & package()
84 // Commented out because package().locale_dir() can be called
85 // from the message translation code in messages.C before
86 // init_package() is called. Lars is on the case...
87 // BOOST_ASSERT(initialised_);
94 string const abs_path_from_binary_name(string const & exe);
96 std::pair<string, string> const get_build_dirs(string const & abs_binary);
98 string const get_document_dir(string const & home_dir);
100 string const get_home_dir();
102 string const get_locale_dir(string const & system_support_dir);
104 string const get_system_support_dir(string const & abs_binary,
105 string const & command_line_system_support_dir);
107 string const get_temp_dir();
109 string const get_default_user_support_dir(string const & home_dir);
111 std::pair<string, bool> const
112 get_user_support_dir(string const & default_user_support_dir,
113 string const & command_line_user_support_dir);
118 Package::Package(string const & command_line_arg0,
119 string const & command_line_system_support_dir,
120 string const & command_line_user_support_dir)
121 : explicit_user_support_dir_(false)
123 home_dir_ = get_home_dir();
124 temp_dir_ = get_temp_dir();
125 document_dir_ = get_document_dir(home_dir_);
127 string const abs_binary = abs_path_from_binary_name(command_line_arg0);
128 binary_dir_ = OnlyPath(abs_binary);
130 // Is LyX being run in-place from the build tree?
131 boost::tie(build_support_dir_, system_support_dir_) =
132 get_build_dirs(abs_binary);
134 if (build_support_dir_.empty())
135 system_support_dir_ =
136 get_system_support_dir(abs_binary,
137 command_line_system_support_dir);
139 locale_dir_ = get_locale_dir(system_support_dir_);
141 string const default_user_support_dir =
142 get_default_user_support_dir(home_dir_);
143 boost::tie(user_support_dir_, explicit_user_support_dir_) =
144 get_user_support_dir(default_user_support_dir,
145 command_line_user_support_dir);
149 << "\tbinary_dir " << binary_dir() << '\n'
150 << "\tsystem_support " << system_support() << '\n'
151 << "\tbuild_support " << build_support() << '\n'
152 << "\tuser_support " << user_support() << '\n'
153 << "\tlocale_dir " << locale_dir() << '\n'
154 << "\tdocument_dir " << document_dir() << '\n'
155 << "\ttemp_dir " << temp_dir() << '\n'
156 << "\thome_dir " << home_dir() << '\n'
157 << "</package>\n" << std::endl;
163 // These next three functions contain the stuff that is substituted at
164 // configuration-time.
165 string const top_srcdir()
167 static string const dir("%TOP_SRCDIR%");
172 string const hardcoded_localedir()
174 return string("%LOCALEDIR%");
178 string const hardcoded_system_support_dir()
180 return string("%LYX_DIR%");
186 string const & Package::top_srcdir() const
188 static string const dir("%TOP_SRCDIR%");
195 bool check_command_line_dir(string const & dir,
197 string const & command_line_switch);
199 string const extract_env_var_dir(string const & env_var);
201 bool check_env_var_dir(string const & dir,
202 string const & env_var);
204 bool check_env_var_dir(string const & dir,
206 string const & env_var);
208 string const relative_locale_dir();
210 string const relative_system_support_dir();
213 #if defined (USE_WINDOWS_PACKAGING)
214 // Given a folder ID, returns the folder name (in unix-style format).
215 // Eg CSIDL_PERSONAL -> "C:/Documents and Settings/USERNAME/My Documents"
216 string const win32_folder_path(int folder_id)
218 char folder_path[PATH_MAX + 1];
219 if (SUCCEEDED(SHGetFolderPath(0, folder_id, 0,
220 SHGFP_TYPE_CURRENT, folder_path)))
221 return os::internal_path(folder_path);
227 std::pair<string, string> const get_build_dirs(string const & abs_binary)
229 string const check_text = "Checking whether LyX is run in place...";
231 // We're looking for "lyxrc.defaults" in a directory
233 // We're also looking for "chkconfig.ltx" in a directory
234 // binary_dir/top_srcdir()/lib
235 // If both are found, then we're running LyX in-place.
237 // Note that the name of the lyx binary may be a symbolic link.
238 // If that is the case, then we follow the links too.
239 string binary = abs_binary;
241 // Try and find "lyxrc.defaults".
242 string const binary_dir = OnlyPath(binary);
243 string const build_support_dir =
244 NormalizePath(AddPath(binary_dir, "../lib"));
246 if (!FileSearch(build_support_dir, "lyxrc.defaults").empty()) {
247 // Try and find "chkconfig.ltx".
248 string const system_support_dir =
249 MakeAbsPath(AddPath(top_srcdir(), "lib"),
250 AddPath(binary_dir, "support"));
252 if (!FileSearch(system_support_dir, "chkconfig.ltx").empty()) {
253 lyxerr[Debug::INIT] << check_text << " yes"
255 return std::make_pair(build_support_dir, system_support_dir);
259 // Check whether binary is a symbolic link.
260 // If so, resolve it and repeat the exercise.
261 if (!fs::symbolic_link_exists(binary))
265 if (LyXReadLink(binary, link, true)) {
268 // Unable to resolve the link.
273 lyxerr[Debug::INIT] << check_text << " no" << std::endl;
274 return std::make_pair(string(), string());
278 // Specification of document_dir_ may be reset by LyXRC,
279 // but the default is fixed for a given OS.
280 string const get_document_dir(string const & home_dir)
282 #if defined (USE_WINDOWS_PACKAGING)
283 (void)home_dir; // Silence warning about unused variable.
284 return win32_folder_path(CSIDL_PERSONAL);
291 // The specification of home_dir_ is fixed for a given OS.
292 // A typical example on Windows: "C:/Documents and Settings/USERNAME"
293 // and on a Posix-like machine: "/home/USERNAME".
294 string const get_home_dir()
296 #if defined (USE_WINDOWS_PACKAGING)
297 string const home_dir = GetEnv("USERPROFILE");
299 string const home_dir = GetEnv("HOME");
302 return os::internal_path(home_dir);
306 // Several sources are probed to ascertain the locale directory.
307 // The only requirement is that the result is indeed a directory.
308 string const get_locale_dir(string const & system_support_dir)
310 // 1. Use the "LYX_LOCALEDIR" environment variable.
311 string path = extract_env_var_dir("LYX_LOCALEDIR");
312 if (!path.empty() && check_env_var_dir(path, "LYX_LOCALEDIR"))
315 // 2. Search for system_support_dir / <relative locale dir>
316 // The <relative locale dir> is OS-dependent. (On Unix, it will
318 path = NormalizePath(AddPath(system_support_dir, relative_locale_dir()));
320 if (fs::exists(path) && fs::is_directory(path))
323 // 3. Fall back to the hard-coded LOCALEDIR.
324 path = hardcoded_localedir();
325 if (fs::exists(path) && fs::is_directory(path))
332 // Specification of temp_dir_ may be reset by LyXRC,
333 // but the default is fixed for a given OS.
334 string const get_temp_dir()
336 #if defined (USE_WINDOWS_PACKAGING)
337 // Typical example: C:/TEMP/.
338 char path[PATH_MAX + 1];
339 GetTempPath(PATH_MAX, path);
340 return os::internal_path(path);
347 // If we use a 'lazy' lyxerr in the hope of setting the locale before
348 // printing any messages, then we should ensure that it is flushed first.
351 #ifndef CXX_GLOBAL_CSTD
358 // Extracts the absolute path from the foo of "-sysdir foo" or "-userdir foo"
359 string const abs_path_from_command_line(string const & command_line)
361 if (command_line.empty())
364 string const path = os::internal_path(command_line);
365 return os::is_absolute_path(path) ? path : MakeAbsPath(path);
369 // Does the grunt work for abs_path_from_binary_name()
370 string const get_binary_path(string const & exe)
372 string const exe_path = os::internal_path(exe);
373 if (os::is_absolute_path(exe_path))
376 // Two possibilities present themselves.
377 // 1. The binary is relative to the CWD.
378 string const abs_exe_path = MakeAbsPath(exe_path);
379 if (fs::exists(abs_exe_path))
382 // 2. exe must be the name of the binary only and it
383 // can be found on the PATH.
384 string const exe_name = OnlyFilename(exe_path);
385 if (exe_name != exe_path)
388 std::vector<string> const path = getEnvPath("PATH");
389 std::vector<string>::const_iterator it = path.begin();
390 std::vector<string>::const_iterator const end = path.end();
391 for (; it != end; ++it) {
392 // This will do nothing if *it is already absolute.
393 string const exe_dir = MakeAbsPath(*it);
395 string const exe_path = AddName(exe_dir, exe_name);
396 if (fs::exists(exe_path))
400 // Didn't find anything.
405 // Extracts the absolute path to the binary name received as argv[0].
406 string const abs_path_from_binary_name(string const & exe)
408 string const abs_binary = get_binary_path(exe);
409 if (abs_binary.empty()) {
410 lyxerr << bformat(_("Unable to determine the path to the "
411 "LyX binary from the command line %1$s"),
420 // A plethora of directories is searched to ascertain the system
421 // lyxdir which is defined as the first directory to contain
424 get_system_support_dir(string const & abs_binary,
425 string const & command_line_system_support_dir)
427 string const chkconfig_ltx = "chkconfig.ltx";
429 // searched_dirs is used for diagnostic purposes only in the case
430 // that "chkconfig.ltx" is not found.
431 std::list<string> searched_dirs;
433 // 1. Use the -sysdir command line parameter.
434 string path = abs_path_from_command_line(command_line_system_support_dir);
436 searched_dirs.push_back(path);
437 if (check_command_line_dir(path, chkconfig_ltx, "-sysdir"))
441 // 2. Use the "LYX_DIR_13x" environment variable.
442 path = extract_env_var_dir("LYX_DIR_13x");
444 searched_dirs.push_back(path);
445 if (check_env_var_dir(path, chkconfig_ltx, "LYX_DIR_13x"))
449 // 3. Search relative to the lyx binary.
450 // We're looking for "chkconfig.ltx" in a directory
451 // OnlyPath(abs_binary) / <relative dir> / PACKAGE /
452 // PACKAGE is hardcoded in config.h. Eg "lyx" or "lyx-1.3.6cvs".
453 // <relative dir> is OS-dependent; on Unix, it will be "../share/".
454 string const relative_lyxdir = relative_system_support_dir();
456 // One subtlety to be aware of. The name of the lyx binary may be
457 // a symbolic link. If that is the case, then we follow the links too.
458 string binary = abs_binary;
460 // Try and find "chkconfig.ltx".
461 string const binary_dir = OnlyPath(binary);
463 string const lyxdir =
464 NormalizePath(AddPath(binary_dir, relative_lyxdir));
465 searched_dirs.push_back(lyxdir);
467 if (!FileSearch(lyxdir, chkconfig_ltx).empty()) {
468 // Success! "chkconfig.ltx" has been found.
472 // Check whether binary is a symbolic link.
473 // If so, resolve it and repeat the exercise.
474 if (!fs::symbolic_link_exists(binary))
478 if (LyXReadLink(binary, link, true)) {
481 // Unable to resolve the link.
486 // 4. Repeat the exercise on the directory itself.
487 string binary_dir = OnlyPath(abs_binary);
489 // This time test whether the directory is a symbolic link
490 // *before* looking for "chkconfig.ltx".
491 // (We've looked relative to the original already.)
492 if (!fs::symbolic_link_exists(binary))
496 if (LyXReadLink(binary_dir, link, true)) {
499 // Unable to resolve the link.
503 // Try and find "chkconfig.ltx".
504 string const lyxdir =
505 NormalizePath(AddPath(binary_dir, relative_lyxdir));
506 searched_dirs.push_back(lyxdir);
508 if (!FileSearch(lyxdir, chkconfig_ltx).empty()) {
509 // Success! "chkconfig.ltx" has been found.
514 // 5. In desparation, try the hard-coded system support dir.
515 path = hardcoded_system_support_dir();
516 if (!FileSearch(path, chkconfig_ltx).empty())
519 // Everything has failed :-(
520 // So inform the user and exit.
521 string searched_dirs_str;
522 typedef std::list<string>::const_iterator iterator;
523 iterator const begin = searched_dirs.begin();
524 iterator const end = searched_dirs.end();
525 for (iterator it = begin; it != end; ++it) {
527 searched_dirs_str += "\n\t";
528 searched_dirs_str += *it;
531 lyxerr << bformat(_("Unable to determine the system directory "
534 "Try the '-sysdir' command line parameter or "
535 "set the environment variable LYX_DIR_13x to "
536 "the LyX system directory containing the file "
542 // Keep the compiler happy.
547 // Returns the absolute path to the user lyxdir, together with a flag
548 // indicating whether this directory was specified explicitly (as -userdir
549 // or through an environment variable) or whether it was deduced.
550 std::pair<string, bool> const
551 get_user_support_dir(string const & default_user_support_dir,
552 string const & command_line_user_support_dir)
554 bool explicit_userdir = true;
556 // 1. Use the -userdir command line parameter.
558 abs_path_from_command_line(command_line_user_support_dir);
560 return std::make_pair(path, explicit_userdir);
562 // 2. Use the LYX_USERDIR_13x environment variable.
563 path = extract_env_var_dir("LYX_USERDIR_13x");
565 return std::make_pair(path, explicit_userdir);
567 // 3. Use the OS-dependent default_user_support_dir
568 explicit_userdir = false;
569 return std::make_pair(default_user_support_dir, explicit_userdir);
573 // $HOME/.lyx on all platforms but Win32 where it will be something like
574 // "C:/Documents and Settings/USERNAME/Application Data/lyx"
575 string const get_default_user_support_dir(string const & home_dir)
577 #if defined (USE_WINDOWS_PACKAGING)
578 (void)home_dir; // Silence warning about unused variable.
580 return AddPath(win32_folder_path(CSIDL_APPDATA), PACKAGE);
582 #elif defined (USE_MACOSX_PACKAGING)
583 (void)home_dir; // Silence warning about unused variable.
586 OSErr const error_code =
587 FSFindFolder(kUserDomain, kApplicationSupportFolderType,
588 kDontCreateFolder, &fsref);
592 char store[PATH_MAX + 1];
593 OSStatus const status_code =
594 FSRefMakePath(&fsref,
595 reinterpret_cast<UInt8*>(store), PATH_MAX);
596 if (status_code != 0)
599 return AddPath(reinterpret_cast<char const *>(store), PACKAGE);
601 #else // USE_POSIX_PACKAGING
602 return AddPath(home_dir, string(".") + PACKAGE);
607 // Check that directory @c dir contains @c file.
608 // Else emit a warning about an invalid @c command_line_switch.
609 bool check_command_line_dir(string const & dir,
611 string const & command_line_switch)
613 string const abs_path = FileSearch(dir, file);
614 if (abs_path.empty()) {
615 lyxerr << bformat(_("Invalid %1$s switch.\n"
616 "Directory %2$s does not contain %3$s."),
617 command_line_switch, dir, file)
621 return !abs_path.empty();
625 // The environment variable @c env_var expands to a (single) file path.
626 string const extract_env_var_dir(string const & env_var)
628 string const dir = os::internal_path(GetEnv(env_var));
629 return dir.empty() ? dir : MakeAbsPath(dir);
633 // Check that directory @c dir contains @c file.
634 // Else emit a warning about an invalid @c env_var.
635 bool check_env_var_dir(string const & dir,
637 string const & env_var)
639 string const abs_path = FileSearch(dir, file);
640 if (abs_path.empty()) {
641 lyxerr << bformat(_("Invalid %1$s environment variable.\n"
642 "Directory %2$s does not contain %3$s."),
647 return !abs_path.empty();
651 // Check that directory @c dir is indeed a directory.
652 // Else emit a warning about an invalid @c env_var.
653 bool check_env_var_dir(string const & dir,
654 string const & env_var)
656 bool const success = (fs::exists(dir) && fs::is_directory(dir));
659 // Put this string on a single line so that the gettext
660 // search mechanism in po/Makefile.in.in will register
661 // package.C.in as a file containing strings that need
664 _("Invalid %1$s environment variable.\n%2$s is not a directory.");
666 lyxerr << bformat(fmt, env_var, dir)
674 // The locale directory relative to the LyX system directory.
675 string const relative_locale_dir()
681 // The system lyxdir is relative to the directory containing the LyX binary.
682 string const relative_system_support_dir()
686 #if defined (USE_WINDOWS_PACKAGING) || defined (USE_MACOSX_PACKAGING)
687 result = AddPath("../Resources/", PACKAGE);
689 result = AddPath("../share/", PACKAGE);
697 } // namespace support