]> git.lyx.org Git - lyx.git/blob - src/support/os_cygwin.C
* src/support/os.h
[lyx.git] / src / support / os_cygwin.C
1 /**
2  * \file os_cygwin.C
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 cygwin_path_fix_ = 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() ||
59                 (!contains(p, '\\') && (p.length() <= 1 || p[1] == ':'));
60 }
61
62
63 enum PathStyle {
64         posix,
65         windows
66 };
67
68
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         if (target == posix)
80                 cygwin_conv_to_posix_path(p.c_str(), path_buf);
81         else
82                 cygwin_conv_to_win32_path(p.c_str(), path_buf);
83
84         return subst(path_buf[0] ? path_buf : p, '\\', '/');
85 }
86
87
88 string convert_path_list(string const & p, PathStyle const & target)
89 {
90         char const * const pc = p.c_str();
91         PathStyle const actual = cygwin_posix_path_list_p(pc) ? posix : windows;
92
93         if (target != actual) {
94                 int const target_size = (target == posix) ?
95                                 cygwin_win32_to_posix_path_list_buf_size(pc) :
96                                 cygwin_posix_to_win32_path_list_buf_size(pc);
97
98                 char * ptr = new char[target_size];
99
100                 if (ptr) {
101                         if (target == posix)
102                                 cygwin_win32_to_posix_path_list(pc, ptr);
103                         else
104                                 cygwin_posix_to_win32_path_list(pc, ptr);
105
106                         string path_list = subst(ptr, '\\', '/');
107                         delete ptr;
108                         return path_list;
109                 }
110         }
111
112         return subst(p, '\\', '/');
113 }
114
115 } // namespace anon
116
117 void os::init(int, char *[])
118 {
119         // Copy cygwin environment variables to the Windows environment
120         // if they're not already there.
121
122         char **envp = environ;
123         char curval[2];
124         string var;
125         string val;
126         bool temp_seen = false;
127
128         while (envp && *envp) {
129                 val = split(*envp++, var, '=');
130
131                 if (var == "TEMP")
132                         temp_seen = true;
133                 
134                 if (GetEnvironmentVariable(var.c_str(), curval, 2) == 0
135                                 && GetLastError() == ERROR_ENVVAR_NOT_FOUND) {
136                         /* Convert to Windows style where necessary */
137                         if (var == "PATH" || var == "LD_LIBRARY_PATH") {
138                                 string const winpathlist =
139                                     convert_path_list(val, PathStyle(windows));
140                                 if (!winpathlist.empty()) {
141                                         SetEnvironmentVariable(var.c_str(),
142                                                 winpathlist.c_str());
143                                 }
144                         } else if (var == "HOME" || var == "TMPDIR" ||
145                                         var == "TMP" || var == "TEMP") {
146                                 string const winpath =
147                                         convert_path(val, PathStyle(windows));
148                                 SetEnvironmentVariable(var.c_str(), winpath.c_str());
149                         } else {
150                                 SetEnvironmentVariable(var.c_str(), val.c_str());
151                         }
152                 }
153         }
154         if (!temp_seen) {
155                 string const winpath = convert_path("/tmp", PathStyle(windows));
156                 SetEnvironmentVariable("TEMP", winpath.c_str());
157         }
158 }
159
160
161 string current_root()
162 {
163         return string("/");
164 }
165
166
167 string::size_type common_path(string const & p1, string const & p2)
168 {
169         string::size_type i = 0;
170         string::size_type       p1_len = p1.length();
171         string::size_type       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 string external_path(string const & p)
188 {
189         return convert_path(p, cygwin_path_fix_ ? PathStyle(windows)
190                                                 : PathStyle(posix));
191 }
192
193
194 string internal_path(string const & p)
195 {
196         return convert_path(p, PathStyle(posix));
197 }
198
199
200 string external_path_list(string const & p)
201 {
202         return convert_path_list(p, cygwin_path_fix_ ? PathStyle(windows)
203                                                      : PathStyle(posix));
204 }
205
206
207 string internal_path_list(string const & p)
208 {
209         return convert_path_list(p, PathStyle(posix));
210 }
211
212
213 string latex_path(string const & p)
214 {
215         // We may need a posix style path or a windows style path (depending
216         // on cygwin_path_fix_), but we use always forward slashes, since it
217         // gets written into a .tex file.
218
219         if (cygwin_path_fix_ && is_absolute_path(p)) {
220                 string dos_path = convert_path(p, PathStyle(windows));
221                 lyxerr[Debug::LATEX]
222                         << "<Cygwin path correction> ["
223                         << p << "]->>["
224                         << dos_path << ']' << endl;
225                 return dos_path;
226         }
227
228         return convert_path(p, PathStyle(posix));
229 }
230
231
232 bool is_absolute_path(string const & p)
233 {
234         if (p.empty())
235                 return false;
236
237         bool isDosPath = (p.length() > 1 && p[1] == ':');
238         bool isUnixPath = (p[0] == '/');
239
240         return isDosPath || isUnixPath;
241 }
242
243
244 // returns a string suitable to be passed to popen when
245 // reading a pipe
246 char const * popen_read_mode()
247 {
248         return "r";
249 }
250
251
252 string const & nulldev()
253 {
254         static string const nulldev_ = "/dev/null";
255         return nulldev_;
256 }
257
258
259 shell_type shell()
260 {
261         return UNIX;
262 }
263
264
265 char path_separator()
266 {
267         return ':';
268 }
269
270
271 void cygwin_path_fix(bool use_cygwin_paths)
272 {
273         cygwin_path_fix_ = use_cygwin_paths;
274 }
275
276
277 bool canAutoOpenFile(string const & ext, auto_open_mode const mode)
278 {
279         if (ext.empty())
280                 return false;
281
282         string const full_ext = "." + ext;
283
284         DWORD bufSize = MAX_PATH + 100;
285         TCHAR buf[MAX_PATH + 100];
286         // reference: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc
287         //                 /platform/shell/reference/shlwapi/registry/assocquerystring.asp
288         char const * action = (mode == VIEW) ? "open" : "edit";
289         return S_OK == AssocQueryString(0, ASSOCSTR_EXECUTABLE,
290                 full_ext.c_str(), action, buf, &bufSize);
291 }
292
293
294 bool autoOpenFile(string const & filename, auto_open_mode const mode)
295 {
296         // reference: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc
297         //                 /platform/shell/reference/functions/shellexecute.asp
298         string const win_path =
299                 os::convert_path(filename, os::PathStyle(os::windows));
300         char const * action = (mode == VIEW) ? "open" : "edit";
301         return reinterpret_cast<int>(ShellExecute(NULL, action,
302                 win_path.c_str(), NULL, NULL, 1)) > 32;
303 }
304
305
306 } // namespace os
307 } // namespace support
308 } // namespace lyx