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"
23 #include <sys/cygwin.h>
25 using namespace lyx::support;
45 void init(int /* argc */, char * argv[])
47 static bool initialized = false;
52 string tmp = internal_path(argv[0]);
53 binname_ = OnlyFilename(tmp);
54 tmp = ExpandPath(tmp); // This expands ./ and ~/
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);
64 tmp = MakeAbsPath(OnlyPath(tmp));
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);
73 homepath_ = GetEnvPath("HOME");
74 nulldev_ = "/dev/null";
77 homepath_ = GetEnvPath("HOMEDRIVE") + GetEnvPath("HOMEPATH");
83 void warn(string const & mesg)
85 MessageBox(0, mesg.c_str(), "LyX error",
86 MB_OK|MB_ICONSTOP|MB_SYSTEMMODAL);
96 string::size_type common_path(string const & p1, string const & p2)
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]))
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))
108 --i; // here was the last match
109 while (i && p1[i] != '/')
116 string external_path(string const & p)
120 // Translate from cygwin path syntax to dos path syntax
121 if (is_absolute_path(p)) {
123 cygwin_conv_to_full_win32_path(p.c_str(), dp);
124 dos_path = !dp ? "" : dp;
128 #else // regular Win32
132 //No backslashes in LaTeX files
133 dos_path = subst(dos_path,'\\','/');
136 << "<Win32 path correction> ["
138 << dos_path << ']' << endl;
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)
151 cygwin_conv_to_posix_path(p.c_str(), pp);
152 string const posix_path = MakeLatexName(pp);
154 string const posix_path = subst(p,"\\","/");
156 lyxerr[Debug::DEPEND]
157 << "<Win32 path correction> ["
159 << posix_path << ']' << endl;
164 // (Claus H.) On Win32 both Unix and Win32/DOS pathnames are used.
165 // Therefore an absolute path could be either a pathname starting
166 // with a slash (Unix) or a pathname starting with a drive letter
167 // followed by a colon. Because a colon is not valid in pathes in Unix
168 // and at another location in Win32 testing just for the existance
169 // of the colon in the 2nd position seems to be enough!
170 bool is_absolute_path(string const & p)
175 bool isDosPath = (p.length() > 1 && p[1] == ':');
176 bool isUnixPath = (p[0] == '/');
178 return isDosPath || isUnixPath;
182 // returns a string suitable to be passed to popen when
184 char const * popen_read_mode()
202 void setTmpDir(string const & p)
214 string const & homepath()
220 string const & nulldev()
236 } // namespace support