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 wxUsleep(unsigned long milliseconds
)
154 #if defined(HAVE_NANOSLEEP)
156 tmReq
.tv_sec
= (time_t)(milliseconds
/ 1000);
157 tmReq
.tv_nsec
= (milliseconds
% 1000) * 1000 * 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(milliseconds
* 1000); // usleep(3) wants microseconds
171 #elif defined(HAVE_SLEEP)
172 // under BeOS sleep() takes seconds (what about other platforms, if any?)
173 sleep(milliseconds
* 1000);
174 #else // !sleep function
175 #error "usleep() or nanosleep() function required for wxUsleep"
176 #endif // sleep function
179 // ----------------------------------------------------------------------------
180 // process management
181 // ----------------------------------------------------------------------------
183 int wxKill(long pid
, wxSignal sig
, wxKillError
*rc
)
185 int err
= kill((pid_t
)pid
, (int)sig
);
195 *rc
= wxKILL_BAD_SIGNAL
;
199 *rc
= wxKILL_ACCESS_DENIED
;
203 *rc
= wxKILL_NO_PROCESS
;
207 // this goes against Unix98 docs so log it
208 wxLogDebug(_T("unexpected kill(2) return value %d"), err
);
218 #define WXEXECUTE_NARGS 127
220 long wxExecute( const wxString
& command
, int flags
, wxProcess
*process
)
222 wxCHECK_MSG( !command
.IsEmpty(), 0, wxT("can't exec empty command") );
225 // fork() doesn't mix well with POSIX threads: on many systems the program
226 // deadlocks or crashes for some reason. Probably our code is buggy and
227 // doesn't do something which must be done to allow this to work, but I
228 // don't know what yet, so for now just warn the user (this is the least we
230 wxASSERT_MSG( wxThread::IsMain(),
231 _T("wxExecute() can be called only from the main thread") );
232 #endif // wxUSE_THREADS
235 wxChar
*argv
[WXEXECUTE_NARGS
];
237 const wxChar
*cptr
= command
.c_str();
238 wxChar quotechar
= wxT('\0'); // is arg quoted?
239 bool escaped
= FALSE
;
241 // split the command line in arguments
245 quotechar
= wxT('\0');
247 // eat leading whitespace:
248 while ( wxIsspace(*cptr
) )
251 if ( *cptr
== wxT('\'') || *cptr
== wxT('"') )
256 if ( *cptr
== wxT('\\') && ! escaped
)
263 // all other characters:
267 // have we reached the end of the argument?
268 if ( (*cptr
== quotechar
&& ! escaped
)
269 || (quotechar
== wxT('\0') && wxIsspace(*cptr
))
270 || *cptr
== wxT('\0') )
272 wxASSERT_MSG( argc
< WXEXECUTE_NARGS
,
273 wxT("too many arguments in wxExecute") );
275 argv
[argc
] = new wxChar
[argument
.length() + 1];
276 wxStrcpy(argv
[argc
], argument
.c_str());
279 // if not at end of buffer, swallow last character:
283 break; // done with this one, start over
289 // do execute the command
290 long lRc
= wxExecute(argv
, flags
, process
);
295 delete [] argv
[argc
++];
300 // ----------------------------------------------------------------------------
302 // ----------------------------------------------------------------------------
304 static wxString
wxMakeShellCommand(const wxString
& command
)
309 // just an interactive shell
314 // execute command in a shell
315 cmd
<< _T("/bin/sh -c '") << command
<< _T('\'');
321 bool wxShell(const wxString
& command
)
323 return wxExecute(wxMakeShellCommand(command
), wxEXEC_SYNC
) == 0;
326 bool wxShell(const wxString
& command
, wxArrayString
& output
)
328 wxCHECK_MSG( !!command
, FALSE
, _T("can't exec shell non interactively") );
330 return wxExecute(wxMakeShellCommand(command
), output
);
333 // Shutdown or reboot the PC
334 bool wxShutdown(wxShutdownFlags wFlags
)
339 case wxSHUTDOWN_POWEROFF
:
343 case wxSHUTDOWN_REBOOT
:
348 wxFAIL_MSG( _T("unknown wxShutdown() flag") );
352 return system(wxString::Format(_T("init %c"), level
).mb_str()) == 0;
356 // ----------------------------------------------------------------------------
357 // wxStream classes to support IO redirection in wxExecute
358 // ----------------------------------------------------------------------------
362 bool wxPipeInputStream::CanRead() const
364 if ( m_lasterror
== wxSTREAM_EOF
)
367 // check if there is any input available
372 const int fd
= m_file
->fd();
376 FD_SET(fd
, &readfds
);
377 switch ( select(fd
+ 1, &readfds
, NULL
, NULL
, &tv
) )
380 wxLogSysError(_("Impossible to get child process input"));
387 wxFAIL_MSG(_T("unexpected select() return value"));
388 // still fall through
391 // input available -- or maybe not, as select() returns 1 when a
392 // read() will complete without delay, but it could still not read
398 #endif // wxUSE_STREAMS
400 // ----------------------------------------------------------------------------
401 // wxExecute: the real worker function
402 // ----------------------------------------------------------------------------
405 #pragma message disable codeunreachable
408 long wxExecute(wxChar
**argv
,
412 // for the sync execution, we return -1 to indicate failure, but for async
413 // case we return 0 which is never a valid PID
415 // we define this as a macro, not a variable, to avoid compiler warnings
416 // about "ERROR_RETURN_CODE value may be clobbered by fork()"
417 #define ERROR_RETURN_CODE ((flags & wxEXEC_SYNC) ? -1 : 0)
419 wxCHECK_MSG( *argv
, ERROR_RETURN_CODE
, wxT("can't exec empty command") );
423 char *mb_argv
[WXEXECUTE_NARGS
];
425 while (argv
[mb_argc
])
427 wxWX2MBbuf mb_arg
= wxConvertWX2MB(argv
[mb_argc
]);
428 mb_argv
[mb_argc
] = strdup(mb_arg
);
431 mb_argv
[mb_argc
] = (char *) NULL
;
433 // this macro will free memory we used above
434 #define ARGS_CLEANUP \
435 for ( mb_argc = 0; mb_argv[mb_argc]; mb_argc++ ) \
436 free(mb_argv[mb_argc])
438 // no need for cleanup
441 wxChar
**mb_argv
= argv
;
442 #endif // Unicode/ANSI
444 // we want this function to work even if there is no wxApp so ensure that
445 // we have a valid traits pointer
446 wxConsoleAppTraits traitsConsole
;
447 wxAppTraits
*traits
= wxTheApp
? wxTheApp
->GetTraits() : NULL
;
449 traits
= &traitsConsole
;
451 // this struct contains all information which we pass to and from
452 // wxAppTraits methods
453 wxExecuteData execData
;
454 execData
.flags
= flags
;
455 execData
.process
= process
;
458 if ( !traits
->CreateEndProcessPipe(execData
) )
460 wxLogError( _("Failed to execute '%s'\n"), *argv
);
464 return ERROR_RETURN_CODE
;
467 // pipes for inter process communication
468 wxPipe pipeIn
, // stdin
472 if ( process
&& process
->IsRedirected() )
474 if ( !pipeIn
.Create() || !pipeOut
.Create() || !pipeErr
.Create() )
476 wxLogError( _("Failed to execute '%s'\n"), *argv
);
480 return ERROR_RETURN_CODE
;
486 // NB: do *not* use vfork() here, it completely breaks this code for some
487 // reason under Solaris (and maybe others, although not under Linux)
488 // But on OpenVMS we do not have fork so we have to use vfork and
489 // cross our fingers that it works.
495 if ( pid
== -1 ) // error?
497 wxLogSysError( _("Fork failed") );
501 return ERROR_RETURN_CODE
;
503 else if ( pid
== 0 ) // we're in child
505 // These lines close the open file descriptors to to avoid any
506 // input/output which might block the process or irritate the user. If
507 // one wants proper IO for the subprocess, the right thing to do is to
508 // start an xterm executing it.
509 if ( !(flags
& wxEXEC_SYNC
) )
511 // FD_SETSIZE is unsigned under BSD, signed under other platforms
512 // so we need a cast to avoid warnings on all platforms
513 for ( int fd
= 0; fd
< (int)FD_SETSIZE
; fd
++ )
515 if ( fd
== pipeIn
[wxPipe::Read
]
516 || fd
== pipeOut
[wxPipe::Write
]
517 || fd
== pipeErr
[wxPipe::Write
]
518 || traits
->IsWriteFDOfEndProcessPipe(execData
, fd
) )
520 // don't close this one, we still need it
524 // leave stderr opened too, it won't do any harm
525 if ( fd
!= STDERR_FILENO
)
530 #if !defined(__VMS) && !defined(__EMX__)
531 if ( flags
& wxEXEC_MAKE_GROUP_LEADER
)
533 // Set process group to child process' pid. Then killing -pid
534 // of the parent will kill the process and all of its children.
539 // reading side can be safely closed but we should keep the write one
541 traits
->DetachWriteFDOfEndProcessPipe(execData
);
543 // redirect stdin, stdout and stderr
546 if ( dup2(pipeIn
[wxPipe::Read
], STDIN_FILENO
) == -1 ||
547 dup2(pipeOut
[wxPipe::Write
], STDOUT_FILENO
) == -1 ||
548 dup2(pipeErr
[wxPipe::Write
], STDERR_FILENO
) == -1 )
550 wxLogSysError(_("Failed to redirect child process input/output"));
558 execvp (*mb_argv
, mb_argv
);
560 fprintf(stderr
, "execvp(");
561 // CS changed ppc to ppc_ as ppc is not available under mac os CW Mach-O
562 for ( char **ppc_
= mb_argv
; *ppc_
; ppc_
++ )
563 fprintf(stderr
, "%s%s", ppc_
== mb_argv
? "" : ", ", *ppc_
);
564 fprintf(stderr
, ") failed with error %d!\n", errno
);
566 // there is no return after successful exec()
569 // some compilers complain about missing return - of course, they
570 // should know that exit() doesn't return but what else can we do if
573 // and, sure enough, other compilers complain about unreachable code
574 // after exit() call, so we can just always have return here...
575 #if defined(__VMS) || defined(__INTEL_COMPILER)
579 else // we're in parent
583 // save it for WaitForChild() use
586 // prepare for IO redirection
589 // the input buffer bufOut is connected to stdout, this is why it is
590 // called bufOut and not bufIn
591 wxStreamTempInputBuffer bufOut
,
593 #endif // wxUSE_STREAMS
595 if ( process
&& process
->IsRedirected() )
598 wxOutputStream
*inStream
=
599 new wxFileOutputStream(pipeIn
.Detach(wxPipe::Write
));
601 wxPipeInputStream
*outStream
=
602 new wxPipeInputStream(pipeOut
.Detach(wxPipe::Read
));
604 wxPipeInputStream
*errStream
=
605 new wxPipeInputStream(pipeErr
.Detach(wxPipe::Read
));
607 process
->SetPipeStreams(outStream
, inStream
, errStream
);
609 bufOut
.Init(outStream
);
610 bufErr
.Init(errStream
);
612 execData
.bufOut
= &bufOut
;
613 execData
.bufErr
= &bufErr
;
614 #endif // wxUSE_STREAMS
624 return traits
->WaitForChild(execData
);
627 return ERROR_RETURN_CODE
;
631 #pragma message enable codeunreachable
634 #undef ERROR_RETURN_CODE
637 // ----------------------------------------------------------------------------
638 // file and directory functions
639 // ----------------------------------------------------------------------------
641 const wxChar
* wxGetHomeDir( wxString
*home
)
643 *home
= wxGetUserHome( wxString() );
645 if ( home
->IsEmpty() )
649 if ( tmp
.Last() != wxT(']'))
650 if ( tmp
.Last() != wxT('/')) *home
<< wxT('/');
652 return home
->c_str();
656 const wxMB2WXbuf
wxGetUserHome( const wxString
&user
)
657 #else // just for binary compatibility -- there is no 'const' here
658 char *wxGetUserHome( const wxString
&user
)
661 struct passwd
*who
= (struct passwd
*) NULL
;
667 if ((ptr
= wxGetenv(wxT("HOME"))) != NULL
)
670 wxWCharBuffer
buffer( ptr
);
676 if ((ptr
= wxGetenv(wxT("USER"))) != NULL
|| (ptr
= wxGetenv(wxT("LOGNAME"))) != NULL
)
678 who
= getpwnam(wxConvertWX2MB(ptr
));
681 // We now make sure the the user exists!
684 who
= getpwuid(getuid());
689 who
= getpwnam (user
.mb_str());
692 return wxConvertMB2WX(who
? who
->pw_dir
: 0);
695 // ----------------------------------------------------------------------------
696 // network and user id routines
697 // ----------------------------------------------------------------------------
699 // retrieve either the hostname or FQDN depending on platform (caller must
700 // check whether it's one or the other, this is why this function is for
702 static bool wxGetHostNameInternal(wxChar
*buf
, int sz
)
704 wxCHECK_MSG( buf
, FALSE
, wxT("NULL pointer in wxGetHostNameInternal") );
708 // we're using uname() which is POSIX instead of less standard sysinfo()
709 #if defined(HAVE_UNAME)
711 bool ok
= uname(&uts
) != -1;
714 wxStrncpy(buf
, wxConvertMB2WX(uts
.nodename
), sz
- 1);
717 #elif defined(HAVE_GETHOSTNAME)
718 bool ok
= gethostname(buf
, sz
) != -1;
719 #else // no uname, no gethostname
720 wxFAIL_MSG(wxT("don't know host name for this machine"));
723 #endif // uname/gethostname
727 wxLogSysError(_("Cannot get the hostname"));
733 bool wxGetHostName(wxChar
*buf
, int sz
)
735 bool ok
= wxGetHostNameInternal(buf
, sz
);
739 // BSD systems return the FQDN, we only want the hostname, so extract
740 // it (we consider that dots are domain separators)
741 wxChar
*dot
= wxStrchr(buf
, wxT('.'));
752 bool wxGetFullHostName(wxChar
*buf
, int sz
)
754 bool ok
= wxGetHostNameInternal(buf
, sz
);
758 if ( !wxStrchr(buf
, wxT('.')) )
760 struct hostent
*host
= gethostbyname(wxConvertWX2MB(buf
));
763 wxLogSysError(_("Cannot get the official hostname"));
769 // the canonical name
770 wxStrncpy(buf
, wxConvertMB2WX(host
->h_name
), sz
);
773 //else: it's already a FQDN (BSD behaves this way)
779 bool wxGetUserId(wxChar
*buf
, int sz
)
784 if ((who
= getpwuid(getuid ())) != NULL
)
786 wxStrncpy (buf
, wxConvertMB2WX(who
->pw_name
), sz
- 1);
793 bool wxGetUserName(wxChar
*buf
, int sz
)
798 if ((who
= getpwuid (getuid ())) != NULL
)
800 // pw_gecos field in struct passwd is not standard
802 char *comma
= strchr(who
->pw_gecos
, ',');
804 *comma
= '\0'; // cut off non-name comment fields
805 wxStrncpy (buf
, wxConvertMB2WX(who
->pw_gecos
), sz
- 1);
806 #else // !HAVE_PW_GECOS
807 wxStrncpy (buf
, wxConvertMB2WX(who
->pw_name
), sz
- 1);
808 #endif // HAVE_PW_GECOS/!HAVE_PW_GECOS
815 // this function is in mac/utils.cpp for wxMac
818 wxString
wxGetOsDescription()
820 #ifndef WXWIN_OS_DESCRIPTION
821 #error WXWIN_OS_DESCRIPTION should be defined in config.h by configure
823 return wxString::FromAscii( WXWIN_OS_DESCRIPTION
);
829 unsigned long wxGetProcessId()
831 return (unsigned long)getpid();
834 long wxGetFreeMemory()
836 #if defined(__LINUX__)
837 // get it from /proc/meminfo
838 FILE *fp
= fopen("/proc/meminfo", "r");
844 if ( fgets(buf
, WXSIZEOF(buf
), fp
) && fgets(buf
, WXSIZEOF(buf
), fp
) )
846 long memTotal
, memUsed
;
847 sscanf(buf
, "Mem: %ld %ld %ld", &memTotal
, &memUsed
, &memFree
);
854 #elif defined(__SUN__) && defined(_SC_AVPHYS_PAGES)
855 return sysconf(_SC_AVPHYS_PAGES
)*sysconf(_SC_PAGESIZE
);
856 //#elif defined(__FREEBSD__) -- might use sysctl() to find it out, probably
863 bool wxGetDiskSpace(const wxString
& path
, wxLongLong
*pTotal
, wxLongLong
*pFree
)
865 #if defined(HAVE_STATFS) || defined(HAVE_STATVFS)
866 // the case to "char *" is needed for AIX 4.3
868 if ( wxStatfs((char *)(const char*)path
.fn_str(), &fs
) != 0 )
870 wxLogSysError( wxT("Failed to get file system statistics") );
875 // under Solaris we also have to use f_frsize field instead of f_bsize
876 // which is in general a multiple of f_frsize
878 wxLongLong blockSize
= fs
.f_frsize
;
880 wxLongLong blockSize
= fs
.f_bsize
;
881 #endif // HAVE_STATVFS/HAVE_STATFS
885 *pTotal
= wxLongLong(fs
.f_blocks
) * blockSize
;
890 *pFree
= wxLongLong(fs
.f_bavail
) * blockSize
;
894 #else // !HAVE_STATFS && !HAVE_STATVFS
896 #endif // HAVE_STATFS
899 // ----------------------------------------------------------------------------
901 // ----------------------------------------------------------------------------
903 bool wxGetEnv(const wxString
& var
, wxString
*value
)
905 // wxGetenv is defined as getenv()
906 wxChar
*p
= wxGetenv(var
);
918 bool wxSetEnv(const wxString
& variable
, const wxChar
*value
)
920 #if defined(HAVE_SETENV)
921 return setenv(variable
.mb_str(),
922 value
? (const char *)wxString(value
).mb_str()
924 1 /* overwrite */) == 0;
925 #elif defined(HAVE_PUTENV)
926 wxString s
= variable
;
928 s
<< _T('=') << value
;
931 const wxWX2MBbuf p
= s
.mb_str();
933 // the string will be free()d by libc
934 char *buf
= (char *)malloc(strlen(p
) + 1);
937 return putenv(buf
) == 0;
938 #else // no way to set an env var
943 // ----------------------------------------------------------------------------
945 // ----------------------------------------------------------------------------
947 #if wxUSE_ON_FATAL_EXCEPTION
951 extern "C" void wxFatalSignalHandler(wxTYPE_SA_HANDLER
)
955 // give the user a chance to do something special about this
956 wxTheApp
->OnFatalException();
962 bool wxHandleFatalExceptions(bool doit
)
965 static bool s_savedHandlers
= FALSE
;
966 static struct sigaction s_handlerFPE
,
972 if ( doit
&& !s_savedHandlers
)
974 // install the signal handler
975 struct sigaction act
;
977 // some systems extend it with non std fields, so zero everything
978 memset(&act
, 0, sizeof(act
));
980 act
.sa_handler
= wxFatalSignalHandler
;
981 sigemptyset(&act
.sa_mask
);
984 ok
&= sigaction(SIGFPE
, &act
, &s_handlerFPE
) == 0;
985 ok
&= sigaction(SIGILL
, &act
, &s_handlerILL
) == 0;
986 ok
&= sigaction(SIGBUS
, &act
, &s_handlerBUS
) == 0;
987 ok
&= sigaction(SIGSEGV
, &act
, &s_handlerSEGV
) == 0;
990 wxLogDebug(_T("Failed to install our signal handler."));
993 s_savedHandlers
= TRUE
;
995 else if ( s_savedHandlers
)
997 // uninstall the signal handler
998 ok
&= sigaction(SIGFPE
, &s_handlerFPE
, NULL
) == 0;
999 ok
&= sigaction(SIGILL
, &s_handlerILL
, NULL
) == 0;
1000 ok
&= sigaction(SIGBUS
, &s_handlerBUS
, NULL
) == 0;
1001 ok
&= sigaction(SIGSEGV
, &s_handlerSEGV
, NULL
) == 0;
1004 wxLogDebug(_T("Failed to uninstall our signal handler."));
1007 s_savedHandlers
= FALSE
;
1009 //else: nothing to do
1014 #endif // wxUSE_ON_FATAL_EXCEPTION
1016 // ----------------------------------------------------------------------------
1017 // error and debug output routines (deprecated, use wxLog)
1018 // ----------------------------------------------------------------------------
1020 #if WXWIN_COMPATIBILITY_2_2
1022 void wxDebugMsg( const char *format
, ... )
1025 va_start( ap
, format
);
1026 vfprintf( stderr
, format
, ap
);
1031 void wxError( const wxString
&msg
, const wxString
&title
)
1033 wxFprintf( stderr
, _("Error ") );
1034 if (!title
.IsNull()) wxFprintf( stderr
, wxT("%s "), WXSTRINGCAST(title
) );
1035 if (!msg
.IsNull()) wxFprintf( stderr
, wxT(": %s"), WXSTRINGCAST(msg
) );
1036 wxFprintf( stderr
, wxT(".\n") );
1039 void wxFatalError( const wxString
&msg
, const wxString
&title
)
1041 wxFprintf( stderr
, _("Error ") );
1042 if (!title
.IsNull()) wxFprintf( stderr
, wxT("%s "), WXSTRINGCAST(title
) );
1043 if (!msg
.IsNull()) wxFprintf( stderr
, wxT(": %s"), WXSTRINGCAST(msg
) );
1044 wxFprintf( stderr
, wxT(".\n") );
1045 exit(3); // the same exit code as for abort()
1048 #endif // WXWIN_COMPATIBILITY_2_2
1050 #endif // wxUSE_BASE
1054 // ----------------------------------------------------------------------------
1055 // wxExecute support
1056 // ----------------------------------------------------------------------------
1058 // Darwin doesn't use the same process end detection mechanisms so we don't
1059 // need wxExecute-related helpers for it
1060 #if !(defined(__DARWIN__) && defined(__WXMAC__))
1062 bool wxGUIAppTraits::CreateEndProcessPipe(wxExecuteData
& execData
)
1064 return execData
.pipeEndProcDetect
.Create();
1067 bool wxGUIAppTraits::IsWriteFDOfEndProcessPipe(wxExecuteData
& execData
, int fd
)
1069 return fd
== (execData
.pipeEndProcDetect
)[wxPipe::Write
];
1072 void wxGUIAppTraits::DetachWriteFDOfEndProcessPipe(wxExecuteData
& execData
)
1074 execData
.pipeEndProcDetect
.Detach(wxPipe::Write
);
1075 execData
.pipeEndProcDetect
.Close();
1080 bool wxGUIAppTraits::CreateEndProcessPipe(wxExecuteData
& WXUNUSED(execData
))
1086 wxGUIAppTraits::IsWriteFDOfEndProcessPipe(wxExecuteData
& WXUNUSED(execData
),
1093 wxGUIAppTraits::DetachWriteFDOfEndProcessPipe(wxExecuteData
& WXUNUSED(execData
))
1095 // nothing to do here, we don't use the pipe
1098 #endif // !Darwin/Darwin
1100 int wxGUIAppTraits::WaitForChild(wxExecuteData
& execData
)
1102 wxEndProcessData
*endProcData
= new wxEndProcessData
;
1104 // wxAddProcessCallback is now (with DARWIN) allowed to call the
1105 // callback function directly if the process terminates before
1106 // the callback can be added to the run loop. Set up the endProcData.
1107 if ( execData
.flags
& wxEXEC_SYNC
)
1109 // we may have process for capturing the program output, but it's
1110 // not used in wxEndProcessData in the case of sync execution
1111 endProcData
->process
= NULL
;
1113 // sync execution: indicate it by negating the pid
1114 endProcData
->pid
= -execData
.pid
;
1118 // async execution, nothing special to do -- caller will be
1119 // notified about the process termination if process != NULL, endProcData
1120 // will be deleted in GTK_EndProcessDetector
1121 endProcData
->process
= execData
.process
;
1122 endProcData
->pid
= execData
.pid
;
1126 #if defined(__DARWIN__) && defined(__WXMAC__)
1127 endProcData
->tag
= wxAddProcessCallbackForPid(endProcData
, execData
.pid
);
1129 endProcData
->tag
= wxAddProcessCallback
1132 execData
.pipeEndProcDetect
.Detach(wxPipe::Read
)
1135 execData
.pipeEndProcDetect
.Close();
1136 #endif // defined(__DARWIN__) && defined(__WXMAC__)
1138 if ( execData
.flags
& wxEXEC_SYNC
)
1141 wxWindowDisabler wd
;
1143 // endProcData->pid will be set to 0 from GTK_EndProcessDetector when the
1144 // process terminates
1145 while ( endProcData
->pid
!= 0 )
1150 if ( execData
.bufOut
)
1152 execData
.bufOut
->Update();
1156 if ( execData
.bufErr
)
1158 execData
.bufErr
->Update();
1161 #endif // wxUSE_STREAMS
1163 // don't consume 100% of the CPU while we're sitting this in this
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
;
1188 void wxHandleProcessTermination(wxEndProcessData
*proc_data
)
1190 // notify user about termination if required
1191 if ( proc_data
->process
)
1193 proc_data
->process
->OnTerminate(proc_data
->pid
, proc_data
->exitcode
);
1197 if ( proc_data
->pid
> 0 )
1203 // let wxExecute() know that the process has terminated
1208 #endif // wxUSE_BASE