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