3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Ruurd A. Reitsma
7 * \author Claus Hentschel
8 * \author Angus Leeming
10 * Full author contact details are available in file CREDITS.
12 * Various OS specific functions
17 #include "support/os.h"
18 #include "support/lstrings.h"
24 #include <direct.h> // _getdrive
34 void os::init(int /* argc */, char * argv[])
36 /* Note from Angus, 17 Jan 2005:
38 * The code below is taken verbatim from Ruurd's original patch
39 * porting LyX to Win32.
41 * Windows allows us to define LyX either as a console-based app
42 * or as a GUI-based app. Ruurd decided to define LyX as a
43 * console-based app with a "main" function rather than a "WinMain"
44 * function as the point of entry to the program, but to
45 * immediately close the console window that Windows helpfully
46 * opens for us. Doing so allows the user to see all of LyX's
47 * debug output simply by running LyX from a DOS or MSYS-shell
50 * The alternative approach is to define LyX as a genuine
51 * GUI-based app, with a "WinMain" function as the entry point to the
52 * executable rather than a "main" function, so:
54 * #if defined (_WIN32)
55 * # define WIN32_LEAN_AND_MEAN
56 * # include <stdlib.h> // for __argc, __argv
57 * # include <windows.h> // for WinMain
60 * // This will require the "-mwindows" flag when linking with
62 * // For MSVC, use "/subsystem:windows".
63 * #if defined (_WIN32)
64 * int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
66 * return mymain(__argc, __argv);
70 * where "mymain" is just a renamed "main".
72 * However, doing so means that the lyxerr messages would mysteriously
73 * disappear. They could be resurrected with something like:
77 * freopen("conin$","r",stdin);
78 * freopen("conout$","w",stdout);
79 * freopen("conout$","w",stderr);
82 * This code could be invoked (say) the first time that lyxerr
83 * is called. However, Ruurd has tried this route and found that some
84 * shell scripts failed, for mysterious reasons...
86 * I've chosen for now, therefore, to simply add Ruurd's original
89 // Close the console when run (probably)
90 // not run from command prompt
91 char WindowTitle[1024];
92 if (GetConsoleTitle(WindowTitle, sizeof(WindowTitle)) == 0) {
93 // Could not get the title, so we just leave things as they are
97 if ((strcmp(WindowTitle, argv[0]) == 0) ||
98 (strcmp(WindowTitle, "LyX") == 0)) {
99 // format a "unique" newWindowTitle
100 wsprintf(WindowTitle, "%d/%d",
102 GetCurrentProcessId());
103 // change current window title
104 SetConsoleTitle(WindowTitle);
105 // ensure window title has been updated
107 // look for newWindowTitle
108 HWND const hwndFound = FindWindow(NULL, WindowTitle);
110 if (hwndFound != NULL)
111 ShowWindow( hwndFound, SW_HIDE);
116 string current_root()
118 // _getdrive returns the current drive (1=A, 2=B, and so on).
119 char const drive = ::_getdrive() + 'A' - 1;
120 return string(1, drive) + ":/";
124 string::size_type common_path(string const & p1, string const & p2)
126 string::size_type i = 0;
127 string::size_type p1_len = p1.length();
128 string::size_type p2_len = p2.length();
129 while (i < p1_len && i < p2_len && uppercase(p1[i]) == uppercase(p2[i]))
131 if ((i < p1_len && i < p2_len)
132 || (i < p1_len && p1[i] != '/' && i == p2_len)
133 || (i < p2_len && p2[i] != '/' && i == p1_len))
136 --i; // here was the last match
137 while (i && p1[i] != '/')
144 string external_path(string const & p)
146 string const dos_path = subst(p, "/", "\\");
149 << "<Win32 path correction> ["
151 << dos_path << ']' << endl;
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)
162 return subst(p, "\\", "/");
166 // (Claus H.) On Win32 both Unix and Win32/DOS pathnames are used.
167 // Therefore an absolute path could be either a pathname starting
168 // with a slash (Unix) or a pathname starting with a drive letter
169 // followed by a colon. Because a colon is not valid in pathes in Unix
170 // and at another location in Win32 testing just for the existance
171 // of the colon in the 2nd position seems to be enough!
172 bool is_absolute_path(string const & p)
177 bool isDosPath = (p.length() > 1 && p[1] == ':');
178 bool isUnixPath = (p[0] == '/');
180 return isDosPath || isUnixPath;
184 // returns a string suitable to be passed to popen when
186 char const * popen_read_mode()
192 string const & nulldev()
194 static string const nulldev_ = "nul";
205 char path_separator()
211 void cygwin_path_fix(bool)
215 } // namespace support