]>
Commit | Line | Data |
---|---|---|
518b5d2f | 1 | ///////////////////////////////////////////////////////////////////////////// |
79066131 | 2 | // Name: unix/utilsunx.cpp |
518b5d2f VZ |
3 | // Purpose: generic Unix implementation of many wx functions |
4 | // Author: Vadim Zeitlin | |
5 | // Id: $Id$ | |
6 | // Copyright: (c) 1998 Robert Roebling, Vadim Zeitlin | |
65571936 | 7 | // Licence: wxWindows licence |
518b5d2f VZ |
8 | ///////////////////////////////////////////////////////////////////////////// |
9 | ||
10 | // ============================================================================ | |
11 | // declarations | |
12 | // ============================================================================ | |
13 | ||
14 | // ---------------------------------------------------------------------------- | |
15 | // headers | |
16 | // ---------------------------------------------------------------------------- | |
17 | ||
14f355c2 VS |
18 | // for compilers that support precompilation, includes "wx.h". |
19 | #include "wx/wxprec.h" | |
20 | ||
518b5d2f VZ |
21 | #include "wx/defs.h" |
22 | #include "wx/string.h" | |
23 | ||
24 | #include "wx/intl.h" | |
25 | #include "wx/log.h" | |
a37a5a73 | 26 | #include "wx/app.h" |
46446cc2 | 27 | #include "wx/apptrait.h" |
518b5d2f VZ |
28 | |
29 | #include "wx/utils.h" | |
30 | #include "wx/process.h" | |
bdc72a22 | 31 | #include "wx/thread.h" |
518b5d2f | 32 | |
80d6dc0a | 33 | #include "wx/wfstream.h" |
8b33ae2d | 34 | |
e2478fde | 35 | #include "wx/unix/execute.h" |
17a1ebd1 VZ |
36 | #include "wx/unix/private.h" |
37 | ||
38 | #include <pwd.h> | |
e2478fde | 39 | |
2887179b VZ |
40 | #if wxUSE_STREAMS |
41 | ||
42 | // define this to let wxexec.cpp know that we know what we're doing | |
43 | #define _WX_USED_BY_WXEXECUTE_ | |
44 | #include "../common/execcmn.cpp" | |
45 | ||
46 | #endif // wxUSE_STREAMS | |
47 | ||
ec67cff1 | 48 | #if wxUSE_BASE |
e2478fde | 49 | |
74c719ed VZ |
50 | #if defined(__MWERKS__) && defined(__MACH__) |
51 | #ifndef WXWIN_OS_DESCRIPTION | |
52 | #define WXWIN_OS_DESCRIPTION "MacOS X" | |
53 | #endif | |
54 | #ifndef HAVE_NANOSLEEP | |
55 | #define HAVE_NANOSLEEP | |
56 | #endif | |
57 | #ifndef HAVE_UNAME | |
58 | #define HAVE_UNAME | |
59 | #endif | |
60 | ||
61 | // our configure test believes we can use sigaction() if the function is | |
62 | // available but Metrowekrs with MSL run-time does have the function but | |
63 | // doesn't have sigaction struct so finally we can't use it... | |
64 | #ifdef __MSL__ | |
65 | #undef wxUSE_ON_FATAL_EXCEPTION | |
66 | #define wxUSE_ON_FATAL_EXCEPTION 0 | |
67 | #endif | |
8d4f85ce SC |
68 | #endif |
69 | ||
85da04e9 VZ |
70 | // not only the statfs syscall is called differently depending on platform, but |
71 | // one of its incarnations, statvfs(), takes different arguments under | |
72 | // different platforms and even different versions of the same system (Solaris | |
73 | // 7 and 8): if you want to test for this, don't forget that the problems only | |
74 | // appear if the large files support is enabled | |
eadd7bd2 | 75 | #ifdef HAVE_STATFS |
85da04e9 VZ |
76 | #ifdef __BSD__ |
77 | #include <sys/param.h> | |
78 | #include <sys/mount.h> | |
79 | #else // !__BSD__ | |
80 | #include <sys/vfs.h> | |
81 | #endif // __BSD__/!__BSD__ | |
82 | ||
83 | #define wxStatfs statfs | |
84ae7ca4 VZ |
84 | |
85 | #ifndef HAVE_STATFS_DECL | |
86 | // some systems lack statfs() prototype in the system headers (AIX 4) | |
87 | extern "C" int statfs(const char *path, struct statfs *buf); | |
88 | #endif | |
eadd7bd2 VZ |
89 | #endif // HAVE_STATFS |
90 | ||
9952adac VZ |
91 | #ifdef HAVE_STATVFS |
92 | #include <sys/statvfs.h> | |
93 | ||
85da04e9 VZ |
94 | #define wxStatfs statvfs |
95 | #endif // HAVE_STATVFS | |
96 | ||
97 | #if defined(HAVE_STATFS) || defined(HAVE_STATVFS) | |
98 | // WX_STATFS_T is detected by configure | |
99 | #define wxStatfs_t WX_STATFS_T | |
100 | #endif | |
9952adac | 101 | |
f6bcfd97 BP |
102 | // SGI signal.h defines signal handler arguments differently depending on |
103 | // whether _LANGUAGE_C_PLUS_PLUS is set or not - do set it | |
104 | #if defined(__SGI__) && !defined(_LANGUAGE_C_PLUS_PLUS) | |
105 | #define _LANGUAGE_C_PLUS_PLUS 1 | |
106 | #endif // SGI hack | |
107 | ||
518b5d2f VZ |
108 | #include <stdarg.h> |
109 | #include <dirent.h> | |
110 | #include <string.h> | |
111 | #include <sys/stat.h> | |
112 | #include <sys/types.h> | |
518b5d2f | 113 | #include <sys/wait.h> |
e2478fde | 114 | #include <unistd.h> |
518b5d2f VZ |
115 | #include <errno.h> |
116 | #include <netdb.h> | |
117 | #include <signal.h> | |
118 | #include <fcntl.h> // for O_WRONLY and friends | |
119 | #include <time.h> // nanosleep() and/or usleep() | |
fad866f4 | 120 | #include <ctype.h> // isspace() |
b12915c1 | 121 | #include <sys/time.h> // needed for FD_SETSIZE |
7bcb11d3 | 122 | |
0fcdf6dc | 123 | #ifdef HAVE_UNAME |
518b5d2f VZ |
124 | #include <sys/utsname.h> // for uname() |
125 | #endif // HAVE_UNAME | |
126 | ||
127 | // ---------------------------------------------------------------------------- | |
128 | // conditional compilation | |
129 | // ---------------------------------------------------------------------------- | |
130 | ||
131 | // many versions of Unices have this function, but it is not defined in system | |
132 | // headers - please add your system here if it is the case for your OS. | |
133 | // SunOS < 5.6 (i.e. Solaris < 2.6) and DG-UX are like this. | |
1363811b | 134 | #if !defined(HAVE_USLEEP) && \ |
d67fee71 | 135 | ((defined(__SUN__) && !defined(__SunOs_5_6) && \ |
518b5d2f | 136 | !defined(__SunOs_5_7) && !defined(__SUNPRO_CC)) || \ |
d67fee71 | 137 | defined(__osf__) || defined(__EMX__)) |
518b5d2f VZ |
138 | extern "C" |
139 | { | |
1363811b VZ |
140 | #ifdef __SUN__ |
141 | int usleep(unsigned int usec); | |
142 | #else // !Sun | |
bdc72a22 VZ |
143 | #ifdef __EMX__ |
144 | /* I copied this from the XFree86 diffs. AV. */ | |
145 | #define INCL_DOSPROCESS | |
146 | #include <os2.h> | |
147 | inline void usleep(unsigned long delay) | |
148 | { | |
149 | DosSleep(delay ? (delay/1000l) : 1l); | |
150 | } | |
151 | #else // !Sun && !EMX | |
152 | void usleep(unsigned long usec); | |
153 | #endif | |
e6daf794 | 154 | #endif // Sun/EMX/Something else |
518b5d2f | 155 | }; |
bdc72a22 VZ |
156 | |
157 | #define HAVE_USLEEP 1 | |
518b5d2f VZ |
158 | #endif // Unices without usleep() |
159 | ||
518b5d2f VZ |
160 | // ============================================================================ |
161 | // implementation | |
162 | // ============================================================================ | |
163 | ||
164 | // ---------------------------------------------------------------------------- | |
165 | // sleeping | |
166 | // ---------------------------------------------------------------------------- | |
167 | ||
168 | void wxSleep(int nSecs) | |
169 | { | |
170 | sleep(nSecs); | |
171 | } | |
172 | ||
66cd9d7f | 173 | void wxMicroSleep(unsigned long microseconds) |
518b5d2f | 174 | { |
b12915c1 | 175 | #if defined(HAVE_NANOSLEEP) |
518b5d2f | 176 | timespec tmReq; |
66cd9d7f VZ |
177 | tmReq.tv_sec = (time_t)(microseconds / 1000000); |
178 | tmReq.tv_nsec = (microseconds % 1000000) * 1000; | |
518b5d2f VZ |
179 | |
180 | // we're not interested in remaining time nor in return value | |
181 | (void)nanosleep(&tmReq, (timespec *)NULL); | |
b12915c1 | 182 | #elif defined(HAVE_USLEEP) |
518b5d2f VZ |
183 | // uncomment this if you feel brave or if you are sure that your version |
184 | // of Solaris has a safe usleep() function but please notice that usleep() | |
185 | // is known to lead to crashes in MT programs in Solaris 2.[67] and is not | |
186 | // documented as MT-Safe | |
ea18eed9 | 187 | #if defined(__SUN__) && wxUSE_THREADS |
518b5d2f VZ |
188 | #error "usleep() cannot be used in MT programs under Solaris." |
189 | #endif // Sun | |
190 | ||
66cd9d7f | 191 | usleep(microseconds); |
b12915c1 VZ |
192 | #elif defined(HAVE_SLEEP) |
193 | // under BeOS sleep() takes seconds (what about other platforms, if any?) | |
66cd9d7f | 194 | sleep(microseconds * 1000000); |
518b5d2f | 195 | #else // !sleep function |
66cd9d7f | 196 | #error "usleep() or nanosleep() function required for wxMicroSleep" |
518b5d2f VZ |
197 | #endif // sleep function |
198 | } | |
199 | ||
66cd9d7f VZ |
200 | void wxMilliSleep(unsigned long milliseconds) |
201 | { | |
202 | wxMicroSleep(milliseconds*1000); | |
203 | } | |
204 | ||
518b5d2f VZ |
205 | // ---------------------------------------------------------------------------- |
206 | // process management | |
207 | // ---------------------------------------------------------------------------- | |
208 | ||
e0f6b731 | 209 | int wxKill(long pid, wxSignal sig, wxKillError *rc, int flags) |
518b5d2f | 210 | { |
e0f6b731 | 211 | int err = kill((pid_t) (flags & wxKILL_CHILDREN) ? -pid : pid, (int)sig); |
50567b69 VZ |
212 | if ( rc ) |
213 | { | |
f0bce4d4 | 214 | switch ( err ? errno : 0 ) |
50567b69 VZ |
215 | { |
216 | case 0: | |
217 | *rc = wxKILL_OK; | |
218 | break; | |
219 | ||
220 | case EINVAL: | |
221 | *rc = wxKILL_BAD_SIGNAL; | |
222 | break; | |
223 | ||
224 | case EPERM: | |
225 | *rc = wxKILL_ACCESS_DENIED; | |
226 | break; | |
227 | ||
228 | case ESRCH: | |
229 | *rc = wxKILL_NO_PROCESS; | |
230 | break; | |
231 | ||
232 | default: | |
233 | // this goes against Unix98 docs so log it | |
234 | wxLogDebug(_T("unexpected kill(2) return value %d"), err); | |
235 | ||
236 | // something else... | |
237 | *rc = wxKILL_ERROR; | |
238 | } | |
239 | } | |
240 | ||
241 | return err; | |
518b5d2f VZ |
242 | } |
243 | ||
fad866f4 KB |
244 | #define WXEXECUTE_NARGS 127 |
245 | ||
62705a27 RN |
246 | #if defined(__DARWIN__) |
247 | long wxMacExecute(wxChar **argv, | |
248 | int flags, | |
249 | wxProcess *process); | |
250 | #endif | |
251 | ||
fbf456aa | 252 | long wxExecute( const wxString& command, int flags, wxProcess *process ) |
518b5d2f | 253 | { |
8ea92b4d | 254 | wxCHECK_MSG( !command.empty(), 0, wxT("can't exec empty command") ); |
3bdb628b | 255 | wxLogDebug(wxString(wxT("Launching: ")) + command); |
518b5d2f | 256 | |
647b8e37 VZ |
257 | #if wxUSE_THREADS |
258 | // fork() doesn't mix well with POSIX threads: on many systems the program | |
259 | // deadlocks or crashes for some reason. Probably our code is buggy and | |
260 | // doesn't do something which must be done to allow this to work, but I | |
261 | // don't know what yet, so for now just warn the user (this is the least we | |
262 | // can do) about it | |
263 | wxASSERT_MSG( wxThread::IsMain(), | |
264 | _T("wxExecute() can be called only from the main thread") ); | |
265 | #endif // wxUSE_THREADS | |
266 | ||
518b5d2f | 267 | int argc = 0; |
05079acc | 268 | wxChar *argv[WXEXECUTE_NARGS]; |
fad866f4 | 269 | wxString argument; |
05079acc | 270 | const wxChar *cptr = command.c_str(); |
223d09f6 | 271 | wxChar quotechar = wxT('\0'); // is arg quoted? |
9d8aca48 | 272 | bool escaped = false; |
518b5d2f | 273 | |
0ed9a934 | 274 | // split the command line in arguments |
fad866f4 KB |
275 | do |
276 | { | |
223d09f6 KB |
277 | argument=wxT(""); |
278 | quotechar = wxT('\0'); | |
0ed9a934 | 279 | |
fad866f4 | 280 | // eat leading whitespace: |
05079acc | 281 | while ( wxIsspace(*cptr) ) |
fad866f4 | 282 | cptr++; |
0ed9a934 | 283 | |
223d09f6 | 284 | if ( *cptr == wxT('\'') || *cptr == wxT('"') ) |
fad866f4 | 285 | quotechar = *cptr++; |
0ed9a934 | 286 | |
fad866f4 KB |
287 | do |
288 | { | |
223d09f6 | 289 | if ( *cptr == wxT('\\') && ! escaped ) |
fad866f4 | 290 | { |
9d8aca48 | 291 | escaped = true; |
fad866f4 KB |
292 | cptr++; |
293 | continue; | |
294 | } | |
0ed9a934 | 295 | |
fad866f4 | 296 | // all other characters: |
0ed9a934 | 297 | argument += *cptr++; |
9d8aca48 | 298 | escaped = false; |
0ed9a934 VZ |
299 | |
300 | // have we reached the end of the argument? | |
301 | if ( (*cptr == quotechar && ! escaped) | |
223d09f6 KB |
302 | || (quotechar == wxT('\0') && wxIsspace(*cptr)) |
303 | || *cptr == wxT('\0') ) | |
fad866f4 | 304 | { |
0ed9a934 | 305 | wxASSERT_MSG( argc < WXEXECUTE_NARGS, |
223d09f6 | 306 | wxT("too many arguments in wxExecute") ); |
0ed9a934 | 307 | |
05079acc OK |
308 | argv[argc] = new wxChar[argument.length() + 1]; |
309 | wxStrcpy(argv[argc], argument.c_str()); | |
fad866f4 | 310 | argc++; |
0ed9a934 | 311 | |
fad866f4 | 312 | // if not at end of buffer, swallow last character: |
0ed9a934 VZ |
313 | if(*cptr) |
314 | cptr++; | |
315 | ||
fad866f4 KB |
316 | break; // done with this one, start over |
317 | } | |
0ed9a934 VZ |
318 | } while(*cptr); |
319 | } while(*cptr); | |
fad866f4 | 320 | argv[argc] = NULL; |
0ed9a934 | 321 | |
62705a27 RN |
322 | long lRc; |
323 | #if defined(__DARWIN__) | |
3afdfec4 | 324 | // wxMacExecute only executes app bundles. |
fb19fbab SC |
325 | // It returns an error code if the target is not an app bundle, thus falling |
326 | // through to the regular wxExecute for non app bundles. | |
3afdfec4 | 327 | lRc = wxMacExecute(argv, flags, process); |
fb19fbab | 328 | if( lRc != ((flags & wxEXEC_SYNC) ? -1 : 0)) |
62705a27 RN |
329 | return lRc; |
330 | #endif | |
331 | ||
0ed9a934 | 332 | // do execute the command |
62705a27 | 333 | lRc = wxExecute(argv, flags, process); |
518b5d2f | 334 | |
0ed9a934 | 335 | // clean up |
fad866f4 | 336 | argc = 0; |
0ed9a934 | 337 | while( argv[argc] ) |
fad866f4 | 338 | delete [] argv[argc++]; |
518b5d2f VZ |
339 | |
340 | return lRc; | |
341 | } | |
342 | ||
2c8e4738 VZ |
343 | // ---------------------------------------------------------------------------- |
344 | // wxShell | |
345 | // ---------------------------------------------------------------------------- | |
346 | ||
347 | static wxString wxMakeShellCommand(const wxString& command) | |
518b5d2f VZ |
348 | { |
349 | wxString cmd; | |
cd6ce4a9 | 350 | if ( !command ) |
2c8e4738 VZ |
351 | { |
352 | // just an interactive shell | |
cd6ce4a9 | 353 | cmd = _T("xterm"); |
2c8e4738 | 354 | } |
518b5d2f | 355 | else |
2c8e4738 VZ |
356 | { |
357 | // execute command in a shell | |
358 | cmd << _T("/bin/sh -c '") << command << _T('\''); | |
359 | } | |
360 | ||
361 | return cmd; | |
362 | } | |
363 | ||
364 | bool wxShell(const wxString& command) | |
365 | { | |
fbf456aa | 366 | return wxExecute(wxMakeShellCommand(command), wxEXEC_SYNC) == 0; |
2c8e4738 VZ |
367 | } |
368 | ||
369 | bool wxShell(const wxString& command, wxArrayString& output) | |
370 | { | |
9d8aca48 | 371 | wxCHECK_MSG( !command.empty(), false, _T("can't exec shell non interactively") ); |
518b5d2f | 372 | |
2c8e4738 | 373 | return wxExecute(wxMakeShellCommand(command), output); |
518b5d2f VZ |
374 | } |
375 | ||
f6ba47d9 VZ |
376 | // Shutdown or reboot the PC |
377 | bool wxShutdown(wxShutdownFlags wFlags) | |
378 | { | |
379 | wxChar level; | |
380 | switch ( wFlags ) | |
381 | { | |
382 | case wxSHUTDOWN_POWEROFF: | |
383 | level = _T('0'); | |
384 | break; | |
385 | ||
386 | case wxSHUTDOWN_REBOOT: | |
387 | level = _T('6'); | |
388 | break; | |
389 | ||
390 | default: | |
391 | wxFAIL_MSG( _T("unknown wxShutdown() flag") ); | |
9d8aca48 | 392 | return false; |
f6ba47d9 VZ |
393 | } |
394 | ||
395 | return system(wxString::Format(_T("init %c"), level).mb_str()) == 0; | |
396 | } | |
397 | ||
8ea92b4d WS |
398 | wxPowerType wxGetPowerType() |
399 | { | |
400 | // TODO | |
401 | return wxPOWER_UNKNOWN; | |
402 | } | |
403 | ||
404 | wxBatteryState wxGetBatteryState() | |
405 | { | |
406 | // TODO | |
407 | return wxBATTERY_UNKNOWN_STATE; | |
408 | } | |
f6ba47d9 | 409 | |
cd6ce4a9 VZ |
410 | // ---------------------------------------------------------------------------- |
411 | // wxStream classes to support IO redirection in wxExecute | |
412 | // ---------------------------------------------------------------------------- | |
6dc6fda6 | 413 | |
1e6feb95 VZ |
414 | #if wxUSE_STREAMS |
415 | ||
2b5f62a0 | 416 | bool wxPipeInputStream::CanRead() const |
8b33ae2d | 417 | { |
cd6ce4a9 | 418 | if ( m_lasterror == wxSTREAM_EOF ) |
9d8aca48 | 419 | return false; |
cd6ce4a9 VZ |
420 | |
421 | // check if there is any input available | |
422 | struct timeval tv; | |
423 | tv.tv_sec = 0; | |
424 | tv.tv_usec = 0; | |
425 | ||
80d6dc0a VZ |
426 | const int fd = m_file->fd(); |
427 | ||
cd6ce4a9 | 428 | fd_set readfds; |
17a1ebd1 VZ |
429 | |
430 | wxFD_ZERO(&readfds); | |
431 | wxFD_SET(fd, &readfds); | |
432 | ||
80d6dc0a | 433 | switch ( select(fd + 1, &readfds, NULL, NULL, &tv) ) |
cd6ce4a9 VZ |
434 | { |
435 | case -1: | |
436 | wxLogSysError(_("Impossible to get child process input")); | |
437 | // fall through | |
8b33ae2d | 438 | |
cd6ce4a9 | 439 | case 0: |
9d8aca48 | 440 | return false; |
8b33ae2d | 441 | |
cd6ce4a9 VZ |
442 | default: |
443 | wxFAIL_MSG(_T("unexpected select() return value")); | |
444 | // still fall through | |
445 | ||
446 | case 1: | |
6f3d3c68 VZ |
447 | // input available -- or maybe not, as select() returns 1 when a |
448 | // read() will complete without delay, but it could still not read | |
449 | // anything | |
450 | return !Eof(); | |
cd6ce4a9 | 451 | } |
8b33ae2d GL |
452 | } |
453 | ||
1e6feb95 VZ |
454 | #endif // wxUSE_STREAMS |
455 | ||
b477f956 VZ |
456 | // ---------------------------------------------------------------------------- |
457 | // wxExecute: the real worker function | |
458 | // ---------------------------------------------------------------------------- | |
79066131 | 459 | |
17a1ebd1 | 460 | long wxExecute(wxChar **argv, int flags, wxProcess *process) |
518b5d2f | 461 | { |
f6bcfd97 | 462 | // for the sync execution, we return -1 to indicate failure, but for async |
accb3257 VZ |
463 | // case we return 0 which is never a valid PID |
464 | // | |
465 | // we define this as a macro, not a variable, to avoid compiler warnings | |
466 | // about "ERROR_RETURN_CODE value may be clobbered by fork()" | |
fbf456aa | 467 | #define ERROR_RETURN_CODE ((flags & wxEXEC_SYNC) ? -1 : 0) |
f6bcfd97 | 468 | |
accb3257 | 469 | wxCHECK_MSG( *argv, ERROR_RETURN_CODE, wxT("can't exec empty command") ); |
518b5d2f | 470 | |
05079acc OK |
471 | #if wxUSE_UNICODE |
472 | int mb_argc = 0; | |
473 | char *mb_argv[WXEXECUTE_NARGS]; | |
474 | ||
e90c1d2a VZ |
475 | while (argv[mb_argc]) |
476 | { | |
cd6ce4a9 VZ |
477 | wxWX2MBbuf mb_arg = wxConvertWX2MB(argv[mb_argc]); |
478 | mb_argv[mb_argc] = strdup(mb_arg); | |
479 | mb_argc++; | |
05079acc OK |
480 | } |
481 | mb_argv[mb_argc] = (char *) NULL; | |
e90c1d2a VZ |
482 | |
483 | // this macro will free memory we used above | |
484 | #define ARGS_CLEANUP \ | |
345b0247 | 485 | for ( mb_argc = 0; mb_argv[mb_argc]; mb_argc++ ) \ |
e90c1d2a VZ |
486 | free(mb_argv[mb_argc]) |
487 | #else // ANSI | |
488 | // no need for cleanup | |
489 | #define ARGS_CLEANUP | |
490 | ||
05079acc | 491 | wxChar **mb_argv = argv; |
e90c1d2a | 492 | #endif // Unicode/ANSI |
518b5d2f | 493 | |
e2478fde VZ |
494 | // we want this function to work even if there is no wxApp so ensure that |
495 | // we have a valid traits pointer | |
496 | wxConsoleAppTraits traitsConsole; | |
497 | wxAppTraits *traits = wxTheApp ? wxTheApp->GetTraits() : NULL; | |
498 | if ( !traits ) | |
499 | traits = &traitsConsole; | |
500 | ||
501 | // this struct contains all information which we pass to and from | |
502 | // wxAppTraits methods | |
503 | wxExecuteData execData; | |
504 | execData.flags = flags; | |
505 | execData.process = process; | |
506 | ||
518b5d2f | 507 | // create pipes |
e2478fde | 508 | if ( !traits->CreateEndProcessPipe(execData) ) |
518b5d2f | 509 | { |
cd6ce4a9 | 510 | wxLogError( _("Failed to execute '%s'\n"), *argv ); |
e90c1d2a VZ |
511 | |
512 | ARGS_CLEANUP; | |
513 | ||
accb3257 | 514 | return ERROR_RETURN_CODE; |
518b5d2f VZ |
515 | } |
516 | ||
f6bcfd97 | 517 | // pipes for inter process communication |
b477f956 VZ |
518 | wxPipe pipeIn, // stdin |
519 | pipeOut, // stdout | |
520 | pipeErr; // stderr | |
cd6ce4a9 VZ |
521 | |
522 | if ( process && process->IsRedirected() ) | |
8b33ae2d | 523 | { |
b477f956 | 524 | if ( !pipeIn.Create() || !pipeOut.Create() || !pipeErr.Create() ) |
8b33ae2d | 525 | { |
cd6ce4a9 | 526 | wxLogError( _("Failed to execute '%s'\n"), *argv ); |
8b33ae2d GL |
527 | |
528 | ARGS_CLEANUP; | |
529 | ||
accb3257 | 530 | return ERROR_RETURN_CODE; |
8b33ae2d GL |
531 | } |
532 | } | |
8b33ae2d | 533 | |
518b5d2f | 534 | // fork the process |
ef5f8ab6 VZ |
535 | // |
536 | // NB: do *not* use vfork() here, it completely breaks this code for some | |
537 | // reason under Solaris (and maybe others, although not under Linux) | |
b2ddee86 JJ |
538 | // But on OpenVMS we do not have fork so we have to use vfork and |
539 | // cross our fingers that it works. | |
540 | #ifdef __VMS | |
541 | pid_t pid = vfork(); | |
542 | #else | |
543 | pid_t pid = fork(); | |
544 | #endif | |
545 | if ( pid == -1 ) // error? | |
518b5d2f VZ |
546 | { |
547 | wxLogSysError( _("Fork failed") ); | |
e90c1d2a VZ |
548 | |
549 | ARGS_CLEANUP; | |
550 | ||
accb3257 | 551 | return ERROR_RETURN_CODE; |
518b5d2f | 552 | } |
cd6ce4a9 | 553 | else if ( pid == 0 ) // we're in child |
518b5d2f | 554 | { |
cd6ce4a9 | 555 | // These lines close the open file descriptors to to avoid any |
518b5d2f | 556 | // input/output which might block the process or irritate the user. If |
cd6ce4a9 VZ |
557 | // one wants proper IO for the subprocess, the right thing to do is to |
558 | // start an xterm executing it. | |
fbf456aa | 559 | if ( !(flags & wxEXEC_SYNC) ) |
518b5d2f | 560 | { |
35ef537b VZ |
561 | // FD_SETSIZE is unsigned under BSD, signed under other platforms |
562 | // so we need a cast to avoid warnings on all platforms | |
563 | for ( int fd = 0; fd < (int)FD_SETSIZE; fd++ ) | |
518b5d2f | 564 | { |
b477f956 VZ |
565 | if ( fd == pipeIn[wxPipe::Read] |
566 | || fd == pipeOut[wxPipe::Write] | |
567 | || fd == pipeErr[wxPipe::Write] | |
e2478fde | 568 | || traits->IsWriteFDOfEndProcessPipe(execData, fd) ) |
cd6ce4a9 VZ |
569 | { |
570 | // don't close this one, we still need it | |
571 | continue; | |
572 | } | |
e90c1d2a | 573 | |
b477f956 | 574 | // leave stderr opened too, it won't do any harm |
e90c1d2a | 575 | if ( fd != STDERR_FILENO ) |
518b5d2f VZ |
576 | close(fd); |
577 | } | |
0c9c4401 | 578 | } |
e1082c9f | 579 | |
e8e776aa | 580 | #if !defined(__VMS) && !defined(__EMX__) |
0c9c4401 VZ |
581 | if ( flags & wxEXEC_MAKE_GROUP_LEADER ) |
582 | { | |
583 | // Set process group to child process' pid. Then killing -pid | |
584 | // of the parent will kill the process and all of its children. | |
585 | setsid(); | |
518b5d2f | 586 | } |
0c9c4401 VZ |
587 | #endif // !__VMS |
588 | ||
0c9c4401 VZ |
589 | // reading side can be safely closed but we should keep the write one |
590 | // opened | |
e2478fde | 591 | traits->DetachWriteFDOfEndProcessPipe(execData); |
518b5d2f | 592 | |
80d6dc0a | 593 | // redirect stdin, stdout and stderr |
b477f956 | 594 | if ( pipeIn.IsOk() ) |
cd6ce4a9 | 595 | { |
b477f956 VZ |
596 | if ( dup2(pipeIn[wxPipe::Read], STDIN_FILENO) == -1 || |
597 | dup2(pipeOut[wxPipe::Write], STDOUT_FILENO) == -1 || | |
598 | dup2(pipeErr[wxPipe::Write], STDERR_FILENO) == -1 ) | |
cd6ce4a9 | 599 | { |
f6bcfd97 | 600 | wxLogSysError(_("Failed to redirect child process input/output")); |
cd6ce4a9 | 601 | } |
518b5d2f | 602 | |
b477f956 VZ |
603 | pipeIn.Close(); |
604 | pipeOut.Close(); | |
605 | pipeErr.Close(); | |
cd6ce4a9 | 606 | } |
518b5d2f | 607 | |
05079acc | 608 | execvp (*mb_argv, mb_argv); |
17a1ebd1 | 609 | |
d264d709 | 610 | fprintf(stderr, "execvp("); |
8d4f85ce SC |
611 | // CS changed ppc to ppc_ as ppc is not available under mac os CW Mach-O |
612 | for ( char **ppc_ = mb_argv; *ppc_; ppc_++ ) | |
613 | fprintf(stderr, "%s%s", ppc_ == mb_argv ? "" : ", ", *ppc_); | |
d264d709 VZ |
614 | fprintf(stderr, ") failed with error %d!\n", errno); |
615 | ||
518b5d2f | 616 | // there is no return after successful exec() |
518b5d2f | 617 | _exit(-1); |
1d8dd65e VZ |
618 | |
619 | // some compilers complain about missing return - of course, they | |
620 | // should know that exit() doesn't return but what else can we do if | |
621 | // they don't? | |
b477f956 VZ |
622 | // |
623 | // and, sure enough, other compilers complain about unreachable code | |
624 | // after exit() call, so we can just always have return here... | |
1d8dd65e VZ |
625 | #if defined(__VMS) || defined(__INTEL_COMPILER) |
626 | return 0; | |
627 | #endif | |
518b5d2f | 628 | } |
cd6ce4a9 | 629 | else // we're in parent |
518b5d2f | 630 | { |
cd6ce4a9 VZ |
631 | ARGS_CLEANUP; |
632 | ||
7764f973 VZ |
633 | // save it for WaitForChild() use |
634 | execData.pid = pid; | |
635 | ||
80d6dc0a VZ |
636 | // prepare for IO redirection |
637 | ||
0e300ddd | 638 | #if wxUSE_STREAMS |
80d6dc0a VZ |
639 | // the input buffer bufOut is connected to stdout, this is why it is |
640 | // called bufOut and not bufIn | |
641 | wxStreamTempInputBuffer bufOut, | |
642 | bufErr; | |
0e300ddd VZ |
643 | #endif // wxUSE_STREAMS |
644 | ||
cd6ce4a9 VZ |
645 | if ( process && process->IsRedirected() ) |
646 | { | |
1e6feb95 | 647 | #if wxUSE_STREAMS |
80d6dc0a VZ |
648 | wxOutputStream *inStream = |
649 | new wxFileOutputStream(pipeIn.Detach(wxPipe::Write)); | |
b477f956 | 650 | |
79066131 VZ |
651 | wxPipeInputStream *outStream = |
652 | new wxPipeInputStream(pipeOut.Detach(wxPipe::Read)); | |
b477f956 | 653 | |
79066131 VZ |
654 | wxPipeInputStream *errStream = |
655 | new wxPipeInputStream(pipeErr.Detach(wxPipe::Read)); | |
f6bcfd97 | 656 | |
80d6dc0a | 657 | process->SetPipeStreams(outStream, inStream, errStream); |
0e300ddd | 658 | |
80d6dc0a | 659 | bufOut.Init(outStream); |
b477f956 | 660 | bufErr.Init(errStream); |
e2478fde VZ |
661 | |
662 | execData.bufOut = &bufOut; | |
663 | execData.bufErr = &bufErr; | |
1e6feb95 | 664 | #endif // wxUSE_STREAMS |
b477f956 | 665 | } |
1e6feb95 | 666 | |
b477f956 VZ |
667 | if ( pipeIn.IsOk() ) |
668 | { | |
669 | pipeIn.Close(); | |
670 | pipeOut.Close(); | |
671 | pipeErr.Close(); | |
cd6ce4a9 VZ |
672 | } |
673 | ||
e2478fde | 674 | return traits->WaitForChild(execData); |
518b5d2f | 675 | } |
79656e30 | 676 | |
17a1ebd1 | 677 | #if !defined(__VMS) && !defined(__INTEL_COMPILER) |
79656e30 | 678 | return ERROR_RETURN_CODE; |
ef0ed19e | 679 | #endif |
17a1ebd1 | 680 | } |
518b5d2f | 681 | |
accb3257 | 682 | #undef ERROR_RETURN_CODE |
f6bcfd97 BP |
683 | #undef ARGS_CLEANUP |
684 | ||
518b5d2f VZ |
685 | // ---------------------------------------------------------------------------- |
686 | // file and directory functions | |
687 | // ---------------------------------------------------------------------------- | |
688 | ||
05079acc | 689 | const wxChar* wxGetHomeDir( wxString *home ) |
518b5d2f | 690 | { |
8ea92b4d | 691 | *home = wxGetUserHome( wxEmptyString ); |
79066131 | 692 | wxString tmp; |
8ea92b4d | 693 | if ( home->empty() ) |
223d09f6 | 694 | *home = wxT("/"); |
181cbcf4 | 695 | #ifdef __VMS |
79066131 VZ |
696 | tmp = *home; |
697 | if ( tmp.Last() != wxT(']')) | |
698 | if ( tmp.Last() != wxT('/')) *home << wxT('/'); | |
181cbcf4 | 699 | #endif |
518b5d2f VZ |
700 | return home->c_str(); |
701 | } | |
702 | ||
05079acc OK |
703 | #if wxUSE_UNICODE |
704 | const wxMB2WXbuf wxGetUserHome( const wxString &user ) | |
e90c1d2a | 705 | #else // just for binary compatibility -- there is no 'const' here |
518b5d2f | 706 | char *wxGetUserHome( const wxString &user ) |
05079acc | 707 | #endif |
518b5d2f VZ |
708 | { |
709 | struct passwd *who = (struct passwd *) NULL; | |
710 | ||
0fb67cd1 | 711 | if ( !user ) |
518b5d2f | 712 | { |
e90c1d2a | 713 | wxChar *ptr; |
518b5d2f | 714 | |
223d09f6 | 715 | if ((ptr = wxGetenv(wxT("HOME"))) != NULL) |
518b5d2f | 716 | { |
2b5f62a0 VZ |
717 | #if wxUSE_UNICODE |
718 | wxWCharBuffer buffer( ptr ); | |
719 | return buffer; | |
720 | #else | |
518b5d2f | 721 | return ptr; |
2b5f62a0 | 722 | #endif |
518b5d2f | 723 | } |
223d09f6 | 724 | if ((ptr = wxGetenv(wxT("USER"))) != NULL || (ptr = wxGetenv(wxT("LOGNAME"))) != NULL) |
518b5d2f | 725 | { |
e90c1d2a | 726 | who = getpwnam(wxConvertWX2MB(ptr)); |
518b5d2f VZ |
727 | } |
728 | ||
729 | // We now make sure the the user exists! | |
730 | if (who == NULL) | |
731 | { | |
732 | who = getpwuid(getuid()); | |
733 | } | |
734 | } | |
735 | else | |
736 | { | |
05079acc | 737 | who = getpwnam (user.mb_str()); |
518b5d2f VZ |
738 | } |
739 | ||
af111fc3 | 740 | return wxConvertMB2WX(who ? who->pw_dir : 0); |
518b5d2f VZ |
741 | } |
742 | ||
743 | // ---------------------------------------------------------------------------- | |
0fb67cd1 | 744 | // network and user id routines |
518b5d2f VZ |
745 | // ---------------------------------------------------------------------------- |
746 | ||
0fb67cd1 VZ |
747 | // retrieve either the hostname or FQDN depending on platform (caller must |
748 | // check whether it's one or the other, this is why this function is for | |
749 | // private use only) | |
05079acc | 750 | static bool wxGetHostNameInternal(wxChar *buf, int sz) |
518b5d2f | 751 | { |
9d8aca48 | 752 | wxCHECK_MSG( buf, false, wxT("NULL pointer in wxGetHostNameInternal") ); |
518b5d2f | 753 | |
223d09f6 | 754 | *buf = wxT('\0'); |
518b5d2f VZ |
755 | |
756 | // we're using uname() which is POSIX instead of less standard sysinfo() | |
757 | #if defined(HAVE_UNAME) | |
cc743a6f | 758 | struct utsname uts; |
518b5d2f VZ |
759 | bool ok = uname(&uts) != -1; |
760 | if ( ok ) | |
761 | { | |
e90c1d2a | 762 | wxStrncpy(buf, wxConvertMB2WX(uts.nodename), sz - 1); |
223d09f6 | 763 | buf[sz] = wxT('\0'); |
518b5d2f VZ |
764 | } |
765 | #elif defined(HAVE_GETHOSTNAME) | |
766 | bool ok = gethostname(buf, sz) != -1; | |
0fb67cd1 | 767 | #else // no uname, no gethostname |
223d09f6 | 768 | wxFAIL_MSG(wxT("don't know host name for this machine")); |
518b5d2f | 769 | |
9d8aca48 | 770 | bool ok = false; |
0fb67cd1 | 771 | #endif // uname/gethostname |
518b5d2f VZ |
772 | |
773 | if ( !ok ) | |
774 | { | |
775 | wxLogSysError(_("Cannot get the hostname")); | |
776 | } | |
777 | ||
778 | return ok; | |
779 | } | |
780 | ||
05079acc | 781 | bool wxGetHostName(wxChar *buf, int sz) |
0fb67cd1 VZ |
782 | { |
783 | bool ok = wxGetHostNameInternal(buf, sz); | |
784 | ||
785 | if ( ok ) | |
786 | { | |
787 | // BSD systems return the FQDN, we only want the hostname, so extract | |
788 | // it (we consider that dots are domain separators) | |
223d09f6 | 789 | wxChar *dot = wxStrchr(buf, wxT('.')); |
0fb67cd1 VZ |
790 | if ( dot ) |
791 | { | |
792 | // nuke it | |
223d09f6 | 793 | *dot = wxT('\0'); |
0fb67cd1 VZ |
794 | } |
795 | } | |
796 | ||
797 | return ok; | |
798 | } | |
799 | ||
05079acc | 800 | bool wxGetFullHostName(wxChar *buf, int sz) |
0fb67cd1 VZ |
801 | { |
802 | bool ok = wxGetHostNameInternal(buf, sz); | |
803 | ||
804 | if ( ok ) | |
805 | { | |
223d09f6 | 806 | if ( !wxStrchr(buf, wxT('.')) ) |
0fb67cd1 | 807 | { |
e90c1d2a | 808 | struct hostent *host = gethostbyname(wxConvertWX2MB(buf)); |
0fb67cd1 VZ |
809 | if ( !host ) |
810 | { | |
811 | wxLogSysError(_("Cannot get the official hostname")); | |
812 | ||
9d8aca48 | 813 | ok = false; |
0fb67cd1 VZ |
814 | } |
815 | else | |
816 | { | |
817 | // the canonical name | |
e90c1d2a | 818 | wxStrncpy(buf, wxConvertMB2WX(host->h_name), sz); |
0fb67cd1 VZ |
819 | } |
820 | } | |
821 | //else: it's already a FQDN (BSD behaves this way) | |
822 | } | |
823 | ||
824 | return ok; | |
825 | } | |
826 | ||
05079acc | 827 | bool wxGetUserId(wxChar *buf, int sz) |
518b5d2f VZ |
828 | { |
829 | struct passwd *who; | |
830 | ||
223d09f6 | 831 | *buf = wxT('\0'); |
518b5d2f VZ |
832 | if ((who = getpwuid(getuid ())) != NULL) |
833 | { | |
e90c1d2a | 834 | wxStrncpy (buf, wxConvertMB2WX(who->pw_name), sz - 1); |
9d8aca48 | 835 | return true; |
518b5d2f VZ |
836 | } |
837 | ||
9d8aca48 | 838 | return false; |
518b5d2f VZ |
839 | } |
840 | ||
05079acc | 841 | bool wxGetUserName(wxChar *buf, int sz) |
518b5d2f VZ |
842 | { |
843 | struct passwd *who; | |
518b5d2f | 844 | |
223d09f6 | 845 | *buf = wxT('\0'); |
b12915c1 VZ |
846 | if ((who = getpwuid (getuid ())) != NULL) |
847 | { | |
848 | // pw_gecos field in struct passwd is not standard | |
bd3277fe | 849 | #ifdef HAVE_PW_GECOS |
b12915c1 | 850 | char *comma = strchr(who->pw_gecos, ','); |
518b5d2f VZ |
851 | if (comma) |
852 | *comma = '\0'; // cut off non-name comment fields | |
e90c1d2a | 853 | wxStrncpy (buf, wxConvertMB2WX(who->pw_gecos), sz - 1); |
b12915c1 | 854 | #else // !HAVE_PW_GECOS |
0fcdf6dc | 855 | wxStrncpy (buf, wxConvertMB2WX(who->pw_name), sz - 1); |
b12915c1 | 856 | #endif // HAVE_PW_GECOS/!HAVE_PW_GECOS |
9d8aca48 | 857 | return true; |
518b5d2f VZ |
858 | } |
859 | ||
9d8aca48 | 860 | return false; |
518b5d2f VZ |
861 | } |
862 | ||
e2478fde | 863 | // this function is in mac/utils.cpp for wxMac |
6e73695c | 864 | #ifndef __WXMAC__ |
e2478fde | 865 | |
bdc72a22 VZ |
866 | wxString wxGetOsDescription() |
867 | { | |
db1d0193 VZ |
868 | FILE *f = popen("uname -s -r -m", "r"); |
869 | if (f) | |
870 | { | |
871 | char buf[256]; | |
872 | size_t c = fread(buf, 1, sizeof(buf) - 1, f); | |
873 | pclose(f); | |
874 | // Trim newline from output. | |
875 | if (c && buf[c - 1] == '\n') | |
876 | --c; | |
877 | buf[c] = '\0'; | |
878 | return wxString::FromAscii( buf ); | |
879 | } | |
880 | wxFAIL_MSG( _T("uname failed") ); | |
881 | return _T(""); | |
bdc72a22 VZ |
882 | } |
883 | ||
e2478fde | 884 | #endif // !__WXMAC__ |
bd3277fe | 885 | |
c1cb4153 VZ |
886 | unsigned long wxGetProcessId() |
887 | { | |
888 | return (unsigned long)getpid(); | |
889 | } | |
890 | ||
9d8aca48 | 891 | wxMemorySize wxGetFreeMemory() |
bd3277fe VZ |
892 | { |
893 | #if defined(__LINUX__) | |
894 | // get it from /proc/meminfo | |
895 | FILE *fp = fopen("/proc/meminfo", "r"); | |
896 | if ( fp ) | |
897 | { | |
898 | long memFree = -1; | |
899 | ||
900 | char buf[1024]; | |
901 | if ( fgets(buf, WXSIZEOF(buf), fp) && fgets(buf, WXSIZEOF(buf), fp) ) | |
902 | { | |
903 | long memTotal, memUsed; | |
904 | sscanf(buf, "Mem: %ld %ld %ld", &memTotal, &memUsed, &memFree); | |
905 | } | |
906 | ||
907 | fclose(fp); | |
908 | ||
9d8aca48 | 909 | return (wxMemorySize)memFree; |
bd3277fe VZ |
910 | } |
911 | #elif defined(__SUN__) && defined(_SC_AVPHYS_PAGES) | |
9d8aca48 | 912 | return (wxMemorySize)(sysconf(_SC_AVPHYS_PAGES)*sysconf(_SC_PAGESIZE)); |
bd3277fe VZ |
913 | //#elif defined(__FREEBSD__) -- might use sysctl() to find it out, probably |
914 | #endif | |
915 | ||
916 | // can't find it out | |
917 | return -1; | |
918 | } | |
919 | ||
eadd7bd2 VZ |
920 | bool wxGetDiskSpace(const wxString& path, wxLongLong *pTotal, wxLongLong *pFree) |
921 | { | |
9952adac | 922 | #if defined(HAVE_STATFS) || defined(HAVE_STATVFS) |
fbfb3fb3 | 923 | // the case to "char *" is needed for AIX 4.3 |
85da04e9 VZ |
924 | wxStatfs_t fs; |
925 | if ( wxStatfs((char *)(const char*)path.fn_str(), &fs) != 0 ) | |
eadd7bd2 | 926 | { |
401eb3de | 927 | wxLogSysError( wxT("Failed to get file system statistics") ); |
eadd7bd2 | 928 | |
9d8aca48 | 929 | return false; |
eadd7bd2 VZ |
930 | } |
931 | ||
125cb99b VZ |
932 | // under Solaris we also have to use f_frsize field instead of f_bsize |
933 | // which is in general a multiple of f_frsize | |
934 | #ifdef HAVE_STATVFS | |
935 | wxLongLong blockSize = fs.f_frsize; | |
936 | #else // HAVE_STATFS | |
937 | wxLongLong blockSize = fs.f_bsize; | |
938 | #endif // HAVE_STATVFS/HAVE_STATFS | |
9952adac | 939 | |
eadd7bd2 VZ |
940 | if ( pTotal ) |
941 | { | |
125cb99b | 942 | *pTotal = wxLongLong(fs.f_blocks) * blockSize; |
eadd7bd2 VZ |
943 | } |
944 | ||
945 | if ( pFree ) | |
946 | { | |
125cb99b | 947 | *pFree = wxLongLong(fs.f_bavail) * blockSize; |
eadd7bd2 VZ |
948 | } |
949 | ||
9d8aca48 | 950 | return true; |
125cb99b | 951 | #else // !HAVE_STATFS && !HAVE_STATVFS |
9d8aca48 | 952 | return false; |
125cb99b | 953 | #endif // HAVE_STATFS |
eadd7bd2 VZ |
954 | } |
955 | ||
8fd0d89b VZ |
956 | // ---------------------------------------------------------------------------- |
957 | // env vars | |
958 | // ---------------------------------------------------------------------------- | |
959 | ||
97b305b7 | 960 | bool wxGetEnv(const wxString& var, wxString *value) |
308978f6 VZ |
961 | { |
962 | // wxGetenv is defined as getenv() | |
963 | wxChar *p = wxGetenv(var); | |
964 | if ( !p ) | |
9d8aca48 | 965 | return false; |
308978f6 VZ |
966 | |
967 | if ( value ) | |
968 | { | |
969 | *value = p; | |
970 | } | |
971 | ||
9d8aca48 | 972 | return true; |
308978f6 VZ |
973 | } |
974 | ||
8fd0d89b VZ |
975 | bool wxSetEnv(const wxString& variable, const wxChar *value) |
976 | { | |
977 | #if defined(HAVE_SETENV) | |
d90b2df8 VZ |
978 | return setenv(variable.mb_str(), |
979 | value ? (const char *)wxString(value).mb_str() | |
980 | : NULL, | |
981 | 1 /* overwrite */) == 0; | |
8fd0d89b VZ |
982 | #elif defined(HAVE_PUTENV) |
983 | wxString s = variable; | |
984 | if ( value ) | |
985 | s << _T('=') << value; | |
986 | ||
987 | // transform to ANSI | |
67479dbd | 988 | const wxWX2MBbuf p = s.mb_str(); |
8fd0d89b VZ |
989 | |
990 | // the string will be free()d by libc | |
991 | char *buf = (char *)malloc(strlen(p) + 1); | |
992 | strcpy(buf, p); | |
993 | ||
994 | return putenv(buf) == 0; | |
995 | #else // no way to set an env var | |
67479dbd | 996 | return false; |
8fd0d89b VZ |
997 | #endif |
998 | } | |
999 | ||
a37a5a73 VZ |
1000 | // ---------------------------------------------------------------------------- |
1001 | // signal handling | |
1002 | // ---------------------------------------------------------------------------- | |
1003 | ||
1004 | #if wxUSE_ON_FATAL_EXCEPTION | |
1005 | ||
1006 | #include <signal.h> | |
1007 | ||
90350682 | 1008 | extern "C" void wxFatalSignalHandler(wxTYPE_SA_HANDLER) |
a37a5a73 VZ |
1009 | { |
1010 | if ( wxTheApp ) | |
1011 | { | |
1012 | // give the user a chance to do something special about this | |
1013 | wxTheApp->OnFatalException(); | |
1014 | } | |
1015 | ||
1016 | abort(); | |
1017 | } | |
1018 | ||
1019 | bool wxHandleFatalExceptions(bool doit) | |
1020 | { | |
1021 | // old sig handlers | |
9d8aca48 | 1022 | static bool s_savedHandlers = false; |
a37a5a73 VZ |
1023 | static struct sigaction s_handlerFPE, |
1024 | s_handlerILL, | |
1025 | s_handlerBUS, | |
1026 | s_handlerSEGV; | |
1027 | ||
9d8aca48 | 1028 | bool ok = true; |
a37a5a73 VZ |
1029 | if ( doit && !s_savedHandlers ) |
1030 | { | |
1031 | // install the signal handler | |
1032 | struct sigaction act; | |
1033 | ||
1034 | // some systems extend it with non std fields, so zero everything | |
1035 | memset(&act, 0, sizeof(act)); | |
1036 | ||
1037 | act.sa_handler = wxFatalSignalHandler; | |
1038 | sigemptyset(&act.sa_mask); | |
1039 | act.sa_flags = 0; | |
1040 | ||
1041 | ok &= sigaction(SIGFPE, &act, &s_handlerFPE) == 0; | |
1042 | ok &= sigaction(SIGILL, &act, &s_handlerILL) == 0; | |
1043 | ok &= sigaction(SIGBUS, &act, &s_handlerBUS) == 0; | |
1044 | ok &= sigaction(SIGSEGV, &act, &s_handlerSEGV) == 0; | |
1045 | if ( !ok ) | |
1046 | { | |
1047 | wxLogDebug(_T("Failed to install our signal handler.")); | |
1048 | } | |
1049 | ||
9d8aca48 | 1050 | s_savedHandlers = true; |
a37a5a73 VZ |
1051 | } |
1052 | else if ( s_savedHandlers ) | |
1053 | { | |
1054 | // uninstall the signal handler | |
1055 | ok &= sigaction(SIGFPE, &s_handlerFPE, NULL) == 0; | |
1056 | ok &= sigaction(SIGILL, &s_handlerILL, NULL) == 0; | |
1057 | ok &= sigaction(SIGBUS, &s_handlerBUS, NULL) == 0; | |
1058 | ok &= sigaction(SIGSEGV, &s_handlerSEGV, NULL) == 0; | |
1059 | if ( !ok ) | |
1060 | { | |
1061 | wxLogDebug(_T("Failed to uninstall our signal handler.")); | |
1062 | } | |
1063 | ||
9d8aca48 | 1064 | s_savedHandlers = false; |
a37a5a73 VZ |
1065 | } |
1066 | //else: nothing to do | |
1067 | ||
1068 | return ok; | |
1069 | } | |
1070 | ||
1071 | #endif // wxUSE_ON_FATAL_EXCEPTION | |
1072 | ||
518b5d2f VZ |
1073 | // ---------------------------------------------------------------------------- |
1074 | // error and debug output routines (deprecated, use wxLog) | |
1075 | // ---------------------------------------------------------------------------- | |
1076 | ||
73deed44 VZ |
1077 | #if WXWIN_COMPATIBILITY_2_2 |
1078 | ||
518b5d2f VZ |
1079 | void wxDebugMsg( const char *format, ... ) |
1080 | { | |
1081 | va_list ap; | |
1082 | va_start( ap, format ); | |
1083 | vfprintf( stderr, format, ap ); | |
1084 | fflush( stderr ); | |
1085 | va_end(ap); | |
1086 | } | |
1087 | ||
1088 | void wxError( const wxString &msg, const wxString &title ) | |
1089 | { | |
05079acc | 1090 | wxFprintf( stderr, _("Error ") ); |
223d09f6 KB |
1091 | if (!title.IsNull()) wxFprintf( stderr, wxT("%s "), WXSTRINGCAST(title) ); |
1092 | if (!msg.IsNull()) wxFprintf( stderr, wxT(": %s"), WXSTRINGCAST(msg) ); | |
1093 | wxFprintf( stderr, wxT(".\n") ); | |
518b5d2f VZ |
1094 | } |
1095 | ||
1096 | void wxFatalError( const wxString &msg, const wxString &title ) | |
1097 | { | |
05079acc | 1098 | wxFprintf( stderr, _("Error ") ); |
223d09f6 KB |
1099 | if (!title.IsNull()) wxFprintf( stderr, wxT("%s "), WXSTRINGCAST(title) ); |
1100 | if (!msg.IsNull()) wxFprintf( stderr, wxT(": %s"), WXSTRINGCAST(msg) ); | |
1101 | wxFprintf( stderr, wxT(".\n") ); | |
518b5d2f VZ |
1102 | exit(3); // the same exit code as for abort() |
1103 | } | |
93ccaed8 | 1104 | |
73deed44 VZ |
1105 | #endif // WXWIN_COMPATIBILITY_2_2 |
1106 | ||
ec67cff1 | 1107 | #endif // wxUSE_BASE |
e2478fde VZ |
1108 | |
1109 | #if wxUSE_GUI | |
1110 | ||
1111 | // ---------------------------------------------------------------------------- | |
1112 | // wxExecute support | |
1113 | // ---------------------------------------------------------------------------- | |
1114 | ||
1115 | // Darwin doesn't use the same process end detection mechanisms so we don't | |
1116 | // need wxExecute-related helpers for it | |
1117 | #if !(defined(__DARWIN__) && defined(__WXMAC__)) | |
1118 | ||
1119 | bool wxGUIAppTraits::CreateEndProcessPipe(wxExecuteData& execData) | |
1120 | { | |
1121 | return execData.pipeEndProcDetect.Create(); | |
1122 | } | |
1123 | ||
1124 | bool wxGUIAppTraits::IsWriteFDOfEndProcessPipe(wxExecuteData& execData, int fd) | |
1125 | { | |
46446cc2 | 1126 | return fd == (execData.pipeEndProcDetect)[wxPipe::Write]; |
e2478fde VZ |
1127 | } |
1128 | ||
1129 | void wxGUIAppTraits::DetachWriteFDOfEndProcessPipe(wxExecuteData& execData) | |
1130 | { | |
1131 | execData.pipeEndProcDetect.Detach(wxPipe::Write); | |
1132 | execData.pipeEndProcDetect.Close(); | |
1133 | } | |
1134 | ||
1135 | #else // !Darwin | |
1136 | ||
1137 | bool wxGUIAppTraits::CreateEndProcessPipe(wxExecuteData& WXUNUSED(execData)) | |
1138 | { | |
1139 | return true; | |
1140 | } | |
1141 | ||
1142 | bool | |
1143 | wxGUIAppTraits::IsWriteFDOfEndProcessPipe(wxExecuteData& WXUNUSED(execData), | |
1144 | int WXUNUSED(fd)) | |
1145 | { | |
1146 | return false; | |
1147 | } | |
1148 | ||
1149 | void | |
1150 | wxGUIAppTraits::DetachWriteFDOfEndProcessPipe(wxExecuteData& WXUNUSED(execData)) | |
1151 | { | |
1152 | // nothing to do here, we don't use the pipe | |
1153 | } | |
1154 | ||
1155 | #endif // !Darwin/Darwin | |
1156 | ||
1157 | int wxGUIAppTraits::WaitForChild(wxExecuteData& execData) | |
1158 | { | |
1159 | wxEndProcessData *endProcData = new wxEndProcessData; | |
1160 | ||
f38f6899 VZ |
1161 | const int flags = execData.flags; |
1162 | ||
e2478fde VZ |
1163 | // wxAddProcessCallback is now (with DARWIN) allowed to call the |
1164 | // callback function directly if the process terminates before | |
1165 | // the callback can be added to the run loop. Set up the endProcData. | |
f38f6899 | 1166 | if ( flags & wxEXEC_SYNC ) |
e2478fde VZ |
1167 | { |
1168 | // we may have process for capturing the program output, but it's | |
1169 | // not used in wxEndProcessData in the case of sync execution | |
1170 | endProcData->process = NULL; | |
1171 | ||
1172 | // sync execution: indicate it by negating the pid | |
1173 | endProcData->pid = -execData.pid; | |
1174 | } | |
1175 | else | |
1176 | { | |
1177 | // async execution, nothing special to do -- caller will be | |
1178 | // notified about the process termination if process != NULL, endProcData | |
1179 | // will be deleted in GTK_EndProcessDetector | |
1180 | endProcData->process = execData.process; | |
1181 | endProcData->pid = execData.pid; | |
1182 | } | |
1183 | ||
1184 | ||
f7ef0602 | 1185 | #if defined(__DARWIN__) && (defined(__WXMAC__) || defined(__WXCOCOA__)) |
e2478fde VZ |
1186 | endProcData->tag = wxAddProcessCallbackForPid(endProcData, execData.pid); |
1187 | #else | |
1188 | endProcData->tag = wxAddProcessCallback | |
1189 | ( | |
1190 | endProcData, | |
1191 | execData.pipeEndProcDetect.Detach(wxPipe::Read) | |
1192 | ); | |
1193 | ||
1194 | execData.pipeEndProcDetect.Close(); | |
f7ef0602 | 1195 | #endif // defined(__DARWIN__) && (defined(__WXMAC__) || defined(__WXCOCOA__)) |
e2478fde | 1196 | |
f38f6899 | 1197 | if ( flags & wxEXEC_SYNC ) |
e2478fde VZ |
1198 | { |
1199 | wxBusyCursor bc; | |
f38f6899 VZ |
1200 | wxWindowDisabler *wd = flags & wxEXEC_NODISABLE ? NULL |
1201 | : new wxWindowDisabler; | |
e2478fde VZ |
1202 | |
1203 | // endProcData->pid will be set to 0 from GTK_EndProcessDetector when the | |
1204 | // process terminates | |
1205 | while ( endProcData->pid != 0 ) | |
1206 | { | |
05df0f1b VZ |
1207 | bool idle = true; |
1208 | ||
e2478fde VZ |
1209 | #if wxUSE_STREAMS |
1210 | if ( execData.bufOut ) | |
05df0f1b | 1211 | { |
e2478fde | 1212 | execData.bufOut->Update(); |
05df0f1b VZ |
1213 | idle = false; |
1214 | } | |
e2478fde VZ |
1215 | |
1216 | if ( execData.bufErr ) | |
05df0f1b | 1217 | { |
e2478fde | 1218 | execData.bufErr->Update(); |
05df0f1b VZ |
1219 | idle = false; |
1220 | } | |
e2478fde VZ |
1221 | #endif // wxUSE_STREAMS |
1222 | ||
1012c2ce | 1223 | // don't consume 100% of the CPU while we're sitting in this |
05df0f1b VZ |
1224 | // loop |
1225 | if ( idle ) | |
39ef7151 | 1226 | wxMilliSleep(1); |
05df0f1b | 1227 | |
e2478fde VZ |
1228 | // give GTK+ a chance to call GTK_EndProcessDetector here and |
1229 | // also repaint the GUI | |
1230 | wxYield(); | |
1231 | } | |
1232 | ||
1233 | int exitcode = endProcData->exitcode; | |
1234 | ||
f38f6899 | 1235 | delete wd; |
e2478fde VZ |
1236 | delete endProcData; |
1237 | ||
1238 | return exitcode; | |
1239 | } | |
1240 | else // async execution | |
1241 | { | |
1242 | return execData.pid; | |
1243 | } | |
1244 | } | |
1245 | ||
2dbc444a RD |
1246 | #endif // wxUSE_GUI |
1247 | #if wxUSE_BASE | |
1248 | ||
e2478fde VZ |
1249 | void wxHandleProcessTermination(wxEndProcessData *proc_data) |
1250 | { | |
1251 | // notify user about termination if required | |
1252 | if ( proc_data->process ) | |
1253 | { | |
1254 | proc_data->process->OnTerminate(proc_data->pid, proc_data->exitcode); | |
1255 | } | |
1256 | ||
1257 | // clean up | |
1258 | if ( proc_data->pid > 0 ) | |
1259 | { | |
1260 | delete proc_data; | |
1261 | } | |
1262 | else | |
1263 | { | |
1264 | // let wxExecute() know that the process has terminated | |
1265 | proc_data->pid = 0; | |
1266 | } | |
1267 | } | |
1268 | ||
2dbc444a | 1269 | #endif // wxUSE_BASE |