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