This commit is contained in:
Vitor Santos Costa 2016-07-31 10:26:36 -05:00
parent 9ad6bc3120
commit 0c305e8ce3

View File

@ -13,10 +13,22 @@
* *
*************************************************************************/
#include "config.h"
#include "YapInterface.h"
#include "crypto/md5.h"
#include "config.h"
#include <stdlib.h>
#if __ANDROID__
#include <android/asset_manager.h>
#include <android/asset_manager_jni.h>
#include <android/log.h>
#include <jni.h>
#endif
#include "YapStreams.h"
#include "VFS.h"
#include "crypto/md5.h"
#if HAVE_UNISTD_H
#include <unistd.h>
#endif
@ -58,9 +70,9 @@
#include <direct.h>
#endif
#if defined(__MINGW32__) || _MSC_VER
#include <windows.h>
#include <process.h>
#include <io.h>
#include <process.h>
#include <windows.h>
#endif
#ifdef __MINGW32__
#ifdef HAVE_ENVIRON
@ -76,23 +88,18 @@
void init_sys(void);
#if defined(__MINGW32__) || _MSC_VER
static YAP_Term
WinError(void)
{
static YAP_Term WinError(void) {
char msg[256];
/* Error, we could not read time */
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), msg, 256,
NULL);
NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
msg, 256, NULL);
return (YAP_MkAtomTerm(YAP_LookupAtom(msg)));
}
#endif
/* Return time in a structure */
static YAP_Bool
sysmktime(void)
{
static YAP_Bool sysmktime(void) {
#if defined(__MINGW32__) || _MSC_VER
SYSTEMTIME stime, stime0;
@ -120,8 +127,11 @@ sysmktime(void)
}
#if __GNUC__
{
unsigned long long f1 = (((unsigned long long)ftime.dwHighDateTime)<<32)+(unsigned long long)ftime.dwLowDateTime;
unsigned long long f0 = (((unsigned long long)ftime0.dwHighDateTime)<<32)+(unsigned long long)ftime0.dwLowDateTime;
unsigned long long f1 = (((unsigned long long)ftime.dwHighDateTime) << 32) +
(unsigned long long)ftime.dwLowDateTime;
unsigned long long f0 =
(((unsigned long long)ftime0.dwHighDateTime) << 32) +
(unsigned long long)ftime0.dwLowDateTime;
return YAP_Unify(YAP_ARG7, YAP_MkIntTerm((long int)((f1 - f0) / 10000000)));
}
#else
@ -151,9 +161,7 @@ sysmktime(void)
}
/* Return time in a structure */
static YAP_Bool
datime(void)
{
static YAP_Bool datime(void) {
YAP_Term tf, out[6];
#if defined(__MINGW32__) || _MSC_VER
SYSTEMTIME stime;
@ -196,9 +204,7 @@ datime(void)
#define BUF_SIZE 1024
/* Return a list of files for a directory */
static YAP_Bool
list_directory(void)
{
static YAP_Bool list_directory(void) {
YAP_Term tf = YAP_MkAtomTerm(YAP_LookupAtom("[]"));
long sl = YAP_InitSlot(tf);
@ -222,7 +228,8 @@ list_directory(void)
if ((hFile = _findfirst(bs, &c_file)) == -1L) {
return (YAP_Unify(YAP_ARG2, tf));
}
YAP_PutInSlot(sl, YAP_MkPairTerm(YAP_MkAtomTerm(YAP_LookupAtom(c_file.name)), YAP_GetFromSlot(sl)));
YAP_PutInSlot(sl, YAP_MkPairTerm(YAP_MkAtomTerm(YAP_LookupAtom(c_file.name)),
YAP_GetFromSlot(sl)));
while (_findnext(hFile, &c_file) == 0) {
YAP_Term ti = YAP_MkAtomTerm(YAP_LookupAtom(c_file.name));
YAP_PutInSlot(sl, YAP_MkPairTerm(ti, YAP_GetFromSlot(sl)));
@ -231,9 +238,8 @@ list_directory(void)
#else
#if __ANDROID__
{
extern AAssetManager *Yap_assetManager;
const char *dirName = buf + strlen("/assets/");
AAssetManager* mgr = Yap_assetManager;
AAssetManager *mgr = GLOBAL_VFS->priv[0].mgr;
AAssetDir *de;
const char *dp;
@ -267,9 +273,7 @@ list_directory(void)
return YAP_Unify(YAP_ARG2, tf);
}
static YAP_Bool
p_unlink(void)
{
static YAP_Bool p_unlink(void) {
char *fd = (char *)YAP_AtomName(YAP_AtomOfTerm(YAP_ARG1));
#if defined(__MINGW32__) || _MSC_VER
if (_unlink(fd) == -1)
@ -283,9 +287,7 @@ p_unlink(void)
return (TRUE);
}
static YAP_Bool
p_rmdir(void)
{
static YAP_Bool p_rmdir(void) {
char *fd = (char *)YAP_AtomName(YAP_AtomOfTerm(YAP_ARG1));
#if defined(__MINGW32__) || _MSC_VER
if (_rmdir(fd) == -1) {
@ -298,9 +300,7 @@ p_rmdir(void)
return (TRUE);
}
static YAP_Bool
rename_file(void)
{
static YAP_Bool rename_file(void) {
char *s1 = (char *)YAP_AtomName(YAP_AtomOfTerm(YAP_ARG1));
char *s2 = (char *)YAP_AtomName(YAP_AtomOfTerm(YAP_ARG2));
#if HAVE_RENAME
@ -312,15 +312,11 @@ rename_file(void)
return (TRUE);
}
static YAP_Bool
dir_separator(void)
{
static YAP_Bool dir_separator(void) {
return (YAP_Unify(YAP_ARG1, YAP_MkAtomTerm(YAP_LookupAtom("/"))));
}
static YAP_Bool
file_property(void)
{
static YAP_Bool file_property(void) {
const char *fd;
#if HAVE_LSTAT
struct stat buf;
@ -390,18 +386,14 @@ file_property(void)
return (FALSE);
}
#endif
return (
YAP_Unify(YAP_ARG3, YAP_MkIntTerm(buf.st_size)) &&
return (YAP_Unify(YAP_ARG3, YAP_MkIntTerm(buf.st_size)) &&
YAP_Unify(YAP_ARG4, YAP_MkIntTerm(buf.st_mtime)) &&
YAP_Unify(YAP_ARG5, YAP_MkIntTerm(buf.st_mode))
);
YAP_Unify(YAP_ARG5, YAP_MkIntTerm(buf.st_mode)));
}
/* temporary files */
static YAP_Bool
p_mktemp(void)
{
static YAP_Bool p_mktemp(void) {
#if HAVE_MKSTEMP || HAVE_MKTEMP || defined(__MINGW32__) || _MSC_VER
char *s, tmp[BUF_SIZE];
s = (char *)YAP_AtomName(YAP_AtomOfTerm(YAP_ARG1));
@ -436,9 +428,7 @@ p_mktemp(void)
return (TRUE);
}
static YAP_Bool
p_tmpnam(void)
{
static YAP_Bool p_tmpnam(void) {
#if HAVE_MKSTEMP
char s[21];
strcpy(s, "/tmp/YAP_tmpXXXXXXXX");
@ -460,9 +450,7 @@ p_tmpnam(void)
#endif
}
static YAP_Bool
p_tmpdir(void)
{
static YAP_Bool p_tmpdir(void) {
#if defined(__MINGW32__) || _MSC_VER
char buf[512];
DWORD out = GetTempPath(512, buf);
@ -472,7 +460,8 @@ p_tmpdir(void)
if (out > 511) {
char *nbuf = malloc(out + 1);
if (!nbuf)
return YAP_Unify(YAP_ARG2, YAP_MkAtomTerm(YAP_LookupAtom("no malloc memory")));
return YAP_Unify(YAP_ARG2,
YAP_MkAtomTerm(YAP_LookupAtom("no malloc memory")));
out = GetTempPath(512, nbuf);
if (!out) {
return YAP_Unify(YAP_ARG2, WinError());
@ -492,9 +481,7 @@ p_tmpdir(void)
}
/* return YAP's environment */
static YAP_Bool
p_environ(void)
{
static YAP_Bool p_environ(void) {
#if HAVE_ENVIRON && 0
#if HAVE__NSGETENVIRON
char **ptr = _NSGetEnviron();
@ -522,9 +509,7 @@ p_environ(void)
}
#if defined(__MINGW32__) || _MSC_VER
static HANDLE
get_handle(YAP_Term ti, DWORD fd)
{
static HANDLE get_handle(YAP_Term ti, DWORD fd) {
if (YAP_IsAtomTerm(ti)) {
HANDLE out;
SECURITY_ATTRIBUTES satt;
@ -532,25 +517,19 @@ get_handle(YAP_Term ti, DWORD fd)
satt.nLength = sizeof(satt);
satt.lpSecurityDescriptor = NULL;
satt.bInheritHandle = TRUE;
out = CreateFile("NUL",
GENERIC_READ|GENERIC_WRITE,
FILE_SHARE_READ|FILE_SHARE_WRITE,
&satt,
OPEN_EXISTING,
0,
NULL);
out = CreateFile("NUL", GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE, &satt, OPEN_EXISTING,
0, NULL);
return (out);
} else {
if (YAP_IsIntTerm(ti)) {
return (GetStdHandle(fd));
} else
return((HANDLE)YAP_StreamToFileNo(ti));
return ((HANDLE)_get_osfhandle(YAP_StreamToFileNo(ti)));
}
}
static void
close_handle(YAP_Term ti, HANDLE h)
{
static void close_handle(YAP_Term ti, HANDLE h) {
if (YAP_IsAtomTerm(ti)) {
CloseHandle(h);
}
@ -559,9 +538,7 @@ close_handle(YAP_Term ti, HANDLE h)
#endif
/* execute a command as a detached process */
static YAP_Bool
execute_command(void)
{
static YAP_Bool execute_command(void) {
YAP_Term ti = YAP_ARG2, to = YAP_ARG3, te = YAP_ARG4;
int res;
YAP_Term AtomNull = YAP_MkAtomTerm(YAP_LookupAtom("null"));
@ -601,15 +578,8 @@ execute_command(void)
StartupInfo.hStdOutput = outf;
StartupInfo.hStdError = errf;
/* got stdin, stdout and error as I like it */
if (CreateProcess(NULL,
(char *)YAP_AtomName(YAP_AtomOfTerm(YAP_ARG1)),
NULL,
NULL,
TRUE,
CreationFlags,
NULL,
NULL,
&StartupInfo,
if (CreateProcess(NULL, (char *)YAP_AtomName(YAP_AtomOfTerm(YAP_ARG1)), NULL,
NULL, TRUE, CreationFlags, NULL, NULL, &StartupInfo,
&ProcessInformation) == FALSE) {
close_handle(ti, inpf);
close_handle(to, outf);
@ -657,7 +627,8 @@ execute_command(void)
}
if (outf < 0) {
/* return an error number */
if (inpf != 0) close(inpf);
if (inpf != 0)
close(inpf);
return (YAP_Unify(YAP_ARG6, YAP_MkIntTerm(errno)));
}
/* then error stream */
@ -676,17 +647,22 @@ execute_command(void)
}
if (errf < 0) {
/* return an error number */
if (inpf != 0) close(inpf);
if (outf != 1) close(outf);
if (inpf != 0)
close(inpf);
if (outf != 1)
close(outf);
return (YAP_Unify(YAP_ARG6, YAP_MkIntTerm(errno)));
}
YAP_FlushAllStreams();
/* we are now ready to fork */
if ((res = fork()) < 0) {
/* close streams we don't need */
if (inpf != 0) close(inpf);
if (outf != 1) close(outf);
if (errf != 2) close(errf);
if (inpf != 0)
close(inpf);
if (outf != 1)
close(outf);
if (errf != 2)
close(errf);
/* return an error number */
return (YAP_Unify(YAP_ARG6, YAP_MkIntTerm(errno)));
} else if (res == 0) {
@ -721,18 +697,19 @@ execute_command(void)
exit(127);
/* we have the streams where we want them, just want to execute now */
} else {
if (inpf != 0) close(inpf);
if (outf != 1) close(outf);
if (errf != 2) close(errf);
if (inpf != 0)
close(inpf);
if (outf != 1)
close(outf);
if (errf != 2)
close(errf);
return (YAP_Unify(YAP_ARG5, YAP_MkIntTerm(res)));
}
#endif /* UNIX code */
}
/* execute a command as a detached process */
static YAP_Bool
do_system(void)
{
static YAP_Bool do_system(void) {
char *command = (char *)YAP_AtomName(YAP_AtomOfTerm(YAP_ARG1));
#if HAVE_SYSTEM
int sys = system(command);
@ -741,17 +718,14 @@ do_system(void)
}
return YAP_Unify(YAP_ARG2, YAP_MkIntTerm(sys));
#else
YAP_Error(0,0L,"system not available in this configuration, trying %s", command);
YAP_Error(0, 0L, "system not available in this configuration, trying %s",
command);
return FALSE;
#endif
}
/* execute a command as a detached process */
static YAP_Bool
do_shell(void)
{
static YAP_Bool do_shell(void) {
#if defined(__MINGW32__) || _MSC_VER
YAP_Error(0, 0L, "system not available in this configuration");
return (FALSE);
@ -807,9 +781,7 @@ do_shell(void)
}
/* execute a command as a detached process */
static YAP_Bool
plwait(void)
{
static YAP_Bool plwait(void) {
long int pid = YAP_IntOfTerm(YAP_ARG1);
#if defined(__MINGW32__) || _MSC_VER
HANDLE proc = OpenProcess(STANDARD_RIGHTS_REQUIRED | SYNCHRONIZE, FALSE, pid);
@ -851,9 +823,7 @@ plwait(void)
#endif
}
static YAP_Bool
p_sleep(void)
{
static YAP_Bool p_sleep(void) {
YAP_Term ts = YAP_ARG1;
#if defined(__MINGW32__) || _MSC_VER
{
@ -922,9 +892,7 @@ p_sleep(void)
/* host info */
static YAP_Bool
host_name(void)
{
static YAP_Bool host_name(void) {
#if defined(__MINGW32__) || _MSC_VER
char name[MAX_COMPUTERNAME_LENGTH + 1];
DWORD nSize = MAX_COMPUTERNAME_LENGTH + 1;
@ -943,9 +911,7 @@ host_name(void)
return (YAP_Unify(YAP_ARG1, YAP_MkAtomTerm(YAP_LookupAtom(name))));
}
static YAP_Bool
host_id(void)
{
static YAP_Bool host_id(void) {
#if HAVE_GETHOSTID
return (YAP_Unify(YAP_ARG1, YAP_MkIntTerm(gethostid())));
#else
@ -953,9 +919,7 @@ host_id(void)
#endif
}
static YAP_Bool
pid(void)
{
static YAP_Bool pid(void) {
#if defined(__MINGW32__) || _MSC_VER
return (YAP_Unify(YAP_ARG1, YAP_MkIntTerm(_getpid())));
#else
@ -963,9 +927,7 @@ pid(void)
#endif
}
static YAP_Bool
win(void)
{
static YAP_Bool win(void) {
#if defined(__MINGW32__) || _MSC_VER
return (TRUE);
#else
@ -973,14 +935,13 @@ win(void)
#endif
}
static YAP_Bool
p_kill(void)
{
static YAP_Bool p_kill(void) {
#if defined(__MINGW32__) || _MSC_VER
/* Windows does not support cross-process signals, so we shall do the
SICStus thing and assume that a signal to a process will
always kill it */
HANDLE proc = OpenProcess(STANDARD_RIGHTS_REQUIRED|PROCESS_TERMINATE, FALSE, YAP_IntOfTerm(YAP_ARG1));
HANDLE proc = OpenProcess(STANDARD_RIGHTS_REQUIRED | PROCESS_TERMINATE, FALSE,
YAP_IntOfTerm(YAP_ARG1));
if (proc == NULL) {
return (YAP_Unify(YAP_ARG3, WinError()));
}
@ -1010,16 +971,13 @@ p_kill(void)
*
* @return whether ARG1's md5 unifies with the difference liat.
*/
static YAP_Bool
md5(void)
{
static YAP_Bool md5(void) {
unsigned char buf[64];
md5_state_t pms;
const char *s;
size_t len = -1;
if ( ! (s = YAP_StringToBuffer( YAP_ARG1 , NULL, len )) ||
s[0] == 0)
if (!(s = YAP_StringToBuffer(YAP_ARG1, NULL, len)) || s[0] == 0)
return false;
md5_init(&pms);
@ -1028,8 +986,7 @@ p_kill(void)
// free((void *)s);
YAP_Term t = YAP_ARG3;
int i = 16;
while (i > 0)
{
while (i > 0) {
int top, bop;
i--;
top = buf[i] >> 4;
@ -1048,18 +1005,15 @@ p_kill(void)
return YAP_Unify(YAP_ARG2, t);
}
static YAP_Bool
error_message(void)
{
static YAP_Bool error_message(void) {
#if HAVE_STRERROR
return YAP_Unify(YAP_ARG2,YAP_MkAtomTerm(YAP_LookupAtom(strerror(YAP_IntOfTerm(YAP_ARG1)))));
return YAP_Unify(YAP_ARG2, YAP_MkAtomTerm(YAP_LookupAtom(
strerror(YAP_IntOfTerm(YAP_ARG1)))));
#else
return YAP_Unify(YAP_ARG2, YAP_ARG1);
#endif
}
void
init_sys(void)
{
void init_sys(void) {
#if HAVE_MKTIME
tzset();
#endif
@ -1095,10 +1049,8 @@ init_sys(void)
int WINAPI win_sys(HANDLE, DWORD, LPVOID);
int WINAPI win_sys(HANDLE hinst, DWORD reason, LPVOID reserved)
{
switch (reason)
{
int WINAPI win_sys(HANDLE hinst, DWORD reason, LPVOID reserved) {
switch (reason) {
case DLL_PROCESS_ATTACH:
break;
case DLL_PROCESS_DETACH:
@ -1111,4 +1063,3 @@ int WINAPI win_sys(HANDLE hinst, DWORD reason, LPVOID reserved)
return 1;
}
#endif