]> git.lyx.org Git - lyx.git/blob - src/support/os_cygwin.C
Renaming:
[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 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() ||
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, PathStyle(windows));
190 }
191
192
193 string internal_path(string const & p)
194 {
195         return convert_path(p, PathStyle(posix));
196 }
197
198
199 string external_path_list(string const & p)
200 {
201         return convert_path_list(p, PathStyle(windows));
202 }
203
204
205 string internal_path_list(string const & p)
206 {
207         return convert_path_list(p, PathStyle(posix));
208 }
209
210
211 string latex_path(string const & p)
212 {
213         // We may need a posix style path or a windows style path (depending
214         // on windows_style_tex_paths_), but we use always forward slashes,
215         // since it gets written into a .tex file.
216
217         if (windows_style_tex_paths_ && is_absolute_path(p)) {
218                 string dos_path = convert_path(p, PathStyle(windows));
219                 lyxerr[Debug::LATEX]
220                         << "<Path correction for LaTeX> ["
221                         << p << "]->>["
222                         << dos_path << ']' << endl;
223                 return dos_path;
224         }
225
226         return convert_path(p, PathStyle(posix));
227 }
228
229
230 bool is_absolute_path(string const & p)
231 {
232         if (p.empty())
233                 return false;
234
235         bool isDosPath = (p.length() > 1 && p[1] == ':');
236         bool isUnixPath = (p[0] == '/');
237
238         return isDosPath || isUnixPath;
239 }
240
241
242 // returns a string suitable to be passed to popen when
243 // reading a pipe
244 char const * popen_read_mode()
245 {
246         return "r";
247 }
248
249
250 string const & nulldev()
251 {
252         static string const nulldev_ = "/dev/null";
253         return nulldev_;
254 }
255
256
257 shell_type shell()
258 {
259         return UNIX;
260 }
261
262
263 char path_separator()
264 {
265         return ':';
266 }
267
268
269 void windows_style_tex_paths(bool use_windows_paths)
270 {
271         windows_style_tex_paths_ = use_windows_paths;
272 }
273
274
275 bool canAutoOpenFile(string const & ext, auto_open_mode const mode)
276 {
277         if (ext.empty())
278                 return false;
279
280         string const full_ext = "." + ext;
281
282         DWORD bufSize = MAX_PATH + 100;
283         TCHAR buf[MAX_PATH + 100];
284         // reference: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc
285         //                 /platform/shell/reference/shlwapi/registry/assocquerystring.asp
286         char const * action = (mode == VIEW) ? "open" : "edit";
287         return S_OK == AssocQueryString(0, ASSOCSTR_EXECUTABLE,
288                 full_ext.c_str(), action, buf, &bufSize);
289 }
290
291
292 bool autoOpenFile(string const & filename, auto_open_mode const mode)
293 {
294         // reference: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc
295         //                 /platform/shell/reference/functions/shellexecute.asp
296         string const win_path = convert_path(filename, PathStyle(windows));
297         char const * action = (mode == VIEW) ? "open" : "edit";
298         return reinterpret_cast<int>(ShellExecute(NULL, action,
299                 win_path.c_str(), NULL, NULL, 1)) > 32;
300 }
301
302
303 } // namespace os
304 } // namespace support
305 } // namespace lyx