]> git.lyx.org Git - lyx.git/blob - src/support/os_cygwin.C
Convert internal_path() to unicode (partially). Fix bug 3114:
[lyx.git] / src / support / os_cygwin.C
1 /**
2  * \file os_cygwin.C
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 string const win_fonts_truetype[] = {"cmex10", "cmmi10", "cmr10", "cmsy10",
44         "eufm10", "msam10", "msbm10", "wasy10", "esint10"};
45 const int num_fonts_truetype = sizeof(win_fonts_truetype) / sizeof(*win_fonts_truetype);
46 #endif
47
48
49 namespace lyx {
50 namespace support {
51 namespace os {
52
53 namespace {
54
55 bool windows_style_tex_paths_ = false;
56
57 // In both is_posix_path() and is_windows_path() it is assumed that
58 // a valid posix or pseudo-windows path is passed. They simply tell
59 // whether the path looks posix/pseudo-windows or not.
60
61 bool is_posix_path(string const & p)
62 {
63         return  p.empty() ||
64                 (!contains(p, '\\') && (p.length() <= 1 || p[1] != ':'));
65 }
66
67 // This is a test for a win32 style path with forward slashes (pseudo-windows).
68
69 bool is_windows_path(string const & p)
70 {
71         return p.empty() || (!contains(p, '\\') && p[0] != '/');
72 }
73
74
75 enum PathStyle {
76         posix,
77         windows
78 };
79
80
81 string convert_path(string const & p, PathStyle const & target)
82 {
83         char path_buf[PATH_MAX];
84
85         if ((target == posix && is_posix_path(p)) ||
86             (target == windows && is_windows_path(p)))
87                 return p;
88
89         path_buf[0] = '\0';
90
91         if (target == posix)
92                 cygwin_conv_to_posix_path(p.c_str(), path_buf);
93         else
94                 cygwin_conv_to_win32_path(p.c_str(), path_buf);
95
96         return subst(path_buf[0] ? path_buf : p, '\\', '/');
97 }
98
99
100 string convert_path_list(string const & p, PathStyle const & target)
101 {
102         if (p.empty())
103                 return p;
104
105         char const * const pc = p.c_str();
106         PathStyle const actual = cygwin_posix_path_list_p(pc) ? posix : windows;
107
108         if (target != actual) {
109                 int const target_size = (target == posix) ?
110                                 cygwin_win32_to_posix_path_list_buf_size(pc) :
111                                 cygwin_posix_to_win32_path_list_buf_size(pc);
112
113                 char * ptr = new char[target_size];
114
115                 if (ptr) {
116                         if (target == posix)
117                                 cygwin_win32_to_posix_path_list(pc, ptr);
118                         else
119                                 cygwin_posix_to_win32_path_list(pc, ptr);
120
121                         string path_list = subst(ptr, '\\', '/');
122                         delete ptr;
123                         return path_list;
124                 }
125         }
126
127         return subst(p, '\\', '/');
128 }
129
130 } // namespace anon
131
132 void os::init(int, char *[])
133 {
134         // Copy cygwin environment variables to the Windows environment
135         // if they're not already there.
136
137         char **envp = environ;
138         char curval[2];
139         string var;
140         string val;
141         bool temp_seen = false;
142
143         while (envp && *envp) {
144                 val = split(*envp++, var, '=');
145
146                 if (var == "TEMP")
147                         temp_seen = true;
148                 
149                 if (GetEnvironmentVariable(var.c_str(), curval, 2) == 0
150                                 && GetLastError() == ERROR_ENVVAR_NOT_FOUND) {
151                         /* Convert to Windows style where necessary */
152                         if (var == "PATH" || var == "LD_LIBRARY_PATH") {
153                                 string const winpathlist =
154                                     convert_path_list(val, PathStyle(windows));
155                                 if (!winpathlist.empty()) {
156                                         SetEnvironmentVariable(var.c_str(),
157                                                 winpathlist.c_str());
158                                 }
159                         } else if (var == "HOME" || var == "TMPDIR" ||
160                                         var == "TMP" || var == "TEMP") {
161                                 string const winpath =
162                                         convert_path(val, PathStyle(windows));
163                                 SetEnvironmentVariable(var.c_str(), winpath.c_str());
164                         } else {
165                                 SetEnvironmentVariable(var.c_str(), val.c_str());
166                         }
167                 }
168         }
169         if (!temp_seen) {
170                 string const winpath = convert_path("/tmp", PathStyle(windows));
171                 SetEnvironmentVariable("TEMP", winpath.c_str());
172         }
173 }
174
175
176 string current_root()
177 {
178         return string("/");
179 }
180
181
182 string::size_type common_path(string const & p1, string const & p2)
183 {
184         string::size_type i = 0;
185         string::size_type       p1_len = p1.length();
186         string::size_type       p2_len = p2.length();
187         while (i < p1_len && i < p2_len && uppercase(p1[i]) == uppercase(p2[i]))
188                 ++i;
189         if ((i < p1_len && i < p2_len)
190             || (i < p1_len && p1[i] != '/' && i == p2_len)
191             || (i < p2_len && p2[i] != '/' && i == p1_len))
192         {
193                 if (i)
194                         --i;     // here was the last match
195                 while (i && p1[i] != '/')
196                         --i;
197         }
198         return i;
199 }
200
201
202 string external_path(string const & p)
203 {
204         return convert_path(p, PathStyle(posix));
205 }
206
207
208 string internal_path(string const & p)
209 {
210         return convert_path(p, PathStyle(posix));
211 }
212
213
214 docstring internal_path(docstring const & p)
215 {
216         // FIXME UNICODE
217         return from_utf8(convert_path(to_utf8(p), PathStyle(posix)));
218 }
219
220
221 string external_path_list(string const & p)
222 {
223         return convert_path_list(p, PathStyle(posix));
224 }
225
226
227 string internal_path_list(string const & p)
228 {
229         return convert_path_list(p, PathStyle(posix));
230 }
231
232
233 string latex_path(string const & p)
234 {
235         // We may need a posix style path or a windows style path (depending
236         // on windows_style_tex_paths_), but we use always forward slashes,
237         // since it gets written into a .tex file.
238
239         if (windows_style_tex_paths_ && is_absolute_path(p)) {
240                 string dos_path = convert_path(p, PathStyle(windows));
241                 lyxerr[Debug::LATEX]
242                         << "<Path correction for LaTeX> ["
243                         << p << "]->>["
244                         << dos_path << ']' << endl;
245                 return dos_path;
246         }
247
248         return convert_path(p, PathStyle(posix));
249 }
250
251
252 bool is_absolute_path(string const & p)
253 {
254         if (p.empty())
255                 return false;
256
257         bool isDosPath = (p.length() > 1 && p[1] == ':');
258         bool isUnixPath = (p[0] == '/');
259
260         return isDosPath || isUnixPath;
261 }
262
263
264 // returns a string suitable to be passed to popen when
265 // reading a pipe
266 char const * popen_read_mode()
267 {
268         return "r";
269 }
270
271
272 string const & nulldev()
273 {
274         static string const nulldev_ = "/dev/null";
275         return nulldev_;
276 }
277
278
279 shell_type shell()
280 {
281         return UNIX;
282 }
283
284
285 char path_separator()
286 {
287         return ':';
288 }
289
290
291 void windows_style_tex_paths(bool use_windows_paths)
292 {
293         windows_style_tex_paths_ = use_windows_paths;
294 }
295
296
297 bool canAutoOpenFile(string const & ext, auto_open_mode const mode)
298 {
299         if (ext.empty())
300                 return false;
301
302         string const full_ext = "." + ext;
303
304         DWORD bufSize = MAX_PATH + 100;
305         TCHAR buf[MAX_PATH + 100];
306         // reference: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc
307         //                 /platform/shell/reference/shlwapi/registry/assocquerystring.asp
308         char const * action = (mode == VIEW) ? "open" : "edit";
309         return S_OK == AssocQueryString(0, ASSOCSTR_EXECUTABLE,
310                 full_ext.c_str(), action, buf, &bufSize);
311 }
312
313
314 bool autoOpenFile(string const & filename, auto_open_mode const mode)
315 {
316         // reference: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc
317         //                 /platform/shell/reference/functions/shellexecute.asp
318         string const win_path = convert_path(filename, PathStyle(windows));
319         char const * action = (mode == VIEW) ? "open" : "edit";
320         return reinterpret_cast<int>(ShellExecute(NULL, action,
321                 win_path.c_str(), NULL, NULL, 1)) > 32;
322 }
323
324
325 void addFontResources()
326 {
327 #ifdef X_DISPLAY_MISSING
328         // Windows only: Add BaKoMa TrueType font resources
329         string const fonts_dir = addPath(package().system_support(), "fonts");
330         
331         for (int i = 0 ; i < num_fonts_truetype ; ++i) {
332                 string const font_current = convert_path(
333                         addName(fonts_dir, win_fonts_truetype[i] + ".ttf"),
334                         PathStyle(windows));
335                 AddFontResource(font_current.c_str());
336         }
337 #endif
338 }
339
340
341 void restoreFontResources()
342 {
343 #ifdef X_DISPLAY_MISSING
344         // Windows only: Remove BaKoMa TrueType font resources
345         string const fonts_dir = addPath(package().system_support(), "fonts");
346         
347         for(int i = 0 ; i < num_fonts_truetype ; ++i) {
348                 string const font_current = convert_path(
349                         addName(fonts_dir, win_fonts_truetype[i] + ".ttf"),
350                         PathStyle(windows));
351                 RemoveFontResource(font_current.c_str());
352         }
353 #endif
354 }
355
356 } // namespace os
357 } // namespace support
358 } // namespace lyx