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
16 #include "support/filetools.h"
17 #define INCL_DOSFILEMGR
18 #define INCL_DOSMODULEMGR
19 #define INCL_DOSPROCESS
21 #define INCL_DOSERRORS
29 os::shell_type shell_ = os::UNIX;
30 unsigned long cp_ = 0;
38 void init(int * argc, char ** argv[])
40 if (argc != 0 /* This is a hack! */) {
41 _wildcard(argc, argv);
42 PTIB ptib = new TIB[1];
43 PPIB ppib = new PIB[1];
44 APIRET rc = DosGetInfoBlocks(&ptib, &ppib);
47 char* tmp = new char[256];
48 // This is the only reliable way to retrieve the executable name.
49 rc = DosQueryModuleName(ppib->pib_hmte, 256L, tmp);
54 binname_ = OnlyFilename(p);
55 binname_.erase(binname_.length()-4, string::npos);
56 binpath_ = OnlyPath(p);
58 // OS/2 cmd.exe has another use for '&'
59 string sh = OnlyFilename(GetEnvPath("EMXSHELL"));
61 // COMSPEC is set, unless user unsets
62 sh = OnlyFilename(GetEnvPath("COMSPEC"));
66 sh = lowercase(sh); // DosMapCase() is an overkill here
67 if (contains(sh, "cmd.exe") || contains(sh, "4os2.exe"))
73 static bool initialized = false;
78 ULONG CPList[3] = {0};
80 APIRET rc = DosQueryCp(3 * sizeof(ULONG), CPList, &CPList_size);
83 // CPList[0] == process current codepage,
84 // CPList[1] == system default codepage, the rest are auxilary.
85 // Once cp_ is correctly set, you can call other routines.
90 void warn(string const & /*mesg*/)
101 rc = DosQueryCurrentDisk(&drv_num, &drv_map);
104 char drive = 'A' + drv_num -1;
105 string tmp(1, drive);
111 string::size_type common_path(string const & p1, string const & p2)
113 static bool initialized = false;
121 cntry.codepage = cp_;
122 string temp1 = slashify_path(p1);
123 string temp2 = slashify_path(p2);
124 char * tmp1 = const_cast<char *> (temp1.c_str());
125 char * tmp2 = const_cast<char *> (temp2.c_str());
126 /* rc = */ DosMapCase(p1.length(), &cntry, tmp1);
127 // if (rc != NO_ERROR)
129 /* rc = */ DosMapCase(p2.length(), &cntry, tmp2);
130 // if (rc != NO_ERROR)
132 // This algorithm works only if paths are slashified on DBCS systems.
133 string::size_type i = 0;
134 string::size_type p1_len = p1.length();
135 string::size_type p2_len = p2.length();
136 while (i < p1_len && i < p2_len && tmp1[i] == tmp2[i])
138 if ((i < p1_len && i < p2_len)
139 || (i < p1_len && tmp1[i] != '/' && i == p2_len)
140 || (i < p2_len && tmp2[i] != '/' && i == p1_len))
143 --i; // here was the last match
144 while (i && tmp1[i] != '/')
151 string slashify_path(string const & p)
153 static bool initialized = false;
154 static bool leadbyte[256] = {false};
159 cntry.codepage = cp_;
160 unsigned char *DBCSinfo = new unsigned char[12];
161 /* rc = */ DosQueryDBCSEnv(12, &cntry, (char*) DBCSinfo);
162 // if (rc != NO_ERROR)
164 for (int j = 1; j < 12; j += 2)
167 bool isLeadByte = false;
168 while (*DBCSinfo != 0) {
169 if (i == *DBCSinfo) {
170 isLeadByte = !isLeadByte;
173 leadbyte[i++] = isLeadByte;
177 string::iterator lit = p.begin();
178 string::iterator end = p.end();
179 for (; lit < end; ++lit) {
180 if (leadbyte[(*lit)])
185 p = subst(p, "//", "/");
190 string external_path(string const & p)
196 string internal_path(string const & p)
202 bool is_absolute_path(string const & p)
204 return (p.length() > 1
205 && isalpha(static_cast<unsigned char>(p[0]))
210 // returns a string suitable to be passed to popen when
212 char const * popen_read_mode()
230 void setTmpDir(string const & p)
247 } // end namespace os