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
10 * Full author contact details are available in file CREDITS.
12 * Various OS specific functions
17 #include "support/os.h"
19 #include "support/lstrings.h"
20 #include "support/debug.h"
28 #include <sys/cygwin.h>
38 bool windows_style_tex_paths_ = false;
40 // In both is_posix_path() and is_windows_path() it is assumed that
41 // a valid posix or pseudo-windows path is passed. They simply tell
42 // whether the path looks posix/pseudo-windows or not.
44 bool is_posix_path(string const & p)
47 (!contains(p, '\\') && (p.length() <= 1 || p[1] != ':'));
50 // This is a test for a win32 style path with forward slashes (pseudo-windows).
52 bool is_windows_path(string const & p)
54 return p.empty() || (!contains(p, '\\') && p[0] != '/');
64 /// Convert a path to or from posix style.
65 /// \p p is encoded in local 8bit encoding or utf8.
66 /// The result is returned in the same encoding as \p p.
67 string convert_path(string const & p, PathStyle const & target)
69 char path_buf[PATH_MAX];
71 if ((target == posix && is_posix_path(p)) ||
72 (target == windows && is_windows_path(p)))
77 // cygwin_conv_to_posix_path and cygwin_conv_to_win32_path do not
78 // care about the encoding.
80 cygwin_conv_to_posix_path(p.c_str(), path_buf);
82 cygwin_conv_to_win32_path(p.c_str(), path_buf);
84 return subst(path_buf[0] ? path_buf : p, '\\', '/');
88 /// Convert a path list to or from posix style.
89 /// \p p is encoded in local 8bit encoding or utf8.
90 /// The result is returned in the same encoding as \p p.
91 string convert_path_list(string const & p, PathStyle const & target)
96 char const * const pc = p.c_str();
97 PathStyle const actual = cygwin_posix_path_list_p(pc) ? posix : windows;
99 if (target != actual) {
100 int const target_size = (target == posix) ?
101 cygwin_win32_to_posix_path_list_buf_size(pc) :
102 cygwin_posix_to_win32_path_list_buf_size(pc);
104 char * ptr = new char[target_size];
107 // FIXME: See comment in convert_path() above
109 cygwin_win32_to_posix_path_list(pc, ptr);
111 cygwin_posix_to_win32_path_list(pc, ptr);
113 string path_list = subst(ptr, '\\', '/');
119 return subst(p, '\\', '/');
124 void os::init(int, char *[])
126 // Make sure that the TEMP variable is set
127 // and sync the Windows environment.
129 setenv("TEMP", "/tmp", false);
130 cygwin_internal(CW_SYNC_WINENV);
134 string current_root()
140 docstring::size_type common_path(docstring const & p1, docstring const & p2)
142 docstring::size_type i = 0;
143 docstring::size_type const p1_len = p1.length();
144 docstring::size_type const p2_len = p2.length();
145 while (i < p1_len && i < p2_len && uppercase(p1[i]) == uppercase(p2[i]))
147 if ((i < p1_len && i < p2_len)
148 || (i < p1_len && p1[i] != '/' && i == p2_len)
149 || (i < p2_len && p2[i] != '/' && i == p1_len))
152 --i; // here was the last match
153 while (i && p1[i] != '/')
160 string external_path(string const & p)
162 return convert_path(p, PathStyle(posix));
166 string internal_path(string const & p)
168 return convert_path(p, PathStyle(posix));
172 string external_path_list(string const & p)
174 return convert_path_list(p, PathStyle(posix));
178 string internal_path_list(string const & p)
180 return convert_path_list(p, PathStyle(posix));
184 string latex_path(string const & p)
186 // We may need a posix style path or a windows style path (depending
187 // on windows_style_tex_paths_), but we use always forward slashes,
188 // since it gets written into a .tex file.
190 if (windows_style_tex_paths_ && is_absolute_path(p)) {
191 string dos_path = convert_path(p, PathStyle(windows));
192 LYXERR(Debug::LATEX, "<Path correction for LaTeX> ["
193 << p << "]->>[" << dos_path << ']');
197 return convert_path(p, PathStyle(posix));
201 bool is_absolute_path(string const & p)
206 bool isDosPath = (p.length() > 1 && p[1] == ':');
207 bool isUnixPath = (p[0] == '/');
209 return isDosPath || isUnixPath;
213 // returns a string suitable to be passed to popen when
215 char const * popen_read_mode()
221 string const & nulldev()
223 static string const nulldev_ = "/dev/null";
234 char path_separator()
240 void windows_style_tex_paths(bool use_windows_paths)
242 windows_style_tex_paths_ = use_windows_paths;
246 bool canAutoOpenFile(string const & ext, auto_open_mode const mode)
251 string const full_ext = "." + ext;
253 DWORD bufSize = MAX_PATH + 100;
254 TCHAR buf[MAX_PATH + 100];
255 // reference: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc
256 // /platform/shell/reference/shlwapi/registry/assocquerystring.asp
257 char const * action = (mode == VIEW) ? "open" : "edit";
258 return S_OK == AssocQueryString(0, ASSOCSTR_EXECUTABLE,
259 full_ext.c_str(), action, buf, &bufSize);
263 bool autoOpenFile(string const & filename, auto_open_mode const mode)
265 // reference: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc
266 // /platform/shell/reference/functions/shellexecute.asp
267 string const win_path = to_local8bit(from_utf8(convert_path(filename, PathStyle(windows))));
268 char const * action = (mode == VIEW) ? "open" : "edit";
269 return reinterpret_cast<int>(ShellExecute(NULL, action,
270 win_path.c_str(), NULL, NULL, 1)) > 32;
274 } // namespace support