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