1 /////////////////////////////////////////////////////////////////////////////
2 // Name: unix/utilsunx.cpp
3 // Purpose: generic Unix implementation of many wx functions
4 // Author: Vadim Zeitlin
6 // Copyright: (c) 1998 Robert Roebling, Vadim Zeitlin
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // ============================================================================
12 // ============================================================================
14 // ----------------------------------------------------------------------------
16 // ----------------------------------------------------------------------------
18 // for compilers that support precompilation, includes "wx.h".
19 #include "wx/wxprec.h"
22 #include "wx/string.h"
27 #include "wx/apptrait.h"
30 #include "wx/process.h"
31 #include "wx/thread.h"
33 #include "wx/wfstream.h"
35 #include "wx/unix/execute.h"
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"
43 #endif // wxUSE_STREAMS
47 #if defined( __MWERKS__ ) && defined(__MACH__)
48 #define WXWIN_OS_DESCRIPTION "MacOS X"
49 #define HAVE_NANOSLEEP
52 // not only the statfs syscall is called differently depending on platform, but
53 // one of its incarnations, statvfs(), takes different arguments under
54 // different platforms and even different versions of the same system (Solaris
55 // 7 and 8): if you want to test for this, don't forget that the problems only
56 // appear if the large files support is enabled
59 #include <sys/param.h>
60 #include <sys/mount.h>
63 #endif // __BSD__/!__BSD__
65 #define wxStatfs statfs
69 #include <sys/statvfs.h>
71 #define wxStatfs statvfs
72 #endif // HAVE_STATVFS
74 #if defined(HAVE_STATFS) || defined(HAVE_STATVFS)
75 // WX_STATFS_T is detected by configure
76 #define wxStatfs_t WX_STATFS_T
79 // SGI signal.h defines signal handler arguments differently depending on
80 // whether _LANGUAGE_C_PLUS_PLUS is set or not - do set it
81 #if defined(__SGI__) && !defined(_LANGUAGE_C_PLUS_PLUS)
82 #define _LANGUAGE_C_PLUS_PLUS 1
89 #include <sys/types.h>
96 #include <fcntl.h> // for O_WRONLY and friends
97 #include <time.h> // nanosleep() and/or usleep()
98 #include <ctype.h> // isspace()
99 #include <sys/time.h> // needed for FD_SETSIZE
102 #include <sys/utsname.h> // for uname()
105 // ----------------------------------------------------------------------------
106 // conditional compilation
107 // ----------------------------------------------------------------------------
109 // many versions of Unices have this function, but it is not defined in system
110 // headers - please add your system here if it is the case for your OS.
111 // SunOS < 5.6 (i.e. Solaris < 2.6) and DG-UX are like this.
112 #if !defined(HAVE_USLEEP) && \
113 (defined(__SUN__) && !defined(__SunOs_5_6) && \
114 !defined(__SunOs_5_7) && !defined(__SUNPRO_CC)) || \
115 defined(__osf__) || defined(__EMX__)
119 int usleep(unsigned int usec
);
122 /* I copied this from the XFree86 diffs. AV. */
123 #define INCL_DOSPROCESS
125 inline void usleep(unsigned long delay
)
127 DosSleep(delay
? (delay
/1000l) : 1l);
129 #else // !Sun && !EMX
130 void usleep(unsigned long usec
);
132 #endif // Sun/EMX/Something else
135 #define HAVE_USLEEP 1
136 #endif // Unices without usleep()
138 // ============================================================================
140 // ============================================================================
142 // ----------------------------------------------------------------------------
144 // ----------------------------------------------------------------------------
146 void wxSleep(int nSecs
)
151 void wxUsleep(unsigned long milliseconds
)
153 #if defined(HAVE_NANOSLEEP)
155 tmReq
.tv_sec
= (time_t)(milliseconds
/ 1000);
156 tmReq
.tv_nsec
= (milliseconds
% 1000) * 1000 * 1000;
158 // we're not interested in remaining time nor in return value
159 (void)nanosleep(&tmReq
, (timespec
*)NULL
);
160 #elif defined(HAVE_USLEEP)
161 // uncomment this if you feel brave or if you are sure that your version
162 // of Solaris has a safe usleep() function but please notice that usleep()
163 // is known to lead to crashes in MT programs in Solaris 2.[67] and is not
164 // documented as MT-Safe
165 #if defined(__SUN__) && wxUSE_THREADS
166 #error "usleep() cannot be used in MT programs under Solaris."
169 usleep(milliseconds
* 1000); // usleep(3) wants microseconds
170 #elif defined(HAVE_SLEEP)
171 // under BeOS sleep() takes seconds (what about other platforms, if any?)
172 sleep(milliseconds
* 1000);
173 #else // !sleep function
174 #error "usleep() or nanosleep() function required for wxUsleep"
175 #endif // sleep function
178 // ----------------------------------------------------------------------------
179 // process management
180 // ----------------------------------------------------------------------------
182 int wxKill(long pid
, wxSignal sig
, wxKillError
*rc
)
184 int err
= kill((pid_t
)pid
, (int)sig
);
194 *rc
= wxKILL_BAD_SIGNAL
;
198 *rc
= wxKILL_ACCESS_DENIED
;
202 *rc
= wxKILL_NO_PROCESS
;
206 // this goes against Unix98 docs so log it
207 wxLogDebug(_T("unexpected kill(2) return value %d"), err
);
217 #define WXEXECUTE_NARGS 127
219 long wxExecute( const wxString
& command
, int flags
, wxProcess
*process
)
221 wxCHECK_MSG( !command
.IsEmpty(), 0, wxT("can't exec empty command") );
224 // fork() doesn't mix well with POSIX threads: on many systems the program
225 // deadlocks or crashes for some reason. Probably our code is buggy and
226 // doesn't do something which must be done to allow this to work, but I
227 // don't know what yet, so for now just warn the user (this is the least we
229 wxASSERT_MSG( wxThread::IsMain(),
230 _T("wxExecute() can be called only from the main thread") );
231 #endif // wxUSE_THREADS
234 wxChar
*argv
[WXEXECUTE_NARGS
];
236 const wxChar
*cptr
= command
.c_str();
237 wxChar quotechar
= wxT('\0'); // is arg quoted?
238 bool escaped
= FALSE
;
240 // split the command line in arguments
244 quotechar
= wxT('\0');
246 // eat leading whitespace:
247 while ( wxIsspace(*cptr
) )
250 if ( *cptr
== wxT('\'') || *cptr
== wxT('"') )
255 if ( *cptr
== wxT('\\') && ! escaped
)
262 // all other characters:
266 // have we reached the end of the argument?
267 if ( (*cptr
== quotechar
&& ! escaped
)
268 || (quotechar
== wxT('\0') && wxIsspace(*cptr
))
269 || *cptr
== wxT('\0') )
271 wxASSERT_MSG( argc
< WXEXECUTE_NARGS
,
272 wxT("too many arguments in wxExecute") );
274 argv
[argc
] = new wxChar
[argument
.length() + 1];
275 wxStrcpy(argv
[argc
], argument
.c_str());
278 // if not at end of buffer, swallow last character:
282 break; // done with this one, start over
288 // do execute the command
289 long lRc
= wxExecute(argv
, flags
, process
);
294 delete [] argv
[argc
++];
299 // ----------------------------------------------------------------------------
301 // ----------------------------------------------------------------------------
303 static wxString
wxMakeShellCommand(const wxString
& command
)
308 // just an interactive shell
313 // execute command in a shell
314 cmd
<< _T("/bin/sh -c '") << command
<< _T('\'');
320 bool wxShell(const wxString
& command
)
322 return wxExecute(wxMakeShellCommand(command
), wxEXEC_SYNC
) == 0;
325 bool wxShell(const wxString
& command
, wxArrayString
& output
)
327 wxCHECK_MSG( !!command
, FALSE
, _T("can't exec shell non interactively") );
329 return wxExecute(wxMakeShellCommand(command
), output
);
332 // Shutdown or reboot the PC
333 bool wxShutdown(wxShutdownFlags wFlags
)
338 case wxSHUTDOWN_POWEROFF
:
342 case wxSHUTDOWN_REBOOT
:
347 wxFAIL_MSG( _T("unknown wxShutdown() flag") );
351 return system(wxString::Format(_T("init %c"), level
).mb_str()) == 0;
355 // ----------------------------------------------------------------------------
356 // wxStream classes to support IO redirection in wxExecute
357 // ----------------------------------------------------------------------------
361 bool wxPipeInputStream::CanRead() const
363 if ( m_lasterror
== wxSTREAM_EOF
)
366 // check if there is any input available
371 const int fd
= m_file
->fd();
375 FD_SET(fd
, &readfds
);
376 switch ( select(fd
+ 1, &readfds
, NULL
, NULL
, &tv
) )
379 wxLogSysError(_("Impossible to get child process input"));
386 wxFAIL_MSG(_T("unexpected select() return value"));
387 // still fall through
390 // input available -- or maybe not, as select() returns 1 when a
391 // read() will complete without delay, but it could still not read
397 #endif // wxUSE_STREAMS
399 // ----------------------------------------------------------------------------
400 // wxExecute: the real worker function
401 // ----------------------------------------------------------------------------
404 #pragma message disable codeunreachable
407 long wxExecute(wxChar
**argv
,
411 // for the sync execution, we return -1 to indicate failure, but for async
412 // case we return 0 which is never a valid PID
414 // we define this as a macro, not a variable, to avoid compiler warnings
415 // about "ERROR_RETURN_CODE value may be clobbered by fork()"
416 #define ERROR_RETURN_CODE ((flags & wxEXEC_SYNC) ? -1 : 0)
418 wxCHECK_MSG( *argv
, ERROR_RETURN_CODE
, wxT("can't exec empty command") );
422 char *mb_argv
[WXEXECUTE_NARGS
];
424 while (argv
[mb_argc
])
426 wxWX2MBbuf mb_arg
= wxConvertWX2MB(argv
[mb_argc
]);
427 mb_argv
[mb_argc
] = strdup(mb_arg
);
430 mb_argv
[mb_argc
] = (char *) NULL
;
432 // this macro will free memory we used above
433 #define ARGS_CLEANUP \
434 for ( mb_argc = 0; mb_argv[mb_argc]; mb_argc++ ) \
435 free(mb_argv[mb_argc])
437 // no need for cleanup
440 wxChar
**mb_argv
= argv
;
441 #endif // Unicode/ANSI
443 // we want this function to work even if there is no wxApp so ensure that
444 // we have a valid traits pointer
445 wxConsoleAppTraits traitsConsole
;
446 wxAppTraits
*traits
= wxTheApp
? wxTheApp
->GetTraits() : NULL
;
448 traits
= &traitsConsole
;
450 // this struct contains all information which we pass to and from
451 // wxAppTraits methods
452 wxExecuteData execData
;
453 execData
.flags
= flags
;
454 execData
.process
= process
;
457 if ( !traits
->CreateEndProcessPipe(execData
) )
459 wxLogError( _("Failed to execute '%s'\n"), *argv
);
463 return ERROR_RETURN_CODE
;
466 // pipes for inter process communication
467 wxPipe pipeIn
, // stdin
471 if ( process
&& process
->IsRedirected() )
473 if ( !pipeIn
.Create() || !pipeOut
.Create() || !pipeErr
.Create() )
475 wxLogError( _("Failed to execute '%s'\n"), *argv
);
479 return ERROR_RETURN_CODE
;
485 // NB: do *not* use vfork() here, it completely breaks this code for some
486 // reason under Solaris (and maybe others, although not under Linux)
487 // But on OpenVMS we do not have fork so we have to use vfork and
488 // cross our fingers that it works.
494 if ( pid
== -1 ) // error?
496 wxLogSysError( _("Fork failed") );
500 return ERROR_RETURN_CODE
;
502 else if ( pid
== 0 ) // we're in child
504 // These lines close the open file descriptors to to avoid any
505 // input/output which might block the process or irritate the user. If
506 // one wants proper IO for the subprocess, the right thing to do is to
507 // start an xterm executing it.
508 if ( !(flags
& wxEXEC_SYNC
) )
510 for ( int fd
= 0; fd
< FD_SETSIZE
; fd
++ )
512 if ( fd
== pipeIn
[wxPipe::Read
]
513 || fd
== pipeOut
[wxPipe::Write
]
514 || fd
== pipeErr
[wxPipe::Write
]
515 || traits
->IsWriteFDOfEndProcessPipe(execData
, fd
) )
517 // don't close this one, we still need it
521 // leave stderr opened too, it won't do any harm
522 if ( fd
!= STDERR_FILENO
)
527 #if !defined(__VMS) && !defined(__EMX__)
528 if ( flags
& wxEXEC_MAKE_GROUP_LEADER
)
530 // Set process group to child process' pid. Then killing -pid
531 // of the parent will kill the process and all of its children.
536 // reading side can be safely closed but we should keep the write one
538 traits
->DetachWriteFDOfEndProcessPipe(execData
);
540 // redirect stdin, stdout and stderr
543 if ( dup2(pipeIn
[wxPipe::Read
], STDIN_FILENO
) == -1 ||
544 dup2(pipeOut
[wxPipe::Write
], STDOUT_FILENO
) == -1 ||
545 dup2(pipeErr
[wxPipe::Write
], STDERR_FILENO
) == -1 )
547 wxLogSysError(_("Failed to redirect child process input/output"));
555 execvp (*mb_argv
, mb_argv
);
557 fprintf(stderr
, "execvp(");
558 // CS changed ppc to ppc_ as ppc is not available under mac os CW Mach-O
559 for ( char **ppc_
= mb_argv
; *ppc_
; ppc_
++ )
560 fprintf(stderr
, "%s%s", ppc_
== mb_argv
? "" : ", ", *ppc_
);
561 fprintf(stderr
, ") failed with error %d!\n", errno
);
563 // there is no return after successful exec()
566 // some compilers complain about missing return - of course, they
567 // should know that exit() doesn't return but what else can we do if
570 // and, sure enough, other compilers complain about unreachable code
571 // after exit() call, so we can just always have return here...
572 #if defined(__VMS) || defined(__INTEL_COMPILER)
576 else // we're in parent
580 // save it for WaitForChild() use
583 // prepare for IO redirection
586 // the input buffer bufOut is connected to stdout, this is why it is
587 // called bufOut and not bufIn
588 wxStreamTempInputBuffer bufOut
,
590 #endif // wxUSE_STREAMS
592 if ( process
&& process
->IsRedirected() )
595 wxOutputStream
*inStream
=
596 new wxFileOutputStream(pipeIn
.Detach(wxPipe::Write
));
598 wxPipeInputStream
*outStream
=
599 new wxPipeInputStream(pipeOut
.Detach(wxPipe::Read
));
601 wxPipeInputStream
*errStream
=
602 new wxPipeInputStream(pipeErr
.Detach(wxPipe::Read
));
604 process
->SetPipeStreams(outStream
, inStream
, errStream
);
606 bufOut
.Init(outStream
);
607 bufErr
.Init(errStream
);
609 execData
.bufOut
= &bufOut
;
610 execData
.bufErr
= &bufErr
;
611 #endif // wxUSE_STREAMS
621 return traits
->WaitForChild(execData
);
624 return ERROR_RETURN_CODE
;
628 #pragma message enable codeunreachable
631 #undef ERROR_RETURN_CODE
634 // ----------------------------------------------------------------------------
635 // file and directory functions
636 // ----------------------------------------------------------------------------
638 const wxChar
* wxGetHomeDir( wxString
*home
)
640 *home
= wxGetUserHome( wxString() );
642 if ( home
->IsEmpty() )
646 if ( tmp
.Last() != wxT(']'))
647 if ( tmp
.Last() != wxT('/')) *home
<< wxT('/');
649 return home
->c_str();
653 const wxMB2WXbuf
wxGetUserHome( const wxString
&user
)
654 #else // just for binary compatibility -- there is no 'const' here
655 char *wxGetUserHome( const wxString
&user
)
658 struct passwd
*who
= (struct passwd
*) NULL
;
664 if ((ptr
= wxGetenv(wxT("HOME"))) != NULL
)
667 wxWCharBuffer
buffer( ptr
);
673 if ((ptr
= wxGetenv(wxT("USER"))) != NULL
|| (ptr
= wxGetenv(wxT("LOGNAME"))) != NULL
)
675 who
= getpwnam(wxConvertWX2MB(ptr
));
678 // We now make sure the the user exists!
681 who
= getpwuid(getuid());
686 who
= getpwnam (user
.mb_str());
689 return wxConvertMB2WX(who
? who
->pw_dir
: 0);
692 // ----------------------------------------------------------------------------
693 // network and user id routines
694 // ----------------------------------------------------------------------------
696 // retrieve either the hostname or FQDN depending on platform (caller must
697 // check whether it's one or the other, this is why this function is for
699 static bool wxGetHostNameInternal(wxChar
*buf
, int sz
)
701 wxCHECK_MSG( buf
, FALSE
, wxT("NULL pointer in wxGetHostNameInternal") );
705 // we're using uname() which is POSIX instead of less standard sysinfo()
706 #if defined(HAVE_UNAME)
708 bool ok
= uname(&uts
) != -1;
711 wxStrncpy(buf
, wxConvertMB2WX(uts
.nodename
), sz
- 1);
714 #elif defined(HAVE_GETHOSTNAME)
715 bool ok
= gethostname(buf
, sz
) != -1;
716 #else // no uname, no gethostname
717 wxFAIL_MSG(wxT("don't know host name for this machine"));
720 #endif // uname/gethostname
724 wxLogSysError(_("Cannot get the hostname"));
730 bool wxGetHostName(wxChar
*buf
, int sz
)
732 bool ok
= wxGetHostNameInternal(buf
, sz
);
736 // BSD systems return the FQDN, we only want the hostname, so extract
737 // it (we consider that dots are domain separators)
738 wxChar
*dot
= wxStrchr(buf
, wxT('.'));
749 bool wxGetFullHostName(wxChar
*buf
, int sz
)
751 bool ok
= wxGetHostNameInternal(buf
, sz
);
755 if ( !wxStrchr(buf
, wxT('.')) )
757 struct hostent
*host
= gethostbyname(wxConvertWX2MB(buf
));
760 wxLogSysError(_("Cannot get the official hostname"));
766 // the canonical name
767 wxStrncpy(buf
, wxConvertMB2WX(host
->h_name
), sz
);
770 //else: it's already a FQDN (BSD behaves this way)
776 bool wxGetUserId(wxChar
*buf
, int sz
)
781 if ((who
= getpwuid(getuid ())) != NULL
)
783 wxStrncpy (buf
, wxConvertMB2WX(who
->pw_name
), sz
- 1);
790 bool wxGetUserName(wxChar
*buf
, int sz
)
795 if ((who
= getpwuid (getuid ())) != NULL
)
797 // pw_gecos field in struct passwd is not standard
799 char *comma
= strchr(who
->pw_gecos
, ',');
801 *comma
= '\0'; // cut off non-name comment fields
802 wxStrncpy (buf
, wxConvertMB2WX(who
->pw_gecos
), sz
- 1);
803 #else // !HAVE_PW_GECOS
804 wxStrncpy (buf
, wxConvertMB2WX(who
->pw_name
), sz
- 1);
805 #endif // HAVE_PW_GECOS/!HAVE_PW_GECOS
812 // this function is in mac/utils.cpp for wxMac
815 wxString
wxGetOsDescription()
817 #ifndef WXWIN_OS_DESCRIPTION
818 #error WXWIN_OS_DESCRIPTION should be defined in config.h by configure
820 return wxString::FromAscii( WXWIN_OS_DESCRIPTION
);
826 unsigned long wxGetProcessId()
828 return (unsigned long)getpid();
831 long wxGetFreeMemory()
833 #if defined(__LINUX__)
834 // get it from /proc/meminfo
835 FILE *fp
= fopen("/proc/meminfo", "r");
841 if ( fgets(buf
, WXSIZEOF(buf
), fp
) && fgets(buf
, WXSIZEOF(buf
), fp
) )
843 long memTotal
, memUsed
;
844 sscanf(buf
, "Mem: %ld %ld %ld", &memTotal
, &memUsed
, &memFree
);
851 #elif defined(__SUN__) && defined(_SC_AVPHYS_PAGES)
852 return sysconf(_SC_AVPHYS_PAGES
)*sysconf(_SC_PAGESIZE
);
853 //#elif defined(__FREEBSD__) -- might use sysctl() to find it out, probably
860 bool wxGetDiskSpace(const wxString
& path
, wxLongLong
*pTotal
, wxLongLong
*pFree
)
862 #if defined(HAVE_STATFS) || defined(HAVE_STATVFS)
863 // the case to "char *" is needed for AIX 4.3
865 if ( wxStatfs((char *)(const char*)path
.fn_str(), &fs
) != 0 )
867 wxLogSysError( wxT("Failed to get file system statistics") );
872 // under Solaris we also have to use f_frsize field instead of f_bsize
873 // which is in general a multiple of f_frsize
875 wxLongLong blockSize
= fs
.f_frsize
;
877 wxLongLong blockSize
= fs
.f_bsize
;
878 #endif // HAVE_STATVFS/HAVE_STATFS
882 *pTotal
= wxLongLong(fs
.f_blocks
) * blockSize
;
887 *pFree
= wxLongLong(fs
.f_bavail
) * blockSize
;
891 #else // !HAVE_STATFS && !HAVE_STATVFS
893 #endif // HAVE_STATFS
896 // ----------------------------------------------------------------------------
898 // ----------------------------------------------------------------------------
900 bool wxGetEnv(const wxString
& var
, wxString
*value
)
902 // wxGetenv is defined as getenv()
903 wxChar
*p
= wxGetenv(var
);
915 bool wxSetEnv(const wxString
& variable
, const wxChar
*value
)
917 #if defined(HAVE_SETENV)
918 return setenv(variable
.mb_str(),
919 value
? (const char *)wxString(value
).mb_str()
921 1 /* overwrite */) == 0;
922 #elif defined(HAVE_PUTENV)
923 wxString s
= variable
;
925 s
<< _T('=') << value
;
928 const char *p
= s
.mb_str();
930 // the string will be free()d by libc
931 char *buf
= (char *)malloc(strlen(p
) + 1);
934 return putenv(buf
) == 0;
935 #else // no way to set an env var
940 // ----------------------------------------------------------------------------
942 // ----------------------------------------------------------------------------
944 #if wxUSE_ON_FATAL_EXCEPTION
948 extern "C" void wxFatalSignalHandler(wxTYPE_SA_HANDLER
)
952 // give the user a chance to do something special about this
953 wxTheApp
->OnFatalException();
959 bool wxHandleFatalExceptions(bool doit
)
962 static bool s_savedHandlers
= FALSE
;
963 static struct sigaction s_handlerFPE
,
969 if ( doit
&& !s_savedHandlers
)
971 // install the signal handler
972 struct sigaction act
;
974 // some systems extend it with non std fields, so zero everything
975 memset(&act
, 0, sizeof(act
));
977 act
.sa_handler
= wxFatalSignalHandler
;
978 sigemptyset(&act
.sa_mask
);
981 ok
&= sigaction(SIGFPE
, &act
, &s_handlerFPE
) == 0;
982 ok
&= sigaction(SIGILL
, &act
, &s_handlerILL
) == 0;
983 ok
&= sigaction(SIGBUS
, &act
, &s_handlerBUS
) == 0;
984 ok
&= sigaction(SIGSEGV
, &act
, &s_handlerSEGV
) == 0;
987 wxLogDebug(_T("Failed to install our signal handler."));
990 s_savedHandlers
= TRUE
;
992 else if ( s_savedHandlers
)
994 // uninstall the signal handler
995 ok
&= sigaction(SIGFPE
, &s_handlerFPE
, NULL
) == 0;
996 ok
&= sigaction(SIGILL
, &s_handlerILL
, NULL
) == 0;
997 ok
&= sigaction(SIGBUS
, &s_handlerBUS
, NULL
) == 0;
998 ok
&= sigaction(SIGSEGV
, &s_handlerSEGV
, NULL
) == 0;
1001 wxLogDebug(_T("Failed to uninstall our signal handler."));
1004 s_savedHandlers
= FALSE
;
1006 //else: nothing to do
1011 #endif // wxUSE_ON_FATAL_EXCEPTION
1013 // ----------------------------------------------------------------------------
1014 // error and debug output routines (deprecated, use wxLog)
1015 // ----------------------------------------------------------------------------
1017 #if WXWIN_COMPATIBILITY_2_2
1019 void wxDebugMsg( const char *format
, ... )
1022 va_start( ap
, format
);
1023 vfprintf( stderr
, format
, ap
);
1028 void wxError( const wxString
&msg
, const wxString
&title
)
1030 wxFprintf( stderr
, _("Error ") );
1031 if (!title
.IsNull()) wxFprintf( stderr
, wxT("%s "), WXSTRINGCAST(title
) );
1032 if (!msg
.IsNull()) wxFprintf( stderr
, wxT(": %s"), WXSTRINGCAST(msg
) );
1033 wxFprintf( stderr
, wxT(".\n") );
1036 void wxFatalError( const wxString
&msg
, const wxString
&title
)
1038 wxFprintf( stderr
, _("Error ") );
1039 if (!title
.IsNull()) wxFprintf( stderr
, wxT("%s "), WXSTRINGCAST(title
) );
1040 if (!msg
.IsNull()) wxFprintf( stderr
, wxT(": %s"), WXSTRINGCAST(msg
) );
1041 wxFprintf( stderr
, wxT(".\n") );
1042 exit(3); // the same exit code as for abort()
1045 #endif // WXWIN_COMPATIBILITY_2_2
1047 #endif // wxUSE_BASE
1051 // ----------------------------------------------------------------------------
1052 // wxExecute support
1053 // ----------------------------------------------------------------------------
1055 // Darwin doesn't use the same process end detection mechanisms so we don't
1056 // need wxExecute-related helpers for it
1057 #if !(defined(__DARWIN__) && defined(__WXMAC__))
1059 bool wxGUIAppTraits::CreateEndProcessPipe(wxExecuteData
& execData
)
1061 return execData
.pipeEndProcDetect
.Create();
1064 bool wxGUIAppTraits::IsWriteFDOfEndProcessPipe(wxExecuteData
& execData
, int fd
)
1066 return fd
== (execData
.pipeEndProcDetect
)[wxPipe::Write
];
1069 void wxGUIAppTraits::DetachWriteFDOfEndProcessPipe(wxExecuteData
& execData
)
1071 execData
.pipeEndProcDetect
.Detach(wxPipe::Write
);
1072 execData
.pipeEndProcDetect
.Close();
1077 bool wxGUIAppTraits::CreateEndProcessPipe(wxExecuteData
& WXUNUSED(execData
))
1083 wxGUIAppTraits::IsWriteFDOfEndProcessPipe(wxExecuteData
& WXUNUSED(execData
),
1090 wxGUIAppTraits::DetachWriteFDOfEndProcessPipe(wxExecuteData
& WXUNUSED(execData
))
1092 // nothing to do here, we don't use the pipe
1095 #endif // !Darwin/Darwin
1097 int wxGUIAppTraits::WaitForChild(wxExecuteData
& execData
)
1099 wxEndProcessData
*endProcData
= new wxEndProcessData
;
1101 // wxAddProcessCallback is now (with DARWIN) allowed to call the
1102 // callback function directly if the process terminates before
1103 // the callback can be added to the run loop. Set up the endProcData.
1104 if ( execData
.flags
& wxEXEC_SYNC
)
1106 // we may have process for capturing the program output, but it's
1107 // not used in wxEndProcessData in the case of sync execution
1108 endProcData
->process
= NULL
;
1110 // sync execution: indicate it by negating the pid
1111 endProcData
->pid
= -execData
.pid
;
1115 // async execution, nothing special to do -- caller will be
1116 // notified about the process termination if process != NULL, endProcData
1117 // will be deleted in GTK_EndProcessDetector
1118 endProcData
->process
= execData
.process
;
1119 endProcData
->pid
= execData
.pid
;
1123 #if defined(__DARWIN__) && defined(__WXMAC__)
1124 endProcData
->tag
= wxAddProcessCallbackForPid(endProcData
, execData
.pid
);
1126 endProcData
->tag
= wxAddProcessCallback
1129 execData
.pipeEndProcDetect
.Detach(wxPipe::Read
)
1132 execData
.pipeEndProcDetect
.Close();
1133 #endif // defined(__DARWIN__) && defined(__WXMAC__)
1135 if ( execData
.flags
& wxEXEC_SYNC
)
1138 wxWindowDisabler wd
;
1140 // endProcData->pid will be set to 0 from GTK_EndProcessDetector when the
1141 // process terminates
1142 while ( endProcData
->pid
!= 0 )
1147 if ( execData
.bufOut
)
1149 execData
.bufOut
->Update();
1153 if ( execData
.bufErr
)
1155 execData
.bufErr
->Update();
1158 #endif // wxUSE_STREAMS
1160 // don't consume 100% of the CPU while we're sitting this in this
1165 // give GTK+ a chance to call GTK_EndProcessDetector here and
1166 // also repaint the GUI
1170 int exitcode
= endProcData
->exitcode
;
1176 else // async execution
1178 return execData
.pid
;
1185 void wxHandleProcessTermination(wxEndProcessData
*proc_data
)
1187 // notify user about termination if required
1188 if ( proc_data
->process
)
1190 proc_data
->process
->OnTerminate(proc_data
->pid
, proc_data
->exitcode
);
1194 if ( proc_data
->pid
> 0 )
1200 // let wxExecute() know that the process has terminated
1205 #endif // wxUSE_BASE