]> git.lyx.org Git - lyx.git/blob - src/support/os_cygwin.cpp
bd84776e76d65ead4ee5d07b4759148962d23032
[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  * \author Enrico Forestieri
10  *
11  * Full author contact details are available in file CREDITS.
12  *
13  * Various OS specific functions
14  */
15
16 #include <config.h>
17
18 #include "support/os.h"
19
20 #include "support/FileName.h"
21 #include "support/lstrings.h"
22 #include "support/debug.h"
23
24 #include <windows.h>
25 #include <io.h>
26 #include <windef.h>
27 #include <shellapi.h>
28 #include <shlwapi.h>
29 #include <limits.h>
30 #include <stdlib.h>
31
32 #include <sys/cygwin.h>
33
34 using namespace std;
35
36 namespace lyx {
37
38 void emergencyCleanup();
39
40 namespace support {
41 namespace os {
42
43 namespace {
44
45 bool windows_style_tex_paths_ = false;
46
47 // In both is_posix_path() and is_windows_path() it is assumed that
48 // a valid posix or pseudo-windows path is passed. They simply tell
49 // whether the path looks posix/pseudo-windows or not.
50
51 bool is_posix_path(string const & p)
52 {
53         return  p.empty() ||
54                 (!contains(p, '\\') && (p.length() <= 1 || p[1] != ':'));
55 }
56
57 // This is a test for a win32 style path with forward slashes (pseudo-windows).
58
59 bool is_windows_path(string const & p)
60 {
61         return p.empty() || (!contains(p, '\\') && p[0] != '/');
62 }
63
64
65 enum PathStyle {
66         posix,
67         windows
68 };
69
70
71 /// Convert a path to or from posix style.
72 /// \p p is encoded in local 8bit encoding or utf8.
73 /// The result is returned in the same encoding as \p p.
74 string convert_path(string const & p, PathStyle const & target)
75 {
76         char path_buf[PATH_MAX];
77
78         if ((target == posix && is_posix_path(p)) ||
79             (target == windows && is_windows_path(p)))
80                 return p;
81
82         path_buf[0] = '\0';
83
84         // cygwin_conv_to_posix_path and cygwin_conv_to_win32_path do not
85         // care about the encoding.
86         if (target == posix)
87                 cygwin_conv_to_posix_path(p.c_str(), path_buf);
88         else
89                 cygwin_conv_to_win32_path(p.c_str(), path_buf);
90
91         return subst(path_buf[0] ? path_buf : p, '\\', '/');
92 }
93
94
95 /// Convert a path list to or from posix style.
96 /// \p p is encoded in local 8bit encoding or utf8.
97 /// The result is returned in the same encoding as \p p.
98 string convert_path_list(string const & p, PathStyle const & target)
99 {
100         if (p.empty())
101                 return p;
102
103         char const * const pc = p.c_str();
104         PathStyle const actual = cygwin_posix_path_list_p(pc) ? posix : windows;
105
106         if (target != actual) {
107                 int const target_size = (target == posix) ?
108                                 cygwin_win32_to_posix_path_list_buf_size(pc) :
109                                 cygwin_posix_to_win32_path_list_buf_size(pc);
110
111                 char * ptr = new char[target_size];
112
113                 if (ptr) {
114                         // FIXME: See comment in convert_path() above
115                         if (target == posix)
116                                 cygwin_win32_to_posix_path_list(pc, ptr);
117                         else
118                                 cygwin_posix_to_win32_path_list(pc, ptr);
119
120                         string path_list = subst(ptr, '\\', '/');
121                         delete ptr;
122                         return path_list;
123                 }
124         }
125
126         return subst(p, '\\', '/');
127 }
128
129
130 BOOL terminate_handler(DWORD event)
131 {
132         if (event == CTRL_CLOSE_EVENT
133             || event == CTRL_LOGOFF_EVENT
134             || event == CTRL_SHUTDOWN_EVENT) {
135                 lyx::emergencyCleanup();
136                 return TRUE;
137         }
138         return FALSE;
139 }
140
141 } // namespace anon
142
143 void init(int, char *[])
144 {
145         // Make sure that the TEMP variable is set
146         // and sync the Windows environment.
147         setenv("TEMP", "/tmp", false);
148         cygwin_internal(CW_SYNC_WINENV);
149
150         // Catch shutdown events.
151         SetConsoleCtrlHandler((PHANDLER_ROUTINE)terminate_handler, TRUE);
152 }
153
154
155 string current_root()
156 {
157         return string("/");
158 }
159
160
161 bool isFilesystemCaseSensitive()
162 {
163         return false;
164 }
165
166
167 docstring::size_type common_path(docstring const & p1, docstring const & p2)
168 {
169         docstring::size_type i = 0;
170         docstring::size_type const p1_len = p1.length();
171         docstring::size_type const p2_len = p2.length();
172         while (i < p1_len && i < p2_len && uppercase(p1[i]) == uppercase(p2[i]))
173                 ++i;
174         if ((i < p1_len && i < p2_len)
175             || (i < p1_len && p1[i] != '/' && i == p2_len)
176             || (i < p2_len && p2[i] != '/' && i == p1_len))
177         {
178                 if (i)
179                         --i;     // here was the last match
180                 while (i && p1[i] != '/')
181                         --i;
182         }
183         return i;
184 }
185
186
187 bool path_prefix_is(string const & path, string const & pre)
188 {
189         return path_prefix_is(const_cast<string &>(path), pre, CASE_UNCHANGED);
190 }
191
192
193 bool path_prefix_is(string & path, string const & pre, path_case how)
194 {
195         docstring const p1 = from_utf8(path);
196         docstring const p2 = from_utf8(pre);
197         docstring::size_type const p1_len = p1.length();
198         docstring::size_type const p2_len = p2.length();
199         docstring::size_type common_len = common_path(p1, p2);
200
201         if (p2[p2_len - 1] == '/' && p1_len != p2_len)
202                 ++common_len;
203
204         if (common_len != p2_len)
205                 return false;
206
207         if (how == CASE_ADJUSTED && !prefixIs(path, pre)) {
208                 if (p1_len < common_len)
209                         path = to_utf8(p2.substr(0, p1_len));
210                 else
211                         path = to_utf8(p2 + p1.substr(common_len,
212                                                         p1_len - common_len));
213         }
214
215         return true;
216 }
217
218
219 string external_path(string const & p)
220 {
221         return convert_path(p, PathStyle(posix));
222 }
223
224
225 string internal_path(string const & p)
226 {
227         return convert_path(p, PathStyle(posix));
228 }
229
230
231 string external_path_list(string const & p)
232 {
233         return convert_path_list(p, PathStyle(posix));
234 }
235
236
237 string internal_path_list(string const & p)
238 {
239         return convert_path_list(p, PathStyle(posix));
240 }
241
242
243 string latex_path(string const & p)
244 {
245         // We may need a posix style path or a windows style path (depending
246         // on windows_style_tex_paths_), but we use always forward slashes,
247         // since it gets written into a .tex file.
248
249         if (windows_style_tex_paths_ && FileName::isAbsolute(p)) {
250                 string dos_path = convert_path(p, PathStyle(windows));
251                 LYXERR(Debug::LATEX, "<Path correction for LaTeX> ["
252                         << p << "]->>[" << dos_path << ']');
253                 return dos_path;
254         }
255
256         return convert_path(p, PathStyle(posix));
257 }
258
259
260 bool is_valid_strftime(string const & p)
261 {
262         string::size_type pos = p.find_first_of('%');
263         while (pos != string::npos) {
264                 if (pos + 1 == string::npos)
265                         break;
266                 if (!containsOnly(p.substr(pos + 1, 1),
267                         "aAbBcCdDeEFgGhHIjklmMnOpPrRsStTuUVwWxXyYzZ%+"))
268                         return false;
269                 if (pos + 2 == string::npos)
270                       break;
271                 pos = p.find_first_of('%', pos + 2);
272         }
273         return true;
274 }
275
276
277 // returns a string suitable to be passed to popen when
278 // reading a pipe
279 char const * popen_read_mode()
280 {
281         return "r";
282 }
283
284
285 string const & nulldev()
286 {
287         static string const nulldev_ = "/dev/null";
288         return nulldev_;
289 }
290
291
292 bool is_terminal(io_channel channel)
293 {
294         return isatty(channel);
295 }
296
297
298 shell_type shell()
299 {
300         return UNIX;
301 }
302
303
304 char path_separator()
305 {
306         return ':';
307 }
308
309
310 void windows_style_tex_paths(bool use_windows_paths)
311 {
312         windows_style_tex_paths_ = use_windows_paths;
313 }
314
315
316 bool canAutoOpenFile(string const & ext, auto_open_mode const mode)
317 {
318         if (ext.empty())
319                 return false;
320
321         string const full_ext = "." + ext;
322
323         DWORD bufSize = MAX_PATH + 100;
324         TCHAR buf[MAX_PATH + 100];
325         // reference: http://msdn.microsoft.com/en-us/library/bb773471.aspx
326         char const * action = (mode == VIEW) ? "open" : "edit";
327         return S_OK == AssocQueryString(ASSOCF_INIT_IGNOREUNKNOWN,
328                 ASSOCSTR_EXECUTABLE, full_ext.c_str(), action, buf, &bufSize);
329 }
330
331
332 bool autoOpenFile(string const & filename, auto_open_mode const mode)
333 {
334         // reference: http://msdn.microsoft.com/en-us/library/bb762153.aspx
335         string const win_path = to_local8bit(from_utf8(convert_path(filename, PathStyle(windows))));
336         char const * action = (mode == VIEW) ? "open" : "edit";
337         return reinterpret_cast<int>(ShellExecute(NULL, action,
338                 win_path.c_str(), NULL, NULL, 1)) > 32;
339 }
340
341
342 string real_path(string const & path)
343 {
344         char rpath[PATH_MAX + 1];
345         char * result = realpath(path.c_str(), rpath);
346         return FileName::fromFilesystemEncoding(result ? rpath : path).absFilename();
347 }
348
349 } // namespace os
350 } // namespace support
351 } // namespace lyx