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