]> git.lyx.org Git - lyx.git/blob - src/support/path_defines.C.in
tostr -> convert and some bformat work
[lyx.git] / src / support / path_defines.C.in
1 // -*- C++ -*-
2 /**
3  * \file path_defines.C
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author Angus Leeming
8  *
9  * Full author contact details are available in file CREDITS.
10  *
11  * Warning! This file is autogenerated from path_defines.C.in.
12  * All changes to this file will be lost.
13  */
14
15 #include <config.h>
16
17 #include "path_defines.h"
18
19 #include "debug.h"
20 #include "gettext.h"
21
22 #include "FileInfo.h"
23 #include "filetools.h"
24 #include "lstrings.h"
25 #include "os.h"
26 #include "lyxlib.h"
27
28 using std::string;
29 using std::endl;
30
31
32 namespace {
33
34 // Store for the path to the lyx support files we're actually going to use.
35 string system_lyxdir_;
36
37 // Store for the path to the autogenerated files when running in place
38 string build_lyxdir_;
39
40 // Store for the path to the user-level support files.
41 string user_lyxdir_;
42
43 // Store for the path to the locale directory.
44 string localedir_;
45
46 /* The absolute path to the system-level lyx support files.
47  * (Make-time value.)
48  */
49 string const & lyx_dir()
50 {
51         static string const ld = "%LYX_DIR%";
52         return ld;
53 }
54
55 } // namespace anon
56
57
58 namespace lyx {
59 namespace support {
60
61
62 /* The absolute path to the top of the lyx build tree.
63  * (Make-time value.)
64  */
65 string const & top_srcdir()
66 {
67         static string const lts = "%TOP_SRCDIR%";
68         return lts;
69 }
70
71
72 /* The absolute path to the system-level lyx locale directory.
73  * (Make-time value.)
74  */
75 string const & lyx_localedir()
76 {
77         static string const ll = "%LOCALEDIR%";
78         if (localedir_.empty())
79                 return ll;
80         else
81                 return localedir_;
82 }
83
84
85 string const & build_dir()
86 {
87         static string const bl = "%BUILDDIR%";
88         return bl;
89 }
90
91
92 string const & build_lyxdir()
93 {
94         return build_lyxdir_;
95 }
96
97
98 void build_lyxdir(string const & sld)
99 {
100         build_lyxdir_ = sld;
101 }
102
103
104 string const & system_lyxdir()
105 {
106         return system_lyxdir_;
107 }
108
109
110 void system_lyxdir(string const & sld)
111 {
112         system_lyxdir_ = sld;
113 }
114
115
116 string const & user_lyxdir()
117 {
118         return user_lyxdir_;
119 }
120
121
122 void user_lyxdir(string const & uld)
123 {
124         user_lyxdir_ = uld;
125 }
126
127
128 bool setLyxPaths()
129 {
130         //
131         // Determine path of binary
132         //
133
134         string binpath = os::binpath();
135         string binname = os::binname();
136         string fullbinname = MakeAbsPath(binname, binpath);
137
138         if (binpath.empty()) {
139                 lyxerr << _("Warning: could not determine path of binary.")
140                        << "\n"
141                        << _("If you have problems, try starting LyX with an absolute path.")
142                        << endl;
143         }
144         lyxerr[Debug::INIT] << "Name of binary: " << binname << endl;
145         lyxerr[Debug::INIT] << "Path of binary: " << binpath << endl;
146
147
148         string searchpath;
149
150         //
151         // Determine whether we are running in place and set
152         // build_lyxdir accordingly
153         //
154
155         string const buildlyxdir = MakeAbsPath("../lib", binpath);
156         if (!FileSearch(buildlyxdir, "lyxrc.defaults").empty()) {
157                 searchpath += AddPath(top_srcdir(), "lib") + ';';
158                 build_lyxdir(buildlyxdir);
159                 lyxerr[Debug::INIT] << "Checking whether LyX is run in "
160                         "place... yes" << endl;
161         } else {
162                 lyxerr[Debug::INIT]
163                         << "Checking whether LyX is run in place... no"
164                         << endl;
165         }
166
167         //
168         // Determine system directory.
169         //
170
171         // Directories are searched in this order:
172         // 1) -sysdir command line parameter
173         // 2) LYX_DIR_14x environment variable
174         // 3) Maybe <path of binary>/TOP_SRCDIR/lib
175         // 4) <path of binary>/../Resources/<name of binary>/  [for LyX/Mac]
176         // 5) <path of binary>/../share/<name of binary>/
177         // 5a) repeat 4 after following the Symlink if <path of
178         //     binary> is a symbolic link.
179         // 6) hardcoded lyx_dir
180         // The directory is checked for the presence of the file
181         // "chkconfig.ltx", and if that is present, the directory
182         // is accepted as the system directory.
183         // If no chkconfig.ltx file is found, a warning is given,
184         // and the hardcoded lyx_dir is used.
185
186         // If we had a command line switch, system_lyxdir_ is already set
187         if (!system_lyxdir_.empty())
188                 searchpath = MakeAbsPath(system_lyxdir_) + ';';
189
190         string const lyxdir = GetEnvPath("LYX_DIR_14x");
191
192         if (!lyxdir.empty()) {
193                 lyxerr[Debug::INIT] << "LYX_DIR_14x: " << lyxdir << endl;
194                 searchpath += lyxdir + ';';
195         }
196
197         // Path of binary/../Resources/
198         searchpath += NormalizePath(AddPath(binpath, "../Resources/") +
199                                     OnlyFilename(binname)) + ';';
200
201         string fullbinpath = binpath;
202         FileInfo file(fullbinname, true);
203         if (file.isLink()) {
204                 lyxerr[Debug::INIT] << "binary is a link" << endl;
205                 string link;
206                 if (LyXReadLink(fullbinname, link, true)) {
207                         // Path of binary/../share/name of binary/
208                         searchpath += NormalizePath(AddPath(binpath,
209                                                             "../share/")
210                                                     + OnlyFilename(binname));
211                         searchpath += ';';
212                         fullbinpath = link;
213                         binpath = MakeAbsPath(OnlyPath(fullbinpath));
214                 }
215         }
216
217         bool followlink;
218         do {
219                 // Path of binary/../share/name of binary/
220                 string const exe_name = OnlyFilename(binname);
221 #ifdef _WIN32
222                 string const lyx_system_dir_name =
223                         suffixIs(exe_name, ".exe") ?
224                                 ChangeExtension(exe_name, "") :
225                                 exe_name;
226 #else
227                 string const lyx_system_dir_name = exe_name;
228 #endif
229                 string const shared_dir_name =
230                         NormalizePath(AddPath(binpath, "../share/"));
231                 searchpath += shared_dir_name + lyx_system_dir_name + ';';
232
233                 // Follow Symlinks
234                 FileInfo file(fullbinpath, true);
235                 followlink = file.isLink();
236                 if (followlink) {
237                         lyxerr[Debug::INIT] << " directory " << fullbinpath
238                                             << " is a link" << endl;
239                         string link;
240                         if (LyXReadLink(fullbinpath, link, true)) {
241                                 fullbinpath = link;
242                                 binpath = MakeAbsPath(OnlyPath(fullbinpath));
243                         }
244                         else {
245                                 followlink = false;
246                         }
247                 }
248         } while (followlink);
249
250         // <absolute top srcdir>/lib
251         searchpath += AddPath(top_srcdir(), "lib") + ';';
252         // Hardcoded dir
253         searchpath += lyx_dir();
254
255         lyxerr[Debug::INIT] << "System directory search path: "
256                             << searchpath << endl;
257
258         string const filename = "chkconfig.ltx";
259         string const temp = FileOpenSearch(searchpath, filename, string());
260         system_lyxdir_ = OnlyPath(temp);
261
262         // Reduce "path/../path" stuff out of system directory
263         system_lyxdir_ = NormalizePath(system_lyxdir_);
264
265         bool path_shown = false;
266
267         // Warn if environment variable is set, but unusable
268         if (!lyxdir.empty()) {
269                 if (system_lyxdir_ != NormalizePath(lyxdir)) {
270                         lyxerr <<_("LYX_DIR_14x environment variable no good.")
271                                << '\n'
272                                << _("System directory set to: ")
273                                << system_lyxdir_ << endl;
274                         path_shown = true;
275                 }
276         }
277
278         // Warn the user if we couldn't find "chkconfig.ltx"
279         if (system_lyxdir_ == "./") {
280                 lyxerr <<_("LyX Warning! Couldn't determine system directory. ")
281                        <<_("Try the '-sysdir' command line parameter or ")
282                        <<_("set the environment variable LYX_DIR_14x to the "
283                            "LyX system directory ")
284                        << _("containing the file `chkconfig.ltx'.") << endl;
285                 if (!path_shown) {
286                         FileInfo fi(lyx_dir());
287                         if (!fi.exist()) {
288                                 lyxerr << "Couldn't even find the default LYX_DIR." << endl
289                                         << "Giving up." << endl;
290                                 exit(1);
291                         }
292                         lyxerr << bformat(_("Using built-in default %1$s but expect problems."),
293                                 lyx_dir()) << endl;
294                 } else {
295                         lyxerr << _("Expect problems.") << endl;
296                 }
297                 system_lyxdir_ = lyx_dir();
298                 path_shown = true;
299         }
300
301         if (!path_shown)
302                 lyxerr[Debug::INIT] << "System directory: '"
303                                     << system_lyxdir_ << '\'' << endl;
304
305         // This is true when we are running from a Mac OS X bundle
306         // (for LyX/Mac)
307         bool const inOSXBundle =
308                 system_lyxdir_ == NormalizePath(AddPath(binpath, "../Resources/")
309                                                 + OnlyFilename(binname));
310         if (inOSXBundle)
311                 lyxerr[Debug::INIT] << "Running from LyX/Mac bundle."
312                                     << endl;
313
314         //
315         // Set PATH for LyX/Mac
316         // LyX/Mac is a relocatable application bundle; here we add to
317         // the PATH so it can find binaries like reLyX inside its own
318         // application bundle, and also append PATH elements that it
319         // needs to run latex, previewers, etc.
320         //
321
322         if (inOSXBundle) {
323                 const string newpath = GetEnv("PATH") + ":" + binpath
324                         + ":/sw/bin:/usr/local/bin"
325                         + ":/usr/local/teTeX/bin/powerpc-apple-darwin-current";
326                 lyx::support::putenv("PATH", newpath);
327                 lyxerr[Debug::INIT] << "LyX/Mac: setting PATH to: "
328                                     << GetEnv("PATH") << endl;
329         }
330
331
332         //
333         // Determine locale directory
334         //
335         if (!GetEnv("LYX_LOCALEDIR").empty()) {
336                 localedir_ = GetEnv("LYX_LOCALEDIR");
337                 lyxerr[Debug::INIT] << "LYX_LOCALEDIR=" << localedir_ << endl;
338         } else if (inOSXBundle) {
339                 string const maybe_localedir =
340                         NormalizePath(AddPath(system_lyxdir_, "../locale"));
341                 FileInfo fi(maybe_localedir);
342                 if (fi.isOK() && fi.isDir()) {
343                         lyxerr[Debug::INIT]
344                                 << "LyX/Mac: setting locale directory to "
345                                 << maybe_localedir << endl;
346                         localedir_ = maybe_localedir;
347                 }
348         }
349
350         //
351         // Determine user lyx-dir
352         //
353
354         // Directories are searched in this order:
355         // 1) -userdir command line parameter
356         // 2) LYX_USERDIR_14x environment variable
357         // 3) $HOME/.<name of binary>
358
359         // If we had a command line switch, user_lyxdir_ is already set
360         bool explicit_userdir = true;
361         if (user_lyxdir_.empty()) {
362
363                 // LYX_USERDIR_14x environment variable
364                 user_lyxdir_ = GetEnvPath("LYX_USERDIR_14x");
365
366                 // default behaviour
367                 if (user_lyxdir_.empty())
368                         if (inOSXBundle)
369                                 user_lyxdir_ = AddPath(os::homepath(),
370                                                        "Library/Preferences/LyX");
371                         else
372                                 user_lyxdir_ = AddPath(os::homepath(),
373                                                        string(".") + PACKAGE);
374                 explicit_userdir = false;
375         }
376
377         lyxerr[Debug::INIT] << "User LyX directory: '"
378                             <<  user_lyxdir_ << '\'' << endl;
379         return explicit_userdir;
380 }
381
382 } // namespace support
383 } // namespace lyx