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