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;
35 #ifdef X_DISPLAY_MISSING
36 #include "support/filetools.h"
37 #include "support/package.h"
38 #include "support/path.h"
39 using lyx::support::addName;
40 using lyx::support::addPath;
41 using lyx::support::package;
43 string const win_fonts_truetype[] = {"cmex10", "cmmi10", "cmr10", "cmsy10",
44 "eufm10", "msam10", "msbm10", "wasy10", "esint10"};
45 const int num_fonts_truetype = sizeof(win_fonts_truetype) / sizeof(*win_fonts_truetype);
55 bool windows_style_tex_paths_ = false;
57 // In both is_posix_path() and is_windows_path() it is assumed that
58 // a valid posix or pseudo-windows path is passed. They simply tell
59 // whether the path looks posix/pseudo-windows or not.
61 bool is_posix_path(string const & p)
64 (!contains(p, '\\') && (p.length() <= 1 || p[1] != ':'));
67 // This is a test for a win32 style path with forward slashes (pseudo-windows).
69 bool is_windows_path(string const & p)
71 return p.empty() || (!contains(p, '\\') && p[0] != '/');
81 string convert_path(string const & p, PathStyle const & target)
83 char path_buf[PATH_MAX];
85 if ((target == posix && is_posix_path(p)) ||
86 (target == windows && is_windows_path(p)))
92 cygwin_conv_to_posix_path(p.c_str(), path_buf);
94 cygwin_conv_to_win32_path(p.c_str(), path_buf);
96 return subst(path_buf[0] ? path_buf : p, '\\', '/');
100 string convert_path_list(string const & p, PathStyle const & target)
105 char const * const pc = p.c_str();
106 PathStyle const actual = cygwin_posix_path_list_p(pc) ? posix : windows;
108 if (target != actual) {
109 int const target_size = (target == posix) ?
110 cygwin_win32_to_posix_path_list_buf_size(pc) :
111 cygwin_posix_to_win32_path_list_buf_size(pc);
113 char * ptr = new char[target_size];
117 cygwin_win32_to_posix_path_list(pc, ptr);
119 cygwin_posix_to_win32_path_list(pc, ptr);
121 string path_list = subst(ptr, '\\', '/');
127 return subst(p, '\\', '/');
132 void os::init(int, char *[])
134 // Copy cygwin environment variables to the Windows environment
135 // if they're not already there.
137 char **envp = environ;
141 bool temp_seen = false;
143 while (envp && *envp) {
144 val = split(*envp++, var, '=');
149 if (GetEnvironmentVariable(var.c_str(), curval, 2) == 0
150 && GetLastError() == ERROR_ENVVAR_NOT_FOUND) {
151 /* Convert to Windows style where necessary */
152 if (var == "PATH" || var == "LD_LIBRARY_PATH") {
153 string const winpathlist =
154 convert_path_list(val, PathStyle(windows));
155 if (!winpathlist.empty()) {
156 SetEnvironmentVariable(var.c_str(),
157 winpathlist.c_str());
159 } else if (var == "HOME" || var == "TMPDIR" ||
160 var == "TMP" || var == "TEMP") {
161 string const winpath =
162 convert_path(val, PathStyle(windows));
163 SetEnvironmentVariable(var.c_str(), winpath.c_str());
165 SetEnvironmentVariable(var.c_str(), val.c_str());
170 string const winpath = convert_path("/tmp", PathStyle(windows));
171 SetEnvironmentVariable("TEMP", winpath.c_str());
176 string current_root()
182 string::size_type common_path(string const & p1, string const & p2)
184 string::size_type i = 0;
185 string::size_type p1_len = p1.length();
186 string::size_type p2_len = p2.length();
187 while (i < p1_len && i < p2_len && uppercase(p1[i]) == uppercase(p2[i]))
189 if ((i < p1_len && i < p2_len)
190 || (i < p1_len && p1[i] != '/' && i == p2_len)
191 || (i < p2_len && p2[i] != '/' && i == p1_len))
194 --i; // here was the last match
195 while (i && p1[i] != '/')
202 string external_path(string const & p)
204 return convert_path(p, PathStyle(posix));
208 string internal_path(string const & p)
210 return convert_path(p, PathStyle(posix));
214 string external_path_list(string const & p)
216 return convert_path_list(p, PathStyle(posix));
220 string internal_path_list(string const & p)
222 return convert_path_list(p, PathStyle(posix));
226 string latex_path(string const & p)
228 // We may need a posix style path or a windows style path (depending
229 // on windows_style_tex_paths_), but we use always forward slashes,
230 // since it gets written into a .tex file.
232 if (windows_style_tex_paths_ && is_absolute_path(p)) {
233 string dos_path = convert_path(p, PathStyle(windows));
235 << "<Path correction for LaTeX> ["
237 << dos_path << ']' << endl;
241 return convert_path(p, PathStyle(posix));
245 bool is_absolute_path(string const & p)
250 bool isDosPath = (p.length() > 1 && p[1] == ':');
251 bool isUnixPath = (p[0] == '/');
253 return isDosPath || isUnixPath;
257 // returns a string suitable to be passed to popen when
259 char const * popen_read_mode()
265 string const & nulldev()
267 static string const nulldev_ = "/dev/null";
278 char path_separator()
284 void windows_style_tex_paths(bool use_windows_paths)
286 windows_style_tex_paths_ = use_windows_paths;
290 bool canAutoOpenFile(string const & ext, auto_open_mode const mode)
295 string const full_ext = "." + ext;
297 DWORD bufSize = MAX_PATH + 100;
298 TCHAR buf[MAX_PATH + 100];
299 // reference: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc
300 // /platform/shell/reference/shlwapi/registry/assocquerystring.asp
301 char const * action = (mode == VIEW) ? "open" : "edit";
302 return S_OK == AssocQueryString(0, ASSOCSTR_EXECUTABLE,
303 full_ext.c_str(), action, buf, &bufSize);
307 bool autoOpenFile(string const & filename, auto_open_mode const mode)
309 // reference: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc
310 // /platform/shell/reference/functions/shellexecute.asp
311 string const win_path = convert_path(filename, PathStyle(windows));
312 char const * action = (mode == VIEW) ? "open" : "edit";
313 return reinterpret_cast<int>(ShellExecute(NULL, action,
314 win_path.c_str(), NULL, NULL, 1)) > 32;
318 void addFontResources()
320 #ifdef X_DISPLAY_MISSING
321 // Windows only: Add BaKoMa TrueType font resources
322 string const fonts_dir = addPath(package().system_support(), "fonts");
324 for (int i = 0 ; i < num_fonts_truetype ; ++i) {
325 string const font_current = convert_path(
326 addName(fonts_dir, win_fonts_truetype[i] + ".ttf"),
328 AddFontResource(font_current.c_str());
334 void restoreFontResources()
336 #ifdef X_DISPLAY_MISSING
337 // Windows only: Remove BaKoMa TrueType font resources
338 string const fonts_dir = addPath(package().system_support(), "fonts");
340 for(int i = 0 ; i < num_fonts_truetype ; ++i) {
341 string const font_current = convert_path(
342 addName(fonts_dir, win_fonts_truetype[i] + ".ttf"),
344 RemoveFontResource(font_current.c_str());
350 } // namespace support