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 // Make sure that the TEMP variable is set
149 // and sync the Windows environment.
151 char **envp = environ;
153 bool temp_seen = false;
155 while (envp && *envp && !temp_seen) {
156 split(*envp++, var, '=');
161 ::setenv("TEMP", "/tmp", true);
163 cygwin_internal(CW_SYNC_WINENV);
167 string current_root()
173 docstring::size_type common_path(docstring const & p1, docstring const & p2)
175 docstring::size_type i = 0;
176 docstring::size_type const p1_len = p1.length();
177 docstring::size_type const p2_len = p2.length();
178 while (i < p1_len && i < p2_len && uppercase(p1[i]) == uppercase(p2[i]))
180 if ((i < p1_len && i < p2_len)
181 || (i < p1_len && p1[i] != '/' && i == p2_len)
182 || (i < p2_len && p2[i] != '/' && i == p1_len))
185 --i; // here was the last match
186 while (i && p1[i] != '/')
193 string external_path(string const & p)
195 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(posix));
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 = to_local8bit(from_utf8(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;
309 void addFontResources()
311 #ifdef X_DISPLAY_MISSING
312 // Windows only: Add BaKoMa TrueType font resources
313 string const fonts_dir = addPath(package().system_support().absFilename(), "fonts");
315 HMODULE hDLL = LoadLibrary("gdi32");
316 FONTAPI pAddFontResourceEx =
317 (FONTAPI) GetProcAddress(hDLL, "AddFontResourceExA");
319 for (int i = 0 ; i < num_fonts_truetype ; ++i) {
320 string const font_current = to_local8bit(from_utf8(convert_path(
321 addName(fonts_dir, win_fonts_truetype[i] + ".ttf"),
322 PathStyle(windows))));
323 if (pAddFontResourceEx) {
324 // Windows 2000 and later: Use AddFontResourceEx
325 pAddFontResourceEx(font_current.c_str(), FR_PRIVATE, 0);
327 // Older Windows versions: Use AddFontResource
328 AddFontResource(font_current.c_str());
336 void restoreFontResources()
338 #ifdef X_DISPLAY_MISSING
339 // Windows only: Remove BaKoMa TrueType font resources
340 string const fonts_dir = addPath(package().system_support().absFilename(), "fonts");
342 HMODULE hDLL = LoadLibrary("gdi32");
343 FONTAPI pRemoveFontResourceEx = (FONTAPI) GetProcAddress(hDLL, "RemoveFontResourceExA");
345 for(int i = 0 ; i < num_fonts_truetype ; ++i) {
346 string const font_current = to_local8bit(from_utf8(convert_path(
347 addName(fonts_dir, win_fonts_truetype[i] + ".ttf"),
348 PathStyle(windows))));
349 if (pRemoveFontResourceEx) {
350 // Windows 2000 and later: Use RemoveFontResourceEx
351 pRemoveFontResourceEx(font_current.c_str(), FR_PRIVATE, 0);
353 // Older Windows versions: Use RemoveFontResource
354 RemoveFontResource(font_current.c_str());
362 } // namespace support