]> git.lyx.org Git - lyx.git/blob - src/support/os_cygwin.cpp
Fixed some lines that were too long. It compiled afterwards.
[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         // Make sure that the TEMP variable is set
149         // and sync the Windows environment.
150
151         setenv("TEMP", "/tmp", false);
152         cygwin_internal(CW_SYNC_WINENV);
153 }
154
155
156 string current_root()
157 {
158         return string("/");
159 }
160
161
162 docstring::size_type common_path(docstring const & p1, docstring const & p2)
163 {
164         docstring::size_type i = 0;
165         docstring::size_type const p1_len = p1.length();
166         docstring::size_type const p2_len = p2.length();
167         while (i < p1_len && i < p2_len && uppercase(p1[i]) == uppercase(p2[i]))
168                 ++i;
169         if ((i < p1_len && i < p2_len)
170             || (i < p1_len && p1[i] != '/' && i == p2_len)
171             || (i < p2_len && p2[i] != '/' && i == p1_len))
172         {
173                 if (i)
174                         --i;     // here was the last match
175                 while (i && p1[i] != '/')
176                         --i;
177         }
178         return i;
179 }
180
181
182 string external_path(string const & p)
183 {
184         return convert_path(p, PathStyle(posix));
185 }
186
187
188 string internal_path(string const & p)
189 {
190         return convert_path(p, PathStyle(posix));
191 }
192
193
194 string external_path_list(string const & p)
195 {
196         return convert_path_list(p, PathStyle(posix));
197 }
198
199
200 string internal_path_list(string const & p)
201 {
202         return convert_path_list(p, PathStyle(posix));
203 }
204
205
206 string latex_path(string const & p)
207 {
208         // We may need a posix style path or a windows style path (depending
209         // on windows_style_tex_paths_), but we use always forward slashes,
210         // since it gets written into a .tex file.
211
212         if (windows_style_tex_paths_ && is_absolute_path(p)) {
213                 string dos_path = convert_path(p, PathStyle(windows));
214                 LYXERR(Debug::LATEX)
215                         << "<Path correction for LaTeX> ["
216                         << p << "]->>["
217                         << dos_path << ']' << endl;
218                 return dos_path;
219         }
220
221         return convert_path(p, PathStyle(posix));
222 }
223
224
225 bool is_absolute_path(string const & p)
226 {
227         if (p.empty())
228                 return false;
229
230         bool isDosPath = (p.length() > 1 && p[1] == ':');
231         bool isUnixPath = (p[0] == '/');
232
233         return isDosPath || isUnixPath;
234 }
235
236
237 // returns a string suitable to be passed to popen when
238 // reading a pipe
239 char const * popen_read_mode()
240 {
241         return "r";
242 }
243
244
245 string const & nulldev()
246 {
247         static string const nulldev_ = "/dev/null";
248         return nulldev_;
249 }
250
251
252 shell_type shell()
253 {
254         return UNIX;
255 }
256
257
258 char path_separator()
259 {
260         return ':';
261 }
262
263
264 void windows_style_tex_paths(bool use_windows_paths)
265 {
266         windows_style_tex_paths_ = use_windows_paths;
267 }
268
269
270 bool canAutoOpenFile(string const & ext, auto_open_mode const mode)
271 {
272         if (ext.empty())
273                 return false;
274
275         string const full_ext = "." + ext;
276
277         DWORD bufSize = MAX_PATH + 100;
278         TCHAR buf[MAX_PATH + 100];
279         // reference: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc
280         //                 /platform/shell/reference/shlwapi/registry/assocquerystring.asp
281         char const * action = (mode == VIEW) ? "open" : "edit";
282         return S_OK == AssocQueryString(0, ASSOCSTR_EXECUTABLE,
283                 full_ext.c_str(), action, buf, &bufSize);
284 }
285
286
287 bool autoOpenFile(string const & filename, auto_open_mode const mode)
288 {
289         // reference: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc
290         //                 /platform/shell/reference/functions/shellexecute.asp
291         string const win_path = to_local8bit(from_utf8(convert_path(filename, PathStyle(windows))));
292         char const * action = (mode == VIEW) ? "open" : "edit";
293         return reinterpret_cast<int>(ShellExecute(NULL, action,
294                 win_path.c_str(), NULL, NULL, 1)) > 32;
295 }
296
297
298 void addFontResources()
299 {
300 #ifdef X_DISPLAY_MISSING
301         // Windows only: Add BaKoMa TrueType font resources
302         string const fonts_dir = addPath(package().system_support().absFilename(), "fonts");
303
304         HMODULE hDLL = LoadLibrary("gdi32");
305         FONTAPI pAddFontResourceEx =
306                 (FONTAPI) GetProcAddress(hDLL, "AddFontResourceExA");
307
308         for (int i = 0 ; i < num_fonts_truetype ; ++i) {
309                 string const font_current = to_local8bit(from_utf8(convert_path(
310                         addName(fonts_dir, win_fonts_truetype[i] + ".ttf"),
311                         PathStyle(windows))));
312                 if (pAddFontResourceEx) {
313                         // Windows 2000 and later: Use AddFontResourceEx
314                         pAddFontResourceEx(font_current.c_str(), FR_PRIVATE, 0);
315                 } else {
316                         // Older Windows versions: Use AddFontResource
317                         AddFontResource(font_current.c_str());
318                 }
319         }
320         FreeLibrary(hDLL);
321 #endif
322 }
323
324
325 void restoreFontResources()
326 {
327 #ifdef X_DISPLAY_MISSING
328         // Windows only: Remove BaKoMa TrueType font resources
329         string const fonts_dir = addPath(package().system_support().absFilename(), "fonts");
330
331         HMODULE hDLL = LoadLibrary("gdi32");
332         FONTAPI pRemoveFontResourceEx = (FONTAPI) GetProcAddress(hDLL, "RemoveFontResourceExA");
333
334         for(int i = 0 ; i < num_fonts_truetype ; ++i) {
335                 string const font_current = to_local8bit(from_utf8(convert_path(
336                         addName(fonts_dir, win_fonts_truetype[i] + ".ttf"),
337                         PathStyle(windows))));
338                 if (pRemoveFontResourceEx) {
339                         // Windows 2000 and later: Use RemoveFontResourceEx
340                         pRemoveFontResourceEx(font_current.c_str(), FR_PRIVATE, 0);
341                 } else {
342                         // Older Windows versions: Use RemoveFontResource
343                         RemoveFontResource(font_current.c_str());
344                 }
345         }
346         FreeLibrary(hDLL);
347 #endif
348 }
349
350 } // namespace os
351 } // namespace support
352 } // namespace lyx