]> git.lyx.org Git - lyx.git/blob - src/support/os_win32.C
Remove unicode characters.
[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/lstrings.h"
17
18 #include "debug.h"
19
20 #include <windows.h>
21 #include <io.h>
22
23 #if defined(__CYGWIN__) || defined(__CYGWIN32__)
24 # include <sys/cygwin.h>
25
26 #elif defined(_WIN32)
27 # include <direct.h> // _getdrive
28 #endif
29
30 using std::endl;
31 using std::string;
32
33
34 namespace lyx {
35 namespace support {
36 namespace os {
37
38 void os::init(int /* argc */, char * argv[])
39 {
40 #ifdef _WIN32
41         /* Note from Angus, 17 Jan 2005:
42          *
43          * The code below is taken verbatim from Ruurd's original patch
44          * porting LyX to Win32.
45          *
46          * Windows allows us to define LyX either as a console-based app
47          * or as a GUI-based app. Ruurd decided to define LyX as a
48          * console-based app with a "main" function rather than a "WinMain"
49          * function as the point of entry to the program, but to
50          * immediately close the console window that Windows helpfully
51          * opens for us. Doing so allows the user to see all of LyX's
52          * debug output simply by running LyX from a DOS or MSYS-shell
53          * prompt.
54          *
55          * The alternative approach is to define LyX as a genuine
56          * GUI-based app, with a "WinMain" function as the entry point to the
57          * executable rather than a "main" function, so:
58          *
59          * #if defined (_WIN32)
60          * # define WIN32_LEAN_AND_MEAN
61          * # include <stdlib.h>  // for __argc,__argv
62          * # include <windows.h> // for WinMain
63          * #endif
64          *
65          * // This will require the "-mwindows" flag when linking with
66          * // gcc under MinGW.
67          * // For MSVC, use "/subsystem:windows".
68          * #if defined (_WIN32)
69          * int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
70          * {
71          *     return mymain(__argc, __argv);
72          * }
73          * #endif
74          *
75          * where "mymain" is just a renamed "main".
76          *
77          * However, doing so means that the lyxerr messages would mysteriously
78          * disappear. They could be resurrected with something like:
79          *
80          * #ifdef WIN32
81          *  AllocConsole();
82          *  freopen("conin$","r",stdin);
83          *  freopen("conout$","w",stdout);
84          *  freopen("conout$","w",stderr);
85          * #endif
86          *
87          * This code could be invoked (say) the first time that lyxerr
88          * is called. However, Ruurd has tried this route and found that some
89          * shell scripts failed, for mysterious reasons...
90          *
91          * I've chosen for now, therefore, to simply add Ruurd's original
92          * code as-is.
93          */
94         // Close the console when run (probably)
95         // not run from command prompt
96         char WindowTitle[1024];
97         HWND hwndFound;
98         GetConsoleTitle(WindowTitle,1024);
99         if ((strcmp(WindowTitle, argv[0]) == 0) ||
100                 (strcmp(WindowTitle,"LyX") == 0)) {
101                 // format a "unique" newWindowTitle
102                 wsprintf(WindowTitle,"%d/%d",
103                         GetTickCount(),
104                         GetCurrentProcessId());
105                 // change current window title
106                 SetConsoleTitle(WindowTitle);
107                 // ensure window title has been updated
108                 Sleep(40);
109                 // look for newWindowTitle
110                 hwndFound=FindWindow(NULL, WindowTitle);
111                 // If found, hide it
112                 if ( hwndFound != NULL)
113                         ShowWindow( hwndFound, SW_HIDE);
114         }
115 #endif
116 }
117
118
119 string current_root()
120 {
121 #if defined(__CYGWIN__) || defined(__CYGWIN32__)
122         return string("/");
123
124 #else
125         // _getdrive returns the current drive (1=A, 2=B, and so on).
126         char const drive = ::_getdrive() + 'A' - 1;
127         return string(1, drive) + ":/";
128 #endif
129 }
130
131
132 string::size_type common_path(string const & p1, string const & p2)
133 {
134         string::size_type i = 0;
135         string::size_type       p1_len = p1.length();
136         string::size_type       p2_len = p2.length();
137         while (i < p1_len && i < p2_len && uppercase(p1[i]) == uppercase(p2[i]))
138                 ++i;
139         if ((i < p1_len && i < p2_len)
140             || (i < p1_len && p1[i] != '/' && i == p2_len)
141             || (i < p2_len && p2[i] != '/' && i == p1_len))
142         {
143                 if (i)
144                         --i;     // here was the last match
145                 while (i && p1[i] != '/')
146                         --i;
147         }
148         return i;
149 }
150
151
152 #if defined(__CYGWIN__) || defined(__CYGWIN32__)
153 namespace {
154
155 bool cygwin_path_fix_ = false;
156
157 } // namespace anon
158 #endif
159
160
161 string external_path(string const & p)
162 {
163         string dos_path;
164
165 #if defined(__CYGWIN__) || defined(__CYGWIN32__)
166         // Translate from cygwin path syntax to dos path syntax
167         if (cygwin_path_fix_ && is_absolute_path(p)) {
168                 char dp[PATH_MAX];
169                 cygwin_conv_to_full_win32_path(p.c_str(), dp);
170                 dos_path = !dp ? "" : dp;
171         }
172
173         else return p;
174 #else // regular Win32
175         dos_path = p;
176 #endif
177
178         //No backslashes in LaTeX files
179         dos_path = subst(dos_path,'\\','/');
180
181         lyxerr[Debug::LATEX]
182                 << "<Win32 path correction> ["
183                 << p << "]->>["
184                 << dos_path << ']' << endl;
185         return dos_path;
186 }
187
188
189 // (Claus H.) Parsing the latex log file in an Win32 environment all
190 // files are mentioned in Win32/DOS syntax. Because LyX uses the dep file
191 // entries to check if any file has been changed we must retranslate
192 // the Win32/DOS pathnames into Cygwin pathnames.
193 string internal_path(string const & p)
194 {
195 #if defined(__CYGWIN__) || defined(__CYGWIN32__)
196         char posix_path[PATH_MAX];
197         posix_path[0] = '\0';
198         cygwin_conv_to_posix_path(p.c_str(), posix_path);
199         return posix_path;
200 #else
201         return subst(p,"\\","/");
202 #endif
203 }
204
205
206 // (Claus H.) On Win32 both Unix and Win32/DOS pathnames are used.
207 // Therefore an absolute path could be either a pathname starting
208 // with a slash (Unix) or a pathname starting with a drive letter
209 // followed by a colon. Because a colon is not valid in pathes in Unix
210 // and at another location in Win32 testing just for the existance
211 // of the colon in the 2nd position seems to be enough!
212 bool is_absolute_path(string const & p)
213 {
214         if (p.empty())
215                 return false;
216
217         bool isDosPath = (p.length() > 1 && p[1] == ':');
218         bool isUnixPath = (p[0] == '/');
219
220         return isDosPath || isUnixPath;
221 }
222
223
224 // returns a string suitable to be passed to popen when
225 // reading a pipe
226 char const * popen_read_mode()
227 {
228         return "r";
229 }
230
231
232 string const & nulldev()
233 {
234 #if defined(__CYGWIN__) || defined(__CYGWIN32__)
235         static string const nulldev_ = "/dev/null";
236 #else
237         static string const nulldev_ = "nul";
238 #endif
239         return nulldev_;
240 }
241
242
243 shell_type shell()
244 {
245 #if defined(__CYGWIN__) || defined(__CYGWIN32__)
246         return UNIX;
247 #else
248         return CMD_EXE;
249 #endif
250 }
251
252
253 char path_separator()
254 {
255 #if defined (_WIN32)
256         return ';';
257 #else // Cygwin
258         return ':';
259 #endif
260 }
261
262
263 void cygwin_path_fix(bool use_cygwin_paths)
264 {
265 #if defined (_WIN32)
266         // Silence warning.
267         (void)use_cygwin_paths;
268 #else // Cygwin
269         use_cygwin_paths_ = use_cygwin_paths;
270 #endif
271 }
272
273 } // namespace os
274 } // namespace support
275 } // namespace lyx