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/FileName.h"
20 #include "support/lstrings.h"
21 #include "support/debug.h"
30 #include <sys/cygwin.h>
40 bool windows_style_tex_paths_ = false;
42 // In both is_posix_path() and is_windows_path() it is assumed that
43 // a valid posix or pseudo-windows path is passed. They simply tell
44 // whether the path looks posix/pseudo-windows or not.
46 bool is_posix_path(string const & p)
49 (!contains(p, '\\') && (p.length() <= 1 || p[1] != ':'));
52 // This is a test for a win32 style path with forward slashes (pseudo-windows).
54 bool is_windows_path(string const & p)
56 return p.empty() || (!contains(p, '\\') && p[0] != '/');
66 /// Convert a path to or from posix style.
67 /// \p p is encoded in local 8bit encoding or utf8.
68 /// The result is returned in the same encoding as \p p.
69 string convert_path(string const & p, PathStyle const & target)
71 char path_buf[PATH_MAX];
73 if ((target == posix && is_posix_path(p)) ||
74 (target == windows && is_windows_path(p)))
79 // cygwin_conv_to_posix_path and cygwin_conv_to_win32_path do not
80 // care about the encoding.
82 cygwin_conv_to_posix_path(p.c_str(), path_buf);
84 cygwin_conv_to_win32_path(p.c_str(), path_buf);
86 return subst(path_buf[0] ? path_buf : p, '\\', '/');
90 /// Convert a path list to or from posix style.
91 /// \p p is encoded in local 8bit encoding or utf8.
92 /// The result is returned in the same encoding as \p p.
93 string convert_path_list(string const & p, PathStyle const & target)
98 char const * const pc = p.c_str();
99 PathStyle const actual = cygwin_posix_path_list_p(pc) ? posix : windows;
101 if (target != actual) {
102 int const target_size = (target == posix) ?
103 cygwin_win32_to_posix_path_list_buf_size(pc) :
104 cygwin_posix_to_win32_path_list_buf_size(pc);
106 char * ptr = new char[target_size];
109 // FIXME: See comment in convert_path() above
111 cygwin_win32_to_posix_path_list(pc, ptr);
113 cygwin_posix_to_win32_path_list(pc, ptr);
115 string path_list = subst(ptr, '\\', '/');
121 return subst(p, '\\', '/');
126 void init(int, char *[])
128 // Make sure that the TEMP variable is set
129 // and sync the Windows environment.
131 setenv("TEMP", "/tmp", false);
132 cygwin_internal(CW_SYNC_WINENV);
136 string current_root()
142 bool isFilesystemCaseSensitive()
148 docstring::size_type common_path(docstring const & p1, docstring const & p2)
150 docstring::size_type i = 0;
151 docstring::size_type const p1_len = p1.length();
152 docstring::size_type const p2_len = p2.length();
153 while (i < p1_len && i < p2_len && uppercase(p1[i]) == uppercase(p2[i]))
155 if ((i < p1_len && i < p2_len)
156 || (i < p1_len && p1[i] != '/' && i == p2_len)
157 || (i < p2_len && p2[i] != '/' && i == p1_len))
160 --i; // here was the last match
161 while (i && p1[i] != '/')
168 string external_path(string const & p)
170 return convert_path(p, PathStyle(posix));
174 string internal_path(string const & p)
176 return convert_path(p, PathStyle(posix));
180 string external_path_list(string const & p)
182 return convert_path_list(p, PathStyle(posix));
186 string internal_path_list(string const & p)
188 return convert_path_list(p, PathStyle(posix));
192 string latex_path(string const & p)
194 // We may need a posix style path or a windows style path (depending
195 // on windows_style_tex_paths_), but we use always forward slashes,
196 // since it gets written into a .tex file.
198 if (windows_style_tex_paths_ && FileName(p).isAbsolute()) {
199 string dos_path = convert_path(p, PathStyle(windows));
200 LYXERR(Debug::LATEX, "<Path correction for LaTeX> ["
201 << p << "]->>[" << dos_path << ']');
205 return convert_path(p, PathStyle(posix));
209 // returns a string suitable to be passed to popen when
211 char const * popen_read_mode()
217 string const & nulldev()
219 static string const nulldev_ = "/dev/null";
230 char path_separator()
236 void windows_style_tex_paths(bool use_windows_paths)
238 windows_style_tex_paths_ = use_windows_paths;
242 bool canAutoOpenFile(string const & ext, auto_open_mode const mode)
247 string const full_ext = "." + ext;
249 DWORD bufSize = MAX_PATH + 100;
250 TCHAR buf[MAX_PATH + 100];
251 // reference: http://msdn.microsoft.com/en-us/library/bb773471.aspx
252 char const * action = (mode == VIEW) ? "open" : "edit";
253 return S_OK == AssocQueryString(ASSOCF_INIT_IGNOREUNKNOWN,
254 ASSOCSTR_EXECUTABLE, full_ext.c_str(), action, buf, &bufSize);
258 bool autoOpenFile(string const & filename, auto_open_mode const mode)
260 // reference: http://msdn.microsoft.com/en-us/library/bb762153.aspx
261 string const win_path = to_local8bit(from_utf8(convert_path(filename, PathStyle(windows))));
262 char const * action = (mode == VIEW) ? "open" : "edit";
263 return reinterpret_cast<int>(ShellExecute(NULL, action,
264 win_path.c_str(), NULL, NULL, 1)) > 32;
268 } // namespace support