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