]> git.lyx.org Git - lyx.git/blob - src/support/os_cygwin.cpp
032aae7f131b46962129a7d68f39f8d1b9157d42
[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
19 #include "support/FileName.h"
20 #include "support/lstrings.h"
21 #include "support/debug.h"
22
23 #include <windows.h>
24 #include <io.h>
25 #include <windef.h>
26 #include <shellapi.h>
27 #include <shlwapi.h>
28
29 #include <sys/cygwin.h>
30
31 using namespace std;
32
33 namespace lyx {
34 namespace support {
35 namespace os {
36
37 namespace {
38
39 bool windows_style_tex_paths_ = false;
40
41 // In both is_posix_path() and is_windows_path() it is assumed that
42 // a valid posix or pseudo-windows path is passed. They simply tell
43 // whether the path looks posix/pseudo-windows or not.
44
45 bool is_posix_path(string const & p)
46 {
47         return  p.empty() ||
48                 (!contains(p, '\\') && (p.length() <= 1 || p[1] != ':'));
49 }
50
51 // This is a test for a win32 style path with forward slashes (pseudo-windows).
52
53 bool is_windows_path(string const & p)
54 {
55         return p.empty() || (!contains(p, '\\') && p[0] != '/');
56 }
57
58
59 enum PathStyle {
60         posix,
61         windows
62 };
63
64
65 /// Convert a path to or from posix style.
66 /// \p p is encoded in local 8bit encoding or utf8.
67 /// The result is returned in the same encoding as \p p.
68 string convert_path(string const & p, PathStyle const & target)
69 {
70         char path_buf[PATH_MAX];
71
72         if ((target == posix && is_posix_path(p)) ||
73             (target == windows && is_windows_path(p)))
74                 return p;
75
76         path_buf[0] = '\0';
77
78         // cygwin_conv_to_posix_path and cygwin_conv_to_win32_path do not
79         // care about the encoding.
80         if (target == posix)
81                 cygwin_conv_to_posix_path(p.c_str(), path_buf);
82         else
83                 cygwin_conv_to_win32_path(p.c_str(), path_buf);
84
85         return subst(path_buf[0] ? path_buf : p, '\\', '/');
86 }
87
88
89 /// Convert a path list to or from posix style.
90 /// \p p is encoded in local 8bit encoding or utf8.
91 /// The result is returned in the same encoding as \p p.
92 string convert_path_list(string const & p, PathStyle const & target)
93 {
94         if (p.empty())
95                 return p;
96
97         char const * const pc = p.c_str();
98         PathStyle const actual = cygwin_posix_path_list_p(pc) ? posix : windows;
99
100         if (target != actual) {
101                 int const target_size = (target == posix) ?
102                                 cygwin_win32_to_posix_path_list_buf_size(pc) :
103                                 cygwin_posix_to_win32_path_list_buf_size(pc);
104
105                 char * ptr = new char[target_size];
106
107                 if (ptr) {
108                         // FIXME: See comment in convert_path() above
109                         if (target == posix)
110                                 cygwin_win32_to_posix_path_list(pc, ptr);
111                         else
112                                 cygwin_posix_to_win32_path_list(pc, ptr);
113
114                         string path_list = subst(ptr, '\\', '/');
115                         delete ptr;
116                         return path_list;
117                 }
118         }
119
120         return subst(p, '\\', '/');
121 }
122
123 } // namespace anon
124
125 void os::init(int, char *[])
126 {
127         // Make sure that the TEMP variable is set
128         // and sync the Windows environment.
129
130         setenv("TEMP", "/tmp", false);
131         cygwin_internal(CW_SYNC_WINENV);
132 }
133
134
135 string current_root()
136 {
137         return string("/");
138 }
139
140
141 bool isFilesystemCaseSensitive()
142 {
143         // FIXME: is this correct?
144         return true;
145 }
146
147
148 docstring::size_type common_path(docstring const & p1, docstring const & p2)
149 {
150         docstring::size_type i = 0;
151         docstring::size_type const p1_len = p1.length();
152         docstring::size_type const p2_len = p2.length();
153         while (i < p1_len && i < p2_len && uppercase(p1[i]) == uppercase(p2[i]))
154                 ++i;
155         if ((i < p1_len && i < p2_len)
156             || (i < p1_len && p1[i] != '/' && i == p2_len)
157             || (i < p2_len && p2[i] != '/' && i == p1_len))
158         {
159                 if (i)
160                         --i;     // here was the last match
161                 while (i && p1[i] != '/')
162                         --i;
163         }
164         return i;
165 }
166
167
168 string external_path(string const & p)
169 {
170         return convert_path(p, PathStyle(posix));
171 }
172
173
174 string internal_path(string const & p)
175 {
176         return convert_path(p, PathStyle(posix));
177 }
178
179
180 string external_path_list(string const & p)
181 {
182         return convert_path_list(p, PathStyle(posix));
183 }
184
185
186 string internal_path_list(string const & p)
187 {
188         return convert_path_list(p, PathStyle(posix));
189 }
190
191
192 string latex_path(string const & p)
193 {
194         // We may need a posix style path or a windows style path (depending
195         // on windows_style_tex_paths_), but we use always forward slashes,
196         // since it gets written into a .tex file.
197
198         if (windows_style_tex_paths_ && FileName(p).isAbsolute()) {
199                 string dos_path = convert_path(p, PathStyle(windows));
200                 LYXERR(Debug::LATEX, "<Path correction for LaTeX> ["
201                         << p << "]->>[" << dos_path << ']');
202                 return dos_path;
203         }
204
205         return convert_path(p, PathStyle(posix));
206 }
207
208
209 // returns a string suitable to be passed to popen when
210 // reading a pipe
211 char const * popen_read_mode()
212 {
213         return "r";
214 }
215
216
217 string const & nulldev()
218 {
219         static string const nulldev_ = "/dev/null";
220         return nulldev_;
221 }
222
223
224 shell_type shell()
225 {
226         return UNIX;
227 }
228
229
230 char path_separator()
231 {
232         return ':';
233 }
234
235
236 void windows_style_tex_paths(bool use_windows_paths)
237 {
238         windows_style_tex_paths_ = use_windows_paths;
239 }
240
241
242 bool canAutoOpenFile(string const & ext, auto_open_mode const mode)
243 {
244         if (ext.empty())
245                 return false;
246
247         string const full_ext = "." + ext;
248
249         DWORD bufSize = MAX_PATH + 100;
250         TCHAR buf[MAX_PATH + 100];
251         // reference: http://msdn.microsoft.com/en-us/library/bb773471.aspx
252         char const * action = (mode == VIEW) ? "open" : "edit";
253         return S_OK == AssocQueryString(ASSOCF_INIT_IGNOREUNKNOWN,
254                 ASSOCSTR_EXECUTABLE, full_ext.c_str(), action, buf, &bufSize);
255 }
256
257
258 bool autoOpenFile(string const & filename, auto_open_mode const mode)
259 {
260         // reference: http://msdn.microsoft.com/en-us/library/bb762153.aspx
261         string const win_path = to_local8bit(from_utf8(convert_path(filename, PathStyle(windows))));
262         char const * action = (mode == VIEW) ? "open" : "edit";
263         return reinterpret_cast<int>(ShellExecute(NULL, action,
264                 win_path.c_str(), NULL, NULL, 1)) > 32;
265 }
266
267 } // namespace os
268 } // namespace support
269 } // namespace lyx