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