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"
24 #include "wx/apptrait.h"
27 #include "wx/process.h"
28 #include "wx/thread.h"
30 #include "wx/wfstream.h"
32 #include "wx/unix/execute.h"
36 #if defined( __MWERKS__ ) && defined(__MACH__)
37 #define WXWIN_OS_DESCRIPTION "MacOS X"
38 #define HAVE_NANOSLEEP
41 // not only the statfs syscall is called differently depending on platform, but
42 // one of its incarnations, statvfs(), takes different arguments under
43 // different platforms and even different versions of the same system (Solaris
44 // 7 and 8): if you want to test for this, don't forget that the problems only
45 // appear if the large files support is enabled
48 #include <sys/param.h>
49 #include <sys/mount.h>
52 #endif // __BSD__/!__BSD__
54 #define wxStatfs statfs
58 #include <sys/statvfs.h>
60 #define wxStatfs statvfs
61 #endif // HAVE_STATVFS
63 #if defined(HAVE_STATFS) || defined(HAVE_STATVFS)
64 // WX_STATFS_T is detected by configure
65 #define wxStatfs_t WX_STATFS_T
68 // SGI signal.h defines signal handler arguments differently depending on
69 // whether _LANGUAGE_C_PLUS_PLUS is set or not - do set it
70 #if defined(__SGI__) && !defined(_LANGUAGE_C_PLUS_PLUS)
71 #define _LANGUAGE_C_PLUS_PLUS 1
78 #include <sys/types.h>
85 #include <fcntl.h> // for O_WRONLY and friends
86 #include <time.h> // nanosleep() and/or usleep()
87 #include <ctype.h> // isspace()
88 #include <sys/time.h> // needed for FD_SETSIZE
91 #include <sys/utsname.h> // for uname()
94 // ----------------------------------------------------------------------------
95 // conditional compilation
96 // ----------------------------------------------------------------------------
98 // many versions of Unices have this function, but it is not defined in system
99 // headers - please add your system here if it is the case for your OS.
100 // SunOS < 5.6 (i.e. Solaris < 2.6) and DG-UX are like this.
101 #if !defined(HAVE_USLEEP) && \
102 (defined(__SUN__) && !defined(__SunOs_5_6) && \
103 !defined(__SunOs_5_7) && !defined(__SUNPRO_CC)) || \
104 defined(__osf__) || defined(__EMX__)
108 int usleep(unsigned int usec
);
111 /* I copied this from the XFree86 diffs. AV. */
112 #define INCL_DOSPROCESS
114 inline void usleep(unsigned long delay
)
116 DosSleep(delay
? (delay
/1000l) : 1l);
118 #else // !Sun && !EMX
119 void usleep(unsigned long usec
);
121 #endif // Sun/EMX/Something else
124 #define HAVE_USLEEP 1
125 #endif // Unices without usleep()
127 // ============================================================================
129 // ============================================================================
131 // ----------------------------------------------------------------------------
133 // ----------------------------------------------------------------------------
135 void wxSleep(int nSecs
)
140 void wxUsleep(unsigned long milliseconds
)
142 #if defined(HAVE_NANOSLEEP)
144 tmReq
.tv_sec
= (time_t)(milliseconds
/ 1000);
145 tmReq
.tv_nsec
= (milliseconds
% 1000) * 1000 * 1000;
147 // we're not interested in remaining time nor in return value
148 (void)nanosleep(&tmReq
, (timespec
*)NULL
);
149 #elif defined(HAVE_USLEEP)
150 // uncomment this if you feel brave or if you are sure that your version
151 // of Solaris has a safe usleep() function but please notice that usleep()
152 // is known to lead to crashes in MT programs in Solaris 2.[67] and is not
153 // documented as MT-Safe
154 #if defined(__SUN__) && wxUSE_THREADS
155 #error "usleep() cannot be used in MT programs under Solaris."
158 usleep(milliseconds
* 1000); // usleep(3) wants microseconds
159 #elif defined(HAVE_SLEEP)
160 // under BeOS sleep() takes seconds (what about other platforms, if any?)
161 sleep(milliseconds
* 1000);
162 #else // !sleep function
163 #error "usleep() or nanosleep() function required for wxUsleep"
164 #endif // sleep function
167 // ----------------------------------------------------------------------------
168 // process management
169 // ----------------------------------------------------------------------------
171 int wxKill(long pid
, wxSignal sig
, wxKillError
*rc
)
173 int err
= kill((pid_t
)pid
, (int)sig
);
183 *rc
= wxKILL_BAD_SIGNAL
;
187 *rc
= wxKILL_ACCESS_DENIED
;
191 *rc
= wxKILL_NO_PROCESS
;
195 // this goes against Unix98 docs so log it
196 wxLogDebug(_T("unexpected kill(2) return value %d"), err
);
206 #define WXEXECUTE_NARGS 127
208 long wxExecute( const wxString
& command
, int flags
, wxProcess
*process
)
210 wxCHECK_MSG( !command
.IsEmpty(), 0, wxT("can't exec empty command") );
213 // fork() doesn't mix well with POSIX threads: on many systems the program
214 // deadlocks or crashes for some reason. Probably our code is buggy and
215 // doesn't do something which must be done to allow this to work, but I
216 // don't know what yet, so for now just warn the user (this is the least we
218 wxASSERT_MSG( wxThread::IsMain(),
219 _T("wxExecute() can be called only from the main thread") );
220 #endif // wxUSE_THREADS
223 wxChar
*argv
[WXEXECUTE_NARGS
];
225 const wxChar
*cptr
= command
.c_str();
226 wxChar quotechar
= wxT('\0'); // is arg quoted?
227 bool escaped
= FALSE
;
229 // split the command line in arguments
233 quotechar
= wxT('\0');
235 // eat leading whitespace:
236 while ( wxIsspace(*cptr
) )
239 if ( *cptr
== wxT('\'') || *cptr
== wxT('"') )
244 if ( *cptr
== wxT('\\') && ! escaped
)
251 // all other characters:
255 // have we reached the end of the argument?
256 if ( (*cptr
== quotechar
&& ! escaped
)
257 || (quotechar
== wxT('\0') && wxIsspace(*cptr
))
258 || *cptr
== wxT('\0') )
260 wxASSERT_MSG( argc
< WXEXECUTE_NARGS
,
261 wxT("too many arguments in wxExecute") );
263 argv
[argc
] = new wxChar
[argument
.length() + 1];
264 wxStrcpy(argv
[argc
], argument
.c_str());
267 // if not at end of buffer, swallow last character:
271 break; // done with this one, start over
277 // do execute the command
278 long lRc
= wxExecute(argv
, flags
, process
);
283 delete [] argv
[argc
++];
288 // ----------------------------------------------------------------------------
290 // ----------------------------------------------------------------------------
292 static wxString
wxMakeShellCommand(const wxString
& command
)
297 // just an interactive shell
302 // execute command in a shell
303 cmd
<< _T("/bin/sh -c '") << command
<< _T('\'');
309 bool wxShell(const wxString
& command
)
311 return wxExecute(wxMakeShellCommand(command
), wxEXEC_SYNC
) == 0;
314 bool wxShell(const wxString
& command
, wxArrayString
& output
)
316 wxCHECK_MSG( !!command
, FALSE
, _T("can't exec shell non interactively") );
318 return wxExecute(wxMakeShellCommand(command
), output
);
321 // Shutdown or reboot the PC
322 bool wxShutdown(wxShutdownFlags wFlags
)
327 case wxSHUTDOWN_POWEROFF
:
331 case wxSHUTDOWN_REBOOT
:
336 wxFAIL_MSG( _T("unknown wxShutdown() flag") );
340 return system(wxString::Format(_T("init %c"), level
).mb_str()) == 0;
344 // ----------------------------------------------------------------------------
345 // wxStream classes to support IO redirection in wxExecute
346 // ----------------------------------------------------------------------------
350 // ----------------------------------------------------------------------------
351 // wxPipeInputStream: stream for reading from a pipe
352 // ----------------------------------------------------------------------------
354 class wxPipeInputStream
: public wxFileInputStream
357 wxPipeInputStream(int fd
) : wxFileInputStream(fd
) { }
359 // return TRUE if the pipe is still opened
360 bool IsOpened() const { return !Eof(); }
362 // return TRUE if we have anything to read, don't block
363 virtual bool CanRead() const;
366 bool wxPipeInputStream::CanRead() const
368 if ( m_lasterror
== wxSTREAM_EOF
)
371 // check if there is any input available
376 const int fd
= m_file
->fd();
380 FD_SET(fd
, &readfds
);
381 switch ( select(fd
+ 1, &readfds
, NULL
, NULL
, &tv
) )
384 wxLogSysError(_("Impossible to get child process input"));
391 wxFAIL_MSG(_T("unexpected select() return value"));
392 // still fall through
395 // input available -- or maybe not, as select() returns 1 when a
396 // read() will complete without delay, but it could still not read
402 // define this to let wxexec.cpp know that we know what we're doing
403 #define _WX_USED_BY_WXEXECUTE_
404 #include "../common/execcmn.cpp"
406 #endif // wxUSE_STREAMS
408 // ----------------------------------------------------------------------------
409 // wxExecute: the real worker function
410 // ----------------------------------------------------------------------------
413 #pragma message disable codeunreachable
416 long wxExecute(wxChar
**argv
,
420 // for the sync execution, we return -1 to indicate failure, but for async
421 // case we return 0 which is never a valid PID
423 // we define this as a macro, not a variable, to avoid compiler warnings
424 // about "ERROR_RETURN_CODE value may be clobbered by fork()"
425 #define ERROR_RETURN_CODE ((flags & wxEXEC_SYNC) ? -1 : 0)
427 wxCHECK_MSG( *argv
, ERROR_RETURN_CODE
, wxT("can't exec empty command") );
431 char *mb_argv
[WXEXECUTE_NARGS
];
433 while (argv
[mb_argc
])
435 wxWX2MBbuf mb_arg
= wxConvertWX2MB(argv
[mb_argc
]);
436 mb_argv
[mb_argc
] = strdup(mb_arg
);
439 mb_argv
[mb_argc
] = (char *) NULL
;
441 // this macro will free memory we used above
442 #define ARGS_CLEANUP \
443 for ( mb_argc = 0; mb_argv[mb_argc]; mb_argc++ ) \
444 free(mb_argv[mb_argc])
446 // no need for cleanup
449 wxChar
**mb_argv
= argv
;
450 #endif // Unicode/ANSI
452 // we want this function to work even if there is no wxApp so ensure that
453 // we have a valid traits pointer
454 wxConsoleAppTraits traitsConsole
;
455 wxAppTraits
*traits
= wxTheApp
? wxTheApp
->GetTraits() : NULL
;
457 traits
= &traitsConsole
;
459 // this struct contains all information which we pass to and from
460 // wxAppTraits methods
461 wxExecuteData execData
;
462 execData
.flags
= flags
;
463 execData
.process
= process
;
466 if ( !traits
->CreateEndProcessPipe(execData
) )
468 wxLogError( _("Failed to execute '%s'\n"), *argv
);
472 return ERROR_RETURN_CODE
;
475 // pipes for inter process communication
476 wxPipe pipeIn
, // stdin
480 if ( process
&& process
->IsRedirected() )
482 if ( !pipeIn
.Create() || !pipeOut
.Create() || !pipeErr
.Create() )
484 wxLogError( _("Failed to execute '%s'\n"), *argv
);
488 return ERROR_RETURN_CODE
;
494 // NB: do *not* use vfork() here, it completely breaks this code for some
495 // reason under Solaris (and maybe others, although not under Linux)
496 // But on OpenVMS we do not have fork so we have to use vfork and
497 // cross our fingers that it works.
503 if ( pid
== -1 ) // error?
505 wxLogSysError( _("Fork failed") );
509 return ERROR_RETURN_CODE
;
511 else if ( pid
== 0 ) // we're in child
513 // These lines close the open file descriptors to to avoid any
514 // input/output which might block the process or irritate the user. If
515 // one wants proper IO for the subprocess, the right thing to do is to
516 // start an xterm executing it.
517 if ( !(flags
& wxEXEC_SYNC
) )
519 for ( int fd
= 0; fd
< FD_SETSIZE
; fd
++ )
521 if ( fd
== pipeIn
[wxPipe::Read
]
522 || fd
== pipeOut
[wxPipe::Write
]
523 || fd
== pipeErr
[wxPipe::Write
]
524 || traits
->IsWriteFDOfEndProcessPipe(execData
, fd
) )
526 // don't close this one, we still need it
530 // leave stderr opened too, it won't do any harm
531 if ( fd
!= STDERR_FILENO
)
536 #if !defined(__VMS) && !defined(__EMX__)
537 if ( flags
& wxEXEC_MAKE_GROUP_LEADER
)
539 // Set process group to child process' pid. Then killing -pid
540 // of the parent will kill the process and all of its children.
545 // reading side can be safely closed but we should keep the write one
547 traits
->DetachWriteFDOfEndProcessPipe(execData
);
549 // redirect stdin, stdout and stderr
552 if ( dup2(pipeIn
[wxPipe::Read
], STDIN_FILENO
) == -1 ||
553 dup2(pipeOut
[wxPipe::Write
], STDOUT_FILENO
) == -1 ||
554 dup2(pipeErr
[wxPipe::Write
], STDERR_FILENO
) == -1 )
556 wxLogSysError(_("Failed to redirect child process input/output"));
564 execvp (*mb_argv
, mb_argv
);
566 fprintf(stderr
, "execvp(");
567 // CS changed ppc to ppc_ as ppc is not available under mac os CW Mach-O
568 for ( char **ppc_
= mb_argv
; *ppc_
; ppc_
++ )
569 fprintf(stderr
, "%s%s", ppc_
== mb_argv
? "" : ", ", *ppc_
);
570 fprintf(stderr
, ") failed with error %d!\n", errno
);
572 // there is no return after successful exec()
575 // some compilers complain about missing return - of course, they
576 // should know that exit() doesn't return but what else can we do if
579 // and, sure enough, other compilers complain about unreachable code
580 // after exit() call, so we can just always have return here...
581 #if defined(__VMS) || defined(__INTEL_COMPILER)
585 else // we're in parent
589 // prepare for IO redirection
592 // the input buffer bufOut is connected to stdout, this is why it is
593 // called bufOut and not bufIn
594 wxStreamTempInputBuffer bufOut
,
596 #endif // wxUSE_STREAMS
598 if ( process
&& process
->IsRedirected() )
601 wxOutputStream
*inStream
=
602 new wxFileOutputStream(pipeIn
.Detach(wxPipe::Write
));
604 wxPipeInputStream
*outStream
=
605 new wxPipeInputStream(pipeOut
.Detach(wxPipe::Read
));
607 wxPipeInputStream
*errStream
=
608 new wxPipeInputStream(pipeErr
.Detach(wxPipe::Read
));
610 process
->SetPipeStreams(outStream
, inStream
, errStream
);
612 bufOut
.Init(outStream
);
613 bufErr
.Init(errStream
);
615 execData
.bufOut
= &bufOut
;
616 execData
.bufErr
= &bufErr
;
617 #endif // wxUSE_STREAMS
627 return traits
->WaitForChild(execData
);
630 return ERROR_RETURN_CODE
;
634 #pragma message enable codeunreachable
637 #undef ERROR_RETURN_CODE
640 // ----------------------------------------------------------------------------
641 // file and directory functions
642 // ----------------------------------------------------------------------------
644 const wxChar
* wxGetHomeDir( wxString
*home
)
646 *home
= wxGetUserHome( wxString() );
648 if ( home
->IsEmpty() )
652 if ( tmp
.Last() != wxT(']'))
653 if ( tmp
.Last() != wxT('/')) *home
<< wxT('/');
655 return home
->c_str();
659 const wxMB2WXbuf
wxGetUserHome( const wxString
&user
)
660 #else // just for binary compatibility -- there is no 'const' here
661 char *wxGetUserHome( const wxString
&user
)
664 struct passwd
*who
= (struct passwd
*) NULL
;
670 if ((ptr
= wxGetenv(wxT("HOME"))) != NULL
)
673 wxWCharBuffer
buffer( ptr
);
679 if ((ptr
= wxGetenv(wxT("USER"))) != NULL
|| (ptr
= wxGetenv(wxT("LOGNAME"))) != NULL
)
681 who
= getpwnam(wxConvertWX2MB(ptr
));
684 // We now make sure the the user exists!
687 who
= getpwuid(getuid());
692 who
= getpwnam (user
.mb_str());
695 return wxConvertMB2WX(who
? who
->pw_dir
: 0);
698 // ----------------------------------------------------------------------------
699 // network and user id routines
700 // ----------------------------------------------------------------------------
702 // retrieve either the hostname or FQDN depending on platform (caller must
703 // check whether it's one or the other, this is why this function is for
705 static bool wxGetHostNameInternal(wxChar
*buf
, int sz
)
707 wxCHECK_MSG( buf
, FALSE
, wxT("NULL pointer in wxGetHostNameInternal") );
711 // we're using uname() which is POSIX instead of less standard sysinfo()
712 #if defined(HAVE_UNAME)
714 bool ok
= uname(&uts
) != -1;
717 wxStrncpy(buf
, wxConvertMB2WX(uts
.nodename
), sz
- 1);
720 #elif defined(HAVE_GETHOSTNAME)
721 bool ok
= gethostname(buf
, sz
) != -1;
722 #else // no uname, no gethostname
723 wxFAIL_MSG(wxT("don't know host name for this machine"));
726 #endif // uname/gethostname
730 wxLogSysError(_("Cannot get the hostname"));
736 bool wxGetHostName(wxChar
*buf
, int sz
)
738 bool ok
= wxGetHostNameInternal(buf
, sz
);
742 // BSD systems return the FQDN, we only want the hostname, so extract
743 // it (we consider that dots are domain separators)
744 wxChar
*dot
= wxStrchr(buf
, wxT('.'));
755 bool wxGetFullHostName(wxChar
*buf
, int sz
)
757 bool ok
= wxGetHostNameInternal(buf
, sz
);
761 if ( !wxStrchr(buf
, wxT('.')) )
763 struct hostent
*host
= gethostbyname(wxConvertWX2MB(buf
));
766 wxLogSysError(_("Cannot get the official hostname"));
772 // the canonical name
773 wxStrncpy(buf
, wxConvertMB2WX(host
->h_name
), sz
);
776 //else: it's already a FQDN (BSD behaves this way)
782 bool wxGetUserId(wxChar
*buf
, int sz
)
787 if ((who
= getpwuid(getuid ())) != NULL
)
789 wxStrncpy (buf
, wxConvertMB2WX(who
->pw_name
), sz
- 1);
796 bool wxGetUserName(wxChar
*buf
, int sz
)
801 if ((who
= getpwuid (getuid ())) != NULL
)
803 // pw_gecos field in struct passwd is not standard
805 char *comma
= strchr(who
->pw_gecos
, ',');
807 *comma
= '\0'; // cut off non-name comment fields
808 wxStrncpy (buf
, wxConvertMB2WX(who
->pw_gecos
), sz
- 1);
809 #else // !HAVE_PW_GECOS
810 wxStrncpy (buf
, wxConvertMB2WX(who
->pw_name
), sz
- 1);
811 #endif // HAVE_PW_GECOS/!HAVE_PW_GECOS
818 // this function is in mac/utils.cpp for wxMac
821 wxString
wxGetOsDescription()
823 #ifndef WXWIN_OS_DESCRIPTION
824 #error WXWIN_OS_DESCRIPTION should be defined in config.h by configure
826 return wxString::FromAscii( WXWIN_OS_DESCRIPTION
);
832 int wxGetOsVersion(int *verMaj
, int *verMin
)
834 // we want this function to work even if there is no wxApp
835 wxConsoleAppTraits traitsConsole
;
836 wxAppTraits
*traits
= wxTheApp
? wxTheApp
->GetTraits() : NULL
;
838 traits
= &traitsConsole
;
840 return traits
->GetOSVersion(verMaj
, verMin
);
843 unsigned long wxGetProcessId()
845 return (unsigned long)getpid();
848 long wxGetFreeMemory()
850 #if defined(__LINUX__)
851 // get it from /proc/meminfo
852 FILE *fp
= fopen("/proc/meminfo", "r");
858 if ( fgets(buf
, WXSIZEOF(buf
), fp
) && fgets(buf
, WXSIZEOF(buf
), fp
) )
860 long memTotal
, memUsed
;
861 sscanf(buf
, "Mem: %ld %ld %ld", &memTotal
, &memUsed
, &memFree
);
868 #elif defined(__SUN__) && defined(_SC_AVPHYS_PAGES)
869 return sysconf(_SC_AVPHYS_PAGES
)*sysconf(_SC_PAGESIZE
);
870 //#elif defined(__FREEBSD__) -- might use sysctl() to find it out, probably
877 bool wxGetDiskSpace(const wxString
& path
, wxLongLong
*pTotal
, wxLongLong
*pFree
)
879 #if defined(HAVE_STATFS) || defined(HAVE_STATVFS)
880 // the case to "char *" is needed for AIX 4.3
882 if ( wxStatfs((char *)(const char*)path
.fn_str(), &fs
) != 0 )
884 wxLogSysError( wxT("Failed to get file system statistics") );
889 // under Solaris we also have to use f_frsize field instead of f_bsize
890 // which is in general a multiple of f_frsize
892 wxLongLong blockSize
= fs
.f_frsize
;
894 wxLongLong blockSize
= fs
.f_bsize
;
895 #endif // HAVE_STATVFS/HAVE_STATFS
899 *pTotal
= wxLongLong(fs
.f_blocks
) * blockSize
;
904 *pFree
= wxLongLong(fs
.f_bavail
) * blockSize
;
908 #else // !HAVE_STATFS && !HAVE_STATVFS
910 #endif // HAVE_STATFS
913 // ----------------------------------------------------------------------------
915 // ----------------------------------------------------------------------------
917 bool wxGetEnv(const wxString
& var
, wxString
*value
)
919 // wxGetenv is defined as getenv()
920 wxChar
*p
= wxGetenv(var
);
932 bool wxSetEnv(const wxString
& variable
, const wxChar
*value
)
934 #if defined(HAVE_SETENV)
935 return setenv(variable
.mb_str(),
936 value
? (const char *)wxString(value
).mb_str()
938 1 /* overwrite */) == 0;
939 #elif defined(HAVE_PUTENV)
940 wxString s
= variable
;
942 s
<< _T('=') << value
;
945 const char *p
= s
.mb_str();
947 // the string will be free()d by libc
948 char *buf
= (char *)malloc(strlen(p
) + 1);
951 return putenv(buf
) == 0;
952 #else // no way to set an env var
957 // ----------------------------------------------------------------------------
959 // ----------------------------------------------------------------------------
961 #if wxUSE_ON_FATAL_EXCEPTION
965 extern "C" void wxFatalSignalHandler(wxTYPE_SA_HANDLER
)
969 // give the user a chance to do something special about this
970 wxTheApp
->OnFatalException();
976 bool wxHandleFatalExceptions(bool doit
)
979 static bool s_savedHandlers
= FALSE
;
980 static struct sigaction s_handlerFPE
,
986 if ( doit
&& !s_savedHandlers
)
988 // install the signal handler
989 struct sigaction act
;
991 // some systems extend it with non std fields, so zero everything
992 memset(&act
, 0, sizeof(act
));
994 act
.sa_handler
= wxFatalSignalHandler
;
995 sigemptyset(&act
.sa_mask
);
998 ok
&= sigaction(SIGFPE
, &act
, &s_handlerFPE
) == 0;
999 ok
&= sigaction(SIGILL
, &act
, &s_handlerILL
) == 0;
1000 ok
&= sigaction(SIGBUS
, &act
, &s_handlerBUS
) == 0;
1001 ok
&= sigaction(SIGSEGV
, &act
, &s_handlerSEGV
) == 0;
1004 wxLogDebug(_T("Failed to install our signal handler."));
1007 s_savedHandlers
= TRUE
;
1009 else if ( s_savedHandlers
)
1011 // uninstall the signal handler
1012 ok
&= sigaction(SIGFPE
, &s_handlerFPE
, NULL
) == 0;
1013 ok
&= sigaction(SIGILL
, &s_handlerILL
, NULL
) == 0;
1014 ok
&= sigaction(SIGBUS
, &s_handlerBUS
, NULL
) == 0;
1015 ok
&= sigaction(SIGSEGV
, &s_handlerSEGV
, NULL
) == 0;
1018 wxLogDebug(_T("Failed to uninstall our signal handler."));
1021 s_savedHandlers
= FALSE
;
1023 //else: nothing to do
1028 #endif // wxUSE_ON_FATAL_EXCEPTION
1030 // ----------------------------------------------------------------------------
1031 // error and debug output routines (deprecated, use wxLog)
1032 // ----------------------------------------------------------------------------
1034 #if WXWIN_COMPATIBILITY_2_2
1036 void wxDebugMsg( const char *format
, ... )
1039 va_start( ap
, format
);
1040 vfprintf( stderr
, format
, ap
);
1045 void wxError( const wxString
&msg
, const wxString
&title
)
1047 wxFprintf( stderr
, _("Error ") );
1048 if (!title
.IsNull()) wxFprintf( stderr
, wxT("%s "), WXSTRINGCAST(title
) );
1049 if (!msg
.IsNull()) wxFprintf( stderr
, wxT(": %s"), WXSTRINGCAST(msg
) );
1050 wxFprintf( stderr
, wxT(".\n") );
1053 void wxFatalError( const wxString
&msg
, const wxString
&title
)
1055 wxFprintf( stderr
, _("Error ") );
1056 if (!title
.IsNull()) wxFprintf( stderr
, wxT("%s "), WXSTRINGCAST(title
) );
1057 if (!msg
.IsNull()) wxFprintf( stderr
, wxT(": %s"), WXSTRINGCAST(msg
) );
1058 wxFprintf( stderr
, wxT(".\n") );
1059 exit(3); // the same exit code as for abort()
1062 #endif // WXWIN_COMPATIBILITY_2_2
1064 #endif // __WXBASE__
1068 // ----------------------------------------------------------------------------
1069 // wxExecute support
1070 // ----------------------------------------------------------------------------
1072 // Darwin doesn't use the same process end detection mechanisms so we don't
1073 // need wxExecute-related helpers for it
1074 #if !(defined(__DARWIN__) && defined(__WXMAC__))
1076 bool wxGUIAppTraits::CreateEndProcessPipe(wxExecuteData
& execData
)
1078 return execData
.pipeEndProcDetect
.Create();
1081 bool wxGUIAppTraits::IsWriteFDOfEndProcessPipe(wxExecuteData
& execData
, int fd
)
1083 return fd
== (execData
.pipeEndProcDetect
)[wxPipe::Write
];
1086 void wxGUIAppTraits::DetachWriteFDOfEndProcessPipe(wxExecuteData
& execData
)
1088 execData
.pipeEndProcDetect
.Detach(wxPipe::Write
);
1089 execData
.pipeEndProcDetect
.Close();
1094 bool wxGUIAppTraits::CreateEndProcessPipe(wxExecuteData
& WXUNUSED(execData
))
1100 wxGUIAppTraits::IsWriteFDOfEndProcessPipe(wxExecuteData
& WXUNUSED(execData
),
1107 wxGUIAppTraits::DetachWriteFDOfEndProcessPipe(wxExecuteData
& WXUNUSED(execData
))
1109 // nothing to do here, we don't use the pipe
1112 #endif // !Darwin/Darwin
1114 int wxGUIAppTraits::WaitForChild(wxExecuteData
& execData
)
1116 wxEndProcessData
*endProcData
= new wxEndProcessData
;
1118 // wxAddProcessCallback is now (with DARWIN) allowed to call the
1119 // callback function directly if the process terminates before
1120 // the callback can be added to the run loop. Set up the endProcData.
1121 if ( execData
.flags
& wxEXEC_SYNC
)
1123 // we may have process for capturing the program output, but it's
1124 // not used in wxEndProcessData in the case of sync execution
1125 endProcData
->process
= NULL
;
1127 // sync execution: indicate it by negating the pid
1128 endProcData
->pid
= -execData
.pid
;
1132 // async execution, nothing special to do -- caller will be
1133 // notified about the process termination if process != NULL, endProcData
1134 // will be deleted in GTK_EndProcessDetector
1135 endProcData
->process
= execData
.process
;
1136 endProcData
->pid
= execData
.pid
;
1140 #if defined(__DARWIN__) && defined(__WXMAC__)
1141 endProcData
->tag
= wxAddProcessCallbackForPid(endProcData
, execData
.pid
);
1143 endProcData
->tag
= wxAddProcessCallback
1146 execData
.pipeEndProcDetect
.Detach(wxPipe::Read
)
1149 execData
.pipeEndProcDetect
.Close();
1150 #endif // defined(__DARWIN__) && defined(__WXMAC__)
1152 if ( execData
.flags
& wxEXEC_SYNC
)
1155 wxWindowDisabler wd
;
1157 // endProcData->pid will be set to 0 from GTK_EndProcessDetector when the
1158 // process terminates
1159 while ( endProcData
->pid
!= 0 )
1162 if ( execData
.bufOut
)
1163 execData
.bufOut
->Update();
1165 if ( execData
.bufErr
)
1166 execData
.bufErr
->Update();
1167 #endif // wxUSE_STREAMS
1169 // give GTK+ a chance to call GTK_EndProcessDetector here and
1170 // also repaint the GUI
1174 int exitcode
= endProcData
->exitcode
;
1180 else // async execution
1182 return execData
.pid
;
1186 void wxHandleProcessTermination(wxEndProcessData
*proc_data
)
1188 // notify user about termination if required
1189 if ( proc_data
->process
)
1191 proc_data
->process
->OnTerminate(proc_data
->pid
, proc_data
->exitcode
);
1195 if ( proc_data
->pid
> 0 )
1201 // let wxExecute() know that the process has terminated