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>
39 bool windows_style_tex_paths_ = false;
41 // In both is_posix_path() and is_windows_path() it is assumed that
42 // a valid posix or pseudo-windows path is passed. They simply tell
43 // whether the path looks posix/pseudo-windows or not.
45 bool is_posix_path(string const & p)
48 (!contains(p, '\\') && (p.length() <= 1 || p[1] != ':'));
51 // This is a test for a win32 style path with forward slashes (pseudo-windows).
53 bool is_windows_path(string const & p)
55 return p.empty() || (!contains(p, '\\') && p[0] != '/');
65 /// Convert a path to or from posix style.
66 /// \p p is encoded in local 8bit encoding or utf8.
67 /// The result is returned in the same encoding as \p p.
68 string convert_path(string const & p, PathStyle const & target)
70 char path_buf[PATH_MAX];
72 if ((target == posix && is_posix_path(p)) ||
73 (target == windows && is_windows_path(p)))
78 // cygwin_conv_to_posix_path and cygwin_conv_to_win32_path do not
79 // care about the encoding.
81 cygwin_conv_to_posix_path(p.c_str(), path_buf);
83 cygwin_conv_to_win32_path(p.c_str(), path_buf);
85 return subst(path_buf[0] ? path_buf : p, '\\', '/');
89 /// Convert a path list to or from posix style.
90 /// \p p is encoded in local 8bit encoding or utf8.
91 /// The result is returned in the same encoding as \p p.
92 string convert_path_list(string const & p, PathStyle const & target)
97 char const * const pc = p.c_str();
98 PathStyle const actual = cygwin_posix_path_list_p(pc) ? posix : windows;
100 if (target != actual) {
101 int const target_size = (target == posix) ?
102 cygwin_win32_to_posix_path_list_buf_size(pc) :
103 cygwin_posix_to_win32_path_list_buf_size(pc);
105 char * ptr = new char[target_size];
108 // FIXME: See comment in convert_path() above
110 cygwin_win32_to_posix_path_list(pc, ptr);
112 cygwin_posix_to_win32_path_list(pc, ptr);
114 string path_list = subst(ptr, '\\', '/');
120 return subst(p, '\\', '/');
125 void os::init(int, char *[])
127 // Make sure that the TEMP variable is set
128 // and sync the Windows environment.
130 setenv("TEMP", "/tmp", false);
131 cygwin_internal(CW_SYNC_WINENV);
135 string current_root()
141 docstring::size_type common_path(docstring const & p1, docstring const & p2)
143 docstring::size_type i = 0;
144 docstring::size_type const p1_len = p1.length();
145 docstring::size_type const p2_len = p2.length();
146 while (i < p1_len && i < p2_len && uppercase(p1[i]) == uppercase(p2[i]))
148 if ((i < p1_len && i < p2_len)
149 || (i < p1_len && p1[i] != '/' && i == p2_len)
150 || (i < p2_len && p2[i] != '/' && i == p1_len))
153 --i; // here was the last match
154 while (i && p1[i] != '/')
161 string external_path(string const & p)
163 return convert_path(p, PathStyle(posix));
167 string internal_path(string const & p)
169 return convert_path(p, PathStyle(posix));
173 string external_path_list(string const & p)
175 return convert_path_list(p, PathStyle(posix));
179 string internal_path_list(string const & p)
181 return convert_path_list(p, PathStyle(posix));
185 string latex_path(string const & p)
187 // We may need a posix style path or a windows style path (depending
188 // on windows_style_tex_paths_), but we use always forward slashes,
189 // since it gets written into a .tex file.
191 if (windows_style_tex_paths_ && is_absolute_path(p)) {
192 string dos_path = convert_path(p, PathStyle(windows));
193 LYXERR(Debug::LATEX, "<Path correction for LaTeX> ["
194 << p << "]->>[" << dos_path << ']');
198 return convert_path(p, PathStyle(posix));
202 bool is_absolute_path(string const & p)
207 bool isDosPath = (p.length() > 1 && p[1] == ':');
208 bool isUnixPath = (p[0] == '/');
210 return isDosPath || isUnixPath;
214 // returns a string suitable to be passed to popen when
216 char const * popen_read_mode()
222 string const & nulldev()
224 static string const nulldev_ = "/dev/null";
235 char path_separator()
241 void windows_style_tex_paths(bool use_windows_paths)
243 windows_style_tex_paths_ = use_windows_paths;
247 bool canAutoOpenFile(string const & ext, auto_open_mode const mode)
252 string const full_ext = "." + ext;
254 DWORD bufSize = MAX_PATH + 100;
255 TCHAR buf[MAX_PATH + 100];
256 // reference: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc
257 // /platform/shell/reference/shlwapi/registry/assocquerystring.asp
258 char const * action = (mode == VIEW) ? "open" : "edit";
259 return S_OK == AssocQueryString(0, ASSOCSTR_EXECUTABLE,
260 full_ext.c_str(), action, buf, &bufSize);
264 bool autoOpenFile(string const & filename, auto_open_mode const mode)
266 // reference: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc
267 // /platform/shell/reference/functions/shellexecute.asp
268 string const win_path = to_local8bit(from_utf8(convert_path(filename, PathStyle(windows))));
269 char const * action = (mode == VIEW) ? "open" : "edit";
270 return reinterpret_cast<int>(ShellExecute(NULL, action,
271 win_path.c_str(), NULL, NULL, 1)) > 32;
275 } // namespace support