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