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, "");
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);
226 // Catch shutdown events.
227 SetConsoleCtrlHandler((PHANDLER_ROUTINE)terminate_handler, TRUE);
231 string utf8_argv(int i)
233 LASSERT(i < argc_, return "");
234 return to_utf8(from_local8bit(argv_[i]));
238 void remove_internal_args(int, int)
242 string current_root()
248 bool isFilesystemCaseSensitive()
254 docstring::size_type common_path(docstring const & p1, docstring const & p2)
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]))
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))
266 --i; // here was the last match
267 while (i && p1[i] != '/')
274 bool path_prefix_is(string const & path, string const & pre)
276 return path_prefix_is(const_cast<string &>(path), pre, CASE_UNCHANGED);
280 bool path_prefix_is(string & path, string const & pre, path_case how)
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);
288 if (p2[p2_len - 1] == '/' && p1_len != p2_len)
291 if (common_len != p2_len)
294 if (how == CASE_ADJUSTED && !prefixIs(path, pre)) {
295 if (p1_len < common_len)
296 path = to_utf8(p2.substr(0, p1_len));
298 path = to_utf8(p2 + p1.substr(common_len,
299 p1_len - common_len));
306 string external_path(string const & p)
308 return convert_path(p, PathStyle(posix));
312 string internal_path(string const & p)
314 return convert_path(p, PathStyle(posix));
318 string safe_internal_path(string const & p, file_access)
320 return convert_path(p, PathStyle(posix));
324 string external_path_list(string const & p)
326 return convert_path_list(p, PathStyle(posix));
330 string internal_path_list(string const & p)
332 return convert_path_list(p, PathStyle(posix));
336 string latex_path(string const & p)
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.
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 << ']');
349 return convert_path(p, PathStyle(posix));
353 string latex_path_list(string const & p)
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.
359 if (windows_style_tex_paths_)
360 return convert_path_list(p, PathStyle(windows));
362 return convert_path_list(p, PathStyle(posix));
366 bool is_valid_strftime(string const & p)
368 string::size_type pos = p.find_first_of('%');
369 while (pos != string::npos) {
370 if (pos + 1 == string::npos)
372 if (!containsOnly(p.substr(pos + 1, 1),
373 "aAbBcCdDeEFgGhHIjklmMnOpPrRsStTuUVwWxXyYzZ%+"))
375 if (pos + 2 == string::npos)
377 pos = p.find_first_of('%', pos + 2);
383 // returns a string suitable to be passed to popen when
385 char const * popen_read_mode()
391 string const & nulldev()
393 static string const nulldev_ = "/dev/null";
404 char path_separator(path_type type)
406 if (type == TEXENGINE)
407 return windows_style_tex_paths_ ? ';' : ':';
413 void windows_style_tex_paths(bool use_windows_paths)
415 windows_style_tex_paths_ = use_windows_paths;
419 bool canAutoOpenFile(string const & ext, auto_open_mode const mode)
424 string const full_ext = "." + ext;
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);
435 bool autoOpenFile(string const & filename, auto_open_mode const mode,
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);
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;
454 if (!path.empty() && !lyxrc.texinputs_prefix.empty()) {
455 setEnv("TEXINPUTS", oldval);
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