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