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