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/filetools.h"
17 #include "support/lstrings.h"
24 #if defined(__CYGWIN__) || defined(__CYGWIN32__)
25 # include <sys/cygwin.h>
28 # include <direct.h> // _getdrive
31 using namespace lyx::support;
51 void init(int /* argc */, char * argv[])
53 static bool initialized = false;
58 string tmp = internal_path(argv[0]);
59 binname_ = OnlyFilename(tmp);
60 tmp = ExpandPath(tmp); // This expands ./ and ~/
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);
70 tmp = MakeAbsPath(OnlyPath(tmp));
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);
77 #if defined(__CYGWIN__) || defined(__CYGWIN32__)
79 homepath_ = GetEnvPath("HOME");
80 nulldev_ = "/dev/null";
83 homepath_ = GetEnvPath("HOMEDRIVE") + GetEnvPath("HOMEPATH");
89 void warn(string const & mesg)
91 MessageBox(0, mesg.c_str(), "LyX error",
92 MB_OK|MB_ICONSTOP|MB_SYSTEMMODAL);
98 #if defined(__CYGWIN__) || defined(__CYGWIN32__)
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) + ":/";
109 string::size_type common_path(string const & p1, string const & p2)
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]))
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))
121 --i; // here was the last match
122 while (i && p1[i] != '/')
129 string external_path(string const & p)
132 #if defined(__CYGWIN__) || defined(__CYGWIN32__)
133 // Translate from cygwin path syntax to dos path syntax
134 if (is_absolute_path(p)) {
136 cygwin_conv_to_full_win32_path(p.c_str(), dp);
137 dos_path = !dp ? "" : dp;
141 #else // regular Win32
145 //No backslashes in LaTeX files
146 dos_path = subst(dos_path,'\\','/');
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 #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);
168 return subst(p,"\\","/");
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)
184 bool isDosPath = (p.length() > 1 && p[1] == ':');
185 bool isUnixPath = (p[0] == '/');
187 return isDosPath || isUnixPath;
191 // returns a string suitable to be passed to popen when
193 char const * popen_read_mode()
199 string const & binpath()
205 string const & binname()
211 void setTmpDir(string const & p)
217 string const & getTmpDir()
223 string const & homepath()
229 string const & nulldev()
237 #if defined(__CYGWIN__) || defined(__CYGWIN32__)
245 } // namespace support