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: load_foreign.c *
|
|
|
|
* comments: dynamic loader of external routines *
|
|
|
|
* *
|
|
|
|
*************************************************************************/
|
|
|
|
#ifdef SCCS
|
2017-10-17 00:13:09 +01:00
|
|
|
static char SccsId[] = "%W% %G%.2";
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "Yap.h"
|
2009-10-23 14:22:17 +01:00
|
|
|
#include "YapHeap.h"
|
2013-12-08 19:12:24 +00:00
|
|
|
#include "YapText.h"
|
2017-10-17 00:13:09 +01:00
|
|
|
#include "Yatom.h"
|
|
|
|
#include "yapio.h"
|
2001-04-09 20:54:03 +01:00
|
|
|
#include <stdlib.h>
|
|
|
|
#if HAVE_STRING_H
|
|
|
|
#include <string.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "Foreign.h"
|
|
|
|
|
2002-11-11 17:38:10 +00:00
|
|
|
#if _WIN32 || defined(__CYGWIN__)
|
2013-10-29 12:43:31 +00:00
|
|
|
#ifndef SO_EXT
|
2013-11-13 15:10:15 +00:00
|
|
|
#define SO_EXT "dll"
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2017-10-17 00:13:09 +01:00
|
|
|
Int p_load_foreign(USES_REGS1);
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2017-10-17 00:13:09 +01:00
|
|
|
Int p_load_foreign(USES_REGS1) {
|
2008-01-23 17:57:56 +00:00
|
|
|
StringList ofiles = NULL;
|
|
|
|
StringList libs = NULL;
|
2001-04-09 20:54:03 +01:00
|
|
|
char *InitProcName;
|
|
|
|
YapInitProc InitProc = NULL;
|
|
|
|
Term t, t1;
|
|
|
|
StringList new;
|
2017-02-20 15:28:46 +00:00
|
|
|
bool returncode = FALSE;
|
2015-01-30 07:25:34 +00:00
|
|
|
yhandle_t CurSlot = Yap_StartSlots();
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2015-07-23 01:27:29 +01:00
|
|
|
// Yap_DebugPlWrite(ARG1); printf("%s\n", " \n");
|
2017-10-17 00:13:09 +01:00
|
|
|
// Yap_DebugPlWrite(ARG2); printf("%s\n", " \n");
|
|
|
|
// ap_DebugPlWrite(ARG3); printf("%s\n", " \n");
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
/* collect the list of object files */
|
|
|
|
t = Deref(ARG1);
|
2017-10-17 00:13:09 +01:00
|
|
|
while (1) {
|
|
|
|
if (t == TermNil)
|
|
|
|
break;
|
2001-04-09 20:54:03 +01:00
|
|
|
t1 = HeadOfTerm(t);
|
|
|
|
t = TailOfTerm(t);
|
2017-10-17 00:13:09 +01:00
|
|
|
new = (StringList)Yap_AllocCodeSpace(sizeof(StringListItem));
|
2001-04-09 20:54:03 +01:00
|
|
|
new->next = ofiles;
|
2010-12-12 18:28:55 +00:00
|
|
|
new->name = AtomOfTerm(t1);
|
2001-04-09 20:54:03 +01:00
|
|
|
ofiles = new;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* collect the list of library files */
|
|
|
|
t = Deref(ARG2);
|
2017-10-17 00:13:09 +01:00
|
|
|
while (1) {
|
|
|
|
if (t == TermNil)
|
|
|
|
break;
|
2001-04-09 20:54:03 +01:00
|
|
|
t1 = HeadOfTerm(t);
|
|
|
|
t = TailOfTerm(t);
|
2017-10-17 00:13:09 +01:00
|
|
|
new = (StringList)Yap_AllocCodeSpace(sizeof(StringListItem));
|
2001-04-09 20:54:03 +01:00
|
|
|
new->next = libs;
|
2010-12-12 18:28:55 +00:00
|
|
|
new->name = AtomOfTerm(t1);
|
2001-04-09 20:54:03 +01:00
|
|
|
libs = new;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* get the initialization function name */
|
|
|
|
t1 = Deref(ARG3);
|
2015-09-21 23:05:36 +01:00
|
|
|
InitProcName = (char *)RepAtom(AtomOfTerm(t1))->StrOfAE;
|
2017-02-20 15:28:46 +00:00
|
|
|
|
|
|
|
// verify if it was waiting for initialization
|
2017-10-17 00:13:09 +01:00
|
|
|
if (Yap_LateInit(InitProcName)) {
|
|
|
|
returncode = true;
|
|
|
|
} else
|
|
|
|
/* call the OS specific function for dynamic loading */
|
|
|
|
if (Yap_LoadForeign(ofiles, libs, InitProcName, &InitProc) ==
|
|
|
|
LOAD_SUCCEEDED) {
|
|
|
|
if (InitProc == NULL) {
|
|
|
|
char *f;
|
|
|
|
if (ofiles) {
|
|
|
|
f = RepAtom(ofiles->name)->StrOfAE;
|
|
|
|
} else {
|
|
|
|
f = RepAtom(libs->name)->StrOfAE;
|
|
|
|
}
|
|
|
|
Yap_Error(SYSTEM_ERROR_OPERATING_SYSTEM, ARG3,
|
|
|
|
"Foreign module %s does not have initialization function %s", f,
|
|
|
|
InitProcName);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
Yap_StartSlots();
|
2001-04-09 20:54:03 +01:00
|
|
|
(*InitProc)();
|
2015-02-03 02:36:51 +00:00
|
|
|
Yap_CloseSlots(CurSlot);
|
2017-02-20 15:28:46 +00:00
|
|
|
returncode = true;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
2017-10-17 00:13:09 +01:00
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
/* I should recover space if load foreign fails */
|
|
|
|
if (returncode == TRUE) {
|
2002-11-18 18:18:05 +00:00
|
|
|
ForeignObj *f_code = (ForeignObj *)Yap_AllocCodeSpace(sizeof(ForeignObj));
|
2001-04-09 20:54:03 +01:00
|
|
|
f_code->objs = ofiles;
|
|
|
|
f_code->libs = libs;
|
2012-06-11 22:11:30 +01:00
|
|
|
f_code->f = AtomOfTerm(t1);
|
2001-04-09 20:54:03 +01:00
|
|
|
f_code->next = ForeignCodeLoaded;
|
|
|
|
f_code->module = CurrentModule;
|
2010-04-12 17:21:50 +01:00
|
|
|
ForeignCodeLoaded = f_code;
|
2008-01-23 17:57:56 +00:00
|
|
|
} else {
|
|
|
|
while (ofiles) {
|
|
|
|
new = ofiles->next;
|
|
|
|
Yap_FreeCodeSpace((ADDR)ofiles);
|
|
|
|
ofiles = new;
|
|
|
|
}
|
|
|
|
while (libs) {
|
|
|
|
new = libs->next;
|
|
|
|
Yap_FreeCodeSpace((ADDR)libs);
|
|
|
|
libs = new;
|
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
return returncode;
|
|
|
|
}
|
|
|
|
|
2017-10-17 00:13:09 +01:00
|
|
|
static Int p_open_shared_object(USES_REGS1) {
|
2010-06-17 00:29:01 +01:00
|
|
|
Term t = Deref(ARG1);
|
|
|
|
Term tflags = Deref(ARG2);
|
2010-11-29 18:44:39 +00:00
|
|
|
char *s;
|
|
|
|
void *handle;
|
2010-06-17 00:29:01 +01:00
|
|
|
|
|
|
|
if (IsVarTerm(t)) {
|
2017-10-17 00:13:09 +01:00
|
|
|
Yap_Error(INSTANTIATION_ERROR, t, "open_shared_object/3");
|
2010-06-17 00:29:01 +01:00
|
|
|
return FALSE;
|
2017-10-17 00:13:09 +01:00
|
|
|
}
|
2010-06-17 00:29:01 +01:00
|
|
|
if (!IsAtomTerm(t)) {
|
2017-10-17 00:13:09 +01:00
|
|
|
Yap_Error(TYPE_ERROR_ATOM, t, "open_shared_object/3");
|
2010-06-17 00:29:01 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
2017-10-17 00:13:09 +01:00
|
|
|
|
2010-06-17 00:29:01 +01:00
|
|
|
if (IsVarTerm(tflags)) {
|
2017-10-17 00:13:09 +01:00
|
|
|
Yap_Error(INSTANTIATION_ERROR, tflags, "open_shared_object/3");
|
2010-06-17 00:29:01 +01:00
|
|
|
return FALSE;
|
2017-10-17 00:13:09 +01:00
|
|
|
}
|
2010-11-29 18:44:39 +00:00
|
|
|
if (!IsIntegerTerm(tflags)) {
|
2017-10-17 00:13:09 +01:00
|
|
|
Yap_Error(TYPE_ERROR_INTEGER, tflags, "open_shared_object/3");
|
2010-06-17 00:29:01 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
2017-10-17 00:13:09 +01:00
|
|
|
|
2015-09-21 23:05:36 +01:00
|
|
|
s = (char *)RepAtom(AtomOfTerm(t))->StrOfAE;
|
2017-10-17 00:13:09 +01:00
|
|
|
if ((handle = Yap_LoadForeignFile(s, IntegerOfTerm(tflags))) == NULL) {
|
|
|
|
Yap_Error(EXISTENCE_ERROR_SOURCE_SINK, t,
|
|
|
|
"open_shared_object_failed for %s"
|
|
|
|
" with %s\n",
|
|
|
|
s, LOCAL_ErrorMessage);
|
2010-11-29 18:44:39 +00:00
|
|
|
return FALSE;
|
2010-06-17 00:29:01 +01:00
|
|
|
} else {
|
2017-10-17 00:13:09 +01:00
|
|
|
return Yap_unify(MkIntegerTerm((Int)handle), ARG3);
|
2010-06-17 00:29:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-17 00:13:09 +01:00
|
|
|
static Int p_close_shared_object(USES_REGS1) {
|
2010-06-17 00:29:01 +01:00
|
|
|
Term t = Deref(ARG1);
|
|
|
|
void *handle;
|
|
|
|
|
|
|
|
if (IsVarTerm(t)) {
|
2017-10-17 00:13:09 +01:00
|
|
|
Yap_Error(INSTANTIATION_ERROR, t, "close_shared_object/1");
|
2010-06-17 00:29:01 +01:00
|
|
|
return FALSE;
|
2017-10-17 00:13:09 +01:00
|
|
|
}
|
2010-06-17 00:29:01 +01:00
|
|
|
if (!IsIntegerTerm(t)) {
|
2017-10-17 00:13:09 +01:00
|
|
|
Yap_Error(TYPE_ERROR_INTEGER, t, "close_shared_object/1");
|
2010-06-17 00:29:01 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
2010-11-29 18:44:39 +00:00
|
|
|
handle = (char *)IntegerOfTerm(t);
|
2017-10-17 00:13:09 +01:00
|
|
|
|
2010-11-29 18:44:39 +00:00
|
|
|
return Yap_CloseForeignFile(handle);
|
2010-06-17 00:29:01 +01:00
|
|
|
}
|
|
|
|
|
2017-10-17 00:13:09 +01:00
|
|
|
static Int p_call_shared_object_function(USES_REGS1) {
|
2010-06-17 00:29:01 +01:00
|
|
|
Term t = Deref(ARG1);
|
|
|
|
Term tfunc = Deref(ARG2);
|
2010-12-01 19:30:58 +00:00
|
|
|
Term tmod;
|
2010-06-17 00:29:01 +01:00
|
|
|
void *handle;
|
2010-12-01 19:30:58 +00:00
|
|
|
Term OldCurrentModule = CurrentModule;
|
|
|
|
Int res;
|
2010-06-17 00:29:01 +01:00
|
|
|
|
2010-12-01 19:30:58 +00:00
|
|
|
tmod = CurrentModule;
|
2017-10-17 00:13:09 +01:00
|
|
|
restart:
|
2010-06-17 00:29:01 +01:00
|
|
|
if (IsVarTerm(t)) {
|
2017-10-17 00:13:09 +01:00
|
|
|
Yap_Error(INSTANTIATION_ERROR, t, "call_shared_object_function/2");
|
2010-06-17 00:29:01 +01:00
|
|
|
return FALSE;
|
2010-12-01 19:30:58 +00:00
|
|
|
} else if (IsApplTerm(t)) {
|
2017-10-17 00:13:09 +01:00
|
|
|
Functor fun = FunctorOfTerm(t);
|
2010-12-01 19:30:58 +00:00
|
|
|
if (fun == FunctorModule) {
|
|
|
|
tmod = ArgOfTerm(1, t);
|
2017-10-17 00:13:09 +01:00
|
|
|
if (IsVarTerm(tmod)) {
|
|
|
|
Yap_Error(INSTANTIATION_ERROR, t, "call_shared_object_function/2");
|
|
|
|
return FALSE;
|
2010-12-01 19:30:58 +00:00
|
|
|
}
|
2017-10-17 00:13:09 +01:00
|
|
|
if (!IsAtomTerm(tmod)) {
|
|
|
|
Yap_Error(TYPE_ERROR_ATOM, ARG1, "call_shared_object_function/2");
|
|
|
|
return FALSE;
|
2010-12-01 19:30:58 +00:00
|
|
|
}
|
|
|
|
t = ArgOfTerm(2, t);
|
|
|
|
goto restart;
|
|
|
|
}
|
|
|
|
} else if (!IsIntegerTerm(t)) {
|
2017-10-17 00:13:09 +01:00
|
|
|
Yap_Error(TYPE_ERROR_INTEGER, t, "call_shared_object_function/2");
|
2010-06-17 00:29:01 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
2010-11-29 18:44:39 +00:00
|
|
|
handle = (void *)IntegerOfTerm(t);
|
2010-06-17 00:29:01 +01:00
|
|
|
if (IsVarTerm(tfunc)) {
|
2017-10-17 00:13:09 +01:00
|
|
|
Yap_Error(INSTANTIATION_ERROR, t, "call_shared_object_function/2");
|
2010-06-17 00:29:01 +01:00
|
|
|
return FALSE;
|
2017-10-17 00:13:09 +01:00
|
|
|
}
|
2010-06-17 00:29:01 +01:00
|
|
|
if (!IsAtomTerm(tfunc)) {
|
2017-10-17 00:13:09 +01:00
|
|
|
Yap_Error(TYPE_ERROR_ATOM, t, "call_shared_object_function/2/3");
|
2010-06-17 00:29:01 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
2010-12-01 19:30:58 +00:00
|
|
|
CurrentModule = tmod;
|
2017-10-17 00:13:09 +01:00
|
|
|
res =
|
|
|
|
Yap_CallForeignFile(handle, (char *)RepAtom(AtomOfTerm(tfunc))->StrOfAE);
|
2010-12-01 19:30:58 +00:00
|
|
|
CurrentModule = OldCurrentModule;
|
|
|
|
return res;
|
2010-06-17 00:29:01 +01:00
|
|
|
}
|
|
|
|
|
2017-10-17 00:13:09 +01:00
|
|
|
static Int p_obj_suffix(USES_REGS1) {
|
|
|
|
return Yap_unify(Yap_CharsToListOfCodes(SO_EXT, ENC_ISO_LATIN1 PASS_REGS),
|
|
|
|
ARG1);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
2017-10-17 00:13:09 +01:00
|
|
|
static Int p_open_shared_objects(USES_REGS1) {
|
2013-10-29 12:43:31 +00:00
|
|
|
#ifdef SO_EXT
|
2010-06-14 22:43:15 +01:00
|
|
|
return TRUE;
|
|
|
|
#else
|
|
|
|
return FALSE;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2017-10-17 00:13:09 +01:00
|
|
|
void Yap_InitLoadForeign(void) {
|
|
|
|
Yap_InitCPred("$load_foreign_files", 3, p_load_foreign,
|
|
|
|
SafePredFlag | SyncPredFlag);
|
2012-10-19 18:10:48 +01:00
|
|
|
Yap_InitCPred("$open_shared_objects", 0, p_open_shared_objects, SafePredFlag);
|
|
|
|
Yap_InitCPred("$open_shared_object", 3, p_open_shared_object, SyncPredFlag);
|
2017-10-17 00:13:09 +01:00
|
|
|
Yap_InitCPred("close_shared_object", 1, p_close_shared_object,
|
|
|
|
SyncPredFlag | SafePredFlag);
|
|
|
|
/** @pred close_shared_object(+ _Handle_)
|
2014-09-11 20:06:57 +01:00
|
|
|
|
2017-10-17 00:13:09 +01:00
|
|
|
Detach the shared object identified by _Handle_.
|
2014-09-11 20:06:57 +01:00
|
|
|
|
2017-10-17 00:13:09 +01:00
|
|
|
|
|
|
|
*/
|
|
|
|
Yap_InitCPred("$call_shared_object_function", 2,
|
|
|
|
p_call_shared_object_function, SyncPredFlag);
|
2012-10-19 18:10:48 +01:00
|
|
|
Yap_InitCPred("$obj_suffix", 1, p_obj_suffix, SafePredFlag);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
2017-10-17 00:13:09 +01:00
|
|
|
void Yap_ReOpenLoadForeign(void) {
|
2011-03-07 16:02:55 +00:00
|
|
|
CACHE_REGS
|
2001-04-09 20:54:03 +01:00
|
|
|
ForeignObj *f_code = ForeignCodeLoaded;
|
2004-02-12 12:37:12 +00:00
|
|
|
Term OldModule = CurrentModule;
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
while (f_code != NULL) {
|
2010-12-07 18:21:10 +00:00
|
|
|
YapInitProc InitProc = NULL;
|
|
|
|
|
2001-11-15 00:01:43 +00:00
|
|
|
CurrentModule = f_code->module;
|
2017-10-17 00:13:09 +01:00
|
|
|
if (Yap_ReLoadForeign(f_code->objs, f_code->libs,
|
|
|
|
(char *)RepAtom(f_code->f)->StrOfAE,
|
|
|
|
&InitProc) == LOAD_SUCCEEDED) {
|
2010-11-22 18:07:01 +00:00
|
|
|
if (InitProc)
|
2017-10-17 00:13:09 +01:00
|
|
|
(*InitProc)();
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
f_code = f_code->next;
|
|
|
|
}
|
2001-11-15 00:01:43 +00:00
|
|
|
CurrentModule = OldModule;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|