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"
30 #include <sys/cygwin.h>
41 bool windows_style_tex_paths_ = false;
43 // In both is_posix_path() and is_windows_path() it is assumed that
44 // a valid posix or pseudo-windows path is passed. They simply tell
45 // whether the path looks posix/pseudo-windows or not.
47 bool is_posix_path(string const & p)
50 (!contains(p, '\\') && (p.length() <= 1 || p[1] != ':'));
53 // This is a test for a win32 style path with forward slashes (pseudo-windows).
55 bool is_windows_path(string const & p)
57 return p.empty() || (!contains(p, '\\') && p[0] != '/');
67 /// Convert a path to or from posix style.
68 /// \p p is encoded in local 8bit encoding or utf8.
69 /// The result is returned in the same encoding as \p p.
70 string convert_path(string const & p, PathStyle const & target)
72 char path_buf[PATH_MAX];
74 if ((target == posix && is_posix_path(p)) ||
75 (target == windows && is_windows_path(p)))
80 // cygwin_conv_to_posix_path and cygwin_conv_to_win32_path do not
81 // care about the encoding.
83 cygwin_conv_to_posix_path(p.c_str(), path_buf);
85 cygwin_conv_to_win32_path(p.c_str(), path_buf);
87 return subst(path_buf[0] ? path_buf : p, '\\', '/');
91 /// Convert a path list to or from posix style.
92 /// \p p is encoded in local 8bit encoding or utf8.
93 /// The result is returned in the same encoding as \p p.
94 string convert_path_list(string const & p, PathStyle const & target)
99 char const * const pc = p.c_str();
100 PathStyle const actual = cygwin_posix_path_list_p(pc) ? posix : windows;
102 if (target != actual) {
103 int const target_size = (target == posix) ?
104 cygwin_win32_to_posix_path_list_buf_size(pc) :
105 cygwin_posix_to_win32_path_list_buf_size(pc);
107 char * ptr = new char[target_size];
110 // FIXME: See comment in convert_path() above
112 cygwin_win32_to_posix_path_list(pc, ptr);
114 cygwin_posix_to_win32_path_list(pc, ptr);
116 string path_list = subst(ptr, '\\', '/');
122 return subst(p, '\\', '/');
127 void init(int, char *[])
129 // Make sure that the TEMP variable is set
130 // and sync the Windows environment.
132 setenv("TEMP", "/tmp", false);
133 cygwin_internal(CW_SYNC_WINENV);
137 string current_root()
143 bool isFilesystemCaseSensitive()
149 docstring::size_type common_path(docstring const & p1, docstring const & p2)
151 docstring::size_type i = 0;
152 docstring::size_type const p1_len = p1.length();
153 docstring::size_type const p2_len = p2.length();
154 while (i < p1_len && i < p2_len && uppercase(p1[i]) == uppercase(p2[i]))
156 if ((i < p1_len && i < p2_len)
157 || (i < p1_len && p1[i] != '/' && i == p2_len)
158 || (i < p2_len && p2[i] != '/' && i == p1_len))
161 --i; // here was the last match
162 while (i && p1[i] != '/')
169 string external_path(string const & p)
171 return convert_path(p, PathStyle(posix));
175 string internal_path(string const & p)
177 return convert_path(p, PathStyle(posix));
181 string external_path_list(string const & p)
183 return convert_path_list(p, PathStyle(posix));
187 string internal_path_list(string const & p)
189 return convert_path_list(p, PathStyle(posix));
193 string latex_path(string const & p)
195 // We may need a posix style path or a windows style path (depending
196 // on windows_style_tex_paths_), but we use always forward slashes,
197 // since it gets written into a .tex file.
199 if (windows_style_tex_paths_ && FileName(p).isAbsolute()) {
200 string dos_path = convert_path(p, PathStyle(windows));
201 LYXERR(Debug::LATEX, "<Path correction for LaTeX> ["
202 << p << "]->>[" << dos_path << ']');
206 return convert_path(p, PathStyle(posix));
210 bool is_valid_strftime(string const & p)
212 string::size_type pos = p.find_first_of('%');
213 while (pos != string::npos) {
214 if (pos + 1 == string::npos)
216 if (!containsOnly(p.substr(pos + 1, 1),
217 "aAbBcCdDeEFgGhHIjklmMnOpPrRsStTuUVwWxXyYzZ%+"))
219 if (pos + 2 == string::npos)
221 pos = p.find_first_of('%', pos + 2);
227 // returns a string suitable to be passed to popen when
229 char const * popen_read_mode()
235 string const & nulldev()
237 static string const nulldev_ = "/dev/null";
248 char path_separator()
254 void windows_style_tex_paths(bool use_windows_paths)
256 windows_style_tex_paths_ = use_windows_paths;
260 bool canAutoOpenFile(string const & ext, auto_open_mode const mode)
265 string const full_ext = "." + ext;
267 DWORD bufSize = MAX_PATH + 100;
268 TCHAR buf[MAX_PATH + 100];
269 // reference: http://msdn.microsoft.com/en-us/library/bb773471.aspx
270 char const * action = (mode == VIEW) ? "open" : "edit";
271 return S_OK == AssocQueryString(ASSOCF_INIT_IGNOREUNKNOWN,
272 ASSOCSTR_EXECUTABLE, full_ext.c_str(), action, buf, &bufSize);
276 bool autoOpenFile(string const & filename, auto_open_mode const mode)
278 // reference: http://msdn.microsoft.com/en-us/library/bb762153.aspx
279 string const win_path = to_local8bit(from_utf8(convert_path(filename, PathStyle(windows))));
280 char const * action = (mode == VIEW) ? "open" : "edit";
281 return reinterpret_cast<int>(ShellExecute(NULL, action,
282 win_path.c_str(), NULL, NULL, 1)) > 32;
286 bool isSameFile(string const & fileone, string const & filetwo)
291 if (::stat(fileone.c_str(), &st1) == 0
292 && ::stat(filetwo.c_str(), &st2) == 0) {
293 return st1.st_ino == st2.st_ino && st1.st_dev == st2.st_dev;
296 // One or both files cannot be accessed.
301 } // namespace support