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 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 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)))
79 cygwin_conv_to_posix_path(p.c_str(), path_buf);
81 cygwin_conv_to_win32_path(p.c_str(), path_buf);
83 return subst(path_buf[0] ? path_buf : p, '\\', '/');
87 string convert_path_list(string const & p, PathStyle const & target)
92 char const * const pc = p.c_str();
93 PathStyle const actual = cygwin_posix_path_list_p(pc) ? posix : windows;
95 if (target != actual) {
96 int const target_size = (target == posix) ?
97 cygwin_win32_to_posix_path_list_buf_size(pc) :
98 cygwin_posix_to_win32_path_list_buf_size(pc);
100 char * ptr = new char[target_size];
104 cygwin_win32_to_posix_path_list(pc, ptr);
106 cygwin_posix_to_win32_path_list(pc, ptr);
108 string path_list = subst(ptr, '\\', '/');
114 return subst(p, '\\', '/');
119 void os::init(int, char *[])
121 // Copy cygwin environment variables to the Windows environment
122 // if they're not already there.
124 char **envp = environ;
128 bool temp_seen = false;
130 while (envp && *envp) {
131 val = split(*envp++, var, '=');
136 if (GetEnvironmentVariable(var.c_str(), curval, 2) == 0
137 && GetLastError() == ERROR_ENVVAR_NOT_FOUND) {
138 /* Convert to Windows style where necessary */
139 if (var == "PATH" || var == "LD_LIBRARY_PATH") {
140 string const winpathlist =
141 convert_path_list(val, PathStyle(windows));
142 if (!winpathlist.empty()) {
143 SetEnvironmentVariable(var.c_str(),
144 winpathlist.c_str());
146 } else if (var == "HOME" || var == "TMPDIR" ||
147 var == "TMP" || var == "TEMP") {
148 string const winpath =
149 convert_path(val, PathStyle(windows));
150 SetEnvironmentVariable(var.c_str(), winpath.c_str());
152 SetEnvironmentVariable(var.c_str(), val.c_str());
157 string const winpath = convert_path("/tmp", PathStyle(windows));
158 SetEnvironmentVariable("TEMP", winpath.c_str());
163 string current_root()
169 string::size_type common_path(string const & p1, string const & p2)
171 string::size_type i = 0;
172 string::size_type p1_len = p1.length();
173 string::size_type p2_len = p2.length();
174 while (i < p1_len && i < p2_len && uppercase(p1[i]) == uppercase(p2[i]))
176 if ((i < p1_len && i < p2_len)
177 || (i < p1_len && p1[i] != '/' && i == p2_len)
178 || (i < p2_len && p2[i] != '/' && i == p1_len))
181 --i; // here was the last match
182 while (i && p1[i] != '/')
189 string external_path(string const & p)
191 #ifdef X_DISPLAY_MISSING
192 return convert_path(p, PathStyle(windows));
194 return convert_path(p, PathStyle(posix));
199 string internal_path(string const & p)
201 return convert_path(p, PathStyle(posix));
205 string external_path_list(string const & p)
207 return convert_path_list(p, PathStyle(windows));
211 string internal_path_list(string const & p)
213 return convert_path_list(p, PathStyle(posix));
217 string latex_path(string const & p)
219 // We may need a posix style path or a windows style path (depending
220 // on windows_style_tex_paths_), but we use always forward slashes,
221 // since it gets written into a .tex file.
223 if (windows_style_tex_paths_ && is_absolute_path(p)) {
224 string dos_path = convert_path(p, PathStyle(windows));
226 << "<Path correction for LaTeX> ["
228 << dos_path << ']' << endl;
232 return convert_path(p, PathStyle(posix));
236 bool is_absolute_path(string const & p)
241 bool isDosPath = (p.length() > 1 && p[1] == ':');
242 bool isUnixPath = (p[0] == '/');
244 return isDosPath || isUnixPath;
248 // returns a string suitable to be passed to popen when
250 char const * popen_read_mode()
256 string const & nulldev()
258 static string const nulldev_ = "/dev/null";
269 char path_separator()
275 void windows_style_tex_paths(bool use_windows_paths)
277 windows_style_tex_paths_ = use_windows_paths;
281 bool canAutoOpenFile(string const & ext, auto_open_mode const mode)
286 string const full_ext = "." + ext;
288 DWORD bufSize = MAX_PATH + 100;
289 TCHAR buf[MAX_PATH + 100];
290 // reference: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc
291 // /platform/shell/reference/shlwapi/registry/assocquerystring.asp
292 char const * action = (mode == VIEW) ? "open" : "edit";
293 return S_OK == AssocQueryString(0, ASSOCSTR_EXECUTABLE,
294 full_ext.c_str(), action, buf, &bufSize);
298 bool autoOpenFile(string const & filename, auto_open_mode const mode)
300 // reference: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc
301 // /platform/shell/reference/functions/shellexecute.asp
302 string const win_path = convert_path(filename, PathStyle(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