]> git.lyx.org Git - lyx.git/blob - src/support/os_win32.C
s/MAX_PATH/PATH_MAX/.
[lyx.git] / src / support / os_win32.C
1 /**
2  * \file os_win32.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  *
8  * Full author contact details are available in file CREDITS.
9  *
10  * Various OS specific functions
11  */
12
13 #include <config.h>
14
15 #include "support/os.h"
16 #include "support/filetools.h"
17 #include "support/lstrings.h"
18
19 #include "debug.h"
20
21 #include <windows.h>
22 #include <io.h>
23
24 #if defined(__CYGWIN__) || defined(__CYGWIN32__)
25 # include <sys/cygwin.h>
26
27 #elif defined(_WIN32)
28 # include <direct.h> // _getdrive
29 #endif
30
31 using namespace lyx::support;
32 using std::endl;
33 using std::string;
34
35
36 namespace {
37
38 string binpath_;
39 string binname_;
40 string tmpdir_;
41 string homepath_;
42 string nulldev_;
43
44 }
45
46
47 namespace lyx {
48 namespace support {
49 namespace os {
50
51 void init(int /* argc */, char * argv[])
52 {
53         static bool initialized = false;
54         if (initialized)
55                 return;
56         initialized = true;
57
58         string tmp = internal_path(argv[0]);
59         binname_ = OnlyFilename(tmp);
60         tmp = ExpandPath(tmp); // This expands ./ and ~/
61
62         if (!is_absolute_path(tmp)) {
63                 string binsearchpath = GetEnvPath("PATH");
64                 // This will make "src/lyx" work always :-)
65                 binsearchpath += ";.";
66                 tmp = internal_path(argv[0]);
67                 tmp = FileOpenSearch(binsearchpath, tmp);
68         }
69
70         tmp = MakeAbsPath(OnlyPath(tmp));
71
72         // In case we are running in place and compiled with shared libraries
73         if (suffixIs(tmp, "/.libs/"))
74                 tmp.erase(tmp.length()-6, string::npos);
75         binpath_ = tmp;
76
77 #if defined(__CYGWIN__) || defined(__CYGWIN32__)
78         tmpdir_ = "/tmp";
79         homepath_ = GetEnvPath("HOME");
80         nulldev_ = "/dev/null";
81 #else
82         tmpdir_ = string();
83         homepath_ = GetEnvPath("HOMEDRIVE") + GetEnvPath("HOMEPATH");
84         nulldev_ = "nul";
85 #endif
86 }
87
88
89 void warn(string const & mesg)
90 {
91         MessageBox(0, mesg.c_str(), "LyX error",
92         MB_OK|MB_ICONSTOP|MB_SYSTEMMODAL);
93 }
94
95
96 string current_root()
97 {
98 #if defined(__CYGWIN__) || defined(__CYGWIN32__)
99         return string("/");
100
101 #else
102         // _getdrive returns the current drive (1=A, 2=B, and so on).
103         char const drive = ::_getdrive() + 'A' - 1;
104         return string(1, drive) + ":/";
105 #endif
106 }
107
108
109 string::size_type common_path(string const & p1, string const & p2)
110 {
111         string::size_type i = 0;
112         string::size_type       p1_len = p1.length();
113         string::size_type       p2_len = p2.length();
114         while (i < p1_len && i < p2_len && uppercase(p1[i]) == uppercase(p2[i]))
115                 ++i;
116         if ((i < p1_len && i < p2_len)
117             || (i < p1_len && p1[i] != '/' && i == p2_len)
118             || (i < p2_len && p2[i] != '/' && i == p1_len))
119         {
120                 if (i)
121                         --i;     // here was the last match
122                 while (i && p1[i] != '/')
123                         --i;
124         }
125         return i;
126 }
127
128
129 string external_path(string const & p)
130 {
131         string dos_path;
132 #if defined(__CYGWIN__) || defined(__CYGWIN32__)
133         // Translate from cygwin path syntax to dos path syntax
134         if (is_absolute_path(p)) {
135                 char dp[PATH_MAX];
136                 cygwin_conv_to_full_win32_path(p.c_str(), dp);
137                 dos_path = !dp ? "" : dp;
138         }
139
140         else return p;
141 #else // regular Win32
142         dos_path = p;
143 #endif
144         
145         //No backslashes in LaTeX files
146         dos_path = subst(dos_path,'\\','/');
147
148         lyxerr[Debug::LATEX]
149                 << "<Win32 path correction> ["
150                 << p << "]->>["
151                 << dos_path << ']' << endl;
152         return dos_path;
153 }
154
155
156 // (Claus H.) Parsing the latex log file in an Win32 environment all
157 // files are mentioned in Win32/DOS syntax. Because LyX uses the dep file
158 // entries to check if any file has been changed we must retranslate
159 // the Win32/DOS pathnames into Cygwin pathnames.
160 string internal_path(string const & p)
161 {
162 #if defined(__CYGWIN__) || defined(__CYGWIN32__)
163         char posix_path[PATH_MAX];
164         posix_path[0] = '\0';
165         cygwin_conv_to_posix_path(p.c_str(), posix_path);
166         return posix_path;
167 #else
168         return subst(p,"\\","/");
169 #endif
170 }
171
172
173 // (Claus H.) On Win32 both Unix and Win32/DOS pathnames are used.
174 // Therefore an absolute path could be either a pathname starting
175 // with a slash (Unix) or a pathname starting with a drive letter
176 // followed by a colon. Because a colon is not valid in pathes in Unix
177 // and at another location in Win32 testing just for the existance
178 // of the colon in the 2nd position seems to be enough!
179 bool is_absolute_path(string const & p)
180 {
181         if (p.empty())
182                 return false;
183
184         bool isDosPath = (p.length() > 1 && p[1] == ':');
185         bool isUnixPath = (p[0] == '/');
186
187         return isDosPath || isUnixPath;
188 }
189
190
191 // returns a string suitable to be passed to popen when
192 // reading a pipe
193 char const * popen_read_mode()
194 {
195         return "r";
196 }
197
198
199 string const & binpath()
200 {
201         return binpath_;
202 }
203
204
205 string const & binname()
206 {
207         return binname_;
208 }
209
210
211 void setTmpDir(string const & p)
212 {
213         tmpdir_ = p;
214 }
215
216
217 string const & getTmpDir()
218 {
219         return tmpdir_;
220 }
221
222
223 string const & homepath()
224 {
225         return homepath_;
226 }
227
228
229 string const & nulldev()
230 {
231         return nulldev_;
232 }
233
234
235 shell_type shell()
236 {
237 #if defined(__CYGWIN__) || defined(__CYGWIN32__)
238         return UNIX;
239 #else
240         return CMD_EXE;
241 #endif
242 }
243
244 } // namespace os
245 } // namespace support
246 } // namespace lyx