]> git.lyx.org Git - lyx.git/blob - src/support/path_defines.C.in
More 'standard conformant blurb' nonsense.
[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
27 using std::endl;
28
29
30 namespace {
31
32 // Store for the path to the lyx support files we're actually going to use.
33 string system_lyxdir_;
34
35 // Store for the path to the autogenerated files when running in place
36 string build_lyxdir_;
37
38 // Store for the path to the user-level support files.
39 string user_lyxdir_;
40
41
42 /* The absolute path to the system-level lyx support files.
43  * (Make-time value.)
44  */
45 string const & lyx_dir()
46 {
47         static string const ld = "%LYX_DIR%";
48         return ld;
49 }
50
51
52 /* The absolute path to the top of the lyx build tree.
53  * (Make-time value.)
54  */
55 string const & lyx_top_srcdir()
56 {
57         static string const lts = "%TOP_SRCDIR%";
58         return lts;
59 }
60
61 } // namespace anon
62
63
64 namespace lyx {
65 namespace support {
66
67
68 /* The absolute path to the system-level lyx locale directory.
69  * (Make-time value.)
70  */
71 string const & lyx_localedir()
72 {
73         static string const ll = "%LOCALEDIR%";
74         return ll;
75 }
76
77
78 string const & build_dir()
79 {
80         static string const bl = "%BUILDDIR%";
81         return bl;
82 }
83
84
85 string const & build_lyxdir()
86 {
87         return build_lyxdir_;
88 }
89
90
91 void build_lyxdir(string const & sld)
92 {
93         build_lyxdir_ = sld;
94 }
95
96
97 string const & system_lyxdir()
98 {
99         return system_lyxdir_;
100 }
101
102
103 void system_lyxdir(string const & sld)
104 {
105         system_lyxdir_ = sld;
106 }
107
108
109 string const & user_lyxdir()
110 {
111         return user_lyxdir_;
112 }
113
114
115 void user_lyxdir(string const & uld)
116 {
117         user_lyxdir_ = uld;
118 }
119
120
121 bool setLyxPaths()
122 {
123         //
124         // Determine path of binary
125         //
126
127         string binpath = os::binpath();
128         string binname = os::binname();
129         string fullbinname = MakeAbsPath(binname, binpath);
130
131         if (binpath.empty()) {
132                 lyxerr << _("Warning: could not determine path of binary.")
133                        << "\n"
134                        << _("If you have problems, try starting LyX with an absolute path.")
135                        << endl;
136         }
137         lyxerr[Debug::INIT] << "Name of binary: " << binname << endl;
138         lyxerr[Debug::INIT] << "Path of binary: " << binpath << endl;
139
140
141         string searchpath;
142
143         //
144         // Determine whether we are running in place and set
145         // build_lyxdir accordingly
146         //
147
148         string const buildlyxdir = MakeAbsPath("../lib", binpath);
149         if (!FileSearch(buildlyxdir, "lyxrc.defaults").empty()) {
150                 searchpath += AddPath(lyx_top_srcdir(), "lib") + ';';
151                 build_lyxdir(buildlyxdir);
152                 lyxerr[Debug::INIT] << "Checking whether LyX is run in "
153                         "place... yes" << endl;
154         } else {
155                 lyxerr[Debug::INIT]
156                         << "Checking whether LyX is run in place... no"
157                         << endl;
158         }
159
160         //
161         // Determine system directory.
162         //
163
164         // Directories are searched in this order:
165         // 1) -sysdir command line parameter
166         // 2) LYX_DIR_14x environment variable
167         // 3) Maybe <path of binary>/TOP_SRCDIR/lib
168         // 4) <path of binary>/../share/<name of binary>/
169         // 4a) repeat 4 after following the Symlink if <path of
170         //     binary> is a symbolic link.
171         // 5) hardcoded lyx_dir
172         // The directory is checked for the presence of the file
173         // "chkconfig.ltx", and if that is present, the directory
174         // is accepted as the system directory.
175         // If no chkconfig.ltx file is found, a warning is given,
176         // and the hardcoded lyx_dir is used.
177
178         // If we had a command line switch, system_lyxdir_ is already set
179         if (!system_lyxdir_.empty())
180                 searchpath = MakeAbsPath(system_lyxdir_) + ';';
181
182         string const lyxdir = GetEnvPath("LYX_DIR_14x");
183
184         if (!lyxdir.empty()) {
185                 lyxerr[Debug::INIT] << "LYX_DIR_14x: " << lyxdir << endl;
186                 searchpath += lyxdir + ';';
187         }
188
189         string fullbinpath = binpath;
190         FileInfo file(fullbinname, true);
191         if (file.isLink()) {
192                 lyxerr[Debug::INIT] << "binary is a link" << endl;
193                 string link;
194                 if (LyXReadLink(fullbinname, link, true)) {
195                         // Path of binary/../share/name of binary/
196                         searchpath += NormalizePath(AddPath(binpath,
197                                                             "../share/")
198                                                     + OnlyFilename(binname));
199                         searchpath += ';';
200                         fullbinpath = link;
201                         binpath = MakeAbsPath(OnlyPath(fullbinpath));
202                 }
203         }
204
205         bool followlink;
206         do {
207                 // Path of binary/../share/name of binary/
208                 searchpath += NormalizePath(AddPath(binpath, "../share/") +
209                       OnlyFilename(binname)) + ';';
210
211                 // Follow Symlinks
212                 FileInfo file(fullbinpath, true);
213                 followlink = file.isLink();
214                 if (followlink) {
215                         lyxerr[Debug::INIT] << " directory " << fullbinpath
216                                             << " is a link" << endl;
217                         string link;
218                         if (LyXReadLink(fullbinpath, link, true)) {
219                                 fullbinpath = link;
220                                 binpath = MakeAbsPath(OnlyPath(fullbinpath));
221                         }
222                         else {
223                                 followlink = false;
224                         }
225                 }
226         } while (followlink);
227
228         // <absolute top srcdir>/lib
229         searchpath += AddPath(lyx_top_srcdir(), "lib") + ';';
230         // Hardcoded dir
231         searchpath += lyx_dir();
232
233         lyxerr[Debug::INIT] << "System directory search path: "
234                             << searchpath << endl;
235
236         string const filename = "chkconfig.ltx";
237         string const temp = FileOpenSearch(searchpath, filename, string());
238         system_lyxdir_ = OnlyPath(temp);
239
240         // Reduce "path/../path" stuff out of system directory
241         system_lyxdir_ = NormalizePath(system_lyxdir_);
242
243         bool path_shown = false;
244
245         // Warn if environment variable is set, but unusable
246         if (!lyxdir.empty()) {
247                 if (system_lyxdir_ != NormalizePath(lyxdir)) {
248                         lyxerr <<_("LYX_DIR_14x environment variable no good.")
249                                << '\n'
250                                << _("System directory set to: ")
251                                << system_lyxdir_ << endl;
252                         path_shown = true;
253                 }
254         }
255
256         // Warn the user if we couldn't find "chkconfig.ltx"
257         if (system_lyxdir_ == "./") {
258                 lyxerr <<_("LyX Warning! Couldn't determine system directory. ")
259                        <<_("Try the '-sysdir' command line parameter or ")
260                        <<_("set the environment variable LYX_DIR_14x to the "
261                            "LyX system directory ")
262                        << _("containing the file `chkconfig.ltx'.") << endl;
263                 if (!path_shown) {
264                         FileInfo fi(lyx_dir());
265                         if (!fi.exist()) {
266                                 lyxerr << "Couldn't even find the default LYX_DIR." << endl
267                                         << "Giving up." << endl;
268                                 exit(1);
269                         }
270                         lyxerr << bformat(_("Using built-in default %1$s but expect problems."),
271                                 lyx_dir()) << endl;
272                 } else {
273                         lyxerr << _("Expect problems.") << endl;
274                 }
275                 system_lyxdir_ = lyx_dir();
276                 path_shown = true;
277         }
278
279         if (!path_shown)
280                 lyxerr[Debug::INIT] << "System directory: '"
281                                     << system_lyxdir_ << '\'' << endl;
282
283         //
284         // Determine user lyx-dir
285         //
286
287         // Directories are searched in this order:
288         // 1) -userdir command line parameter
289         // 2) LYX_USERDIR_14x environment variable
290         // 3) $HOME/.<name of binary>
291
292         // If we had a command line switch, user_lyxdir_ is already set
293         bool explicit_userdir = true;
294         if (user_lyxdir_.empty()) {
295
296                 // LYX_USERDIR_14x environment variable
297                 user_lyxdir_ = GetEnvPath("LYX_USERDIR_14x");
298
299                 // default behaviour
300                 if (user_lyxdir_.empty())
301                         user_lyxdir_ = AddPath(GetEnvPath("HOME"),
302                                                string(".") + PACKAGE);
303                         explicit_userdir = false;
304         }
305
306         lyxerr[Debug::INIT] << "User LyX directory: '"
307                             <<  user_lyxdir_ << '\'' << endl;
308         return explicit_userdir;
309 }
310
311 } // namespace support
312 } // namespace lyx