utilsunx.cpp
来自「A*算法 A*算法 A*算法 A*算法A*算法A*算法」· C++ 代码 · 共 1,275 行 · 第 1/3 页
CPP
1,275 行
/////////////////////////////////////////////////////////////////////////////
// Name: unix/utilsunx.cpp
// Purpose: generic Unix implementation of many wx functions
// Author: Vadim Zeitlin
// Id: $Id: utilsunx.cpp,v 1.127 2005/08/23 23:12:37 VZ Exp $
// Copyright: (c) 1998 Robert Roebling, Vadim Zeitlin
// Licence: wxWindows licence
/////////////////////////////////////////////////////////////////////////////
// ============================================================================
// declarations
// ============================================================================
// ----------------------------------------------------------------------------
// headers
// ----------------------------------------------------------------------------
#include <pwd.h>
// for compilers that support precompilation, includes "wx.h".
#include "wx/wxprec.h"
#include "wx/defs.h"
#include "wx/string.h"
#include "wx/intl.h"
#include "wx/log.h"
#include "wx/app.h"
#include "wx/apptrait.h"
#include "wx/utils.h"
#include "wx/process.h"
#include "wx/thread.h"
#include "wx/wfstream.h"
#include "wx/unix/execute.h"
#if wxUSE_STREAMS
// define this to let wxexec.cpp know that we know what we're doing
#define _WX_USED_BY_WXEXECUTE_
#include "../common/execcmn.cpp"
#endif // wxUSE_STREAMS
#if wxUSE_BASE
#if defined(__MWERKS__) && defined(__MACH__)
#ifndef WXWIN_OS_DESCRIPTION
#define WXWIN_OS_DESCRIPTION "MacOS X"
#endif
#ifndef HAVE_NANOSLEEP
#define HAVE_NANOSLEEP
#endif
#ifndef HAVE_UNAME
#define HAVE_UNAME
#endif
// our configure test believes we can use sigaction() if the function is
// available but Metrowekrs with MSL run-time does have the function but
// doesn't have sigaction struct so finally we can't use it...
#ifdef __MSL__
#undef wxUSE_ON_FATAL_EXCEPTION
#define wxUSE_ON_FATAL_EXCEPTION 0
#endif
#endif
// not only the statfs syscall is called differently depending on platform, but
// one of its incarnations, statvfs(), takes different arguments under
// different platforms and even different versions of the same system (Solaris
// 7 and 8): if you want to test for this, don't forget that the problems only
// appear if the large files support is enabled
#ifdef HAVE_STATFS
#ifdef __BSD__
#include <sys/param.h>
#include <sys/mount.h>
#else // !__BSD__
#include <sys/vfs.h>
#endif // __BSD__/!__BSD__
#define wxStatfs statfs
#ifndef HAVE_STATFS_DECL
// some systems lack statfs() prototype in the system headers (AIX 4)
extern "C" int statfs(const char *path, struct statfs *buf);
#endif
#endif // HAVE_STATFS
#ifdef HAVE_STATVFS
#include <sys/statvfs.h>
#define wxStatfs statvfs
#endif // HAVE_STATVFS
#if defined(HAVE_STATFS) || defined(HAVE_STATVFS)
// WX_STATFS_T is detected by configure
#define wxStatfs_t WX_STATFS_T
#endif
// SGI signal.h defines signal handler arguments differently depending on
// whether _LANGUAGE_C_PLUS_PLUS is set or not - do set it
#if defined(__SGI__) && !defined(_LANGUAGE_C_PLUS_PLUS)
#define _LANGUAGE_C_PLUS_PLUS 1
#endif // SGI hack
#include <stdarg.h>
#include <dirent.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <errno.h>
#include <netdb.h>
#include <signal.h>
#include <fcntl.h> // for O_WRONLY and friends
#include <time.h> // nanosleep() and/or usleep()
#include <ctype.h> // isspace()
#include <sys/time.h> // needed for FD_SETSIZE
#ifdef HAVE_UNAME
#include <sys/utsname.h> // for uname()
#endif // HAVE_UNAME
// ----------------------------------------------------------------------------
// conditional compilation
// ----------------------------------------------------------------------------
// many versions of Unices have this function, but it is not defined in system
// headers - please add your system here if it is the case for your OS.
// SunOS < 5.6 (i.e. Solaris < 2.6) and DG-UX are like this.
#if !defined(HAVE_USLEEP) && \
((defined(__SUN__) && !defined(__SunOs_5_6) && \
!defined(__SunOs_5_7) && !defined(__SUNPRO_CC)) || \
defined(__osf__) || defined(__EMX__))
extern "C"
{
#ifdef __SUN__
int usleep(unsigned int usec);
#else // !Sun
#ifdef __EMX__
/* I copied this from the XFree86 diffs. AV. */
#define INCL_DOSPROCESS
#include <os2.h>
inline void usleep(unsigned long delay)
{
DosSleep(delay ? (delay/1000l) : 1l);
}
#else // !Sun && !EMX
void usleep(unsigned long usec);
#endif
#endif // Sun/EMX/Something else
};
#define HAVE_USLEEP 1
#endif // Unices without usleep()
// ============================================================================
// implementation
// ============================================================================
// ----------------------------------------------------------------------------
// sleeping
// ----------------------------------------------------------------------------
void wxSleep(int nSecs)
{
sleep(nSecs);
}
void wxMicroSleep(unsigned long microseconds)
{
#if defined(HAVE_NANOSLEEP)
timespec tmReq;
tmReq.tv_sec = (time_t)(microseconds / 1000000);
tmReq.tv_nsec = (microseconds % 1000000) * 1000;
// we're not interested in remaining time nor in return value
(void)nanosleep(&tmReq, (timespec *)NULL);
#elif defined(HAVE_USLEEP)
// uncomment this if you feel brave or if you are sure that your version
// of Solaris has a safe usleep() function but please notice that usleep()
// is known to lead to crashes in MT programs in Solaris 2.[67] and is not
// documented as MT-Safe
#if defined(__SUN__) && wxUSE_THREADS
#error "usleep() cannot be used in MT programs under Solaris."
#endif // Sun
usleep(microseconds);
#elif defined(HAVE_SLEEP)
// under BeOS sleep() takes seconds (what about other platforms, if any?)
sleep(microseconds * 1000000);
#else // !sleep function
#error "usleep() or nanosleep() function required for wxMicroSleep"
#endif // sleep function
}
void wxMilliSleep(unsigned long milliseconds)
{
wxMicroSleep(milliseconds*1000);
}
// ----------------------------------------------------------------------------
// process management
// ----------------------------------------------------------------------------
int wxKill(long pid, wxSignal sig, wxKillError *rc, int flags)
{
int err = kill((pid_t) (flags & wxKILL_CHILDREN) ? -pid : pid, (int)sig);
if ( rc )
{
switch ( err ? errno : 0 )
{
case 0:
*rc = wxKILL_OK;
break;
case EINVAL:
*rc = wxKILL_BAD_SIGNAL;
break;
case EPERM:
*rc = wxKILL_ACCESS_DENIED;
break;
case ESRCH:
*rc = wxKILL_NO_PROCESS;
break;
default:
// this goes against Unix98 docs so log it
wxLogDebug(_T("unexpected kill(2) return value %d"), err);
// something else...
*rc = wxKILL_ERROR;
}
}
return err;
}
#define WXEXECUTE_NARGS 127
#if defined(__DARWIN__)
long wxMacExecute(wxChar **argv,
int flags,
wxProcess *process);
#endif
long wxExecute( const wxString& command, int flags, wxProcess *process )
{
wxCHECK_MSG( !command.empty(), 0, wxT("can't exec empty command") );
wxLogDebug(wxString(wxT("Launching: ")) + command);
#if wxUSE_THREADS
// fork() doesn't mix well with POSIX threads: on many systems the program
// deadlocks or crashes for some reason. Probably our code is buggy and
// doesn't do something which must be done to allow this to work, but I
// don't know what yet, so for now just warn the user (this is the least we
// can do) about it
wxASSERT_MSG( wxThread::IsMain(),
_T("wxExecute() can be called only from the main thread") );
#endif // wxUSE_THREADS
int argc = 0;
wxChar *argv[WXEXECUTE_NARGS];
wxString argument;
const wxChar *cptr = command.c_str();
wxChar quotechar = wxT('\0'); // is arg quoted?
bool escaped = false;
// split the command line in arguments
do
{
argument=wxT("");
quotechar = wxT('\0');
// eat leading whitespace:
while ( wxIsspace(*cptr) )
cptr++;
if ( *cptr == wxT('\'') || *cptr == wxT('"') )
quotechar = *cptr++;
do
{
if ( *cptr == wxT('\\') && ! escaped )
{
escaped = true;
cptr++;
continue;
}
// all other characters:
argument += *cptr++;
escaped = false;
// have we reached the end of the argument?
if ( (*cptr == quotechar && ! escaped)
|| (quotechar == wxT('\0') && wxIsspace(*cptr))
|| *cptr == wxT('\0') )
{
wxASSERT_MSG( argc < WXEXECUTE_NARGS,
wxT("too many arguments in wxExecute") );
argv[argc] = new wxChar[argument.length() + 1];
wxStrcpy(argv[argc], argument.c_str());
argc++;
// if not at end of buffer, swallow last character:
if(*cptr)
cptr++;
break; // done with this one, start over
}
} while(*cptr);
} while(*cptr);
argv[argc] = NULL;
long lRc;
#if defined(__DARWIN__)
// wxMacExecute only executes app bundles.
// It returns an error code if the target is not an app bundle, thus falling
// through to the regular wxExecute for non app bundles.
lRc = wxMacExecute(argv, flags, process);
if( lRc != ((flags & wxEXEC_SYNC) ? -1 : 0))
return lRc;
#endif
// do execute the command
lRc = wxExecute(argv, flags, process);
// clean up
argc = 0;
while( argv[argc] )
delete [] argv[argc++];
return lRc;
}
// ----------------------------------------------------------------------------
// wxShell
// ----------------------------------------------------------------------------
static wxString wxMakeShellCommand(const wxString& command)
{
wxString cmd;
if ( !command )
{
// just an interactive shell
cmd = _T("xterm");
}
else
{
// execute command in a shell
cmd << _T("/bin/sh -c '") << command << _T('\'');
}
return cmd;
}
bool wxShell(const wxString& command)
{
return wxExecute(wxMakeShellCommand(command), wxEXEC_SYNC) == 0;
}
bool wxShell(const wxString& command, wxArrayString& output)
{
wxCHECK_MSG( !command.empty(), false, _T("can't exec shell non interactively") );
return wxExecute(wxMakeShellCommand(command), output);
}
// Shutdown or reboot the PC
bool wxShutdown(wxShutdownFlags wFlags)
{
wxChar level;
switch ( wFlags )
{
case wxSHUTDOWN_POWEROFF:
level = _T('0');
break;
case wxSHUTDOWN_REBOOT:
level = _T('6');
break;
default:
wxFAIL_MSG( _T("unknown wxShutdown() flag") );
return false;
}
return system(wxString::Format(_T("init %c"), level).mb_str()) == 0;
}
wxPowerType wxGetPowerType()
{
// TODO
return wxPOWER_UNKNOWN;
}
wxBatteryState wxGetBatteryState()
{
// TODO
return wxBATTERY_UNKNOWN_STATE;
}
// ----------------------------------------------------------------------------
// wxStream classes to support IO redirection in wxExecute
// ----------------------------------------------------------------------------
#if wxUSE_STREAMS
bool wxPipeInputStream::CanRead() const
{
if ( m_lasterror == wxSTREAM_EOF )
return false;
// check if there is any input available
struct timeval tv;
tv.tv_sec = 0;
tv.tv_usec = 0;
const int fd = m_file->fd();
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?