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
18 #include "support/os.h"
20 #include "support/FileName.h"
21 #include "support/lstrings.h"
22 #include "support/debug.h"
32 #include <cygwin/version.h>
33 #include <sys/cygwin.h>
48 bool windows_style_tex_paths_ = false;
50 // In both is_posix_path() and is_windows_path() it is assumed that
51 // a valid posix or pseudo-windows path is passed. They simply tell
52 // whether the path looks posix/pseudo-windows or not.
54 bool is_posix_path(string const & p)
57 (!contains(p, '\\') && (p.length() <= 1 || p[1] != ':'));
60 // This is a test for a win32 style path with forward slashes (pseudo-windows).
62 bool is_windows_path(string const & p)
64 return p.empty() || (!contains(p, '\\') && p[0] != '/');
68 // Starting from Cygwin 1.7, new APIs for path conversions were introduced.
69 // The old ones are now deprecated, so avoid them if we detect a modern Cygwin.
71 #if CYGWIN_VERSION_DLL_MAJOR >= 1007
74 posix = CCP_WIN_A_TO_POSIX | CCP_RELATIVE,
75 windows = CCP_POSIX_TO_WIN_A | CCP_RELATIVE
79 /// Convert a path to or from posix style.
80 /// \p p is encoded in local 8bit encoding or utf8.
81 /// The result is returned in the same encoding as \p p.
82 string convert_path(string const & p, PathStyle const & target)
84 if ((target == posix && is_posix_path(p)) ||
85 (target == windows && is_windows_path(p)))
88 char path_buf[PATH_MAX];
90 // cygwin_conv_path does not care about the encoding.
91 if (cygwin_conv_path(target, p.c_str(), path_buf, sizeof(path_buf))) {
92 lyxerr << "LyX: Cannot convert path: " << p << endl;
93 return subst(p, '\\', '/');
95 return subst(path_buf, '\\', '/');
99 /// Convert a path list to or from posix style.
100 /// \p p is encoded in local 8bit encoding or utf8.
101 /// The result is returned in the same encoding as \p p.
102 string convert_path_list(string const & p, PathStyle const & target)
107 char const * const pc = p.c_str();
108 PathStyle const actual = cygwin_posix_path_list_p(pc) ? posix : windows;
110 if (target != actual) {
111 int const size = cygwin_conv_path_list(target, pc, NULL, 0);
112 char * ptr = new char[size];
113 if (ptr && cygwin_conv_path_list(target, pc, ptr, size) == 0) {
114 string const path_list = subst(ptr, '\\', '/');
118 lyxerr << "LyX: Cannot convert path list: " << p << endl;
120 return subst(p, '\\', '/');
131 /// Convert a path to or from posix style.
132 /// \p p is encoded in local 8bit encoding or utf8.
133 /// The result is returned in the same encoding as \p p.
134 string convert_path(string const & p, PathStyle const & target)
136 char path_buf[PATH_MAX];
138 if ((target == posix && is_posix_path(p)) ||
139 (target == windows && is_windows_path(p)))
144 // cygwin_conv_to_posix_path and cygwin_conv_to_win32_path do not
145 // care about the encoding.
147 cygwin_conv_to_posix_path(p.c_str(), path_buf);
149 cygwin_conv_to_win32_path(p.c_str(), path_buf);
151 return subst(path_buf[0] ? path_buf : p, '\\', '/');
155 /// Convert a path list to or from posix style.
156 /// \p p is encoded in local 8bit encoding or utf8.
157 /// The result is returned in the same encoding as \p p.
158 string convert_path_list(string const & p, PathStyle const & target)
163 char const * const pc = p.c_str();
164 PathStyle const actual = cygwin_posix_path_list_p(pc) ? posix : windows;
166 if (target != actual) {
167 int const target_size = (target == posix) ?
168 cygwin_win32_to_posix_path_list_buf_size(pc) :
169 cygwin_posix_to_win32_path_list_buf_size(pc);
171 char * ptr = new char[target_size];
174 // FIXME: See comment in convert_path() above
176 cygwin_win32_to_posix_path_list(pc, ptr);
178 cygwin_posix_to_win32_path_list(pc, ptr);
180 string path_list = subst(ptr, '\\', '/');
186 return subst(p, '\\', '/');
192 BOOL terminate_handler(DWORD event)
194 if (event == CTRL_CLOSE_EVENT
195 || event == CTRL_LOGOFF_EVENT
196 || event == CTRL_SHUTDOWN_EVENT) {
205 void init(int, char *[])
207 // Make sure that the TEMP variable is set
208 // and sync the Windows environment.
209 setenv("TEMP", "/tmp", false);
210 cygwin_internal(CW_SYNC_WINENV);
212 // Catch shutdown events.
213 SetConsoleCtrlHandler((PHANDLER_ROUTINE)terminate_handler, TRUE);
217 string current_root()
223 bool isFilesystemCaseSensitive()
229 docstring::size_type common_path(docstring const & p1, docstring const & p2)
231 docstring::size_type i = 0;
232 docstring::size_type const p1_len = p1.length();
233 docstring::size_type const p2_len = p2.length();
234 while (i < p1_len && i < p2_len && uppercase(p1[i]) == uppercase(p2[i]))
236 if ((i < p1_len && i < p2_len)
237 || (i < p1_len && p1[i] != '/' && i == p2_len)
238 || (i < p2_len && p2[i] != '/' && i == p1_len))
241 --i; // here was the last match
242 while (i && p1[i] != '/')
249 bool path_prefix_is(string const & path, string const & pre)
251 return path_prefix_is(const_cast<string &>(path), pre, CASE_UNCHANGED);
255 bool path_prefix_is(string & path, string const & pre, path_case how)
257 docstring const p1 = from_utf8(path);
258 docstring const p2 = from_utf8(pre);
259 docstring::size_type const p1_len = p1.length();
260 docstring::size_type const p2_len = p2.length();
261 docstring::size_type common_len = common_path(p1, p2);
263 if (p2[p2_len - 1] == '/' && p1_len != p2_len)
266 if (common_len != p2_len)
269 if (how == CASE_ADJUSTED && !prefixIs(path, pre)) {
270 if (p1_len < common_len)
271 path = to_utf8(p2.substr(0, p1_len));
273 path = to_utf8(p2 + p1.substr(common_len,
274 p1_len - common_len));
281 string external_path(string const & p)
283 return convert_path(p, PathStyle(posix));
287 string internal_path(string const & p)
289 return convert_path(p, PathStyle(posix));
293 string external_path_list(string const & p)
295 return convert_path_list(p, PathStyle(posix));
299 string internal_path_list(string const & p)
301 return convert_path_list(p, PathStyle(posix));
305 string latex_path(string const & p)
307 // We may need a posix style path or a windows style path (depending
308 // on windows_style_tex_paths_), but we use always forward slashes,
309 // since it gets written into a .tex file.
311 if (windows_style_tex_paths_ && FileName::isAbsolute(p)) {
312 string dos_path = convert_path(p, PathStyle(windows));
313 LYXERR(Debug::LATEX, "<Path correction for LaTeX> ["
314 << p << "]->>[" << dos_path << ']');
318 return convert_path(p, PathStyle(posix));
322 bool is_valid_strftime(string const & p)
324 string::size_type pos = p.find_first_of('%');
325 while (pos != string::npos) {
326 if (pos + 1 == string::npos)
328 if (!containsOnly(p.substr(pos + 1, 1),
329 "aAbBcCdDeEFgGhHIjklmMnOpPrRsStTuUVwWxXyYzZ%+"))
331 if (pos + 2 == string::npos)
333 pos = p.find_first_of('%', pos + 2);
339 // returns a string suitable to be passed to popen when
341 char const * popen_read_mode()
347 string const & nulldev()
349 static string const nulldev_ = "/dev/null";
354 bool is_terminal(io_channel channel)
356 return isatty(channel);
366 char path_separator()
372 void windows_style_tex_paths(bool use_windows_paths)
374 windows_style_tex_paths_ = use_windows_paths;
378 bool canAutoOpenFile(string const & ext, auto_open_mode const mode)
383 string const full_ext = "." + ext;
385 DWORD bufSize = MAX_PATH + 100;
386 TCHAR buf[MAX_PATH + 100];
387 // reference: http://msdn.microsoft.com/en-us/library/bb773471.aspx
388 char const * action = (mode == VIEW) ? "open" : "edit";
389 return S_OK == AssocQueryString(ASSOCF_INIT_IGNOREUNKNOWN,
390 ASSOCSTR_EXECUTABLE, full_ext.c_str(), action, buf, &bufSize);
394 bool autoOpenFile(string const & filename, auto_open_mode const mode)
396 // reference: http://msdn.microsoft.com/en-us/library/bb762153.aspx
397 string const win_path = to_local8bit(from_utf8(convert_path(filename, PathStyle(windows))));
398 char const * action = (mode == VIEW) ? "open" : "edit";
399 return reinterpret_cast<int>(ShellExecute(NULL, action,
400 win_path.c_str(), NULL, NULL, 1)) > 32;
404 string real_path(string const & path)
406 char rpath[PATH_MAX + 1];
407 char * result = realpath(path.c_str(), rpath);
408 return FileName::fromFilesystemEncoding(result ? rpath : path).absFilename();
412 } // namespace support