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/FileInfo.h"
23 #include "support/filetools.h"
24 #include "support/lstrings.h"
25 #include "support/os.h"
27 #include <boost/assert.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
61 bool initialised_ = false;
66 void init_package(string const & command_line_arg0,
67 string const & command_line_system_support_dir,
68 string const & command_line_user_support_dir)
70 // Can do so only once.
74 package_ = Package(command_line_arg0,
75 command_line_system_support_dir,
76 command_line_user_support_dir);
81 Package const & package()
83 // BOOST_ASSERT(initialised_);
90 string const abs_path_from_binary_name(string const & exe);
92 std::pair<string, string> const get_build_dirs(string const & abs_binary);
94 string const get_document_dir(string const & home_dir);
96 string const get_home_dir();
98 string const get_locale_dir(string const & system_support_dir);
100 string const get_system_support_dir(string const & abs_binary,
101 string const & command_line_system_support_dir);
103 string const get_temp_dir();
105 string const get_default_user_support_dir(string const & home_dir);
107 std::pair<string, bool> const
108 get_user_support_dir(string const & default_user_support_dir,
109 string const & command_line_user_support_dir);
114 Package::Package(string const & command_line_arg0,
115 string const & command_line_system_support_dir,
116 string const & command_line_user_support_dir)
117 : explicit_user_support_dir_(false)
119 home_dir_ = get_home_dir();
120 temp_dir_ = get_temp_dir();
121 document_dir_ = get_document_dir(home_dir_);
123 string const abs_binary = abs_path_from_binary_name(command_line_arg0);
124 binary_dir_ = OnlyPath(abs_binary);
126 // Is LyX being run in-place from the build tree?
127 boost::tie(build_support_dir_, system_support_dir_) =
128 get_build_dirs(abs_binary);
130 if (build_support_dir_.empty())
131 system_support_dir_ =
132 get_system_support_dir(abs_binary,
133 command_line_system_support_dir);
135 locale_dir_ = get_locale_dir(system_support_dir_);
137 string const default_user_support_dir =
138 get_default_user_support_dir(home_dir_);
139 boost::tie(user_support_dir_, explicit_user_support_dir_) =
140 get_user_support_dir(default_user_support_dir,
141 command_line_user_support_dir);
145 << "\tbinary_dir " << binary_dir() << '\n'
146 << "\tsystem_support " << system_support() << '\n'
147 << "\tbuild_support " << build_support() << '\n'
148 << "\tuser_support " << user_support() << '\n'
149 << "\tlocale_dir " << locale_dir() << '\n'
150 << "\tdocument_dir " << document_dir() << '\n'
151 << "\ttemp_dir " << temp_dir() << '\n'
152 << "\thome_dir " << home_dir() << '\n'
153 << "<\\package>\n" << std::endl;
159 // These next three functions contain the stuff that is substituted at
160 // configuration-time.
161 string const top_srcdir()
163 static string const dir("%TOP_SRCDIR%");
168 string const hardcoded_localedir()
170 return string("%LOCALEDIR%");
174 string const hardcoded_system_support_dir()
176 return string("%LYX_DIR%");
182 string const & Package::top_srcdir() const
184 static string const dir("%TOP_SRCDIR%");
191 bool check_command_line_dir(string const & dir,
193 string const & command_line_switch);
195 string const extract_env_var_dir(string const & env_var);
197 bool check_env_var_dir(string const & dir,
198 string const & env_var);
200 bool check_env_var_dir(string const & dir,
202 string const & env_var);
204 string const relative_locale_dir();
206 string const relative_system_support_dir();
209 #if defined (USE_WINDOWS_PACKAGING)
210 // Given a folder ID, returns the folder name (in unix-style format).
211 // Eg CSIDL_PERSONAL -> "C:/Documents and Settings/USERNAME/My Documents"
212 string const win32_folder_path(int folder_id)
214 char folder_path[PATH_MAX + 1];
215 if (SUCCEEDED(SHGetFolderPath(0, folder_id, 0,
216 SHGFP_TYPE_CURRENT, folder_path)))
217 return os::internal_path(folder_path);
223 std::pair<string, string> const get_build_dirs(string const & abs_binary)
225 string const check_text = "Checking whether LyX is run in place...";
227 // We're looking for "lyxrc.defaults" in a directory
229 // We're also looking for "chkconfig.ltx" in a directory
230 // binary_dir/top_srcdir()/lib
231 // If both are found, then we're running LyX in-place.
233 // Note that the name of the lyx binary may be a symbolic link.
234 // If that is the case, then we follow the links too.
235 string binary = abs_binary;
237 // Try and find "lyxrc.defaults".
238 string const binary_dir = OnlyPath(binary);
239 string const build_support_dir =
240 NormalizePath(AddPath(binary_dir, "../lib"));
242 if (!FileSearch(build_support_dir, "lyxrc.defaults").empty()) {
243 // Try and find "chkconfig.ltx".
244 string const system_support_dir =
245 MakeAbsPath(AddPath(top_srcdir(), "lib"),
246 AddPath(binary_dir, "support"));
248 if (!FileSearch(system_support_dir, "chkconfig.ltx").empty()) {
249 lyxerr[Debug::INIT] << check_text << " yes"
251 return std::make_pair(build_support_dir, system_support_dir);
255 // Check whether binary is a symbolic link.
256 // If so, resolve it and repeat the exercise.
257 FileInfo const file(binary, true);
258 if (!file.isOK() || !file.isLink())
262 if (LyXReadLink(binary, link, true)) {
265 // Unable to resolve the link.
270 lyxerr[Debug::INIT] << check_text << " no" << std::endl;
271 return std::make_pair(string(), string());
275 // Specification of document_dir_ may be reset by LyXRC,
276 // but the default is fixed for a given OS.
277 string const get_document_dir(string const & home_dir)
279 #if defined (USE_WINDOWS_PACKAGING)
280 (void)home_dir; // Silence warning about unused variable.
281 return win32_folder_path(CSIDL_PERSONAL);
288 // The specification of home_dir_ is fixed for a given OS.
289 // A typical example on Windows: "C:/Documents and Settings/USERNAME"
290 // and on a Posix-like machine: "/home/USERNAME".
291 string const get_home_dir()
293 #if defined (USE_WINDOWS_PACKAGING)
294 string const home_dir = GetEnv("USERPROFILE");
296 string const home_dir = GetEnv("HOME");
299 return os::internal_path(home_dir);
303 // Several sources are probed to ascertain the locale directory.
304 // The only requirement is that the result is indeed a directory.
305 string const get_locale_dir(string const & system_support_dir)
307 // 1. Use the "LYX_LOCALEDIR" environment variable.
308 string path = extract_env_var_dir("LYX_LOCALEDIR");
309 if (!path.empty() && check_env_var_dir(path, "LYX_LOCALEDIR"))
312 // 2. Search for system_support_dir / <relative locale dir>
313 // The <relative locale dir> is OS-dependent. (On Unix, it will
315 path = NormalizePath(AddPath(system_support_dir, relative_locale_dir()));
318 if (fi.isOK() && fi.isDir())
321 // 3. Fall back to the hard-coded LOCALEDIR.
322 path = hardcoded_localedir();
324 if (fi2.isOK() && fi2.isDir())
331 // Specification of temp_dir_ may be reset by LyXRC,
332 // but the default is fixed for a given OS.
333 string const get_temp_dir()
335 #if defined (USE_WINDOWS_PACKAGING)
336 // Typical example: C:/TEMP/.
337 char path[PATH_MAX + 1];
338 GetTempPath(PATH_MAX, path);
339 return os::internal_path(path);
346 // If we use a 'lazy' lyxerr in the hope of setting the locale before
347 // printing any messages, then we should ensure that it is flushed first.
350 #ifndef CXX_GLOBAL_CSTD
357 // Extracts the absolute path from the foo of "-sysdir foo" or "-userdir foo"
358 string const abs_path_from_command_line(string const & command_line)
360 if (command_line.empty())
363 string const path = os::internal_path(command_line);
364 return os::is_absolute_path(path) ? path : MakeAbsPath(path);
368 // Does the grunt work for abs_path_from_binary_name()
369 string const get_binary_path(string const & exe)
371 string const exe_path = os::internal_path(exe);
372 if (os::is_absolute_path(exe_path))
375 // Two possibilities present themselves.
376 // 1. The binary is relative to the CWD.
377 string const abs_exe_path = MakeAbsPath(exe_path);
378 if (FileInfo(abs_exe_path, true).isOK())
381 // 2. exe must be the name of the binary only and it
382 // can be found on the PATH.
383 string const exe_name = OnlyFilename(exe_path);
384 if (exe_name != exe_path)
387 std::vector<string> const path = getEnvPath("PATH");
388 std::vector<string>::const_iterator it = path.begin();
389 std::vector<string>::const_iterator const end = path.end();
390 for (; it != end; ++it) {
391 if (!os::is_absolute_path(*it))
392 // Someone is playing silly buggers.
395 string const exe_path = AddName(*it, exe_name);
396 if (FileInfo(exe_path, true).isOK())
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%"),
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 FileInfo const file(binary, true);
475 if (!file.isOK() || !file.isLink())
479 if (LyXReadLink(binary, link, true)) {
482 // Unable to resolve the link.
487 // 4. Repeat the exercise on the directory itself.
488 string binary_dir = OnlyPath(abs_binary);
490 // This time test whether the directory is a symbolic link
491 // *before* looking for "chkconfig.ltx".
492 // (We've looked relative to the original already.)
493 FileInfo const file(binary_dir, true);
494 if (!file.isOK() || !file.isLink())
498 if (LyXReadLink(binary_dir, link, true)) {
501 // Unable to resolve the link.
505 // Try and find "chkconfig.ltx".
506 string const lyxdir =
507 NormalizePath(AddPath(binary_dir, relative_lyxdir));
508 searched_dirs.push_back(lyxdir);
510 if (!FileSearch(lyxdir, chkconfig_ltx).empty()) {
511 // Success! "chkconfig.ltx" has been found.
516 // 5. In desparation, try the hard-coded system support dir.
517 path = hardcoded_system_support_dir();
518 if (!FileSearch(path, chkconfig_ltx).empty())
521 // Everything has failed :-(
522 // So inform the user and exit.
523 string searched_dirs_str;
524 typedef std::list<string>::const_iterator iterator;
525 iterator const begin = searched_dirs.begin();
526 iterator const end = searched_dirs.end();
527 for (iterator it = begin; it != end; ++it) {
529 searched_dirs_str += "\n\t";
530 searched_dirs_str += *it;
533 lyxerr << bformat(_("Unable to determine the system directory "
536 "Try the '-sysdir' command line parameter or "
537 "set the environment variable LYX_DIR_13x to "
538 "the LyX system directory containing the file "
544 // Keep the compiler happy.
549 // Returns the absolute path to the user lyxdir, together with a flag
550 // indicating whether this directory was specified explicitly (as -userdir
551 // or through an environment variable) or whether it was deduced.
552 std::pair<string, bool> const
553 get_user_support_dir(string const & default_user_support_dir,
554 string const & command_line_user_support_dir)
556 bool explicit_userdir = true;
558 // 1. Use the -userdir command line parameter.
560 abs_path_from_command_line(command_line_user_support_dir);
562 return std::make_pair(path, explicit_userdir);
564 // 2. Use the LYX_USERDIR_13x environment variable.
565 path = extract_env_var_dir("LYX_USERDIR_13x");
567 return std::make_pair(path, explicit_userdir);
569 // 3. Use the OS-dependent default_user_support_dir
570 explicit_userdir = false;
571 return std::make_pair(default_user_support_dir, explicit_userdir);
575 // $HOME/.lyx on all platforms but Win32 where it will be something like
576 // "C:/Documents and Settings/USERNAME/Application Data/lyx"
577 string const get_default_user_support_dir(string const & home_dir)
579 #if defined (USE_WINDOWS_PACKAGING)
580 (void)home_dir; // Silence warning about unused variable.
582 return AddPath(win32_folder_path(CSIDL_APPDATA), PACKAGE);
584 #elif defined (USE_MACOSX_PACKAGING)
585 (void)home_dir; // Silence warning about unused variable.
588 OSErr const error_code =
589 FSFindFolder(kUserDomain, kApplicationSupportFolderType,
590 kDontCreateFolder, &fsref);
594 char store[PATH_MAX + 1];
595 OSStatus const status_code =
596 FSRefMakePath(&fsref,
597 reinterpret_cast<UInt8*>(store), PATH_MAX);
598 if (status_code != 0)
601 return AddPath(reinterpret_cast<char const *>(store), PACKAGE);
603 #else // USE_POSIX_PACKAGING
604 return AddPath(home_dir, string(".") + PACKAGE);
609 // Check that directory @c dir contains @c file.
610 // Else emit a warning about an invalid @c command_line_switch.
611 bool check_command_line_dir(string const & dir,
613 string const & command_line_switch)
615 string const abs_path = FileSearch(dir, file);
616 if (abs_path.empty()) {
617 lyxerr << bformat(_("Invalid %1% switch.\n"
618 "Directory %2% does not contain %3%."),
619 command_line_switch, dir, file)
623 return !abs_path.empty();
627 // The environment variable @c env_var expands to a (single) file path.
628 string const extract_env_var_dir(string const & env_var)
630 string const dir = os::internal_path(GetEnv(env_var));
631 return dir.empty() ? dir : MakeAbsPath(dir);
635 // Check that directory @c dir contains @c file.
636 // Else emit a warning about an invalid @c env_var.
637 bool check_env_var_dir(string const & dir,
639 string const & env_var)
641 string const abs_path = FileSearch(dir, file);
642 if (abs_path.empty()) {
643 lyxerr << bformat(_("Invalid %1% environment variable.\n"
644 "Directory %2% does not contain %3%."),
649 return !abs_path.empty();
653 // Check that directory @c dir is indeed a directory.
654 // Else emit a warning about an invalid @c env_var.
655 bool check_env_var_dir(string const & dir,
656 string const & env_var)
659 bool const success = (fi.isOK() && fi.isDir());
662 // Put this string on a single line so that the gettext
663 // search mechanism in po/Makefile.in.in will register
664 // package.C.in as a file containing strings that need
667 _("Invalid %1% environment variable.\n%2% is not a directory.");
669 lyxerr << bformat(fmt, env_var, dir)
677 // The locale directory relative to the LyX system directory.
678 string const relative_locale_dir()
684 // The system lyxdir is relative to the directory containing the LyX binary.
685 string const relative_system_support_dir()
689 #if defined (USE_WINDOWS_PACKAGING) || defined (USE_MACOSX_PACKAGING)
690 result = AddPath("../Resources/", PACKAGE);
692 result = AddPath("../share/", PACKAGE);
700 } // namespace support