]> git.lyx.org Git - lyx.git/blob - src/support/os_cygwin.cpp
TR1: check in cmake for GCC version, fallback in checktr1.h for other build systems...
[lyx.git] / src / support / os_cygwin.cpp
1 /**
2  * \file os_cygwin.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  * \author Claus Hentschel
8  * \author Angus Leeming
9  * \author Enrico Forestieri
10  *
11  * Full author contact details are available in file CREDITS.
12  *
13  * Various OS specific functions
14  */
15
16 #include <config.h>
17
18 #include "support/os.h"
19
20 #include "support/FileName.h"
21 #include "support/lassert.h"
22 #include "support/lstrings.h"
23 #include "support/debug.h"
24
25 #include <windows.h>
26 #include <io.h>
27 #include <windef.h>
28 #include <shellapi.h>
29 #include <shlwapi.h>
30 #include <limits.h>
31 #include <stdlib.h>
32
33 #include <cygwin/version.h>
34 #include <sys/cygwin.h>
35
36 #include <ostream>
37
38 using namespace std;
39
40 namespace lyx {
41
42 void lyx_exit(int);
43
44 namespace support {
45 namespace os {
46
47 namespace {
48
49 int argc_ = 0;
50 char ** argv_ = 0;
51
52 bool windows_style_tex_paths_ = false;
53
54 // In both is_posix_path() and is_windows_path() it is assumed that
55 // a valid posix or pseudo-windows path is passed. They simply tell
56 // whether the path looks posix/pseudo-windows or not.
57
58 bool is_posix_path(string const & p)
59 {
60         return  p.empty() ||
61                 (!contains(p, '\\') && (p.length() <= 1 || p[1] != ':'));
62 }
63
64 // This is a test for a win32 style path with forward slashes (pseudo-windows).
65
66 bool is_windows_path(string const & p)
67 {
68         return p.empty() || (!contains(p, '\\') && p[0] != '/');
69 }
70
71
72 // Starting from Cygwin 1.7, new APIs for path conversions were introduced.
73 // The old ones are now deprecated, so avoid them if we detect a modern Cygwin.
74
75 #if CYGWIN_VERSION_DLL_MAJOR >= 1007
76
77 enum PathStyle {
78         posix = CCP_WIN_A_TO_POSIX | CCP_RELATIVE,
79         windows = CCP_POSIX_TO_WIN_A | CCP_RELATIVE
80 };
81
82
83 /// Convert a path to or from posix style.
84 /// \p p is encoded in local 8bit encoding or utf8.
85 /// The result is returned in the same encoding as \p p.
86 string convert_path(string const & p, PathStyle const & target)
87 {
88         if ((target == posix && is_posix_path(p)) ||
89             (target == windows && is_windows_path(p)))
90                 return p;
91
92         char path_buf[PATH_MAX];
93
94         // cygwin_conv_path does not care about the encoding.
95         if (cygwin_conv_path(target, p.c_str(), path_buf, sizeof(path_buf))) {
96                 lyxerr << "LyX: Cannot convert path: " << p << endl;
97                 return subst(p, '\\', '/');
98         }
99         return subst(path_buf, '\\', '/');
100 }
101
102
103 /// Convert a path list to or from posix style.
104 /// \p p is encoded in local 8bit encoding or utf8.
105 /// The result is returned in the same encoding as \p p.
106 string convert_path_list(string const & p, PathStyle const & target)
107 {
108         if (p.empty())
109                 return p;
110
111         char const * const pc = p.c_str();
112         PathStyle const actual = cygwin_posix_path_list_p(pc) ? posix : windows;
113
114         if (target != actual) {
115                 int const size = cygwin_conv_path_list(target, pc, NULL, 0);
116                 char * ptr = new char[size];
117                 if (ptr && cygwin_conv_path_list(target, pc, ptr, size) == 0) {
118                         string const path_list = subst(ptr, '\\', '/');
119                         delete [] ptr;
120                         return path_list;
121                 } else
122                         lyxerr << "LyX: Cannot convert path list: " << p << endl;
123         }
124         return subst(p, '\\', '/');
125 }
126
127 #else
128
129 enum PathStyle {
130         posix,
131         windows
132 };
133
134
135 /// Convert a path to or from posix style.
136 /// \p p is encoded in local 8bit encoding or utf8.
137 /// The result is returned in the same encoding as \p p.
138 string convert_path(string const & p, PathStyle const & target)
139 {
140         char path_buf[PATH_MAX];
141
142         if ((target == posix && is_posix_path(p)) ||
143             (target == windows && is_windows_path(p)))
144                 return p;
145
146         path_buf[0] = '\0';
147
148         // cygwin_conv_to_posix_path and cygwin_conv_to_win32_path do not
149         // care about the encoding.
150         if (target == posix)
151                 cygwin_conv_to_posix_path(p.c_str(), path_buf);
152         else
153                 cygwin_conv_to_win32_path(p.c_str(), path_buf);
154
155         return subst(path_buf[0] ? path_buf : p, '\\', '/');
156 }
157
158
159 /// Convert a path list to or from posix style.
160 /// \p p is encoded in local 8bit encoding or utf8.
161 /// The result is returned in the same encoding as \p p.
162 string convert_path_list(string const & p, PathStyle const & target)
163 {
164         if (p.empty())
165                 return p;
166
167         char const * const pc = p.c_str();
168         PathStyle const actual = cygwin_posix_path_list_p(pc) ? posix : windows;
169
170         if (target != actual) {
171                 int const target_size = (target == posix) ?
172                                 cygwin_win32_to_posix_path_list_buf_size(pc) :
173                                 cygwin_posix_to_win32_path_list_buf_size(pc);
174
175                 char * ptr = new char[target_size];
176
177                 if (ptr) {
178                         // FIXME: See comment in convert_path() above
179                         if (target == posix)
180                                 cygwin_win32_to_posix_path_list(pc, ptr);
181                         else
182                                 cygwin_posix_to_win32_path_list(pc, ptr);
183
184                         string path_list = subst(ptr, '\\', '/');
185                         delete [] ptr;
186                         return path_list;
187                 }
188         }
189
190         return subst(p, '\\', '/');
191 }
192
193 #endif
194
195
196 BOOL terminate_handler(DWORD event)
197 {
198         if (event == CTRL_CLOSE_EVENT
199             || event == CTRL_LOGOFF_EVENT
200             || event == CTRL_SHUTDOWN_EVENT) {
201                 lyx::lyx_exit(1);
202                 return TRUE;
203         }
204         return FALSE;
205 }
206
207 } // namespace anon
208
209 void init(int argc, char * argv[])
210 {
211         argc_ = argc;
212         argv_ = argv;
213
214         // Make sure that the TEMP variable is set
215         // and sync the Windows environment.
216         setenv("TEMP", "/tmp", false);
217         cygwin_internal(CW_SYNC_WINENV);
218
219         // Catch shutdown events.
220         SetConsoleCtrlHandler((PHANDLER_ROUTINE)terminate_handler, TRUE);
221 }
222
223
224 string utf8_argv(int i)
225 {
226         LASSERT(i < argc_, /**/);
227         return to_utf8(from_local8bit(argv_[i]));
228 }
229
230
231 void remove_internal_args(int, int)
232 {}
233
234
235 string current_root()
236 {
237         return string("/");
238 }
239
240
241 bool isFilesystemCaseSensitive()
242 {
243         return false;
244 }
245
246
247 docstring::size_type common_path(docstring const & p1, docstring const & p2)
248 {
249         docstring::size_type i = 0;
250         docstring::size_type const p1_len = p1.length();
251         docstring::size_type const p2_len = p2.length();
252         while (i < p1_len && i < p2_len && uppercase(p1[i]) == uppercase(p2[i]))
253                 ++i;
254         if ((i < p1_len && i < p2_len)
255             || (i < p1_len && p1[i] != '/' && i == p2_len)
256             || (i < p2_len && p2[i] != '/' && i == p1_len))
257         {
258                 if (i)
259                         --i;     // here was the last match
260                 while (i && p1[i] != '/')
261                         --i;
262         }
263         return i;
264 }
265
266
267 bool path_prefix_is(string const & path, string const & pre)
268 {
269         return path_prefix_is(const_cast<string &>(path), pre, CASE_UNCHANGED);
270 }
271
272
273 bool path_prefix_is(string & path, string const & pre, path_case how)
274 {
275         docstring const p1 = from_utf8(path);
276         docstring const p2 = from_utf8(pre);
277         docstring::size_type const p1_len = p1.length();
278         docstring::size_type const p2_len = p2.length();
279         docstring::size_type common_len = common_path(p1, p2);
280
281         if (p2[p2_len - 1] == '/' && p1_len != p2_len)
282                 ++common_len;
283
284         if (common_len != p2_len)
285                 return false;
286
287         if (how == CASE_ADJUSTED && !prefixIs(path, pre)) {
288                 if (p1_len < common_len)
289                         path = to_utf8(p2.substr(0, p1_len));
290                 else
291                         path = to_utf8(p2 + p1.substr(common_len,
292                                                         p1_len - common_len));
293         }
294
295         return true;
296 }
297
298
299 string external_path(string const & p)
300 {
301         return convert_path(p, PathStyle(posix));
302 }
303
304
305 string internal_path(string const & p)
306 {
307         return convert_path(p, PathStyle(posix));
308 }
309
310
311 string safe_internal_path(string const & p, file_access)
312 {
313         return convert_path(p, PathStyle(posix));
314 }
315
316
317 string external_path_list(string const & p)
318 {
319         return convert_path_list(p, PathStyle(posix));
320 }
321
322
323 string internal_path_list(string const & p)
324 {
325         return convert_path_list(p, PathStyle(posix));
326 }
327
328
329 string latex_path(string const & p)
330 {
331         // We may need a posix style path or a windows style path (depending
332         // on windows_style_tex_paths_), but we use always forward slashes,
333         // since it gets written into a .tex file.
334
335         if (windows_style_tex_paths_ && FileName::isAbsolute(p)) {
336                 string dos_path = convert_path(p, PathStyle(windows));
337                 LYXERR(Debug::LATEX, "<Path correction for LaTeX> ["
338                         << p << "]->>[" << dos_path << ']');
339                 return dos_path;
340         }
341
342         return convert_path(p, PathStyle(posix));
343 }
344
345
346 bool is_valid_strftime(string const & p)
347 {
348         string::size_type pos = p.find_first_of('%');
349         while (pos != string::npos) {
350                 if (pos + 1 == string::npos)
351                         break;
352                 if (!containsOnly(p.substr(pos + 1, 1),
353                         "aAbBcCdDeEFgGhHIjklmMnOpPrRsStTuUVwWxXyYzZ%+"))
354                         return false;
355                 if (pos + 2 == string::npos)
356                       break;
357                 pos = p.find_first_of('%', pos + 2);
358         }
359         return true;
360 }
361
362
363 // returns a string suitable to be passed to popen when
364 // reading a pipe
365 char const * popen_read_mode()
366 {
367         return "r";
368 }
369
370
371 string const & nulldev()
372 {
373         static string const nulldev_ = "/dev/null";
374         return nulldev_;
375 }
376
377
378 bool is_terminal(io_channel channel)
379 {
380         return isatty(channel);
381 }
382
383
384 shell_type shell()
385 {
386         return UNIX;
387 }
388
389
390 char path_separator()
391 {
392         return ':';
393 }
394
395
396 void windows_style_tex_paths(bool use_windows_paths)
397 {
398         windows_style_tex_paths_ = use_windows_paths;
399 }
400
401
402 bool canAutoOpenFile(string const & ext, auto_open_mode const mode)
403 {
404         if (ext.empty())
405                 return false;
406
407         string const full_ext = "." + ext;
408
409         DWORD bufSize = MAX_PATH + 100;
410         TCHAR buf[MAX_PATH + 100];
411         // reference: http://msdn.microsoft.com/en-us/library/bb773471.aspx
412         char const * action = (mode == VIEW) ? "open" : "edit";
413         return S_OK == AssocQueryString(ASSOCF_INIT_IGNOREUNKNOWN,
414                 ASSOCSTR_EXECUTABLE, full_ext.c_str(), action, buf, &bufSize);
415 }
416
417
418 bool autoOpenFile(string const & filename, auto_open_mode const mode)
419 {
420         // reference: http://msdn.microsoft.com/en-us/library/bb762153.aspx
421         string const win_path = to_local8bit(from_utf8(convert_path(filename, PathStyle(windows))));
422         char const * action = (mode == VIEW) ? "open" : "edit";
423         return reinterpret_cast<int>(ShellExecute(NULL, action,
424                 win_path.c_str(), NULL, NULL, 1)) > 32;
425 }
426
427
428 string real_path(string const & path)
429 {
430         char rpath[PATH_MAX + 1];
431         char * result = realpath(path.c_str(), rpath);
432         return FileName::fromFilesystemEncoding(result ? rpath : path).absFileName();
433 }
434
435 } // namespace os
436 } // namespace support
437 } // namespace lyx