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"
18 #include "support/lstrings.h"
28 #include <sys/cygwin.h>
33 using lyx::support::contains;
42 bool cygwin_path_fix_ = 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)
59 (!contains(p, '\\') && (p.length() <= 1 || p[1] == ':'));
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)))
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 string convert_path_list(string const & p, PathStyle const & target)
90 char const * const pc = p.c_str();
91 PathStyle const actual = cygwin_posix_path_list_p(pc) ? posix : windows;
93 if (target != actual) {
94 int const target_size = (target == posix) ?
95 cygwin_win32_to_posix_path_list_buf_size(pc) :
96 cygwin_posix_to_win32_path_list_buf_size(pc);
98 char * ptr = new char[target_size];
102 cygwin_win32_to_posix_path_list(pc, ptr);
104 cygwin_posix_to_win32_path_list(pc, ptr);
106 string path_list = subst(ptr, '\\', '/');
112 return subst(p, '\\', '/');
117 void os::init(int, char *[])
119 // Copy cygwin environment variables to the Windows environment
120 // if they're not already there.
122 char **envp = environ;
126 bool temp_seen = false;
128 while (envp && *envp) {
129 val = split(*envp++, var, '=');
134 if (GetEnvironmentVariable(var.c_str(), curval, 2) == 0
135 && GetLastError() == ERROR_ENVVAR_NOT_FOUND) {
136 /* Convert to Windows style where necessary */
137 if (var == "PATH" || var == "LD_LIBRARY_PATH") {
138 string const winpathlist =
139 convert_path_list(val, PathStyle(windows));
140 if (!winpathlist.empty()) {
141 SetEnvironmentVariable(var.c_str(),
142 winpathlist.c_str());
144 } else if (var == "HOME" || var == "TMPDIR" ||
145 var == "TMP" || var == "TEMP") {
146 string const winpath =
147 convert_path(val, PathStyle(windows));
148 SetEnvironmentVariable(var.c_str(), winpath.c_str());
150 SetEnvironmentVariable(var.c_str(), val.c_str());
155 string const winpath = convert_path("/tmp", PathStyle(windows));
156 SetEnvironmentVariable("TEMP", winpath.c_str());
161 string current_root()
167 string::size_type common_path(string const & p1, string const & p2)
169 string::size_type i = 0;
170 string::size_type p1_len = p1.length();
171 string::size_type p2_len = p2.length();
172 while (i < p1_len && i < p2_len && uppercase(p1[i]) == uppercase(p2[i]))
174 if ((i < p1_len && i < p2_len)
175 || (i < p1_len && p1[i] != '/' && i == p2_len)
176 || (i < p2_len && p2[i] != '/' && i == p1_len))
179 --i; // here was the last match
180 while (i && p1[i] != '/')
187 string external_path(string const & p)
189 return convert_path(p, cygwin_path_fix_ ? PathStyle(windows)
194 string internal_path(string const & p)
196 return convert_path(p, PathStyle(posix));
200 string external_path_list(string const & p)
202 return convert_path_list(p, cygwin_path_fix_ ? PathStyle(windows)
207 string internal_path_list(string const & p)
209 return convert_path_list(p, PathStyle(posix));
213 string latex_path(string const & p)
215 // We may need a posix style path or a windows style path (depending
216 // on cygwin_path_fix_), but we use always forward slashes, since it
217 // gets written into a .tex file.
219 if (cygwin_path_fix_ && is_absolute_path(p)) {
220 string dos_path = convert_path(p, PathStyle(windows));
222 << "<Cygwin path correction> ["
224 << dos_path << ']' << endl;
228 return convert_path(p, PathStyle(posix));
232 bool is_absolute_path(string const & p)
237 bool isDosPath = (p.length() > 1 && p[1] == ':');
238 bool isUnixPath = (p[0] == '/');
240 return isDosPath || isUnixPath;
244 // returns a string suitable to be passed to popen when
246 char const * popen_read_mode()
252 string const & nulldev()
254 static string const nulldev_ = "/dev/null";
265 char path_separator()
271 void cygwin_path_fix(bool use_cygwin_paths)
273 cygwin_path_fix_ = use_cygwin_paths;
277 bool canAutoOpenFile(string const & ext, auto_open_mode const mode)
282 string full_ext = ext;
283 // if the extension is passed without leading dot
284 if (full_ext[0] != '.')
285 full_ext = "." + ext;
287 DWORD bufSize = MAX_PATH + 100;
288 TCHAR buf[MAX_PATH + 100];
289 // reference: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc
290 // /platform/shell/reference/shlwapi/registry/assocquerystring.asp
291 char const * action = (mode == VIEW) ? "open" : "edit";
292 return S_OK == AssocQueryString(0, ASSOCSTR_EXECUTABLE,
293 full_ext.c_str(), action, buf, &bufSize);
297 bool autoOpenFile(string const & filename, auto_open_mode const mode)
299 // reference: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc
300 // /platform/shell/reference/functions/shellexecute.asp
301 string const win_path =
302 os::convert_path(filename, os::PathStyle(os::windows));
303 char const * action = (mode == VIEW) ? "open" : "edit";
304 return reinterpret_cast<int>(ShellExecute(NULL, action,
305 win_path.c_str(), NULL, NULL, 1)) > 32;
310 } // namespace support