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