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