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