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