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 // define this to let wxexec.cpp know that we know what we're doing
37 #define _WX_USED_BY_WXEXECUTE_
38 #include "../common/execcmn.cpp"
40 #endif // wxUSE_STREAMS
44 #if defined( __MWERKS__ ) && defined(__MACH__)
45 #define WXWIN_OS_DESCRIPTION "MacOS X"
46 #define HAVE_NANOSLEEP
49 // not only the statfs syscall is called differently depending on platform, but
50 // one of its incarnations, statvfs(), takes different arguments under
51 // different platforms and even different versions of the same system (Solaris
52 // 7 and 8): if you want to test for this, don't forget that the problems only
53 // appear if the large files support is enabled
56 #include <sys/param.h>
57 #include <sys/mount.h>
60 #endif // __BSD__/!__BSD__
62 #define wxStatfs statfs
66 #include <sys/statvfs.h>
68 #define wxStatfs statvfs
69 #endif // HAVE_STATVFS
71 #if defined(HAVE_STATFS) || defined(HAVE_STATVFS)
72 // WX_STATFS_T is detected by configure
73 #define wxStatfs_t WX_STATFS_T
76 // SGI signal.h defines signal handler arguments differently depending on
77 // whether _LANGUAGE_C_PLUS_PLUS is set or not - do set it
78 #if defined(__SGI__) && !defined(_LANGUAGE_C_PLUS_PLUS)
79 #define _LANGUAGE_C_PLUS_PLUS 1
86 #include <sys/types.h>
93 #include <fcntl.h> // for O_WRONLY and friends
94 #include <time.h> // nanosleep() and/or usleep()
95 #include <ctype.h> // isspace()
96 #include <sys/time.h> // needed for FD_SETSIZE
99 #include <sys/utsname.h> // for uname()
102 // ----------------------------------------------------------------------------
103 // conditional compilation
104 // ----------------------------------------------------------------------------
106 // many versions of Unices have this function, but it is not defined in system
107 // headers - please add your system here if it is the case for your OS.
108 // SunOS < 5.6 (i.e. Solaris < 2.6) and DG-UX are like this.
109 #if !defined(HAVE_USLEEP) && \
110 (defined(__SUN__) && !defined(__SunOs_5_6) && \
111 !defined(__SunOs_5_7) && !defined(__SUNPRO_CC)) || \
112 defined(__osf__) || defined(__EMX__)
116 int usleep(unsigned int usec
);
119 /* I copied this from the XFree86 diffs. AV. */
120 #define INCL_DOSPROCESS
122 inline void usleep(unsigned long delay
)
124 DosSleep(delay
? (delay
/1000l) : 1l);
126 #else // !Sun && !EMX
127 void usleep(unsigned long usec
);
129 #endif // Sun/EMX/Something else
132 #define HAVE_USLEEP 1
133 #endif // Unices without usleep()
135 // ============================================================================
137 // ============================================================================
139 // ----------------------------------------------------------------------------
141 // ----------------------------------------------------------------------------
143 void wxSleep(int nSecs
)
148 void wxUsleep(unsigned long milliseconds
)
150 #if defined(HAVE_NANOSLEEP)
152 tmReq
.tv_sec
= (time_t)(milliseconds
/ 1000);
153 tmReq
.tv_nsec
= (milliseconds
% 1000) * 1000 * 1000;
155 // we're not interested in remaining time nor in return value
156 (void)nanosleep(&tmReq
, (timespec
*)NULL
);
157 #elif defined(HAVE_USLEEP)
158 // uncomment this if you feel brave or if you are sure that your version
159 // of Solaris has a safe usleep() function but please notice that usleep()
160 // is known to lead to crashes in MT programs in Solaris 2.[67] and is not
161 // documented as MT-Safe
162 #if defined(__SUN__) && wxUSE_THREADS
163 #error "usleep() cannot be used in MT programs under Solaris."
166 usleep(milliseconds
* 1000); // usleep(3) wants microseconds
167 #elif defined(HAVE_SLEEP)
168 // under BeOS sleep() takes seconds (what about other platforms, if any?)
169 sleep(milliseconds
* 1000);
170 #else // !sleep function
171 #error "usleep() or nanosleep() function required for wxUsleep"
172 #endif // sleep function
175 // ----------------------------------------------------------------------------
176 // process management
177 // ----------------------------------------------------------------------------
179 int wxKill(long pid
, wxSignal sig
, wxKillError
*rc
)
181 int err
= kill((pid_t
)pid
, (int)sig
);
191 *rc
= wxKILL_BAD_SIGNAL
;
195 *rc
= wxKILL_ACCESS_DENIED
;
199 *rc
= wxKILL_NO_PROCESS
;
203 // this goes against Unix98 docs so log it
204 wxLogDebug(_T("unexpected kill(2) return value %d"), err
);
214 #define WXEXECUTE_NARGS 127
216 long wxExecute( const wxString
& command
, int flags
, wxProcess
*process
)
218 wxCHECK_MSG( !command
.IsEmpty(), 0, wxT("can't exec empty command") );
221 // fork() doesn't mix well with POSIX threads: on many systems the program
222 // deadlocks or crashes for some reason. Probably our code is buggy and
223 // doesn't do something which must be done to allow this to work, but I
224 // don't know what yet, so for now just warn the user (this is the least we
226 wxASSERT_MSG( wxThread::IsMain(),
227 _T("wxExecute() can be called only from the main thread") );
228 #endif // wxUSE_THREADS
231 wxChar
*argv
[WXEXECUTE_NARGS
];
233 const wxChar
*cptr
= command
.c_str();
234 wxChar quotechar
= wxT('\0'); // is arg quoted?
235 bool escaped
= FALSE
;
237 // split the command line in arguments
241 quotechar
= wxT('\0');
243 // eat leading whitespace:
244 while ( wxIsspace(*cptr
) )
247 if ( *cptr
== wxT('\'') || *cptr
== wxT('"') )
252 if ( *cptr
== wxT('\\') && ! escaped
)
259 // all other characters:
263 // have we reached the end of the argument?
264 if ( (*cptr
== quotechar
&& ! escaped
)
265 || (quotechar
== wxT('\0') && wxIsspace(*cptr
))
266 || *cptr
== wxT('\0') )
268 wxASSERT_MSG( argc
< WXEXECUTE_NARGS
,
269 wxT("too many arguments in wxExecute") );
271 argv
[argc
] = new wxChar
[argument
.length() + 1];
272 wxStrcpy(argv
[argc
], argument
.c_str());
275 // if not at end of buffer, swallow last character:
279 break; // done with this one, start over
285 // do execute the command
286 long lRc
= wxExecute(argv
, flags
, process
);
291 delete [] argv
[argc
++];
296 // ----------------------------------------------------------------------------
298 // ----------------------------------------------------------------------------
300 static wxString
wxMakeShellCommand(const wxString
& command
)
305 // just an interactive shell
310 // execute command in a shell
311 cmd
<< _T("/bin/sh -c '") << command
<< _T('\'');
317 bool wxShell(const wxString
& command
)
319 return wxExecute(wxMakeShellCommand(command
), wxEXEC_SYNC
) == 0;
322 bool wxShell(const wxString
& command
, wxArrayString
& output
)
324 wxCHECK_MSG( !!command
, FALSE
, _T("can't exec shell non interactively") );
326 return wxExecute(wxMakeShellCommand(command
), output
);
329 // Shutdown or reboot the PC
330 bool wxShutdown(wxShutdownFlags wFlags
)
335 case wxSHUTDOWN_POWEROFF
:
339 case wxSHUTDOWN_REBOOT
:
344 wxFAIL_MSG( _T("unknown wxShutdown() flag") );
348 return system(wxString::Format(_T("init %c"), level
).mb_str()) == 0;
352 // ----------------------------------------------------------------------------
353 // wxStream classes to support IO redirection in wxExecute
354 // ----------------------------------------------------------------------------
358 bool wxPipeInputStream::CanRead() const
360 if ( m_lasterror
== wxSTREAM_EOF
)
363 // check if there is any input available
368 const int fd
= m_file
->fd();
372 FD_SET(fd
, &readfds
);
373 switch ( select(fd
+ 1, &readfds
, NULL
, NULL
, &tv
) )
376 wxLogSysError(_("Impossible to get child process input"));
383 wxFAIL_MSG(_T("unexpected select() return value"));
384 // still fall through
387 // input available -- or maybe not, as select() returns 1 when a
388 // read() will complete without delay, but it could still not read
394 #endif // wxUSE_STREAMS
396 // ----------------------------------------------------------------------------
397 // wxExecute: the real worker function
398 // ----------------------------------------------------------------------------
401 #pragma message disable codeunreachable
404 long wxExecute(wxChar
**argv
,
408 // for the sync execution, we return -1 to indicate failure, but for async
409 // case we return 0 which is never a valid PID
411 // we define this as a macro, not a variable, to avoid compiler warnings
412 // about "ERROR_RETURN_CODE value may be clobbered by fork()"
413 #define ERROR_RETURN_CODE ((flags & wxEXEC_SYNC) ? -1 : 0)
415 wxCHECK_MSG( *argv
, ERROR_RETURN_CODE
, wxT("can't exec empty command") );
419 char *mb_argv
[WXEXECUTE_NARGS
];
421 while (argv
[mb_argc
])
423 wxWX2MBbuf mb_arg
= wxConvertWX2MB(argv
[mb_argc
]);
424 mb_argv
[mb_argc
] = strdup(mb_arg
);
427 mb_argv
[mb_argc
] = (char *) NULL
;
429 // this macro will free memory we used above
430 #define ARGS_CLEANUP \
431 for ( mb_argc = 0; mb_argv[mb_argc]; mb_argc++ ) \
432 free(mb_argv[mb_argc])
434 // no need for cleanup
437 wxChar
**mb_argv
= argv
;
438 #endif // Unicode/ANSI
440 // we want this function to work even if there is no wxApp so ensure that
441 // we have a valid traits pointer
442 wxConsoleAppTraits traitsConsole
;
443 wxAppTraits
*traits
= wxTheApp
? wxTheApp
->GetTraits() : NULL
;
445 traits
= &traitsConsole
;
447 // this struct contains all information which we pass to and from
448 // wxAppTraits methods
449 wxExecuteData execData
;
450 execData
.flags
= flags
;
451 execData
.process
= process
;
454 if ( !traits
->CreateEndProcessPipe(execData
) )
456 wxLogError( _("Failed to execute '%s'\n"), *argv
);
460 return ERROR_RETURN_CODE
;
463 // pipes for inter process communication
464 wxPipe pipeIn
, // stdin
468 if ( process
&& process
->IsRedirected() )
470 if ( !pipeIn
.Create() || !pipeOut
.Create() || !pipeErr
.Create() )
472 wxLogError( _("Failed to execute '%s'\n"), *argv
);
476 return ERROR_RETURN_CODE
;
482 // NB: do *not* use vfork() here, it completely breaks this code for some
483 // reason under Solaris (and maybe others, although not under Linux)
484 // But on OpenVMS we do not have fork so we have to use vfork and
485 // cross our fingers that it works.
491 if ( pid
== -1 ) // error?
493 wxLogSysError( _("Fork failed") );
497 return ERROR_RETURN_CODE
;
499 else if ( pid
== 0 ) // we're in child
501 // These lines close the open file descriptors to to avoid any
502 // input/output which might block the process or irritate the user. If
503 // one wants proper IO for the subprocess, the right thing to do is to
504 // start an xterm executing it.
505 if ( !(flags
& wxEXEC_SYNC
) )
507 for ( int fd
= 0; fd
< FD_SETSIZE
; fd
++ )
509 if ( fd
== pipeIn
[wxPipe::Read
]
510 || fd
== pipeOut
[wxPipe::Write
]
511 || fd
== pipeErr
[wxPipe::Write
]
512 || traits
->IsWriteFDOfEndProcessPipe(execData
, fd
) )
514 // don't close this one, we still need it
518 // leave stderr opened too, it won't do any harm
519 if ( fd
!= STDERR_FILENO
)
524 #if !defined(__VMS) && !defined(__EMX__)
525 if ( flags
& wxEXEC_MAKE_GROUP_LEADER
)
527 // Set process group to child process' pid. Then killing -pid
528 // of the parent will kill the process and all of its children.
533 // reading side can be safely closed but we should keep the write one
535 traits
->DetachWriteFDOfEndProcessPipe(execData
);
537 // redirect stdin, stdout and stderr
540 if ( dup2(pipeIn
[wxPipe::Read
], STDIN_FILENO
) == -1 ||
541 dup2(pipeOut
[wxPipe::Write
], STDOUT_FILENO
) == -1 ||
542 dup2(pipeErr
[wxPipe::Write
], STDERR_FILENO
) == -1 )
544 wxLogSysError(_("Failed to redirect child process input/output"));
552 execvp (*mb_argv
, mb_argv
);
554 fprintf(stderr
, "execvp(");
555 // CS changed ppc to ppc_ as ppc is not available under mac os CW Mach-O
556 for ( char **ppc_
= mb_argv
; *ppc_
; ppc_
++ )
557 fprintf(stderr
, "%s%s", ppc_
== mb_argv
? "" : ", ", *ppc_
);
558 fprintf(stderr
, ") failed with error %d!\n", errno
);
560 // there is no return after successful exec()
563 // some compilers complain about missing return - of course, they
564 // should know that exit() doesn't return but what else can we do if
567 // and, sure enough, other compilers complain about unreachable code
568 // after exit() call, so we can just always have return here...
569 #if defined(__VMS) || defined(__INTEL_COMPILER)
573 else // we're in parent
577 // save it for WaitForChild() use
580 // prepare for IO redirection
583 // the input buffer bufOut is connected to stdout, this is why it is
584 // called bufOut and not bufIn
585 wxStreamTempInputBuffer bufOut
,
587 #endif // wxUSE_STREAMS
589 if ( process
&& process
->IsRedirected() )
592 wxOutputStream
*inStream
=
593 new wxFileOutputStream(pipeIn
.Detach(wxPipe::Write
));
595 wxPipeInputStream
*outStream
=
596 new wxPipeInputStream(pipeOut
.Detach(wxPipe::Read
));
598 wxPipeInputStream
*errStream
=
599 new wxPipeInputStream(pipeErr
.Detach(wxPipe::Read
));
601 process
->SetPipeStreams(outStream
, inStream
, errStream
);
603 bufOut
.Init(outStream
);
604 bufErr
.Init(errStream
);
606 execData
.bufOut
= &bufOut
;
607 execData
.bufErr
= &bufErr
;
608 #endif // wxUSE_STREAMS
618 return traits
->WaitForChild(execData
);
621 return ERROR_RETURN_CODE
;
625 #pragma message enable codeunreachable
628 #undef ERROR_RETURN_CODE
631 // ----------------------------------------------------------------------------
632 // file and directory functions
633 // ----------------------------------------------------------------------------
635 const wxChar
* wxGetHomeDir( wxString
*home
)
637 *home
= wxGetUserHome( wxString() );
639 if ( home
->IsEmpty() )
643 if ( tmp
.Last() != wxT(']'))
644 if ( tmp
.Last() != wxT('/')) *home
<< wxT('/');
646 return home
->c_str();
650 const wxMB2WXbuf
wxGetUserHome( const wxString
&user
)
651 #else // just for binary compatibility -- there is no 'const' here
652 char *wxGetUserHome( const wxString
&user
)
655 struct passwd
*who
= (struct passwd
*) NULL
;
661 if ((ptr
= wxGetenv(wxT("HOME"))) != NULL
)
664 wxWCharBuffer
buffer( ptr
);
670 if ((ptr
= wxGetenv(wxT("USER"))) != NULL
|| (ptr
= wxGetenv(wxT("LOGNAME"))) != NULL
)
672 who
= getpwnam(wxConvertWX2MB(ptr
));
675 // We now make sure the the user exists!
678 who
= getpwuid(getuid());
683 who
= getpwnam (user
.mb_str());
686 return wxConvertMB2WX(who
? who
->pw_dir
: 0);
689 // ----------------------------------------------------------------------------
690 // network and user id routines
691 // ----------------------------------------------------------------------------
693 // retrieve either the hostname or FQDN depending on platform (caller must
694 // check whether it's one or the other, this is why this function is for
696 static bool wxGetHostNameInternal(wxChar
*buf
, int sz
)
698 wxCHECK_MSG( buf
, FALSE
, wxT("NULL pointer in wxGetHostNameInternal") );
702 // we're using uname() which is POSIX instead of less standard sysinfo()
703 #if defined(HAVE_UNAME)
705 bool ok
= uname(&uts
) != -1;
708 wxStrncpy(buf
, wxConvertMB2WX(uts
.nodename
), sz
- 1);
711 #elif defined(HAVE_GETHOSTNAME)
712 bool ok
= gethostname(buf
, sz
) != -1;
713 #else // no uname, no gethostname
714 wxFAIL_MSG(wxT("don't know host name for this machine"));
717 #endif // uname/gethostname
721 wxLogSysError(_("Cannot get the hostname"));
727 bool wxGetHostName(wxChar
*buf
, int sz
)
729 bool ok
= wxGetHostNameInternal(buf
, sz
);
733 // BSD systems return the FQDN, we only want the hostname, so extract
734 // it (we consider that dots are domain separators)
735 wxChar
*dot
= wxStrchr(buf
, wxT('.'));
746 bool wxGetFullHostName(wxChar
*buf
, int sz
)
748 bool ok
= wxGetHostNameInternal(buf
, sz
);
752 if ( !wxStrchr(buf
, wxT('.')) )
754 struct hostent
*host
= gethostbyname(wxConvertWX2MB(buf
));
757 wxLogSysError(_("Cannot get the official hostname"));
763 // the canonical name
764 wxStrncpy(buf
, wxConvertMB2WX(host
->h_name
), sz
);
767 //else: it's already a FQDN (BSD behaves this way)
773 bool wxGetUserId(wxChar
*buf
, int sz
)
778 if ((who
= getpwuid(getuid ())) != NULL
)
780 wxStrncpy (buf
, wxConvertMB2WX(who
->pw_name
), sz
- 1);
787 bool wxGetUserName(wxChar
*buf
, int sz
)
792 if ((who
= getpwuid (getuid ())) != NULL
)
794 // pw_gecos field in struct passwd is not standard
796 char *comma
= strchr(who
->pw_gecos
, ',');
798 *comma
= '\0'; // cut off non-name comment fields
799 wxStrncpy (buf
, wxConvertMB2WX(who
->pw_gecos
), sz
- 1);
800 #else // !HAVE_PW_GECOS
801 wxStrncpy (buf
, wxConvertMB2WX(who
->pw_name
), sz
- 1);
802 #endif // HAVE_PW_GECOS/!HAVE_PW_GECOS
809 // this function is in mac/utils.cpp for wxMac
812 wxString
wxGetOsDescription()
814 #ifndef WXWIN_OS_DESCRIPTION
815 #error WXWIN_OS_DESCRIPTION should be defined in config.h by configure
817 return wxString::FromAscii( WXWIN_OS_DESCRIPTION
);
823 unsigned long wxGetProcessId()
825 return (unsigned long)getpid();
828 long wxGetFreeMemory()
830 #if defined(__LINUX__)
831 // get it from /proc/meminfo
832 FILE *fp
= fopen("/proc/meminfo", "r");
838 if ( fgets(buf
, WXSIZEOF(buf
), fp
) && fgets(buf
, WXSIZEOF(buf
), fp
) )
840 long memTotal
, memUsed
;
841 sscanf(buf
, "Mem: %ld %ld %ld", &memTotal
, &memUsed
, &memFree
);
848 #elif defined(__SUN__) && defined(_SC_AVPHYS_PAGES)
849 return sysconf(_SC_AVPHYS_PAGES
)*sysconf(_SC_PAGESIZE
);
850 //#elif defined(__FREEBSD__) -- might use sysctl() to find it out, probably
857 bool wxGetDiskSpace(const wxString
& path
, wxLongLong
*pTotal
, wxLongLong
*pFree
)
859 #if defined(HAVE_STATFS) || defined(HAVE_STATVFS)
860 // the case to "char *" is needed for AIX 4.3
862 if ( wxStatfs((char *)(const char*)path
.fn_str(), &fs
) != 0 )
864 wxLogSysError( wxT("Failed to get file system statistics") );
869 // under Solaris we also have to use f_frsize field instead of f_bsize
870 // which is in general a multiple of f_frsize
872 wxLongLong blockSize
= fs
.f_frsize
;
874 wxLongLong blockSize
= fs
.f_bsize
;
875 #endif // HAVE_STATVFS/HAVE_STATFS
879 *pTotal
= wxLongLong(fs
.f_blocks
) * blockSize
;
884 *pFree
= wxLongLong(fs
.f_bavail
) * blockSize
;
888 #else // !HAVE_STATFS && !HAVE_STATVFS
890 #endif // HAVE_STATFS
893 // ----------------------------------------------------------------------------
895 // ----------------------------------------------------------------------------
897 bool wxGetEnv(const wxString
& var
, wxString
*value
)
899 // wxGetenv is defined as getenv()
900 wxChar
*p
= wxGetenv(var
);
912 bool wxSetEnv(const wxString
& variable
, const wxChar
*value
)
914 #if defined(HAVE_SETENV)
915 return setenv(variable
.mb_str(),
916 value
? (const char *)wxString(value
).mb_str()
918 1 /* overwrite */) == 0;
919 #elif defined(HAVE_PUTENV)
920 wxString s
= variable
;
922 s
<< _T('=') << value
;
925 const char *p
= s
.mb_str();
927 // the string will be free()d by libc
928 char *buf
= (char *)malloc(strlen(p
) + 1);
931 return putenv(buf
) == 0;
932 #else // no way to set an env var
937 // ----------------------------------------------------------------------------
939 // ----------------------------------------------------------------------------
941 #if wxUSE_ON_FATAL_EXCEPTION
945 extern "C" void wxFatalSignalHandler(wxTYPE_SA_HANDLER
)
949 // give the user a chance to do something special about this
950 wxTheApp
->OnFatalException();
956 bool wxHandleFatalExceptions(bool doit
)
959 static bool s_savedHandlers
= FALSE
;
960 static struct sigaction s_handlerFPE
,
966 if ( doit
&& !s_savedHandlers
)
968 // install the signal handler
969 struct sigaction act
;
971 // some systems extend it with non std fields, so zero everything
972 memset(&act
, 0, sizeof(act
));
974 act
.sa_handler
= wxFatalSignalHandler
;
975 sigemptyset(&act
.sa_mask
);
978 ok
&= sigaction(SIGFPE
, &act
, &s_handlerFPE
) == 0;
979 ok
&= sigaction(SIGILL
, &act
, &s_handlerILL
) == 0;
980 ok
&= sigaction(SIGBUS
, &act
, &s_handlerBUS
) == 0;
981 ok
&= sigaction(SIGSEGV
, &act
, &s_handlerSEGV
) == 0;
984 wxLogDebug(_T("Failed to install our signal handler."));
987 s_savedHandlers
= TRUE
;
989 else if ( s_savedHandlers
)
991 // uninstall the signal handler
992 ok
&= sigaction(SIGFPE
, &s_handlerFPE
, NULL
) == 0;
993 ok
&= sigaction(SIGILL
, &s_handlerILL
, NULL
) == 0;
994 ok
&= sigaction(SIGBUS
, &s_handlerBUS
, NULL
) == 0;
995 ok
&= sigaction(SIGSEGV
, &s_handlerSEGV
, NULL
) == 0;
998 wxLogDebug(_T("Failed to uninstall our signal handler."));
1001 s_savedHandlers
= FALSE
;
1003 //else: nothing to do
1008 #endif // wxUSE_ON_FATAL_EXCEPTION
1010 // ----------------------------------------------------------------------------
1011 // error and debug output routines (deprecated, use wxLog)
1012 // ----------------------------------------------------------------------------
1014 #if WXWIN_COMPATIBILITY_2_2
1016 void wxDebugMsg( const char *format
, ... )
1019 va_start( ap
, format
);
1020 vfprintf( stderr
, format
, ap
);
1025 void wxError( const wxString
&msg
, const wxString
&title
)
1027 wxFprintf( stderr
, _("Error ") );
1028 if (!title
.IsNull()) wxFprintf( stderr
, wxT("%s "), WXSTRINGCAST(title
) );
1029 if (!msg
.IsNull()) wxFprintf( stderr
, wxT(": %s"), WXSTRINGCAST(msg
) );
1030 wxFprintf( stderr
, wxT(".\n") );
1033 void wxFatalError( const wxString
&msg
, const wxString
&title
)
1035 wxFprintf( stderr
, _("Error ") );
1036 if (!title
.IsNull()) wxFprintf( stderr
, wxT("%s "), WXSTRINGCAST(title
) );
1037 if (!msg
.IsNull()) wxFprintf( stderr
, wxT(": %s"), WXSTRINGCAST(msg
) );
1038 wxFprintf( stderr
, wxT(".\n") );
1039 exit(3); // the same exit code as for abort()
1042 #endif // WXWIN_COMPATIBILITY_2_2
1044 #endif // wxUSE_BASE
1048 // ----------------------------------------------------------------------------
1049 // wxExecute support
1050 // ----------------------------------------------------------------------------
1052 // Darwin doesn't use the same process end detection mechanisms so we don't
1053 // need wxExecute-related helpers for it
1054 #if !(defined(__DARWIN__) && defined(__WXMAC__))
1056 bool wxGUIAppTraits::CreateEndProcessPipe(wxExecuteData
& execData
)
1058 return execData
.pipeEndProcDetect
.Create();
1061 bool wxGUIAppTraits::IsWriteFDOfEndProcessPipe(wxExecuteData
& execData
, int fd
)
1063 return fd
== (execData
.pipeEndProcDetect
)[wxPipe::Write
];
1066 void wxGUIAppTraits::DetachWriteFDOfEndProcessPipe(wxExecuteData
& execData
)
1068 execData
.pipeEndProcDetect
.Detach(wxPipe::Write
);
1069 execData
.pipeEndProcDetect
.Close();
1074 bool wxGUIAppTraits::CreateEndProcessPipe(wxExecuteData
& WXUNUSED(execData
))
1080 wxGUIAppTraits::IsWriteFDOfEndProcessPipe(wxExecuteData
& WXUNUSED(execData
),
1087 wxGUIAppTraits::DetachWriteFDOfEndProcessPipe(wxExecuteData
& WXUNUSED(execData
))
1089 // nothing to do here, we don't use the pipe
1092 #endif // !Darwin/Darwin
1094 int wxGUIAppTraits::WaitForChild(wxExecuteData
& execData
)
1096 wxEndProcessData
*endProcData
= new wxEndProcessData
;
1098 // wxAddProcessCallback is now (with DARWIN) allowed to call the
1099 // callback function directly if the process terminates before
1100 // the callback can be added to the run loop. Set up the endProcData.
1101 if ( execData
.flags
& wxEXEC_SYNC
)
1103 // we may have process for capturing the program output, but it's
1104 // not used in wxEndProcessData in the case of sync execution
1105 endProcData
->process
= NULL
;
1107 // sync execution: indicate it by negating the pid
1108 endProcData
->pid
= -execData
.pid
;
1112 // async execution, nothing special to do -- caller will be
1113 // notified about the process termination if process != NULL, endProcData
1114 // will be deleted in GTK_EndProcessDetector
1115 endProcData
->process
= execData
.process
;
1116 endProcData
->pid
= execData
.pid
;
1120 #if defined(__DARWIN__) && defined(__WXMAC__)
1121 endProcData
->tag
= wxAddProcessCallbackForPid(endProcData
, execData
.pid
);
1123 endProcData
->tag
= wxAddProcessCallback
1126 execData
.pipeEndProcDetect
.Detach(wxPipe::Read
)
1129 execData
.pipeEndProcDetect
.Close();
1130 #endif // defined(__DARWIN__) && defined(__WXMAC__)
1132 if ( execData
.flags
& wxEXEC_SYNC
)
1135 wxWindowDisabler wd
;
1137 // endProcData->pid will be set to 0 from GTK_EndProcessDetector when the
1138 // process terminates
1139 while ( endProcData
->pid
!= 0 )
1142 if ( execData
.bufOut
)
1143 execData
.bufOut
->Update();
1145 if ( execData
.bufErr
)
1146 execData
.bufErr
->Update();
1147 #endif // wxUSE_STREAMS
1149 // give GTK+ a chance to call GTK_EndProcessDetector here and
1150 // also repaint the GUI
1154 int exitcode
= endProcData
->exitcode
;
1160 else // async execution
1162 return execData
.pid
;
1166 void wxHandleProcessTermination(wxEndProcessData
*proc_data
)
1168 // notify user about termination if required
1169 if ( proc_data
->process
)
1171 proc_data
->process
->OnTerminate(proc_data
->pid
, proc_data
->exitcode
);
1175 if ( proc_data
->pid
> 0 )
1181 // let wxExecute() know that the process has terminated