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 #define INCL_DOSFILEMGR
18 #define INCL_DOSMODULEMGR
19 #define INCL_DOSPROCESS
21 #define INCL_DOSERRORS
24 #include <boost/scoped_array.hpp>
26 using boost::scoped_array;
33 os::shell_type shell_ = os::UNIX;
34 unsigned long cp_ = 0;
42 void init(int argc, char * argv[])
44 _wildcard(&argc, &argv);
45 PTIB ptib = new TIB[1];
46 PPIB ppib = new PIB[1];
47 APIRET rc = DosGetInfoBlocks(&ptib, &ppib);
50 scoped_array<char> tmp(new char[256]);
51 // This is the only reliable way to retrieve the executable name.
52 rc = DosQueryModuleName(ppib->pib_hmte, 256L, tmp);
57 binname_ = OnlyFilename(p);
58 binname_.erase(binname_.length()-4, string::npos);
59 binpath_ = OnlyPath(p);
61 // OS/2 cmd.exe has another use for '&'
62 string sh = OnlyFilename(GetEnvPath("EMXSHELL"));
64 // COMSPEC is set, unless user unsets
65 sh = OnlyFilename(GetEnvPath("COMSPEC"));
69 sh = lowercase(sh); // DosMapCase() is an overkill here
70 if (contains(sh, "cmd.exe") || contains(sh, "4os2.exe"))
75 static bool initialized = false;
80 ULONG CPList[3] = {0};
82 APIRET rc = DosQueryCp(3 * sizeof(ULONG), CPList, &CPList_size);
85 // CPList[0] == process current codepage,
86 // CPList[1] == system default codepage, the rest are auxilary.
87 // Once cp_ is correctly set, you can call other routines.
92 void warn(string const & /*mesg*/)
103 rc = DosQueryCurrentDisk(&drv_num, &drv_map);
106 char drive = 'A' + drv_num -1;
107 string tmp(1, drive);
113 string::size_type common_path(string const & p1, string const & p2)
115 static bool initialized = false;
123 cntry.codepage = cp_;
124 string temp1 = slashify_path(p1);
125 string temp2 = slashify_path(p2);
126 char * tmp1 = const_cast<char *> (temp1.c_str());
127 char * tmp2 = const_cast<char *> (temp2.c_str());
128 /* rc = */ DosMapCase(p1.length(), &cntry, tmp1);
129 // if (rc != NO_ERROR)
131 /* rc = */ DosMapCase(p2.length(), &cntry, tmp2);
132 // if (rc != NO_ERROR)
134 // This algorithm works only if paths are slashified on DBCS systems.
135 string::size_type i = 0;
136 string::size_type p1_len = p1.length();
137 string::size_type p2_len = p2.length();
138 while (i < p1_len && i < p2_len && tmp1[i] == tmp2[i])
140 if ((i < p1_len && i < p2_len)
141 || (i < p1_len && tmp1[i] != '/' && i == p2_len)
142 || (i < p2_len && tmp2[i] != '/' && i == p1_len))
145 --i; // here was the last match
146 while (i && tmp1[i] != '/')
153 string slashify_path(string const & p)
155 static bool initialized = false;
156 static bool leadbyte[256] = {false};
161 cntry.codepage = cp_;
162 unsigned char *DBCSinfo = new unsigned char[12];
163 /* rc = */ DosQueryDBCSEnv(12, &cntry, (char*) DBCSinfo);
164 // if (rc != NO_ERROR)
166 for (int j = 1; j < 12; j += 2)
169 bool isLeadByte = false;
170 while (*DBCSinfo != 0) {
171 if (i == *DBCSinfo) {
172 isLeadByte = !isLeadByte;
175 leadbyte[i++] = isLeadByte;
179 string::iterator lit = p.begin();
180 string::iterator end = p.end();
181 for (; lit < end; ++lit) {
182 if (leadbyte[(*lit)])
187 p = subst(p, "//", "/");
192 string external_path(string const & p)
198 string internal_path(string const & p)
204 bool is_absolute_path(string const & p)
206 return (p.length() > 1
207 && isalpha(static_cast<unsigned char>(p[0]))
212 // returns a string suitable to be passed to popen when
214 char const * popen_read_mode()
232 void setTmpDir(string const & p)
249 } // end namespace os