3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Ruurd A. Reitsma
7 * \author Claus Hentschel
8 * \author Angus Leeming
9 * \author Enrico Forestieri
11 * Full author contact details are available in file CREDITS.
13 * Various OS specific functions
20 #include "support/os.h"
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"
37 #include <cygwin/version.h>
38 #include <sys/cygwin.h>
56 bool windows_style_tex_paths_ = false;
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.
62 bool is_posix_path(string const & p)
65 (!contains(p, '\\') && (p.length() <= 1 || p[1] != ':'));
68 // This is a test for a win32 style path with forward slashes (pseudo-windows).
70 bool is_windows_path(string const & p)
72 return p.empty() || (!contains(p, '\\') && p[0] != '/');
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.
79 #if CYGWIN_VERSION_DLL_MAJOR >= 1007
82 posix = CCP_WIN_A_TO_POSIX | CCP_RELATIVE,
83 windows = CCP_POSIX_TO_WIN_A | CCP_RELATIVE
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)
92 if ((target == posix && is_posix_path(p)) ||
93 (target == windows && is_windows_path(p)))
96 char path_buf[PATH_MAX];
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, '\\', '/');
103 return subst(path_buf, '\\', '/');
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)
115 char const * const pc = p.c_str();
116 PathStyle const actual = cygwin_posix_path_list_p(pc) ? posix : windows;
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, '\\', '/');
126 lyxerr << "LyX: Cannot convert path list: " << p << endl;
128 return subst(p, '\\', '/');
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)
144 char path_buf[PATH_MAX];
146 if ((target == posix && is_posix_path(p)) ||
147 (target == windows && is_windows_path(p)))
152 // cygwin_conv_to_posix_path and cygwin_conv_to_win32_path do not
153 // care about the encoding.
155 cygwin_conv_to_posix_path(p.c_str(), path_buf);
157 cygwin_conv_to_win32_path(p.c_str(), path_buf);
159 return subst(path_buf[0] ? path_buf : p, '\\', '/');
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)
171 char const * const pc = p.c_str();
172 PathStyle const actual = cygwin_posix_path_list_p(pc) ? posix : windows;
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);
179 char * ptr = new char[target_size];
182 // FIXME: See comment in convert_path() above
184 cygwin_win32_to_posix_path_list(pc, ptr);
186 cygwin_posix_to_win32_path_list(pc, ptr);
188 string path_list = subst(ptr, '\\', '/');
194 return subst(p, '\\', '/');
200 BOOL terminate_handler(DWORD event)
202 if (event == CTRL_CLOSE_EVENT
203 || event == CTRL_LOGOFF_EVENT
204 || event == CTRL_SHUTDOWN_EVENT) {
213 void init(int argc, char ** argv[])
218 // Set environment's default locale
219 setlocale(LC_ALL, "");
220 setlocale(LC_NUMERIC, "C");
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);
227 // Catch shutdown events.
228 SetConsoleCtrlHandler((PHANDLER_ROUTINE)terminate_handler, TRUE);
232 string utf8_argv(int i)
234 LASSERT(i < argc_, return "");
235 return to_utf8(from_local8bit(argv_[i]));
239 void remove_internal_args(int, int)
243 string current_root()
249 bool isFilesystemCaseSensitive()
255 docstring::size_type common_path(docstring const & p1, docstring const & p2)
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]))
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))
267 --i; // here was the last match
268 while (i && p1[i] != '/')
275 bool path_prefix_is(string const & path, string const & pre)
277 return path_prefix_is(const_cast<string &>(path), pre, CASE_UNCHANGED);
281 bool path_prefix_is(string & path, string const & pre, path_case how)
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);
289 if (p2[p2_len - 1] == '/' && p1_len != p2_len)
292 if (common_len != p2_len)
295 if (how == CASE_ADJUSTED && !prefixIs(path, pre)) {
296 if (p1_len < common_len)
297 path = to_utf8(p2.substr(0, p1_len));
299 path = to_utf8(p2 + p1.substr(common_len,
300 p1_len - common_len));
307 string external_path(string const & p)
309 return convert_path(p, PathStyle(posix));
313 string internal_path(string const & p)
315 return convert_path(p, PathStyle(posix));
319 string safe_internal_path(string const & p, file_access)
321 return convert_path(p, PathStyle(posix));
325 string external_path_list(string const & p)
327 return convert_path_list(p, PathStyle(posix));
331 string internal_path_list(string const & p)
333 return convert_path_list(p, PathStyle(posix));
337 string latex_path(string const & p)
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.
343 if (windows_style_tex_paths_ && FileName::isAbsolute(p)) {
344 string dos_path = convert_path(p, PathStyle(windows));
345 LYXERR(Debug::OUTFILE, "<Path correction for LaTeX> ["
346 << p << "]->>[" << dos_path << ']');
350 return convert_path(p, PathStyle(posix));
354 string latex_path_list(string const & p)
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.
360 if (windows_style_tex_paths_)
361 return convert_path_list(p, PathStyle(windows));
363 return convert_path_list(p, PathStyle(posix));
367 // returns a string suitable to be passed to popen when
369 char const * popen_read_mode()
375 string const & nulldev()
377 static string const nulldev_ = "/dev/null";
388 char path_separator(path_type type)
390 if (type == TEXENGINE)
391 return windows_style_tex_paths_ ? ';' : ':';
397 void windows_style_tex_paths(bool use_windows_paths)
399 windows_style_tex_paths_ = use_windows_paths;
403 bool canAutoOpenFile(string const & ext, auto_open_mode const mode)
408 string const full_ext = "." + ext;
410 DWORD bufSize = MAX_PATH + 100;
411 TCHAR buf[MAX_PATH + 100];
412 // reference: http://msdn.microsoft.com/en-us/library/bb773471.aspx
413 char const * action = (mode == VIEW) ? "open" : "edit";
414 return S_OK == AssocQueryString(ASSOCF_INIT_IGNOREUNKNOWN,
415 ASSOCSTR_EXECUTABLE, full_ext.c_str(), action, buf, &bufSize);
419 bool autoOpenFile(string const & filename, auto_open_mode const mode,
422 string const texinputs = os::latex_path_list(
423 replaceCurdirPath(path, lyxrc.texinputs_prefix));
424 string const otherinputs = os::latex_path_list(path);
425 string const sep = windows_style_tex_paths_ ? ";" : ":";
426 string const oldtexinputs = getEnv("TEXINPUTS");
427 string const newtexinputs = "." + sep + texinputs + sep + oldtexinputs;
428 string const oldbibinputs = getEnv("BIBINPUTS");
429 string const newbibinputs = "." + sep + otherinputs + sep + oldbibinputs;
430 string const oldbstinputs = getEnv("BSTINPUTS");
431 string const newbstinputs = "." + sep + otherinputs + sep + oldbstinputs;
432 string const oldtexfonts = getEnv("TEXFONTS");
433 string const newtexfonts = "." + sep + otherinputs + sep + oldtexfonts;
434 if (!path.empty() && !lyxrc.texinputs_prefix.empty()) {
435 setEnv("TEXINPUTS", newtexinputs);
436 setEnv("BIBINPUTS", newbibinputs);
437 setEnv("BSTINPUTS", newbstinputs);
438 setEnv("TEXFONTS", newtexfonts);
439 cygwin_internal(CW_SYNC_WINENV);
442 QString const win_path =
443 toqstr(convert_path(filename, PathStyle(windows)));
445 // reference: http://msdn.microsoft.com/en-us/library/bb762153.aspx
446 wchar_t const * action = (mode == VIEW) ? L"open" : L"edit";
447 bool success = reinterpret_cast<long>(ShellExecuteW(NULL, action,
448 reinterpret_cast<wchar_t const *>(win_path.utf16()),
449 NULL, NULL, 1)) > 32;
451 if (!path.empty() && !lyxrc.texinputs_prefix.empty()) {
452 setEnv("TEXINPUTS", oldtexinputs);
453 setEnv("BIBINPUTS", oldbibinputs);
454 setEnv("BSTINPUTS", oldbstinputs);
455 setEnv("TEXFONTS", oldtexfonts);
456 cygwin_internal(CW_SYNC_WINENV);
462 string real_path(string const & path)
464 char rpath[PATH_MAX + 1];
465 char * result = realpath(path.c_str(), rpath);
466 return FileName::fromFilesystemEncoding(result ? rpath : path).absFileName();
470 } // namespace support