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 // ----------------------------------------------------------------------------
19 #include "wx/string.h"
26 #include "wx/process.h"
27 #include "wx/thread.h"
29 #include "wx/wfstream.h"
31 #include "wx/unix/execute.h"
35 #if defined( __MWERKS__ ) && defined(__MACH__)
36 #define WXWIN_OS_DESCRIPTION "MacOS X"
37 #define HAVE_NANOSLEEP
40 // not only the statfs syscall is called differently depending on platform, but
41 // one of its incarnations, statvfs(), takes different arguments under
42 // different platforms and even different versions of the same system (Solaris
43 // 7 and 8): if you want to test for this, don't forget that the problems only
44 // appear if the large files support is enabled
47 #include <sys/param.h>
48 #include <sys/mount.h>
51 #endif // __BSD__/!__BSD__
53 #define wxStatfs statfs
57 #include <sys/statvfs.h>
59 #define wxStatfs statvfs
60 #endif // HAVE_STATVFS
62 #if defined(HAVE_STATFS) || defined(HAVE_STATVFS)
63 // WX_STATFS_T is detected by configure
64 #define wxStatfs_t WX_STATFS_T
67 // SGI signal.h defines signal handler arguments differently depending on
68 // whether _LANGUAGE_C_PLUS_PLUS is set or not - do set it
69 #if defined(__SGI__) && !defined(_LANGUAGE_C_PLUS_PLUS)
70 #define _LANGUAGE_C_PLUS_PLUS 1
77 #include <sys/types.h>
84 #include <fcntl.h> // for O_WRONLY and friends
85 #include <time.h> // nanosleep() and/or usleep()
86 #include <ctype.h> // isspace()
87 #include <sys/time.h> // needed for FD_SETSIZE
90 #include <sys/utsname.h> // for uname()
93 // ----------------------------------------------------------------------------
94 // conditional compilation
95 // ----------------------------------------------------------------------------
97 // many versions of Unices have this function, but it is not defined in system
98 // headers - please add your system here if it is the case for your OS.
99 // SunOS < 5.6 (i.e. Solaris < 2.6) and DG-UX are like this.
100 #if !defined(HAVE_USLEEP) && \
101 (defined(__SUN__) && !defined(__SunOs_5_6) && \
102 !defined(__SunOs_5_7) && !defined(__SUNPRO_CC)) || \
103 defined(__osf__) || defined(__EMX__)
107 int usleep(unsigned int usec
);
110 /* I copied this from the XFree86 diffs. AV. */
111 #define INCL_DOSPROCESS
113 inline void usleep(unsigned long delay
)
115 DosSleep(delay
? (delay
/1000l) : 1l);
117 #else // !Sun && !EMX
118 void usleep(unsigned long usec
);
120 #endif // Sun/EMX/Something else
123 #define HAVE_USLEEP 1
124 #endif // Unices without usleep()
126 // ============================================================================
128 // ============================================================================
130 // ----------------------------------------------------------------------------
132 // ----------------------------------------------------------------------------
134 void wxSleep(int nSecs
)
139 void wxUsleep(unsigned long milliseconds
)
141 #if defined(HAVE_NANOSLEEP)
143 tmReq
.tv_sec
= (time_t)(milliseconds
/ 1000);
144 tmReq
.tv_nsec
= (milliseconds
% 1000) * 1000 * 1000;
146 // we're not interested in remaining time nor in return value
147 (void)nanosleep(&tmReq
, (timespec
*)NULL
);
148 #elif defined(HAVE_USLEEP)
149 // uncomment this if you feel brave or if you are sure that your version
150 // of Solaris has a safe usleep() function but please notice that usleep()
151 // is known to lead to crashes in MT programs in Solaris 2.[67] and is not
152 // documented as MT-Safe
153 #if defined(__SUN__) && wxUSE_THREADS
154 #error "usleep() cannot be used in MT programs under Solaris."
157 usleep(milliseconds
* 1000); // usleep(3) wants microseconds
158 #elif defined(HAVE_SLEEP)
159 // under BeOS sleep() takes seconds (what about other platforms, if any?)
160 sleep(milliseconds
* 1000);
161 #else // !sleep function
162 #error "usleep() or nanosleep() function required for wxUsleep"
163 #endif // sleep function
166 // ----------------------------------------------------------------------------
167 // process management
168 // ----------------------------------------------------------------------------
170 int wxKill(long pid
, wxSignal sig
, wxKillError
*rc
)
172 int err
= kill((pid_t
)pid
, (int)sig
);
182 *rc
= wxKILL_BAD_SIGNAL
;
186 *rc
= wxKILL_ACCESS_DENIED
;
190 *rc
= wxKILL_NO_PROCESS
;
194 // this goes against Unix98 docs so log it
195 wxLogDebug(_T("unexpected kill(2) return value %d"), err
);
205 #define WXEXECUTE_NARGS 127
207 long wxExecute( const wxString
& command
, int flags
, wxProcess
*process
)
209 wxCHECK_MSG( !command
.IsEmpty(), 0, wxT("can't exec empty command") );
212 // fork() doesn't mix well with POSIX threads: on many systems the program
213 // deadlocks or crashes for some reason. Probably our code is buggy and
214 // doesn't do something which must be done to allow this to work, but I
215 // don't know what yet, so for now just warn the user (this is the least we
217 wxASSERT_MSG( wxThread::IsMain(),
218 _T("wxExecute() can be called only from the main thread") );
219 #endif // wxUSE_THREADS
222 wxChar
*argv
[WXEXECUTE_NARGS
];
224 const wxChar
*cptr
= command
.c_str();
225 wxChar quotechar
= wxT('\0'); // is arg quoted?
226 bool escaped
= FALSE
;
228 // split the command line in arguments
232 quotechar
= wxT('\0');
234 // eat leading whitespace:
235 while ( wxIsspace(*cptr
) )
238 if ( *cptr
== wxT('\'') || *cptr
== wxT('"') )
243 if ( *cptr
== wxT('\\') && ! escaped
)
250 // all other characters:
254 // have we reached the end of the argument?
255 if ( (*cptr
== quotechar
&& ! escaped
)
256 || (quotechar
== wxT('\0') && wxIsspace(*cptr
))
257 || *cptr
== wxT('\0') )
259 wxASSERT_MSG( argc
< WXEXECUTE_NARGS
,
260 wxT("too many arguments in wxExecute") );
262 argv
[argc
] = new wxChar
[argument
.length() + 1];
263 wxStrcpy(argv
[argc
], argument
.c_str());
266 // if not at end of buffer, swallow last character:
270 break; // done with this one, start over
276 // do execute the command
277 long lRc
= wxExecute(argv
, flags
, process
);
282 delete [] argv
[argc
++];
287 // ----------------------------------------------------------------------------
289 // ----------------------------------------------------------------------------
291 static wxString
wxMakeShellCommand(const wxString
& command
)
296 // just an interactive shell
301 // execute command in a shell
302 cmd
<< _T("/bin/sh -c '") << command
<< _T('\'');
308 bool wxShell(const wxString
& command
)
310 return wxExecute(wxMakeShellCommand(command
), wxEXEC_SYNC
) == 0;
313 bool wxShell(const wxString
& command
, wxArrayString
& output
)
315 wxCHECK_MSG( !!command
, FALSE
, _T("can't exec shell non interactively") );
317 return wxExecute(wxMakeShellCommand(command
), output
);
320 // Shutdown or reboot the PC
321 bool wxShutdown(wxShutdownFlags wFlags
)
326 case wxSHUTDOWN_POWEROFF
:
330 case wxSHUTDOWN_REBOOT
:
335 wxFAIL_MSG( _T("unknown wxShutdown() flag") );
339 return system(wxString::Format(_T("init %c"), level
).mb_str()) == 0;
343 // ----------------------------------------------------------------------------
344 // wxStream classes to support IO redirection in wxExecute
345 // ----------------------------------------------------------------------------
349 // ----------------------------------------------------------------------------
350 // wxPipeInputStream: stream for reading from a pipe
351 // ----------------------------------------------------------------------------
353 class wxPipeInputStream
: public wxFileInputStream
356 wxPipeInputStream(int fd
) : wxFileInputStream(fd
) { }
358 // return TRUE if the pipe is still opened
359 bool IsOpened() const { return !Eof(); }
361 // return TRUE if we have anything to read, don't block
362 virtual bool CanRead() const;
365 bool wxPipeInputStream::CanRead() const
367 if ( m_lasterror
== wxSTREAM_EOF
)
370 // check if there is any input available
375 const int fd
= m_file
->fd();
379 FD_SET(fd
, &readfds
);
380 switch ( select(fd
+ 1, &readfds
, NULL
, NULL
, &tv
) )
383 wxLogSysError(_("Impossible to get child process input"));
390 wxFAIL_MSG(_T("unexpected select() return value"));
391 // still fall through
394 // input available -- or maybe not, as select() returns 1 when a
395 // read() will complete without delay, but it could still not read
401 // define this to let wxexec.cpp know that we know what we're doing
402 #define _WX_USED_BY_WXEXECUTE_
403 #include "../common/execcmn.cpp"
405 #endif // wxUSE_STREAMS
407 // ----------------------------------------------------------------------------
408 // wxExecute: the real worker function
409 // ----------------------------------------------------------------------------
412 #pragma message disable codeunreachable
415 long wxExecute(wxChar
**argv
,
419 // for the sync execution, we return -1 to indicate failure, but for async
420 // case we return 0 which is never a valid PID
422 // we define this as a macro, not a variable, to avoid compiler warnings
423 // about "ERROR_RETURN_CODE value may be clobbered by fork()"
424 #define ERROR_RETURN_CODE ((flags & wxEXEC_SYNC) ? -1 : 0)
426 wxCHECK_MSG( *argv
, ERROR_RETURN_CODE
, wxT("can't exec empty command") );
430 char *mb_argv
[WXEXECUTE_NARGS
];
432 while (argv
[mb_argc
])
434 wxWX2MBbuf mb_arg
= wxConvertWX2MB(argv
[mb_argc
]);
435 mb_argv
[mb_argc
] = strdup(mb_arg
);
438 mb_argv
[mb_argc
] = (char *) NULL
;
440 // this macro will free memory we used above
441 #define ARGS_CLEANUP \
442 for ( mb_argc = 0; mb_argv[mb_argc]; mb_argc++ ) \
443 free(mb_argv[mb_argc])
445 // no need for cleanup
448 wxChar
**mb_argv
= argv
;
449 #endif // Unicode/ANSI
451 // we want this function to work even if there is no wxApp so ensure that
452 // we have a valid traits pointer
453 wxConsoleAppTraits traitsConsole
;
454 wxAppTraits
*traits
= wxTheApp
? wxTheApp
->GetTraits() : NULL
;
456 traits
= &traitsConsole
;
458 // this struct contains all information which we pass to and from
459 // wxAppTraits methods
460 wxExecuteData execData
;
461 execData
.flags
= flags
;
462 execData
.process
= process
;
465 if ( !traits
->CreateEndProcessPipe(execData
) )
467 wxLogError( _("Failed to execute '%s'\n"), *argv
);
471 return ERROR_RETURN_CODE
;
474 // pipes for inter process communication
475 wxPipe pipeIn
, // stdin
479 if ( process
&& process
->IsRedirected() )
481 if ( !pipeIn
.Create() || !pipeOut
.Create() || !pipeErr
.Create() )
483 wxLogError( _("Failed to execute '%s'\n"), *argv
);
487 return ERROR_RETURN_CODE
;
493 // NB: do *not* use vfork() here, it completely breaks this code for some
494 // reason under Solaris (and maybe others, although not under Linux)
495 // But on OpenVMS we do not have fork so we have to use vfork and
496 // cross our fingers that it works.
502 if ( pid
== -1 ) // error?
504 wxLogSysError( _("Fork failed") );
508 return ERROR_RETURN_CODE
;
510 else if ( pid
== 0 ) // we're in child
512 // These lines close the open file descriptors to to avoid any
513 // input/output which might block the process or irritate the user. If
514 // one wants proper IO for the subprocess, the right thing to do is to
515 // start an xterm executing it.
516 if ( !(flags
& wxEXEC_SYNC
) )
518 for ( int fd
= 0; fd
< FD_SETSIZE
; fd
++ )
520 if ( fd
== pipeIn
[wxPipe::Read
]
521 || fd
== pipeOut
[wxPipe::Write
]
522 || fd
== pipeErr
[wxPipe::Write
]
523 || traits
->IsWriteFDOfEndProcessPipe(execData
, fd
) )
525 // don't close this one, we still need it
529 // leave stderr opened too, it won't do any harm
530 if ( fd
!= STDERR_FILENO
)
535 #if !defined(__VMS) && !defined(__EMX__)
536 if ( flags
& wxEXEC_MAKE_GROUP_LEADER
)
538 // Set process group to child process' pid. Then killing -pid
539 // of the parent will kill the process and all of its children.
544 // reading side can be safely closed but we should keep the write one
546 traits
->DetachWriteFDOfEndProcessPipe(execData
);
548 // redirect stdin, stdout and stderr
551 if ( dup2(pipeIn
[wxPipe::Read
], STDIN_FILENO
) == -1 ||
552 dup2(pipeOut
[wxPipe::Write
], STDOUT_FILENO
) == -1 ||
553 dup2(pipeErr
[wxPipe::Write
], STDERR_FILENO
) == -1 )
555 wxLogSysError(_("Failed to redirect child process input/output"));
563 execvp (*mb_argv
, mb_argv
);
565 fprintf(stderr
, "execvp(");
566 // CS changed ppc to ppc_ as ppc is not available under mac os CW Mach-O
567 for ( char **ppc_
= mb_argv
; *ppc_
; ppc_
++ )
568 fprintf(stderr
, "%s%s", ppc_
== mb_argv
? "" : ", ", *ppc_
);
569 fprintf(stderr
, ") failed with error %d!\n", errno
);
571 // there is no return after successful exec()
574 // some compilers complain about missing return - of course, they
575 // should know that exit() doesn't return but what else can we do if
578 // and, sure enough, other compilers complain about unreachable code
579 // after exit() call, so we can just always have return here...
580 #if defined(__VMS) || defined(__INTEL_COMPILER)
584 else // we're in parent
588 // prepare for IO redirection
591 // the input buffer bufOut is connected to stdout, this is why it is
592 // called bufOut and not bufIn
593 wxStreamTempInputBuffer bufOut
,
595 #endif // wxUSE_STREAMS
597 if ( process
&& process
->IsRedirected() )
600 wxOutputStream
*inStream
=
601 new wxFileOutputStream(pipeIn
.Detach(wxPipe::Write
));
603 wxPipeInputStream
*outStream
=
604 new wxPipeInputStream(pipeOut
.Detach(wxPipe::Read
));
606 wxPipeInputStream
*errStream
=
607 new wxPipeInputStream(pipeErr
.Detach(wxPipe::Read
));
609 process
->SetPipeStreams(outStream
, inStream
, errStream
);
611 bufOut
.Init(outStream
);
612 bufErr
.Init(errStream
);
614 execData
.bufOut
= &bufOut
;
615 execData
.bufErr
= &bufErr
;
616 #endif // wxUSE_STREAMS
626 return traits
->WaitForChild(execData
);
629 return ERROR_RETURN_CODE
;
633 #pragma message enable codeunreachable
636 #undef ERROR_RETURN_CODE
639 // ----------------------------------------------------------------------------
640 // file and directory functions
641 // ----------------------------------------------------------------------------
643 const wxChar
* wxGetHomeDir( wxString
*home
)
645 *home
= wxGetUserHome( wxString() );
647 if ( home
->IsEmpty() )
651 if ( tmp
.Last() != wxT(']'))
652 if ( tmp
.Last() != wxT('/')) *home
<< wxT('/');
654 return home
->c_str();
658 const wxMB2WXbuf
wxGetUserHome( const wxString
&user
)
659 #else // just for binary compatibility -- there is no 'const' here
660 char *wxGetUserHome( const wxString
&user
)
663 struct passwd
*who
= (struct passwd
*) NULL
;
669 if ((ptr
= wxGetenv(wxT("HOME"))) != NULL
)
672 wxWCharBuffer
buffer( ptr
);
678 if ((ptr
= wxGetenv(wxT("USER"))) != NULL
|| (ptr
= wxGetenv(wxT("LOGNAME"))) != NULL
)
680 who
= getpwnam(wxConvertWX2MB(ptr
));
683 // We now make sure the the user exists!
686 who
= getpwuid(getuid());
691 who
= getpwnam (user
.mb_str());
694 return wxConvertMB2WX(who
? who
->pw_dir
: 0);
697 // ----------------------------------------------------------------------------
698 // network and user id routines
699 // ----------------------------------------------------------------------------
701 // retrieve either the hostname or FQDN depending on platform (caller must
702 // check whether it's one or the other, this is why this function is for
704 static bool wxGetHostNameInternal(wxChar
*buf
, int sz
)
706 wxCHECK_MSG( buf
, FALSE
, wxT("NULL pointer in wxGetHostNameInternal") );
710 // we're using uname() which is POSIX instead of less standard sysinfo()
711 #if defined(HAVE_UNAME)
713 bool ok
= uname(&uts
) != -1;
716 wxStrncpy(buf
, wxConvertMB2WX(uts
.nodename
), sz
- 1);
719 #elif defined(HAVE_GETHOSTNAME)
720 bool ok
= gethostname(buf
, sz
) != -1;
721 #else // no uname, no gethostname
722 wxFAIL_MSG(wxT("don't know host name for this machine"));
725 #endif // uname/gethostname
729 wxLogSysError(_("Cannot get the hostname"));
735 bool wxGetHostName(wxChar
*buf
, int sz
)
737 bool ok
= wxGetHostNameInternal(buf
, sz
);
741 // BSD systems return the FQDN, we only want the hostname, so extract
742 // it (we consider that dots are domain separators)
743 wxChar
*dot
= wxStrchr(buf
, wxT('.'));
754 bool wxGetFullHostName(wxChar
*buf
, int sz
)
756 bool ok
= wxGetHostNameInternal(buf
, sz
);
760 if ( !wxStrchr(buf
, wxT('.')) )
762 struct hostent
*host
= gethostbyname(wxConvertWX2MB(buf
));
765 wxLogSysError(_("Cannot get the official hostname"));
771 // the canonical name
772 wxStrncpy(buf
, wxConvertMB2WX(host
->h_name
), sz
);
775 //else: it's already a FQDN (BSD behaves this way)
781 bool wxGetUserId(wxChar
*buf
, int sz
)
786 if ((who
= getpwuid(getuid ())) != NULL
)
788 wxStrncpy (buf
, wxConvertMB2WX(who
->pw_name
), sz
- 1);
795 bool wxGetUserName(wxChar
*buf
, int sz
)
800 if ((who
= getpwuid (getuid ())) != NULL
)
802 // pw_gecos field in struct passwd is not standard
804 char *comma
= strchr(who
->pw_gecos
, ',');
806 *comma
= '\0'; // cut off non-name comment fields
807 wxStrncpy (buf
, wxConvertMB2WX(who
->pw_gecos
), sz
- 1);
808 #else // !HAVE_PW_GECOS
809 wxStrncpy (buf
, wxConvertMB2WX(who
->pw_name
), sz
- 1);
810 #endif // HAVE_PW_GECOS/!HAVE_PW_GECOS
817 // this function is in mac/utils.cpp for wxMac
820 wxString
wxGetOsDescription()
822 #ifndef WXWIN_OS_DESCRIPTION
823 #error WXWIN_OS_DESCRIPTION should be defined in config.h by configure
825 return wxString::FromAscii( WXWIN_OS_DESCRIPTION
);
831 int wxGetOsVersion(int *verMaj
, int *verMin
)
833 // we want this function to work even if there is no wxApp
834 wxConsoleAppTraits traitsConsole
;
835 wxAppTraits
*traits
= wxTheApp
? wxTheApp
->GetTraits() : NULL
;
837 traits
= &traitsConsole
;
839 return traits
->GetOSVersion(verMaj
, verMin
);
842 unsigned long wxGetProcessId()
844 return (unsigned long)getpid();
847 long wxGetFreeMemory()
849 #if defined(__LINUX__)
850 // get it from /proc/meminfo
851 FILE *fp
= fopen("/proc/meminfo", "r");
857 if ( fgets(buf
, WXSIZEOF(buf
), fp
) && fgets(buf
, WXSIZEOF(buf
), fp
) )
859 long memTotal
, memUsed
;
860 sscanf(buf
, "Mem: %ld %ld %ld", &memTotal
, &memUsed
, &memFree
);
867 #elif defined(__SUN__) && defined(_SC_AVPHYS_PAGES)
868 return sysconf(_SC_AVPHYS_PAGES
)*sysconf(_SC_PAGESIZE
);
869 //#elif defined(__FREEBSD__) -- might use sysctl() to find it out, probably
876 bool wxGetDiskSpace(const wxString
& path
, wxLongLong
*pTotal
, wxLongLong
*pFree
)
878 #if defined(HAVE_STATFS) || defined(HAVE_STATVFS)
879 // the case to "char *" is needed for AIX 4.3
881 if ( wxStatfs((char *)(const char*)path
.fn_str(), &fs
) != 0 )
883 wxLogSysError( wxT("Failed to get file system statistics") );
888 // under Solaris we also have to use f_frsize field instead of f_bsize
889 // which is in general a multiple of f_frsize
891 wxLongLong blockSize
= fs
.f_frsize
;
893 wxLongLong blockSize
= fs
.f_bsize
;
894 #endif // HAVE_STATVFS/HAVE_STATFS
898 *pTotal
= wxLongLong(fs
.f_blocks
) * blockSize
;
903 *pFree
= wxLongLong(fs
.f_bavail
) * blockSize
;
907 #else // !HAVE_STATFS && !HAVE_STATVFS
909 #endif // HAVE_STATFS
912 // ----------------------------------------------------------------------------
914 // ----------------------------------------------------------------------------
916 bool wxGetEnv(const wxString
& var
, wxString
*value
)
918 // wxGetenv is defined as getenv()
919 wxChar
*p
= wxGetenv(var
);
931 bool wxSetEnv(const wxString
& variable
, const wxChar
*value
)
933 #if defined(HAVE_SETENV)
934 return setenv(variable
.mb_str(),
935 value
? (const char *)wxString(value
).mb_str()
937 1 /* overwrite */) == 0;
938 #elif defined(HAVE_PUTENV)
939 wxString s
= variable
;
941 s
<< _T('=') << value
;
944 const char *p
= s
.mb_str();
946 // the string will be free()d by libc
947 char *buf
= (char *)malloc(strlen(p
) + 1);
950 return putenv(buf
) == 0;
951 #else // no way to set an env var
956 // ----------------------------------------------------------------------------
958 // ----------------------------------------------------------------------------
960 #if wxUSE_ON_FATAL_EXCEPTION
964 extern "C" void wxFatalSignalHandler(wxTYPE_SA_HANDLER
)
968 // give the user a chance to do something special about this
969 wxTheApp
->OnFatalException();
975 bool wxHandleFatalExceptions(bool doit
)
978 static bool s_savedHandlers
= FALSE
;
979 static struct sigaction s_handlerFPE
,
985 if ( doit
&& !s_savedHandlers
)
987 // install the signal handler
988 struct sigaction act
;
990 // some systems extend it with non std fields, so zero everything
991 memset(&act
, 0, sizeof(act
));
993 act
.sa_handler
= wxFatalSignalHandler
;
994 sigemptyset(&act
.sa_mask
);
997 ok
&= sigaction(SIGFPE
, &act
, &s_handlerFPE
) == 0;
998 ok
&= sigaction(SIGILL
, &act
, &s_handlerILL
) == 0;
999 ok
&= sigaction(SIGBUS
, &act
, &s_handlerBUS
) == 0;
1000 ok
&= sigaction(SIGSEGV
, &act
, &s_handlerSEGV
) == 0;
1003 wxLogDebug(_T("Failed to install our signal handler."));
1006 s_savedHandlers
= TRUE
;
1008 else if ( s_savedHandlers
)
1010 // uninstall the signal handler
1011 ok
&= sigaction(SIGFPE
, &s_handlerFPE
, NULL
) == 0;
1012 ok
&= sigaction(SIGILL
, &s_handlerILL
, NULL
) == 0;
1013 ok
&= sigaction(SIGBUS
, &s_handlerBUS
, NULL
) == 0;
1014 ok
&= sigaction(SIGSEGV
, &s_handlerSEGV
, NULL
) == 0;
1017 wxLogDebug(_T("Failed to uninstall our signal handler."));
1020 s_savedHandlers
= FALSE
;
1022 //else: nothing to do
1027 #endif // wxUSE_ON_FATAL_EXCEPTION
1029 // ----------------------------------------------------------------------------
1030 // error and debug output routines (deprecated, use wxLog)
1031 // ----------------------------------------------------------------------------
1033 #if WXWIN_COMPATIBILITY_2_2
1035 void wxDebugMsg( const char *format
, ... )
1038 va_start( ap
, format
);
1039 vfprintf( stderr
, format
, ap
);
1044 void wxError( const wxString
&msg
, const wxString
&title
)
1046 wxFprintf( stderr
, _("Error ") );
1047 if (!title
.IsNull()) wxFprintf( stderr
, wxT("%s "), WXSTRINGCAST(title
) );
1048 if (!msg
.IsNull()) wxFprintf( stderr
, wxT(": %s"), WXSTRINGCAST(msg
) );
1049 wxFprintf( stderr
, wxT(".\n") );
1052 void wxFatalError( const wxString
&msg
, const wxString
&title
)
1054 wxFprintf( stderr
, _("Error ") );
1055 if (!title
.IsNull()) wxFprintf( stderr
, wxT("%s "), WXSTRINGCAST(title
) );
1056 if (!msg
.IsNull()) wxFprintf( stderr
, wxT(": %s"), WXSTRINGCAST(msg
) );
1057 wxFprintf( stderr
, wxT(".\n") );
1058 exit(3); // the same exit code as for abort()
1061 #endif // WXWIN_COMPATIBILITY_2_2
1063 #endif // __WXBASE__
1067 // ----------------------------------------------------------------------------
1068 // wxExecute support
1069 // ----------------------------------------------------------------------------
1071 // Darwin doesn't use the same process end detection mechanisms so we don't
1072 // need wxExecute-related helpers for it
1073 #if !(defined(__DARWIN__) && defined(__WXMAC__))
1075 bool wxGUIAppTraits::CreateEndProcessPipe(wxExecuteData
& execData
)
1077 return execData
.pipeEndProcDetect
.Create();
1080 bool wxGUIAppTraits::IsWriteFDOfEndProcessPipe(wxExecuteData
& execData
, int fd
)
1082 return fd
== execData
.pipeEndProcDetect
[wxPipe::Write
]
1085 void wxGUIAppTraits::DetachWriteFDOfEndProcessPipe(wxExecuteData
& execData
)
1087 execData
.pipeEndProcDetect
.Detach(wxPipe::Write
);
1088 execData
.pipeEndProcDetect
.Close();
1093 bool wxGUIAppTraits::CreateEndProcessPipe(wxExecuteData
& WXUNUSED(execData
))
1099 wxGUIAppTraits::IsWriteFDOfEndProcessPipe(wxExecuteData
& WXUNUSED(execData
),
1106 wxGUIAppTraits::DetachWriteFDOfEndProcessPipe(wxExecuteData
& WXUNUSED(execData
))
1108 // nothing to do here, we don't use the pipe
1111 #endif // !Darwin/Darwin
1113 int wxGUIAppTraits::WaitForChild(wxExecuteData
& execData
)
1115 wxEndProcessData
*endProcData
= new wxEndProcessData
;
1117 // wxAddProcessCallback is now (with DARWIN) allowed to call the
1118 // callback function directly if the process terminates before
1119 // the callback can be added to the run loop. Set up the endProcData.
1120 if ( execData
.flags
& wxEXEC_SYNC
)
1122 // we may have process for capturing the program output, but it's
1123 // not used in wxEndProcessData in the case of sync execution
1124 endProcData
->process
= NULL
;
1126 // sync execution: indicate it by negating the pid
1127 endProcData
->pid
= -execData
.pid
;
1131 // async execution, nothing special to do -- caller will be
1132 // notified about the process termination if process != NULL, endProcData
1133 // will be deleted in GTK_EndProcessDetector
1134 endProcData
->process
= execData
.process
;
1135 endProcData
->pid
= execData
.pid
;
1139 #if defined(__DARWIN__) && defined(__WXMAC__)
1140 endProcData
->tag
= wxAddProcessCallbackForPid(endProcData
, execData
.pid
);
1142 endProcData
->tag
= wxAddProcessCallback
1145 execData
.pipeEndProcDetect
.Detach(wxPipe::Read
)
1148 execData
.pipeEndProcDetect
.Close();
1149 #endif // defined(__DARWIN__) && defined(__WXMAC__)
1151 if ( execData
.flags
& wxEXEC_SYNC
)
1154 wxWindowDisabler wd
;
1156 // endProcData->pid will be set to 0 from GTK_EndProcessDetector when the
1157 // process terminates
1158 while ( endProcData
->pid
!= 0 )
1161 if ( execData
.bufOut
)
1162 execData
.bufOut
->Update();
1164 if ( execData
.bufErr
)
1165 execData
.bufErr
->Update();
1166 #endif // wxUSE_STREAMS
1168 // give GTK+ a chance to call GTK_EndProcessDetector here and
1169 // also repaint the GUI
1173 int exitcode
= endProcData
->exitcode
;
1179 else // async execution
1181 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