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 // ----------------------------------------------------------------------------
20 // for compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
24 #include "wx/string.h"
29 #include "wx/apptrait.h"
32 #include "wx/process.h"
33 #include "wx/thread.h"
35 #include "wx/wfstream.h"
37 #include "wx/unix/execute.h"
41 // define this to let wxexec.cpp know that we know what we're doing
42 #define _WX_USED_BY_WXEXECUTE_
43 #include "../common/execcmn.cpp"
45 #endif // wxUSE_STREAMS
49 #if defined(__MWERKS__) && defined(__MACH__)
50 #ifndef WXWIN_OS_DESCRIPTION
51 #define WXWIN_OS_DESCRIPTION "MacOS X"
53 #ifndef HAVE_NANOSLEEP
54 #define HAVE_NANOSLEEP
60 // our configure test believes we can use sigaction() if the function is
61 // available but Metrowekrs with MSL run-time does have the function but
62 // doesn't have sigaction struct so finally we can't use it...
64 #undef wxUSE_ON_FATAL_EXCEPTION
65 #define wxUSE_ON_FATAL_EXCEPTION 0
69 // not only the statfs syscall is called differently depending on platform, but
70 // one of its incarnations, statvfs(), takes different arguments under
71 // different platforms and even different versions of the same system (Solaris
72 // 7 and 8): if you want to test for this, don't forget that the problems only
73 // appear if the large files support is enabled
76 #include <sys/param.h>
77 #include <sys/mount.h>
80 #endif // __BSD__/!__BSD__
82 #define wxStatfs statfs
86 #include <sys/statvfs.h>
88 #define wxStatfs statvfs
89 #endif // HAVE_STATVFS
91 #if defined(HAVE_STATFS) || defined(HAVE_STATVFS)
92 // WX_STATFS_T is detected by configure
93 #define wxStatfs_t WX_STATFS_T
96 // SGI signal.h defines signal handler arguments differently depending on
97 // whether _LANGUAGE_C_PLUS_PLUS is set or not - do set it
98 #if defined(__SGI__) && !defined(_LANGUAGE_C_PLUS_PLUS)
99 #define _LANGUAGE_C_PLUS_PLUS 1
105 #include <sys/stat.h>
106 #include <sys/types.h>
107 #include <sys/wait.h>
112 #include <fcntl.h> // for O_WRONLY and friends
113 #include <time.h> // nanosleep() and/or usleep()
114 #include <ctype.h> // isspace()
115 #include <sys/time.h> // needed for FD_SETSIZE
118 #include <sys/utsname.h> // for uname()
121 // ----------------------------------------------------------------------------
122 // conditional compilation
123 // ----------------------------------------------------------------------------
125 // many versions of Unices have this function, but it is not defined in system
126 // headers - please add your system here if it is the case for your OS.
127 // SunOS < 5.6 (i.e. Solaris < 2.6) and DG-UX are like this.
128 #if !defined(HAVE_USLEEP) && \
129 (defined(__SUN__) && !defined(__SunOs_5_6) && \
130 !defined(__SunOs_5_7) && !defined(__SUNPRO_CC)) || \
131 defined(__osf__) || defined(__EMX__)
135 int usleep(unsigned int usec
);
138 /* I copied this from the XFree86 diffs. AV. */
139 #define INCL_DOSPROCESS
141 inline void usleep(unsigned long delay
)
143 DosSleep(delay
? (delay
/1000l) : 1l);
145 #else // !Sun && !EMX
146 void usleep(unsigned long usec
);
148 #endif // Sun/EMX/Something else
151 #define HAVE_USLEEP 1
152 #endif // Unices without usleep()
154 // ============================================================================
156 // ============================================================================
158 // ----------------------------------------------------------------------------
160 // ----------------------------------------------------------------------------
162 void wxSleep(int nSecs
)
167 void wxMicroSleep(unsigned long microseconds
)
169 #if defined(HAVE_NANOSLEEP)
171 tmReq
.tv_sec
= (time_t)(microseconds
/ 1000000);
172 tmReq
.tv_nsec
= (microseconds
% 1000000) * 1000;
174 // we're not interested in remaining time nor in return value
175 (void)nanosleep(&tmReq
, (timespec
*)NULL
);
176 #elif defined(HAVE_USLEEP)
177 // uncomment this if you feel brave or if you are sure that your version
178 // of Solaris has a safe usleep() function but please notice that usleep()
179 // is known to lead to crashes in MT programs in Solaris 2.[67] and is not
180 // documented as MT-Safe
181 #if defined(__SUN__) && wxUSE_THREADS
182 #error "usleep() cannot be used in MT programs under Solaris."
185 usleep(microseconds
);
186 #elif defined(HAVE_SLEEP)
187 // under BeOS sleep() takes seconds (what about other platforms, if any?)
188 sleep(microseconds
* 1000000);
189 #else // !sleep function
190 #error "usleep() or nanosleep() function required for wxMicroSleep"
191 #endif // sleep function
194 void wxMilliSleep(unsigned long milliseconds
)
196 wxMicroSleep(milliseconds
*1000);
199 // ----------------------------------------------------------------------------
200 // process management
201 // ----------------------------------------------------------------------------
203 int wxKill(long pid
, wxSignal sig
, wxKillError
*rc
)
205 int err
= kill((pid_t
)pid
, (int)sig
);
215 *rc
= wxKILL_BAD_SIGNAL
;
219 *rc
= wxKILL_ACCESS_DENIED
;
223 *rc
= wxKILL_NO_PROCESS
;
227 // this goes against Unix98 docs so log it
228 wxLogDebug(_T("unexpected kill(2) return value %d"), err
);
238 #define WXEXECUTE_NARGS 127
240 long wxExecute( const wxString
& command
, int flags
, wxProcess
*process
)
242 wxCHECK_MSG( !command
.IsEmpty(), 0, wxT("can't exec empty command") );
245 // fork() doesn't mix well with POSIX threads: on many systems the program
246 // deadlocks or crashes for some reason. Probably our code is buggy and
247 // doesn't do something which must be done to allow this to work, but I
248 // don't know what yet, so for now just warn the user (this is the least we
250 wxASSERT_MSG( wxThread::IsMain(),
251 _T("wxExecute() can be called only from the main thread") );
252 #endif // wxUSE_THREADS
255 wxChar
*argv
[WXEXECUTE_NARGS
];
257 const wxChar
*cptr
= command
.c_str();
258 wxChar quotechar
= wxT('\0'); // is arg quoted?
259 bool escaped
= FALSE
;
261 // split the command line in arguments
265 quotechar
= wxT('\0');
267 // eat leading whitespace:
268 while ( wxIsspace(*cptr
) )
271 if ( *cptr
== wxT('\'') || *cptr
== wxT('"') )
276 if ( *cptr
== wxT('\\') && ! escaped
)
283 // all other characters:
287 // have we reached the end of the argument?
288 if ( (*cptr
== quotechar
&& ! escaped
)
289 || (quotechar
== wxT('\0') && wxIsspace(*cptr
))
290 || *cptr
== wxT('\0') )
292 wxASSERT_MSG( argc
< WXEXECUTE_NARGS
,
293 wxT("too many arguments in wxExecute") );
295 argv
[argc
] = new wxChar
[argument
.length() + 1];
296 wxStrcpy(argv
[argc
], argument
.c_str());
299 // if not at end of buffer, swallow last character:
303 break; // done with this one, start over
309 // do execute the command
310 long lRc
= wxExecute(argv
, flags
, process
);
315 delete [] argv
[argc
++];
320 // ----------------------------------------------------------------------------
322 // ----------------------------------------------------------------------------
324 static wxString
wxMakeShellCommand(const wxString
& command
)
329 // just an interactive shell
334 // execute command in a shell
335 cmd
<< _T("/bin/sh -c '") << command
<< _T('\'');
341 bool wxShell(const wxString
& command
)
343 return wxExecute(wxMakeShellCommand(command
), wxEXEC_SYNC
) == 0;
346 bool wxShell(const wxString
& command
, wxArrayString
& output
)
348 wxCHECK_MSG( !!command
, FALSE
, _T("can't exec shell non interactively") );
350 return wxExecute(wxMakeShellCommand(command
), output
);
353 // Shutdown or reboot the PC
354 bool wxShutdown(wxShutdownFlags wFlags
)
359 case wxSHUTDOWN_POWEROFF
:
363 case wxSHUTDOWN_REBOOT
:
368 wxFAIL_MSG( _T("unknown wxShutdown() flag") );
372 return system(wxString::Format(_T("init %c"), level
).mb_str()) == 0;
376 // ----------------------------------------------------------------------------
377 // wxStream classes to support IO redirection in wxExecute
378 // ----------------------------------------------------------------------------
382 bool wxPipeInputStream::CanRead() const
384 if ( m_lasterror
== wxSTREAM_EOF
)
387 // check if there is any input available
392 const int fd
= m_file
->fd();
396 FD_SET(fd
, &readfds
);
397 switch ( select(fd
+ 1, &readfds
, NULL
, NULL
, &tv
) )
400 wxLogSysError(_("Impossible to get child process input"));
407 wxFAIL_MSG(_T("unexpected select() return value"));
408 // still fall through
411 // input available -- or maybe not, as select() returns 1 when a
412 // read() will complete without delay, but it could still not read
418 #endif // wxUSE_STREAMS
420 // ----------------------------------------------------------------------------
421 // wxExecute: the real worker function
422 // ----------------------------------------------------------------------------
425 #pragma message disable codeunreachable
428 long wxExecute(wxChar
**argv
,
432 // for the sync execution, we return -1 to indicate failure, but for async
433 // case we return 0 which is never a valid PID
435 // we define this as a macro, not a variable, to avoid compiler warnings
436 // about "ERROR_RETURN_CODE value may be clobbered by fork()"
437 #define ERROR_RETURN_CODE ((flags & wxEXEC_SYNC) ? -1 : 0)
439 wxCHECK_MSG( *argv
, ERROR_RETURN_CODE
, wxT("can't exec empty command") );
443 char *mb_argv
[WXEXECUTE_NARGS
];
445 while (argv
[mb_argc
])
447 wxWX2MBbuf mb_arg
= wxConvertWX2MB(argv
[mb_argc
]);
448 mb_argv
[mb_argc
] = strdup(mb_arg
);
451 mb_argv
[mb_argc
] = (char *) NULL
;
453 // this macro will free memory we used above
454 #define ARGS_CLEANUP \
455 for ( mb_argc = 0; mb_argv[mb_argc]; mb_argc++ ) \
456 free(mb_argv[mb_argc])
458 // no need for cleanup
461 wxChar
**mb_argv
= argv
;
462 #endif // Unicode/ANSI
464 // we want this function to work even if there is no wxApp so ensure that
465 // we have a valid traits pointer
466 wxConsoleAppTraits traitsConsole
;
467 wxAppTraits
*traits
= wxTheApp
? wxTheApp
->GetTraits() : NULL
;
469 traits
= &traitsConsole
;
471 // this struct contains all information which we pass to and from
472 // wxAppTraits methods
473 wxExecuteData execData
;
474 execData
.flags
= flags
;
475 execData
.process
= process
;
478 if ( !traits
->CreateEndProcessPipe(execData
) )
480 wxLogError( _("Failed to execute '%s'\n"), *argv
);
484 return ERROR_RETURN_CODE
;
487 // pipes for inter process communication
488 wxPipe pipeIn
, // stdin
492 if ( process
&& process
->IsRedirected() )
494 if ( !pipeIn
.Create() || !pipeOut
.Create() || !pipeErr
.Create() )
496 wxLogError( _("Failed to execute '%s'\n"), *argv
);
500 return ERROR_RETURN_CODE
;
506 // NB: do *not* use vfork() here, it completely breaks this code for some
507 // reason under Solaris (and maybe others, although not under Linux)
508 // But on OpenVMS we do not have fork so we have to use vfork and
509 // cross our fingers that it works.
515 if ( pid
== -1 ) // error?
517 wxLogSysError( _("Fork failed") );
521 return ERROR_RETURN_CODE
;
523 else if ( pid
== 0 ) // we're in child
525 // These lines close the open file descriptors to to avoid any
526 // input/output which might block the process or irritate the user. If
527 // one wants proper IO for the subprocess, the right thing to do is to
528 // start an xterm executing it.
529 if ( !(flags
& wxEXEC_SYNC
) )
531 // FD_SETSIZE is unsigned under BSD, signed under other platforms
532 // so we need a cast to avoid warnings on all platforms
533 for ( int fd
= 0; fd
< (int)FD_SETSIZE
; fd
++ )
535 if ( fd
== pipeIn
[wxPipe::Read
]
536 || fd
== pipeOut
[wxPipe::Write
]
537 || fd
== pipeErr
[wxPipe::Write
]
538 || traits
->IsWriteFDOfEndProcessPipe(execData
, fd
) )
540 // don't close this one, we still need it
544 // leave stderr opened too, it won't do any harm
545 if ( fd
!= STDERR_FILENO
)
550 #if !defined(__VMS) && !defined(__EMX__)
551 if ( flags
& wxEXEC_MAKE_GROUP_LEADER
)
553 // Set process group to child process' pid. Then killing -pid
554 // of the parent will kill the process and all of its children.
559 // reading side can be safely closed but we should keep the write one
561 traits
->DetachWriteFDOfEndProcessPipe(execData
);
563 // redirect stdin, stdout and stderr
566 if ( dup2(pipeIn
[wxPipe::Read
], STDIN_FILENO
) == -1 ||
567 dup2(pipeOut
[wxPipe::Write
], STDOUT_FILENO
) == -1 ||
568 dup2(pipeErr
[wxPipe::Write
], STDERR_FILENO
) == -1 )
570 wxLogSysError(_("Failed to redirect child process input/output"));
578 execvp (*mb_argv
, mb_argv
);
580 fprintf(stderr
, "execvp(");
581 // CS changed ppc to ppc_ as ppc is not available under mac os CW Mach-O
582 for ( char **ppc_
= mb_argv
; *ppc_
; ppc_
++ )
583 fprintf(stderr
, "%s%s", ppc_
== mb_argv
? "" : ", ", *ppc_
);
584 fprintf(stderr
, ") failed with error %d!\n", errno
);
586 // there is no return after successful exec()
589 // some compilers complain about missing return - of course, they
590 // should know that exit() doesn't return but what else can we do if
593 // and, sure enough, other compilers complain about unreachable code
594 // after exit() call, so we can just always have return here...
595 #if defined(__VMS) || defined(__INTEL_COMPILER)
599 else // we're in parent
603 // save it for WaitForChild() use
606 // prepare for IO redirection
609 // the input buffer bufOut is connected to stdout, this is why it is
610 // called bufOut and not bufIn
611 wxStreamTempInputBuffer bufOut
,
613 #endif // wxUSE_STREAMS
615 if ( process
&& process
->IsRedirected() )
618 wxOutputStream
*inStream
=
619 new wxFileOutputStream(pipeIn
.Detach(wxPipe::Write
));
621 wxPipeInputStream
*outStream
=
622 new wxPipeInputStream(pipeOut
.Detach(wxPipe::Read
));
624 wxPipeInputStream
*errStream
=
625 new wxPipeInputStream(pipeErr
.Detach(wxPipe::Read
));
627 process
->SetPipeStreams(outStream
, inStream
, errStream
);
629 bufOut
.Init(outStream
);
630 bufErr
.Init(errStream
);
632 execData
.bufOut
= &bufOut
;
633 execData
.bufErr
= &bufErr
;
634 #endif // wxUSE_STREAMS
644 return traits
->WaitForChild(execData
);
647 return ERROR_RETURN_CODE
;
651 #pragma message enable codeunreachable
654 #undef ERROR_RETURN_CODE
657 // ----------------------------------------------------------------------------
658 // file and directory functions
659 // ----------------------------------------------------------------------------
661 const wxChar
* wxGetHomeDir( wxString
*home
)
663 *home
= wxGetUserHome( wxString() );
665 if ( home
->IsEmpty() )
669 if ( tmp
.Last() != wxT(']'))
670 if ( tmp
.Last() != wxT('/')) *home
<< wxT('/');
672 return home
->c_str();
676 const wxMB2WXbuf
wxGetUserHome( const wxString
&user
)
677 #else // just for binary compatibility -- there is no 'const' here
678 char *wxGetUserHome( const wxString
&user
)
681 struct passwd
*who
= (struct passwd
*) NULL
;
687 if ((ptr
= wxGetenv(wxT("HOME"))) != NULL
)
690 wxWCharBuffer
buffer( ptr
);
696 if ((ptr
= wxGetenv(wxT("USER"))) != NULL
|| (ptr
= wxGetenv(wxT("LOGNAME"))) != NULL
)
698 who
= getpwnam(wxConvertWX2MB(ptr
));
701 // We now make sure the the user exists!
704 who
= getpwuid(getuid());
709 who
= getpwnam (user
.mb_str());
712 return wxConvertMB2WX(who
? who
->pw_dir
: 0);
715 // ----------------------------------------------------------------------------
716 // network and user id routines
717 // ----------------------------------------------------------------------------
719 // retrieve either the hostname or FQDN depending on platform (caller must
720 // check whether it's one or the other, this is why this function is for
722 static bool wxGetHostNameInternal(wxChar
*buf
, int sz
)
724 wxCHECK_MSG( buf
, FALSE
, wxT("NULL pointer in wxGetHostNameInternal") );
728 // we're using uname() which is POSIX instead of less standard sysinfo()
729 #if defined(HAVE_UNAME)
731 bool ok
= uname(&uts
) != -1;
734 wxStrncpy(buf
, wxConvertMB2WX(uts
.nodename
), sz
- 1);
737 #elif defined(HAVE_GETHOSTNAME)
738 bool ok
= gethostname(buf
, sz
) != -1;
739 #else // no uname, no gethostname
740 wxFAIL_MSG(wxT("don't know host name for this machine"));
743 #endif // uname/gethostname
747 wxLogSysError(_("Cannot get the hostname"));
753 bool wxGetHostName(wxChar
*buf
, int sz
)
755 bool ok
= wxGetHostNameInternal(buf
, sz
);
759 // BSD systems return the FQDN, we only want the hostname, so extract
760 // it (we consider that dots are domain separators)
761 wxChar
*dot
= wxStrchr(buf
, wxT('.'));
772 bool wxGetFullHostName(wxChar
*buf
, int sz
)
774 bool ok
= wxGetHostNameInternal(buf
, sz
);
778 if ( !wxStrchr(buf
, wxT('.')) )
780 struct hostent
*host
= gethostbyname(wxConvertWX2MB(buf
));
783 wxLogSysError(_("Cannot get the official hostname"));
789 // the canonical name
790 wxStrncpy(buf
, wxConvertMB2WX(host
->h_name
), sz
);
793 //else: it's already a FQDN (BSD behaves this way)
799 bool wxGetUserId(wxChar
*buf
, int sz
)
804 if ((who
= getpwuid(getuid ())) != NULL
)
806 wxStrncpy (buf
, wxConvertMB2WX(who
->pw_name
), sz
- 1);
813 bool wxGetUserName(wxChar
*buf
, int sz
)
818 if ((who
= getpwuid (getuid ())) != NULL
)
820 // pw_gecos field in struct passwd is not standard
822 char *comma
= strchr(who
->pw_gecos
, ',');
824 *comma
= '\0'; // cut off non-name comment fields
825 wxStrncpy (buf
, wxConvertMB2WX(who
->pw_gecos
), sz
- 1);
826 #else // !HAVE_PW_GECOS
827 wxStrncpy (buf
, wxConvertMB2WX(who
->pw_name
), sz
- 1);
828 #endif // HAVE_PW_GECOS/!HAVE_PW_GECOS
835 // this function is in mac/utils.cpp for wxMac
838 wxString
wxGetOsDescription()
840 #ifndef WXWIN_OS_DESCRIPTION
841 #error WXWIN_OS_DESCRIPTION should be defined in config.h by configure
843 return wxString::FromAscii( WXWIN_OS_DESCRIPTION
);
849 unsigned long wxGetProcessId()
851 return (unsigned long)getpid();
854 long wxGetFreeMemory()
856 #if defined(__LINUX__)
857 // get it from /proc/meminfo
858 FILE *fp
= fopen("/proc/meminfo", "r");
864 if ( fgets(buf
, WXSIZEOF(buf
), fp
) && fgets(buf
, WXSIZEOF(buf
), fp
) )
866 long memTotal
, memUsed
;
867 sscanf(buf
, "Mem: %ld %ld %ld", &memTotal
, &memUsed
, &memFree
);
874 #elif defined(__SUN__) && defined(_SC_AVPHYS_PAGES)
875 return sysconf(_SC_AVPHYS_PAGES
)*sysconf(_SC_PAGESIZE
);
876 //#elif defined(__FREEBSD__) -- might use sysctl() to find it out, probably
883 bool wxGetDiskSpace(const wxString
& path
, wxLongLong
*pTotal
, wxLongLong
*pFree
)
885 #if defined(HAVE_STATFS) || defined(HAVE_STATVFS)
886 // the case to "char *" is needed for AIX 4.3
888 if ( wxStatfs((char *)(const char*)path
.fn_str(), &fs
) != 0 )
890 wxLogSysError( wxT("Failed to get file system statistics") );
895 // under Solaris we also have to use f_frsize field instead of f_bsize
896 // which is in general a multiple of f_frsize
898 wxLongLong blockSize
= fs
.f_frsize
;
900 wxLongLong blockSize
= fs
.f_bsize
;
901 #endif // HAVE_STATVFS/HAVE_STATFS
905 *pTotal
= wxLongLong(fs
.f_blocks
) * blockSize
;
910 *pFree
= wxLongLong(fs
.f_bavail
) * blockSize
;
914 #else // !HAVE_STATFS && !HAVE_STATVFS
916 #endif // HAVE_STATFS
919 // ----------------------------------------------------------------------------
921 // ----------------------------------------------------------------------------
923 bool wxGetEnv(const wxString
& var
, wxString
*value
)
925 // wxGetenv is defined as getenv()
926 wxChar
*p
= wxGetenv(var
);
938 bool wxSetEnv(const wxString
& variable
, const wxChar
*value
)
940 #if defined(HAVE_SETENV)
941 return setenv(variable
.mb_str(),
942 value
? (const char *)wxString(value
).mb_str()
944 1 /* overwrite */) == 0;
945 #elif defined(HAVE_PUTENV)
946 wxString s
= variable
;
948 s
<< _T('=') << value
;
951 const wxWX2MBbuf p
= s
.mb_str();
953 // the string will be free()d by libc
954 char *buf
= (char *)malloc(strlen(p
) + 1);
957 return putenv(buf
) == 0;
958 #else // no way to set an env var
963 // ----------------------------------------------------------------------------
965 // ----------------------------------------------------------------------------
967 #if wxUSE_ON_FATAL_EXCEPTION
971 extern "C" void wxFatalSignalHandler(wxTYPE_SA_HANDLER
)
975 // give the user a chance to do something special about this
976 wxTheApp
->OnFatalException();
982 bool wxHandleFatalExceptions(bool doit
)
985 static bool s_savedHandlers
= FALSE
;
986 static struct sigaction s_handlerFPE
,
992 if ( doit
&& !s_savedHandlers
)
994 // install the signal handler
995 struct sigaction act
;
997 // some systems extend it with non std fields, so zero everything
998 memset(&act
, 0, sizeof(act
));
1000 act
.sa_handler
= wxFatalSignalHandler
;
1001 sigemptyset(&act
.sa_mask
);
1004 ok
&= sigaction(SIGFPE
, &act
, &s_handlerFPE
) == 0;
1005 ok
&= sigaction(SIGILL
, &act
, &s_handlerILL
) == 0;
1006 ok
&= sigaction(SIGBUS
, &act
, &s_handlerBUS
) == 0;
1007 ok
&= sigaction(SIGSEGV
, &act
, &s_handlerSEGV
) == 0;
1010 wxLogDebug(_T("Failed to install our signal handler."));
1013 s_savedHandlers
= TRUE
;
1015 else if ( s_savedHandlers
)
1017 // uninstall the signal handler
1018 ok
&= sigaction(SIGFPE
, &s_handlerFPE
, NULL
) == 0;
1019 ok
&= sigaction(SIGILL
, &s_handlerILL
, NULL
) == 0;
1020 ok
&= sigaction(SIGBUS
, &s_handlerBUS
, NULL
) == 0;
1021 ok
&= sigaction(SIGSEGV
, &s_handlerSEGV
, NULL
) == 0;
1024 wxLogDebug(_T("Failed to uninstall our signal handler."));
1027 s_savedHandlers
= FALSE
;
1029 //else: nothing to do
1034 #endif // wxUSE_ON_FATAL_EXCEPTION
1036 // ----------------------------------------------------------------------------
1037 // error and debug output routines (deprecated, use wxLog)
1038 // ----------------------------------------------------------------------------
1040 #if WXWIN_COMPATIBILITY_2_2
1042 void wxDebugMsg( const char *format
, ... )
1045 va_start( ap
, format
);
1046 vfprintf( stderr
, format
, ap
);
1051 void wxError( const wxString
&msg
, const wxString
&title
)
1053 wxFprintf( stderr
, _("Error ") );
1054 if (!title
.IsNull()) wxFprintf( stderr
, wxT("%s "), WXSTRINGCAST(title
) );
1055 if (!msg
.IsNull()) wxFprintf( stderr
, wxT(": %s"), WXSTRINGCAST(msg
) );
1056 wxFprintf( stderr
, wxT(".\n") );
1059 void wxFatalError( const wxString
&msg
, const wxString
&title
)
1061 wxFprintf( stderr
, _("Error ") );
1062 if (!title
.IsNull()) wxFprintf( stderr
, wxT("%s "), WXSTRINGCAST(title
) );
1063 if (!msg
.IsNull()) wxFprintf( stderr
, wxT(": %s"), WXSTRINGCAST(msg
) );
1064 wxFprintf( stderr
, wxT(".\n") );
1065 exit(3); // the same exit code as for abort()
1068 #endif // WXWIN_COMPATIBILITY_2_2
1070 #endif // wxUSE_BASE
1074 // ----------------------------------------------------------------------------
1075 // wxExecute support
1076 // ----------------------------------------------------------------------------
1078 // Darwin doesn't use the same process end detection mechanisms so we don't
1079 // need wxExecute-related helpers for it
1080 #if !(defined(__DARWIN__) && defined(__WXMAC__))
1082 bool wxGUIAppTraits::CreateEndProcessPipe(wxExecuteData
& execData
)
1084 return execData
.pipeEndProcDetect
.Create();
1087 bool wxGUIAppTraits::IsWriteFDOfEndProcessPipe(wxExecuteData
& execData
, int fd
)
1089 return fd
== (execData
.pipeEndProcDetect
)[wxPipe::Write
];
1092 void wxGUIAppTraits::DetachWriteFDOfEndProcessPipe(wxExecuteData
& execData
)
1094 execData
.pipeEndProcDetect
.Detach(wxPipe::Write
);
1095 execData
.pipeEndProcDetect
.Close();
1100 bool wxGUIAppTraits::CreateEndProcessPipe(wxExecuteData
& WXUNUSED(execData
))
1106 wxGUIAppTraits::IsWriteFDOfEndProcessPipe(wxExecuteData
& WXUNUSED(execData
),
1113 wxGUIAppTraits::DetachWriteFDOfEndProcessPipe(wxExecuteData
& WXUNUSED(execData
))
1115 // nothing to do here, we don't use the pipe
1118 #endif // !Darwin/Darwin
1120 int wxGUIAppTraits::WaitForChild(wxExecuteData
& execData
)
1122 wxEndProcessData
*endProcData
= new wxEndProcessData
;
1124 // wxAddProcessCallback is now (with DARWIN) allowed to call the
1125 // callback function directly if the process terminates before
1126 // the callback can be added to the run loop. Set up the endProcData.
1127 if ( execData
.flags
& wxEXEC_SYNC
)
1129 // we may have process for capturing the program output, but it's
1130 // not used in wxEndProcessData in the case of sync execution
1131 endProcData
->process
= NULL
;
1133 // sync execution: indicate it by negating the pid
1134 endProcData
->pid
= -execData
.pid
;
1138 // async execution, nothing special to do -- caller will be
1139 // notified about the process termination if process != NULL, endProcData
1140 // will be deleted in GTK_EndProcessDetector
1141 endProcData
->process
= execData
.process
;
1142 endProcData
->pid
= execData
.pid
;
1146 #if defined(__DARWIN__) && defined(__WXMAC__)
1147 endProcData
->tag
= wxAddProcessCallbackForPid(endProcData
, execData
.pid
);
1149 endProcData
->tag
= wxAddProcessCallback
1152 execData
.pipeEndProcDetect
.Detach(wxPipe::Read
)
1155 execData
.pipeEndProcDetect
.Close();
1156 #endif // defined(__DARWIN__) && defined(__WXMAC__)
1158 if ( execData
.flags
& wxEXEC_SYNC
)
1161 wxWindowDisabler wd
;
1163 // endProcData->pid will be set to 0 from GTK_EndProcessDetector when the
1164 // process terminates
1165 while ( endProcData
->pid
!= 0 )
1170 if ( execData
.bufOut
)
1172 execData
.bufOut
->Update();
1176 if ( execData
.bufErr
)
1178 execData
.bufErr
->Update();
1181 #endif // wxUSE_STREAMS
1183 // don't consume 100% of the CPU while we're sitting in this
1188 // give GTK+ a chance to call GTK_EndProcessDetector here and
1189 // also repaint the GUI
1193 int exitcode
= endProcData
->exitcode
;
1199 else // async execution
1201 return execData
.pid
;
1208 void wxHandleProcessTermination(wxEndProcessData
*proc_data
)
1210 // notify user about termination if required
1211 if ( proc_data
->process
)
1213 proc_data
->process
->OnTerminate(proc_data
->pid
, proc_data
->exitcode
);
1217 if ( proc_data
->pid
> 0 )
1223 // let wxExecute() know that the process has terminated
1228 #endif // wxUSE_BASE