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
8 * Full author contact details are available in file CREDITS.
10 * Various OS specific functions
15 #include "support/os.h"
16 #include "support/lstrings.h"
23 #if defined(__CYGWIN__) || defined(__CYGWIN32__)
24 # include <sys/cygwin.h>
27 # include <direct.h> // _getdrive
38 void os::init(int /* argc */, char * argv[])
41 /* Note from Angus, 17 Jan 2005:
43 * The code below is taken verbatim from Ruurd's original patch
44 * porting LyX to Win32.
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
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:
59 * #if defined (_WIN32)
60 * # define WIN32_LEAN_AND_MEAN
61 * # include <stdlib.h> // for __argc, __argv
62 * # include <windows.h> // for WinMain
65 * // This will require the "-mwindows" flag when linking with
67 * // For MSVC, use "/subsystem:windows".
68 * #if defined (_WIN32)
69 * int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
71 * return mymain(__argc, __argv);
75 * where "mymain" is just a renamed "main".
77 * However, doing so means that the lyxerr messages would mysteriously
78 * disappear. They could be resurrected with something like:
82 * freopen("conin$", "r", stdin);
83 * freopen("conout$", "w", stdout);
84 * freopen("conout$", "w", stderr);
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...
91 * I've chosen for now, therefore, to simply add Ruurd's original
94 // Close the console when run (probably)
95 // not run from command prompt
96 char WindowTitle[1024];
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",
104 GetCurrentProcessId());
105 // change current window title
106 SetConsoleTitle(WindowTitle);
107 // ensure window title has been updated
109 // look for newWindowTitle
110 hwndFound=FindWindow(NULL, WindowTitle);
112 if ( hwndFound != NULL)
113 ShowWindow( hwndFound, SW_HIDE);
119 string current_root()
121 #if defined(__CYGWIN__) || defined(__CYGWIN32__)
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) + ":/";
132 string::size_type common_path(string const & p1, string const & p2)
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]))
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))
144 --i; // here was the last match
145 while (i && p1[i] != '/')
152 #if defined(__CYGWIN__) || defined(__CYGWIN32__)
155 bool cygwin_path_fix_ = false;
161 string external_path(string const & p)
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)) {
169 cygwin_conv_to_full_win32_path(p.c_str(), dp);
170 dos_path = !dp ? "" : dp;
174 #else // regular Win32
178 //No backslashes in LaTeX files
179 dos_path = subst(dos_path,'\\','/');
182 << "<Win32 path correction> ["
184 << dos_path << ']' << endl;
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)
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);
201 return subst(p,"\\","/");
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)
217 bool isDosPath = (p.length() > 1 && p[1] == ':');
218 bool isUnixPath = (p[0] == '/');
220 return isDosPath || isUnixPath;
224 // returns a string suitable to be passed to popen when
226 char const * popen_read_mode()
232 string const & nulldev()
234 #if defined(__CYGWIN__) || defined(__CYGWIN32__)
235 static string const nulldev_ = "/dev/null";
237 static string const nulldev_ = "nul";
245 #if defined(__CYGWIN__) || defined(__CYGWIN32__)
253 char path_separator()
263 void cygwin_path_fix(bool use_cygwin_paths)
267 (void)use_cygwin_paths;
269 use_cygwin_paths_ = use_cygwin_paths;
274 } // namespace support