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 if (argc != 0 /* This is a hack! */) {
45 _wildcard(argc, argv);
46 PTIB ptib = new TIB[1];
47 PPIB ppib = new PIB[1];
48 APIRET rc = DosGetInfoBlocks(&ptib, &ppib);
51 scoped_array<char> tmp(new char[256]);
52 // This is the only reliable way to retrieve the executable name.
53 rc = DosQueryModuleName(ppib->pib_hmte, 256L, tmp);
58 binname_ = OnlyFilename(p);
59 binname_.erase(binname_.length()-4, string::npos);
60 binpath_ = OnlyPath(p);
62 // OS/2 cmd.exe has another use for '&'
63 string sh = OnlyFilename(GetEnvPath("EMXSHELL"));
65 // COMSPEC is set, unless user unsets
66 sh = OnlyFilename(GetEnvPath("COMSPEC"));
70 sh = lowercase(sh); // DosMapCase() is an overkill here
71 if (contains(sh, "cmd.exe") || contains(sh, "4os2.exe"))
77 static bool initialized = false;
82 ULONG CPList[3] = {0};
84 APIRET rc = DosQueryCp(3 * sizeof(ULONG), CPList, &CPList_size);
87 // CPList[0] == process current codepage,
88 // CPList[1] == system default codepage, the rest are auxilary.
89 // Once cp_ is correctly set, you can call other routines.
94 void warn(string const & /*mesg*/)
100 string current_root()
105 rc = DosQueryCurrentDisk(&drv_num, &drv_map);
108 char drive = 'A' + drv_num -1;
109 string tmp(1, drive);
115 string::size_type common_path(string const & p1, string const & p2)
117 static bool initialized = false;
125 cntry.codepage = cp_;
126 string temp1 = slashify_path(p1);
127 string temp2 = slashify_path(p2);
128 char * tmp1 = const_cast<char *> (temp1.c_str());
129 char * tmp2 = const_cast<char *> (temp2.c_str());
130 /* rc = */ DosMapCase(p1.length(), &cntry, tmp1);
131 // if (rc != NO_ERROR)
133 /* rc = */ DosMapCase(p2.length(), &cntry, tmp2);
134 // if (rc != NO_ERROR)
136 // This algorithm works only if paths are slashified on DBCS systems.
137 string::size_type i = 0;
138 string::size_type p1_len = p1.length();
139 string::size_type p2_len = p2.length();
140 while (i < p1_len && i < p2_len && tmp1[i] == tmp2[i])
142 if ((i < p1_len && i < p2_len)
143 || (i < p1_len && tmp1[i] != '/' && i == p2_len)
144 || (i < p2_len && tmp2[i] != '/' && i == p1_len))
147 --i; // here was the last match
148 while (i && tmp1[i] != '/')
155 string slashify_path(string const & p)
157 static bool initialized = false;
158 static bool leadbyte[256] = {false};
163 cntry.codepage = cp_;
164 unsigned char *DBCSinfo = new unsigned char[12];
165 /* rc = */ DosQueryDBCSEnv(12, &cntry, (char*) DBCSinfo);
166 // if (rc != NO_ERROR)
168 for (int j = 1; j < 12; j += 2)
171 bool isLeadByte = false;
172 while (*DBCSinfo != 0) {
173 if (i == *DBCSinfo) {
174 isLeadByte = !isLeadByte;
177 leadbyte[i++] = isLeadByte;
181 string::iterator lit = p.begin();
182 string::iterator end = p.end();
183 for (; lit < end; ++lit) {
184 if (leadbyte[(*lit)])
189 p = subst(p, "//", "/");
194 string external_path(string const & p)
200 string internal_path(string const & p)
206 bool is_absolute_path(string const & p)
208 return (p.length() > 1
209 && isalpha(static_cast<unsigned char>(p[0]))
214 // returns a string suitable to be passed to popen when
216 char const * popen_read_mode()
234 void setTmpDir(string const & p)
251 } // end namespace os