]> git.lyx.org Git - lyx.git/blob - src/support/os_cygwin.cpp
304dd3e9975de715bb2476bf77469e13bd384d71
[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 #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
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 } // namespace anon
127
128 void os::init(int, char *[])
129 {
130         // Make sure that the TEMP variable is set
131         // and sync the Windows environment.
132
133         setenv("TEMP", "/tmp", false);
134         cygwin_internal(CW_SYNC_WINENV);
135 }
136
137
138 string current_root()
139 {
140         return string("/");
141 }
142
143
144 docstring::size_type common_path(docstring const & p1, docstring const & p2)
145 {
146         docstring::size_type i = 0;
147         docstring::size_type const p1_len = p1.length();
148         docstring::size_type const p2_len = p2.length();
149         while (i < p1_len && i < p2_len && uppercase(p1[i]) == uppercase(p2[i]))
150                 ++i;
151         if ((i < p1_len && i < p2_len)
152             || (i < p1_len && p1[i] != '/' && i == p2_len)
153             || (i < p2_len && p2[i] != '/' && i == p1_len))
154         {
155                 if (i)
156                         --i;     // here was the last match
157                 while (i && p1[i] != '/')
158                         --i;
159         }
160         return i;
161 }
162
163
164 string external_path(string const & p)
165 {
166         return convert_path(p, PathStyle(posix));
167 }
168
169
170 string internal_path(string const & p)
171 {
172         return convert_path(p, PathStyle(posix));
173 }
174
175
176 string external_path_list(string const & p)
177 {
178         return convert_path_list(p, PathStyle(posix));
179 }
180
181
182 string internal_path_list(string const & p)
183 {
184         return convert_path_list(p, PathStyle(posix));
185 }
186
187
188 string latex_path(string const & p)
189 {
190         // We may need a posix style path or a windows style path (depending
191         // on windows_style_tex_paths_), but we use always forward slashes,
192         // since it gets written into a .tex file.
193
194         if (windows_style_tex_paths_ && is_absolute_path(p)) {
195                 string dos_path = convert_path(p, PathStyle(windows));
196                 LYXERR(Debug::LATEX)
197                         << "<Path correction for LaTeX> ["
198                         << p << "]->>["
199                         << dos_path << ']' << endl;
200                 return dos_path;
201         }
202
203         return convert_path(p, PathStyle(posix));
204 }
205
206
207 bool is_absolute_path(string const & p)
208 {
209         if (p.empty())
210                 return false;
211
212         bool isDosPath = (p.length() > 1 && p[1] == ':');
213         bool isUnixPath = (p[0] == '/');
214
215         return isDosPath || isUnixPath;
216 }
217
218
219 // returns a string suitable to be passed to popen when
220 // reading a pipe
221 char const * popen_read_mode()
222 {
223         return "r";
224 }
225
226
227 string const & nulldev()
228 {
229         static string const nulldev_ = "/dev/null";
230         return nulldev_;
231 }
232
233
234 shell_type shell()
235 {
236         return UNIX;
237 }
238
239
240 char path_separator()
241 {
242         return ':';
243 }
244
245
246 void windows_style_tex_paths(bool use_windows_paths)
247 {
248         windows_style_tex_paths_ = use_windows_paths;
249 }
250
251
252 bool canAutoOpenFile(string const & ext, auto_open_mode const mode)
253 {
254         if (ext.empty())
255                 return false;
256
257         string const full_ext = "." + ext;
258
259         DWORD bufSize = MAX_PATH + 100;
260         TCHAR buf[MAX_PATH + 100];
261         // reference: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc
262         //                 /platform/shell/reference/shlwapi/registry/assocquerystring.asp
263         char const * action = (mode == VIEW) ? "open" : "edit";
264         return S_OK == AssocQueryString(0, ASSOCSTR_EXECUTABLE,
265                 full_ext.c_str(), action, buf, &bufSize);
266 }
267
268
269 bool autoOpenFile(string const & filename, auto_open_mode const mode)
270 {
271         // reference: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc
272         //                 /platform/shell/reference/functions/shellexecute.asp
273         string const win_path = to_local8bit(from_utf8(convert_path(filename, PathStyle(windows))));
274         char const * action = (mode == VIEW) ? "open" : "edit";
275         return reinterpret_cast<int>(ShellExecute(NULL, action,
276                 win_path.c_str(), NULL, NULL, 1)) > 32;
277 }
278
279 } // namespace os
280 } // namespace support
281 } // namespace lyx