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 <sys/cygwin.h>
42 bool windows_style_tex_paths_ = false;
44 // In both is_posix_path() and is_windows_path() it is assumed that
45 // a valid posix or pseudo-windows path is passed. They simply tell
46 // whether the path looks posix/pseudo-windows or not.
48 bool is_posix_path(string const & p)
51 (!contains(p, '\\') && (p.length() <= 1 || p[1] != ':'));
54 // This is a test for a win32 style path with forward slashes (pseudo-windows).
56 bool is_windows_path(string const & p)
58 return p.empty() || (!contains(p, '\\') && p[0] != '/');
68 /// Convert a path to or from posix style.
69 /// \p p is encoded in local 8bit encoding or utf8.
70 /// The result is returned in the same encoding as \p p.
71 string convert_path(string const & p, PathStyle const & target)
73 char path_buf[PATH_MAX];
75 if ((target == posix && is_posix_path(p)) ||
76 (target == windows && is_windows_path(p)))
81 // cygwin_conv_to_posix_path and cygwin_conv_to_win32_path do not
82 // care about the encoding.
84 cygwin_conv_to_posix_path(p.c_str(), path_buf);
86 cygwin_conv_to_win32_path(p.c_str(), path_buf);
88 return subst(path_buf[0] ? path_buf : p, '\\', '/');
92 /// Convert a path list to or from posix style.
93 /// \p p is encoded in local 8bit encoding or utf8.
94 /// The result is returned in the same encoding as \p p.
95 string convert_path_list(string const & p, PathStyle const & target)
100 char const * const pc = p.c_str();
101 PathStyle const actual = cygwin_posix_path_list_p(pc) ? posix : windows;
103 if (target != actual) {
104 int const target_size = (target == posix) ?
105 cygwin_win32_to_posix_path_list_buf_size(pc) :
106 cygwin_posix_to_win32_path_list_buf_size(pc);
108 char * ptr = new char[target_size];
111 // FIXME: See comment in convert_path() above
113 cygwin_win32_to_posix_path_list(pc, ptr);
115 cygwin_posix_to_win32_path_list(pc, ptr);
117 string path_list = subst(ptr, '\\', '/');
123 return subst(p, '\\', '/');
128 void init(int, char *[])
130 // Make sure that the TEMP variable is set
131 // and sync the Windows environment.
133 setenv("TEMP", "/tmp", false);
134 cygwin_internal(CW_SYNC_WINENV);
138 string current_root()
144 bool isFilesystemCaseSensitive()
150 docstring::size_type common_path(docstring const & p1, docstring const & p2)
152 docstring::size_type i = 0;
153 docstring::size_type const p1_len = p1.length();
154 docstring::size_type const p2_len = p2.length();
155 while (i < p1_len && i < p2_len && uppercase(p1[i]) == uppercase(p2[i]))
157 if ((i < p1_len && i < p2_len)
158 || (i < p1_len && p1[i] != '/' && i == p2_len)
159 || (i < p2_len && p2[i] != '/' && i == p1_len))
162 --i; // here was the last match
163 while (i && p1[i] != '/')
170 string external_path(string const & p)
172 return convert_path(p, PathStyle(posix));
176 string internal_path(string const & p)
178 return convert_path(p, PathStyle(posix));
182 string external_path_list(string const & p)
184 return convert_path_list(p, PathStyle(posix));
188 string internal_path_list(string const & p)
190 return convert_path_list(p, PathStyle(posix));
194 string latex_path(string const & p)
196 // We may need a posix style path or a windows style path (depending
197 // on windows_style_tex_paths_), but we use always forward slashes,
198 // since it gets written into a .tex file.
200 if (windows_style_tex_paths_ && FileName::isAbsolute(p)) {
201 string dos_path = convert_path(p, PathStyle(windows));
202 LYXERR(Debug::LATEX, "<Path correction for LaTeX> ["
203 << p << "]->>[" << dos_path << ']');
207 return convert_path(p, PathStyle(posix));
211 bool is_valid_strftime(string const & p)
213 string::size_type pos = p.find_first_of('%');
214 while (pos != string::npos) {
215 if (pos + 1 == string::npos)
217 if (!containsOnly(p.substr(pos + 1, 1),
218 "aAbBcCdDeEFgGhHIjklmMnOpPrRsStTuUVwWxXyYzZ%+"))
220 if (pos + 2 == string::npos)
222 pos = p.find_first_of('%', pos + 2);
228 // returns a string suitable to be passed to popen when
230 char const * popen_read_mode()
236 string const & nulldev()
238 static string const nulldev_ = "/dev/null";
249 char path_separator()
255 void windows_style_tex_paths(bool use_windows_paths)
257 windows_style_tex_paths_ = use_windows_paths;
261 bool canAutoOpenFile(string const & ext, auto_open_mode const mode)
266 string const full_ext = "." + ext;
268 DWORD bufSize = MAX_PATH + 100;
269 TCHAR buf[MAX_PATH + 100];
270 // reference: http://msdn.microsoft.com/en-us/library/bb773471.aspx
271 char const * action = (mode == VIEW) ? "open" : "edit";
272 return S_OK == AssocQueryString(ASSOCF_INIT_IGNOREUNKNOWN,
273 ASSOCSTR_EXECUTABLE, full_ext.c_str(), action, buf, &bufSize);
277 bool autoOpenFile(string const & filename, auto_open_mode const mode)
279 // reference: http://msdn.microsoft.com/en-us/library/bb762153.aspx
280 string const win_path = to_local8bit(from_utf8(convert_path(filename, PathStyle(windows))));
281 char const * action = (mode == VIEW) ? "open" : "edit";
282 return reinterpret_cast<int>(ShellExecute(NULL, action,
283 win_path.c_str(), NULL, NULL, 1)) > 32;
287 string real_path(string const & path)
289 char rpath[PATH_MAX + 1];
290 char * result = realpath(path.c_str(), rpath);
291 return FileName::fromFilesystemEncoding(result ? rpath : path).absFilename();
295 } // namespace support