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