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