]> git.lyx.org Git - features.git/blob - src/support/os_cygwin.cpp
Use the internal function of the cygwin dll for syncing the
[features.git] / src / support / os_cygwin.cpp
1 /**
2  * \file os_cygwin.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Ruurd A. Reitsma
7  * \author Claus Hentschel
8  * \author Angus Leeming
9  *
10  * Full author contact details are available in file CREDITS.
11  *
12  * Various OS specific functions
13  */
14
15 #include <config.h>
16
17 #include "support/os.h"
18 #include "support/lstrings.h"
19
20 #include "debug.h"
21
22 #include <windows.h>
23 #include <io.h>
24 #include <windef.h>
25 #include <shellapi.h>
26 #include <shlwapi.h>
27
28 #include <sys/cygwin.h>
29
30 using std::endl;
31 using std::string;
32
33 using lyx::support::contains;
34
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;
42
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
46
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);
51 #endif
52
53
54 namespace lyx {
55 namespace support {
56 namespace os {
57
58 namespace {
59
60 bool windows_style_tex_paths_ = false;
61
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.
65
66 bool is_posix_path(string const & p)
67 {
68         return  p.empty() ||
69                 (!contains(p, '\\') && (p.length() <= 1 || p[1] != ':'));
70 }
71
72 // This is a test for a win32 style path with forward slashes (pseudo-windows).
73
74 bool is_windows_path(string const & p)
75 {
76         return p.empty() || (!contains(p, '\\') && p[0] != '/');
77 }
78
79
80 enum PathStyle {
81         posix,
82         windows
83 };
84
85
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)
90 {
91         char path_buf[PATH_MAX];
92
93         if ((target == posix && is_posix_path(p)) ||
94             (target == windows && is_windows_path(p)))
95                 return p;
96
97         path_buf[0] = '\0';
98
99         // cygwin_conv_to_posix_path and cygwin_conv_to_win32_path do not
100         // care about the encoding.
101         if (target == posix)
102                 cygwin_conv_to_posix_path(p.c_str(), path_buf);
103         else
104                 cygwin_conv_to_win32_path(p.c_str(), path_buf);
105
106         return subst(path_buf[0] ? path_buf : p, '\\', '/');
107 }
108
109
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)
114 {
115         if (p.empty())
116                 return p;
117
118         char const * const pc = p.c_str();
119         PathStyle const actual = cygwin_posix_path_list_p(pc) ? posix : windows;
120
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);
125
126                 char * ptr = new char[target_size];
127
128                 if (ptr) {
129                         // FIXME: See comment in convert_path() above
130                         if (target == posix)
131                                 cygwin_win32_to_posix_path_list(pc, ptr);
132                         else
133                                 cygwin_posix_to_win32_path_list(pc, ptr);
134
135                         string path_list = subst(ptr, '\\', '/');
136                         delete ptr;
137                         return path_list;
138                 }
139         }
140
141         return subst(p, '\\', '/');
142 }
143
144 } // namespace anon
145
146 void os::init(int, char *[])
147 {
148         // Make sure that the TEMP variable is set
149         // and sync the Windows environment.
150
151         char **envp = environ;
152         string var;
153         bool temp_seen = false;
154
155         while (envp && *envp && !temp_seen) {
156                 split(*envp++, var, '=');
157                 if (var == "TEMP")
158                         temp_seen = true;
159         }
160         if (!temp_seen)
161                 ::setenv("TEMP", "/tmp", true);
162
163         cygwin_internal(CW_SYNC_WINENV);
164 }
165
166
167 string current_root()
168 {
169         return string("/");
170 }
171
172
173 docstring::size_type common_path(docstring const & p1, docstring const & p2)
174 {
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]))
179                 ++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))
183         {
184                 if (i)
185                         --i;     // here was the last match
186                 while (i && p1[i] != '/')
187                         --i;
188         }
189         return i;
190 }
191
192
193 string external_path(string const & p)
194 {
195         return convert_path(p, PathStyle(posix));
196 }
197
198
199 string internal_path(string const & p)
200 {
201         return convert_path(p, PathStyle(posix));
202 }
203
204
205 string external_path_list(string const & p)
206 {
207         return convert_path_list(p, PathStyle(posix));
208 }
209
210
211 string internal_path_list(string const & p)
212 {
213         return convert_path_list(p, PathStyle(posix));
214 }
215
216
217 string latex_path(string const & p)
218 {
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.
222
223         if (windows_style_tex_paths_ && is_absolute_path(p)) {
224                 string dos_path = convert_path(p, PathStyle(windows));
225                 LYXERR(Debug::LATEX)
226                         << "<Path correction for LaTeX> ["
227                         << p << "]->>["
228                         << dos_path << ']' << endl;
229                 return dos_path;
230         }
231
232         return convert_path(p, PathStyle(posix));
233 }
234
235
236 bool is_absolute_path(string const & p)
237 {
238         if (p.empty())
239                 return false;
240
241         bool isDosPath = (p.length() > 1 && p[1] == ':');
242         bool isUnixPath = (p[0] == '/');
243
244         return isDosPath || isUnixPath;
245 }
246
247
248 // returns a string suitable to be passed to popen when
249 // reading a pipe
250 char const * popen_read_mode()
251 {
252         return "r";
253 }
254
255
256 string const & nulldev()
257 {
258         static string const nulldev_ = "/dev/null";
259         return nulldev_;
260 }
261
262
263 shell_type shell()
264 {
265         return UNIX;
266 }
267
268
269 char path_separator()
270 {
271         return ':';
272 }
273
274
275 void windows_style_tex_paths(bool use_windows_paths)
276 {
277         windows_style_tex_paths_ = use_windows_paths;
278 }
279
280
281 bool canAutoOpenFile(string const & ext, auto_open_mode const mode)
282 {
283         if (ext.empty())
284                 return false;
285
286         string const full_ext = "." + ext;
287
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);
295 }
296
297
298 bool autoOpenFile(string const & filename, auto_open_mode const mode)
299 {
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;
306 }
307
308
309 void addFontResources()
310 {
311 #ifdef X_DISPLAY_MISSING
312         // Windows only: Add BaKoMa TrueType font resources
313         string const fonts_dir = addPath(package().system_support().absFilename(), "fonts");
314
315         HMODULE hDLL = LoadLibrary("gdi32");
316         FONTAPI pAddFontResourceEx =
317                 (FONTAPI) GetProcAddress(hDLL, "AddFontResourceExA");
318
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);
326                 } else {
327                         // Older Windows versions: Use AddFontResource
328                         AddFontResource(font_current.c_str());
329                 }
330         }
331         FreeLibrary(hDLL);
332 #endif
333 }
334
335
336 void restoreFontResources()
337 {
338 #ifdef X_DISPLAY_MISSING
339         // Windows only: Remove BaKoMa TrueType font resources
340         string const fonts_dir = addPath(package().system_support().absFilename(), "fonts");
341
342         HMODULE hDLL = LoadLibrary("gdi32");
343         FONTAPI pRemoveFontResourceEx = (FONTAPI) GetProcAddress(hDLL, "RemoveFontResourceExA");
344
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);
352                 } else {
353                         // Older Windows versions: Use RemoveFontResource
354                         RemoveFontResource(font_current.c_str());
355                 }
356         }
357         FreeLibrary(hDLL);
358 #endif
359 }
360
361 } // namespace os
362 } // namespace support
363 } // namespace lyx