]> git.lyx.org Git - lyx.git/blob - src/support/os_unix.cpp
Fix import of latex documents with scaled fonts.
[lyx.git] / src / support / os_unix.cpp
1 /**
2  * \file os_unix.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Ruurd A. Reitsma
7  *
8  * Full author contact details are available in file CREDITS.
9  *
10  * Various OS specific functions
11  */
12
13 #include <config.h>
14
15 #include "LyXRC.h"
16
17 #include "support/os.h"
18 #include "support/docstring.h"
19 #include "support/environment.h"
20 #include "support/FileName.h"
21 #include "support/filetools.h"
22 #include "support/lstrings.h"
23 #include "support/lassert.h"
24
25 #include <limits.h>
26 #include <stdlib.h>
27
28 #ifdef __APPLE__
29 #include <CoreServices/CoreServices.h>
30 #endif
31
32 using namespace std;
33
34 namespace lyx {
35 namespace support {
36 namespace os {
37
38 namespace {
39
40 int argc_ = 0;
41 char ** argv_ = 0;
42
43 } // namespace anon
44
45 void init(int argc, char * argv[])
46 {
47         argc_ = argc;
48         argv_ = argv;
49
50         // Set environment's default locale
51         setlocale(LC_ALL, "");
52         setlocale(LC_NUMERIC, "C");
53 }
54
55
56 string utf8_argv(int i)
57 {
58         LASSERT(i < argc_, return "");
59         return to_utf8(from_local8bit(argv_[i]));
60 }
61
62
63 void remove_internal_args(int, int)
64 {}
65
66
67 string current_root()
68 {
69         return "/";
70 }
71
72
73 bool isFilesystemCaseSensitive()
74 {
75 #ifdef __APPLE__
76         return false;
77 #else
78         return true;
79 #endif
80 }
81
82
83 docstring::size_type common_path(docstring const & p1, docstring const & p2)
84 {
85         docstring::size_type i = 0;
86         docstring::size_type const p1_len = p1.length();
87         docstring::size_type const p2_len = p2.length();
88 #ifdef __APPLE__
89         while (i < p1_len && i < p2_len && uppercase(p1[i]) == uppercase(p2[i]))
90                 ++i;
91 #else
92         while (i < p1_len && i < p2_len && p1[i] == p2[i])
93                 ++i;
94 #endif
95         if ((i < p1_len && i < p2_len)
96             || (i < p1_len && p1[i] != '/' && i == p2_len)
97             || (i < p2_len && p2[i] != '/' && i == p1_len))
98         {
99                 if (i)
100                         --i;     // here was the last match
101                 while (i && p1[i] != '/')
102                         --i;
103         }
104         return i;
105 }
106
107
108 bool path_prefix_is(string const & path, string const & pre)
109 {
110 #ifdef __APPLE__
111         return path_prefix_is(const_cast<string &>(path), pre, CASE_UNCHANGED);
112 #else
113         return prefixIs(path, pre);
114 #endif
115 }
116
117
118 bool path_prefix_is(string & path, string const & pre, path_case how)
119 {
120 #ifdef __APPLE__
121         docstring const p1 = from_utf8(path);
122         docstring const p2 = from_utf8(pre);
123         docstring::size_type const p1_len = p1.length();
124         docstring::size_type const p2_len = p2.length();
125         docstring::size_type common_len = common_path(p1, p2);
126
127         if (p2[p2_len - 1] == '/' && p1_len != p2_len)
128                 ++common_len;
129
130         if (common_len != p2_len)
131                 return false;
132
133         if (how == CASE_ADJUSTED && !prefixIs(path, pre)) {
134                 if (p1_len < common_len)
135                         path = to_utf8(p2.substr(0, p1_len));
136                 else
137                         path = to_utf8(p2 + p1.substr(common_len,
138                                                         p1_len - common_len));
139         }
140
141         return true;
142 #else
143         // silence compiler warnings
144         (void)how;
145
146         return prefixIs(path, pre);
147 #endif
148 }
149
150
151 string external_path(string const & p)
152 {
153         return p;
154 }
155
156
157 string internal_path(string const & p)
158 {
159         return p;
160 }
161
162
163 string safe_internal_path(string const & p, file_access)
164 {
165         return p;
166 }
167
168
169 string external_path_list(string const & p)
170 {
171         return p;
172 }
173
174
175 string internal_path_list(string const & p)
176 {
177         return p;
178 }
179
180
181 string latex_path(string const & p)
182 {
183         return p;
184 }
185
186
187 string latex_path_list(string const & p)
188 {
189         return p;
190 }
191
192
193 bool is_valid_strftime(string const & p)
194 {
195         string::size_type pos = p.find_first_of('%');
196         while (pos != string::npos) {
197                 if (pos + 1 == string::npos)
198                         break;
199                 if (!containsOnly(p.substr(pos + 1, 1),
200                         "aAbBcCdDeEFgGhHIjklmMnOpPrRsStTuUVwWxXyYzZ%+"))
201                         return false;
202                 if (pos + 2 == string::npos)
203                       break;
204                 pos = p.find_first_of('%', pos + 2);
205         }
206         return true;
207 }
208
209
210 char const * popen_read_mode()
211 {
212         return "r";
213 }
214
215
216 string const & nulldev()
217 {
218         static string const nulldev_ = "/dev/null";
219         return nulldev_;
220 }
221
222
223 shell_type shell()
224 {
225         return UNIX;
226 }
227
228
229 char path_separator(path_type)
230 {
231         return ':';
232 }
233
234
235 void windows_style_tex_paths(bool)
236 {}
237
238 bool canAutoOpenFile(string const & ext, auto_open_mode const mode)
239 {
240 #ifdef __APPLE__
241 // Reference: http://developer.apple.com/documentation/Carbon/Reference/LaunchServicesReference/
242         CFStringRef cfs_ext = CFStringCreateWithBytes(kCFAllocatorDefault,
243                                         (UInt8 *) ext.c_str(), ext.length(),
244                                         kCFStringEncodingISOLatin1, false);
245         // this is what we would like to do but it seems that the
246         // viewer for PDF is often quicktime...
247         //LSRolesMask role = (mode == VIEW) ? kLSRolesViewer :  kLSRolesEditor;
248         (void)mode;
249         LSRolesMask role = kLSRolesAll;
250         FSRef outAppRef;
251         OSStatus status =
252                 LSGetApplicationForInfo(kLSUnknownType, kLSUnknownCreator,
253                                         cfs_ext, role, &outAppRef, NULL);
254         CFRelease(cfs_ext);
255
256         return status != kLSApplicationNotFoundErr;
257 #else
258         // silence compiler warnings
259         (void)ext;
260         (void)mode;
261
262         // currently, no default viewer is tried for non-windows system
263         // support for KDE/Gnome/Macintosh may be added later
264         return false;
265 #endif
266 }
267
268
269 bool autoOpenFile(string const & filename, auto_open_mode const mode,
270                   string const & path)
271 {
272 #ifdef __APPLE__
273 // Reference: http://developer.apple.com/documentation/Carbon/Reference/LaunchServicesReference/
274         FSRef fileref;
275         OSStatus status =
276                 FSPathMakeRef((UInt8 *) filename.c_str(), &fileref, NULL);
277         if (status != 0)
278                 return false;
279
280         // this is what we would like to do but it seems that the
281         // viewer for PDF is often quicktime...
282         //LSRolesMask role = (mode == VIEW) ? kLSRolesViewer :  kLSRolesEditor;
283         (void)mode;
284         LSRolesMask role = kLSRolesAll;
285         FSRef outAppRef;
286
287         status = LSGetApplicationForItem(&fileref, role, &outAppRef, NULL);
288         if (status == kLSApplicationNotFoundErr)
289                 return false;
290
291         string const texinputs = os::latex_path_list(
292                         replaceCurdirPath(path, lyxrc.texinputs_prefix));
293         string const oldval = getEnv("TEXINPUTS");
294         string const newval = ".:" + texinputs + ":" + oldval;
295         if (!path.empty() && !lyxrc.texinputs_prefix.empty())
296                 setEnv("TEXINPUTS", newval);
297
298         LSLaunchFSRefSpec inLaunchSpec;
299         inLaunchSpec.appRef = &outAppRef;
300         inLaunchSpec.numDocs = 1;
301         inLaunchSpec.itemRefs = &fileref;
302         inLaunchSpec.passThruParams = NULL;
303         inLaunchSpec.launchFlags = kLSLaunchDefaults;
304         inLaunchSpec.asyncRefCon = NULL;
305         status = LSOpenFromRefSpec(&inLaunchSpec, NULL);
306
307         if (!path.empty() && !lyxrc.texinputs_prefix.empty())
308                 setEnv("TEXINPUTS", oldval);
309
310         return status != kLSApplicationNotFoundErr;
311 #else
312         // silence compiler warnings
313         (void)filename;
314         (void)mode;
315         (void)path;
316
317         // currently, no default viewer is tried for non-windows system
318         // support for KDE/Gnome/Macintosh may be added later
319         return false;
320 #endif
321 }
322
323
324 string real_path(string const & path)
325 {
326 #ifdef HAVE_DEF_PATH_MAX
327         char rpath[PATH_MAX + 1];
328         char * result = realpath(path.c_str(), rpath);
329         return FileName::fromFilesystemEncoding(result ? rpath : path).absFileName();
330 #else
331         char * result = realpath(path.c_str(), NULL);
332         string ret = FileName::fromFilesystemEncoding(result ? result : path).absFileName();
333         free(result);
334         return ret;
335 #endif
336 }
337
338 } // namespace os
339 } // namespace support
340 } // namespace lyx