]> git.lyx.org Git - lyx.git/blob - src/support/os_cygwin.C
Partly revert revision 16744. Georg doesn't want to mix string/docstring in there.
[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 string external_path_list(string const & p)
215 {
216         return convert_path_list(p, PathStyle(posix));
217 }
218
219
220 string internal_path_list(string const & p)
221 {
222         return convert_path_list(p, PathStyle(posix));
223 }
224
225
226 string latex_path(string const & p)
227 {
228         // We may need a posix style path or a windows style path (depending
229         // on windows_style_tex_paths_), but we use always forward slashes,
230         // since it gets written into a .tex file.
231
232         if (windows_style_tex_paths_ && is_absolute_path(p)) {
233                 string dos_path = convert_path(p, PathStyle(windows));
234                 lyxerr[Debug::LATEX]
235                         << "<Path correction for LaTeX> ["
236                         << p << "]->>["
237                         << dos_path << ']' << endl;
238                 return dos_path;
239         }
240
241         return convert_path(p, PathStyle(posix));
242 }
243
244
245 bool is_absolute_path(string const & p)
246 {
247         if (p.empty())
248                 return false;
249
250         bool isDosPath = (p.length() > 1 && p[1] == ':');
251         bool isUnixPath = (p[0] == '/');
252
253         return isDosPath || isUnixPath;
254 }
255
256
257 // returns a string suitable to be passed to popen when
258 // reading a pipe
259 char const * popen_read_mode()
260 {
261         return "r";
262 }
263
264
265 string const & nulldev()
266 {
267         static string const nulldev_ = "/dev/null";
268         return nulldev_;
269 }
270
271
272 shell_type shell()
273 {
274         return UNIX;
275 }
276
277
278 char path_separator()
279 {
280         return ':';
281 }
282
283
284 void windows_style_tex_paths(bool use_windows_paths)
285 {
286         windows_style_tex_paths_ = use_windows_paths;
287 }
288
289
290 bool canAutoOpenFile(string const & ext, auto_open_mode const mode)
291 {
292         if (ext.empty())
293                 return false;
294
295         string const full_ext = "." + ext;
296
297         DWORD bufSize = MAX_PATH + 100;
298         TCHAR buf[MAX_PATH + 100];
299         // reference: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc
300         //                 /platform/shell/reference/shlwapi/registry/assocquerystring.asp
301         char const * action = (mode == VIEW) ? "open" : "edit";
302         return S_OK == AssocQueryString(0, ASSOCSTR_EXECUTABLE,
303                 full_ext.c_str(), action, buf, &bufSize);
304 }
305
306
307 bool autoOpenFile(string const & filename, auto_open_mode const mode)
308 {
309         // reference: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc
310         //                 /platform/shell/reference/functions/shellexecute.asp
311         string const win_path = convert_path(filename, PathStyle(windows));
312         char const * action = (mode == VIEW) ? "open" : "edit";
313         return reinterpret_cast<int>(ShellExecute(NULL, action,
314                 win_path.c_str(), NULL, NULL, 1)) > 32;
315 }
316
317
318 void addFontResources()
319 {
320 #ifdef X_DISPLAY_MISSING
321         // Windows only: Add BaKoMa TrueType font resources
322         string const fonts_dir = addPath(package().system_support(), "fonts");
323         
324         for (int i = 0 ; i < num_fonts_truetype ; ++i) {
325                 string const font_current = convert_path(
326                         addName(fonts_dir, win_fonts_truetype[i] + ".ttf"),
327                         PathStyle(windows));
328                 AddFontResource(font_current.c_str());
329         }
330 #endif
331 }
332
333
334 void restoreFontResources()
335 {
336 #ifdef X_DISPLAY_MISSING
337         // Windows only: Remove BaKoMa TrueType font resources
338         string const fonts_dir = addPath(package().system_support(), "fonts");
339         
340         for(int i = 0 ; i < num_fonts_truetype ; ++i) {
341                 string const font_current = convert_path(
342                         addName(fonts_dir, win_fonts_truetype[i] + ".ttf"),
343                         PathStyle(windows));
344                 RemoveFontResource(font_current.c_str());
345         }
346 #endif
347 }
348
349 } // namespace os
350 } // namespace support
351 } // namespace lyx