2001-04-09 20:54:03 +01:00
|
|
|
/*************************************************************************
|
|
|
|
* *
|
|
|
|
* YAP Prolog *
|
|
|
|
* *
|
|
|
|
* Yap Prolog was developed at NCCUP - Universidade do Porto *
|
|
|
|
* *
|
|
|
|
* Copyright L.Damas, V.S.Costa and Universidade do Porto 1985-1997 *
|
|
|
|
* *
|
|
|
|
**************************************************************************
|
|
|
|
* *
|
|
|
|
* File: sysbits.c *
|
|
|
|
* Last rev: 4/03/88 *
|
|
|
|
* mods: *
|
|
|
|
* comments: very much machine dependent routines *
|
|
|
|
* *
|
|
|
|
*************************************************************************/
|
|
|
|
#ifdef SCCS
|
|
|
|
static char SccsId[] = "%W% %G%";
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* In this routine we shall try to include the inevitably machine dependant
|
2001-11-26 16:05:19 +00:00
|
|
|
* routines. These include, for the moment : Time, A rudimentary form of
|
2001-04-09 20:54:03 +01:00
|
|
|
* signal handling, OS calls,
|
|
|
|
*
|
|
|
|
* Vitor Santos Costa, February 1987
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2002-10-17 04:42:14 +01:00
|
|
|
/* windows.h does not like absmi.h, this
|
|
|
|
should fix it for now */
|
|
|
|
#include "absmi.h"
|
|
|
|
#include "yapio.h"
|
2001-04-09 20:54:03 +01:00
|
|
|
#include "alloc.h"
|
|
|
|
#include <math.h>
|
|
|
|
#if STDC_HEADERS
|
|
|
|
#include <stdlib.h>
|
|
|
|
#endif
|
2002-11-11 17:38:10 +00:00
|
|
|
#if HAVE_WINDOWS_H
|
|
|
|
#include <windows.h>
|
|
|
|
#endif
|
2007-05-26 09:19:33 +01:00
|
|
|
#if HAVE_SYS_TIME_H && !_MSC_VER
|
2001-04-09 20:54:03 +01:00
|
|
|
#include <sys/time.h>
|
|
|
|
#endif
|
|
|
|
#if HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
#if HAVE_SYS_WAIT_H && !defined(__MINGW32__) && !_MSC_VER
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#endif
|
|
|
|
#if HAVE_STRING_H
|
|
|
|
#include <string.h>
|
|
|
|
#endif
|
|
|
|
#if !HAVE_STRNCAT
|
|
|
|
#define strncat(X,Y,Z) strcat(X,Y)
|
|
|
|
#endif
|
|
|
|
#if !HAVE_STRNCPY
|
|
|
|
#define strncpy(X,Y,Z) strcpy(X,Y)
|
|
|
|
#endif
|
|
|
|
#if HAVE_GETPWNAM
|
|
|
|
#include <pwd.h>
|
|
|
|
#endif
|
|
|
|
#if HAVE_SYS_STAT_H
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#endif
|
|
|
|
#if HAVE_SYS_TYPES_H
|
|
|
|
#include <sys/types.h>
|
|
|
|
#endif
|
|
|
|
#if HAVE_FCNTL_H
|
|
|
|
#include <fcntl.h>
|
|
|
|
#endif
|
2002-01-19 16:10:53 +00:00
|
|
|
#if _MSC_VER || defined(__MINGW32__)
|
2001-04-09 20:54:03 +01:00
|
|
|
#include <windows.h>
|
|
|
|
/* required for DLL compatibility */
|
|
|
|
#if HAVE_DIRECT_H
|
|
|
|
#include <direct.h>
|
|
|
|
#endif
|
|
|
|
#include <io.h>
|
|
|
|
#else
|
|
|
|
#if HAVE_SYS_PARAM_H
|
|
|
|
#include <sys/param.h>
|
|
|
|
#endif
|
|
|
|
#endif
|
2012-04-18 20:14:56 +01:00
|
|
|
/* CYGWIN seems to include this automatically */
|
|
|
|
#if HAVE_FENV_H && !defined(__CYGWIN__)
|
2009-06-15 20:59:50 +01:00
|
|
|
#include <fenv.h>
|
|
|
|
#endif
|
|
|
|
#if HAVE_READLINE_READLINE_H
|
2001-06-12 18:25:28 +01:00
|
|
|
#include <readline/readline.h>
|
|
|
|
#endif
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2011-05-25 16:40:36 +01:00
|
|
|
|
2013-04-25 23:15:04 +01:00
|
|
|
static void InitTime(int);
|
|
|
|
static void InitWTime(void);
|
|
|
|
static Int p_sh( USES_REGS1 );
|
|
|
|
static Int p_shell( USES_REGS1 );
|
|
|
|
static Int p_system( USES_REGS1 );
|
|
|
|
static Int p_mv( USES_REGS1 );
|
|
|
|
static Int p_dir_sp( USES_REGS1 );
|
|
|
|
static void InitRandom(void);
|
|
|
|
static Int p_srandom( USES_REGS1 );
|
|
|
|
static Int p_alarm( USES_REGS1 );
|
|
|
|
static Int p_getenv( USES_REGS1 );
|
|
|
|
static Int p_putenv( USES_REGS1 );
|
|
|
|
static void set_fpu_exceptions(int);
|
2001-04-09 20:54:03 +01:00
|
|
|
#ifdef MACYAP
|
2013-04-25 23:15:04 +01:00
|
|
|
static int chdir(char *);
|
2001-04-09 20:54:03 +01:00
|
|
|
/* #define signal skel_signal */
|
|
|
|
#endif /* MACYAP */
|
|
|
|
|
2002-01-23 02:28:31 +00:00
|
|
|
|
2013-04-25 23:15:04 +01:00
|
|
|
void exit(int);
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2001-04-26 18:53:37 +01:00
|
|
|
#ifdef _WIN32
|
2010-04-07 01:35:44 +01:00
|
|
|
void
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_WinError(char *yap_error)
|
2001-04-26 18:53:37 +01:00
|
|
|
{
|
|
|
|
char msg[256];
|
|
|
|
/* Error, we could not read time */
|
2004-08-11 17:14:55 +01:00
|
|
|
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
|
2001-04-26 18:53:37 +01:00
|
|
|
NULL, GetLastError(),
|
|
|
|
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), msg, 256,
|
|
|
|
NULL);
|
2005-11-23 13:24:00 +00:00
|
|
|
Yap_Error(OPERATING_SYSTEM_ERROR, TermNil, "%s at %s", msg, yap_error);
|
2001-04-26 18:53:37 +01:00
|
|
|
}
|
|
|
|
#endif /* _WIN32 */
|
|
|
|
|
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
#define is_valid_env_char(C) ( ((C) >= 'a' && (C) <= 'z') || ((C) >= 'A' && \
|
|
|
|
(C) <= 'Z') || (C) == '_' )
|
|
|
|
|
|
|
|
|
2006-05-17 19:38:11 +01:00
|
|
|
static int
|
|
|
|
is_directory(char *FileName)
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
DWORD dwAtts = GetFileAttributes(FileName);
|
|
|
|
if (dwAtts == INVALID_FILE_ATTRIBUTES)
|
|
|
|
return FALSE;
|
|
|
|
return (dwAtts & FILE_ATTRIBUTE_DIRECTORY);
|
|
|
|
#elif HAVE_LSTAT
|
|
|
|
struct stat buf;
|
|
|
|
|
|
|
|
if (lstat(FileName, &buf) == -1) {
|
|
|
|
/* return an error number */
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
return S_ISDIR(buf.st_mode);
|
|
|
|
#else
|
|
|
|
return FALSE;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2002-11-11 17:38:10 +00:00
|
|
|
static int
|
2001-04-09 20:54:03 +01:00
|
|
|
dir_separator (int ch)
|
|
|
|
{
|
|
|
|
#ifdef MAC
|
|
|
|
return (ch == ':');
|
|
|
|
#elif ATARI || _MSC_VER
|
|
|
|
return (ch == '\\');
|
2002-10-17 02:37:46 +01:00
|
|
|
#elif defined(__MINGW32__) || defined(__CYGWIN__)
|
2001-04-09 20:54:03 +01:00
|
|
|
return (ch == '\\' || ch == '/');
|
|
|
|
#else
|
|
|
|
return (ch == '/');
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2002-11-11 17:38:10 +00:00
|
|
|
int
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_dir_separator (int ch)
|
2002-11-11 17:38:10 +00:00
|
|
|
{
|
|
|
|
return dir_separator (ch);
|
|
|
|
}
|
|
|
|
|
2006-08-30 02:06:30 +01:00
|
|
|
#if _MSC_VER || defined(__MINGW32__)
|
|
|
|
#include <psapi.h>
|
|
|
|
|
|
|
|
char *libdir = NULL;
|
|
|
|
#endif
|
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
void
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_InitSysPath(void) {
|
2011-03-07 16:02:55 +00:00
|
|
|
CACHE_REGS
|
2009-02-16 12:25:03 +00:00
|
|
|
int len;
|
2008-03-27 00:41:33 +00:00
|
|
|
#if _MSC_VER || defined(__MINGW32__)
|
2009-02-16 12:25:03 +00:00
|
|
|
int dir_done = FALSE;
|
|
|
|
int commons_done = FALSE;
|
2008-03-27 00:41:33 +00:00
|
|
|
{
|
|
|
|
char *dir;
|
|
|
|
if ((dir = Yap_RegistryGetString("library"))) {
|
2008-12-23 01:53:52 +00:00
|
|
|
Yap_PutValue(AtomSystemLibraryDir,
|
2008-03-27 00:41:33 +00:00
|
|
|
MkAtomTerm(Yap_LookupAtom(dir)));
|
2009-02-16 12:25:03 +00:00
|
|
|
dir_done = TRUE;
|
|
|
|
}
|
|
|
|
if ((dir = Yap_RegistryGetString("prolog_commons"))) {
|
|
|
|
Yap_PutValue(AtomPrologCommonsDir,
|
|
|
|
MkAtomTerm(Yap_LookupAtom(dir)));
|
|
|
|
commons_done = TRUE;
|
2008-03-27 00:41:33 +00:00
|
|
|
}
|
|
|
|
}
|
2009-02-16 12:25:03 +00:00
|
|
|
if (dir_done && commons_done)
|
|
|
|
return;
|
2008-03-27 00:41:33 +00:00
|
|
|
#endif
|
2011-05-23 16:19:47 +01:00
|
|
|
strncpy(LOCAL_FileNameBuf, YAP_SHAREDIR, YAP_FILENAME_MAX);
|
2006-08-30 02:06:30 +01:00
|
|
|
#if _MSC_VER || defined(__MINGW32__)
|
|
|
|
{
|
|
|
|
DWORD fatts;
|
|
|
|
int buflen;
|
|
|
|
char *pt;
|
|
|
|
|
2011-05-23 16:19:47 +01:00
|
|
|
if ((fatts = GetFileAttributes(LOCAL_FileNameBuf)) == 0xFFFFFFFFL ||
|
2006-08-30 02:06:30 +01:00
|
|
|
!(fatts & FILE_ATTRIBUTE_DIRECTORY)) {
|
|
|
|
/* couldn't find it where it was supposed to be,
|
|
|
|
let's try using the executable */
|
2011-05-23 16:19:47 +01:00
|
|
|
if (!GetModuleFileNameEx( GetCurrentProcess(), NULL, LOCAL_FileNameBuf, YAP_FILENAME_MAX)) {
|
2006-08-30 02:06:30 +01:00
|
|
|
Yap_Error(OPERATING_SYSTEM_ERROR, TermNil, "could not find executable name");
|
|
|
|
/* do nothing */
|
|
|
|
return;
|
|
|
|
}
|
2011-05-23 16:19:47 +01:00
|
|
|
buflen = strlen(LOCAL_FileNameBuf);
|
2012-06-21 09:09:49 +01:00
|
|
|
pt = LOCAL_FileNameBuf+buflen;
|
2006-08-30 02:06:30 +01:00
|
|
|
while (*--pt != '\\') {
|
|
|
|
/* skip executable */
|
2011-05-23 16:19:47 +01:00
|
|
|
if (pt == LOCAL_FileNameBuf) {
|
2006-08-30 02:06:30 +01:00
|
|
|
Yap_Error(OPERATING_SYSTEM_ERROR, TermNil, "could not find executable name");
|
|
|
|
/* do nothing */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (*--pt != '\\') {
|
|
|
|
/* skip parent directory "bin\\" */
|
2011-05-23 16:19:47 +01:00
|
|
|
if (pt == LOCAL_FileNameBuf) {
|
2006-08-30 02:06:30 +01:00
|
|
|
Yap_Error(OPERATING_SYSTEM_ERROR, TermNil, "could not find executable name");
|
|
|
|
/* do nothing */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* now, this is a possible location for the ROOT_DIR, let's look for a share directory here */
|
|
|
|
pt[1] = '\0';
|
|
|
|
/* grosse */
|
2011-05-23 16:19:47 +01:00
|
|
|
strncat(LOCAL_FileNameBuf,"lib\\Yap",YAP_FILENAME_MAX);
|
|
|
|
libdir = Yap_AllocCodeSpace(strlen(LOCAL_FileNameBuf)+1);
|
|
|
|
strncpy(libdir, LOCAL_FileNameBuf, strlen(LOCAL_FileNameBuf)+1);
|
2006-08-30 02:06:30 +01:00
|
|
|
pt[1] = '\0';
|
2011-05-23 16:19:47 +01:00
|
|
|
strncat(LOCAL_FileNameBuf,"share",YAP_FILENAME_MAX);
|
2006-08-30 02:06:30 +01:00
|
|
|
}
|
|
|
|
}
|
2011-05-23 16:19:47 +01:00
|
|
|
strncat(LOCAL_FileNameBuf,"\\", YAP_FILENAME_MAX);
|
2001-04-09 20:54:03 +01:00
|
|
|
#else
|
2011-05-23 16:19:47 +01:00
|
|
|
strncat(LOCAL_FileNameBuf,"/", YAP_FILENAME_MAX);
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif
|
2011-05-23 16:19:47 +01:00
|
|
|
len = strlen(LOCAL_FileNameBuf);
|
|
|
|
strncat(LOCAL_FileNameBuf, "Yap", YAP_FILENAME_MAX);
|
2009-02-16 12:25:03 +00:00
|
|
|
#if _MSC_VER || defined(__MINGW32__)
|
|
|
|
if (!dir_done)
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
Yap_PutValue(AtomSystemLibraryDir,
|
2011-05-23 16:19:47 +01:00
|
|
|
MkAtomTerm(Yap_LookupAtom(LOCAL_FileNameBuf)));
|
2009-02-16 12:25:03 +00:00
|
|
|
}
|
|
|
|
#if _MSC_VER || defined(__MINGW32__)
|
|
|
|
if (!commons_done)
|
|
|
|
#endif
|
|
|
|
{
|
2011-05-23 16:19:47 +01:00
|
|
|
LOCAL_FileNameBuf[len] = '\0';
|
|
|
|
strncat(LOCAL_FileNameBuf, "PrologCommons", YAP_FILENAME_MAX);
|
2009-02-16 12:25:03 +00:00
|
|
|
Yap_PutValue(AtomPrologCommonsDir,
|
2011-05-23 16:19:47 +01:00
|
|
|
MkAtomTerm(Yap_LookupAtom(LOCAL_FileNameBuf)));
|
2009-02-16 12:25:03 +00:00
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static Int
|
2011-03-07 16:02:55 +00:00
|
|
|
p_dir_sp ( USES_REGS1 )
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
|
|
|
#ifdef MAC
|
|
|
|
Term t = MkIntTerm(':');
|
2011-03-22 11:07:17 +00:00
|
|
|
Term t2 = MkIntTerm('/');
|
2001-04-09 20:54:03 +01:00
|
|
|
#elif ATARI || _MSC_VER || defined(__MINGW32__)
|
|
|
|
Term t = MkIntTerm('\\');
|
2011-03-22 11:07:17 +00:00
|
|
|
Term t2 = MkIntTerm('/');
|
2001-04-09 20:54:03 +01:00
|
|
|
#else
|
|
|
|
Term t = MkIntTerm('/');
|
2011-03-22 11:07:17 +00:00
|
|
|
Term t2 = MkIntTerm('/');
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif
|
|
|
|
|
2011-03-22 11:07:17 +00:00
|
|
|
return Yap_unify_constant(ARG1,t) || Yap_unify_constant(ARG1,t2) ;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-05-25 16:40:36 +01:00
|
|
|
void
|
|
|
|
Yap_InitPageSize(void)
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
SYSTEM_INFO si;
|
|
|
|
GetSystemInfo(&si);
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_page_size = si.dwPageSize;
|
2001-04-09 20:54:03 +01:00
|
|
|
#elif HAVE_UNISTD_H
|
2010-10-27 10:11:19 +01:00
|
|
|
#if defined(__FreeBSD__) || defined(__DragonFly__)
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_page_size = getpagesize();
|
2001-04-09 20:54:03 +01:00
|
|
|
#elif defined(_AIX)
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_page_size = sysconf(_SC_PAGE_SIZE);
|
2001-04-09 20:54:03 +01:00
|
|
|
#elif !defined(_SC_PAGESIZE)
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_page_size = getpagesize();
|
2001-04-09 20:54:03 +01:00
|
|
|
#else
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_page_size = sysconf(_SC_PAGESIZE);
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif
|
|
|
|
#else
|
|
|
|
bla bla
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef SIMICS
|
|
|
|
#ifdef HAVE_GETRUSAGE
|
|
|
|
#undef HAVE_GETRUSAGE
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_TIMES
|
|
|
|
#undef HAVE_TIMES
|
|
|
|
#endif
|
|
|
|
#endif /* SIMICS */
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
#if HAVE_GETRUSAGE
|
|
|
|
#undef HAVE_GETRUSAGE
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if HAVE_GETRUSAGE
|
|
|
|
|
|
|
|
#if HAVE_SYS_TIMES_H
|
|
|
|
#include <sys/times.h>
|
|
|
|
#endif
|
|
|
|
#if HAVE_SYS_RESOURCE_H
|
|
|
|
#include <sys/resource.h>
|
|
|
|
#endif
|
|
|
|
|
2004-03-02 16:44:58 +00:00
|
|
|
#if THREADS
|
2011-05-09 19:55:06 +01:00
|
|
|
#define StartOfTimes (*(LOCAL_ThreadHandle.start_of_timesp))
|
|
|
|
#define last_time (*(LOCAL_ThreadHandle.last_timep))
|
2008-02-12 21:50:58 +00:00
|
|
|
|
2012-12-11 22:07:10 +00:00
|
|
|
#define StartOfTimes_sys (*(LOCAL_ThreadHandle.start_of_times_sysp))
|
|
|
|
#define last_time_sys (*(LOCAL_ThreadHandle.last_time_sysp))
|
|
|
|
|
2004-03-02 16:44:58 +00:00
|
|
|
#else
|
2001-04-09 20:54:03 +01:00
|
|
|
/* since the point YAP was started */
|
|
|
|
static struct timeval StartOfTimes;
|
|
|
|
|
|
|
|
/* since last call to runtime */
|
|
|
|
static struct timeval last_time;
|
2012-12-11 22:07:10 +00:00
|
|
|
|
|
|
|
/* same for system time */
|
2009-02-09 21:56:40 +00:00
|
|
|
static struct timeval last_time_sys;
|
|
|
|
static struct timeval StartOfTimes_sys;
|
2012-12-11 22:07:10 +00:00
|
|
|
#endif
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
/* store user time in this variable */
|
|
|
|
static void
|
2012-12-11 22:07:10 +00:00
|
|
|
InitTime (int wid)
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
|
|
|
struct rusage rusage;
|
|
|
|
|
2004-03-02 16:44:58 +00:00
|
|
|
#if THREADS
|
2012-12-11 22:07:10 +00:00
|
|
|
REMOTE_ThreadHandle(wid).start_of_timesp = (struct timeval *)malloc(sizeof(struct timeval));
|
|
|
|
REMOTE_ThreadHandle(wid).last_timep = (struct timeval *)malloc(sizeof(struct timeval));
|
|
|
|
REMOTE_ThreadHandle(wid).start_of_times_sysp = (struct timeval *)malloc(sizeof(struct timeval));
|
|
|
|
REMOTE_ThreadHandle(wid).last_time_sysp = (struct timeval *)malloc(sizeof(struct timeval));
|
|
|
|
getrusage(RUSAGE_SELF, &rusage);
|
|
|
|
(*REMOTE_ThreadHandle(wid).last_timep).tv_sec =
|
|
|
|
(*REMOTE_ThreadHandle(wid).start_of_timesp).tv_sec =
|
|
|
|
rusage.ru_utime.tv_sec;
|
|
|
|
(*REMOTE_ThreadHandle(wid).last_timep).tv_usec =
|
|
|
|
(*REMOTE_ThreadHandle(wid).start_of_timesp).tv_usec =
|
|
|
|
rusage.ru_utime.tv_usec;
|
|
|
|
(*REMOTE_ThreadHandle(wid).last_time_sysp).tv_sec =
|
|
|
|
(*REMOTE_ThreadHandle(wid).start_of_times_sysp).tv_sec =
|
|
|
|
rusage.ru_stime.tv_sec;
|
|
|
|
(*REMOTE_ThreadHandle(wid).last_time_sysp).tv_usec =
|
|
|
|
(*REMOTE_ThreadHandle(wid).start_of_times_sysp).tv_usec =
|
|
|
|
rusage.ru_stime.tv_usec;
|
|
|
|
#else
|
2001-04-09 20:54:03 +01:00
|
|
|
getrusage(RUSAGE_SELF, &rusage);
|
2012-12-11 22:07:10 +00:00
|
|
|
last_time.tv_sec =
|
2012-12-11 22:15:02 +00:00
|
|
|
StartOfTimes.tv_sec =
|
2012-12-11 22:07:10 +00:00
|
|
|
rusage.ru_utime.tv_sec;
|
|
|
|
last_time.tv_usec =
|
2012-12-11 22:15:02 +00:00
|
|
|
StartOfTimes.tv_usec =
|
2012-12-11 22:07:10 +00:00
|
|
|
rusage.ru_utime.tv_usec;
|
|
|
|
last_time_sys.tv_sec =
|
2012-12-11 22:15:02 +00:00
|
|
|
StartOfTimes_sys.tv_sec =
|
2012-12-11 22:07:10 +00:00
|
|
|
rusage.ru_stime.tv_sec;
|
|
|
|
last_time_sys.tv_usec =
|
2012-12-11 22:15:02 +00:00
|
|
|
StartOfTimes_sys.tv_usec =
|
2012-12-11 22:07:10 +00:00
|
|
|
rusage.ru_stime.tv_usec;
|
|
|
|
#endif
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-03-02 16:44:58 +00:00
|
|
|
UInt
|
2011-03-07 16:02:55 +00:00
|
|
|
Yap_cputime ( void )
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
2011-03-07 16:02:55 +00:00
|
|
|
CACHE_REGS
|
2001-04-09 20:54:03 +01:00
|
|
|
struct rusage rusage;
|
|
|
|
|
|
|
|
getrusage(RUSAGE_SELF, &rusage);
|
|
|
|
return((rusage.ru_utime.tv_sec - StartOfTimes.tv_sec)) * 1000 +
|
|
|
|
((rusage.ru_utime.tv_usec - StartOfTimes.tv_usec) / 1000);
|
|
|
|
}
|
|
|
|
|
2002-11-18 18:18:05 +00:00
|
|
|
void Yap_cputime_interval(Int *now,Int *interval)
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
2011-03-07 16:02:55 +00:00
|
|
|
CACHE_REGS
|
2001-04-09 20:54:03 +01:00
|
|
|
struct rusage rusage;
|
|
|
|
|
|
|
|
getrusage(RUSAGE_SELF, &rusage);
|
|
|
|
*now = (rusage.ru_utime.tv_sec - StartOfTimes.tv_sec) * 1000 +
|
|
|
|
(rusage.ru_utime.tv_usec - StartOfTimes.tv_usec) / 1000;
|
|
|
|
*interval = (rusage.ru_utime.tv_sec - last_time.tv_sec) * 1000 +
|
|
|
|
(rusage.ru_utime.tv_usec - last_time.tv_usec) / 1000;
|
|
|
|
last_time.tv_usec = rusage.ru_utime.tv_usec;
|
|
|
|
last_time.tv_sec = rusage.ru_utime.tv_sec;
|
|
|
|
}
|
|
|
|
|
2009-02-09 21:56:40 +00:00
|
|
|
void Yap_systime_interval(Int *now,Int *interval)
|
|
|
|
{
|
2012-12-11 22:07:10 +00:00
|
|
|
CACHE_REGS
|
2009-02-09 21:56:40 +00:00
|
|
|
struct rusage rusage;
|
|
|
|
|
|
|
|
getrusage(RUSAGE_SELF, &rusage);
|
|
|
|
*now = (rusage.ru_stime.tv_sec - StartOfTimes_sys.tv_sec) * 1000 +
|
|
|
|
(rusage.ru_stime.tv_usec - StartOfTimes_sys.tv_usec) / 1000;
|
|
|
|
*interval = (rusage.ru_stime.tv_sec - last_time_sys.tv_sec) * 1000 +
|
|
|
|
(rusage.ru_stime.tv_usec - last_time_sys.tv_usec) / 1000;
|
|
|
|
last_time_sys.tv_usec = rusage.ru_stime.tv_usec;
|
|
|
|
last_time_sys.tv_sec = rusage.ru_stime.tv_sec;
|
|
|
|
}
|
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
#elif defined(_WIN32)
|
|
|
|
|
2001-04-26 18:53:37 +01:00
|
|
|
#ifdef __GNUC__
|
|
|
|
|
|
|
|
/* This is stolen from the Linux kernel.
|
|
|
|
The problem is that mingw32 does not seem to have acces to div */
|
|
|
|
#ifndef do_div
|
|
|
|
#define do_div(n,base) ({ \
|
|
|
|
unsigned long __upper, __low, __high, __mod; \
|
|
|
|
asm("":"=a" (__low), "=d" (__high):"A" (n)); \
|
|
|
|
__upper = __high; \
|
|
|
|
if (__high) { \
|
|
|
|
__upper = __high % (base); \
|
|
|
|
__high = __high / (base); \
|
|
|
|
} \
|
|
|
|
asm("divl %2":"=a" (__low), "=d" (__mod):"rm" (base), "0" (__low), "1" (__upper)); \
|
|
|
|
asm("":"=A" (n):"a" (__low),"d" (__high)); \
|
|
|
|
__mod; \
|
|
|
|
})
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
#include <time.h>
|
|
|
|
|
2001-04-26 18:53:37 +01:00
|
|
|
static FILETIME StartOfTimes, last_time;
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2009-02-09 21:56:40 +00:00
|
|
|
static FILETIME StartOfTimes_sys, last_time_sys;
|
|
|
|
|
2002-02-04 16:12:54 +00:00
|
|
|
static clock_t TimesStartOfTimes, Times_last_time;
|
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
/* store user time in this variable */
|
|
|
|
static void
|
2012-12-19 00:48:43 +00:00
|
|
|
InitTime (int wid)
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
2001-04-26 18:53:37 +01:00
|
|
|
HANDLE hProcess = GetCurrentProcess();
|
|
|
|
FILETIME CreationTime, ExitTime, KernelTime, UserTime;
|
2002-02-04 16:12:54 +00:00
|
|
|
if (!GetProcessTimes(hProcess, &CreationTime, &ExitTime, &KernelTime, &UserTime)) {
|
|
|
|
/* WIN98 */
|
|
|
|
clock_t t;
|
|
|
|
t = clock ();
|
|
|
|
Times_last_time = TimesStartOfTimes = t;
|
|
|
|
} else {
|
2012-12-19 00:48:43 +00:00
|
|
|
#if THREADS
|
2012-12-20 21:13:20 +00:00
|
|
|
REMOTE_ThreadHandle(wid).start_of_timesp = (struct _FILETIME *)malloc(sizeof(FILETIME));
|
|
|
|
REMOTE_ThreadHandle(wid).last_timep = (struct _FILETIME *)malloc(sizeof(FILETIME));
|
|
|
|
REMOTE_ThreadHandle(wid).start_of_times_sysp = (struct _FILETIME *)malloc(sizeof(FILETIME));
|
|
|
|
REMOTE_ThreadHandle(wid).last_time_sysp = (struct _FILETIME *)malloc(sizeof(FILETIME));
|
2012-12-11 22:07:10 +00:00
|
|
|
(*REMOTE_ThreadHandle(wid).last_timep).dwLowDateTime =
|
2012-12-20 21:13:20 +00:00
|
|
|
UserTime.dwLowDateTime;
|
2012-12-11 22:07:10 +00:00
|
|
|
(*REMOTE_ThreadHandle(wid).last_timep).dwHighDateTime =
|
|
|
|
UserTime.dwHighDateTime;
|
|
|
|
(*REMOTE_ThreadHandle(wid).start_of_timesp).dwLowDateTime =
|
|
|
|
UserTime.dwLowDateTime;
|
|
|
|
(*REMOTE_ThreadHandle(wid).start_of_timesp).dwHighDateTime =
|
|
|
|
UserTime.dwHighDateTime;
|
|
|
|
(*REMOTE_ThreadHandle(wid).last_time_sysp).dwLowDateTime =
|
|
|
|
KernelTime.dwLowDateTime;
|
|
|
|
(*REMOTE_ThreadHandle(wid).last_time_sysp).dwHighDateTime =
|
|
|
|
KernelTime.dwHighDateTime;
|
2012-12-19 00:48:43 +00:00
|
|
|
(*REMOTE_ThreadHandle(wid).start_of_times_sysp).dwLowDateTime =
|
2012-12-11 22:07:10 +00:00
|
|
|
KernelTime.dwLowDateTime;
|
|
|
|
(*REMOTE_ThreadHandle(wid).start_of_times_sysp).dwHighDateTime =
|
|
|
|
KernelTime.dwHighDateTime;
|
2012-12-19 00:48:43 +00:00
|
|
|
#else
|
|
|
|
last_time.dwLowDateTime =
|
|
|
|
UserTime.dwLowDateTime;
|
|
|
|
last_time.dwHighDateTime =
|
|
|
|
UserTime.dwHighDateTime;
|
|
|
|
StartOfTimes.dwLowDateTime =
|
|
|
|
UserTime.dwLowDateTime;
|
|
|
|
StartOfTimes.dwHighDateTime =
|
|
|
|
UserTime.dwHighDateTime;
|
|
|
|
last_time_sys.dwLowDateTime =
|
|
|
|
KernelTime.dwLowDateTime;
|
|
|
|
last_time_sys.dwHighDateTime =
|
|
|
|
KernelTime.dwHighDateTime;
|
|
|
|
StartOfTimes_sys.dwLowDateTime =
|
|
|
|
KernelTime.dwLowDateTime;
|
|
|
|
StartOfTimes_sys.dwHighDateTime =
|
|
|
|
KernelTime.dwHighDateTime;
|
|
|
|
#endif
|
2002-02-04 16:12:54 +00:00
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
2007-05-26 09:19:33 +01:00
|
|
|
#ifdef __GNUC__
|
|
|
|
static unsigned long long int
|
|
|
|
sub_utime(FILETIME t1, FILETIME t2)
|
|
|
|
{
|
|
|
|
ULARGE_INTEGER u[2];
|
|
|
|
memcpy((void *)u,(void *)&t1,sizeof(FILETIME));
|
|
|
|
memcpy((void *)(u+1),(void *)&t2,sizeof(FILETIME));
|
|
|
|
return
|
|
|
|
u[0].QuadPart - u[1].QuadPart;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2004-03-02 16:44:58 +00:00
|
|
|
UInt
|
2012-06-29 21:37:11 +01:00
|
|
|
Yap_cputime ( void )
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
2001-04-26 18:53:37 +01:00
|
|
|
HANDLE hProcess = GetCurrentProcess();
|
|
|
|
FILETIME CreationTime, ExitTime, KernelTime, UserTime;
|
2002-02-04 16:12:54 +00:00
|
|
|
if (!GetProcessTimes(hProcess, &CreationTime, &ExitTime, &KernelTime, &UserTime)) {
|
|
|
|
clock_t t;
|
|
|
|
t = clock ();
|
|
|
|
return(((t - TimesStartOfTimes)*1000) / CLOCKS_PER_SEC);
|
|
|
|
} else {
|
2001-04-26 18:53:37 +01:00
|
|
|
#ifdef __GNUC__
|
|
|
|
unsigned long long int t =
|
2007-05-26 09:19:33 +01:00
|
|
|
sub_utime(UserTime,StartOfTimes);
|
2002-01-17 17:16:22 +00:00
|
|
|
do_div(t,10000);
|
|
|
|
return((Int)t);
|
2001-04-26 18:53:37 +01:00
|
|
|
#endif
|
|
|
|
#ifdef _MSC_VER
|
2002-02-04 16:12:54 +00:00
|
|
|
__int64 t = *(__int64 *)&UserTime - *(__int64 *)&StartOfTimes;
|
2001-04-26 18:53:37 +01:00
|
|
|
return((Int)(t/10000));
|
|
|
|
#endif
|
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
2002-11-18 18:18:05 +00:00
|
|
|
void Yap_cputime_interval(Int *now,Int *interval)
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
2001-04-26 18:53:37 +01:00
|
|
|
HANDLE hProcess = GetCurrentProcess();
|
|
|
|
FILETIME CreationTime, ExitTime, KernelTime, UserTime;
|
2002-02-04 16:12:54 +00:00
|
|
|
if (!GetProcessTimes(hProcess, &CreationTime, &ExitTime, &KernelTime, &UserTime)) {
|
|
|
|
clock_t t;
|
|
|
|
t = clock ();
|
|
|
|
*now = ((t - TimesStartOfTimes)*1000) / CLOCKS_PER_SEC;
|
|
|
|
*interval = (t - Times_last_time) * 1000 / CLOCKS_PER_SEC;
|
|
|
|
Times_last_time = t;
|
|
|
|
} else {
|
2001-04-26 18:53:37 +01:00
|
|
|
#ifdef __GNUC__
|
|
|
|
unsigned long long int t1 =
|
2007-05-26 09:19:33 +01:00
|
|
|
sub_utime(UserTime, StartOfTimes);
|
2001-04-26 18:53:37 +01:00
|
|
|
unsigned long long int t2 =
|
2007-05-26 09:19:33 +01:00
|
|
|
sub_utime(UserTime, last_time);
|
2002-01-17 17:16:22 +00:00
|
|
|
do_div(t1,10000);
|
|
|
|
*now = (Int)t1;
|
|
|
|
do_div(t2,10000);
|
|
|
|
*interval = (Int)t2;
|
2001-04-26 18:53:37 +01:00
|
|
|
#endif
|
|
|
|
#ifdef _MSC_VER
|
2002-02-04 16:12:54 +00:00
|
|
|
__int64 t1 = *(__int64 *)&UserTime - *(__int64 *)&StartOfTimes;
|
|
|
|
__int64 t2 = *(__int64 *)&UserTime - *(__int64 *)&last_time;
|
2001-04-26 18:53:37 +01:00
|
|
|
*now = (Int)(t1/10000);
|
|
|
|
*interval = (Int)(t2/10000);
|
|
|
|
#endif
|
|
|
|
last_time.dwLowDateTime = UserTime.dwLowDateTime;
|
|
|
|
last_time.dwHighDateTime = UserTime.dwHighDateTime;
|
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
2009-02-09 21:56:40 +00:00
|
|
|
void Yap_systime_interval(Int *now,Int *interval)
|
|
|
|
{
|
|
|
|
HANDLE hProcess = GetCurrentProcess();
|
|
|
|
FILETIME CreationTime, ExitTime, KernelTime, UserTime;
|
|
|
|
if (!GetProcessTimes(hProcess, &CreationTime, &ExitTime, &KernelTime, &UserTime)) {
|
|
|
|
*now = *interval = 0; /* not available */
|
|
|
|
} else {
|
|
|
|
#ifdef __GNUC__
|
|
|
|
unsigned long long int t1 =
|
|
|
|
sub_utime(KernelTime, StartOfTimes_sys);
|
|
|
|
unsigned long long int t2 =
|
|
|
|
sub_utime(KernelTime, last_time_sys);
|
|
|
|
do_div(t1,10000);
|
|
|
|
*now = (Int)t1;
|
|
|
|
do_div(t2,10000);
|
|
|
|
*interval = (Int)t2;
|
|
|
|
#endif
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
__int64 t1 = *(__int64 *)&KernelTime - *(__int64 *)&StartOfTimes_sys;
|
|
|
|
__int64 t2 = *(__int64 *)&KernelTime - *(__int64 *)&last_time_sys;
|
|
|
|
*now = (Int)(t1/10000);
|
|
|
|
*interval = (Int)(t2/10000);
|
|
|
|
#endif
|
|
|
|
last_time_sys.dwLowDateTime = KernelTime.dwLowDateTime;
|
|
|
|
last_time_sys.dwHighDateTime = KernelTime.dwHighDateTime;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
#elif HAVE_TIMES
|
|
|
|
|
|
|
|
#if defined(_WIN32)
|
|
|
|
|
|
|
|
#include <time.h>
|
|
|
|
|
|
|
|
#define TicksPerSec CLOCKS_PER_SEC
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
#if HAVE_SYS_TIMES_H
|
|
|
|
#include <sys/times.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2001-09-24 19:07:16 +01:00
|
|
|
#if defined(__sun__) && (defined(__svr4__) || defined(__SVR4))
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
#if HAVE_LIMITS_H
|
|
|
|
#include <limits.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define TicksPerSec CLK_TCK
|
|
|
|
#endif
|
|
|
|
|
2010-10-27 10:11:19 +01:00
|
|
|
#if defined(__alpha) || defined(__FreeBSD__) || defined(__linux__) || defined(__DragonFly__)
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
#if HAVE_TIME_H
|
|
|
|
#include <time.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define TicksPerSec sysconf(_SC_CLK_TCK)
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if !TMS_IN_SYS_TIME
|
|
|
|
#if HAVE_SYS_TIMES_H
|
|
|
|
#include <sys/times.h>
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static clock_t StartOfTimes, last_time;
|
|
|
|
|
2009-02-09 21:56:40 +00:00
|
|
|
static clock_t StartOfTimes_sys, last_time_sys;
|
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
/* store user time in this variable */
|
|
|
|
static void
|
|
|
|
InitTime (void)
|
|
|
|
{
|
|
|
|
struct tms t;
|
|
|
|
times (&t);
|
2012-12-11 22:07:10 +00:00
|
|
|
(*REMOTE_ThreadHandle(wid).last_timep) = StartOfTimes = t.tms_utime;
|
2009-02-09 21:56:40 +00:00
|
|
|
last_time_sys = StartOfTimes_sys = t.tms_stime;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
2004-03-02 16:44:58 +00:00
|
|
|
UInt
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_cputime (void)
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
|
|
|
struct tms t;
|
|
|
|
times(&t);
|
|
|
|
return((t.tms_utime - StartOfTimes)*1000 / TicksPerSec);
|
|
|
|
}
|
|
|
|
|
2002-11-18 18:18:05 +00:00
|
|
|
void Yap_cputime_interval(Int *now,Int *interval)
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
|
|
|
struct tms t;
|
|
|
|
times (&t);
|
|
|
|
*now = ((t.tms_utime - StartOfTimes)*1000) / TicksPerSec;
|
|
|
|
*interval = (t.tms_utime - last_time) * 1000 / TicksPerSec;
|
2001-04-26 18:53:37 +01:00
|
|
|
last_time = t.tms_utime;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
2009-02-09 21:56:40 +00:00
|
|
|
void Yap_systime_interval(Int *now,Int *interval)
|
|
|
|
{
|
|
|
|
struct tms t;
|
|
|
|
times (&t);
|
|
|
|
*now = ((t.tms_stime - StartOfTimes_sys)*1000) / TicksPerSec;
|
|
|
|
*interval = (t.tms_stime - last_time_sys) * 1000 / TicksPerSec;
|
|
|
|
last_time_sys = t.tms_stime;
|
|
|
|
}
|
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
#else /* HAVE_TIMES */
|
|
|
|
|
|
|
|
#ifdef SIMICS
|
|
|
|
|
|
|
|
#include <sys/time.h>
|
|
|
|
|
|
|
|
/* since the point YAP was started */
|
|
|
|
static struct timeval StartOfTimes;
|
|
|
|
|
|
|
|
/* since last call to runtime */
|
|
|
|
static struct timeval last_time;
|
|
|
|
|
|
|
|
/* store user time in this variable */
|
|
|
|
static void
|
2012-12-11 22:07:10 +00:00
|
|
|
InitTime (int wid)
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
|
|
|
struct timeval tp;
|
|
|
|
|
|
|
|
gettimeofday(&tp,NULL);
|
2012-12-11 22:07:10 +00:00
|
|
|
(*REMOTE_ThreadHandle(wid).last_timep).tv_sec = (*REMOTE_ThreadHandle.start_of_timesp(wid)).tv_sec = tp.tv_sec;
|
|
|
|
(*REMOTE_ThreadHandle(wid).last_timep).tv_usec = (*REMOTE_ThreadHandle.start_of_timesp(wid)).tv_usec = tp.tv_usec;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-03-02 16:44:58 +00:00
|
|
|
UInt
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_cputime (void)
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
|
|
|
struct timeval tp;
|
|
|
|
|
|
|
|
gettimeofday(&tp,NULL);
|
|
|
|
if (StartOfTimes.tv_usec > tp.tv_usec)
|
|
|
|
return((tp.tv_sec - StartOfTimes.tv_sec - 1) * 1000 +
|
|
|
|
(StartOfTimes.tv_usec - tp.tv_usec) /1000);
|
|
|
|
else
|
|
|
|
return((tp.tv_sec - StartOfTimes.tv_sec)) * 1000 +
|
|
|
|
((tp.tv_usec - StartOfTimes.tv_usec) / 1000);
|
|
|
|
}
|
|
|
|
|
2002-11-18 18:18:05 +00:00
|
|
|
void Yap_cputime_interval(Int *now,Int *interval)
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
|
|
|
struct timeval tp;
|
|
|
|
|
|
|
|
gettimeofday(&tp,NULL);
|
|
|
|
*now = (tp.tv_sec - StartOfTimes.tv_sec) * 1000 +
|
|
|
|
(tp.tv_usec - StartOfTimes.tv_usec) / 1000;
|
|
|
|
*interval = (tp.tv_sec - last_time.tv_sec) * 1000 +
|
|
|
|
(tp.tv_usec - last_time.tv_usec) / 1000;
|
|
|
|
last_time.tv_usec = tp.tv_usec;
|
|
|
|
last_time.tv_sec = tp.tv_sec;
|
|
|
|
}
|
|
|
|
|
2009-02-09 21:56:40 +00:00
|
|
|
void Yap_systime_interval(Int *now,Int *interval)
|
|
|
|
{
|
|
|
|
*now = *interval = 0; /* not available */
|
|
|
|
}
|
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif /* SIMICS */
|
|
|
|
|
|
|
|
#ifdef COMMENTED_OUT
|
|
|
|
/* This code is not working properly. I left it here to help future ports */
|
|
|
|
#ifdef MPW
|
|
|
|
|
|
|
|
#include <files.h>
|
|
|
|
#include <Events.h>
|
|
|
|
|
|
|
|
#define TicksPerSec 60.0
|
|
|
|
|
|
|
|
static double
|
|
|
|
real_cputime ()
|
|
|
|
{
|
|
|
|
return (((double) TickCount ()) / TicksPerSec);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* MPW */
|
|
|
|
|
|
|
|
#ifdef LATTICE
|
|
|
|
|
|
|
|
#include "osbind.h"
|
|
|
|
|
|
|
|
static long *ptime;
|
|
|
|
|
|
|
|
gettime ()
|
|
|
|
{
|
|
|
|
*ptime = *(long *) 0x462;
|
|
|
|
}
|
|
|
|
|
|
|
|
static double
|
|
|
|
real_cputime ()
|
|
|
|
{
|
|
|
|
long thetime;
|
|
|
|
ptime = &thetime;
|
|
|
|
xbios (38, gettime);
|
|
|
|
return (((double) thetime) / (Getrez () == 2 ? 70 : 60));
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* LATTICE */
|
|
|
|
|
|
|
|
#ifdef M_WILLIAMS
|
|
|
|
|
|
|
|
#include <osbind.h>
|
|
|
|
#include <xbios.h>
|
|
|
|
|
|
|
|
static long *ptime;
|
|
|
|
|
|
|
|
static long
|
|
|
|
readtime ()
|
|
|
|
{
|
|
|
|
return (*((long *) 0x4ba));
|
|
|
|
}
|
|
|
|
|
|
|
|
static double
|
|
|
|
real_cputime ()
|
|
|
|
{
|
|
|
|
long time;
|
|
|
|
|
|
|
|
time = Supexec (readtime);
|
|
|
|
return (time / 200.0);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* M_WILLIAMS */
|
|
|
|
|
|
|
|
#ifdef LIGHT
|
|
|
|
|
|
|
|
#undef FALSE
|
|
|
|
#undef TRUE
|
|
|
|
|
|
|
|
#include <FileMgr.h>
|
|
|
|
|
|
|
|
#define TicksPerSec 60.0
|
|
|
|
|
|
|
|
static double
|
|
|
|
real_cputime ()
|
|
|
|
{
|
|
|
|
return (((double) TickCount ()) / TicksPerSec);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* LIGHT */
|
|
|
|
|
|
|
|
#endif /* COMMENTED_OUT */
|
|
|
|
|
|
|
|
#endif /* HAVE_GETRUSAGE */
|
|
|
|
|
|
|
|
#if HAVE_GETHRTIME
|
2002-06-13 19:24:04 +01:00
|
|
|
|
|
|
|
#if HAVE_TIME_H
|
|
|
|
#include <time.h>
|
|
|
|
#endif
|
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
/* since the point YAP was started */
|
|
|
|
static hrtime_t StartOfWTimes;
|
|
|
|
|
|
|
|
/* since last call to walltime */
|
|
|
|
#define LastWtime (*(hrtime_t *)ALIGN_YAPTYPE(LastWtimePtr,hrtime_t))
|
|
|
|
|
|
|
|
static void
|
|
|
|
InitWTime (void)
|
|
|
|
{
|
|
|
|
StartOfWTimes = gethrtime();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
InitLastWtime(void) {
|
|
|
|
/* ask for twice the space in order to guarantee alignment */
|
2002-11-18 18:18:05 +00:00
|
|
|
LastWtimePtr = (void *)Yap_AllocCodeSpace(2*sizeof(hrtime_t));
|
2001-04-09 20:54:03 +01:00
|
|
|
LastWtime = StartOfWTimes;
|
|
|
|
}
|
|
|
|
|
|
|
|
Int
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_walltime (void)
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
|
|
|
hrtime_t tp = gethrtime();
|
|
|
|
/* return time in milliseconds */
|
|
|
|
return((Int)((tp-StartOfWTimes)/((hrtime_t)1000000)));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2002-11-18 18:18:05 +00:00
|
|
|
void Yap_walltime_interval(Int *now,Int *interval)
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
|
|
|
hrtime_t tp = gethrtime();
|
|
|
|
/* return time in milliseconds */
|
|
|
|
*now = (Int)((tp-StartOfWTimes)/((hrtime_t)1000000));
|
|
|
|
*interval = (Int)((tp-LastWtime)/((hrtime_t)1000000));
|
|
|
|
LastWtime = tp;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#elif HAVE_GETTIMEOFDAY
|
|
|
|
|
|
|
|
/* since the point YAP was started */
|
|
|
|
static struct timeval StartOfWTimes;
|
|
|
|
|
|
|
|
/* since last call to walltime */
|
|
|
|
#define LastWtime (*(struct timeval *)LastWtimePtr)
|
|
|
|
|
|
|
|
/* store user time in this variable */
|
|
|
|
static void
|
|
|
|
InitWTime (void)
|
|
|
|
{
|
|
|
|
gettimeofday(&StartOfWTimes,NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
InitLastWtime(void) {
|
2002-11-18 18:18:05 +00:00
|
|
|
LastWtimePtr = (void *)Yap_AllocCodeSpace(sizeof(struct timeval));
|
2001-04-09 20:54:03 +01:00
|
|
|
LastWtime.tv_usec = StartOfWTimes.tv_usec;
|
|
|
|
LastWtime.tv_sec = StartOfWTimes.tv_sec;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Int
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_walltime (void)
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
|
|
|
struct timeval tp;
|
|
|
|
|
|
|
|
gettimeofday(&tp,NULL);
|
|
|
|
if (StartOfWTimes.tv_usec > tp.tv_usec)
|
|
|
|
return((tp.tv_sec - StartOfWTimes.tv_sec - 1) * 1000 +
|
|
|
|
(StartOfWTimes.tv_usec - tp.tv_usec) /1000);
|
|
|
|
else
|
|
|
|
return((tp.tv_sec - StartOfWTimes.tv_sec)) * 1000 +
|
|
|
|
((tp.tv_usec - LastWtime.tv_usec) / 1000);
|
|
|
|
}
|
|
|
|
|
2002-11-18 18:18:05 +00:00
|
|
|
void Yap_walltime_interval(Int *now,Int *interval)
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
|
|
|
struct timeval tp;
|
|
|
|
|
|
|
|
gettimeofday(&tp,NULL);
|
|
|
|
*now = (tp.tv_sec - StartOfWTimes.tv_sec) * 1000 +
|
|
|
|
(tp.tv_usec - StartOfWTimes.tv_usec) / 1000;
|
|
|
|
*interval = (tp.tv_sec - LastWtime.tv_sec) * 1000 +
|
|
|
|
(tp.tv_usec - LastWtime.tv_usec) / 1000;
|
|
|
|
LastWtime.tv_usec = tp.tv_usec;
|
|
|
|
LastWtime.tv_sec = tp.tv_sec;
|
|
|
|
}
|
|
|
|
|
|
|
|
#elif defined(_WIN32)
|
|
|
|
|
|
|
|
#include <sys/timeb.h>
|
|
|
|
#include <time.h>
|
|
|
|
|
|
|
|
/* since the point YAP was started */
|
2001-06-22 18:53:36 +01:00
|
|
|
static struct _timeb StartOfWTimes;
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
/* since last call to walltime */
|
|
|
|
#define LastWtime (*(struct timeb *)LastWtimePtr)
|
|
|
|
|
|
|
|
/* store user time in this variable */
|
|
|
|
static void
|
|
|
|
InitWTime (void)
|
|
|
|
{
|
2001-06-22 18:53:36 +01:00
|
|
|
_ftime(&StartOfWTimes);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
InitLastWtime(void) {
|
2002-11-18 18:18:05 +00:00
|
|
|
LastWtimePtr = (void *)Yap_AllocCodeSpace(sizeof(struct timeb));
|
2001-04-09 20:54:03 +01:00
|
|
|
LastWtime.time = StartOfWTimes.time;
|
|
|
|
LastWtime.millitm = StartOfWTimes.millitm;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Int
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_walltime (void)
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
2001-06-22 18:53:36 +01:00
|
|
|
struct _timeb tp;
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2001-06-22 18:53:36 +01:00
|
|
|
_ftime(&tp);
|
2001-04-09 20:54:03 +01:00
|
|
|
if (StartOfWTimes.millitm > tp.millitm)
|
|
|
|
return((tp.time - StartOfWTimes.time - 1) * 1000 +
|
|
|
|
(StartOfWTimes.millitm - tp.millitm));
|
|
|
|
else
|
|
|
|
return((tp.time - StartOfWTimes.time)) * 1000 +
|
|
|
|
((tp.millitm - LastWtime.millitm) / 1000);
|
|
|
|
}
|
|
|
|
|
2002-11-18 18:18:05 +00:00
|
|
|
void Yap_walltime_interval(Int *now,Int *interval)
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
2001-06-22 18:53:36 +01:00
|
|
|
struct _timeb tp;
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2001-06-22 18:53:36 +01:00
|
|
|
_ftime(&tp);
|
2001-04-09 20:54:03 +01:00
|
|
|
*now = (tp.time - StartOfWTimes.time) * 1000 +
|
|
|
|
(tp.millitm - StartOfWTimes.millitm);
|
|
|
|
*interval = (tp.time - LastWtime.time) * 1000 +
|
|
|
|
(tp.millitm - LastWtime.millitm) ;
|
|
|
|
LastWtime.millitm = tp.millitm;
|
|
|
|
LastWtime.time = tp.time;
|
|
|
|
}
|
|
|
|
|
|
|
|
#elif HAVE_TIMES
|
|
|
|
|
|
|
|
static clock_t StartOfWTimes;
|
|
|
|
|
|
|
|
#define LastWtime (*(clock_t *)LastWtimePtr)
|
|
|
|
|
|
|
|
/* store user time in this variable */
|
|
|
|
static void
|
|
|
|
InitWTime (void)
|
|
|
|
{
|
|
|
|
StartOfWTimes = times(NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
InitLastWtime(void) {
|
2002-11-18 18:18:05 +00:00
|
|
|
LastWtimePtr = (void *)Yap_AllocCodeSpace(sizeof(clock_t));
|
2001-04-09 20:54:03 +01:00
|
|
|
LastWtime = StartOfWTimes;
|
|
|
|
}
|
|
|
|
|
|
|
|
Int
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_walltime (void)
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
|
|
|
clock_t t;
|
|
|
|
t = times(NULL);
|
|
|
|
return ((t - StartOfWTimes)*1000 / TicksPerSec));
|
|
|
|
}
|
|
|
|
|
2002-11-18 18:18:05 +00:00
|
|
|
void Yap_walltime_interval(Int *now,Int *interval)
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
|
|
|
clock_t t;
|
|
|
|
t = times(NULL);
|
|
|
|
*now = ((t - StartOfWTimes)*1000) / TicksPerSec;
|
|
|
|
*interval = (t - LastWtime) * 1000 / TicksPerSec;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* HAVE_TIMES */
|
|
|
|
|
|
|
|
#if HAVE_TIME_H
|
|
|
|
#include <time.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
unsigned int current_seed;
|
|
|
|
|
|
|
|
static void
|
|
|
|
InitRandom (void)
|
|
|
|
{
|
|
|
|
current_seed = (unsigned int) time (NULL);
|
|
|
|
#if HAVE_RANDOM
|
|
|
|
srandom (current_seed);
|
|
|
|
#elif HAVE_RAND
|
|
|
|
srand (current_seed);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2013-04-25 23:15:04 +01:00
|
|
|
extern int rand(void);
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
|
|
|
|
double
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_random (void)
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
|
|
|
#if HAVE_RANDOM
|
|
|
|
/* extern long random (); */
|
|
|
|
return (((double) random ()) / 0x7fffffffL /* 2**31-1 */);
|
|
|
|
#elif HAVE_RAND
|
|
|
|
return (((double) (rand ()) / RAND_MAX));
|
|
|
|
#else
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(SYSTEM_ERROR, TermNil,
|
2001-04-09 20:54:03 +01:00
|
|
|
"random not available in this configuration");
|
|
|
|
return (0.0);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static Int
|
2011-03-07 16:02:55 +00:00
|
|
|
p_srandom ( USES_REGS1 )
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
|
|
|
register Term t0 = Deref (ARG1);
|
|
|
|
if (IsVarTerm (t0)) {
|
2002-11-18 18:18:05 +00:00
|
|
|
return(Yap_unify(ARG1,MkIntegerTerm((Int)current_seed)));
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
if(!IsNumTerm (t0))
|
|
|
|
return (FALSE);
|
|
|
|
if (IsIntTerm (t0))
|
|
|
|
current_seed = (unsigned int) IntOfTerm (t0);
|
|
|
|
else if (IsFloatTerm (t0))
|
|
|
|
current_seed = (unsigned int) FloatOfTerm (t0);
|
|
|
|
else
|
|
|
|
current_seed = (unsigned int) LongIntOfTerm (t0);
|
|
|
|
#if HAVE_RANDOM
|
|
|
|
srandom(current_seed);
|
|
|
|
#elif HAVE_RAND
|
|
|
|
srand(current_seed);
|
2002-08-19 00:18:18 +01:00
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif
|
|
|
|
return (TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
#if HAVE_SIGNAL
|
|
|
|
|
|
|
|
#include <signal.h>
|
|
|
|
|
|
|
|
#ifdef MPW
|
|
|
|
#define signal sigset
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef MSH
|
|
|
|
|
|
|
|
#define SIGFPE SIGDIV
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2013-04-25 23:15:04 +01:00
|
|
|
static void InitSignals(void);
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2010-06-17 08:20:34 +01:00
|
|
|
#define PLSIG_PREPARED 0x01 /* signal is prepared */
|
|
|
|
#define PLSIG_THROW 0x02 /* throw signal(num, name) */
|
|
|
|
#define PLSIG_SYNC 0x04 /* call synchronously */
|
|
|
|
#define PLSIG_NOFRAME 0x08 /* Do not create a Prolog frame */
|
|
|
|
|
|
|
|
#define SIG_PROLOG_OFFSET 32 /* Start of Prolog signals */
|
|
|
|
|
|
|
|
#define SIG_EXCEPTION (SIG_PROLOG_OFFSET+0)
|
|
|
|
#ifdef O_ATOMGC
|
|
|
|
#define SIG_ATOM_GC (SIG_PROLOG_OFFSET+1)
|
|
|
|
#endif
|
|
|
|
#define SIG_GC (SIG_PROLOG_OFFSET+2)
|
|
|
|
#ifdef O_PLMT
|
|
|
|
#define SIG_THREAD_SIGNAL (SIG_PROLOG_OFFSET+3)
|
|
|
|
#endif
|
|
|
|
#define SIG_FREECLAUSES (SIG_PROLOG_OFFSET+4)
|
|
|
|
#define SIG_PLABORT (SIG_PROLOG_OFFSET+5)
|
|
|
|
|
|
|
|
static struct signame
|
|
|
|
{ int sig;
|
|
|
|
const char *name;
|
|
|
|
int flags;
|
|
|
|
} signames[] =
|
|
|
|
{
|
|
|
|
#ifdef SIGHUP
|
|
|
|
{ SIGHUP, "hup", 0},
|
|
|
|
#endif
|
|
|
|
{ SIGINT, "int", 0},
|
|
|
|
#ifdef SIGQUIT
|
|
|
|
{ SIGQUIT, "quit", 0},
|
|
|
|
#endif
|
|
|
|
{ SIGILL, "ill", 0},
|
|
|
|
{ SIGABRT, "abrt", 0},
|
|
|
|
{ SIGFPE, "fpe", PLSIG_THROW},
|
|
|
|
#ifdef SIGKILL
|
|
|
|
{ SIGKILL, "kill", 0},
|
|
|
|
#endif
|
|
|
|
{ SIGSEGV, "segv", 0},
|
|
|
|
#ifdef SIGPIPE
|
|
|
|
{ SIGPIPE, "pipe", 0},
|
|
|
|
#endif
|
|
|
|
#ifdef SIGALRM
|
|
|
|
{ SIGALRM, "alrm", PLSIG_THROW},
|
|
|
|
#endif
|
|
|
|
{ SIGTERM, "term", 0},
|
|
|
|
#ifdef SIGUSR1
|
|
|
|
{ SIGUSR1, "usr1", 0},
|
|
|
|
#endif
|
|
|
|
#ifdef SIGUSR2
|
|
|
|
{ SIGUSR2, "usr2", 0},
|
|
|
|
#endif
|
|
|
|
#ifdef SIGCHLD
|
|
|
|
{ SIGCHLD, "chld", 0},
|
|
|
|
#endif
|
|
|
|
#ifdef SIGCONT
|
|
|
|
{ SIGCONT, "cont", 0},
|
|
|
|
#endif
|
|
|
|
#ifdef SIGSTOP
|
|
|
|
{ SIGSTOP, "stop", 0},
|
|
|
|
#endif
|
|
|
|
#ifdef SIGTSTP
|
|
|
|
{ SIGTSTP, "tstp", 0},
|
|
|
|
#endif
|
|
|
|
#ifdef SIGTTIN
|
|
|
|
{ SIGTTIN, "ttin", 0},
|
|
|
|
#endif
|
|
|
|
#ifdef SIGTTOU
|
|
|
|
{ SIGTTOU, "ttou", 0},
|
|
|
|
#endif
|
|
|
|
#ifdef SIGTRAP
|
|
|
|
{ SIGTRAP, "trap", 0},
|
|
|
|
#endif
|
|
|
|
#ifdef SIGBUS
|
|
|
|
{ SIGBUS, "bus", 0},
|
|
|
|
#endif
|
|
|
|
#ifdef SIGSTKFLT
|
|
|
|
{ SIGSTKFLT, "stkflt", 0},
|
|
|
|
#endif
|
|
|
|
#ifdef SIGURG
|
|
|
|
{ SIGURG, "urg", 0},
|
|
|
|
#endif
|
|
|
|
#ifdef SIGIO
|
|
|
|
{ SIGIO, "io", 0},
|
|
|
|
#endif
|
|
|
|
#ifdef SIGPOLL
|
|
|
|
{ SIGPOLL, "poll", 0},
|
|
|
|
#endif
|
|
|
|
#ifdef SIGXCPU
|
|
|
|
{ SIGXCPU, "xcpu", PLSIG_THROW},
|
|
|
|
#endif
|
|
|
|
#ifdef SIGXFSZ
|
|
|
|
{ SIGXFSZ, "xfsz", PLSIG_THROW},
|
|
|
|
#endif
|
|
|
|
#ifdef SIGVTALRM
|
|
|
|
{ SIGVTALRM, "vtalrm", PLSIG_THROW},
|
|
|
|
#endif
|
|
|
|
#ifdef SIGPROF
|
|
|
|
{ SIGPROF, "prof", 0},
|
|
|
|
#endif
|
|
|
|
#ifdef SIGPWR
|
|
|
|
{ SIGPWR, "pwr", 0},
|
|
|
|
#endif
|
|
|
|
{ SIG_EXCEPTION, "prolog:exception", 0 },
|
|
|
|
#ifdef SIG_ATOM_GC
|
|
|
|
{ SIG_ATOM_GC, "prolog:atom_gc", 0 },
|
|
|
|
#endif
|
|
|
|
{ SIG_GC, "prolog:gc", 0 },
|
|
|
|
#ifdef SIG_THREAD_SIGNAL
|
|
|
|
{ SIG_THREAD_SIGNAL, "prolog:thread_signal", 0 },
|
|
|
|
#endif
|
|
|
|
|
|
|
|
{ -1, NULL, 0}
|
|
|
|
};
|
|
|
|
|
|
|
|
/* SWI emulation */
|
|
|
|
int
|
|
|
|
Yap_signal_index(const char *name)
|
|
|
|
{ struct signame *sn = signames;
|
|
|
|
char tmp[12];
|
|
|
|
|
|
|
|
if ( strncmp(name, "SIG", 3) == 0 && strlen(name) < 12 )
|
|
|
|
{ char *p = (char *)name+3, *q = tmp;
|
|
|
|
while ((*q++ = tolower(*p++))) {};
|
|
|
|
name = tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
for( ; sn->name; sn++ )
|
|
|
|
{ if ( !strcmp(sn->name, name) )
|
|
|
|
return sn->sig;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2001-09-24 19:07:16 +01:00
|
|
|
#if (defined(__svr4__) || defined(__SVR4))
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
#if HAVE_SIGINFO_H
|
|
|
|
#include <siginfo.h>
|
|
|
|
#endif
|
|
|
|
#if HAVE_SYS_UCONTEXT_H
|
|
|
|
#include <sys/ucontext.h>
|
|
|
|
#endif
|
|
|
|
|
2013-04-25 23:15:04 +01:00
|
|
|
static void HandleSIGSEGV(int, siginfo_t *, ucontext_t *);
|
|
|
|
static void HandleMatherr, (int, siginfo_t *, ucontext_t *);
|
|
|
|
static void my_signal_info(int, void (*)(int, siginfo_t *, ucontext_t *));
|
|
|
|
static void my_signal(int, void (*)(int, siginfo_t *, ucontext_t *));
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
/* This routine believes there is a continuous space starting from the
|
|
|
|
HeapBase and ending on TrailTop */
|
|
|
|
static void
|
|
|
|
HandleSIGSEGV(int sig, siginfo_t *sip, ucontext_t *uap)
|
|
|
|
{
|
2004-02-05 16:57:02 +00:00
|
|
|
|
2004-06-05 04:37:01 +01:00
|
|
|
#if !USE_SYSTEM_MALLOC
|
2005-02-08 04:05:39 +00:00
|
|
|
if (
|
|
|
|
sip->si_code != SI_NOINFO &&
|
2001-04-09 20:54:03 +01:00
|
|
|
sip->si_code == SEGV_MAPERR &&
|
2002-11-18 18:18:05 +00:00
|
|
|
(void *)(sip->si_addr) > (void *)(Yap_HeapBase) &&
|
2011-05-23 16:19:47 +01:00
|
|
|
(void *)(sip->si_addr) < (void *)(LOCAL_TrailTop+K64)) {
|
2010-05-11 12:25:49 +01:00
|
|
|
Yap_growtrail(K64, TRUE);
|
2004-06-05 04:37:01 +01:00
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
Yap_Error(FATAL_ERROR, TermNil,
|
|
|
|
"likely bug in YAP, segmentation violation at %p", sip->si_addr);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
HandleMatherr(int sig, siginfo_t *sip, ucontext_t *uap)
|
|
|
|
{
|
2011-05-25 16:40:36 +01:00
|
|
|
CACHE_REGS
|
2001-04-09 20:54:03 +01:00
|
|
|
yap_error_number error_no;
|
|
|
|
|
|
|
|
/* reset the registers so that we don't have trash in abstract machine */
|
|
|
|
|
|
|
|
switch(sip->si_code) {
|
|
|
|
case FPE_INTDIV:
|
|
|
|
error_no = EVALUATION_ERROR_ZERO_DIVISOR;
|
|
|
|
break;
|
|
|
|
case FPE_INTOVF:
|
|
|
|
error_no = EVALUATION_ERROR_INT_OVERFLOW;
|
|
|
|
break;
|
|
|
|
case FPE_FLTDIV:
|
|
|
|
error_no = EVALUATION_ERROR_ZERO_DIVISOR;
|
|
|
|
break;
|
|
|
|
case FPE_FLTOVF:
|
|
|
|
error_no = EVALUATION_ERROR_FLOAT_OVERFLOW;
|
|
|
|
break;
|
|
|
|
case FPE_FLTUND:
|
|
|
|
error_no = EVALUATION_ERROR_FLOAT_UNDERFLOW;
|
|
|
|
break;
|
|
|
|
case FPE_FLTRES:
|
|
|
|
case FPE_FLTINV:
|
|
|
|
case FPE_FLTSUB:
|
|
|
|
default:
|
|
|
|
error_no = EVALUATION_ERROR_UNDEFINED;
|
|
|
|
}
|
2002-02-04 16:12:54 +00:00
|
|
|
set_fpu_exceptions(0);
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(error_no, TermNil, "");
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-05-20 07:53:14 +01:00
|
|
|
#if HAVE_SIGSEGV && !defined(THREADS)
|
2001-04-09 20:54:03 +01:00
|
|
|
static void
|
|
|
|
my_signal_info(int sig, void (*handler)(int, siginfo_t *, ucontext_t *))
|
|
|
|
{
|
|
|
|
struct sigaction sigact;
|
|
|
|
|
|
|
|
sigact.sa_handler = handler;
|
|
|
|
sigemptyset(&sigact.sa_mask);
|
2001-08-08 22:17:27 +01:00
|
|
|
sigact.sa_flags = SA_SIGINFO;
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
sigaction(sig,&sigact,NULL);
|
|
|
|
}
|
2009-05-20 07:53:14 +01:00
|
|
|
#endif
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
static void
|
|
|
|
my_signal(int sig, void (*handler)(int, siginfo_t *, ucontext_t *))
|
|
|
|
{
|
|
|
|
struct sigaction sigact;
|
|
|
|
|
|
|
|
sigact.sa_handler=handler;
|
|
|
|
sigemptyset(&sigact.sa_mask);
|
2001-08-08 22:17:27 +01:00
|
|
|
sigact.sa_flags = 0;
|
2001-04-09 20:54:03 +01:00
|
|
|
sigaction(sig,&sigact,NULL);
|
|
|
|
}
|
|
|
|
|
2005-02-08 04:05:39 +00:00
|
|
|
#elif defined(__linux__)
|
|
|
|
|
2013-04-25 23:15:04 +01:00
|
|
|
static RETSIGTYPE HandleMatherr(int);
|
2011-03-02 09:17:13 +00:00
|
|
|
#if HAVE_SIGSEGV && !defined(THREADS)
|
2013-04-25 23:15:04 +01:00
|
|
|
static RETSIGTYPE HandleSIGSEGV(int,siginfo_t *,void *);
|
|
|
|
static void my_signal_info(int, void (*)(int,siginfo_t *,void *));
|
2011-03-02 09:17:13 +00:00
|
|
|
#endif
|
2013-04-25 23:15:04 +01:00
|
|
|
static void my_signal(int, void (*)(int));
|
2005-02-08 04:05:39 +00:00
|
|
|
|
|
|
|
/******** Handling floating point errors *******************/
|
|
|
|
|
|
|
|
|
|
|
|
/* old code, used to work with matherror(), deprecated now:
|
|
|
|
char err_msg[256];
|
|
|
|
switch (x->type)
|
|
|
|
{
|
|
|
|
case DOMAIN:
|
|
|
|
case SING:
|
|
|
|
Yap_Error(EVALUATION_ERROR_UNDEFINED, TermNil, "%s", x->name);
|
|
|
|
return(0);
|
|
|
|
case OVERFLOW:
|
|
|
|
Yap_Error(EVALUATION_ERROR_FLOAT_OVERFLOW, TermNil, "%s", x->name);
|
|
|
|
return(0);
|
|
|
|
case UNDERFLOW:
|
|
|
|
Yap_Error(EVALUATION_ERROR_FLOAT_UNDERFLOW, TermNil, "%s", x->name);
|
|
|
|
return(0);
|
|
|
|
case PLOSS:
|
|
|
|
case TLOSS:
|
|
|
|
Yap_Error(EVALUATION_ERROR_UNDEFINED, TermNil, "%s(%g) = %g", x->name,
|
|
|
|
x->arg1, x->retval);
|
|
|
|
return(0);
|
|
|
|
default:
|
|
|
|
Yap_Error(EVALUATION_ERROR_UNDEFINED, TermNil, NULL);
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
static RETSIGTYPE
|
|
|
|
HandleMatherr(int sig)
|
|
|
|
{
|
2011-05-25 16:40:36 +01:00
|
|
|
CACHE_REGS
|
2005-02-08 04:05:39 +00:00
|
|
|
#if HAVE_FETESTEXCEPT
|
|
|
|
|
|
|
|
/* This should work in Linux, but it doesn't seem to. */
|
|
|
|
|
|
|
|
int raised = fetestexcept(FE_ALL_EXCEPT);
|
|
|
|
|
|
|
|
if (raised & FE_OVERFLOW) {
|
2011-05-25 16:40:36 +01:00
|
|
|
LOCAL_matherror = EVALUATION_ERROR_FLOAT_OVERFLOW;
|
2005-02-08 04:05:39 +00:00
|
|
|
} else if (raised & (FE_INVALID|FE_INEXACT)) {
|
2011-05-25 16:40:36 +01:00
|
|
|
LOCAL_matherror = EVALUATION_ERROR_UNDEFINED;
|
2005-02-08 04:05:39 +00:00
|
|
|
} else if (raised & FE_DIVBYZERO) {
|
2011-05-25 16:40:36 +01:00
|
|
|
LOCAL_matherror = EVALUATION_ERROR_ZERO_DIVISOR;
|
2005-02-08 04:05:39 +00:00
|
|
|
} else if (raised & FE_UNDERFLOW) {
|
2011-05-25 16:40:36 +01:00
|
|
|
LOCAL_matherror = EVALUATION_ERROR_FLOAT_UNDERFLOW;
|
2005-02-08 04:05:39 +00:00
|
|
|
} else
|
|
|
|
#endif
|
2011-05-25 16:40:36 +01:00
|
|
|
LOCAL_matherror = EVALUATION_ERROR_UNDEFINED;
|
2005-02-08 04:05:39 +00:00
|
|
|
/* something very bad happened on the way to the forum */
|
|
|
|
set_fpu_exceptions(FALSE);
|
2011-05-25 16:40:36 +01:00
|
|
|
Yap_Error(LOCAL_matherror , TermNil, "");
|
2005-02-08 04:05:39 +00:00
|
|
|
}
|
|
|
|
|
2011-03-02 09:17:13 +00:00
|
|
|
#if HAVE_SIGSEGV && !defined(THREADS)
|
|
|
|
static void
|
|
|
|
my_signal_info(int sig, void (*handler)(int,siginfo_t *,void *))
|
|
|
|
{
|
|
|
|
struct sigaction sigact;
|
|
|
|
|
|
|
|
sigact.sa_sigaction = handler;
|
|
|
|
sigemptyset(&sigact.sa_mask);
|
|
|
|
#if HAVE_SIGINFO
|
|
|
|
sigact.sa_flags = SA_SIGINFO;
|
|
|
|
#else
|
|
|
|
sigact.sa_flags = 0;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
sigaction(sig,&sigact,NULL);
|
|
|
|
}
|
|
|
|
|
2005-02-08 04:05:39 +00:00
|
|
|
static void
|
|
|
|
SearchForTrailFault(siginfo_t *siginfo)
|
|
|
|
{
|
|
|
|
void *ptr = siginfo->si_addr;
|
|
|
|
|
|
|
|
/* If the TRAIL is very close to the top of mmaped allocked space,
|
|
|
|
then we can try increasing the TR space and restarting the
|
|
|
|
instruction. In the worst case, the system will
|
|
|
|
crash again
|
|
|
|
*/
|
|
|
|
#if OS_HANDLES_TR_OVERFLOW && !USE_SYSTEM_MALLOC
|
2011-05-23 16:19:47 +01:00
|
|
|
if ((ptr > (void *)LOCAL_TrailTop-1024 &&
|
|
|
|
TR < (tr_fr_ptr) LOCAL_TrailTop+(64*1024))) {
|
2005-02-08 04:05:39 +00:00
|
|
|
if (!Yap_growtrail(64*1024, TRUE)) {
|
2010-05-11 12:25:49 +01:00
|
|
|
Yap_Error(OUT_OF_TRAIL_ERROR, TermNil, "YAP failed to reserve %ld bytes in growtrail", K64);
|
2005-02-08 04:05:39 +00:00
|
|
|
}
|
|
|
|
/* just in case, make sure the OS keeps the signal handler. */
|
|
|
|
/* my_signal_info(SIGSEGV, HandleSIGSEGV); */
|
|
|
|
} else
|
|
|
|
#endif /* OS_HANDLES_TR_OVERFLOW */
|
|
|
|
{
|
|
|
|
Yap_Error(FATAL_ERROR, TermNil,
|
2005-02-08 18:05:21 +00:00
|
|
|
"tried to access illegal address %p!!!!", ptr);
|
2005-02-08 04:05:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static RETSIGTYPE
|
|
|
|
HandleSIGSEGV(int sig, siginfo_t *siginfo, void *context)
|
|
|
|
{
|
2011-05-25 16:40:36 +01:00
|
|
|
if (LOCAL_PrologMode & ExtendStackMode) {
|
2011-05-23 16:19:47 +01:00
|
|
|
Yap_Error(FATAL_ERROR, TermNil, "OS memory allocation crashed at address %p, bailing out\n",LOCAL_TrailTop);
|
2005-02-08 04:05:39 +00:00
|
|
|
}
|
|
|
|
SearchForTrailFault(siginfo);
|
|
|
|
}
|
2009-05-20 07:53:14 +01:00
|
|
|
#endif
|
2005-02-08 04:05:39 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
my_signal(int sig, void (*handler)(int))
|
|
|
|
{
|
|
|
|
struct sigaction sigact;
|
|
|
|
|
|
|
|
sigact.sa_handler=handler;
|
|
|
|
sigemptyset(&sigact.sa_mask);
|
|
|
|
sigact.sa_flags = 0;
|
|
|
|
|
|
|
|
sigaction(sig,&sigact,NULL);
|
|
|
|
}
|
|
|
|
|
2002-01-09 17:19:36 +00:00
|
|
|
#else /* if not (defined(__svr4__) || defined(__SVR4)) */
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2013-04-25 23:15:04 +01:00
|
|
|
static RETSIGTYPE HandleMatherr(int);
|
|
|
|
static RETSIGTYPE HandleSIGSEGV(int);
|
|
|
|
static void my_signal_info(int, void (*)(int));
|
|
|
|
static void my_signal(int, void (*)(int));
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
/******** Handling floating point errors *******************/
|
|
|
|
|
|
|
|
|
|
|
|
/* old code, used to work with matherror(), deprecated now:
|
|
|
|
char err_msg[256];
|
|
|
|
switch (x->type)
|
|
|
|
{
|
|
|
|
case DOMAIN:
|
|
|
|
case SING:
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(EVALUATION_ERROR_UNDEFINED, TermNil, "%s", x->name);
|
2001-04-09 20:54:03 +01:00
|
|
|
return(0);
|
|
|
|
case OVERFLOW:
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(EVALUATION_ERROR_FLOAT_OVERFLOW, TermNil, "%s", x->name);
|
2001-04-09 20:54:03 +01:00
|
|
|
return(0);
|
|
|
|
case UNDERFLOW:
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(EVALUATION_ERROR_FLOAT_UNDERFLOW, TermNil, "%s", x->name);
|
2001-04-09 20:54:03 +01:00
|
|
|
return(0);
|
|
|
|
case PLOSS:
|
|
|
|
case TLOSS:
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(EVALUATION_ERROR_UNDEFINED, TermNil, "%s(%g) = %g", x->name,
|
2001-04-09 20:54:03 +01:00
|
|
|
x->arg1, x->retval);
|
|
|
|
return(0);
|
|
|
|
default:
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(EVALUATION_ERROR_UNDEFINED, TermNil, NULL);
|
2001-04-09 20:54:03 +01:00
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#if HAVE_FENV_H
|
|
|
|
#include <fenv.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static RETSIGTYPE
|
|
|
|
HandleMatherr(int sig)
|
|
|
|
{
|
2011-07-05 07:05:07 +01:00
|
|
|
CACHE_REGS
|
2012-12-20 21:13:20 +00:00
|
|
|
#if HAVE_FETESTEXCEPT
|
2001-04-09 20:54:03 +01:00
|
|
|
/* This should work in Linux, but it doesn't seem to. */
|
|
|
|
|
|
|
|
int raised = fetestexcept(FE_ALL_EXCEPT);
|
|
|
|
|
|
|
|
if (raised & FE_OVERFLOW) {
|
2011-05-25 16:40:36 +01:00
|
|
|
LOCAL_matherror = EVALUATION_ERROR_FLOAT_OVERFLOW;
|
2001-04-09 20:54:03 +01:00
|
|
|
} else if (raised & (FE_INVALID|FE_INEXACT)) {
|
2011-05-25 16:40:36 +01:00
|
|
|
LOCAL_matherror = EVALUATION_ERROR_UNDEFINED;
|
2001-04-09 20:54:03 +01:00
|
|
|
} else if (raised & FE_DIVBYZERO) {
|
2011-05-25 16:40:36 +01:00
|
|
|
LOCAL_matherror = EVALUATION_ERROR_ZERO_DIVISOR;
|
2001-04-09 20:54:03 +01:00
|
|
|
} else if (raised & FE_UNDERFLOW) {
|
2011-05-25 16:40:36 +01:00
|
|
|
LOCAL_matherror = EVALUATION_ERROR_FLOAT_UNDERFLOW;
|
2002-02-04 16:12:54 +00:00
|
|
|
} else
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif
|
2011-05-25 16:40:36 +01:00
|
|
|
LOCAL_matherror = EVALUATION_ERROR_UNDEFINED;
|
2001-04-09 20:54:03 +01:00
|
|
|
/* something very bad happened on the way to the forum */
|
2002-02-04 16:12:54 +00:00
|
|
|
set_fpu_exceptions(FALSE);
|
2011-05-25 16:40:36 +01:00
|
|
|
Yap_Error(LOCAL_matherror , TermNil, "");
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
SearchForTrailFault(void)
|
|
|
|
{
|
|
|
|
/* If the TRAIL is very close to the top of mmaped allocked space,
|
|
|
|
then we can try increasing the TR space and restarting the
|
|
|
|
instruction. In the worst case, the system will
|
|
|
|
crash again
|
|
|
|
*/
|
|
|
|
#ifdef DEBUG
|
|
|
|
/* fprintf(stderr,"Catching a sigsegv at %p with %p\n", TR, TrailTop); */
|
|
|
|
#endif
|
2004-02-05 16:57:02 +00:00
|
|
|
#if OS_HANDLES_TR_OVERFLOW && !USE_SYSTEM_MALLOC
|
2011-05-23 16:19:47 +01:00
|
|
|
if ((TR > (tr_fr_ptr)LOCAL_TrailTop-1024 &&
|
|
|
|
TR < (tr_fr_ptr)LOCAL_TrailTop+(64*1024))|| Yap_DBTrailOverflow()) {
|
2010-05-11 12:25:49 +01:00
|
|
|
long trsize = K64;
|
2004-11-18 22:32:40 +00:00
|
|
|
|
2011-05-23 16:19:47 +01:00
|
|
|
while ((CELL)TR > (CELL)LOCAL_TrailTop+trsize) {
|
2010-05-11 12:25:49 +01:00
|
|
|
trsize += K64;
|
2003-02-12 13:20:52 +00:00
|
|
|
}
|
2004-12-28 22:20:37 +00:00
|
|
|
if (!Yap_growtrail(trsize, TRUE)) {
|
2010-05-11 12:25:49 +01:00
|
|
|
Yap_Error(OUT_OF_TRAIL_ERROR, TermNil, "YAP failed to reserve %ld bytes in growtrail", K64);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
/* just in case, make sure the OS keeps the signal handler. */
|
|
|
|
/* my_signal_info(SIGSEGV, HandleSIGSEGV); */
|
|
|
|
} else
|
2002-10-21 23:52:36 +01:00
|
|
|
#endif /* OS_HANDLES_TR_OVERFLOW */
|
2005-03-01 22:25:09 +00:00
|
|
|
Yap_Error(INTERNAL_ERROR, TermNil,
|
2001-04-09 20:54:03 +01:00
|
|
|
"likely bug in YAP, segmentation violation");
|
|
|
|
}
|
|
|
|
|
|
|
|
static RETSIGTYPE
|
|
|
|
HandleSIGSEGV(int sig)
|
|
|
|
{
|
2011-07-05 07:05:07 +01:00
|
|
|
CACHE_REGS
|
2011-05-25 16:40:36 +01:00
|
|
|
if (LOCAL_PrologMode & ExtendStackMode) {
|
2011-05-23 16:19:47 +01:00
|
|
|
Yap_Error(FATAL_ERROR, TermNil, "OS memory allocation crashed at address %p, bailing out\n",LOCAL_TrailTop);
|
2002-10-10 06:58:49 +01:00
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
SearchForTrailFault();
|
|
|
|
}
|
|
|
|
|
2002-01-11 15:54:17 +00:00
|
|
|
#if HAVE_SIGACTION
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
static void
|
|
|
|
my_signal_info(int sig, void (*handler)(int))
|
|
|
|
{
|
|
|
|
struct sigaction sigact;
|
|
|
|
|
|
|
|
sigact.sa_handler = handler;
|
|
|
|
sigemptyset(&sigact.sa_mask);
|
2002-05-28 17:23:53 +01:00
|
|
|
#if HAVE_SIGINFO
|
2002-05-26 05:03:56 +01:00
|
|
|
sigact.sa_flags = SA_SIGINFO;
|
2002-05-28 17:23:53 +01:00
|
|
|
#else
|
|
|
|
sigact.sa_flags = 0;
|
|
|
|
#endif
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
sigaction(sig,&sigact,NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
my_signal(int sig, void (*handler)(int))
|
|
|
|
{
|
|
|
|
struct sigaction sigact;
|
|
|
|
|
|
|
|
sigact.sa_handler=handler;
|
|
|
|
sigemptyset(&sigact.sa_mask);
|
2002-05-26 05:03:56 +01:00
|
|
|
sigact.sa_flags = 0;
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
sigaction(sig,&sigact,NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
static void
|
|
|
|
my_signal(int sig, void (*handler)(int))
|
|
|
|
{
|
|
|
|
signal(sig, handler);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
my_signal_info(sig, handler)
|
|
|
|
int sig;
|
|
|
|
void (*handler)(int);
|
|
|
|
{
|
|
|
|
if(signal(sig, handler) == SIG_ERR)
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
#endif /* __linux__ */
|
|
|
|
|
2002-01-09 17:19:36 +00:00
|
|
|
#endif /* (defined(__svr4__) || defined(__SVR4)) */
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2001-06-12 18:25:28 +01:00
|
|
|
|
2001-06-22 18:53:36 +01:00
|
|
|
static int
|
2002-02-04 16:12:54 +00:00
|
|
|
InteractSIGINT(int ch) {
|
2011-03-26 15:45:42 +00:00
|
|
|
CACHE_REGS
|
2001-06-22 18:53:36 +01:00
|
|
|
switch (ch) {
|
|
|
|
case 'a':
|
|
|
|
/* abort computation */
|
2012-03-03 21:46:07 +00:00
|
|
|
if (LOCAL_PrologMode & (GCMode|ConsoleGetcMode|CritMode)) {
|
2011-05-25 16:40:36 +01:00
|
|
|
LOCAL_PrologMode |= AbortMode;
|
2012-03-03 21:46:07 +00:00
|
|
|
return -1;
|
2002-02-12 18:24:21 +00:00
|
|
|
} else {
|
2009-04-25 17:00:39 +01:00
|
|
|
Yap_Error(PURE_ABORT, TermNil, "abort from console");
|
2002-02-12 18:24:21 +00:00
|
|
|
}
|
2011-05-25 16:40:36 +01:00
|
|
|
LOCAL_PrologMode &= ~AsyncIntMode;
|
2012-02-05 12:04:29 +00:00
|
|
|
Yap_RestartYap( 1 );
|
2004-10-08 18:20:03 +01:00
|
|
|
return -1;
|
2005-11-09 18:02:53 +00:00
|
|
|
case 'b':
|
|
|
|
/* continue */
|
|
|
|
Yap_signal (YAP_BREAK_SIGNAL);
|
|
|
|
return 1;
|
2001-06-22 18:53:36 +01:00
|
|
|
case 'c':
|
|
|
|
/* continue */
|
2004-10-08 18:20:03 +01:00
|
|
|
return 1;
|
2001-08-08 22:17:27 +01:00
|
|
|
case 'd':
|
2004-10-08 18:20:03 +01:00
|
|
|
Yap_signal (YAP_DEBUG_SIGNAL);
|
2001-08-08 22:17:27 +01:00
|
|
|
/* enter debug mode */
|
2004-10-08 18:20:03 +01:00
|
|
|
return 1;
|
2001-06-22 18:53:36 +01:00
|
|
|
case 'e':
|
|
|
|
/* exit */
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_exit(0);
|
2004-10-08 18:20:03 +01:00
|
|
|
return -1;
|
2010-04-29 10:29:06 +01:00
|
|
|
case 'g':
|
|
|
|
/* exit */
|
|
|
|
Yap_signal (YAP_STACK_DUMP_SIGNAL);
|
|
|
|
return -1;
|
2001-06-22 18:53:36 +01:00
|
|
|
case 't':
|
|
|
|
/* start tracing */
|
2004-10-08 18:20:03 +01:00
|
|
|
Yap_signal (YAP_TRACE_SIGNAL);
|
|
|
|
return 1;
|
2001-06-22 18:53:36 +01:00
|
|
|
#ifdef LOW_LEVEL_TRACER
|
|
|
|
case 'T':
|
|
|
|
toggle_low_level_trace();
|
2004-10-08 18:20:03 +01:00
|
|
|
return 1;
|
2001-06-22 18:53:36 +01:00
|
|
|
#endif
|
|
|
|
case 's':
|
|
|
|
/* show some statistics */
|
2004-10-08 18:20:03 +01:00
|
|
|
Yap_signal (YAP_STATISTICS_SIGNAL);
|
|
|
|
return 1;
|
2001-06-22 18:53:36 +01:00
|
|
|
case EOF:
|
|
|
|
return(0);
|
|
|
|
break;
|
|
|
|
case 'h':
|
|
|
|
case '?':
|
|
|
|
default:
|
|
|
|
/* show an helpful message */
|
2011-05-25 16:40:36 +01:00
|
|
|
fprintf(GLOBAL_stderr, "Please press one of:\n");
|
|
|
|
fprintf(GLOBAL_stderr, " a for abort\n c for continue\n d for debug\n");
|
|
|
|
fprintf(GLOBAL_stderr, " e for exit\n g for stack dump\n s for statistics\n t for trace\n");
|
|
|
|
fprintf(GLOBAL_stderr, " b for break\n");
|
2001-06-22 18:53:36 +01:00
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
This function talks to the user about a signal. We assume we are in
|
|
|
|
the context of the main Prolog thread (trivial in Unix, but hard in WIN32)
|
|
|
|
*/
|
2002-11-11 17:38:10 +00:00
|
|
|
static int
|
2001-04-09 20:54:03 +01:00
|
|
|
ProcessSIGINT(void)
|
|
|
|
{
|
2012-04-11 22:08:02 +01:00
|
|
|
CACHE_REGS
|
2001-06-22 18:53:36 +01:00
|
|
|
int ch, out;
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2012-03-03 21:46:07 +00:00
|
|
|
LOCAL_PrologMode |= AsyncIntMode;
|
2001-06-22 18:53:36 +01:00
|
|
|
do {
|
2002-11-18 18:18:05 +00:00
|
|
|
ch = Yap_GetCharForSIGINT();
|
2001-06-22 18:53:36 +01:00
|
|
|
} while (!(out = InteractSIGINT(ch)));
|
2012-03-03 21:46:07 +00:00
|
|
|
LOCAL_PrologMode &= ~AsyncIntMode;
|
|
|
|
LOCAL_PrologMode &= ~InterruptMode;
|
2001-06-22 18:53:36 +01:00
|
|
|
return(out);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
2010-02-10 09:03:03 +00:00
|
|
|
#if !_MSC_VER && !defined(__MINGW32__)
|
|
|
|
|
|
|
|
#if HAVE_SIGNAL
|
|
|
|
static int snoozing = FALSE;
|
|
|
|
#endif
|
|
|
|
|
2001-06-22 18:53:36 +01:00
|
|
|
/* This function is called from the signal handler to process signals.
|
|
|
|
We assume we are within the context of the signal handler, whatever
|
|
|
|
that might be
|
|
|
|
*/
|
2001-04-09 20:54:03 +01:00
|
|
|
static RETSIGTYPE
|
2001-09-24 19:07:16 +01:00
|
|
|
#if (defined(__svr4__) || defined(__SVR4))
|
2001-04-09 20:54:03 +01:00
|
|
|
HandleSIGINT (int sig, siginfo_t *x, ucontext_t *y)
|
|
|
|
#else
|
|
|
|
HandleSIGINT (int sig)
|
|
|
|
#endif
|
|
|
|
{
|
2011-03-07 16:02:55 +00:00
|
|
|
CACHE_REGS
|
2012-03-03 21:46:07 +00:00
|
|
|
/* fprintf(stderr,"mode = %x\n",LOCAL_PrologMode); */
|
2001-06-22 18:53:36 +01:00
|
|
|
my_signal(SIGINT, HandleSIGINT);
|
2002-10-23 21:55:37 +01:00
|
|
|
/* do this before we act */
|
2001-06-27 14:22:30 +01:00
|
|
|
#if HAVE_ISATTY
|
2011-05-25 16:40:36 +01:00
|
|
|
if (!isatty(0)) {
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(INTERRUPT_ERROR,MkIntTerm(SIGINT),NULL);
|
2009-05-20 07:53:14 +01:00
|
|
|
return;
|
2001-06-27 14:22:30 +01:00
|
|
|
}
|
2001-06-22 18:53:36 +01:00
|
|
|
#endif
|
2011-05-04 10:11:41 +01:00
|
|
|
if (LOCAL_InterruptsDisabled) {
|
2009-05-20 07:53:14 +01:00
|
|
|
return;
|
|
|
|
}
|
2012-03-03 21:46:07 +00:00
|
|
|
if (LOCAL_PrologMode & ConsoleGetcMode) {
|
2011-05-25 16:40:36 +01:00
|
|
|
LOCAL_PrologMode |= InterruptMode;
|
2012-03-03 21:46:07 +00:00
|
|
|
return;
|
2001-06-27 14:22:30 +01:00
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
#ifdef HAVE_SETBUF
|
|
|
|
/* make sure we are not waiting for the end of line */
|
2001-08-08 22:17:27 +01:00
|
|
|
YP_setbuf (stdin, NULL);
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif
|
2001-08-08 22:17:27 +01:00
|
|
|
if (snoozing) {
|
|
|
|
snoozing = FALSE;
|
|
|
|
return;
|
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
ProcessSIGINT();
|
|
|
|
}
|
2010-02-10 09:03:03 +00:00
|
|
|
#endif
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
#if !defined(_WIN32)
|
|
|
|
/* this routine is called if the system activated the alarm */
|
|
|
|
static RETSIGTYPE
|
2001-09-24 19:07:16 +01:00
|
|
|
#if (defined(__svr4__) || defined(__SVR4))
|
2001-04-09 20:54:03 +01:00
|
|
|
HandleALRM (int s, siginfo_t *x, ucontext_t *y)
|
|
|
|
#else
|
|
|
|
HandleALRM(int s)
|
|
|
|
#endif
|
|
|
|
{
|
2001-08-13 10:19:00 +01:00
|
|
|
my_signal (SIGALRM, HandleALRM);
|
2001-04-09 20:54:03 +01:00
|
|
|
/* force the system to creep */
|
2004-01-23 02:23:51 +00:00
|
|
|
Yap_signal (YAP_ALARM_SIGNAL);
|
2001-04-09 20:54:03 +01:00
|
|
|
/* now, say what is going on */
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_PutValue(AtomAlarm, MkAtomTerm(AtomTrue));
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2002-01-09 17:19:36 +00:00
|
|
|
|
2009-06-03 16:09:14 +01:00
|
|
|
#if !defined(_WIN32)
|
|
|
|
/* this routine is called if the system activated the alarm */
|
|
|
|
static RETSIGTYPE
|
|
|
|
#if (defined(__svr4__) || defined(__SVR4))
|
|
|
|
HandleVTALRM (int s, siginfo_t *x, ucontext_t *y)
|
|
|
|
#else
|
|
|
|
HandleVTALRM(int s)
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
my_signal (SIGVTALRM, HandleVTALRM);
|
|
|
|
/* force the system to creep */
|
|
|
|
Yap_signal (YAP_VTALARM_SIGNAL);
|
|
|
|
/* now, say what is going on */
|
|
|
|
Yap_PutValue(AtomAlarm, MkAtomTerm(AtomTrue));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
/*
|
2002-01-11 15:54:17 +00:00
|
|
|
* This function is called after a normal interrupt had been caught.
|
|
|
|
* It allows 6 possibilities: abort, continue, trace, debug, help, exit.
|
2001-04-09 20:54:03 +01:00
|
|
|
*/
|
|
|
|
|
2001-06-22 18:53:36 +01:00
|
|
|
#if !defined(LIGHT) && !_MSC_VER && !defined(__MINGW32__) && !defined(LIGHT)
|
2001-04-09 20:54:03 +01:00
|
|
|
static RETSIGTYPE
|
2001-09-24 19:07:16 +01:00
|
|
|
#if (defined(__svr4__) || defined(__SVR4))
|
2001-04-09 20:54:03 +01:00
|
|
|
ReceiveSignal (int s, siginfo_t *x, ucontext_t *y)
|
|
|
|
#else
|
|
|
|
ReceiveSignal (int s)
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
switch (s)
|
|
|
|
{
|
|
|
|
#ifndef MPW
|
|
|
|
case SIGFPE:
|
2002-02-04 16:12:54 +00:00
|
|
|
set_fpu_exceptions(FALSE);
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error (SYSTEM_ERROR, TermNil, "floating point exception ]");
|
2001-04-09 20:54:03 +01:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#if !defined(LIGHT) && !defined(_WIN32)
|
|
|
|
/* These signals are not handled by WIN32 and not the Macintosh */
|
|
|
|
case SIGQUIT:
|
|
|
|
case SIGKILL:
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(INTERRUPT_ERROR,MkIntTerm(s),NULL);
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif
|
2008-05-23 11:02:13 +01:00
|
|
|
#ifdef SIGUSR1
|
2002-01-11 15:54:17 +00:00
|
|
|
case SIGUSR1:
|
|
|
|
/* force the system to creep */
|
2004-01-23 02:23:51 +00:00
|
|
|
Yap_signal (YAP_USR1_SIGNAL);
|
2002-01-11 15:54:17 +00:00
|
|
|
break;
|
2002-01-15 15:44:57 +00:00
|
|
|
#endif /* defined(SIGUSR1) */
|
2008-05-23 11:02:13 +01:00
|
|
|
#ifdef SIGUSR2
|
2002-01-11 15:54:17 +00:00
|
|
|
case SIGUSR2:
|
|
|
|
/* force the system to creep */
|
2004-01-23 02:23:51 +00:00
|
|
|
Yap_signal (YAP_USR2_SIGNAL);
|
2002-01-11 15:54:17 +00:00
|
|
|
break;
|
2002-01-15 15:44:57 +00:00
|
|
|
#endif /* defined(SIGUSR2) */
|
2008-05-23 11:02:13 +01:00
|
|
|
#ifdef SIGPIPE
|
|
|
|
case SIGPIPE:
|
|
|
|
/* force the system to creep */
|
|
|
|
Yap_signal (YAP_PIPE_SIGNAL);
|
|
|
|
break;
|
|
|
|
#endif /* defined(SIGPIPE) */
|
|
|
|
#ifdef SIGHUP
|
2002-01-11 15:54:17 +00:00
|
|
|
case SIGHUP:
|
|
|
|
/* force the system to creep */
|
2013-03-04 02:29:02 +00:00
|
|
|
/* Just ignore SUGHUP Yap_signal (YAP_HUP_SIGNAL); */
|
2002-01-11 15:54:17 +00:00
|
|
|
break;
|
2002-01-15 15:44:57 +00:00
|
|
|
#endif /* defined(SIGHUP) */
|
2001-04-09 20:54:03 +01:00
|
|
|
default:
|
2011-05-25 16:40:36 +01:00
|
|
|
fprintf(GLOBAL_stderr, "\n[ Unexpected signal ]\n");
|
2002-01-11 15:54:17 +00:00
|
|
|
exit (EXIT_FAILURE);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
}
|
2001-06-22 18:53:36 +01:00
|
|
|
#endif
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2001-06-22 18:53:36 +01:00
|
|
|
#if (_MSC_VER || defined(__MINGW32__))
|
|
|
|
static BOOL WINAPI
|
|
|
|
MSCHandleSignal(DWORD dwCtrlType) {
|
2012-06-29 21:37:11 +01:00
|
|
|
CACHE_REGS
|
2011-05-04 10:11:41 +01:00
|
|
|
if (LOCAL_InterruptsDisabled) {
|
2009-05-20 07:53:14 +01:00
|
|
|
return FALSE;
|
2009-05-29 20:36:49 +01:00
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
switch(dwCtrlType) {
|
|
|
|
case CTRL_C_EVENT:
|
|
|
|
case CTRL_BREAK_EVENT:
|
2004-01-23 02:23:51 +00:00
|
|
|
Yap_signal(YAP_ALARM_SIGNAL);
|
2011-05-25 16:40:36 +01:00
|
|
|
LOCAL_PrologMode |= InterruptMode;
|
2001-06-22 18:53:36 +01:00
|
|
|
return(TRUE);
|
2001-04-09 20:54:03 +01:00
|
|
|
default:
|
2001-06-22 18:53:36 +01:00
|
|
|
return(FALSE);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* SIGINT can cause problems, if caught before full initialization */
|
|
|
|
static void
|
|
|
|
InitSignals (void)
|
|
|
|
{
|
2011-05-25 16:40:36 +01:00
|
|
|
if (GLOBAL_PrologShouldHandleInterrupts) {
|
2001-06-22 18:53:36 +01:00
|
|
|
#if !defined(LIGHT) && !_MSC_VER && !defined(__MINGW32__) && !defined(LIGHT)
|
2004-11-18 22:32:40 +00:00
|
|
|
my_signal (SIGQUIT, ReceiveSignal);
|
|
|
|
my_signal (SIGKILL, ReceiveSignal);
|
|
|
|
my_signal (SIGUSR1, ReceiveSignal);
|
|
|
|
my_signal (SIGUSR2, ReceiveSignal);
|
|
|
|
my_signal (SIGHUP, ReceiveSignal);
|
|
|
|
my_signal (SIGALRM, HandleALRM);
|
2009-06-03 16:09:14 +01:00
|
|
|
my_signal (SIGVTALRM, HandleVTALRM);
|
2001-06-22 18:53:36 +01:00
|
|
|
#endif
|
2008-05-23 11:02:13 +01:00
|
|
|
#ifdef SIGPIPE
|
|
|
|
my_signal (SIGPIPE, ReceiveSignal);
|
|
|
|
#endif
|
2001-04-09 20:54:03 +01:00
|
|
|
#if _MSC_VER || defined(__MINGW32__)
|
2004-11-18 22:32:40 +00:00
|
|
|
signal (SIGINT, SIG_IGN);
|
|
|
|
SetConsoleCtrlHandler(MSCHandleSignal,TRUE);
|
2001-04-09 20:54:03 +01:00
|
|
|
#else
|
2004-11-18 22:32:40 +00:00
|
|
|
my_signal (SIGINT, HandleSIGINT);
|
2001-06-22 18:53:36 +01:00
|
|
|
#endif
|
2001-04-09 20:54:03 +01:00
|
|
|
#ifndef MPW
|
2004-11-18 22:32:40 +00:00
|
|
|
my_signal (SIGFPE, HandleMatherr);
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif
|
2005-09-08 23:06:45 +01:00
|
|
|
#if HAVE_SIGSEGV && !defined(THREADS)
|
2004-11-18 22:32:40 +00:00
|
|
|
my_signal_info (SIGSEGV, HandleSIGSEGV);
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif
|
2011-03-30 16:39:09 +01:00
|
|
|
#ifdef YAPOR_COW
|
2004-11-18 22:32:40 +00:00
|
|
|
signal(SIGCHLD, SIG_IGN); /* avoid ghosts */
|
|
|
|
#endif
|
|
|
|
} else {
|
|
|
|
#if OS_HANDLES_TR_OVERFLOW
|
|
|
|
#if HAVE_SIGSEGV && !defined(THREADS)
|
|
|
|
my_signal_info (SIGSEGV, HandleSIGSEGV);
|
|
|
|
#endif
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif
|
2004-11-18 22:32:40 +00:00
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* HAVE_SIGNAL */
|
|
|
|
|
|
|
|
|
|
|
|
/* TrueFileName -> Finds the true name of a file */
|
|
|
|
|
|
|
|
#ifdef __MINGW32__
|
|
|
|
#include <ctype.h>
|
|
|
|
#endif
|
|
|
|
|
2002-11-11 17:38:10 +00:00
|
|
|
static int
|
2001-04-09 20:54:03 +01:00
|
|
|
volume_header(char *file)
|
|
|
|
{
|
|
|
|
#if _MSC_VER || defined(__MINGW32__)
|
|
|
|
char *ch = file;
|
|
|
|
int c;
|
|
|
|
|
|
|
|
while ((c = ch[0]) != '\0') {
|
|
|
|
if (isalnum(c)) ch++;
|
|
|
|
else return(c == ':');
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return(FALSE);
|
|
|
|
}
|
|
|
|
|
2002-11-11 17:38:10 +00:00
|
|
|
int
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_volume_header(char *file)
|
2002-11-11 17:38:10 +00:00
|
|
|
{
|
|
|
|
return volume_header(file);
|
|
|
|
}
|
|
|
|
|
2008-12-13 16:07:55 +00:00
|
|
|
|
|
|
|
int Yap_getcwd(const char *buf, int len)
|
|
|
|
{
|
2011-03-07 16:02:55 +00:00
|
|
|
CACHE_REGS
|
2008-12-13 16:07:55 +00:00
|
|
|
#if __simplescalar__
|
|
|
|
/* does not implement getcwd */
|
2011-05-25 16:40:36 +01:00
|
|
|
strncpy(Yap_buf,GLOBAL_pwd,len);
|
2008-12-13 16:07:55 +00:00
|
|
|
#elif HAVE_GETCWD
|
|
|
|
if (getcwd ((char *)buf, len) == NULL) {
|
|
|
|
#if HAVE_STRERROR
|
|
|
|
Yap_Error(OPERATING_SYSTEM_ERROR, ARG1, "%s in getcwd/1", strerror(errno));
|
|
|
|
#else
|
|
|
|
Yap_Error(OPERATING_SYSTEM_ERROR, ARG1, "error %d in getcwd/1", errno);
|
|
|
|
#endif
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
if (getwd (buf) == NULL) {
|
|
|
|
#if HAVE_STRERROR
|
|
|
|
Yap_Error(OPERATING_SYSTEM_ERROR, ARG1, "%s in getcwd/1", strerror(errno));
|
|
|
|
#else
|
|
|
|
Yap_Error(OPERATING_SYSTEM_ERROR, ARG1, "in getcwd/1");
|
|
|
|
#endif
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2008-03-12 15:37:34 +00:00
|
|
|
/******
|
|
|
|
TODO: rewrite to use wordexp
|
|
|
|
****/
|
2002-11-11 17:38:10 +00:00
|
|
|
static int
|
2007-09-27 23:40:22 +01:00
|
|
|
TrueFileName (char *source, char *root, char *result, int in_lib)
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
2011-03-07 16:02:55 +00:00
|
|
|
CACHE_REGS
|
2009-12-11 09:49:47 +00:00
|
|
|
char *work;
|
2008-03-12 15:37:34 +00:00
|
|
|
char ares1[YAP_FILENAME_MAX];
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
result[0] = '\0';
|
|
|
|
#if defined(__MINGW32__) || _MSC_VER
|
|
|
|
/* step 0: replace / by \ */
|
2009-11-23 18:42:28 +00:00
|
|
|
strncpy(ares1, source, YAP_FILENAME_MAX);
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
2009-11-23 18:42:28 +00:00
|
|
|
char *p = ares1, ch = p[0];
|
2001-04-09 20:54:03 +01:00
|
|
|
while (ch != '\0') {
|
|
|
|
if (ch == '/') p[0] = '\\';
|
|
|
|
p++;
|
|
|
|
ch = p[0];
|
|
|
|
}
|
|
|
|
}
|
2009-11-23 18:42:28 +00:00
|
|
|
source = ares1;
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif
|
|
|
|
/* step 1: eating home information */
|
2008-03-12 15:37:34 +00:00
|
|
|
if (source[0] == '~') {
|
|
|
|
if (dir_separator(source[1]) || source[1] == '\0')
|
|
|
|
{
|
|
|
|
char *s;
|
|
|
|
source++;
|
2001-04-09 20:54:03 +01:00
|
|
|
#if defined(_WIN32)
|
2008-03-12 15:37:34 +00:00
|
|
|
s = getenv("HOMEDRIVE");
|
|
|
|
if (s != NULL)
|
|
|
|
strncpy (result, getenv ("HOMEDRIVE"), YAP_FILENAME_MAX);
|
|
|
|
s = getenv("HOMEPATH");
|
|
|
|
if (s != NULL)
|
|
|
|
strncpy (result, s, YAP_FILENAME_MAX);
|
2001-04-09 20:54:03 +01:00
|
|
|
#else
|
2008-03-12 15:37:34 +00:00
|
|
|
s = getenv ("HOME");
|
|
|
|
if (s != NULL)
|
|
|
|
strncpy (result, s, YAP_FILENAME_MAX);
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif
|
2008-03-12 15:37:34 +00:00
|
|
|
} else {
|
2001-04-09 20:54:03 +01:00
|
|
|
#if HAVE_GETPWNAM
|
2008-03-12 15:37:34 +00:00
|
|
|
struct passwd *user_passwd;
|
2009-12-11 09:49:47 +00:00
|
|
|
char *res0 = result;
|
2008-03-12 15:37:34 +00:00
|
|
|
|
|
|
|
source++;
|
|
|
|
while (!dir_separator((*res0 = *source)) && *res0 != '\0')
|
|
|
|
res0++, source++;
|
|
|
|
*res0++ = '\0';
|
|
|
|
if ((user_passwd = getpwnam (result)) == NULL) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
strncpy (result, user_passwd->pw_dir, YAP_FILENAME_MAX);
|
|
|
|
#else
|
|
|
|
return FALSE;
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif
|
|
|
|
}
|
2008-03-12 15:37:34 +00:00
|
|
|
strncat (result, source, YAP_FILENAME_MAX);
|
|
|
|
} else if (source[0] == '$') {
|
|
|
|
/* follow SICStus expansion rules */
|
|
|
|
int ch;
|
|
|
|
char *s;
|
|
|
|
char *res0 = source+1;
|
|
|
|
|
|
|
|
while ((ch = *res0) && is_valid_env_char (ch)) {
|
|
|
|
res0++;
|
|
|
|
}
|
|
|
|
*res0 = '\0';
|
|
|
|
if (!(s = (char *) getenv (source+1))) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
*res0 = ch;
|
|
|
|
strncpy (result, s, YAP_FILENAME_MAX);
|
|
|
|
strncat (result, res0, YAP_FILENAME_MAX);
|
|
|
|
} else {
|
2001-04-09 20:54:03 +01:00
|
|
|
strncpy (result, source, YAP_FILENAME_MAX);
|
2008-03-12 15:37:34 +00:00
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
/* step 3: get the full file name */
|
2007-09-27 23:40:22 +01:00
|
|
|
if (!dir_separator(result[0]) && !volume_header(result)) {
|
2008-12-13 16:07:55 +00:00
|
|
|
if (!Yap_getcwd(ares1, YAP_FILENAME_MAX))
|
2008-03-12 15:37:34 +00:00
|
|
|
return FALSE;
|
|
|
|
#if _MSC_VER || defined(__MINGW32__)
|
2010-03-22 16:57:19 +00:00
|
|
|
strncat (ares1, "\\", YAP_FILENAME_MAX-1);
|
2008-03-12 15:37:34 +00:00
|
|
|
#else
|
2010-03-22 16:57:19 +00:00
|
|
|
strncat (ares1, "/", YAP_FILENAME_MAX-1);
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif
|
2008-03-12 15:37:34 +00:00
|
|
|
if (root) {
|
|
|
|
if (!dir_separator(root[0]) && !volume_header(root)) {
|
2008-11-26 09:56:18 +00:00
|
|
|
strncat(ares1, root, YAP_FILENAME_MAX-1);
|
2008-03-12 15:37:34 +00:00
|
|
|
} else {
|
2008-11-26 09:56:18 +00:00
|
|
|
strncpy(ares1, root, YAP_FILENAME_MAX-1);
|
2008-03-12 15:37:34 +00:00
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
#if _MSC_VER || defined(__MINGW32__)
|
2008-11-26 09:56:18 +00:00
|
|
|
strncat (ares1, "\\", YAP_FILENAME_MAX-1);
|
2001-04-09 20:54:03 +01:00
|
|
|
#else
|
2008-11-26 09:56:18 +00:00
|
|
|
strncat (ares1, "/", YAP_FILENAME_MAX-1);
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif
|
2008-03-12 15:37:34 +00:00
|
|
|
}
|
2008-11-26 09:56:18 +00:00
|
|
|
strncat (ares1, result, YAP_FILENAME_MAX-1);
|
2008-03-12 15:37:34 +00:00
|
|
|
if (in_lib) {
|
|
|
|
int tmpf;
|
|
|
|
if ((tmpf = open(ares1, O_RDONLY)) < 0) {
|
|
|
|
/* not in current directory, let us try the library */
|
|
|
|
if (Yap_LibDir != NULL) {
|
2011-05-23 16:19:47 +01:00
|
|
|
strncpy(LOCAL_FileNameBuf, Yap_LibDir, YAP_FILENAME_MAX);
|
2001-04-09 20:54:03 +01:00
|
|
|
#if HAVE_GETENV
|
2008-03-12 15:37:34 +00:00
|
|
|
} else {
|
|
|
|
char *yap_env = getenv("YAPLIBDIR");
|
|
|
|
if (yap_env != NULL) {
|
|
|
|
strncpy(ares1, yap_env, YAP_FILENAME_MAX);
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif
|
2008-03-12 15:37:34 +00:00
|
|
|
} else {
|
2006-08-30 02:06:30 +01:00
|
|
|
#if _MSC_VER || defined(__MINGW32__)
|
2008-03-12 15:37:34 +00:00
|
|
|
if (libdir)
|
|
|
|
strncpy(ares1, libdir, YAP_FILENAME_MAX);
|
|
|
|
else
|
2006-08-30 02:06:30 +01:00
|
|
|
#endif
|
2009-10-23 09:09:16 +01:00
|
|
|
strncpy(ares1, YAP_LIBDIR, YAP_FILENAME_MAX);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
2008-03-12 15:37:34 +00:00
|
|
|
#if HAVE_GETENV
|
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif
|
|
|
|
#if _MSC_VER || defined(__MINGW32__)
|
2008-11-26 09:56:18 +00:00
|
|
|
strncat(ares1,"\\", YAP_FILENAME_MAX-1);
|
2001-04-09 20:54:03 +01:00
|
|
|
#else
|
2008-11-26 09:56:18 +00:00
|
|
|
strncat(ares1,"/", YAP_FILENAME_MAX-1);
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif
|
2008-11-26 09:56:18 +00:00
|
|
|
strncat(ares1,result, YAP_FILENAME_MAX-1);
|
2008-03-12 15:37:34 +00:00
|
|
|
if ((tmpf = open(ares1, O_RDONLY)) >= 0) {
|
2001-04-09 20:54:03 +01:00
|
|
|
close(tmpf);
|
2008-03-12 15:37:34 +00:00
|
|
|
strncpy (result, ares1, YAP_FILENAME_MAX);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
strncpy (result, ares1, YAP_FILENAME_MAX);
|
2008-03-12 15:37:34 +00:00
|
|
|
close(tmpf);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
2008-03-12 15:37:34 +00:00
|
|
|
} else {
|
|
|
|
strncpy (result, ares1, YAP_FILENAME_MAX);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
2007-09-27 23:40:22 +01:00
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
/* step 4: simplifying the file name */
|
|
|
|
work = result;
|
|
|
|
while (*work != '\0')
|
|
|
|
{
|
|
|
|
char *new_work, *next_work;
|
|
|
|
if (*work++ != '.')
|
|
|
|
continue;
|
|
|
|
if (*work != '.')
|
|
|
|
{
|
|
|
|
if (!dir_separator(*work) || !dir_separator(work[-2]))
|
|
|
|
continue;
|
|
|
|
next_work = work + 1;
|
|
|
|
new_work = --work;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!dir_separator(work[1]) || !dir_separator(work[-2]))
|
|
|
|
continue;
|
|
|
|
next_work = work + 2;
|
|
|
|
work -= 2;
|
|
|
|
if (work == result)
|
|
|
|
return (FALSE);
|
|
|
|
while (!dir_separator(*--work) && work != result);
|
|
|
|
if (work == result && !dir_separator(work[0]))
|
|
|
|
return (FALSE);
|
|
|
|
new_work = ++work;
|
|
|
|
}
|
|
|
|
while ((*new_work++ = *next_work++)!=0);
|
|
|
|
}
|
2006-05-17 19:38:11 +01:00
|
|
|
if (work != result && dir_separator(work[-1])) {
|
|
|
|
/* should only do this on result being a directory */
|
|
|
|
int ch0 = work[-1];
|
|
|
|
work--;
|
|
|
|
work[0] = '\0';
|
|
|
|
if (!is_directory(result)) {
|
|
|
|
/* put it back: */
|
|
|
|
work[0] = ch0;
|
|
|
|
work++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return TRUE;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
2002-11-11 17:38:10 +00:00
|
|
|
int
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_TrueFileName (char *source, char *result, int in_lib)
|
2002-11-11 17:38:10 +00:00
|
|
|
{
|
2007-09-27 23:40:22 +01:00
|
|
|
return TrueFileName (source, NULL, result, in_lib);
|
2002-11-11 17:38:10 +00:00
|
|
|
}
|
|
|
|
|
2006-05-17 19:38:11 +01:00
|
|
|
static Int
|
2011-03-07 16:02:55 +00:00
|
|
|
p_true_file_name ( USES_REGS1 )
|
2006-05-17 19:38:11 +01:00
|
|
|
{
|
|
|
|
Term t = Deref(ARG1);
|
|
|
|
|
|
|
|
if (IsVarTerm(t)) {
|
|
|
|
Yap_Error(INSTANTIATION_ERROR,t,"argument to true_file_name unbound");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
if (!IsAtomTerm(t)) {
|
|
|
|
Yap_Error(TYPE_ERROR_ATOM,t,"argument to true_file_name");
|
|
|
|
return FALSE;
|
|
|
|
}
|
2011-05-23 16:19:47 +01:00
|
|
|
TrueFileName (RepAtom(AtomOfTerm(t))->StrOfAE, NULL, LOCAL_FileNameBuf, FALSE);
|
|
|
|
return Yap_unify(ARG2, MkAtomTerm(Yap_LookupAtom(LOCAL_FileNameBuf)));
|
2006-05-17 19:38:11 +01:00
|
|
|
}
|
|
|
|
|
2007-09-27 23:40:22 +01:00
|
|
|
static Int
|
2011-03-07 16:02:55 +00:00
|
|
|
p_true_file_name3 ( USES_REGS1 )
|
2007-09-27 23:40:22 +01:00
|
|
|
{
|
|
|
|
Term t = Deref(ARG1), t2 = Deref(ARG2);
|
|
|
|
char *root = NULL;
|
|
|
|
|
|
|
|
if (IsVarTerm(t)) {
|
|
|
|
Yap_Error(INSTANTIATION_ERROR,t,"argument to true_file_name unbound");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
if (!IsAtomTerm(t)) {
|
|
|
|
Yap_Error(TYPE_ERROR_ATOM,t,"argument to true_file_name");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
if (!IsVarTerm(t2)) {
|
|
|
|
if (!IsAtomTerm(t)) {
|
|
|
|
Yap_Error(TYPE_ERROR_ATOM,t2,"argument to true_file_name");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
root = RepAtom(AtomOfTerm(t2))->StrOfAE;
|
|
|
|
}
|
2011-05-23 16:19:47 +01:00
|
|
|
TrueFileName (RepAtom(AtomOfTerm(t))->StrOfAE, root, LOCAL_FileNameBuf, FALSE);
|
|
|
|
return Yap_unify(ARG3, MkAtomTerm(Yap_LookupAtom(LOCAL_FileNameBuf)));
|
2007-09-27 23:40:22 +01:00
|
|
|
}
|
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
/* Executes $SHELL under Prolog */
|
|
|
|
|
|
|
|
static Int
|
2011-03-07 16:02:55 +00:00
|
|
|
p_sh ( USES_REGS1 )
|
2001-04-09 20:54:03 +01:00
|
|
|
{ /* sh */
|
|
|
|
#ifdef HAVE_SYSTEM
|
2005-11-23 13:24:00 +00:00
|
|
|
char *shell;
|
2001-04-09 20:54:03 +01:00
|
|
|
shell = (char *) getenv ("SHELL");
|
|
|
|
if (shell == NULL)
|
|
|
|
shell = "/bin/sh";
|
|
|
|
if (system (shell) < 0) {
|
|
|
|
#if HAVE_STRERROR
|
2005-11-23 13:24:00 +00:00
|
|
|
Yap_Error(OPERATING_SYSTEM_ERROR, TermNil, "%s in sh/0", strerror(errno));
|
2001-04-09 20:54:03 +01:00
|
|
|
#else
|
2005-11-23 13:24:00 +00:00
|
|
|
Yap_Error(OPERATING_SYSTEM_ERROR, TermNil, "in sh/0");
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif
|
2005-11-23 13:24:00 +00:00
|
|
|
return FALSE;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
2005-11-23 13:24:00 +00:00
|
|
|
return TRUE;
|
2001-04-09 20:54:03 +01:00
|
|
|
#else
|
|
|
|
#ifdef MSH
|
|
|
|
register char *shell;
|
|
|
|
shell = "msh -i";
|
|
|
|
system (shell);
|
|
|
|
return (TRUE);
|
|
|
|
#else
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(SYSTEM_ERROR,TermNil,"sh not available in this configuration");
|
2001-04-09 20:54:03 +01:00
|
|
|
return(FALSE);
|
|
|
|
#endif /* MSH */
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static Int
|
2011-03-07 16:02:55 +00:00
|
|
|
p_shell ( USES_REGS1 )
|
2001-04-09 20:54:03 +01:00
|
|
|
{ /* '$shell'(+SystCommand) */
|
|
|
|
#if _MSC_VER || defined(__MINGW32__)
|
2005-11-23 13:24:00 +00:00
|
|
|
Yap_Error(SYSTEM_ERROR,TermNil,"shell not available in this configuration");
|
|
|
|
return FALSE;
|
2001-04-09 20:54:03 +01:00
|
|
|
#else
|
|
|
|
#if HAVE_SYSTEM
|
|
|
|
char *shell;
|
|
|
|
register int bourne = FALSE;
|
|
|
|
Term t1 = Deref (ARG1);
|
|
|
|
|
|
|
|
shell = (char *) getenv ("SHELL");
|
|
|
|
if (!strcmp (shell, "/bin/sh"))
|
|
|
|
bourne = TRUE;
|
|
|
|
if (shell == NIL)
|
|
|
|
bourne = TRUE;
|
2002-11-18 18:18:05 +00:00
|
|
|
/* Yap_CloseStreams(TRUE); */
|
2001-04-09 20:54:03 +01:00
|
|
|
if (bourne)
|
2006-04-25 04:23:40 +01:00
|
|
|
return system(RepAtom(AtomOfTerm(t1))->StrOfAE) == 0;
|
2005-11-23 13:24:00 +00:00
|
|
|
else {
|
2006-04-25 04:23:40 +01:00
|
|
|
int status = -1;
|
|
|
|
int child = fork ();
|
|
|
|
|
|
|
|
if (child == 0) { /* let the children go */
|
|
|
|
if (!execl (shell, shell, "-c", RepAtom(AtomOfTerm(t1))->StrOfAE , NULL)) {
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
exit(TRUE);
|
|
|
|
}
|
|
|
|
{ /* put the father on wait */
|
|
|
|
int result = child < 0 ||
|
2001-04-09 20:54:03 +01:00
|
|
|
/* vsc:I am not sure this is used, Stevens say wait returns an integer.
|
|
|
|
#if NO_UNION_WAIT
|
|
|
|
*/
|
|
|
|
wait ((&status)) != child ||
|
|
|
|
/*
|
|
|
|
#else
|
|
|
|
wait ((union wait *) (&status)) != child ||
|
|
|
|
#endif
|
|
|
|
*/
|
|
|
|
status == 0;
|
2005-11-23 13:24:00 +00:00
|
|
|
return result;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#else /* HAVE_SYSTEM */
|
|
|
|
#ifdef MSH
|
|
|
|
register char *shell;
|
|
|
|
shell = "msh -i";
|
2002-11-18 18:18:05 +00:00
|
|
|
/* Yap_CloseStreams(); */
|
2001-04-09 20:54:03 +01:00
|
|
|
system (shell);
|
2006-04-25 04:23:40 +01:00
|
|
|
return TRUE;
|
2001-04-09 20:54:03 +01:00
|
|
|
#else
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error (SYSTEM_ERROR,TermNil,"shell not available in this configuration");
|
2006-04-25 04:23:40 +01:00
|
|
|
return FALSE;
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif
|
|
|
|
#endif /* HAVE_SYSTEM */
|
|
|
|
#endif /* _MSC_VER */
|
|
|
|
}
|
|
|
|
|
|
|
|
static Int
|
2011-03-07 16:02:55 +00:00
|
|
|
p_system ( USES_REGS1 )
|
2001-04-09 20:54:03 +01:00
|
|
|
{ /* '$system'(+SystCommand) */
|
|
|
|
#ifdef HAVE_SYSTEM
|
|
|
|
Term t1 = Deref (ARG1);
|
2005-11-23 13:24:00 +00:00
|
|
|
char *s;
|
|
|
|
|
|
|
|
if (IsVarTerm(t1)) {
|
|
|
|
Yap_Error(INSTANTIATION_ERROR,t1,"argument to system/1 unbound");
|
|
|
|
return FALSE;
|
|
|
|
} else if (IsAtomTerm(t1)) {
|
|
|
|
s = RepAtom(AtomOfTerm(t1))->StrOfAE;
|
|
|
|
} else {
|
2011-05-23 16:19:47 +01:00
|
|
|
if (!Yap_GetName (LOCAL_FileNameBuf, YAP_FILENAME_MAX, t1)) {
|
2005-11-23 13:24:00 +00:00
|
|
|
Yap_Error(TYPE_ERROR_ATOM,t1,"argument to system/1");
|
|
|
|
return FALSE;
|
|
|
|
}
|
2011-05-23 16:19:47 +01:00
|
|
|
s = LOCAL_FileNameBuf;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
2002-11-18 18:18:05 +00:00
|
|
|
/* Yap_CloseStreams(TRUE); */
|
2001-04-09 20:54:03 +01:00
|
|
|
#if _MSC_VER
|
|
|
|
_flushall();
|
|
|
|
#endif
|
2005-11-23 13:24:00 +00:00
|
|
|
if (system (s)) {
|
|
|
|
#if HAVE_STRERROR
|
|
|
|
Yap_Error(OPERATING_SYSTEM_ERROR,t1,"%s in system(%s)", strerror(errno), s);
|
|
|
|
#else
|
|
|
|
Yap_Error(OPERATING_SYSTEM_ERROR,t1,"in system(%s)", s);
|
|
|
|
#endif
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
return TRUE;
|
2001-04-09 20:54:03 +01:00
|
|
|
#else
|
|
|
|
#ifdef MSH
|
|
|
|
register char *shell;
|
|
|
|
shell = "msh -i";
|
2002-11-18 18:18:05 +00:00
|
|
|
/* Yap_CloseStreams(); */
|
2001-04-09 20:54:03 +01:00
|
|
|
system (shell);
|
|
|
|
return (TRUE);
|
2002-01-02 05:35:20 +00:00
|
|
|
#undef command
|
2001-04-09 20:54:03 +01:00
|
|
|
#else
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(SYSTEM_ERROR,TermNil,"sh not available in this machine");
|
2001-04-09 20:54:03 +01:00
|
|
|
return(FALSE);
|
|
|
|
#endif
|
|
|
|
#endif /* HAVE_SYSTEM */
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Rename a file */
|
|
|
|
static Int
|
2011-03-07 16:02:55 +00:00
|
|
|
p_mv ( USES_REGS1 )
|
2001-04-09 20:54:03 +01:00
|
|
|
{ /* rename(+OldName,+NewName) */
|
|
|
|
#if HAVE_LINK
|
|
|
|
int r;
|
|
|
|
char oldname[YAP_FILENAME_MAX], newname[YAP_FILENAME_MAX];
|
|
|
|
Term t1 = Deref (ARG1);
|
|
|
|
Term t2 = Deref (ARG2);
|
2005-11-23 13:24:00 +00:00
|
|
|
if (IsVarTerm(t1)) {
|
|
|
|
Yap_Error(INSTANTIATION_ERROR, t1, "first argument to rename/2 unbound");
|
|
|
|
} else if (!IsAtomTerm(t1)) {
|
|
|
|
Yap_Error(TYPE_ERROR_ATOM, t1, "first argument to rename/2 not atom");
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
2005-11-23 13:24:00 +00:00
|
|
|
if (IsVarTerm(t2)) {
|
|
|
|
Yap_Error(INSTANTIATION_ERROR, t2, "second argument to rename/2 unbound");
|
|
|
|
} else if (!IsAtomTerm(t2)) {
|
|
|
|
Yap_Error(TYPE_ERROR_ATOM, t2, "second argument to rename/2 not atom");
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
2007-09-27 23:40:22 +01:00
|
|
|
TrueFileName (RepAtom(AtomOfTerm(t1))->StrOfAE, NULL, oldname, FALSE);
|
|
|
|
TrueFileName (RepAtom(AtomOfTerm(t2))->StrOfAE, NULL, newname, FALSE);
|
2001-04-09 20:54:03 +01:00
|
|
|
if ((r = link (oldname, newname)) == 0 && (r = unlink (oldname)) != 0)
|
|
|
|
unlink (newname);
|
|
|
|
if (r != 0) {
|
2005-11-23 13:24:00 +00:00
|
|
|
#if HAVE_STRERROR
|
|
|
|
Yap_Error(OPERATING_SYSTEM_ERROR,t2,"%s in rename(%s,%s)", strerror(errno),oldname,newname);
|
|
|
|
#else
|
|
|
|
Yap_Error(OPERATING_SYSTEM_ERROR,t2,"in rename(%s,%s)",oldname,newname);
|
|
|
|
#endif
|
|
|
|
return FALSE;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
2005-11-23 13:24:00 +00:00
|
|
|
return TRUE;
|
2001-04-09 20:54:03 +01:00
|
|
|
#else
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(SYSTEM_ERROR,TermNil,"rename/2 not available in this machine");
|
2001-04-09 20:54:03 +01:00
|
|
|
return (FALSE);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef MAC
|
|
|
|
|
|
|
|
void
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_SetTextFile (name)
|
2001-04-09 20:54:03 +01:00
|
|
|
char *name;
|
|
|
|
{
|
|
|
|
#ifdef MACC
|
|
|
|
SetFileType (name, 'TEXT');
|
|
|
|
SetFileSignature (name, 'EDIT');
|
|
|
|
#else
|
|
|
|
FInfo f;
|
|
|
|
FInfo *p = &f;
|
|
|
|
GetFInfo (name, 0, p);
|
|
|
|
p->fdType = 'TEXT';
|
|
|
|
#ifdef MPW
|
|
|
|
if (mpwshell)
|
|
|
|
p->fdCreator = 'MPS\0';
|
|
|
|
#endif
|
|
|
|
#ifndef LIGHT
|
|
|
|
else
|
|
|
|
p->fdCreator = 'EDIT';
|
|
|
|
#endif
|
|
|
|
SetFInfo (name, 0, p);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
/* return YAP's environment */
|
2011-03-07 16:02:55 +00:00
|
|
|
static Int p_getenv( USES_REGS1 )
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
|
|
|
#if HAVE_GETENV
|
|
|
|
Term t1 = Deref(ARG1), to;
|
|
|
|
char *s, *so;
|
|
|
|
|
|
|
|
if (IsVarTerm(t1)) {
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(INSTANTIATION_ERROR, t1,
|
2001-04-09 20:54:03 +01:00
|
|
|
"first arg of getenv/2");
|
|
|
|
return(FALSE);
|
|
|
|
} else if (!IsAtomTerm(t1)) {
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(TYPE_ERROR_ATOM, t1,
|
2001-04-09 20:54:03 +01:00
|
|
|
"first arg of getenv/2");
|
|
|
|
return(FALSE);
|
|
|
|
} else s = RepAtom(AtomOfTerm(t1))->StrOfAE;
|
|
|
|
if ((so = getenv(s)) == NULL)
|
|
|
|
return(FALSE);
|
2002-11-18 18:18:05 +00:00
|
|
|
to = MkAtomTerm(Yap_LookupAtom(so));
|
|
|
|
return(Yap_unify_constant(ARG2,to));
|
2001-04-09 20:54:03 +01:00
|
|
|
#else
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(SYSTEM_ERROR, TermNil,
|
2001-04-09 20:54:03 +01:00
|
|
|
"getenv not available in this configuration");
|
|
|
|
return (FALSE);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/* set a variable in YAP's environment */
|
2011-03-07 16:02:55 +00:00
|
|
|
static Int p_putenv( USES_REGS1 )
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
|
|
|
#if HAVE_PUTENV
|
|
|
|
Term t1 = Deref(ARG1), t2 = Deref(ARG2);
|
|
|
|
char *s, *s2, *p0, *p;
|
|
|
|
|
|
|
|
if (IsVarTerm(t1)) {
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(INSTANTIATION_ERROR, t1,
|
2001-04-09 20:54:03 +01:00
|
|
|
"first arg to putenv/2");
|
|
|
|
return(FALSE);
|
|
|
|
} else if (!IsAtomTerm(t1)) {
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(TYPE_ERROR_ATOM, t1,
|
2001-04-09 20:54:03 +01:00
|
|
|
"first arg to putenv/2");
|
|
|
|
return(FALSE);
|
|
|
|
} else s = RepAtom(AtomOfTerm(t1))->StrOfAE;
|
|
|
|
if (IsVarTerm(t2)) {
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(INSTANTIATION_ERROR, t1,
|
2001-04-09 20:54:03 +01:00
|
|
|
"second arg to putenv/2");
|
|
|
|
return(FALSE);
|
|
|
|
} else if (!IsAtomTerm(t2)) {
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(TYPE_ERROR_ATOM, t2,
|
2001-04-09 20:54:03 +01:00
|
|
|
"second arg to putenv/2");
|
|
|
|
return(FALSE);
|
|
|
|
} else s2 = RepAtom(AtomOfTerm(t2))->StrOfAE;
|
2007-02-13 11:26:17 +00:00
|
|
|
while (!(p0 = p = Yap_AllocAtomSpace(strlen(s)+strlen(s2)+3))) {
|
|
|
|
if (!Yap_growheap(FALSE, MinHeapGap, NULL)) {
|
2011-05-23 16:19:47 +01:00
|
|
|
Yap_Error(OUT_OF_HEAP_ERROR, TermNil, LOCAL_ErrorMessage);
|
2007-02-13 11:26:17 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
while ((*p++ = *s++) != '\0');
|
|
|
|
p[-1] = '=';
|
|
|
|
while ((*p++ = *s2++) != '\0');
|
|
|
|
if (putenv(p0) == 0)
|
2007-02-13 11:26:17 +00:00
|
|
|
return TRUE;
|
2001-04-09 20:54:03 +01:00
|
|
|
#if HAVE_STRERROR
|
2005-11-23 13:24:00 +00:00
|
|
|
Yap_Error(OPERATING_SYSTEM_ERROR, TermNil,
|
|
|
|
"in putenv(%s)", strerror(errno), p0);
|
2001-04-09 20:54:03 +01:00
|
|
|
#else
|
2005-11-23 13:24:00 +00:00
|
|
|
Yap_Error(OPERATING_SYSTEM_ERROR, TermNil,
|
|
|
|
"in putenv(%s)", p0);
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif
|
2007-02-13 11:26:17 +00:00
|
|
|
return FALSE;
|
2001-04-09 20:54:03 +01:00
|
|
|
#else
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(SYSTEM_ERROR, TermNil,
|
2001-04-09 20:54:03 +01:00
|
|
|
"putenv not available in this configuration");
|
2007-02-13 11:26:17 +00:00
|
|
|
return FALSE;
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/* wrapper for alarm system call */
|
2002-01-17 22:12:09 +00:00
|
|
|
#if _MSC_VER || defined(__MINGW32__)
|
|
|
|
|
2002-01-21 20:07:15 +00:00
|
|
|
static DWORD WINAPI
|
|
|
|
DoTimerThread(LPVOID targ)
|
|
|
|
{
|
2007-05-26 09:19:33 +01:00
|
|
|
Int *time = (Int *)targ;
|
2002-01-21 20:07:15 +00:00
|
|
|
HANDLE htimer;
|
|
|
|
LARGE_INTEGER liDueTime;
|
|
|
|
|
2007-05-21 00:00:38 +01:00
|
|
|
htimer = CreateWaitableTimer(NULL, FALSE, NULL);
|
2002-02-04 16:12:54 +00:00
|
|
|
liDueTime.QuadPart = -10000000;
|
2007-05-26 09:19:33 +01:00
|
|
|
liDueTime.QuadPart *= time[0];
|
|
|
|
/* add time in usecs */
|
|
|
|
liDueTime.QuadPart -= time[1]*10;
|
2002-01-21 20:07:15 +00:00
|
|
|
/* Copy the relative time into a LARGE_INTEGER. */
|
|
|
|
if (SetWaitableTimer(htimer, &liDueTime,0,NULL,NULL,0) == 0) {
|
|
|
|
return(FALSE);
|
|
|
|
}
|
|
|
|
if (WaitForSingleObject(htimer, INFINITE) != WAIT_OBJECT_0)
|
|
|
|
fprintf(stderr,"WaitForSingleObject failed (%ld)\n", GetLastError());
|
2004-01-23 02:23:51 +00:00
|
|
|
Yap_signal (YAP_ALARM_SIGNAL);
|
2001-04-09 20:54:03 +01:00
|
|
|
/* now, say what is going on */
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_PutValue(AtomAlarm, MkAtomTerm(AtomTrue));
|
2002-01-21 20:07:15 +00:00
|
|
|
ExitThread(1);
|
2002-02-04 16:12:54 +00:00
|
|
|
#if _MSC_VER
|
|
|
|
return(0L);
|
|
|
|
#endif
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
2002-01-21 20:07:15 +00:00
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
static Int
|
2011-03-07 16:02:55 +00:00
|
|
|
p_alarm( USES_REGS1 )
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
|
|
|
Term t = Deref(ARG1);
|
2007-05-21 00:00:38 +01:00
|
|
|
Term t2 = Deref(ARG2);
|
2009-05-26 23:31:21 +01:00
|
|
|
Int i1, i2;
|
2001-04-09 20:54:03 +01:00
|
|
|
if (IsVarTerm(t)) {
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(INSTANTIATION_ERROR, t, "alarm/2");
|
2001-04-09 20:54:03 +01:00
|
|
|
return(FALSE);
|
|
|
|
}
|
|
|
|
if (!IsIntegerTerm(t)) {
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(TYPE_ERROR_INTEGER, t, "alarm/2");
|
2001-04-09 20:54:03 +01:00
|
|
|
return(FALSE);
|
|
|
|
}
|
2007-05-21 00:00:38 +01:00
|
|
|
if (IsVarTerm(t2)) {
|
|
|
|
Yap_Error(INSTANTIATION_ERROR, t2, "alarm/2");
|
|
|
|
return(FALSE);
|
|
|
|
}
|
|
|
|
if (!IsIntegerTerm(t2)) {
|
|
|
|
Yap_Error(TYPE_ERROR_INTEGER, t2, "alarm/2");
|
|
|
|
return(FALSE);
|
|
|
|
}
|
2009-05-26 23:31:21 +01:00
|
|
|
i1 = IntegerOfTerm(t);
|
|
|
|
i2 = IntegerOfTerm(t2);
|
|
|
|
if (i1 == 0 && i2 == 0) {
|
2011-05-04 10:11:41 +01:00
|
|
|
LOCK(LOCAL_SignalLock);
|
|
|
|
if (LOCAL_ActiveSignals & YAP_ALARM_SIGNAL) {
|
|
|
|
LOCAL_ActiveSignals &= ~YAP_ALARM_SIGNAL;
|
|
|
|
if (!LOCAL_ActiveSignals) {
|
2009-05-26 23:31:21 +01:00
|
|
|
CreepFlag = CalculateStackGap();
|
|
|
|
}
|
|
|
|
}
|
2011-05-04 10:11:41 +01:00
|
|
|
UNLOCK(LOCAL_SignalLock);
|
2009-05-26 23:31:21 +01:00
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
#if _MSC_VER || defined(__MINGW32__)
|
|
|
|
{
|
|
|
|
Term tout;
|
2007-05-26 09:19:33 +01:00
|
|
|
Int time[2];
|
|
|
|
|
2009-05-26 23:31:21 +01:00
|
|
|
time[0] = i1;
|
|
|
|
time[1] = i2;
|
2002-01-17 22:12:09 +00:00
|
|
|
|
2007-05-26 09:19:33 +01:00
|
|
|
if (time[0] != 0 && time[1] != 0) {
|
2002-01-21 20:07:15 +00:00
|
|
|
DWORD dwThreadId;
|
|
|
|
HANDLE hThread;
|
|
|
|
|
|
|
|
hThread = CreateThread(
|
|
|
|
NULL, /* no security attributes */
|
|
|
|
0, /* use default stack size */
|
|
|
|
DoTimerThread, /* thread function */
|
2007-05-26 09:19:33 +01:00
|
|
|
(LPVOID)time, /* argument to thread function */
|
2002-01-21 20:07:15 +00:00
|
|
|
0, /* use default creation flags */
|
|
|
|
&dwThreadId); /* returns the thread identifier */
|
|
|
|
|
|
|
|
/* Check the return value for success. */
|
|
|
|
if (hThread == NULL) {
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_WinError("trying to use alarm");
|
2002-01-17 22:12:09 +00:00
|
|
|
}
|
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
tout = MkIntegerTerm(0);
|
2007-05-21 00:00:38 +01:00
|
|
|
return Yap_unify(ARG3,tout) && Yap_unify(ARG4,MkIntTerm(0));
|
|
|
|
}
|
2008-02-12 21:50:58 +00:00
|
|
|
#elif HAVE_SETITIMER && !SUPPORT_CONDOR
|
2007-05-21 00:00:38 +01:00
|
|
|
{
|
|
|
|
struct itimerval new, old;
|
|
|
|
|
|
|
|
new.it_interval.tv_sec = 0;
|
|
|
|
new.it_interval.tv_usec = 0;
|
2009-05-26 23:31:21 +01:00
|
|
|
new.it_value.tv_sec = i1;
|
|
|
|
new.it_value.tv_usec = i2;
|
2007-05-21 00:00:38 +01:00
|
|
|
if (setitimer(ITIMER_REAL, &new, &old) < 0) {
|
|
|
|
#if HAVE_STRERROR
|
|
|
|
Yap_Error(OPERATING_SYSTEM_ERROR, ARG1, "setitimer: %s", strerror(errno));
|
|
|
|
#else
|
|
|
|
Yap_Error(OPERATING_SYSTEM_ERROR, ARG1, "setitimer %d", errno);
|
|
|
|
#endif
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
return Yap_unify(ARG3,MkIntegerTerm(old.it_value.tv_sec)) &&
|
|
|
|
Yap_unify(ARG4,MkIntegerTerm(old.it_value.tv_usec));
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
2008-02-12 21:50:58 +00:00
|
|
|
#elif HAVE_ALARM && !SUPPORT_CONDOR
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
|
|
|
Int left;
|
|
|
|
Term tout;
|
|
|
|
|
2009-05-26 23:31:21 +01:00
|
|
|
left = alarm(i1);
|
2001-04-09 20:54:03 +01:00
|
|
|
tout = MkIntegerTerm(left);
|
2007-05-21 00:00:38 +01:00
|
|
|
return Yap_unify(ARG3,tout) && Yap_unify(ARG4,MkIntTerm(0)) ;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
#else
|
2002-08-19 00:18:18 +01:00
|
|
|
/* not actually trying to set the alarm */
|
|
|
|
if (IntegerOfTerm(t) == 0)
|
2004-10-14 23:14:53 +01:00
|
|
|
return TRUE;
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(SYSTEM_ERROR, TermNil,
|
2001-04-09 20:54:03 +01:00
|
|
|
"alarm not available in this configuration");
|
2007-05-21 00:00:38 +01:00
|
|
|
return FALSE;
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2009-06-02 01:49:03 +01:00
|
|
|
static Int
|
2011-03-07 16:02:55 +00:00
|
|
|
p_virtual_alarm( USES_REGS1 )
|
2009-06-02 01:49:03 +01:00
|
|
|
{
|
|
|
|
Term t = Deref(ARG1);
|
|
|
|
Term t2 = Deref(ARG2);
|
|
|
|
if (IsVarTerm(t)) {
|
|
|
|
Yap_Error(INSTANTIATION_ERROR, t, "alarm/2");
|
|
|
|
return(FALSE);
|
|
|
|
}
|
|
|
|
if (!IsIntegerTerm(t)) {
|
|
|
|
Yap_Error(TYPE_ERROR_INTEGER, t, "alarm/2");
|
|
|
|
return(FALSE);
|
|
|
|
}
|
|
|
|
if (IsVarTerm(t2)) {
|
|
|
|
Yap_Error(INSTANTIATION_ERROR, t2, "alarm/2");
|
|
|
|
return(FALSE);
|
|
|
|
}
|
|
|
|
if (!IsIntegerTerm(t2)) {
|
|
|
|
Yap_Error(TYPE_ERROR_INTEGER, t2, "alarm/2");
|
|
|
|
return(FALSE);
|
|
|
|
}
|
|
|
|
#if _MSC_VER || defined(__MINGW32__)
|
|
|
|
{
|
|
|
|
Term tout;
|
|
|
|
Int time[2];
|
|
|
|
|
|
|
|
time[0] = IntegerOfTerm(t);
|
|
|
|
time[1] = IntegerOfTerm(t2);
|
|
|
|
|
|
|
|
if (time[0] != 0 && time[1] != 0) {
|
|
|
|
DWORD dwThreadId;
|
|
|
|
HANDLE hThread;
|
|
|
|
|
|
|
|
hThread = CreateThread(
|
|
|
|
NULL, /* no security attributes */
|
|
|
|
0, /* use default stack size */
|
|
|
|
DoTimerThread, /* thread function */
|
|
|
|
(LPVOID)time, /* argument to thread function */
|
|
|
|
0, /* use default creation flags */
|
|
|
|
&dwThreadId); /* returns the thread identifier */
|
|
|
|
|
|
|
|
/* Check the return value for success. */
|
|
|
|
if (hThread == NULL) {
|
|
|
|
Yap_WinError("trying to use alarm");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
tout = MkIntegerTerm(0);
|
|
|
|
return Yap_unify(ARG3,tout) && Yap_unify(ARG4,MkIntTerm(0));
|
|
|
|
}
|
|
|
|
#elif HAVE_SETITIMER && !SUPPORT_CONDOR
|
|
|
|
{
|
|
|
|
struct itimerval new, old;
|
|
|
|
|
|
|
|
new.it_interval.tv_sec = 0;
|
|
|
|
new.it_interval.tv_usec = 0;
|
|
|
|
new.it_value.tv_sec = IntegerOfTerm(t);
|
|
|
|
new.it_value.tv_usec = IntegerOfTerm(t2);
|
|
|
|
if (setitimer(ITIMER_VIRTUAL, &new, &old) < 0) {
|
|
|
|
#if HAVE_STRERROR
|
|
|
|
Yap_Error(OPERATING_SYSTEM_ERROR, ARG1, "setitimer: %s", strerror(errno));
|
|
|
|
#else
|
|
|
|
Yap_Error(OPERATING_SYSTEM_ERROR, ARG1, "setitimer %d", errno);
|
|
|
|
#endif
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
return Yap_unify(ARG3,MkIntegerTerm(old.it_value.tv_sec)) &&
|
|
|
|
Yap_unify(ARG4,MkIntegerTerm(old.it_value.tv_usec));
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
/* not actually trying to set the alarm */
|
|
|
|
if (IntegerOfTerm(t) == 0)
|
|
|
|
return TRUE;
|
|
|
|
Yap_Error(SYSTEM_ERROR, TermNil,
|
|
|
|
"virtual_alarm not available in this configuration");
|
|
|
|
return FALSE;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
#if HAVE_FPU_CONTROL_H
|
|
|
|
#include <fpu_control.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* by default Linux with glibc is IEEE compliant anyway..., but we will pretend it is not. */
|
2002-11-11 17:38:10 +00:00
|
|
|
static void
|
2001-04-09 20:54:03 +01:00
|
|
|
set_fpu_exceptions(int flag)
|
|
|
|
{
|
|
|
|
if (flag) {
|
|
|
|
#if defined(__hpux)
|
2002-06-13 19:24:04 +01:00
|
|
|
# if HAVE_FESETTRAPENABLE
|
|
|
|
/* From HP-UX 11.0 onwards: */
|
|
|
|
fesettrapenable(FE_INVALID|FE_DIVBYZERO|FE_OVERFLOW|FE_UNDERFLOW);
|
|
|
|
# else
|
|
|
|
/*
|
|
|
|
Up until HP-UX 10.20:
|
|
|
|
FP_X_INV invalid operation exceptions
|
|
|
|
FP_X_DZ divide-by-zero exception
|
|
|
|
FP_X_OFL overflow exception
|
|
|
|
FP_X_UFL underflow exception
|
|
|
|
FP_X_IMP imprecise (inexact result)
|
|
|
|
FP_X_CLEAR simply zero to clear all flags
|
|
|
|
*/
|
2001-04-09 20:54:03 +01:00
|
|
|
fpsetmask(FP_X_INV|FP_X_DZ|FP_X_OFL|FP_X_UFL);
|
2002-06-13 19:24:04 +01:00
|
|
|
# endif
|
|
|
|
#endif /* __hpux */
|
2002-02-11 20:22:15 +00:00
|
|
|
#if HAVE_FPU_CONTROL_H && i386 && defined(__GNUC__)
|
2001-04-09 20:54:03 +01:00
|
|
|
/* I shall ignore denormalization and precision errors */
|
|
|
|
int v = _FPU_IEEE & ~(_FPU_MASK_IM|_FPU_MASK_ZM|_FPU_MASK_OM|_FPU_MASK_UM);
|
|
|
|
_FPU_SETCW(v);
|
|
|
|
#endif
|
|
|
|
#if HAVE_FETESTEXCEPT
|
|
|
|
feclearexcept(FE_ALL_EXCEPT);
|
|
|
|
#endif
|
2002-02-04 16:12:54 +00:00
|
|
|
my_signal (SIGFPE, HandleMatherr);
|
2001-04-09 20:54:03 +01:00
|
|
|
} else {
|
|
|
|
/* do IEEE arithmetic in the way the big boys do */
|
|
|
|
#if defined(__hpux)
|
2002-06-13 19:24:04 +01:00
|
|
|
# if HAVE_FESETTRAPENABLE
|
|
|
|
fesettrapenable(FE_ALL_EXCEPT);
|
|
|
|
# else
|
2001-04-09 20:54:03 +01:00
|
|
|
fpsetmask(FP_X_CLEAR);
|
2002-06-13 19:24:04 +01:00
|
|
|
# endif
|
|
|
|
#endif /* __hpux */
|
2002-02-11 20:22:15 +00:00
|
|
|
#if HAVE_FPU_CONTROL_H && i386 && defined(__GNUC__)
|
2001-04-09 20:54:03 +01:00
|
|
|
/* this will probably not work in older releases of Linux */
|
|
|
|
int v = _FPU_IEEE;
|
|
|
|
_FPU_SETCW(v);
|
|
|
|
#endif
|
2002-02-04 16:12:54 +00:00
|
|
|
my_signal (SIGFPE, SIG_IGN);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-11-11 17:38:10 +00:00
|
|
|
void
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_set_fpu_exceptions(int flag)
|
2002-11-11 17:38:10 +00:00
|
|
|
{
|
|
|
|
set_fpu_exceptions(flag);
|
|
|
|
}
|
2002-02-04 16:12:54 +00:00
|
|
|
static Int
|
2011-03-07 16:02:55 +00:00
|
|
|
p_set_fpu_exceptions( USES_REGS1 ) {
|
2002-02-04 16:12:54 +00:00
|
|
|
if (yap_flags[LANGUAGE_MODE_FLAG] == 1) {
|
|
|
|
set_fpu_exceptions(FALSE); /* can't make it work right */
|
|
|
|
} else {
|
|
|
|
set_fpu_exceptions(FALSE);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
2002-02-04 16:12:54 +00:00
|
|
|
return(TRUE);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
2002-02-08 22:19:24 +00:00
|
|
|
static Int
|
2011-03-07 16:02:55 +00:00
|
|
|
p_host_type( USES_REGS1 ) {
|
2002-11-18 18:18:05 +00:00
|
|
|
Term out = MkAtomTerm(Yap_LookupAtom(HOST_ALIAS));
|
|
|
|
return(Yap_unify(out,ARG1));
|
2002-02-08 22:19:24 +00:00
|
|
|
}
|
|
|
|
|
2005-03-13 06:26:13 +00:00
|
|
|
static Int
|
2011-03-07 16:02:55 +00:00
|
|
|
p_yap_home( USES_REGS1 ) {
|
2009-10-23 09:09:16 +01:00
|
|
|
Term out = MkAtomTerm(Yap_LookupAtom(YAP_ROOTDIR));
|
2005-03-13 06:26:13 +00:00
|
|
|
return(Yap_unify(out,ARG1));
|
|
|
|
}
|
|
|
|
|
2005-10-28 18:38:50 +01:00
|
|
|
static Int
|
2011-03-07 16:02:55 +00:00
|
|
|
p_env_separator( USES_REGS1 ) {
|
2005-10-28 18:38:50 +01:00
|
|
|
#if defined(_WIN32)
|
|
|
|
return Yap_unify(MkIntegerTerm(';'),ARG1);
|
|
|
|
#else
|
|
|
|
return Yap_unify(MkIntegerTerm(':'),ARG1);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
/*
|
|
|
|
* This is responsable for the initialization of all machine dependant
|
|
|
|
* predicates
|
|
|
|
*/
|
|
|
|
void
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_InitSysbits (void)
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
2002-01-23 02:28:31 +00:00
|
|
|
#if __simplescalar__
|
|
|
|
{
|
|
|
|
char *pwd = getenv("PWD");
|
2011-05-25 16:40:36 +01:00
|
|
|
strncpy(GLOBAL_pwd,pwd,YAP_FILENAME_MAX);
|
2002-01-23 02:28:31 +00:00
|
|
|
}
|
|
|
|
#endif
|
2001-04-09 20:54:03 +01:00
|
|
|
InitWTime ();
|
|
|
|
InitRandom ();
|
|
|
|
/* let the caller control signals as it sees fit */
|
|
|
|
InitSignals ();
|
|
|
|
}
|
|
|
|
|
2004-03-02 16:44:58 +00:00
|
|
|
void
|
2012-12-11 22:07:10 +00:00
|
|
|
Yap_InitTime( int wid )
|
2004-03-02 16:44:58 +00:00
|
|
|
{
|
2012-12-11 22:07:10 +00:00
|
|
|
InitTime( wid );
|
2004-03-02 16:44:58 +00:00
|
|
|
}
|
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
void
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_ReInitWallTime (void)
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
|
|
|
InitWTime();
|
2004-12-02 06:06:47 +00:00
|
|
|
if (Yap_heap_regs->last_wtime != NULL)
|
|
|
|
Yap_FreeCodeSpace(Yap_heap_regs->last_wtime);
|
2001-04-09 20:54:03 +01:00
|
|
|
InitLastWtime();
|
|
|
|
}
|
|
|
|
|
2007-09-27 16:25:34 +01:00
|
|
|
static Int
|
2011-03-07 16:02:55 +00:00
|
|
|
p_unix( USES_REGS1 )
|
2007-09-27 16:25:34 +01:00
|
|
|
{
|
|
|
|
#ifdef unix
|
|
|
|
return TRUE;
|
|
|
|
#else
|
|
|
|
#ifdef __unix__
|
|
|
|
return TRUE;
|
2008-03-31 20:51:07 +01:00
|
|
|
#else
|
|
|
|
#ifdef __APPLE__
|
|
|
|
return TRUE;
|
2007-09-27 16:25:34 +01:00
|
|
|
#else
|
|
|
|
return FALSE;
|
|
|
|
#endif
|
|
|
|
#endif
|
2008-03-31 20:51:07 +01:00
|
|
|
#endif
|
2007-09-27 16:25:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static Int
|
2011-03-07 16:02:55 +00:00
|
|
|
p_win32( USES_REGS1 )
|
2007-09-27 16:25:34 +01:00
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
return TRUE;
|
|
|
|
#else
|
|
|
|
#ifdef __CYGWIN__
|
|
|
|
return TRUE;
|
|
|
|
#else
|
|
|
|
return FALSE;
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2009-05-20 07:53:14 +01:00
|
|
|
|
|
|
|
static Int
|
2011-03-07 16:02:55 +00:00
|
|
|
p_enable_interrupts( USES_REGS1 )
|
2009-05-20 07:53:14 +01:00
|
|
|
{
|
2011-05-04 10:11:41 +01:00
|
|
|
LOCK(LOCAL_SignalLock);
|
|
|
|
LOCAL_InterruptsDisabled--;
|
|
|
|
if (LOCAL_ActiveSignals && !LOCAL_InterruptsDisabled) {
|
2009-05-21 06:38:35 +01:00
|
|
|
CreepFlag = Unsigned(LCL0);
|
|
|
|
}
|
2011-05-04 10:11:41 +01:00
|
|
|
UNLOCK(LOCAL_SignalLock);
|
2009-05-20 07:53:14 +01:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static Int
|
2011-03-07 16:02:55 +00:00
|
|
|
p_disable_interrupts( USES_REGS1 )
|
2009-05-20 07:53:14 +01:00
|
|
|
{
|
2011-05-04 10:11:41 +01:00
|
|
|
LOCK(LOCAL_SignalLock);
|
|
|
|
LOCAL_InterruptsDisabled++;
|
|
|
|
if (LOCAL_ActiveSignals) {
|
2009-05-21 06:38:35 +01:00
|
|
|
CreepFlag = CalculateStackGap();
|
|
|
|
}
|
2011-05-04 10:11:41 +01:00
|
|
|
UNLOCK(LOCAL_SignalLock);
|
2009-05-20 07:53:14 +01:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2007-09-27 16:25:34 +01:00
|
|
|
static Int
|
2011-03-07 16:02:55 +00:00
|
|
|
p_ld_path( USES_REGS1 )
|
2007-09-27 16:25:34 +01:00
|
|
|
{
|
2009-10-23 09:09:16 +01:00
|
|
|
return Yap_unify(ARG1,MkAtomTerm(Yap_LookupAtom(YAP_LIBDIR)));
|
2007-09-27 16:25:34 +01:00
|
|
|
}
|
|
|
|
|
2010-02-23 00:29:17 +00:00
|
|
|
static Int
|
2011-03-07 16:02:55 +00:00
|
|
|
p_address_bits( USES_REGS1 )
|
2010-02-23 00:29:17 +00:00
|
|
|
{
|
|
|
|
#if SIZEOF_INT_P==4
|
|
|
|
return Yap_unify(ARG1,MkIntTerm(32));
|
|
|
|
#else
|
|
|
|
return Yap_unify(ARG1,MkIntTerm(64));
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2007-09-27 16:25:34 +01:00
|
|
|
|
|
|
|
|
2008-03-27 00:41:33 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
|
|
|
|
/* This code is from SWI-Prolog by Jan Wielemaker */
|
|
|
|
|
|
|
|
#define wstreq(s,q) (wcscmp((s), (q)) == 0)
|
|
|
|
|
|
|
|
static HKEY
|
|
|
|
reg_open_key(const wchar_t *which, int create)
|
|
|
|
{ HKEY key = HKEY_CURRENT_USER;
|
|
|
|
DWORD disp;
|
|
|
|
LONG rval;
|
|
|
|
|
|
|
|
while(*which)
|
|
|
|
{ wchar_t buf[256];
|
|
|
|
wchar_t *s;
|
|
|
|
HKEY tmp;
|
|
|
|
|
|
|
|
for(s=buf; *which && !(*which == '/' || *which == '\\'); )
|
|
|
|
*s++ = *which++;
|
|
|
|
*s = '\0';
|
|
|
|
if ( *which )
|
|
|
|
which++;
|
|
|
|
|
|
|
|
if ( wstreq(buf, L"HKEY_CLASSES_ROOT") )
|
|
|
|
{ key = HKEY_CLASSES_ROOT;
|
|
|
|
continue;
|
|
|
|
} else if ( wstreq(buf, L"HKEY_CURRENT_USER") )
|
|
|
|
{ key = HKEY_CURRENT_USER;
|
|
|
|
continue;
|
|
|
|
} else if ( wstreq(buf, L"HKEY_LOCAL_MACHINE") )
|
|
|
|
{ key = HKEY_LOCAL_MACHINE;
|
|
|
|
continue;
|
|
|
|
} else if ( wstreq(buf, L"HKEY_USERS") )
|
|
|
|
{ key = HKEY_USERS;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( RegOpenKeyExW(key, buf, 0L, KEY_READ, &tmp) == ERROR_SUCCESS )
|
|
|
|
{ RegCloseKey(key);
|
|
|
|
key = tmp;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( !create )
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
rval = RegCreateKeyExW(key, buf, 0, L"", 0,
|
|
|
|
KEY_ALL_ACCESS, NULL, &tmp, &disp);
|
|
|
|
RegCloseKey(key);
|
|
|
|
if ( rval == ERROR_SUCCESS )
|
|
|
|
key = tmp;
|
|
|
|
else
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return key;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define MAXREGSTRLEN 1024
|
|
|
|
|
|
|
|
static void
|
|
|
|
recover_space(wchar_t *k, Atom At)
|
|
|
|
{
|
|
|
|
if (At->WStrOfAE != k)
|
|
|
|
Yap_FreeCodeSpace((char *)k);
|
|
|
|
}
|
|
|
|
|
|
|
|
static wchar_t *
|
2013-10-30 13:56:35 +00:00
|
|
|
WideStringFromAtom(Atom KeyAt USES_REGS)
|
2008-03-27 00:41:33 +00:00
|
|
|
{
|
|
|
|
if (IsWideAtom(KeyAt)) {
|
|
|
|
return KeyAt->WStrOfAE;
|
|
|
|
} else {
|
|
|
|
int len = strlen(KeyAt->StrOfAE);
|
|
|
|
int sz = sizeof(wchar_t)*(len+1);
|
|
|
|
char *chp = KeyAt->StrOfAE;
|
|
|
|
wchar_t *kptr, *k;
|
|
|
|
|
|
|
|
k = (wchar_t *)Yap_AllocCodeSpace(sz);
|
|
|
|
while (k == NULL) {
|
|
|
|
if (!Yap_growheap(FALSE, sz, NULL)) {
|
|
|
|
Yap_Error(OUT_OF_HEAP_ERROR, MkIntegerTerm(sz), "generating key in win_registry_get_value/3");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
kptr = k;
|
|
|
|
while ((*kptr++ = *chp++));
|
|
|
|
return k;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static Int
|
2011-03-07 16:02:55 +00:00
|
|
|
p_win_registry_get_value( USES_REGS1 )
|
2008-03-27 00:41:33 +00:00
|
|
|
{
|
|
|
|
DWORD type;
|
|
|
|
BYTE data[MAXREGSTRLEN];
|
|
|
|
DWORD len = sizeof(data);
|
|
|
|
wchar_t *k, *name;
|
|
|
|
HKEY key;
|
|
|
|
Term Key = Deref(ARG1);
|
|
|
|
Term Name = Deref(ARG2);
|
|
|
|
Atom KeyAt, NameAt;
|
|
|
|
|
|
|
|
if (IsVarTerm(Key)) {
|
|
|
|
Yap_Error(INSTANTIATION_ERROR,Key,"argument to win_registry_get_value unbound");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
if (!IsAtomTerm(Key)) {
|
|
|
|
Yap_Error(TYPE_ERROR_ATOM,Key,"argument to win_registry_get_value");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
KeyAt = AtomOfTerm(Key);
|
|
|
|
if (IsVarTerm(Name)) {
|
|
|
|
Yap_Error(INSTANTIATION_ERROR,Key,"argument to win_registry_get_value unbound");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
if (!IsAtomTerm(Name)) {
|
|
|
|
Yap_Error(TYPE_ERROR_ATOM,Key,"argument to win_registry_get_value");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
NameAt = AtomOfTerm(Name);
|
|
|
|
|
2013-10-30 13:56:35 +00:00
|
|
|
k = WideStringFromAtom(KeyAt PASS_REGS);
|
2008-03-27 00:41:33 +00:00
|
|
|
if ( !(key=reg_open_key(k, FALSE)) ) {
|
|
|
|
Yap_Error(EXISTENCE_ERROR_KEY, Key, "argument to win_registry_get_value");
|
|
|
|
recover_space(k, KeyAt);
|
|
|
|
return FALSE;
|
|
|
|
}
|
2013-10-30 13:56:35 +00:00
|
|
|
name = WideStringFromAtom(NameAt PASS_REGS);
|
2008-03-27 00:41:33 +00:00
|
|
|
|
|
|
|
if ( RegQueryValueExW(key, name, NULL, &type, data, &len) == ERROR_SUCCESS ) {
|
|
|
|
RegCloseKey(key);
|
|
|
|
switch(type) {
|
|
|
|
case REG_SZ:
|
|
|
|
recover_space(k, KeyAt);
|
|
|
|
recover_space(name, NameAt);
|
|
|
|
((wchar_t *)data)[len] = '\0';
|
|
|
|
return Yap_unify(MkAtomTerm(Yap_LookupMaybeWideAtom((wchar_t *)data)),ARG3);
|
|
|
|
case REG_DWORD:
|
|
|
|
recover_space(k, KeyAt);
|
|
|
|
recover_space(name, NameAt);
|
2010-02-10 09:03:03 +00:00
|
|
|
{
|
|
|
|
DWORD *d = (DWORD *)data;
|
|
|
|
return Yap_unify(MkIntegerTerm((Int)d[0]),ARG3);
|
|
|
|
}
|
2008-03-27 00:41:33 +00:00
|
|
|
default:
|
|
|
|
recover_space(k, KeyAt);
|
|
|
|
recover_space(name, NameAt);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
recover_space(k, KeyAt);
|
|
|
|
recover_space(name, NameAt);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
|
|
|
Yap_RegistryGetString(char *name)
|
|
|
|
{
|
|
|
|
DWORD type;
|
|
|
|
BYTE data[MAXREGSTRLEN];
|
|
|
|
DWORD len = sizeof(data);
|
|
|
|
HKEY key;
|
|
|
|
char *ptr;
|
|
|
|
int i;
|
|
|
|
|
2012-06-21 09:09:49 +01:00
|
|
|
#if SIZEOF_INT_P == 8
|
|
|
|
if ( !(key=reg_open_key(L"HKEY_LOCAL_MACHINE/SOFTWARE/YAP/Prolog64", FALSE)) ) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
#else
|
2008-03-27 00:41:33 +00:00
|
|
|
if ( !(key=reg_open_key(L"HKEY_LOCAL_MACHINE/SOFTWARE/YAP/Prolog", FALSE)) ) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2012-06-21 09:09:49 +01:00
|
|
|
#endif
|
2008-03-27 00:41:33 +00:00
|
|
|
if ( RegQueryValueEx(key, name, NULL, &type, data, &len) == ERROR_SUCCESS ) {
|
|
|
|
RegCloseKey(key);
|
|
|
|
switch(type) {
|
|
|
|
case REG_SZ:
|
|
|
|
ptr = malloc(len+2);
|
|
|
|
if (!ptr)
|
|
|
|
return NULL;
|
|
|
|
for (i=0; i<= len; i++)
|
|
|
|
ptr[i] = data[i];
|
|
|
|
ptr[len+1] = '\0';
|
|
|
|
return ptr;
|
|
|
|
default:
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
void
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_InitSysPreds(void)
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
2011-03-07 16:02:55 +00:00
|
|
|
CACHE_REGS
|
2006-05-17 19:38:11 +01:00
|
|
|
Term cm = CurrentModule;
|
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
/* can only do after heap is initialised */
|
|
|
|
InitLastWtime();
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_InitCPred ("srandom", 1, p_srandom, SafePredFlag);
|
|
|
|
Yap_InitCPred ("sh", 0, p_sh, SafePredFlag|SyncPredFlag);
|
2012-10-19 18:10:48 +01:00
|
|
|
Yap_InitCPred ("$shell", 1, p_shell, SafePredFlag|SyncPredFlag);
|
2005-11-23 13:24:00 +00:00
|
|
|
Yap_InitCPred ("system", 1, p_system, SafePredFlag|SyncPredFlag);
|
|
|
|
Yap_InitCPred ("rename", 2, p_mv, SafePredFlag|SyncPredFlag);
|
2012-10-19 18:10:48 +01:00
|
|
|
Yap_InitCPred ("$yap_home", 1, p_yap_home, SafePredFlag|SyncPredFlag);
|
|
|
|
Yap_InitCPred ("$dir_separator", 1, p_dir_sp, SafePredFlag);
|
|
|
|
Yap_InitCPred ("$alarm", 4, p_alarm, SafePredFlag|SyncPredFlag);
|
|
|
|
Yap_InitCPred ("$getenv", 2, p_getenv, SafePredFlag);
|
|
|
|
Yap_InitCPred ("$putenv", 2, p_putenv, SafePredFlag|SyncPredFlag);
|
|
|
|
Yap_InitCPred ("$set_fpu_exceptions", 0, p_set_fpu_exceptions, SafePredFlag|SyncPredFlag);
|
|
|
|
Yap_InitCPred ("$host_type", 1, p_host_type, SafePredFlag|SyncPredFlag);
|
2005-10-28 18:38:50 +01:00
|
|
|
Yap_InitCPred ("$env_separator", 1, p_env_separator, SafePredFlag);
|
2007-09-27 16:25:34 +01:00
|
|
|
Yap_InitCPred ("$unix", 0, p_unix, SafePredFlag);
|
|
|
|
Yap_InitCPred ("$win32", 0, p_win32, SafePredFlag);
|
|
|
|
Yap_InitCPred ("$ld_path", 1, p_ld_path, SafePredFlag);
|
2010-02-23 00:29:17 +00:00
|
|
|
Yap_InitCPred ("$address_bits", 1, p_address_bits, SafePredFlag);
|
2008-03-27 00:41:33 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
Yap_InitCPred ("win_registry_get_value", 3, p_win_registry_get_value,0);
|
|
|
|
#endif
|
2009-05-21 06:38:35 +01:00
|
|
|
CurrentModule = HACKS_MODULE;
|
2012-10-19 18:10:48 +01:00
|
|
|
Yap_InitCPred ("virtual_alarm", 4, p_virtual_alarm, SafePredFlag|SyncPredFlag);
|
2009-05-21 06:38:35 +01:00
|
|
|
Yap_InitCPred ("enable_interrupts", 0, p_enable_interrupts, SafePredFlag);
|
|
|
|
Yap_InitCPred ("disable_interrupts", 0, p_disable_interrupts, SafePredFlag);
|
2010-04-22 12:11:15 +01:00
|
|
|
CurrentModule = OPERATING_SYSTEM_MODULE;
|
2006-05-17 19:38:11 +01:00
|
|
|
Yap_InitCPred ("true_file_name", 2, p_true_file_name, SyncPredFlag);
|
2007-09-27 23:40:22 +01:00
|
|
|
Yap_InitCPred ("true_file_name", 3, p_true_file_name3, SyncPredFlag);
|
2006-05-17 19:38:11 +01:00
|
|
|
CurrentModule = cm;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef VAX
|
|
|
|
|
|
|
|
/* avoid longjmp botch */
|
|
|
|
|
|
|
|
int vax_absmi_fp;
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
int eh;
|
|
|
|
int flgs;
|
|
|
|
int ap;
|
|
|
|
int fp;
|
|
|
|
int pc;
|
|
|
|
int dummy1;
|
|
|
|
int dummy2;
|
|
|
|
int dummy3;
|
|
|
|
int oldfp;
|
|
|
|
int dummy4;
|
|
|
|
int dummy5;
|
|
|
|
int dummy6;
|
|
|
|
int oldpc;
|
|
|
|
}
|
|
|
|
|
|
|
|
*VaxFramePtr;
|
|
|
|
|
|
|
|
|
|
|
|
VaxFixFrame (dummy)
|
|
|
|
{
|
|
|
|
int maxframes = 100;
|
|
|
|
VaxFramePtr fp = (VaxFramePtr) (((int *) &dummy) - 6);
|
|
|
|
while (--maxframes)
|
|
|
|
{
|
|
|
|
fp = (VaxFramePtr) fp->fp;
|
|
|
|
if (fp->flgs == 0)
|
|
|
|
{
|
|
|
|
if (fp->oldfp >= ®S[6] && fp->oldfp < ®S[REG_SIZE])
|
|
|
|
fp->oldfp = vax_absmi_fp;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2002-11-11 17:38:10 +00:00
|
|
|
#if defined(_WIN32)
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2002-02-22 06:12:18 +00:00
|
|
|
#include <windows.h>
|
|
|
|
|
2013-04-25 23:15:04 +01:00
|
|
|
int WINAPI win_yap(HANDLE, DWORD, LPVOID);
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
int WINAPI win_yap(HANDLE hinst, DWORD reason, LPVOID reserved)
|
|
|
|
{
|
|
|
|
switch (reason)
|
|
|
|
{
|
|
|
|
case DLL_PROCESS_ATTACH:
|
|
|
|
break;
|
|
|
|
case DLL_PROCESS_DETACH:
|
|
|
|
break;
|
|
|
|
case DLL_THREAD_ATTACH:
|
|
|
|
break;
|
|
|
|
case DLL_THREAD_DETACH:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
#endif
|
2002-02-08 22:19:24 +00:00
|
|
|
|
2004-02-12 17:09:17 +00:00
|
|
|
#if (defined(YAPOR) || defined(THREADS)) && !defined(USE_PTHREAD_LOCKING)
|
2002-10-21 23:14:29 +01:00
|
|
|
#ifdef sparc
|
2013-04-25 23:15:04 +01:00
|
|
|
void rw_lock_voodoo(void);
|
2002-10-21 23:14:29 +01:00
|
|
|
|
|
|
|
void
|
|
|
|
rw_lock_voodoo(void) {
|
|
|
|
/* code taken from the Linux kernel, it handles shifting between locks */
|
|
|
|
/* Read/writer locks, as usual this is overly clever to make it as fast as possible. */
|
|
|
|
/* caches... */
|
|
|
|
__asm__ __volatile__(
|
|
|
|
"___rw_read_enter_spin_on_wlock:\n"
|
|
|
|
" orcc %g2, 0x0, %g0\n"
|
|
|
|
" be,a ___rw_read_enter\n"
|
|
|
|
" ldstub [%g1 + 3], %g2\n"
|
|
|
|
" b ___rw_read_enter_spin_on_wlock\n"
|
|
|
|
" ldub [%g1 + 3], %g2\n"
|
|
|
|
"___rw_read_exit_spin_on_wlock:\n"
|
|
|
|
" orcc %g2, 0x0, %g0\n"
|
|
|
|
" be,a ___rw_read_exit\n"
|
|
|
|
" ldstub [%g1 + 3], %g2\n"
|
|
|
|
" b ___rw_read_exit_spin_on_wlock\n"
|
|
|
|
" ldub [%g1 + 3], %g2\n"
|
|
|
|
"___rw_write_enter_spin_on_wlock:\n"
|
|
|
|
" orcc %g2, 0x0, %g0\n"
|
|
|
|
" be,a ___rw_write_enter\n"
|
|
|
|
" ldstub [%g1 + 3], %g2\n"
|
|
|
|
" b ___rw_write_enter_spin_on_wlock\n"
|
|
|
|
" ld [%g1], %g2\n"
|
|
|
|
"\n"
|
|
|
|
" .globl ___rw_read_enter\n"
|
|
|
|
"___rw_read_enter:\n"
|
|
|
|
" orcc %g2, 0x0, %g0\n"
|
|
|
|
" bne,a ___rw_read_enter_spin_on_wlock\n"
|
|
|
|
" ldub [%g1 + 3], %g2\n"
|
|
|
|
" ld [%g1], %g2\n"
|
|
|
|
" add %g2, 1, %g2\n"
|
|
|
|
" st %g2, [%g1]\n"
|
|
|
|
" retl\n"
|
|
|
|
" mov %g4, %o7\n"
|
|
|
|
" .globl ___rw_read_exit\n"
|
|
|
|
"___rw_read_exit:\n"
|
|
|
|
" orcc %g2, 0x0, %g0\n"
|
|
|
|
" bne,a ___rw_read_exit_spin_on_wlock\n"
|
|
|
|
" ldub [%g1 + 3], %g2\n"
|
|
|
|
" ld [%g1], %g2\n"
|
|
|
|
" sub %g2, 0x1ff, %g2\n"
|
|
|
|
" st %g2, [%g1]\n"
|
|
|
|
" retl\n"
|
|
|
|
" mov %g4, %o7\n"
|
|
|
|
" .globl ___rw_write_enter\n"
|
|
|
|
"___rw_write_enter:\n"
|
|
|
|
" orcc %g2, 0x0, %g0\n"
|
|
|
|
" bne ___rw_write_enter_spin_on_wlock\n"
|
|
|
|
" ld [%g1], %g2\n"
|
|
|
|
" andncc %g2, 0xff, %g0\n"
|
|
|
|
" bne,a ___rw_write_enter_spin_on_wlock\n"
|
|
|
|
" stb %g0, [%g1 + 3]\n"
|
|
|
|
" retl\n"
|
|
|
|
" mov %g4, %o7\n"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
#endif /* sparc */
|
|
|
|
|
|
|
|
|
|
|
|
#endif /* YAPOR || THREADS */
|