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 // API definition for manually calling font functions on Windows 2000 and later
44 typedef int (WINAPI *FONTAPI)(LPCSTR, DWORD, PVOID);
45 #define FR_PRIVATE 0x10
47 // Names of TrueType fonts to load
48 string const win_fonts_truetype[] = {"cmex10", "cmmi10", "cmr10", "cmsy10",
49 "eufm10", "msam10", "msbm10", "wasy10", "esint10"};
50 const int num_fonts_truetype = sizeof(win_fonts_truetype) / sizeof(*win_fonts_truetype);
60 bool windows_style_tex_paths_ = false;
62 // In both is_posix_path() and is_windows_path() it is assumed that
63 // a valid posix or pseudo-windows path is passed. They simply tell
64 // whether the path looks posix/pseudo-windows or not.
66 bool is_posix_path(string const & p)
69 (!contains(p, '\\') && (p.length() <= 1 || p[1] != ':'));
72 // This is a test for a win32 style path with forward slashes (pseudo-windows).
74 bool is_windows_path(string const & p)
76 return p.empty() || (!contains(p, '\\') && p[0] != '/');
86 /// Convert a path to or from posix style.
87 /// \p p is encoded in local 8bit encoding or utf8.
88 /// The result is returned in the same encoding as \p p.
89 string convert_path(string const & p, PathStyle const & target)
91 char path_buf[PATH_MAX];
93 if ((target == posix && is_posix_path(p)) ||
94 (target == windows && is_windows_path(p)))
99 // cygwin_conv_to_posix_path and cygwin_conv_to_win32_path do not
100 // care about the encoding.
102 cygwin_conv_to_posix_path(p.c_str(), path_buf);
104 cygwin_conv_to_win32_path(p.c_str(), path_buf);
106 return subst(path_buf[0] ? path_buf : p, '\\', '/');
110 /// Convert a path list to or from posix style.
111 /// \p p is encoded in local 8bit encoding or utf8.
112 /// The result is returned in the same encoding as \p p.
113 string convert_path_list(string const & p, PathStyle const & target)
118 char const * const pc = p.c_str();
119 PathStyle const actual = cygwin_posix_path_list_p(pc) ? posix : windows;
121 if (target != actual) {
122 int const target_size = (target == posix) ?
123 cygwin_win32_to_posix_path_list_buf_size(pc) :
124 cygwin_posix_to_win32_path_list_buf_size(pc);
126 char * ptr = new char[target_size];
129 // FIXME: See comment in convert_path() above
131 cygwin_win32_to_posix_path_list(pc, ptr);
133 cygwin_posix_to_win32_path_list(pc, ptr);
135 string path_list = subst(ptr, '\\', '/');
141 return subst(p, '\\', '/');
146 void os::init(int, char *[])
148 // Copy cygwin environment variables to the Windows environment
149 // if they're not already there.
151 char **envp = environ;
155 bool temp_seen = false;
157 while (envp && *envp) {
158 val = split(*envp++, var, '=');
163 if (GetEnvironmentVariable(var.c_str(), curval, 2) == 0
164 && GetLastError() == ERROR_ENVVAR_NOT_FOUND) {
165 /* Convert to Windows style where necessary */
166 if (var == "PATH" || var == "LD_LIBRARY_PATH") {
167 string const winpathlist =
168 convert_path_list(val, PathStyle(windows));
169 if (!winpathlist.empty()) {
170 SetEnvironmentVariable(var.c_str(),
171 winpathlist.c_str());
173 } else if (var == "HOME" || var == "TMPDIR" ||
174 var == "TMP" || var == "TEMP") {
175 string const winpath =
176 convert_path(val, PathStyle(windows));
177 SetEnvironmentVariable(var.c_str(), winpath.c_str());
179 SetEnvironmentVariable(var.c_str(), val.c_str());
184 string const winpath = convert_path("/tmp", PathStyle(windows));
185 SetEnvironmentVariable("TEMP", winpath.c_str());
190 string current_root()
196 docstring::size_type common_path(docstring const & p1, docstring const & p2)
198 docstring::size_type i = 0;
199 docstring::size_type const p1_len = p1.length();
200 docstring::size_type const p2_len = p2.length();
201 while (i < p1_len && i < p2_len && uppercase(p1[i]) == uppercase(p2[i]))
203 if ((i < p1_len && i < p2_len)
204 || (i < p1_len && p1[i] != '/' && i == p2_len)
205 || (i < p2_len && p2[i] != '/' && i == p1_len))
208 --i; // here was the last match
209 while (i && p1[i] != '/')
216 string external_path(string const & p)
218 return convert_path(p, PathStyle(posix));
222 string internal_path(string const & p)
224 return convert_path(p, PathStyle(posix));
228 string external_path_list(string const & p)
230 return convert_path_list(p, PathStyle(posix));
234 string internal_path_list(string const & p)
236 return convert_path_list(p, PathStyle(posix));
240 string latex_path(string const & p)
242 // We may need a posix style path or a windows style path (depending
243 // on windows_style_tex_paths_), but we use always forward slashes,
244 // since it gets written into a .tex file.
246 if (windows_style_tex_paths_ && is_absolute_path(p)) {
247 string dos_path = convert_path(p, PathStyle(windows));
249 << "<Path correction for LaTeX> ["
251 << dos_path << ']' << endl;
255 return convert_path(p, PathStyle(posix));
259 bool is_absolute_path(string const & p)
264 bool isDosPath = (p.length() > 1 && p[1] == ':');
265 bool isUnixPath = (p[0] == '/');
267 return isDosPath || isUnixPath;
271 // returns a string suitable to be passed to popen when
273 char const * popen_read_mode()
279 string const & nulldev()
281 static string const nulldev_ = "/dev/null";
292 char path_separator()
298 void windows_style_tex_paths(bool use_windows_paths)
300 windows_style_tex_paths_ = use_windows_paths;
304 bool canAutoOpenFile(string const & ext, auto_open_mode const mode)
309 string const full_ext = "." + ext;
311 DWORD bufSize = MAX_PATH + 100;
312 TCHAR buf[MAX_PATH + 100];
313 // reference: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc
314 // /platform/shell/reference/shlwapi/registry/assocquerystring.asp
315 char const * action = (mode == VIEW) ? "open" : "edit";
316 return S_OK == AssocQueryString(0, ASSOCSTR_EXECUTABLE,
317 full_ext.c_str(), action, buf, &bufSize);
321 bool autoOpenFile(string const & filename, auto_open_mode const mode)
323 // reference: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc
324 // /platform/shell/reference/functions/shellexecute.asp
325 string const win_path = to_local8bit(from_utf8(convert_path(filename, PathStyle(windows))));
326 char const * action = (mode == VIEW) ? "open" : "edit";
327 return reinterpret_cast<int>(ShellExecute(NULL, action,
328 win_path.c_str(), NULL, NULL, 1)) > 32;
332 void addFontResources()
334 #ifdef X_DISPLAY_MISSING
335 // Windows only: Add BaKoMa TrueType font resources
336 string const fonts_dir = addPath(package().system_support().absFilename(), "fonts");
338 HMODULE hDLL = LoadLibrary("gdi32");
339 FONTAPI pAddFontResourceEx =
340 (FONTAPI) GetProcAddress(hDLL, "AddFontResourceExA");
342 for (int i = 0 ; i < num_fonts_truetype ; ++i) {
343 string const font_current = to_local8bit(from_utf8(convert_path(
344 addName(fonts_dir, win_fonts_truetype[i] + ".ttf"),
345 PathStyle(windows))));
346 if (pAddFontResourceEx) {
347 // Windows 2000 and later: Use AddFontResourceEx
348 pAddFontResourceEx(font_current.c_str(), FR_PRIVATE, 0);
350 // Older Windows versions: Use AddFontResource
351 AddFontResource(font_current.c_str());
359 void restoreFontResources()
361 #ifdef X_DISPLAY_MISSING
362 // Windows only: Remove BaKoMa TrueType font resources
363 string const fonts_dir = addPath(package().system_support().absFilename(), "fonts");
365 HMODULE hDLL = LoadLibrary("gdi32");
366 FONTAPI pRemoveFontResourceEx = (FONTAPI) GetProcAddress(hDLL, "RemoveFontResourceExA");
368 for(int i = 0 ; i < num_fonts_truetype ; ++i) {
369 string const font_current = to_local8bit(from_utf8(convert_path(
370 addName(fonts_dir, win_fonts_truetype[i] + ".ttf"),
371 PathStyle(windows))));
372 if (pRemoveFontResourceEx) {
373 // Windows 2000 and later: Use RemoveFontResourceEx
374 pRemoveFontResourceEx(font_current.c_str(), FR_PRIVATE, 0);
376 // Older Windows versions: Use RemoveFontResource
377 RemoveFontResource(font_current.c_str());
385 } // namespace support