]> git.lyx.org Git - lyx.git/blob - src/support/os_cygwin.cpp
1de0da6ef195c50c58d6b2a7964c09ce9867b832
[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         // Set environment's default locale
219         setlocale(LC_ALL, "");
220         setlocale(LC_NUMERIC, "C");
221
222         // Make sure that the TEMP variable is set
223         // and sync the Windows environment.
224         setenv("TEMP", "/tmp", false);
225         cygwin_internal(CW_SYNC_WINENV);
226
227         // Catch shutdown events.
228         SetConsoleCtrlHandler((PHANDLER_ROUTINE)terminate_handler, TRUE);
229 }
230
231
232 string utf8_argv(int i)
233 {
234         LASSERT(i < argc_, return "");
235         return to_utf8(from_local8bit(argv_[i]));
236 }
237
238
239 void remove_internal_args(int, int)
240 {}
241
242
243 string current_root()
244 {
245         return string("/");
246 }
247
248
249 bool isFilesystemCaseSensitive()
250 {
251         return false;
252 }
253
254
255 docstring::size_type common_path(docstring const & p1, docstring const & p2)
256 {
257         docstring::size_type i = 0;
258         docstring::size_type const p1_len = p1.length();
259         docstring::size_type const p2_len = p2.length();
260         while (i < p1_len && i < p2_len && uppercase(p1[i]) == uppercase(p2[i]))
261                 ++i;
262         if ((i < p1_len && i < p2_len)
263             || (i < p1_len && p1[i] != '/' && i == p2_len)
264             || (i < p2_len && p2[i] != '/' && i == p1_len))
265         {
266                 if (i)
267                         --i;     // here was the last match
268                 while (i && p1[i] != '/')
269                         --i;
270         }
271         return i;
272 }
273
274
275 bool path_prefix_is(string const & path, string const & pre)
276 {
277         return path_prefix_is(const_cast<string &>(path), pre, CASE_UNCHANGED);
278 }
279
280
281 bool path_prefix_is(string & path, string const & pre, path_case how)
282 {
283         docstring const p1 = from_utf8(path);
284         docstring const p2 = from_utf8(pre);
285         docstring::size_type const p1_len = p1.length();
286         docstring::size_type const p2_len = p2.length();
287         docstring::size_type common_len = common_path(p1, p2);
288
289         if (p2[p2_len - 1] == '/' && p1_len != p2_len)
290                 ++common_len;
291
292         if (common_len != p2_len)
293                 return false;
294
295         if (how == CASE_ADJUSTED && !prefixIs(path, pre)) {
296                 if (p1_len < common_len)
297                         path = to_utf8(p2.substr(0, p1_len));
298                 else
299                         path = to_utf8(p2 + p1.substr(common_len,
300                                                         p1_len - common_len));
301         }
302
303         return true;
304 }
305
306
307 string external_path(string const & p)
308 {
309         return convert_path(p, PathStyle(posix));
310 }
311
312
313 string internal_path(string const & p)
314 {
315         return convert_path(p, PathStyle(posix));
316 }
317
318
319 string safe_internal_path(string const & p, file_access)
320 {
321         return convert_path(p, PathStyle(posix));
322 }
323
324
325 string external_path_list(string const & p)
326 {
327         return convert_path_list(p, PathStyle(posix));
328 }
329
330
331 string internal_path_list(string const & p)
332 {
333         return convert_path_list(p, PathStyle(posix));
334 }
335
336
337 string latex_path(string const & p)
338 {
339         // We may need a posix style path or a windows style path (depending
340         // on windows_style_tex_paths_), but we use always forward slashes,
341         // since it gets written into a .tex file.
342
343         if (windows_style_tex_paths_ && FileName::isAbsolute(p)) {
344                 string dos_path = convert_path(p, PathStyle(windows));
345                 LYXERR(Debug::LATEX, "<Path correction for LaTeX> ["
346                         << p << "]->>[" << dos_path << ']');
347                 return dos_path;
348         }
349
350         return convert_path(p, PathStyle(posix));
351 }
352
353
354 string latex_path_list(string const & p)
355 {
356         // We may need a posix style path or a windows style path (depending
357         // on windows_style_tex_paths_), but we use always forward slashes,
358         // since this is standard for all tex engines.
359
360         if (windows_style_tex_paths_)
361                 return convert_path_list(p, PathStyle(windows));
362
363         return convert_path_list(p, PathStyle(posix));
364 }
365
366
367 bool is_valid_strftime(string const & p)
368 {
369         string::size_type pos = p.find_first_of('%');
370         while (pos != string::npos) {
371                 if (pos + 1 == string::npos)
372                         break;
373                 if (!containsOnly(p.substr(pos + 1, 1),
374                         "aAbBcCdDeEFgGhHIjklmMnOpPrRsStTuUVwWxXyYzZ%+"))
375                         return false;
376                 if (pos + 2 == string::npos)
377                       break;
378                 pos = p.find_first_of('%', pos + 2);
379         }
380         return true;
381 }
382
383
384 // returns a string suitable to be passed to popen when
385 // reading a pipe
386 char const * popen_read_mode()
387 {
388         return "r";
389 }
390
391
392 string const & nulldev()
393 {
394         static string const nulldev_ = "/dev/null";
395         return nulldev_;
396 }
397
398
399 shell_type shell()
400 {
401         return UNIX;
402 }
403
404
405 char path_separator(path_type type)
406 {
407         if (type == TEXENGINE)
408                 return windows_style_tex_paths_ ? ';' : ':';
409
410         return ':';
411 }
412
413
414 void windows_style_tex_paths(bool use_windows_paths)
415 {
416         windows_style_tex_paths_ = use_windows_paths;
417 }
418
419
420 bool canAutoOpenFile(string const & ext, auto_open_mode const mode)
421 {
422         if (ext.empty())
423                 return false;
424
425         string const full_ext = "." + ext;
426
427         DWORD bufSize = MAX_PATH + 100;
428         TCHAR buf[MAX_PATH + 100];
429         // reference: http://msdn.microsoft.com/en-us/library/bb773471.aspx
430         char const * action = (mode == VIEW) ? "open" : "edit";
431         return S_OK == AssocQueryString(ASSOCF_INIT_IGNOREUNKNOWN,
432                 ASSOCSTR_EXECUTABLE, full_ext.c_str(), action, buf, &bufSize);
433 }
434
435
436 bool autoOpenFile(string const & filename, auto_open_mode const mode,
437                   string const & path)
438 {
439         string const texinputs = os::latex_path_list(
440                         replaceCurdirPath(path, lyxrc.texinputs_prefix));
441         string const otherinputs = os::latex_path_list(path);
442         string const sep = windows_style_tex_paths_ ? ";" : ":";
443         string const oldtexinputs = getEnv("TEXINPUTS");
444         string const newtexinputs = "." + sep + texinputs + sep + oldtexinputs;
445         string const oldbibinputs = getEnv("BIBINPUTS");
446         string const newbibinputs = "." + sep + otherinputs + sep + oldbibinputs;
447         string const oldbstinputs = getEnv("BSTINPUTS");
448         string const newbstinputs = "." + sep + otherinputs + sep + oldbstinputs;
449         string const oldtexfonts = getEnv("TEXFONTS");
450         string const newtexfonts = "." + sep + otherinputs + sep + oldtexfonts;
451         if (!path.empty() && !lyxrc.texinputs_prefix.empty()) {
452                 setEnv("TEXINPUTS", newtexinputs);
453                 setEnv("BIBINPUTS", newbibinputs);
454                 setEnv("BSTINPUTS", newbstinputs);
455                 setEnv("TEXFONTS", newtexfonts);
456                 cygwin_internal(CW_SYNC_WINENV);
457         }
458
459         QString const win_path =
460                 toqstr(convert_path(filename, PathStyle(windows)));
461
462         // reference: http://msdn.microsoft.com/en-us/library/bb762153.aspx
463         wchar_t const * action = (mode == VIEW) ? L"open" : L"edit";
464         bool success = reinterpret_cast<long>(ShellExecuteW(NULL, action,
465                         reinterpret_cast<wchar_t const *>(win_path.utf16()),
466                         NULL, NULL, 1)) > 32;
467
468         if (!path.empty() && !lyxrc.texinputs_prefix.empty()) {
469                 setEnv("TEXINPUTS", oldtexinputs);
470                 setEnv("BIBINPUTS", oldbibinputs);
471                 setEnv("BSTINPUTS", oldbstinputs);
472                 setEnv("TEXFONTS", oldtexfonts);
473                 cygwin_internal(CW_SYNC_WINENV);
474         }
475         return success;
476 }
477
478
479 string real_path(string const & path)
480 {
481         char rpath[PATH_MAX + 1];
482         char * result = realpath(path.c_str(), rpath);
483         return FileName::fromFilesystemEncoding(result ? rpath : path).absFileName();
484 }
485
486 } // namespace os
487 } // namespace support
488 } // namespace lyx