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
53 // not only the statfs syscall is called differently depending on platform, but
54 // one of its incarnations, statvfs(), takes different arguments under
55 // different platforms and even different versions of the same system (Solaris
56 // 7 and 8): if you want to test for this, don't forget that the problems only
57 // appear if the large files support is enabled
60 #include <sys/param.h>
61 #include <sys/mount.h>
64 #endif // __BSD__/!__BSD__
66 #define wxStatfs statfs
70 #include <sys/statvfs.h>
72 #define wxStatfs statvfs
73 #endif // HAVE_STATVFS
75 #if defined(HAVE_STATFS) || defined(HAVE_STATVFS)
76 // WX_STATFS_T is detected by configure
77 #define wxStatfs_t WX_STATFS_T
80 // SGI signal.h defines signal handler arguments differently depending on
81 // whether _LANGUAGE_C_PLUS_PLUS is set or not - do set it
82 #if defined(__SGI__) && !defined(_LANGUAGE_C_PLUS_PLUS)
83 #define _LANGUAGE_C_PLUS_PLUS 1
90 #include <sys/types.h>
97 #include <fcntl.h> // for O_WRONLY and friends
98 #include <time.h> // nanosleep() and/or usleep()
99 #include <ctype.h> // isspace()
100 #include <sys/time.h> // needed for FD_SETSIZE
103 #include <sys/utsname.h> // for uname()
106 // ----------------------------------------------------------------------------
107 // conditional compilation
108 // ----------------------------------------------------------------------------
110 // many versions of Unices have this function, but it is not defined in system
111 // headers - please add your system here if it is the case for your OS.
112 // SunOS < 5.6 (i.e. Solaris < 2.6) and DG-UX are like this.
113 #if !defined(HAVE_USLEEP) && \
114 (defined(__SUN__) && !defined(__SunOs_5_6) && \
115 !defined(__SunOs_5_7) && !defined(__SUNPRO_CC)) || \
116 defined(__osf__) || defined(__EMX__)
120 int usleep(unsigned int usec
);
123 /* I copied this from the XFree86 diffs. AV. */
124 #define INCL_DOSPROCESS
126 inline void usleep(unsigned long delay
)
128 DosSleep(delay
? (delay
/1000l) : 1l);
130 #else // !Sun && !EMX
131 void usleep(unsigned long usec
);
133 #endif // Sun/EMX/Something else
136 #define HAVE_USLEEP 1
137 #endif // Unices without usleep()
139 // ============================================================================
141 // ============================================================================
143 // ----------------------------------------------------------------------------
145 // ----------------------------------------------------------------------------
147 void wxSleep(int nSecs
)
152 void wxMicroSleep(unsigned long microseconds
)
154 #if defined(HAVE_NANOSLEEP)
156 tmReq
.tv_sec
= (time_t)(microseconds
/ 1000000);
157 tmReq
.tv_nsec
= (microseconds
% 1000000) * 1000;
159 // we're not interested in remaining time nor in return value
160 (void)nanosleep(&tmReq
, (timespec
*)NULL
);
161 #elif defined(HAVE_USLEEP)
162 // uncomment this if you feel brave or if you are sure that your version
163 // of Solaris has a safe usleep() function but please notice that usleep()
164 // is known to lead to crashes in MT programs in Solaris 2.[67] and is not
165 // documented as MT-Safe
166 #if defined(__SUN__) && wxUSE_THREADS
167 #error "usleep() cannot be used in MT programs under Solaris."
170 usleep(microseconds
);
171 #elif defined(HAVE_SLEEP)
172 // under BeOS sleep() takes seconds (what about other platforms, if any?)
173 sleep(microseconds
* 1000000);
174 #else // !sleep function
175 #error "usleep() or nanosleep() function required for wxMicroSleep"
176 #endif // sleep function
179 void wxMilliSleep(unsigned long milliseconds
)
181 wxMicroSleep(milliseconds
*1000);
184 // ----------------------------------------------------------------------------
185 // process management
186 // ----------------------------------------------------------------------------
188 int wxKill(long pid
, wxSignal sig
, wxKillError
*rc
)
190 int err
= kill((pid_t
)pid
, (int)sig
);
200 *rc
= wxKILL_BAD_SIGNAL
;
204 *rc
= wxKILL_ACCESS_DENIED
;
208 *rc
= wxKILL_NO_PROCESS
;
212 // this goes against Unix98 docs so log it
213 wxLogDebug(_T("unexpected kill(2) return value %d"), err
);
223 #define WXEXECUTE_NARGS 127
225 long wxExecute( const wxString
& command
, int flags
, wxProcess
*process
)
227 wxCHECK_MSG( !command
.IsEmpty(), 0, wxT("can't exec empty command") );
230 // fork() doesn't mix well with POSIX threads: on many systems the program
231 // deadlocks or crashes for some reason. Probably our code is buggy and
232 // doesn't do something which must be done to allow this to work, but I
233 // don't know what yet, so for now just warn the user (this is the least we
235 wxASSERT_MSG( wxThread::IsMain(),
236 _T("wxExecute() can be called only from the main thread") );
237 #endif // wxUSE_THREADS
240 wxChar
*argv
[WXEXECUTE_NARGS
];
242 const wxChar
*cptr
= command
.c_str();
243 wxChar quotechar
= wxT('\0'); // is arg quoted?
244 bool escaped
= FALSE
;
246 // split the command line in arguments
250 quotechar
= wxT('\0');
252 // eat leading whitespace:
253 while ( wxIsspace(*cptr
) )
256 if ( *cptr
== wxT('\'') || *cptr
== wxT('"') )
261 if ( *cptr
== wxT('\\') && ! escaped
)
268 // all other characters:
272 // have we reached the end of the argument?
273 if ( (*cptr
== quotechar
&& ! escaped
)
274 || (quotechar
== wxT('\0') && wxIsspace(*cptr
))
275 || *cptr
== wxT('\0') )
277 wxASSERT_MSG( argc
< WXEXECUTE_NARGS
,
278 wxT("too many arguments in wxExecute") );
280 argv
[argc
] = new wxChar
[argument
.length() + 1];
281 wxStrcpy(argv
[argc
], argument
.c_str());
284 // if not at end of buffer, swallow last character:
288 break; // done with this one, start over
294 // do execute the command
295 long lRc
= wxExecute(argv
, flags
, process
);
300 delete [] argv
[argc
++];
305 // ----------------------------------------------------------------------------
307 // ----------------------------------------------------------------------------
309 static wxString
wxMakeShellCommand(const wxString
& command
)
314 // just an interactive shell
319 // execute command in a shell
320 cmd
<< _T("/bin/sh -c '") << command
<< _T('\'');
326 bool wxShell(const wxString
& command
)
328 return wxExecute(wxMakeShellCommand(command
), wxEXEC_SYNC
) == 0;
331 bool wxShell(const wxString
& command
, wxArrayString
& output
)
333 wxCHECK_MSG( !!command
, FALSE
, _T("can't exec shell non interactively") );
335 return wxExecute(wxMakeShellCommand(command
), output
);
338 // Shutdown or reboot the PC
339 bool wxShutdown(wxShutdownFlags wFlags
)
344 case wxSHUTDOWN_POWEROFF
:
348 case wxSHUTDOWN_REBOOT
:
353 wxFAIL_MSG( _T("unknown wxShutdown() flag") );
357 return system(wxString::Format(_T("init %c"), level
).mb_str()) == 0;
361 // ----------------------------------------------------------------------------
362 // wxStream classes to support IO redirection in wxExecute
363 // ----------------------------------------------------------------------------
367 bool wxPipeInputStream::CanRead() const
369 if ( m_lasterror
== wxSTREAM_EOF
)
372 // check if there is any input available
377 const int fd
= m_file
->fd();
381 FD_SET(fd
, &readfds
);
382 switch ( select(fd
+ 1, &readfds
, NULL
, NULL
, &tv
) )
385 wxLogSysError(_("Impossible to get child process input"));
392 wxFAIL_MSG(_T("unexpected select() return value"));
393 // still fall through
396 // input available -- or maybe not, as select() returns 1 when a
397 // read() will complete without delay, but it could still not read
403 #endif // wxUSE_STREAMS
405 // ----------------------------------------------------------------------------
406 // wxExecute: the real worker function
407 // ----------------------------------------------------------------------------
410 #pragma message disable codeunreachable
413 long wxExecute(wxChar
**argv
,
417 // for the sync execution, we return -1 to indicate failure, but for async
418 // case we return 0 which is never a valid PID
420 // we define this as a macro, not a variable, to avoid compiler warnings
421 // about "ERROR_RETURN_CODE value may be clobbered by fork()"
422 #define ERROR_RETURN_CODE ((flags & wxEXEC_SYNC) ? -1 : 0)
424 wxCHECK_MSG( *argv
, ERROR_RETURN_CODE
, wxT("can't exec empty command") );
428 char *mb_argv
[WXEXECUTE_NARGS
];
430 while (argv
[mb_argc
])
432 wxWX2MBbuf mb_arg
= wxConvertWX2MB(argv
[mb_argc
]);
433 mb_argv
[mb_argc
] = strdup(mb_arg
);
436 mb_argv
[mb_argc
] = (char *) NULL
;
438 // this macro will free memory we used above
439 #define ARGS_CLEANUP \
440 for ( mb_argc = 0; mb_argv[mb_argc]; mb_argc++ ) \
441 free(mb_argv[mb_argc])
443 // no need for cleanup
446 wxChar
**mb_argv
= argv
;
447 #endif // Unicode/ANSI
449 // we want this function to work even if there is no wxApp so ensure that
450 // we have a valid traits pointer
451 wxConsoleAppTraits traitsConsole
;
452 wxAppTraits
*traits
= wxTheApp
? wxTheApp
->GetTraits() : NULL
;
454 traits
= &traitsConsole
;
456 // this struct contains all information which we pass to and from
457 // wxAppTraits methods
458 wxExecuteData execData
;
459 execData
.flags
= flags
;
460 execData
.process
= process
;
463 if ( !traits
->CreateEndProcessPipe(execData
) )
465 wxLogError( _("Failed to execute '%s'\n"), *argv
);
469 return ERROR_RETURN_CODE
;
472 // pipes for inter process communication
473 wxPipe pipeIn
, // stdin
477 if ( process
&& process
->IsRedirected() )
479 if ( !pipeIn
.Create() || !pipeOut
.Create() || !pipeErr
.Create() )
481 wxLogError( _("Failed to execute '%s'\n"), *argv
);
485 return ERROR_RETURN_CODE
;
491 // NB: do *not* use vfork() here, it completely breaks this code for some
492 // reason under Solaris (and maybe others, although not under Linux)
493 // But on OpenVMS we do not have fork so we have to use vfork and
494 // cross our fingers that it works.
500 if ( pid
== -1 ) // error?
502 wxLogSysError( _("Fork failed") );
506 return ERROR_RETURN_CODE
;
508 else if ( pid
== 0 ) // we're in child
510 // These lines close the open file descriptors to to avoid any
511 // input/output which might block the process or irritate the user. If
512 // one wants proper IO for the subprocess, the right thing to do is to
513 // start an xterm executing it.
514 if ( !(flags
& wxEXEC_SYNC
) )
516 // FD_SETSIZE is unsigned under BSD, signed under other platforms
517 // so we need a cast to avoid warnings on all platforms
518 for ( int fd
= 0; fd
< (int)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 // save it for WaitForChild() use
591 // prepare for IO redirection
594 // the input buffer bufOut is connected to stdout, this is why it is
595 // called bufOut and not bufIn
596 wxStreamTempInputBuffer bufOut
,
598 #endif // wxUSE_STREAMS
600 if ( process
&& process
->IsRedirected() )
603 wxOutputStream
*inStream
=
604 new wxFileOutputStream(pipeIn
.Detach(wxPipe::Write
));
606 wxPipeInputStream
*outStream
=
607 new wxPipeInputStream(pipeOut
.Detach(wxPipe::Read
));
609 wxPipeInputStream
*errStream
=
610 new wxPipeInputStream(pipeErr
.Detach(wxPipe::Read
));
612 process
->SetPipeStreams(outStream
, inStream
, errStream
);
614 bufOut
.Init(outStream
);
615 bufErr
.Init(errStream
);
617 execData
.bufOut
= &bufOut
;
618 execData
.bufErr
= &bufErr
;
619 #endif // wxUSE_STREAMS
629 return traits
->WaitForChild(execData
);
632 return ERROR_RETURN_CODE
;
636 #pragma message enable codeunreachable
639 #undef ERROR_RETURN_CODE
642 // ----------------------------------------------------------------------------
643 // file and directory functions
644 // ----------------------------------------------------------------------------
646 const wxChar
* wxGetHomeDir( wxString
*home
)
648 *home
= wxGetUserHome( wxString() );
650 if ( home
->IsEmpty() )
654 if ( tmp
.Last() != wxT(']'))
655 if ( tmp
.Last() != wxT('/')) *home
<< wxT('/');
657 return home
->c_str();
661 const wxMB2WXbuf
wxGetUserHome( const wxString
&user
)
662 #else // just for binary compatibility -- there is no 'const' here
663 char *wxGetUserHome( const wxString
&user
)
666 struct passwd
*who
= (struct passwd
*) NULL
;
672 if ((ptr
= wxGetenv(wxT("HOME"))) != NULL
)
675 wxWCharBuffer
buffer( ptr
);
681 if ((ptr
= wxGetenv(wxT("USER"))) != NULL
|| (ptr
= wxGetenv(wxT("LOGNAME"))) != NULL
)
683 who
= getpwnam(wxConvertWX2MB(ptr
));
686 // We now make sure the the user exists!
689 who
= getpwuid(getuid());
694 who
= getpwnam (user
.mb_str());
697 return wxConvertMB2WX(who
? who
->pw_dir
: 0);
700 // ----------------------------------------------------------------------------
701 // network and user id routines
702 // ----------------------------------------------------------------------------
704 // retrieve either the hostname or FQDN depending on platform (caller must
705 // check whether it's one or the other, this is why this function is for
707 static bool wxGetHostNameInternal(wxChar
*buf
, int sz
)
709 wxCHECK_MSG( buf
, FALSE
, wxT("NULL pointer in wxGetHostNameInternal") );
713 // we're using uname() which is POSIX instead of less standard sysinfo()
714 #if defined(HAVE_UNAME)
716 bool ok
= uname(&uts
) != -1;
719 wxStrncpy(buf
, wxConvertMB2WX(uts
.nodename
), sz
- 1);
722 #elif defined(HAVE_GETHOSTNAME)
723 bool ok
= gethostname(buf
, sz
) != -1;
724 #else // no uname, no gethostname
725 wxFAIL_MSG(wxT("don't know host name for this machine"));
728 #endif // uname/gethostname
732 wxLogSysError(_("Cannot get the hostname"));
738 bool wxGetHostName(wxChar
*buf
, int sz
)
740 bool ok
= wxGetHostNameInternal(buf
, sz
);
744 // BSD systems return the FQDN, we only want the hostname, so extract
745 // it (we consider that dots are domain separators)
746 wxChar
*dot
= wxStrchr(buf
, wxT('.'));
757 bool wxGetFullHostName(wxChar
*buf
, int sz
)
759 bool ok
= wxGetHostNameInternal(buf
, sz
);
763 if ( !wxStrchr(buf
, wxT('.')) )
765 struct hostent
*host
= gethostbyname(wxConvertWX2MB(buf
));
768 wxLogSysError(_("Cannot get the official hostname"));
774 // the canonical name
775 wxStrncpy(buf
, wxConvertMB2WX(host
->h_name
), sz
);
778 //else: it's already a FQDN (BSD behaves this way)
784 bool wxGetUserId(wxChar
*buf
, int sz
)
789 if ((who
= getpwuid(getuid ())) != NULL
)
791 wxStrncpy (buf
, wxConvertMB2WX(who
->pw_name
), sz
- 1);
798 bool wxGetUserName(wxChar
*buf
, int sz
)
803 if ((who
= getpwuid (getuid ())) != NULL
)
805 // pw_gecos field in struct passwd is not standard
807 char *comma
= strchr(who
->pw_gecos
, ',');
809 *comma
= '\0'; // cut off non-name comment fields
810 wxStrncpy (buf
, wxConvertMB2WX(who
->pw_gecos
), sz
- 1);
811 #else // !HAVE_PW_GECOS
812 wxStrncpy (buf
, wxConvertMB2WX(who
->pw_name
), sz
- 1);
813 #endif // HAVE_PW_GECOS/!HAVE_PW_GECOS
820 // this function is in mac/utils.cpp for wxMac
823 wxString
wxGetOsDescription()
825 #ifndef WXWIN_OS_DESCRIPTION
826 #error WXWIN_OS_DESCRIPTION should be defined in config.h by configure
828 return wxString::FromAscii( WXWIN_OS_DESCRIPTION
);
834 unsigned long wxGetProcessId()
836 return (unsigned long)getpid();
839 long wxGetFreeMemory()
841 #if defined(__LINUX__)
842 // get it from /proc/meminfo
843 FILE *fp
= fopen("/proc/meminfo", "r");
849 if ( fgets(buf
, WXSIZEOF(buf
), fp
) && fgets(buf
, WXSIZEOF(buf
), fp
) )
851 long memTotal
, memUsed
;
852 sscanf(buf
, "Mem: %ld %ld %ld", &memTotal
, &memUsed
, &memFree
);
859 #elif defined(__SUN__) && defined(_SC_AVPHYS_PAGES)
860 return sysconf(_SC_AVPHYS_PAGES
)*sysconf(_SC_PAGESIZE
);
861 //#elif defined(__FREEBSD__) -- might use sysctl() to find it out, probably
868 bool wxGetDiskSpace(const wxString
& path
, wxLongLong
*pTotal
, wxLongLong
*pFree
)
870 #if defined(HAVE_STATFS) || defined(HAVE_STATVFS)
871 // the case to "char *" is needed for AIX 4.3
873 if ( wxStatfs((char *)(const char*)path
.fn_str(), &fs
) != 0 )
875 wxLogSysError( wxT("Failed to get file system statistics") );
880 // under Solaris we also have to use f_frsize field instead of f_bsize
881 // which is in general a multiple of f_frsize
883 wxLongLong blockSize
= fs
.f_frsize
;
885 wxLongLong blockSize
= fs
.f_bsize
;
886 #endif // HAVE_STATVFS/HAVE_STATFS
890 *pTotal
= wxLongLong(fs
.f_blocks
) * blockSize
;
895 *pFree
= wxLongLong(fs
.f_bavail
) * blockSize
;
899 #else // !HAVE_STATFS && !HAVE_STATVFS
901 #endif // HAVE_STATFS
904 // ----------------------------------------------------------------------------
906 // ----------------------------------------------------------------------------
908 bool wxGetEnv(const wxString
& var
, wxString
*value
)
910 // wxGetenv is defined as getenv()
911 wxChar
*p
= wxGetenv(var
);
923 bool wxSetEnv(const wxString
& variable
, const wxChar
*value
)
925 #if defined(HAVE_SETENV)
926 return setenv(variable
.mb_str(),
927 value
? (const char *)wxString(value
).mb_str()
929 1 /* overwrite */) == 0;
930 #elif defined(HAVE_PUTENV)
931 wxString s
= variable
;
933 s
<< _T('=') << value
;
936 const wxWX2MBbuf p
= s
.mb_str();
938 // the string will be free()d by libc
939 char *buf
= (char *)malloc(strlen(p
) + 1);
942 return putenv(buf
) == 0;
943 #else // no way to set an env var
948 // ----------------------------------------------------------------------------
950 // ----------------------------------------------------------------------------
952 #if wxUSE_ON_FATAL_EXCEPTION
956 extern "C" void wxFatalSignalHandler(wxTYPE_SA_HANDLER
)
960 // give the user a chance to do something special about this
961 wxTheApp
->OnFatalException();
967 bool wxHandleFatalExceptions(bool doit
)
970 static bool s_savedHandlers
= FALSE
;
971 static struct sigaction s_handlerFPE
,
977 if ( doit
&& !s_savedHandlers
)
979 // install the signal handler
980 struct sigaction act
;
982 // some systems extend it with non std fields, so zero everything
983 memset(&act
, 0, sizeof(act
));
985 act
.sa_handler
= wxFatalSignalHandler
;
986 sigemptyset(&act
.sa_mask
);
989 ok
&= sigaction(SIGFPE
, &act
, &s_handlerFPE
) == 0;
990 ok
&= sigaction(SIGILL
, &act
, &s_handlerILL
) == 0;
991 ok
&= sigaction(SIGBUS
, &act
, &s_handlerBUS
) == 0;
992 ok
&= sigaction(SIGSEGV
, &act
, &s_handlerSEGV
) == 0;
995 wxLogDebug(_T("Failed to install our signal handler."));
998 s_savedHandlers
= TRUE
;
1000 else if ( s_savedHandlers
)
1002 // uninstall the signal handler
1003 ok
&= sigaction(SIGFPE
, &s_handlerFPE
, NULL
) == 0;
1004 ok
&= sigaction(SIGILL
, &s_handlerILL
, NULL
) == 0;
1005 ok
&= sigaction(SIGBUS
, &s_handlerBUS
, NULL
) == 0;
1006 ok
&= sigaction(SIGSEGV
, &s_handlerSEGV
, NULL
) == 0;
1009 wxLogDebug(_T("Failed to uninstall our signal handler."));
1012 s_savedHandlers
= FALSE
;
1014 //else: nothing to do
1019 #endif // wxUSE_ON_FATAL_EXCEPTION
1021 // ----------------------------------------------------------------------------
1022 // error and debug output routines (deprecated, use wxLog)
1023 // ----------------------------------------------------------------------------
1025 #if WXWIN_COMPATIBILITY_2_2
1027 void wxDebugMsg( const char *format
, ... )
1030 va_start( ap
, format
);
1031 vfprintf( stderr
, format
, ap
);
1036 void wxError( 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") );
1044 void wxFatalError( 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") );
1050 exit(3); // the same exit code as for abort()
1053 #endif // WXWIN_COMPATIBILITY_2_2
1055 #endif // wxUSE_BASE
1059 // ----------------------------------------------------------------------------
1060 // wxExecute support
1061 // ----------------------------------------------------------------------------
1063 // Darwin doesn't use the same process end detection mechanisms so we don't
1064 // need wxExecute-related helpers for it
1065 #if !(defined(__DARWIN__) && defined(__WXMAC__))
1067 bool wxGUIAppTraits::CreateEndProcessPipe(wxExecuteData
& execData
)
1069 return execData
.pipeEndProcDetect
.Create();
1072 bool wxGUIAppTraits::IsWriteFDOfEndProcessPipe(wxExecuteData
& execData
, int fd
)
1074 return fd
== (execData
.pipeEndProcDetect
)[wxPipe::Write
];
1077 void wxGUIAppTraits::DetachWriteFDOfEndProcessPipe(wxExecuteData
& execData
)
1079 execData
.pipeEndProcDetect
.Detach(wxPipe::Write
);
1080 execData
.pipeEndProcDetect
.Close();
1085 bool wxGUIAppTraits::CreateEndProcessPipe(wxExecuteData
& WXUNUSED(execData
))
1091 wxGUIAppTraits::IsWriteFDOfEndProcessPipe(wxExecuteData
& WXUNUSED(execData
),
1098 wxGUIAppTraits::DetachWriteFDOfEndProcessPipe(wxExecuteData
& WXUNUSED(execData
))
1100 // nothing to do here, we don't use the pipe
1103 #endif // !Darwin/Darwin
1105 int wxGUIAppTraits::WaitForChild(wxExecuteData
& execData
)
1107 wxEndProcessData
*endProcData
= new wxEndProcessData
;
1109 // wxAddProcessCallback is now (with DARWIN) allowed to call the
1110 // callback function directly if the process terminates before
1111 // the callback can be added to the run loop. Set up the endProcData.
1112 if ( execData
.flags
& wxEXEC_SYNC
)
1114 // we may have process for capturing the program output, but it's
1115 // not used in wxEndProcessData in the case of sync execution
1116 endProcData
->process
= NULL
;
1118 // sync execution: indicate it by negating the pid
1119 endProcData
->pid
= -execData
.pid
;
1123 // async execution, nothing special to do -- caller will be
1124 // notified about the process termination if process != NULL, endProcData
1125 // will be deleted in GTK_EndProcessDetector
1126 endProcData
->process
= execData
.process
;
1127 endProcData
->pid
= execData
.pid
;
1131 #if defined(__DARWIN__) && defined(__WXMAC__)
1132 endProcData
->tag
= wxAddProcessCallbackForPid(endProcData
, execData
.pid
);
1134 endProcData
->tag
= wxAddProcessCallback
1137 execData
.pipeEndProcDetect
.Detach(wxPipe::Read
)
1140 execData
.pipeEndProcDetect
.Close();
1141 #endif // defined(__DARWIN__) && defined(__WXMAC__)
1143 if ( execData
.flags
& wxEXEC_SYNC
)
1146 wxWindowDisabler wd
;
1148 // endProcData->pid will be set to 0 from GTK_EndProcessDetector when the
1149 // process terminates
1150 while ( endProcData
->pid
!= 0 )
1155 if ( execData
.bufOut
)
1157 execData
.bufOut
->Update();
1161 if ( execData
.bufErr
)
1163 execData
.bufErr
->Update();
1166 #endif // wxUSE_STREAMS
1168 // don't consume 100% of the CPU while we're sitting in this
1173 // give GTK+ a chance to call GTK_EndProcessDetector here and
1174 // also repaint the GUI
1178 int exitcode
= endProcData
->exitcode
;
1184 else // async execution
1186 return execData
.pid
;
1193 void wxHandleProcessTermination(wxEndProcessData
*proc_data
)
1195 // notify user about termination if required
1196 if ( proc_data
->process
)
1198 proc_data
->process
->OnTerminate(proc_data
->pid
, proc_data
->exitcode
);
1202 if ( proc_data
->pid
> 0 )
1208 // let wxExecute() know that the process has terminated
1213 #endif // wxUSE_BASE