]> git.lyx.org Git - lyx.git/blob - src/support/os_cygwin.cpp
5ec1d1e3d2591e3ffa0950c2e92cb80c73299603
[lyx.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         // Copy cygwin environment variables to the Windows environment
149         // if they're not already there.
150
151         char **envp = environ;
152         char curval[2];
153         string var;
154         string val;
155         bool temp_seen = false;
156
157         while (envp && *envp) {
158                 val = split(*envp++, var, '=');
159
160                 if (var == "TEMP")
161                         temp_seen = true;
162
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());
172                                 }
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());
178                         } else {
179                                 SetEnvironmentVariable(var.c_str(), val.c_str());
180                         }
181                 }
182         }
183         if (!temp_seen) {
184                 string const winpath = convert_path("/tmp", PathStyle(windows));
185                 SetEnvironmentVariable("TEMP", winpath.c_str());
186         }
187 }
188
189
190 string current_root()
191 {
192         return string("/");
193 }
194
195
196 docstring::size_type common_path(docstring const & p1, docstring const & p2)
197 {
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]))
202                 ++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))
206         {
207                 if (i)
208                         --i;     // here was the last match
209                 while (i && p1[i] != '/')
210                         --i;
211         }
212         return i;
213 }
214
215
216 string external_path(string const & p)
217 {
218         return convert_path(p, PathStyle(posix));
219 }
220
221
222 string internal_path(string const & p)
223 {
224         return convert_path(p, PathStyle(posix));
225 }
226
227
228 string external_path_list(string const & p)
229 {
230         return convert_path_list(p, PathStyle(posix));
231 }
232
233
234 string internal_path_list(string const & p)
235 {
236         return convert_path_list(p, PathStyle(posix));
237 }
238
239
240 string latex_path(string const & p)
241 {
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.
245
246         if (windows_style_tex_paths_ && is_absolute_path(p)) {
247                 string dos_path = convert_path(p, PathStyle(windows));
248                 LYXERR(Debug::LATEX)
249                         << "<Path correction for LaTeX> ["
250                         << p << "]->>["
251                         << dos_path << ']' << endl;
252                 return dos_path;
253         }
254
255         return convert_path(p, PathStyle(posix));
256 }
257
258
259 bool is_absolute_path(string const & p)
260 {
261         if (p.empty())
262                 return false;
263
264         bool isDosPath = (p.length() > 1 && p[1] == ':');
265         bool isUnixPath = (p[0] == '/');
266
267         return isDosPath || isUnixPath;
268 }
269
270
271 // returns a string suitable to be passed to popen when
272 // reading a pipe
273 char const * popen_read_mode()
274 {
275         return "r";
276 }
277
278
279 string const & nulldev()
280 {
281         static string const nulldev_ = "/dev/null";
282         return nulldev_;
283 }
284
285
286 shell_type shell()
287 {
288         return UNIX;
289 }
290
291
292 char path_separator()
293 {
294         return ':';
295 }
296
297
298 void windows_style_tex_paths(bool use_windows_paths)
299 {
300         windows_style_tex_paths_ = use_windows_paths;
301 }
302
303
304 bool canAutoOpenFile(string const & ext, auto_open_mode const mode)
305 {
306         if (ext.empty())
307                 return false;
308
309         string const full_ext = "." + ext;
310
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);
318 }
319
320
321 bool autoOpenFile(string const & filename, auto_open_mode const mode)
322 {
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;
329 }
330
331
332 void addFontResources()
333 {
334 #ifdef X_DISPLAY_MISSING
335         // Windows only: Add BaKoMa TrueType font resources
336         string const fonts_dir = addPath(package().system_support().absFilename(), "fonts");
337
338         HMODULE hDLL = LoadLibrary("gdi32");
339         FONTAPI pAddFontResourceEx =
340                 (FONTAPI) GetProcAddress(hDLL, "AddFontResourceExA");
341
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);
349                 } else {
350                         // Older Windows versions: Use AddFontResource
351                         AddFontResource(font_current.c_str());
352                 }
353         }
354         FreeLibrary(hDLL);
355 #endif
356 }
357
358
359 void restoreFontResources()
360 {
361 #ifdef X_DISPLAY_MISSING
362         // Windows only: Remove BaKoMa TrueType font resources
363         string const fonts_dir = addPath(package().system_support().absFilename(), "fonts");
364
365         HMODULE hDLL = LoadLibrary("gdi32");
366         FONTAPI pRemoveFontResourceEx = (FONTAPI) GetProcAddress(hDLL, "RemoveFontResourceExA");
367
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);
375                 } else {
376                         // Older Windows versions: Use RemoveFontResource
377                         RemoveFontResource(font_current.c_str());
378                 }
379         }
380         FreeLibrary(hDLL);
381 #endif
382 }
383
384 } // namespace os
385 } // namespace support
386 } // namespace lyx