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