]> git.lyx.org Git - lyx.git/blob - src/support/os_cygwin.cpp
Micro-optimization.
[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 string current_root()
232 {
233         return string("/");
234 }
235
236
237 bool isFilesystemCaseSensitive()
238 {
239         return false;
240 }
241
242
243 docstring::size_type common_path(docstring const & p1, docstring const & p2)
244 {
245         docstring::size_type i = 0;
246         docstring::size_type const p1_len = p1.length();
247         docstring::size_type const p2_len = p2.length();
248         while (i < p1_len && i < p2_len && uppercase(p1[i]) == uppercase(p2[i]))
249                 ++i;
250         if ((i < p1_len && i < p2_len)
251             || (i < p1_len && p1[i] != '/' && i == p2_len)
252             || (i < p2_len && p2[i] != '/' && i == p1_len))
253         {
254                 if (i)
255                         --i;     // here was the last match
256                 while (i && p1[i] != '/')
257                         --i;
258         }
259         return i;
260 }
261
262
263 bool path_prefix_is(string const & path, string const & pre)
264 {
265         return path_prefix_is(const_cast<string &>(path), pre, CASE_UNCHANGED);
266 }
267
268
269 bool path_prefix_is(string & path, string const & pre, path_case how)
270 {
271         docstring const p1 = from_utf8(path);
272         docstring const p2 = from_utf8(pre);
273         docstring::size_type const p1_len = p1.length();
274         docstring::size_type const p2_len = p2.length();
275         docstring::size_type common_len = common_path(p1, p2);
276
277         if (p2[p2_len - 1] == '/' && p1_len != p2_len)
278                 ++common_len;
279
280         if (common_len != p2_len)
281                 return false;
282
283         if (how == CASE_ADJUSTED && !prefixIs(path, pre)) {
284                 if (p1_len < common_len)
285                         path = to_utf8(p2.substr(0, p1_len));
286                 else
287                         path = to_utf8(p2 + p1.substr(common_len,
288                                                         p1_len - common_len));
289         }
290
291         return true;
292 }
293
294
295 string external_path(string const & p)
296 {
297         return convert_path(p, PathStyle(posix));
298 }
299
300
301 string internal_path(string const & p)
302 {
303         return convert_path(p, PathStyle(posix));
304 }
305
306
307 string safe_internal_path(string const & p, file_access)
308 {
309         return convert_path(p, PathStyle(posix));
310 }
311
312
313 string external_path_list(string const & p)
314 {
315         return convert_path_list(p, PathStyle(posix));
316 }
317
318
319 string internal_path_list(string const & p)
320 {
321         return convert_path_list(p, PathStyle(posix));
322 }
323
324
325 string latex_path(string const & p)
326 {
327         // We may need a posix style path or a windows style path (depending
328         // on windows_style_tex_paths_), but we use always forward slashes,
329         // since it gets written into a .tex file.
330
331         if (windows_style_tex_paths_ && FileName::isAbsolute(p)) {
332                 string dos_path = convert_path(p, PathStyle(windows));
333                 LYXERR(Debug::LATEX, "<Path correction for LaTeX> ["
334                         << p << "]->>[" << dos_path << ']');
335                 return dos_path;
336         }
337
338         return convert_path(p, PathStyle(posix));
339 }
340
341
342 bool is_valid_strftime(string const & p)
343 {
344         string::size_type pos = p.find_first_of('%');
345         while (pos != string::npos) {
346                 if (pos + 1 == string::npos)
347                         break;
348                 if (!containsOnly(p.substr(pos + 1, 1),
349                         "aAbBcCdDeEFgGhHIjklmMnOpPrRsStTuUVwWxXyYzZ%+"))
350                         return false;
351                 if (pos + 2 == string::npos)
352                       break;
353                 pos = p.find_first_of('%', pos + 2);
354         }
355         return true;
356 }
357
358
359 // returns a string suitable to be passed to popen when
360 // reading a pipe
361 char const * popen_read_mode()
362 {
363         return "r";
364 }
365
366
367 string const & nulldev()
368 {
369         static string const nulldev_ = "/dev/null";
370         return nulldev_;
371 }
372
373
374 bool is_terminal(io_channel channel)
375 {
376         return isatty(channel);
377 }
378
379
380 shell_type shell()
381 {
382         return UNIX;
383 }
384
385
386 char path_separator()
387 {
388         return ':';
389 }
390
391
392 void windows_style_tex_paths(bool use_windows_paths)
393 {
394         windows_style_tex_paths_ = use_windows_paths;
395 }
396
397
398 bool canAutoOpenFile(string const & ext, auto_open_mode const mode)
399 {
400         if (ext.empty())
401                 return false;
402
403         string const full_ext = "." + ext;
404
405         DWORD bufSize = MAX_PATH + 100;
406         TCHAR buf[MAX_PATH + 100];
407         // reference: http://msdn.microsoft.com/en-us/library/bb773471.aspx
408         char const * action = (mode == VIEW) ? "open" : "edit";
409         return S_OK == AssocQueryString(ASSOCF_INIT_IGNOREUNKNOWN,
410                 ASSOCSTR_EXECUTABLE, full_ext.c_str(), action, buf, &bufSize);
411 }
412
413
414 bool autoOpenFile(string const & filename, auto_open_mode const mode)
415 {
416         // reference: http://msdn.microsoft.com/en-us/library/bb762153.aspx
417         string const win_path = to_local8bit(from_utf8(convert_path(filename, PathStyle(windows))));
418         char const * action = (mode == VIEW) ? "open" : "edit";
419         return reinterpret_cast<int>(ShellExecute(NULL, action,
420                 win_path.c_str(), NULL, NULL, 1)) > 32;
421 }
422
423
424 string real_path(string const & path)
425 {
426         char rpath[PATH_MAX + 1];
427         char * result = realpath(path.c_str(), rpath);
428         return FileName::fromFilesystemEncoding(result ? rpath : path).absFilename();
429 }
430
431 } // namespace os
432 } // namespace support
433 } // namespace lyx