]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: src/msdos/utils.cpp | |
3 | // Purpose: DOS implementations of utility functions | |
4 | // Author: Vaclav Slavik, M.J.Wetherell | |
5 | // Id: $Id$ | |
6 | // Copyright: (c) 2001-2002 SciTech Software, Inc. (www.scitechsoft.com) | |
7 | // (c) 2005 M.J.Wetherell | |
8 | // Licence: wxWindows licence | |
9 | ///////////////////////////////////////////////////////////////////////////// | |
10 | ||
11 | // For compilers that support precompilation, includes "wx.h". | |
12 | #include "wx/wxprec.h" | |
13 | ||
14 | #ifdef __BORLANDC__ | |
15 | #pragma hdrstop | |
16 | #endif | |
17 | ||
18 | #include "wx/utils.h" | |
19 | ||
20 | #ifndef WX_PRECOMP | |
21 | #include "wx/string.h" | |
22 | #include "wx/intl.h" | |
23 | #include "wx/log.h" | |
24 | #include "wx/app.h" | |
25 | #endif | |
26 | ||
27 | #include "wx/apptrait.h" | |
28 | #include "wx/process.h" | |
29 | #include "wx/confbase.h" // for wxExpandEnvVars() | |
30 | #include "wx/cmdline.h" | |
31 | #include "wx/filename.h" | |
32 | #include "wx/wfstream.h" | |
33 | ||
34 | #include <stdarg.h> | |
35 | #include <string.h> | |
36 | #include <sys/stat.h> | |
37 | #include <sys/types.h> | |
38 | #include <unistd.h> | |
39 | #include <signal.h> | |
40 | #include <time.h> | |
41 | #include <dos.h> | |
42 | #include <process.h> | |
43 | ||
44 | //---------------------------------------------------------------------------- | |
45 | // Sleep | |
46 | //---------------------------------------------------------------------------- | |
47 | ||
48 | void wxSleep(int nSecs) | |
49 | { | |
50 | wxMilliSleep(1000 * nSecs); | |
51 | } | |
52 | ||
53 | void wxMilliSleep(unsigned long milliseconds) | |
54 | { | |
55 | #if HAVE_USLEEP || defined __DJGPP__ | |
56 | usleep(milliseconds * 1000); | |
57 | #elif defined __WATCOMC__ | |
58 | delay(milliseconds); | |
59 | #else | |
60 | clock_t start = clock(); | |
61 | while ((clock() - start) * 1000 / CLOCKS_PER_SEC < (clock_t)milliseconds) | |
62 | { | |
63 | // yield if in a multitasking environment | |
64 | // "Release Current Virtual Machine's Time Slice" in DPMI 1.0 | |
65 | REGS r; | |
66 | memset(&r, 0, sizeof(r)); | |
67 | r.x.ax = 0x1680; | |
68 | int386(0x2f, &r, &r); | |
69 | } | |
70 | #endif | |
71 | } | |
72 | ||
73 | void wxMicroSleep(unsigned long microseconds) | |
74 | { | |
75 | #if HAVE_USLEEP || defined __DJGPP__ | |
76 | usleep(microseconds); | |
77 | #else | |
78 | wxMilliSleep(microseconds/1000); | |
79 | #endif | |
80 | } | |
81 | ||
82 | //---------------------------------------------------------------------------- | |
83 | // Get/Set environment variables | |
84 | //---------------------------------------------------------------------------- | |
85 | ||
86 | bool wxGetEnv(const wxString& var, wxString *value) | |
87 | { | |
88 | // wxGetenv is defined as getenv() | |
89 | wxChar *p = wxGetenv(var); | |
90 | if ( !p ) | |
91 | return false; | |
92 | ||
93 | if ( value ) | |
94 | *value = p; | |
95 | ||
96 | return true; | |
97 | } | |
98 | ||
99 | static bool wxDoSetEnv(const wxString& variable, const char *value) | |
100 | { | |
101 | wxString s = variable; | |
102 | if ( value ) | |
103 | s << wxT('=') << value; | |
104 | ||
105 | // transform to ANSI | |
106 | const char *p = s.mb_str(); | |
107 | ||
108 | // the string will be free()d by libc | |
109 | char *buf = (char *)malloc(strlen(p) + 1); | |
110 | strcpy(buf, p); | |
111 | ||
112 | return putenv(buf) == 0; | |
113 | } | |
114 | ||
115 | bool wxSetEnv(const wxString& variable, const wxString& value) | |
116 | { | |
117 | return wxDoSetEnv(variable, value.mb_str()); | |
118 | } | |
119 | ||
120 | bool wxUnsetEnv(const wxString& variable) | |
121 | { | |
122 | return wxDoSetEnv(variable, NULL); | |
123 | } | |
124 | ||
125 | ||
126 | //---------------------------------------------------------------------------- | |
127 | // Hostname, username, home directory | |
128 | //---------------------------------------------------------------------------- | |
129 | ||
130 | // Based on the MSW implementation | |
131 | // | |
132 | // Respects the following environment variables in this order: %HomeDrive% + | |
133 | // %HomePath%, %UserProfile%, $HOME. Otherwise takes program's directory if | |
134 | // wxApp has been initialised, otherwise returns ".". | |
135 | // | |
136 | const wxChar* wxGetHomeDir(wxString *home) | |
137 | { | |
138 | wxString& strDir = *home; | |
139 | ||
140 | strDir.clear(); | |
141 | ||
142 | // try HOMEDRIVE/PATH | |
143 | const wxChar *szHome = wxGetenv(wxT("HOMEDRIVE")); | |
144 | if ( szHome != NULL ) | |
145 | strDir << szHome; | |
146 | szHome = wxGetenv(wxT("HOMEPATH")); | |
147 | ||
148 | if ( szHome != NULL ) | |
149 | { | |
150 | strDir << szHome; | |
151 | ||
152 | // the idea is that under NT these variables have default values of | |
153 | // "%systemdrive%:" and "\\". As we don't want to create our config | |
154 | // files in the root directory of the system drive, we will create it | |
155 | // in our program's dir. However, if the user took care to set | |
156 | // HOMEPATH to something other than "\\", we suppose that he knows | |
157 | // what he is doing and use the supplied value. | |
158 | if ( wxStrcmp(szHome, wxT("\\")) == 0 ) | |
159 | strDir.clear(); | |
160 | } | |
161 | ||
162 | if ( strDir.empty() ) | |
163 | { | |
164 | // If we have a valid USERPROFILE directory, as is the case in | |
165 | // Windows NT, 2000 and XP, we should use that as our home directory. | |
166 | szHome = wxGetenv(wxT("USERPROFILE")); | |
167 | ||
168 | if ( szHome != NULL ) | |
169 | strDir = szHome; | |
170 | } | |
171 | ||
172 | if ( strDir.empty() ) | |
173 | { | |
174 | // If we have a valid HOME directory, as is used on many machines | |
175 | // that have unix utilities on them, we should use that. | |
176 | szHome = wxGetenv(wxT("HOME")); | |
177 | ||
178 | if ( szHome != NULL ) | |
179 | { | |
180 | strDir = szHome; | |
181 | // when msys sets %HOME% it uses '/' (cygwin uses '\\') | |
182 | strDir.Replace(wxT("/"), wxT("\\")); | |
183 | } | |
184 | } | |
185 | ||
186 | if ( !strDir.empty() ) | |
187 | { | |
188 | // sometimes the value of HOME may be "%USERPROFILE%", so reexpand the | |
189 | // value once again, it shouldn't hurt anyhow | |
190 | strDir = wxExpandEnvVars(strDir); | |
191 | } | |
192 | else // fall back to the program directory | |
193 | { | |
194 | if ( wxTheApp ) | |
195 | { | |
196 | wxString prog(wxTheApp->argv[0]); | |
197 | #ifdef __DJGPP__ | |
198 | // djgpp startup code switches the slashes around, so restore them | |
199 | prog.Replace(wxT("/"), wxT("\\")); | |
200 | #endif | |
201 | // it needs to be a full path to be usable | |
202 | if ( prog.compare(1, 2, wxT(":\\")) == 0 ) | |
203 | wxFileName::SplitPath(prog, &strDir, NULL, NULL); | |
204 | } | |
205 | if ( strDir.empty() ) | |
206 | { | |
207 | strDir = wxT("."); | |
208 | } | |
209 | } | |
210 | ||
211 | return strDir.c_str(); | |
212 | } | |
213 | ||
214 | wxString wxGetUserHome(const wxString& user) | |
215 | { | |
216 | wxString home; | |
217 | ||
218 | if (user.empty() || user == wxGetUserId()) | |
219 | wxGetHomeDir(&home); | |
220 | ||
221 | return home; | |
222 | } | |
223 | ||
224 | // returns %UserName%, $USER or just "user" | |
225 | // | |
226 | bool wxGetUserId(wxChar *buf, int n) | |
227 | { | |
228 | const wxChar *user = wxGetenv(wxT("UserName")); | |
229 | ||
230 | if (!user) | |
231 | user = wxGetenv(wxT("USER")); | |
232 | ||
233 | if (!user) | |
234 | user = wxT("user"); | |
235 | ||
236 | wxStrlcpy(buf, user, n); | |
237 | return true; | |
238 | } | |
239 | ||
240 | bool wxGetUserName(wxChar *buf, int n) | |
241 | { | |
242 | return wxGetUserId(buf, n); | |
243 | } | |
244 | ||
245 | // returns %ComputerName%, or $HOSTNAME, or "host" | |
246 | // | |
247 | bool wxGetHostName(wxChar *buf, int n) | |
248 | { | |
249 | const wxChar *host = wxGetenv(wxT("ComputerName")); | |
250 | ||
251 | if (!host) | |
252 | host = wxGetenv(wxT("HOSTNAME")); | |
253 | ||
254 | if (!host) | |
255 | host = wxT("host"); | |
256 | ||
257 | wxStrlcpy(buf, host, n); | |
258 | return true; | |
259 | } | |
260 | ||
261 | // adds %UserDnsDomain% to wxGetHostName() | |
262 | // | |
263 | bool wxGetFullHostName(wxChar *buf, int n) | |
264 | { | |
265 | wxGetHostName(buf, n); | |
266 | ||
267 | const wxChar *domain = wxGetenv(wxT("UserDnsDomain")); | |
268 | ||
269 | if (domain) | |
270 | wxStrncat(wxStrncat(buf, wxT("."), n), domain, n); | |
271 | ||
272 | return true; | |
273 | } | |
274 | ||
275 | //---------------------------------------------------------------------------- | |
276 | // Processes | |
277 | //---------------------------------------------------------------------------- | |
278 | ||
279 | unsigned long wxGetProcessId() | |
280 | { | |
281 | return (unsigned long)getpid(); | |
282 | } | |
283 | ||
284 | int wxKill(long pid, wxSignal sig, wxKillError *rc, int WXUNUSED(flags)) | |
285 | { | |
286 | int result = -1; | |
287 | ||
288 | if (pid != (long)wxGetProcessId()) | |
289 | { | |
290 | result = raise(sig); | |
291 | if (rc) | |
292 | *rc = result == 0 ? wxKILL_OK : wxKILL_BAD_SIGNAL; | |
293 | } | |
294 | else | |
295 | { | |
296 | wxLogDebug(wxT("wxKill can only send signals to the current process under MSDOS")); | |
297 | if (rc) | |
298 | *rc = wxKILL_NO_PROCESS; | |
299 | } | |
300 | ||
301 | return result; | |
302 | } | |
303 | ||
304 | bool wxShell(const wxString& command /*=wxEmptyString*/) | |
305 | { | |
306 | // FIXME: suspend/resume gui | |
307 | int result = system(command); | |
308 | ||
309 | if (result == -1) | |
310 | { | |
311 | wxLogSysError(_("can't execute '%s'"), command.c_str()); | |
312 | } | |
313 | ||
314 | return result == 0; | |
315 | } | |
316 | ||
317 | long wxExecute(const wxString& command, int flags, wxProcess *process) | |
318 | { | |
319 | // FIXME: shouldn't depend on wxCmdLineParser | |
320 | wxArrayString args(wxCmdLineParser::ConvertStringToArgs(command)); | |
321 | size_t n = args.size(); | |
322 | wxChar **argv = new wxChar*[n + 1]; | |
323 | ||
324 | argv[n] = NULL; | |
325 | while (n-- > 0) | |
326 | argv[n] = const_cast<wxChar*>((const char *)args[n].c_str()); | |
327 | ||
328 | long result = wxExecute(argv, flags, process); | |
329 | ||
330 | delete [] argv; | |
331 | return result; | |
332 | } | |
333 | ||
334 | #if wxUSE_STREAMS | |
335 | ||
336 | // A wxFFileInputStream that deletes the file in it's destructor | |
337 | // | |
338 | class wxTempFileInStream : public wxFFileInputStream | |
339 | { | |
340 | public: | |
341 | wxTempFileInStream(const wxString& name) | |
342 | : wxFFileInputStream(name, wxT("rt")) | |
343 | { } | |
344 | ||
345 | virtual ~wxTempFileInStream() | |
346 | { | |
347 | m_file->Close(); | |
348 | wxRemoveFile(m_file->GetName()); | |
349 | } | |
350 | }; | |
351 | ||
352 | // A file descriptor that can be redirected to a file | |
353 | // | |
354 | class wxRedirectableFd | |
355 | { | |
356 | public: | |
357 | wxRedirectableFd(int fd) : m_fd(fd), m_dup(-1) { } | |
358 | ~wxRedirectableFd(); | |
359 | ||
360 | // Redirect the descriptor to a file, similar to ANSI C's freopen, but | |
361 | // for low level descriptors. The desctructor un-redirects. If O_CREAT | |
362 | // is in the flags then the destructor will delete the file unless it is | |
363 | // given away with Release(). | |
364 | bool Reopen(const wxString& name, int flags); | |
365 | ||
366 | // un-redirect the redirected file descriptor, closing the file, and give | |
367 | // away the filename without deleting it | |
368 | wxString Release(); | |
369 | ||
370 | private: | |
371 | // un-redirect the descriptor, closing the file | |
372 | void Restore(); | |
373 | ||
374 | int m_fd; | |
375 | int m_dup; | |
376 | wxString m_name; | |
377 | }; | |
378 | ||
379 | wxRedirectableFd::~wxRedirectableFd() | |
380 | { | |
381 | Restore(); | |
382 | if (!m_name.empty()) | |
383 | wxRemoveFile(m_name); | |
384 | } | |
385 | ||
386 | bool wxRedirectableFd::Reopen(const wxString& name, int flags) | |
387 | { | |
388 | wxASSERT(m_dup == -1); | |
389 | bool result = false; | |
390 | ||
391 | // save a duplicate so that the descriptor can be closed now and | |
392 | // restored later | |
393 | m_dup = dup(m_fd); | |
394 | ||
395 | if (m_dup != -1) | |
396 | { | |
397 | int tmp = open(name.mb_str(), flags); | |
398 | ||
399 | if (tmp != -1) | |
400 | { | |
401 | close(m_fd); | |
402 | ||
403 | if (flags & O_CREAT) | |
404 | m_name = name; | |
405 | ||
406 | result = dup2(tmp, m_fd) == m_fd; | |
407 | close(tmp); | |
408 | } | |
409 | } | |
410 | ||
411 | if (!result) | |
412 | { | |
413 | wxLogSysError(_("error opening '%s'"), name.c_str()); | |
414 | } | |
415 | ||
416 | return result; | |
417 | } | |
418 | ||
419 | void wxRedirectableFd::Restore() | |
420 | { | |
421 | if (m_dup != -1) | |
422 | { | |
423 | close(m_fd); | |
424 | dup2(m_dup, m_fd); | |
425 | close(m_dup); | |
426 | m_dup = -1; | |
427 | } | |
428 | } | |
429 | ||
430 | wxString wxRedirectableFd::Release() | |
431 | { | |
432 | Restore(); | |
433 | wxString name = m_name; | |
434 | m_name.clear(); | |
435 | return name; | |
436 | } | |
437 | ||
438 | #endif // wxUSE_STREAMS | |
439 | ||
440 | // wxExecute implementation | |
441 | // | |
442 | long wxExecute(wxChar **argv, int flags, wxProcess *process) | |
443 | { | |
444 | #if wxUSE_STREAMS | |
445 | const int STDIN = 0; | |
446 | const int STDOUT = 1; | |
447 | const int STDERR = 2; | |
448 | ||
449 | wxRedirectableFd in(STDIN), out(STDOUT), err(STDERR); | |
450 | bool redirect = process && process->IsRedirected() && (flags & wxEXEC_SYNC); | |
451 | ||
452 | if (redirect) | |
453 | { | |
454 | // close stdin/out/err and reopen them as files | |
455 | if (!in.Reopen(wxT("NUL"), O_RDONLY | O_TEXT)) | |
456 | return -1; | |
457 | ||
458 | if (!out.Reopen(wxFileName::CreateTempFileName(wxT("out")), | |
459 | O_CREAT | O_WRONLY | O_TRUNC | O_TEXT)) | |
460 | return -1; | |
461 | ||
462 | if (!err.Reopen(wxFileName::CreateTempFileName(wxT("err")), | |
463 | O_CREAT | O_WRONLY | O_TRUNC | O_TEXT)) | |
464 | return -1; | |
465 | } | |
466 | #endif // wxUSE_STREAMS | |
467 | ||
468 | // FIXME: suspend/resume gui | |
469 | int mode = flags & wxEXEC_SYNC ? P_WAIT : P_NOWAIT; | |
470 | int result = spawnvp(mode, argv[0], argv); | |
471 | ||
472 | if (result == -1) | |
473 | { | |
474 | wxLogSysError(_("can't execute '%s'"), argv[0]); | |
475 | } | |
476 | ||
477 | #if wxUSE_STREAMS | |
478 | if (redirect) | |
479 | process->SetPipeStreams(new wxTempFileInStream(out.Release()), | |
480 | new wxFFileOutputStream(wxT("NUL"), wxT("wt")), | |
481 | new wxTempFileInStream(err.Release())); | |
482 | #endif // wxUSE_STREAMS | |
483 | ||
484 | return result; | |
485 | } | |
486 | ||
487 | ||
488 | //---------------------------------------------------------------------------- | |
489 | // OS-related | |
490 | //---------------------------------------------------------------------------- | |
491 | ||
492 | wxString wxGetOsDescription() | |
493 | { | |
494 | wxString osname(wxT("DOS")); | |
495 | return osname; | |
496 | } | |
497 | ||
498 | wxOperatingSystemId wxGetOsVersion(int *verMaj, int *verMin) | |
499 | { | |
500 | if ( verMaj ) | |
501 | *verMaj = _osmajor; | |
502 | if ( verMin ) | |
503 | *verMin = _osminor; | |
504 | ||
505 | return wxOS_DOS; | |
506 | } | |
507 | ||
508 | bool wxIsPlatform64Bit() | |
509 | { | |
510 | return false; | |
511 | } | |
512 |