This commit is contained in:
Vitor Santos Costa 2017-11-29 13:47:57 +00:00
parent 8feca162bf
commit 3688819ea2
31 changed files with 2944 additions and 2897 deletions

View File

@ -209,8 +209,8 @@ bool YAP_get_blob(Term t, void **blob, size_t *len, blob_type_t **type) {
return TRUE; return TRUE;
} }
void *YAP_blob_data(Atom x, size_t *len, blob_type_t **type) { void *YAP_blob_data(YAP_Atom at, size_t *len, blob_type_t **type) {
Atom x = at;
if (!IsBlob(x)) { if (!IsBlob(x)) {
if (len) if (len)

View File

@ -33,7 +33,6 @@
#include <unistd.h> #include <unistd.h>
#endif #endif
#include <stdlib.h>
#include <string.h> #include <string.h>
#if HAVE_STDARG_H #if HAVE_STDARG_H
@ -62,7 +61,6 @@
#endif /* YAPOR */ #endif /* YAPOR */
#include "cut_c.h" #include "cut_c.h"
#include "threads.h"
#if HAVE_MALLOC_H #if HAVE_MALLOC_H
@ -428,16 +426,16 @@ X_API Term YAP_MkFloatTerm(double n) {
X_API YAP_Float YAP_FloatOfTerm(YAP_Term t) { return (FloatOfTerm(t)); } X_API YAP_Float YAP_FloatOfTerm(YAP_Term t) { return (FloatOfTerm(t)); }
X_API Term YAP_MkAtomTerm(Atom n) { X_API Term YAP_MkAtomTerm(YAP_Atom n) {
Term t; Term t;
t = MkAtomTerm(n); t = MkAtomTerm(n);
return t; return t;
} }
X_API Atom YAP_AtomOfTerm(Term t) { return (AtomOfTerm(t)); } X_API YAP_Atom YAP_AtomOfTerm(Term t) { return (AtomOfTerm(t)); }
X_API bool YAP_IsWideAtom(Atom a) { X_API bool YAP_IsWideAtom(YAP_Atom a) {
const unsigned char *s = RepAtom(a)->UStrOfAE; const unsigned char *s = RepAtom(a)->UStrOfAE;
int32_t v; int32_t v;
while (*s) { while (*s) {
@ -448,14 +446,14 @@ X_API bool YAP_IsWideAtom(Atom a) {
return false; return false;
} }
X_API const char *YAP_AtomName(Atom a) { X_API const char *YAP_AtomName(YAP_Atom a) {
const char *o; const char *o;
o = AtomName(a); o = AtomName(a);
return (o); return (o);
} }
X_API const wchar_t *YAP_WideAtomName(Atom a) { X_API const wchar_t *YAP_WideAtomName(YAP_Atom a) {
int32_t v; int32_t v;
const unsigned char *s = RepAtom(a)->UStrOfAE; const unsigned char *s = RepAtom(a)->UStrOfAE;
size_t n = strlen_utf8(s); size_t n = strlen_utf8(s);
@ -470,7 +468,7 @@ X_API const wchar_t *YAP_WideAtomName(Atom a) {
return dest; return dest;
} }
X_API Atom YAP_LookupAtom(const char *c) { X_API YAP_Atom YAP_LookupAtom(const char *c) {
CACHE_REGS CACHE_REGS
Atom a; Atom a;
@ -488,7 +486,7 @@ X_API Atom YAP_LookupAtom(const char *c) {
return NULL; return NULL;
} }
X_API Atom YAP_LookupWideAtom(const wchar_t *c) { X_API YAP_Atom YAP_LookupWideAtom(const wchar_t *c) {
CACHE_REGS CACHE_REGS
Atom a; Atom a;
@ -506,7 +504,7 @@ X_API Atom YAP_LookupWideAtom(const wchar_t *c) {
return NULL; return NULL;
} }
X_API Atom YAP_FullLookupAtom(const char *c) { X_API YAP_Atom YAP_FullLookupAtom(const char *c) {
CACHE_REGS CACHE_REGS
Atom at; Atom at;
@ -524,7 +522,7 @@ X_API Atom YAP_FullLookupAtom(const char *c) {
return NULL; return NULL;
} }
X_API size_t YAP_AtomNameLength(Atom at) { X_API size_t YAP_AtomNameLength(YAP_Atom at) {
if (IsBlob(at)) { if (IsBlob(at)) {
return RepAtom(at)->rep.blob->length; return RepAtom(at)->rep.blob->length;
} }
@ -648,7 +646,7 @@ X_API Int YAP_SkipList(Term *l, Term **tailp) {
return length; return length;
} }
X_API Term YAP_MkApplTerm(Functor f, UInt arity, Term args[]) { X_API Term YAP_MkApplTerm(YAP_Functor f, UInt arity, Term args[]) {
CACHE_REGS CACHE_REGS
Term t; Term t;
BACKUP_H(); BACKUP_H();
@ -662,7 +660,7 @@ X_API Term YAP_MkApplTerm(Functor f, UInt arity, Term args[]) {
return t; return t;
} }
X_API Term YAP_MkNewApplTerm(Functor f, UInt arity) { X_API Term YAP_MkNewApplTerm(YAP_Functor f, UInt arity) {
CACHE_REGS CACHE_REGS
Term t; Term t;
BACKUP_H(); BACKUP_H();
@ -676,7 +674,7 @@ X_API Term YAP_MkNewApplTerm(Functor f, UInt arity) {
return t; return t;
} }
X_API Functor YAP_FunctorOfTerm(Term t) { return (FunctorOfTerm(t)); } X_API YAP_Functor YAP_FunctorOfTerm(Term t) { return (FunctorOfTerm(t)); }
X_API Term YAP_ArgOfTerm(UInt n, Term t) { return (ArgOfTerm(n, t)); } X_API Term YAP_ArgOfTerm(UInt n, Term t) { return (ArgOfTerm(n, t)); }
@ -688,11 +686,11 @@ X_API Term *YAP_ArgsOfTerm(Term t) {
return NULL; return NULL;
} }
X_API Functor YAP_MkFunctor(Atom a, UInt n) { return (Yap_MkFunctor(a, n)); } X_API YAP_Functor YAP_MkFunctor(YAP_Atom a, UInt n) { return (Yap_MkFunctor(a, n)); }
X_API Atom YAP_NameOfFunctor(Functor f) { return (NameOfFunctor(f)); } X_API YAP_Atom YAP_NameOfFunctor(YAP_Functor f) { return (NameOfFunctor(f)); }
X_API UInt YAP_ArityOfFunctor(Functor f) { return (ArityOfFunctor(f)); } X_API UInt YAP_ArityOfFunctor(YAP_Functor f) { return (ArityOfFunctor(f)); }
X_API void *YAP_ExtraSpaceCut(void) { X_API void *YAP_ExtraSpaceCut(void) {
CACHE_REGS CACHE_REGS
@ -1703,21 +1701,21 @@ X_API void YAP_Error(int myerrno, Term t, const char *buf, ...) {
Yap_Error(myerrno, t, tmpbuf); Yap_Error(myerrno, t, tmpbuf);
} }
X_API PredEntry *YAP_FunctorToPred(Functor func) { X_API YAP_PredEntryPtr YAP_FunctorToPred(YAP_Functor func) {
CACHE_REGS CACHE_REGS
return RepPredProp(PredPropByFunc(func, CurrentModule)); return RepPredProp(PredPropByFunc(func, CurrentModule));
} }
X_API PredEntry *YAP_AtomToPred(Atom at) { X_API YAP_PredEntryPtr YAP_AtomToPred(YAP_Atom at) {
CACHE_REGS CACHE_REGS
return RepPredProp(PredPropByAtom(at, CurrentModule)); return RepPredProp(PredPropByAtom(at, CurrentModule));
} }
X_API PredEntry *YAP_FunctorToPredInModule(Functor func, Term mod) { X_API YAP_PredEntryPtr YAP_FunctorToPredInModule(YAP_Functor func, Term mod) {
return RepPredProp(PredPropByFunc(func, mod)); return RepPredProp(PredPropByFunc(func, mod));
} }
X_API PredEntry *YAP_AtomToPredInModule(Atom at, Term mod) { X_API YAP_PredEntryPtr YAP_AtomToPredInModule(YAP_Atom at, Term mod) {
return RepPredProp(PredPropByAtom(at, mod)); return RepPredProp(PredPropByAtom(at, mod));
} }
@ -1729,8 +1727,9 @@ static int run_emulator(USES_REGS1) {
return out; return out;
} }
X_API bool YAP_EnterGoal(PredEntry *pe, CELL *ptr, YAP_dogoalinfo *dgi) { X_API bool YAP_EnterGoal(YAP_PredEntryPtr ape, CELL *ptr, YAP_dogoalinfo *dgi) {
CACHE_REGS CACHE_REGS
PredEntry *pe = ape;
bool out; bool out;
BACKUP_MACHINE_REGS(); BACKUP_MACHINE_REGS();
@ -1920,7 +1919,8 @@ X_API Term YAP_NewOpaqueObject(YAP_opaque_tag_t blob_tag, size_t bytes) {
blob_tag = pt[1]; blob_tag = pt[1];
if (blob_tag < USER_BLOB_START || if (blob_tag < USER_BLOB_START ||
blob_tag >= USER_BLOB_END) { blob_tag >= USER_BLOB_END) {
Yap_Error(SYSTEM_ERROR_INTERNAL, AbsAppl(pt), "clean opaque: bad blob with tag " UInt_FORMAT ,blob_tag); Yap_Error(SYSTEM_ERROR_INTERNAL, AbsAppl(pt), "clean opaque: bad blob with tag "
UInt_FORMAT, blob_tag);
return FALSE; return FALSE;
} }
YAP_opaque_tag_t blob_info = blob_tag; YAP_opaque_tag_t blob_info = blob_tag;
@ -2133,12 +2133,13 @@ X_API int YAP_InitConsult(int mode, const char *fname, char *full,
return -1; return -1;
} }
Yap_init_consult(consulted, bfp); Yap_init_consult(consulted, bfp);
sno = Yap_OpenStream(fl, "r" ); sno = Yap_OpenStream(fl, AtomRead);
*osnop = Yap_CheckAlias(AtomLoopStream); *osnop = Yap_CheckAlias(AtomLoopStream);
if (!Yap_AddAlias(AtomLoopStream, sno)) { if (!Yap_AddAlias(AtomLoopStream, sno)) {
Yap_CloseStream(sno); Yap_CloseStream(sno);
pop_text_stack(lvl); pop_text_stack(lvl);
sno = -1; sno = -1;
return sno;
} }
GLOBAL_Stream[sno].name = Yap_LookupAtom(fl); GLOBAL_Stream[sno].name = Yap_LookupAtom(fl);
GLOBAL_Stream[sno].user_name = MkAtomTerm(Yap_LookupAtom(fname)); GLOBAL_Stream[sno].user_name = MkAtomTerm(Yap_LookupAtom(fname));
@ -2151,8 +2152,7 @@ X_API int YAP_InitConsult(int mode, const char *fname, char *full,
/// given a stream descriptor or stream alias (see open/3), /// given a stream descriptor or stream alias (see open/3),
/// return YAP's internal handle. /// return YAP's internal handle.
X_API void *YAP_GetStreamFromId(int no) X_API void *YAP_GetStreamFromId(int no) {
{
return GLOBAL_Stream + no; return GLOBAL_Stream + no;
} }
@ -2195,18 +2195,17 @@ X_API Term YAP_ReadFromStream(int sno) {
Term o; Term o;
BACKUP_MACHINE_REGS(); BACKUP_MACHINE_REGS();
o = Yap_read_term(sno, TermNil, 1); o = Yap_read_term(sno, TermNil, false);
RECOVER_MACHINE_REGS(); RECOVER_MACHINE_REGS();
return o; return o;
} }
X_API Term YAP_ReadClauseFromStream(int sno) { X_API Term YAP_ReadClauseFromStream(int sno) {
Term o;
BACKUP_MACHINE_REGS(); BACKUP_MACHINE_REGS();
o = Yap_read_term(sno, TermNil, -1); Term t = Yap_read_term(sno, TermNil, true);
RECOVER_MACHINE_REGS(); RECOVER_MACHINE_REGS();
return o; return t;
} }
X_API void YAP_Write(Term t, FILE *f, int flags) { X_API void YAP_Write(Term t, FILE *f, int flags) {
@ -2253,7 +2252,8 @@ X_API char *YAP_WriteBuffer(Term t, char *buf, size_t sze, int flags) {
} else { } else {
return pop_output_text_stack(l, out.val.c); return pop_output_text_stack(l, out.val.c);
} }
}} }
}
/// write a a term to n user-provided buffer: make sure not tp /// write a a term to n user-provided buffer: make sure not tp
@ -2307,7 +2307,7 @@ static int yap_lineno = 0;
/* do initial boot by consulting the file boot.yap */ /* do initial boot by consulting the file boot.yap */
static void do_bootfile(const char *b_file USES_REGS) { static void do_bootfile(const char *b_file USES_REGS) {
Term t; Term t;
int bootfile, osno; int boot_stream, osno;
Functor functor_query = Yap_MkFunctor(Yap_LookupAtom("?-"), 1); Functor functor_query = Yap_MkFunctor(Yap_LookupAtom("?-"), 1);
Functor functor_command1 = Yap_MkFunctor(Yap_LookupAtom(":-"), 1); Functor functor_command1 = Yap_MkFunctor(Yap_LookupAtom(":-"), 1);
@ -2315,9 +2315,9 @@ static void do_bootfile(const char *b_file USES_REGS) {
char *full = malloc(YAP_FILENAME_MAX + 1); char *full = malloc(YAP_FILENAME_MAX + 1);
full[0] = '\0'; full[0] = '\0';
/* the consult mode does not matter here, really */ /* the consult mode does not matter here, really */
bootfile = YAP_InitConsult(YAP_BOOT_MODE, b_file, full, &osno); boot_stream = YAP_InitConsult(YAP_BOOT_MODE, b_file, full, &osno);
if (bootfile < 0) { if (boot_stream < 0) {
fprintf(stderr, "[ FATAL ERROR: could not open bootfile %s ]\n", fprintf(stderr, "[ FATAL ERROR: could not open boot_stream %s ]\n",
b_file); b_file);
exit(1); exit(1);
} }
@ -2326,19 +2326,19 @@ free(full);
CACHE_REGS CACHE_REGS
YAP_Reset(YAP_FULL_RESET); YAP_Reset(YAP_FULL_RESET);
Yap_StartSlots(); Yap_StartSlots();
t = YAP_ReadClauseFromStream(bootfile); t = YAP_ReadClauseFromStream(boot_stream);
// Yap_DebugPlWriteln(t); // Yap_DebugPlWriteln(t);
if (t == 0) { if (t == 0) {
fprintf(stderr, fprintf(stderr,
"[ SYNTAX ERROR: while parsing bootfile %s at line %d ]\n", "[ SYNTAX ERROR: while parsing boot_stream %s at line %d ]\n",
b_file, yap_lineno); b_file, yap_lineno);
} else if (YAP_IsVarTerm(t) || t == TermNil) { } else if (YAP_IsVarTerm(t) || t == TermNil) {
fprintf(stderr, "[ line %d: term cannot be compiled ]", yap_lineno); fprintf(stderr, "[ line %d: term cannot be compiled ]", yap_lineno);
} else if (YAP_IsPairTerm(t)) { } else if (YAP_IsPairTerm(t)) {
fprintf(stderr, "[ SYSTEM ERROR: consult not allowed in boot file ]\n"); fprintf(stderr, "[ SYSTEM ERROR: consult not allowed in boot file ]\n");
fprintf(stderr, "error found at line %d and pos %d", yap_lineno, fprintf(stderr, "error found at line %d and pos %d", yap_lineno,
fseek(GLOBAL_Stream[bootfile].file, 0L, SEEK_CUR)); fseek(GLOBAL_Stream[boot_stream].file, 0L, SEEK_CUR));
} else if (IsApplTerm(t) && (FunctorOfTerm(t) == functor_query || } else if (IsApplTerm(t) && (FunctorOfTerm(t) == functor_query ||
FunctorOfTerm(t) == functor_command1)) { FunctorOfTerm(t) == functor_command1)) {
YAP_RunGoalOnce(ArgOfTerm(1, t)); YAP_RunGoalOnce(ArgOfTerm(1, t));
@ -2361,7 +2361,7 @@ free(full);
} }
} while (t != TermEof); } while (t != TermEof);
YAP_EndConsult(bootfile, &osno); YAP_EndConsult(boot_stream, &osno);
#if DEBUG #if DEBUG
if (Yap_output_msg) if (Yap_output_msg)
fprintf(stderr, "Boot loaded\n"); fprintf(stderr, "Boot loaded\n");
@ -2425,12 +2425,16 @@ free(full);
CELL Trail = 0, Stack = 0, Heap = 0, Atts = 0; CELL Trail = 0, Stack = 0, Heap = 0, Atts = 0;
char *boot_file; char *boot_file;
Int rc; Int rc;
const char *yroot; char *yroot;
boot_file = calloc(YAP_FILENAME_MAX + 1, 1);
/* ignore repeated calls to YAP_Init */
if (YAP_initialized) if (YAP_initialized)
return YAP_FOUND_BOOT_ERROR; return YAP_FOUND_BOOT_ERROR;
if (!LOCAL_TextBuffer)
LOCAL_TextBuffer = Yap_InitTextAllocator();
int lvl = push_text_stack();
yroot = Malloc(YAP_FILENAME_MAX + 1);
boot_file = Malloc(YAP_FILENAME_MAX + 1);
/* ignore repeated calls to YAP_Init */
Yap_embedded = yap_init->Embedded; Yap_embedded = yap_init->Embedded;
Yap_page_size = Yap_InitPageSize(); /* init memory page size, required by Yap_page_size = Yap_InitPageSize(); /* init memory page size, required by
later functions */ later functions */
@ -2446,25 +2450,28 @@ free(full);
GLOBAL_argc = yap_init->Argc; GLOBAL_argc = yap_init->Argc;
if (0 && ((YAP_QLY && yap_init->SavedState) || if (0 && ((YAP_QLY && yap_init->SavedState) ||
(YAP_BOOT_PL && (yap_init->YapPrologBootFile)))) { (YAP_BOOT_PL && (yap_init->YapPrologBootFile)))) {
yroot = "."; strcpy(yroot, ".");
} else { } else {
yroot = BootFilePath; strcpy(yroot, BootFile);
} }
} }
char *tmp = NULL;
if (yap_init->SavedState == NULL) { if (yap_init->SavedState == NULL) {
yap_init->SavedState = YAP_STARTUP; tmp = Malloc(strlen(YAP_STARTUP) + 1);
strncpy(tmp, YAP_STARTUP, strlen(YAP_STARTUP) + 1);;
} }
if (!LOCAL_TextBuffer)
LOCAL_TextBuffer = Yap_InitTextAllocator();
#if __ANDROID__ #if __ANDROID__
//if (yap_init->assetManager) //if (yap_init->assetManager)
Yap_InitAssetManager(); Yap_InitAssetManager();
#endif #endif
#if USE_DL_MALLOC #if USE_DL_MALLOC
if (yap_init->SavedState == NULL) if (yap_init->SavedState == NULL)
yap_init->SavedState = YAP_STARTUP; yap_init->SavedState = YAP_STARTUP;
#else #else
yap_init->SavedState = Yap_findFile(yap_init->SavedState, YAP_STARTUP, yap_init->YapLibDir, yap_init->SavedState = Yap_findFile(tmp, YAP_STARTUP, yap_init->YapLibDir,
boot_file, true, YAP_QLY, true, true); boot_file, true, YAP_QLY, true, true);
#endif #endif
if (yap_init->SavedState == NULL) { if (yap_init->SavedState == NULL) {
@ -2473,13 +2480,19 @@ LOCAL_TextBuffer = Yap_InitTextAllocator();
if (restore_result == YAP_BOOT_PL) { if (restore_result == YAP_BOOT_PL) {
#if USE_DL_MALLOC #if USE_DL_MALLOC
if (yap_init->YapPrologBootFile == NULL) if (yap_init->YapPrologBootFile == NULL ||
yap_init->YapPrologBootFile[0] == 0)
yap_init->YapPrologBootFile = BootFile; yap_init->YapPrologBootFile = BootFile;
strcpy(boot_file, BootFile);
}
#else #else
strcpy(boot_file, BootFile);
const char *s = Yap_findFile(yap_init->YapPrologBootFile, BootFile, yroot, boot_file, const char *s = Yap_findFile(yap_init->YapPrologBootFile,
BootFile, yroot, boot_file,
true, YAP_BOOT_PL, true, true); true, YAP_BOOT_PL, true, true);
if (s && s[0] != '\0') strcpy(boot_file, s); if (s && s[0] != '\0') {
strcpy(boot_file, s);
}
#endif #endif
} }
@ -2504,7 +2517,8 @@ LOCAL_TextBuffer = Yap_InitTextAllocator();
} }
#endif #endif
Yap_InitWorkspace(yap_init, Heap, Stack, Trail, Atts, yap_init->MaxTableSpaceSize, Yap_InitWorkspace(yap_init, Heap, Stack, Trail, Atts, yap_init
->MaxTableSpaceSize,
yap_init->NumberWorkers, yap_init->SchedulerLoop, yap_init->NumberWorkers, yap_init->SchedulerLoop,
yap_init->DelayedReleaseLoad); yap_init->DelayedReleaseLoad);
// //
@ -2521,15 +2535,19 @@ LOCAL_TextBuffer = Yap_InitTextAllocator();
restore will print out messages .... restore will print out messages ....
*/ */
setBooleanGlobalPrologFlag(HALT_AFTER_CONSULT_FLAG, setBooleanGlobalPrologFlag(HALT_AFTER_CONSULT_FLAG,
yap_init->HaltAfterConsult); yap_init
->HaltAfterConsult);
} }
/* tell the system who should cope with interrupts */ /* tell the system who should cope with interrupts */
Yap_ExecutionMode = yap_init->ExecutionMode; Yap_ExecutionMode = yap_init->ExecutionMode;
if (do_bootstrap) { if (do_bootstrap) {
restore_result |= YAP_BOOT_PL; restore_result |=
YAP_BOOT_PL;
} else { // try always to boot from the saved state. } else { // try always to boot from the saved state.
if (restore_result == YAP_QLY) { if (restore_result == YAP_QLY) {
if (!Yap_SavedInfo(yap_init->SavedState, yap_init->YapLibDir, &Trail, if (!
Yap_SavedInfo(yap_init
->SavedState, yap_init->YapLibDir, &Trail,
&Stack, &Heap)) { &Stack, &Heap)) {
restore_result = YAP_BOOT_PL; restore_result = YAP_BOOT_PL;
} else { } else {
@ -2543,7 +2561,9 @@ LOCAL_TextBuffer = Yap_InitTextAllocator();
} }
GLOBAL_FAST_BOOT_FLAG = yap_init->FastBoot; GLOBAL_FAST_BOOT_FLAG = yap_init->FastBoot;
#if defined(YAPOR) || defined(TABLING) #if defined(YAPOR) || defined(TABLING)
Yap_init_root_frames(); Yap_init_root_frames();
#endif /* YAPOR || TABLING */ #endif /* YAPOR || TABLING */
#ifdef YAPOR #ifdef YAPOR
Yap_init_yapor_workers(); Yap_init_yapor_workers();
@ -2590,56 +2610,73 @@ LOCAL_TextBuffer = Yap_InitTextAllocator();
} }
if (yap_init->YapPrologRCFile) { if (yap_init->YapPrologRCFile) {
Yap_PutValue(AtomConsultOnBoot, Yap_PutValue(AtomConsultOnBoot,
MkAtomTerm(Yap_LookupAtom(yap_init->YapPrologRCFile))); MkAtomTerm(Yap_LookupAtom(yap_init->YapPrologRCFile))
);
/* /*
This must be done again after restore, as yap_flags This must be done again after restore, as yap_flags
has been overwritten .... has been overwritten ....
*/ */
setBooleanGlobalPrologFlag(HALT_AFTER_CONSULT_FLAG, setBooleanGlobalPrologFlag(HALT_AFTER_CONSULT_FLAG,
yap_init->HaltAfterConsult); yap_init
->HaltAfterConsult);
} }
if (yap_init->YapPrologTopLevelGoal) { if (yap_init->YapPrologTopLevelGoal) {
Yap_PutValue(AtomTopLevelGoal, Yap_PutValue(AtomTopLevelGoal,
MkAtomTerm(Yap_LookupAtom(yap_init->YapPrologTopLevelGoal))); MkAtomTerm(Yap_LookupAtom(yap_init->YapPrologTopLevelGoal))
);
} }
if (yap_init->YapPrologGoal) { if (yap_init->YapPrologGoal) {
Yap_PutValue(AtomInitGoal, Yap_PutValue(AtomInitGoal,
MkAtomTerm(Yap_LookupAtom(yap_init->YapPrologGoal))); MkAtomTerm(Yap_LookupAtom(yap_init->YapPrologGoal))
);
} }
if (yap_init->YapPrologAddPath) { if (yap_init->YapPrologAddPath) {
Yap_PutValue(AtomExtendFileSearchPath, Yap_PutValue(AtomExtendFileSearchPath,
MkAtomTerm(Yap_LookupAtom(yap_init->YapPrologAddPath))); MkAtomTerm(Yap_LookupAtom(yap_init->YapPrologAddPath))
);
} }
if (yap_init->YapShareDir) { if (yap_init->YapShareDir) {
setAtomicGlobalPrologFlag(PROLOG_LIBRARY_DIRECTORY_FLAG, setAtomicGlobalPrologFlag(PROLOG_LIBRARY_DIRECTORY_FLAG,
MkAtomTerm(Yap_LookupAtom(yap_init->YapShareDir))); MkAtomTerm(Yap_LookupAtom(yap_init->YapShareDir))
);
} }
if (yap_init->YapLibDir) { if (yap_init->YapLibDir) {
setAtomicGlobalPrologFlag(PROLOG_FOREIGN_DIRECTORY_FLAG, setAtomicGlobalPrologFlag(PROLOG_FOREIGN_DIRECTORY_FLAG,
MkAtomTerm(Yap_LookupAtom(yap_init->YapLibDir))); MkAtomTerm(Yap_LookupAtom(yap_init->YapLibDir))
);
} }
if (yap_init->QuietMode) { if (yap_init->QuietMode) {
setVerbosity(TermSilent); setVerbosity(TermSilent);
} }
if (restore_result == YAP_QLY) { if (restore_result == YAP_QLY) {
setAtomicGlobalPrologFlag(RESOURCE_DATABASE_FLAG, setAtomicGlobalPrologFlag(RESOURCE_DATABASE_FLAG,
MkAtomTerm(Yap_LookupAtom(yap_init->SavedState))); MkAtomTerm(Yap_LookupAtom(yap_init->SavedState))
LOCAL_PrologMode &= ~BootMode; );
LOCAL_PrologMode &=
~BootMode;
CurrentModule = LOCAL_SourceModule = USER_MODULE; CurrentModule = LOCAL_SourceModule = USER_MODULE;
setBooleanGlobalPrologFlag(SAVED_PROGRAM_FLAG, true); setBooleanGlobalPrologFlag(SAVED_PROGRAM_FLAG,
true);
rc = YAP_QLY; rc = YAP_QLY;
} else { } else {
if (boot_file[0] == '\0') if (boot_file[0] == '\0')
strcpy(boot_file, BootFile); strcpy(boot_file, BootFile
);
do_bootfile(boot_file PASS_REGS); do_bootfile(boot_file PASS_REGS);
setAtomicGlobalPrologFlag( setAtomicGlobalPrologFlag(
RESOURCE_DATABASE_FLAG, RESOURCE_DATABASE_FLAG,
MkAtomTerm(Yap_LookupAtom(boot_file))); MkAtomTerm(Yap_LookupAtom(boot_file))
setBooleanGlobalPrologFlag(SAVED_PROGRAM_FLAG, false); );
setBooleanGlobalPrologFlag(SAVED_PROGRAM_FLAG,
false);
} }
start_modules(); start_modules();
YAP_initialized = true; YAP_initialized = true;
return YAP_BOOT_PL; pop_text_stack(lvl);
return
YAP_BOOT_PL;
} }
#if (DefTrailSpace < MinTrailSpace) #if (DefTrailSpace < MinTrailSpace)
@ -2674,9 +2711,9 @@ LOCAL_TextBuffer = Yap_InitTextAllocator();
return out; return out;
} }
X_API void YAP_PutValue(Atom at, Term t) { Yap_PutValue(at, t); } X_API void YAP_PutValue(YAP_Atom at, Term t) { Yap_PutValue(at, t); }
X_API Term YAP_GetValue(Atom at) { return (Yap_GetValue(at)); } X_API Term YAP_GetValue(YAP_Atom at) { return (Yap_GetValue(at)); }
X_API int YAP_CompareTerms(Term t1, Term t2) { X_API int YAP_CompareTerms(Term t1, Term t2) {
return Yap_compare_terms(t1, t2); return Yap_compare_terms(t1, t2);
@ -2740,7 +2777,7 @@ LOCAL_TextBuffer = Yap_InitTextAllocator();
return (ASP); return (ASP);
} }
X_API void *YAP_Predicate(Atom a, UInt arity, Term m) { X_API void *YAP_Predicate(YAP_Atom a, UInt arity, Term m) {
if (arity == 0) { if (arity == 0) {
return ((void *) RepPredProp(PredPropByAtom(a, m))); return ((void *) RepPredProp(PredPropByAtom(a, m)));
} else { } else {
@ -2749,7 +2786,7 @@ LOCAL_TextBuffer = Yap_InitTextAllocator();
} }
} }
X_API void YAP_PredicateInfo(void *p, Atom *a, UInt *arity, Term *m) { X_API void YAP_PredicateInfo(void *p, YAP_Atom *a, UInt *arity, Term *m) {
PredEntry *pd = (PredEntry *) p; PredEntry *pd = (PredEntry *) p;
if (pd->ArityOfPE) { if (pd->ArityOfPE) {
*arity = pd->ArityOfPE; *arity = pd->ArityOfPE;
@ -2764,42 +2801,35 @@ LOCAL_TextBuffer = Yap_InitTextAllocator();
*m = TermProlog; *m = TermProlog;
} }
X_API void YAP_UserCPredicate(const char *name, CPredicate def, arity_t arity) { X_API void YAP_UserCPredicate(const char *name, YAP_UserCPred def, YAP_Arity arity) {
Yap_InitCPred(name, arity, def, UserCPredFlag); Yap_InitCPred(name, arity, (CPredicate)def, UserCPredFlag);
} }
X_API void YAP_UserBackCPredicate_(const char *name, CPredicate init, X_API void YAP_UserBackCPredicate_(const char *name, YAP_UserCPred init,
CPredicate cont, arity_t arity, YAP_UserCPred cont, YAP_Arity arity,
YAP_Arity extra) {
Yap_InitCPredBackCut(name, arity, extra, (CPredicate)init, (CPredicate)cont, NULL, UserCPredFlag);
}
X_API void YAP_UserBackCutCPredicate(const char *name, YAP_UserCPred init,
YAP_UserCPred cont, YAP_UserCPred cut,
YAP_Arity arity, YAP_Arity extra) {
Yap_InitCPredBackCut(name, arity, extra, (CPredicate)init,
(CPredicate)cont, (CPredicate)cut, UserCPredFlag);
}
X_API void YAP_UserBackCPredicate(const char *name, YAP_UserCPred init,
YAP_UserCPred cont, arity_t arity,
arity_t extra) { arity_t extra) {
Yap_InitCPredBackCut(name, arity, extra, init, cont, NULL, UserCPredFlag); Yap_InitCPredBackCut(name, arity, extra, (CPredicate)init, (CPredicate)cont, NULL, UserCPredFlag);
} }
X_API void YAP_UserBackCutCPredicate(const char *name, CPredicate init, X_API void YAP_UserCPredicateWithArgs(const char *a, YAP_UserCPred f,
CPredicate cont, CPredicate cut,
arity_t arity, arity_t extra) {
Yap_InitCPredBackCut(name, arity, extra, init, cont, cut, UserCPredFlag);
}
X_API void YAP_UserBackCPredicate(const char *name, CPredicate init,
CPredicate cont, arity_t arity,
arity_t extra) {
Yap_InitCPredBackCut(name, arity, extra, init, cont, NULL, UserCPredFlag);
}
X_API void YAP_UserCPredicateWithArgs(const char *a, CPredicate f,
arity_t arity, Term mod) { arity_t arity, Term mod) {
CACHE_REGS CACHE_REGS
PredEntry *pe;
Term cm = CurrentModule; Term cm = CurrentModule;
CurrentModule = mod; CurrentModule = mod;
YAP_UserCPredicate(a, f, arity); Yap_InitCPred(a, arity, (CPredicate)f, UserCPredFlag|CArgsPredFlag );
if (arity == 0) {
pe = RepPredProp(PredPropByAtom(Yap_LookupAtom(a), mod));
} else {
Functor f = Yap_MkFunctor(Yap_LookupAtom(a), arity);
pe = RepPredProp(PredPropByFunc(f, mod));
}
pe->PredFlags |= CArgsPredFlag;
CurrentModule = cm; CurrentModule = cm;
} }
@ -2815,7 +2845,7 @@ LOCAL_TextBuffer = Yap_InitTextAllocator();
return omod; return omod;
} }
X_API Term YAP_CreateModule(Atom at) { X_API Term YAP_CreateModule(YAP_Atom at) {
Term t; Term t;
WRITE_LOCK(RepAtom(at)->ARWLock); WRITE_LOCK(RepAtom(at)->ARWLock);
t = Yap_Module(MkAtomTerm(at)); t = Yap_Module(MkAtomTerm(at));
@ -2871,13 +2901,13 @@ LOCAL_TextBuffer = Yap_InitTextAllocator();
X_API int YAP_IsTermNil(Term t) { return t == TermNil; } X_API int YAP_IsTermNil(Term t) { return t == TermNil; }
X_API int YAP_AtomGetHold(Atom at) { return Yap_AtomIncreaseHold(at); } X_API int YAP_AtomGetHold(YAP_Atom at) { return Yap_AtomIncreaseHold(at); }
X_API int YAP_AtomReleaseHold(Atom at) { return Yap_AtomDecreaseHold(at); } X_API int YAP_AtomReleaseHold(YAP_Atom at) { return Yap_AtomDecreaseHold(at); }
X_API Agc_hook YAP_AGCRegisterHook(Agc_hook hook) { X_API YAP_agc_hook YAP_AGCRegisterHook(YAP_agc_hook hook) {
Agc_hook old = GLOBAL_AGCHook; YAP_agc_hook old = (YAP_agc_hook)GLOBAL_AGCHook;
GLOBAL_AGCHook = hook; GLOBAL_AGCHook = (Agc_hook)hook;
return old; return old;
} }
@ -3202,11 +3232,12 @@ LOCAL_TextBuffer = Yap_InitTextAllocator();
X_API Term YAP_ModuleUser(void) { return MkAtomTerm(AtomUser); } X_API Term YAP_ModuleUser(void) { return MkAtomTerm(AtomUser); }
/* int YAP_PredicateHasClauses() */ /* int YAP_PredicateHasClauses() */
X_API yhandle_t YAP_NumberOfClausesForPredicate(PredEntry *pe) { X_API YAP_handle_t YAP_NumberOfClausesForPredicate(YAP_PredEntryPtr ape) {
PredEntry *pe = ape;
return pe->cs.p_code.NOfClauses; return pe->cs.p_code.NOfClauses;
} }
X_API int YAP_MaxOpPriority(Atom at, Term module) { X_API int YAP_MaxOpPriority(YAP_Atom at, Term module) {
AtomEntry *ae = RepAtom(at); AtomEntry *ae = RepAtom(at);
OpEntry *info; OpEntry *info;
WRITE_LOCK(ae->ARWLock); WRITE_LOCK(ae->ARWLock);
@ -3224,7 +3255,7 @@ LOCAL_TextBuffer = Yap_InitTextAllocator();
return ret; return ret;
} }
X_API int YAP_OpInfo(Atom at, Term module, int opkind, int *yap_type, int *prio) { X_API int YAP_OpInfo(YAP_Atom at, Term module, int opkind, int *yap_type, int *prio) {
AtomEntry *ae = RepAtom(at); AtomEntry *ae = RepAtom(at);
OpEntry *info; OpEntry *info;
int n; int n;
@ -3445,7 +3476,7 @@ LOCAL_TextBuffer = Yap_InitTextAllocator();
size_t AtomTranslations, MaxAtomTranslations; size_t AtomTranslations, MaxAtomTranslations;
size_t FunctorTranslations, MaxFunctorTranslations; size_t FunctorTranslations, MaxFunctorTranslations;
X_API Int YAP_AtomToInt(Atom At) { X_API Int YAP_AtomToInt(YAP_Atom At) {
TranslationEntry *te = Yap_GetTranslationProp(At, 0); TranslationEntry *te = Yap_GetTranslationProp(At, 0);
if (te != NIL) if (te != NIL)
return te->Translation; return te->Translation;
@ -3468,10 +3499,10 @@ LOCAL_TextBuffer = Yap_InitTextAllocator();
return AtomTranslations - 1; return AtomTranslations - 1;
} }
X_API Atom YAP_IntToAtom(Int i) { return TR_Atoms[i]; } X_API YAP_Atom YAP_IntToAtom(Int i) { return TR_Atoms[i]; }
X_API Int YAP_FunctorToInt(Functor f) { X_API Int YAP_FunctorToInt(YAP_Functor f) {
Atom At = NameOfFunctor(f); YAP_Atom At = NameOfFunctor(f);
arity_t arity = ArityOfFunctor(f); arity_t arity = ArityOfFunctor(f);
TranslationEntry *te = Yap_GetTranslationProp(At, arity); TranslationEntry *te = Yap_GetTranslationProp(At, arity);
if (te != NIL) if (te != NIL)
@ -3498,12 +3529,12 @@ LOCAL_TextBuffer = Yap_InitTextAllocator();
X_API void *YAP_foreign_stream(int sno) { return GLOBAL_Stream[sno].u.private_data; } X_API void *YAP_foreign_stream(int sno) { return GLOBAL_Stream[sno].u.private_data; }
X_API Functor YAP_IntToFunctor(Int i) { return TR_Functors[i]; } X_API YAP_Functor YAP_IntToFunctor(Int i) { return TR_Functors[i]; }
X_API void *YAP_shared(void) { return LOCAL_shared; } X_API void *YAP_shared(void) { return LOCAL_shared; }
X_API PredEntry *YAP_TopGoal(void) { X_API YAP_PredEntryPtr YAP_TopGoal(void) {
YAP_Functor f = Yap_MkFunctor(Yap_LookupAtom("yap_query"), 3); Functor f = Yap_MkFunctor(Yap_LookupAtom("yap_query"), 3);
Term tmod = MkAtomTerm(Yap_LookupAtom("yapi")); Term tmod = MkAtomTerm(Yap_LookupAtom("yapi"));
PredEntry *p = RepPredProp(Yap_GetPredPropByFunc(f, tmod)); PredEntry *p = RepPredProp(Yap_GetPredPropByFunc(f, tmod));
return p; return p;

View File

@ -4911,17 +4911,17 @@ static Int cont_current_key_integer(USES_REGS1) {
return Yap_unify(term, ARG1) && Yap_unify(term, ARG2); return Yap_unify(term, ARG1) && Yap_unify(term, ARG2);
} }
Term Yap_FetchTermFromDB(DBTerm *ref) { Term Yap_FetchTermFromDB(void *ref) {
CACHE_REGS CACHE_REGS
return GetDBTerm(ref, FALSE PASS_REGS); return GetDBTerm(ref, FALSE PASS_REGS);
} }
Term Yap_FetchClauseTermFromDB(DBTerm *ref) { Term Yap_FetchClauseTermFromDB(void *ref) {
CACHE_REGS CACHE_REGS
return GetDBTerm(ref, TRUE PASS_REGS); return GetDBTerm(ref, TRUE PASS_REGS);
} }
Term Yap_PopTermFromDB(DBTerm *ref) { Term Yap_PopTermFromDB(void *ref) {
CACHE_REGS CACHE_REGS
Term t = GetDBTerm(ref, FALSE PASS_REGS); Term t = GetDBTerm(ref, FALSE PASS_REGS);
@ -5304,7 +5304,7 @@ static void ReleaseTermFromDB(DBTerm *ref USES_REGS) {
FreeDBSpace((char *)ref); FreeDBSpace((char *)ref);
} }
void Yap_ReleaseTermFromDB(DBTerm *ref) { void Yap_ReleaseTermFromDB(void *ref) {
CACHE_REGS CACHE_REGS
ReleaseTermFromDB(ref PASS_REGS); ReleaseTermFromDB(ref PASS_REGS);
} }

View File

@ -386,10 +386,10 @@ yamop *Yap_Error__(const char *file, const char *function, int lineno,
Yap_RestartYap(1); Yap_RestartYap(1);
} }
LOCAL_ActiveError->errorNo = type; LOCAL_ActiveError->errorNo = type;
LOCAL_ActiveError->errorAsText = Yap_LookupAtom(Yap_errorName(type)); LOCAL_ActiveError->errorAsText = Yap_errorName(type);
LOCAL_ActiveError->errorClass = Yap_errorClass(type); LOCAL_ActiveError->errorClass = Yap_errorClass(type);
LOCAL_ActiveError->classAsText = LOCAL_ActiveError->classAsText =
Yap_LookupAtom(Yap_errorClassName(LOCAL_ActiveError->errorClass)); Yap_errorClassName(LOCAL_ActiveError->errorClass);
LOCAL_ActiveError->errorLine = lineno; LOCAL_ActiveError->errorLine = lineno;
LOCAL_ActiveError->errorFunction = function; LOCAL_ActiveError->errorFunction = function;
LOCAL_ActiveError->errorFile = file; LOCAL_ActiveError->errorFile = file;

View File

@ -2039,10 +2039,10 @@ static Int jump_env(USES_REGS1) {
// LOCAL_Error_TYPE = ERROR_EVENT; // LOCAL_Error_TYPE = ERROR_EVENT;
Term t1 = ArgOfTerm(1, t); Term t1 = ArgOfTerm(1, t);
if (IsApplTerm(t1) && IsAtomTerm((t2 = ArgOfTerm(1, t1)))) { if (IsApplTerm(t1) && IsAtomTerm((t2 = ArgOfTerm(1, t1)))) {
LOCAL_ActiveError->errorAsText = AtomOfTerm(t2); LOCAL_ActiveError->errorAsText = RepAtom(AtomOfTerm(t2))->StrOfAE;
LOCAL_ActiveError->classAsText = NameOfFunctor(FunctorOfTerm(t1)); LOCAL_ActiveError->classAsText = RepAtom(NameOfFunctor(FunctorOfTerm(t1)))->StrOfAE;
} else if (IsAtomTerm(t)) { } else if (IsAtomTerm(t)) {
LOCAL_ActiveError->errorAsText = AtomOfTerm(t1); LOCAL_ActiveError->errorAsText = RepAtom(AtomOfTerm(t1))->StrOfAE;
LOCAL_ActiveError->classAsText = NULL; LOCAL_ActiveError->classAsText = NULL;
} }
} else { } else {

View File

@ -689,12 +689,12 @@ static int send_error_message(char s[]) {
} }
static wchar_t read_quoted_char(int *scan_nextp, static wchar_t read_quoted_char(int *scan_nextp,
struct stream_desc *inp_stream) { struct stream_desc *st) {
int ch; int ch;
/* escape sequence */ /* escape sequence */
do_switch: do_switch:
ch = getchrq(inp_stream); ch = getchrq(st);
switch (ch) { switch (ch) {
case 10: case 10:
return 0; return 0;
@ -705,7 +705,7 @@ do_switch:
case 'b': case 'b':
return '\b'; return '\b';
case 'c': case 'c':
while (chtype((ch = getchrq(inp_stream))) == BS) while (chtype((ch = getchrq(st))) == BS)
; ;
{ {
if (ch == '\\') { if (ch == '\\') {
@ -732,7 +732,7 @@ do_switch:
wchar_t wc = '\0'; wchar_t wc = '\0';
for (i = 0; i < 4; i++) { for (i = 0; i < 4; i++) {
ch = getchrq(inp_stream); ch = getchrq(st);
if (ch >= '0' && ch <= '9') { if (ch >= '0' && ch <= '9') {
wc += (ch - '0') << ((3 - i) * 4); wc += (ch - '0') << ((3 - i) * 4);
} else if (ch >= 'a' && ch <= 'f') { } else if (ch >= 'a' && ch <= 'f') {
@ -750,7 +750,7 @@ do_switch:
wchar_t wc = '\0'; wchar_t wc = '\0';
for (i = 0; i < 8; i++) { for (i = 0; i < 8; i++) {
ch = getchrq(inp_stream); ch = getchrq(st);
if (ch >= '0' && ch <= '9') { if (ch >= '0' && ch <= '9') {
wc += (ch - '0') << ((7 - i) * 4); wc += (ch - '0') << ((7 - i) * 4);
} else if (ch >= 'a' && ch <= 'f') { } else if (ch >= 'a' && ch <= 'f') {
@ -777,7 +777,7 @@ do_switch:
if (trueGlobalPrologFlag(ISO_FLAG)) { if (trueGlobalPrologFlag(ISO_FLAG)) {
return send_error_message("invalid escape sequence"); return send_error_message("invalid escape sequence");
} else { } else {
ch = getchrq(inp_stream); ch = getchrq(st);
if (ch == '?') { /* delete character */ if (ch == '?') { /* delete character */
return 127; return 127;
} else if (ch >= 'a' && ch < 'z') { /* hexa */ } else if (ch >= 'a' && ch < 'z') { /* hexa */
@ -800,13 +800,13 @@ do_switch:
/* follow ISO */ /* follow ISO */
{ {
unsigned char so_far = ch - '0'; unsigned char so_far = ch - '0';
ch = getchrq(inp_stream); ch = getchrq(st);
if (ch >= '0' && ch < '8') { /* octal */ if (ch >= '0' && ch < '8') { /* octal */
so_far = so_far * 8 + (ch - '0'); so_far = so_far * 8 + (ch - '0');
ch = getchrq(inp_stream); ch = getchrq(st);
if (ch >= '0' && ch < '8') { /* octal */ if (ch >= '0' && ch < '8') { /* octal */
so_far = so_far * 8 + (ch - '0'); so_far = so_far * 8 + (ch - '0');
ch = getchrq(inp_stream); ch = getchrq(st);
if (ch != '\\') { if (ch != '\\') {
return send_error_message("invalid octal escape sequence"); return send_error_message("invalid octal escape sequence");
} }
@ -826,19 +826,19 @@ do_switch:
/* hexadecimal character (YAP allows empty hexadecimal */ /* hexadecimal character (YAP allows empty hexadecimal */
{ {
unsigned char so_far = 0; unsigned char so_far = 0;
ch = getchrq(inp_stream); ch = getchrq(st);
if (my_isxdigit(ch, 'f', 'F')) { /* hexa */ if (my_isxdigit(ch, 'f', 'F')) { /* hexa */
so_far = so_far =
so_far * 16 + (chtype(ch) == NU so_far * 16 + (chtype(ch) == NU
? ch - '0' ? ch - '0'
: (my_isupper(ch) ? ch - 'A' : ch - 'a') + 10); : (my_isupper(ch) ? ch - 'A' : ch - 'a') + 10);
ch = getchrq(inp_stream); ch = getchrq(st);
if (my_isxdigit(ch, 'f', 'F')) { /* hexa */ if (my_isxdigit(ch, 'f', 'F')) { /* hexa */
so_far = so_far =
so_far * 16 + (chtype(ch) == NU so_far * 16 + (chtype(ch) == NU
? ch - '0' ? ch - '0'
: (my_isupper(ch) ? ch - 'A' : ch - 'a') + 10); : (my_isupper(ch) ? ch - 'A' : ch - 'a') + 10);
ch = getchrq(inp_stream); ch = getchrq(st);
if (ch == '\\') { if (ch == '\\') {
return so_far; return so_far;
} else { } else {
@ -890,7 +890,7 @@ static int num_send_error_message(char s[]) {
/* reads a number, either integer or float */ /* reads a number, either integer or float */
static Term get_num(int *chp, int *chbuffp, StreamDesc *inp_stream, int sign) { static Term get_num(int *chp, int *chbuffp, StreamDesc *st, int sign) {
int ch = *chp; int ch = *chp;
Int val = 0L, base = ch - '0'; Int val = 0L, base = ch - '0';
int might_be_float = TRUE, has_overflow = FALSE; int might_be_float = TRUE, has_overflow = FALSE;
@ -899,7 +899,7 @@ static Term get_num(int *chp, int *chbuffp, StreamDesc *inp_stream, int sign) {
int max_size = 254, left = 254; int max_size = 254, left = 254;
*sp++ = ch; *sp++ = ch;
ch = getchr(inp_stream); ch = getchr(st);
/* /*
* because of things like 00'2, 03'2 and even better 12'2, I need to * because of things like 00'2, 03'2 and even better 12'2, I need to
* do this (have mercy) * do this (have mercy)
@ -909,7 +909,7 @@ static Term get_num(int *chp, int *chbuffp, StreamDesc *inp_stream, int sign) {
if (--left == 0) if (--left == 0)
number_overflow(); number_overflow();
base = 10 * base + ch - '0'; base = 10 * base + ch - '0';
ch = getchr(inp_stream); ch = getchr(st);
} }
if (ch == '\'') { if (ch == '\'') {
if (base > 36) { if (base > 36) {
@ -919,7 +919,7 @@ static Term get_num(int *chp, int *chbuffp, StreamDesc *inp_stream, int sign) {
if (--left == 0) if (--left == 0)
number_overflow(); number_overflow();
*sp++ = ch; *sp++ = ch;
ch = getchr(inp_stream); ch = getchr(st);
if (base == 0) { if (base == 0) {
CACHE_REGS CACHE_REGS
wchar_t ascii = ch; wchar_t ascii = ch;
@ -927,11 +927,11 @@ static Term get_num(int *chp, int *chbuffp, StreamDesc *inp_stream, int sign) {
if (ch == '\\' && if (ch == '\\' &&
Yap_GetModuleEntry(CurrentModule)->flags & M_CHARESCAPE) { Yap_GetModuleEntry(CurrentModule)->flags & M_CHARESCAPE) {
ascii = read_quoted_char(&scan_extra, inp_stream); ascii = read_quoted_char(&scan_extra, st);
} }
/* a quick way to represent ASCII */ /* a quick way to represent ASCII */
if (scan_extra) if (scan_extra)
*chp = getchr(inp_stream); *chp = getchr(st);
if (sign == -1) { if (sign == -1) {
return MkIntegerTerm(-ascii); return MkIntegerTerm(-ascii);
} }
@ -951,7 +951,7 @@ static Term get_num(int *chp, int *chbuffp, StreamDesc *inp_stream, int sign) {
val = oval * base + chval; val = oval * base + chval;
if (oval != (val - chval) / base) /* overflow */ if (oval != (val - chval) / base) /* overflow */
has_overflow = (has_overflow || TRUE); has_overflow = (has_overflow || TRUE);
ch = getchr(inp_stream); ch = getchr(st);
} }
} }
} else if (ch == 'x' && base == 0) { } else if (ch == 'x' && base == 0) {
@ -959,7 +959,7 @@ static Term get_num(int *chp, int *chbuffp, StreamDesc *inp_stream, int sign) {
if (--left == 0) if (--left == 0)
number_overflow(); number_overflow();
*sp++ = ch; *sp++ = ch;
ch = getchr(inp_stream); ch = getchr(st);
while (my_isxdigit(ch, 'F', 'f')) { while (my_isxdigit(ch, 'F', 'f')) {
Int oval = val; Int oval = val;
int chval = int chval =
@ -971,17 +971,17 @@ static Term get_num(int *chp, int *chbuffp, StreamDesc *inp_stream, int sign) {
val = val * 16 + chval; val = val * 16 + chval;
if (oval != (val - chval) / 16) /* overflow */ if (oval != (val - chval) / 16) /* overflow */
has_overflow = TRUE; has_overflow = TRUE;
ch = getchr(inp_stream); ch = getchr(st);
} }
*chp = ch; *chp = ch;
} else if (ch == 'o' && base == 0) { } else if (ch == 'o' && base == 0) {
might_be_float = FALSE; might_be_float = FALSE;
base = 8; base = 8;
ch = getchr(inp_stream); ch = getchr(st);
} else if (ch == 'b' && base == 0) { } else if (ch == 'b' && base == 0) {
might_be_float = FALSE; might_be_float = FALSE;
base = 2; base = 2;
ch = getchr(inp_stream); ch = getchr(st);
} else { } else {
val = base; val = base;
base = 10; base = 10;
@ -1002,7 +1002,7 @@ static Term get_num(int *chp, int *chbuffp, StreamDesc *inp_stream, int sign) {
val = val * base + ch - '0'; val = val * base + ch - '0';
if (val / base != oval || val - oval * base != ch - '0') /* overflow */ if (val / base != oval || val - oval * base != ch - '0') /* overflow */
has_overflow = TRUE; has_overflow = TRUE;
ch = getchr(inp_stream); ch = getchr(st);
} }
if (might_be_float && (ch == '.' || ch == 'e' || ch == 'E')) { if (might_be_float && (ch == '.' || ch == 'e' || ch == 'E')) {
int has_dot = (ch == '.'); int has_dot = (ch == '.');
@ -1010,7 +1010,7 @@ static Term get_num(int *chp, int *chbuffp, StreamDesc *inp_stream, int sign) {
unsigned char *dp; unsigned char *dp;
int dc; int dc;
if (chtype(ch = getchr(inp_stream)) != NU) { if (chtype(ch = getchr(st)) != NU) {
if (ch == 'e' || ch == 'E') { if (ch == 'e' || ch == 'E') {
if (trueGlobalPrologFlag(ISO_FLAG)) if (trueGlobalPrologFlag(ISO_FLAG))
return num_send_error_message( return num_send_error_message(
@ -1045,21 +1045,21 @@ static Term get_num(int *chp, int *chbuffp, StreamDesc *inp_stream, int sign) {
if (--left == 0) if (--left == 0)
number_overflow(); number_overflow();
*sp++ = ch; *sp++ = ch;
} while (chtype(ch = getchr(inp_stream)) == NU); } while (chtype(ch = getchr(st)) == NU);
} }
} }
if (ch == 'e' || ch == 'E') { if (ch == 'e' || ch == 'E') {
if (--left == 0) if (--left == 0)
number_overflow(); number_overflow();
*sp++ = ch; *sp++ = ch;
ch = getchr(inp_stream); ch = getchr(st);
if (ch == '-') { if (ch == '-') {
if (--left == 0) if (--left == 0)
number_overflow(); number_overflow();
*sp++ = '-'; *sp++ = '-';
ch = getchr(inp_stream); ch = getchr(st);
} else if (ch == '+') { } else if (ch == '+') {
ch = getchr(inp_stream); ch = getchr(st);
} }
if (chtype(ch) != NU) { if (chtype(ch) != NU) {
CACHE_REGS CACHE_REGS
@ -1071,7 +1071,7 @@ static Term get_num(int *chp, int *chbuffp, StreamDesc *inp_stream, int sign) {
if (--left == 0) if (--left == 0)
number_overflow(); number_overflow();
*sp++ = ch; *sp++ = ch;
} while (chtype(ch = getchr(inp_stream)) == NU); } while (chtype(ch = getchr(st)) == NU);
} }
*sp = '\0'; *sp = '\0';
*chp = ch; *chp = ch;
@ -1257,7 +1257,7 @@ const char *Yap_tokText(void *tokptre) {
return "."; return ".";
} }
static void open_comment(int ch, StreamDesc *inp_stream USES_REGS) { static void open_comment(int ch, StreamDesc *st USES_REGS) {
CELL *h0 = HR; CELL *h0 = HR;
HR += 5; HR += 5;
h0[0] = AbsAppl(h0 + 2); h0[0] = AbsAppl(h0 + 2);
@ -1272,7 +1272,7 @@ static void open_comment(int ch, StreamDesc *inp_stream USES_REGS) {
LOCAL_CommentsTail = h0 + 1; LOCAL_CommentsTail = h0 + 1;
h0 += 2; h0 += 2;
h0[0] = (CELL)FunctorMinus; h0[0] = (CELL)FunctorMinus;
h0[1] = Yap_StreamPosition(inp_stream - GLOBAL_Stream); h0[1] = Yap_StreamPosition(st - GLOBAL_Stream);
h0[2] = TermNil; h0[2] = TermNil;
LOCAL_CommentsNextChar = h0 + 2; LOCAL_CommentsNextChar = h0 + 2;
LOCAL_CommentsBuff = (wchar_t *)malloc(1024 * sizeof(wchar_t)); LOCAL_CommentsBuff = (wchar_t *)malloc(1024 * sizeof(wchar_t));
@ -1301,14 +1301,14 @@ static void close_comment(USES_REGS1) {
// mark that we reached EOF, // mark that we reached EOF,
// next token will be end_of_file) // next token will be end_of_file)
static void mark_eof(struct stream_desc *inp_stream) { static void mark_eof(struct stream_desc *st) {
inp_stream->status |= Push_Eof_Stream_f; st->status |= Push_Eof_Stream_f;
} }
#define add_ch_to_buff(ch) \ #define add_ch_to_buff(ch) \
{ charp += put_utf8(charp, ch); } { charp += put_utf8(charp, ch); }
TokEntry *Yap_tokenizer(struct stream_desc *inp_stream, bool store_comments, TokEntry *Yap_tokenizer(struct stream_desc *st, bool store_comments,
Term *tposp) { Term *tposp) {
CACHE_REGS CACHE_REGS
@ -1324,14 +1324,15 @@ TokEntry *Yap_tokenizer(struct stream_desc *inp_stream, bool store_comments,
LOCAL_AnonVarTable = NULL; LOCAL_AnonVarTable = NULL;
l = NULL; l = NULL;
p = NULL; /* Just to make lint happy */ p = NULL; /* Just to make lint happy */
ch = getchr(inp_stream); __android_log_print(ANDROID_LOG_INFO, "YAPDroid", "i %d", st-GLOBAL_Stream);
ch = getchr(st);
while (chtype(ch) == BS) { while (chtype(ch) == BS) {
ch = getchr(inp_stream); ch = getchr(st);
} }
*tposp = Yap_StreamPosition(inp_stream - GLOBAL_Stream); *tposp = Yap_StreamPosition(st - GLOBAL_Stream);
Yap_setCurrentSourceLocation(inp_stream); Yap_setCurrentSourceLocation(st);
LOCAL_StartLineCount = inp_stream->linecount; LOCAL_StartLineCount = st->linecount;
LOCAL_StartLinePos = inp_stream->linepos; LOCAL_StartLinePos = st->linepos;
do { do {
int quote, isvar; int quote, isvar;
unsigned char *charp, *mp; unsigned char *charp, *mp;
@ -1350,22 +1351,22 @@ TokEntry *Yap_tokenizer(struct stream_desc *inp_stream, bool store_comments,
p = t; p = t;
restart: restart:
while (chtype(ch) == BS) { while (chtype(ch) == BS) {
ch = getchr(inp_stream); ch = getchr(st);
} }
t->TokPos = GetCurInpPos(inp_stream); t->TokPos = GetCurInpPos(st);
switch (chtype(ch)) { switch (chtype(ch)) {
case CC: case CC:
if (store_comments) { if (store_comments) {
open_comment(ch, inp_stream PASS_REGS); open_comment(ch, st PASS_REGS);
continue_comment: continue_comment:
while ((ch = getchr(inp_stream)) != 10 && chtype(ch) != EF) { while ((ch = getchr(st)) != 10 && chtype(ch) != EF) {
extend_comment(ch PASS_REGS); extend_comment(ch PASS_REGS);
} }
extend_comment(ch PASS_REGS); extend_comment(ch PASS_REGS);
if (chtype(ch) != EF) { if (chtype(ch) != EF) {
ch = getchr(inp_stream); ch = getchr(st);
if (chtype(ch) == CC) { if (chtype(ch) == CC) {
extend_comment(ch PASS_REGS); extend_comment(ch PASS_REGS);
goto continue_comment; goto continue_comment;
@ -1373,7 +1374,7 @@ TokEntry *Yap_tokenizer(struct stream_desc *inp_stream, bool store_comments,
} }
close_comment(PASS_REGS1); close_comment(PASS_REGS1);
} else { } else {
while ((ch = getchr(inp_stream)) != 10 && chtype(ch) != EF) while ((ch = getchr(st)) != 10 && chtype(ch) != EF)
; ;
} }
if (chtype(ch) != EF) { if (chtype(ch) != EF) {
@ -1381,15 +1382,15 @@ TokEntry *Yap_tokenizer(struct stream_desc *inp_stream, bool store_comments,
if (t == l) { if (t == l) {
/* we found a comment before reading characters */ /* we found a comment before reading characters */
while (chtype(ch) == BS) { while (chtype(ch) == BS) {
ch = getchr(inp_stream); ch = getchr(st);
} }
*tposp = Yap_StreamPosition(inp_stream - GLOBAL_Stream); *tposp = Yap_StreamPosition(st - GLOBAL_Stream);
Yap_setCurrentSourceLocation(inp_stream); Yap_setCurrentSourceLocation(st);
} }
goto restart; goto restart;
} else { } else {
t->Tok = Ord(kind = eot_tok); t->Tok = Ord(kind = eot_tok);
mark_eof(inp_stream); mark_eof(st);
t->TokInfo = TermEof; t->TokInfo = TermEof;
} }
break; break;
@ -1398,14 +1399,14 @@ TokEntry *Yap_tokenizer(struct stream_desc *inp_stream, bool store_comments,
case UL: case UL:
case LC: { case LC: {
int32_t och = ch; int32_t och = ch;
ch = getchr(inp_stream); ch = getchr(st);
size_t sz = 512; size_t sz = 512;
TokImage = Malloc(sz PASS_REGS); TokImage = Malloc(sz PASS_REGS);
scan_name: scan_name:
charp = (unsigned char *)TokImage; charp = (unsigned char *)TokImage;
isvar = (chtype(och) != LC); isvar = (chtype(och) != LC);
add_ch_to_buff(och); add_ch_to_buff(och);
for (; chtype(ch) <= NU; ch = getchr(inp_stream)) { for (; chtype(ch) <= NU; ch = getchr(st)) {
if (charp == TokImage + (sz - 1)) { if (charp == TokImage + (sz - 1)) {
unsigned char *p0 = TokImage; unsigned char *p0 = TokImage;
sz = Yap_Min(sz * 2, sz + MBYTE); sz = Yap_Min(sz * 2, sz + MBYTE);
@ -1423,7 +1424,7 @@ TokEntry *Yap_tokenizer(struct stream_desc *inp_stream, bool store_comments,
return CodeSpaceError(t, p, l); return CodeSpaceError(t, p, l);
} }
add_ch_to_buff(ch); add_ch_to_buff(ch);
ch = getchr(inp_stream); ch = getchr(st);
} }
add_ch_to_buff('\0'); add_ch_to_buff('\0');
if (!isvar) { if (!isvar) {
@ -1459,7 +1460,7 @@ TokEntry *Yap_tokenizer(struct stream_desc *inp_stream, bool store_comments,
cha = ch; cha = ch;
cherr = 0; cherr = 0;
CHECK_SPACE(); CHECK_SPACE();
if ((t->TokInfo = get_num(&cha, &cherr, inp_stream, sign)) == 0L) { if ((t->TokInfo = get_num(&cha, &cherr, st, sign)) == 0L) {
if (p) { if (p) {
p->Tok = eot_tok; p->Tok = eot_tok;
t->TokInfo = TermError; t->TokInfo = TermError;
@ -1471,7 +1472,7 @@ TokEntry *Yap_tokenizer(struct stream_desc *inp_stream, bool store_comments,
if (cherr) { if (cherr) {
TokEntry *e; TokEntry *e;
t->Tok = Number_tok; t->Tok = Number_tok;
t->TokPos = GetCurInpPos(inp_stream); t->TokPos = GetCurInpPos(st);
e = (TokEntry *)AllocScannerMemory(sizeof(TokEntry)); e = (TokEntry *)AllocScannerMemory(sizeof(TokEntry));
if (e == NULL) { if (e == NULL) {
return TrailSpaceError(p, l); return TrailSpaceError(p, l);
@ -1497,7 +1498,7 @@ TokEntry *Yap_tokenizer(struct stream_desc *inp_stream, bool store_comments,
t->Tok = Ord(Var_tok); t->Tok = Ord(Var_tok);
t->TokInfo = (Term)Yap_LookupVar("E"); t->TokInfo = (Term)Yap_LookupVar("E");
t->TokPos = GetCurInpPos(inp_stream); t->TokPos = GetCurInpPos(st);
e2 = (TokEntry *)AllocScannerMemory(sizeof(TokEntry)); e2 = (TokEntry *)AllocScannerMemory(sizeof(TokEntry));
if (e2 == NULL) { if (e2 == NULL) {
return TrailSpaceError(p, l); return TrailSpaceError(p, l);
@ -1530,7 +1531,7 @@ TokEntry *Yap_tokenizer(struct stream_desc *inp_stream, bool store_comments,
if (ch == '(') if (ch == '(')
solo_flag = FALSE; solo_flag = FALSE;
t->TokInfo = MkAtomTerm(AtomE); t->TokInfo = MkAtomTerm(AtomE);
t->TokPos = GetCurInpPos(inp_stream); t->TokPos = GetCurInpPos(st);
e2 = (TokEntry *)AllocScannerMemory(sizeof(TokEntry)); e2 = (TokEntry *)AllocScannerMemory(sizeof(TokEntry));
if (e2 == NULL) { if (e2 == NULL) {
return TrailSpaceError(p, l); return TrailSpaceError(p, l);
@ -1557,7 +1558,7 @@ TokEntry *Yap_tokenizer(struct stream_desc *inp_stream, bool store_comments,
charp = TokImage; charp = TokImage;
quote = ch; quote = ch;
len = 0; len = 0;
ch = getchrq(inp_stream); ch = getchrq(st);
size_t sz = 1024; size_t sz = 1024;
while (TRUE) { while (TRUE) {
@ -1577,23 +1578,23 @@ TokEntry *Yap_tokenizer(struct stream_desc *inp_stream, bool store_comments,
break; break;
} }
if (ch == quote) { if (ch == quote) {
ch = getchrq(inp_stream); ch = getchrq(st);
if (ch != quote) if (ch != quote)
break; break;
add_ch_to_buff(ch); add_ch_to_buff(ch);
ch = getchrq(inp_stream); ch = getchrq(st);
} else if (ch == '\\' && } else if (ch == '\\' &&
Yap_GetModuleEntry(CurrentModule)->flags & M_CHARESCAPE) { Yap_GetModuleEntry(CurrentModule)->flags & M_CHARESCAPE) {
int scan_next = TRUE; int scan_next = TRUE;
if ((ch = read_quoted_char(&scan_next, inp_stream))) { if ((ch = read_quoted_char(&scan_next, st))) {
add_ch_to_buff(ch); add_ch_to_buff(ch);
} }
if (scan_next) { if (scan_next) {
ch = getchrq(inp_stream); ch = getchrq(st);
} }
} else { } else {
add_ch_to_buff(ch); add_ch_to_buff(ch);
ch = getchrq(inp_stream); ch = getchrq(st);
} }
++len; ++len;
} }
@ -1628,9 +1629,9 @@ TokEntry *Yap_tokenizer(struct stream_desc *inp_stream, bool store_comments,
if (ch == '\0') { if (ch == '\0') {
int pch; int pch;
t->Tok = Ord(kind = eot_tok); t->Tok = Ord(kind = eot_tok);
pch = Yap_peek(inp_stream - GLOBAL_Stream); pch = Yap_peek(st - GLOBAL_Stream);
if (chtype(pch) == EF) { if (chtype(pch) == EF) {
mark_eof(inp_stream); mark_eof(st);
t->TokInfo = TermEof; t->TokInfo = TermEof;
} else { } else {
t->TokInfo = TermNewLine; t->TokInfo = TermNewLine;
@ -1638,11 +1639,11 @@ TokEntry *Yap_tokenizer(struct stream_desc *inp_stream, bool store_comments,
t->TokInfo = TermEof; t->TokInfo = TermEof;
return l; return l;
} else } else
ch = getchr(inp_stream); ch = getchr(st);
break; break;
case SY: { case SY: {
int pch; int pch;
if (ch == '.' && (pch = Yap_peek(inp_stream - GLOBAL_Stream)) && if (ch == '.' && (pch = Yap_peek(st - GLOBAL_Stream)) &&
(chtype(pch) == BS || chtype(pch) == EF || pch == '%')) { (chtype(pch) == BS || chtype(pch) == EF || pch == '%')) {
t->Tok = Ord(kind = eot_tok); t->Tok = Ord(kind = eot_tok);
// consume... // consume...
@ -1655,7 +1656,7 @@ TokEntry *Yap_tokenizer(struct stream_desc *inp_stream, bool store_comments,
if (ch == '`') if (ch == '`')
goto quoted_string; goto quoted_string;
och = ch; och = ch;
ch = getchr(inp_stream); ch = getchr(st);
if (och == '.') { if (och == '.') {
if (chtype(ch) == BS || chtype(ch) == EF || ch == '%') { if (chtype(ch) == BS || chtype(ch) == EF || ch == '%') {
t->Tok = Ord(kind = eot_tok); t->Tok = Ord(kind = eot_tok);
@ -1664,7 +1665,7 @@ TokEntry *Yap_tokenizer(struct stream_desc *inp_stream, bool store_comments,
return l; return l;
} }
if (chtype(ch) == EF) { if (chtype(ch) == EF) {
mark_eof(inp_stream); mark_eof(st);
t->TokInfo = TermEof; t->TokInfo = TermEof;
} else { } else {
t->TokInfo = TermNewLine; t->TokInfo = TermNewLine;
@ -1675,12 +1676,12 @@ TokEntry *Yap_tokenizer(struct stream_desc *inp_stream, bool store_comments,
if (och == '/' && ch == '*') { if (och == '/' && ch == '*') {
if (store_comments) { if (store_comments) {
CHECK_SPACE(); CHECK_SPACE();
open_comment('/', inp_stream PASS_REGS); open_comment('/', st PASS_REGS);
while ((och != '*' || ch != '/') && chtype(ch) != EF) { while ((och != '*' || ch != '/') && chtype(ch) != EF) {
och = ch; och = ch;
CHECK_SPACE(); CHECK_SPACE();
extend_comment(ch PASS_REGS); extend_comment(ch PASS_REGS);
ch = getchr(inp_stream); ch = getchr(st);
} }
if (chtype(ch) != EF) { if (chtype(ch) != EF) {
CHECK_SPACE(); CHECK_SPACE();
@ -1690,7 +1691,7 @@ TokEntry *Yap_tokenizer(struct stream_desc *inp_stream, bool store_comments,
} else { } else {
while ((och != '*' || ch != '/') && chtype(ch) != EF) { while ((och != '*' || ch != '/') && chtype(ch) != EF) {
och = ch; och = ch;
ch = getchr(inp_stream); ch = getchr(st);
} }
} }
if (chtype(ch) == EF) { if (chtype(ch) == EF) {
@ -1699,15 +1700,15 @@ TokEntry *Yap_tokenizer(struct stream_desc *inp_stream, bool store_comments,
break; break;
} else { } else {
/* leave comments */ /* leave comments */
ch = getchr(inp_stream); ch = getchr(st);
if (t == l) { if (t == l) {
/* we found a comment before reading characters */ /* we found a comment before reading characters */
while (chtype(ch) == BS) { while (chtype(ch) == BS) {
ch = getchr(inp_stream); ch = getchr(st);
} }
CHECK_SPACE(); CHECK_SPACE();
*tposp = Yap_StreamPosition(inp_stream - GLOBAL_Stream); *tposp = Yap_StreamPosition(st - GLOBAL_Stream);
Yap_setCurrentSourceLocation(inp_stream); Yap_setCurrentSourceLocation(st);
} }
} }
goto restart; goto restart;
@ -1721,7 +1722,7 @@ TokEntry *Yap_tokenizer(struct stream_desc *inp_stream, bool store_comments,
return l; return l;
} }
if (chtype(ch) == EF) { if (chtype(ch) == EF) {
mark_eof(inp_stream); mark_eof(st);
t->TokInfo = TermEof; t->TokInfo = TermEof;
} else { } else {
t->TokInfo = TermNl; t->TokInfo = TermNl;
@ -1733,7 +1734,7 @@ TokEntry *Yap_tokenizer(struct stream_desc *inp_stream, bool store_comments,
TokImage = Malloc(sz); TokImage = Malloc(sz);
charp = TokImage; charp = TokImage;
add_ch_to_buff(och); add_ch_to_buff(och);
for (; chtype(ch) == SY; ch = getchr(inp_stream)) { for (; chtype(ch) == SY; ch = getchr(st)) {
if (charp >= TokImage + (sz - 10)) { if (charp >= TokImage + (sz - 10)) {
sz = Yap_Min(sz * 2, sz + MBYTE); sz = Yap_Min(sz * 2, sz + MBYTE);
TokImage = Realloc(TokImage, sz); TokImage = Realloc(TokImage, sz);
@ -1764,7 +1765,7 @@ TokEntry *Yap_tokenizer(struct stream_desc *inp_stream, bool store_comments,
unsigned char chs[2]; unsigned char chs[2];
chs[0] = ch; chs[0] = ch;
chs[1] = '\0'; chs[1] = '\0';
ch = getchr(inp_stream); ch = getchr(st);
t->TokInfo = MkAtomTerm(Yap_ULookupAtom(chs)); t->TokInfo = MkAtomTerm(Yap_ULookupAtom(chs));
t->Tok = Ord(kind = Name_tok); t->Tok = Ord(kind = Name_tok);
if (ch == '(') if (ch == '(')
@ -1773,7 +1774,7 @@ TokEntry *Yap_tokenizer(struct stream_desc *inp_stream, bool store_comments,
case BK: case BK:
och = ch; och = ch;
ch = getchr(inp_stream); ch = getchr(st);
{ {
unsigned char chs[10]; unsigned char chs[10];
TokImage = charp = chs; TokImage = charp = chs;
@ -1783,12 +1784,12 @@ TokEntry *Yap_tokenizer(struct stream_desc *inp_stream, bool store_comments,
} }
if (och == '(') { if (och == '(') {
while (chtype(ch) == BS) { while (chtype(ch) == BS) {
ch = getchr(inp_stream); ch = getchr(st);
} }
if (ch == ')') { if (ch == ')') {
t->TokInfo = TermEmptyBrackets; t->TokInfo = TermEmptyBrackets;
t->Tok = Ord(kind = Name_tok); t->Tok = Ord(kind = Name_tok);
ch = getchr(inp_stream); ch = getchr(st);
solo_flag = FALSE; solo_flag = FALSE;
break; break;
} else if (!solo_flag) { } else if (!solo_flag) {
@ -1797,12 +1798,12 @@ TokEntry *Yap_tokenizer(struct stream_desc *inp_stream, bool store_comments,
} }
} else if (och == '[') { } else if (och == '[') {
while (chtype(ch) == BS) { while (chtype(ch) == BS) {
ch = getchr(inp_stream); ch = getchr(st);
}; };
if (ch == ']') { if (ch == ']') {
t->TokInfo = TermNil; t->TokInfo = TermNil;
t->Tok = Ord(kind = Name_tok); t->Tok = Ord(kind = Name_tok);
ch = getchr(inp_stream); ch = getchr(st);
solo_flag = FALSE; solo_flag = FALSE;
break; break;
} }
@ -1827,26 +1828,26 @@ TokEntry *Yap_tokenizer(struct stream_desc *inp_stream, bool store_comments,
cur_qq = qq; cur_qq = qq;
} }
t->TokInfo = (CELL)qq; t->TokInfo = (CELL)qq;
if (inp_stream->status & Seekable_Stream_f) { if (st->status & Seekable_Stream_f) {
qq->start.byteno = fseek(inp_stream->file, 0, 0); qq->start.byteno = fseek(st->file, 0, 0);
} else { } else {
qq->start.byteno = inp_stream->charcount - 1; qq->start.byteno = st->charcount - 1;
} }
qq->start.lineno = inp_stream->linecount; qq->start.lineno = st->linecount;
qq->start.linepos = inp_stream->linepos - 1; qq->start.linepos = st->linepos - 1;
qq->start.charno = inp_stream->charcount - 1; qq->start.charno = st->charcount - 1;
t->Tok = Ord(kind = QuasiQuotes_tok); t->Tok = Ord(kind = QuasiQuotes_tok);
ch = getchr(inp_stream); ch = getchr(st);
solo_flag = FALSE; solo_flag = FALSE;
break; break;
} }
while (chtype(ch) == BS) { while (chtype(ch) == BS) {
ch = getchr(inp_stream); ch = getchr(st);
}; };
if (ch == '}') { if (ch == '}') {
t->TokInfo = TermBraces; t->TokInfo = TermBraces;
t->Tok = Ord(kind = Name_tok); t->Tok = Ord(kind = Name_tok);
ch = getchr(inp_stream); ch = getchr(st);
solo_flag = FALSE; solo_flag = FALSE;
break; break;
} }
@ -1863,16 +1864,16 @@ TokEntry *Yap_tokenizer(struct stream_desc *inp_stream, bool store_comments,
} }
cur_qq = NULL; cur_qq = NULL;
t->TokInfo = (CELL)qq; t->TokInfo = (CELL)qq;
if (inp_stream->status & Seekable_Stream_f) { if (st->status & Seekable_Stream_f) {
qq->mid.byteno = fseek(inp_stream->file, 0, 0); qq->mid.byteno = fseek(st->file, 0, 0);
} else { } else {
qq->mid.byteno = inp_stream->charcount - 1; qq->mid.byteno = st->charcount - 1;
} }
qq->mid.lineno = inp_stream->linecount; qq->mid.lineno = st->linecount;
qq->mid.linepos = inp_stream->linepos - 1; qq->mid.linepos = st->linepos - 1;
qq->mid.charno = inp_stream->charcount - 1; qq->mid.charno = st->charcount - 1;
t->Tok = Ord(kind = QuasiQuotes_tok); t->Tok = Ord(kind = QuasiQuotes_tok);
ch = getchr(inp_stream); ch = getchr(st);
sz = 1024; sz = 1024;
TokImage = Malloc(sz); TokImage = Malloc(sz);
if (!TokImage) { if (!TokImage) {
@ -1885,11 +1886,11 @@ TokEntry *Yap_tokenizer(struct stream_desc *inp_stream, bool store_comments,
charp = TokImage; charp = TokImage;
quote = ch; quote = ch;
len = 0; len = 0;
ch = getchrq(inp_stream); ch = getchrq(st);
while (TRUE) { while (TRUE) {
if (ch == '|') { if (ch == '|') {
ch = getchrq(inp_stream); ch = getchrq(st);
if (ch != '}') { if (ch != '}') {
} else { } else {
charp += put_utf8((unsigned char *)charp, och); charp += put_utf8((unsigned char *)charp, och);
@ -1899,13 +1900,13 @@ TokEntry *Yap_tokenizer(struct stream_desc *inp_stream, bool store_comments,
} }
} else if (chtype(ch) == EF) { } else if (chtype(ch) == EF) {
Free(TokImage); Free(TokImage);
mark_eof(inp_stream); mark_eof(st);
t->Tok = Ord(kind = eot_tok); t->Tok = Ord(kind = eot_tok);
t->TokInfo = TermOutOfHeapError; t->TokInfo = TermOutOfHeapError;
break; break;
} else { } else {
charp += put_utf8(charp, ch); charp += put_utf8(charp, ch);
ch = getchrq(inp_stream); ch = getchrq(st);
} }
if (charp > (unsigned char *)AuxSp - 1024) { if (charp > (unsigned char *)AuxSp - 1024) {
/* Not enough space to read in the string. */ /* Not enough space to read in the string. */
@ -1925,26 +1926,26 @@ TokEntry *Yap_tokenizer(struct stream_desc *inp_stream, bool store_comments,
strncpy((char *)mp, (const char *)TokImage, len + 1); strncpy((char *)mp, (const char *)TokImage, len + 1);
qq->text = (unsigned char *)mp; qq->text = (unsigned char *)mp;
Yap_ReleasePreAllocCodeSpace((CODEADDR)TokImage); Yap_ReleasePreAllocCodeSpace((CODEADDR)TokImage);
if (inp_stream->status & Seekable_Stream_f) { if (st->status & Seekable_Stream_f) {
qq->end.byteno = fseek(inp_stream->file, 0, 0); qq->end.byteno = fseek(st->file, 0, 0);
} else { } else {
qq->end.byteno = inp_stream->charcount - 1; qq->end.byteno = st->charcount - 1;
} }
qq->end.lineno = inp_stream->linecount; qq->end.lineno = st->linecount;
qq->end.linepos = inp_stream->linepos - 1; qq->end.linepos = st->linepos - 1;
qq->end.charno = inp_stream->charcount - 1; qq->end.charno = st->charcount - 1;
if (!(t->TokInfo)) { if (!(t->TokInfo)) {
return CodeSpaceError(t, p, l); return CodeSpaceError(t, p, l);
} }
Yap_ReleasePreAllocCodeSpace((CODEADDR)TokImage); Yap_ReleasePreAllocCodeSpace((CODEADDR)TokImage);
solo_flag = FALSE; solo_flag = FALSE;
ch = getchr(inp_stream); ch = getchr(st);
break; break;
} }
t->Tok = Ord(kind = Ponctuation_tok); t->Tok = Ord(kind = Ponctuation_tok);
break; break;
case EF: case EF:
mark_eof(inp_stream); mark_eof(st);
t->Tok = Ord(kind = eot_tok); t->Tok = Ord(kind = eot_tok);
t->TokInfo = TermEof; t->TokInfo = TermEof;
return l; return l;
@ -1966,7 +1967,7 @@ TokEntry *Yap_tokenizer(struct stream_desc *inp_stream, bool store_comments,
p->TokNext = e; p->TokNext = e;
e->Tok = Error_tok; e->Tok = Error_tok;
e->TokInfo = MkAtomTerm(Yap_LookupAtom(LOCAL_ErrorMessage)); e->TokInfo = MkAtomTerm(Yap_LookupAtom(LOCAL_ErrorMessage));
e->TokPos = GetCurInpPos(inp_stream); e->TokPos = GetCurInpPos(st);
e->TokNext = NULL; e->TokNext = NULL;
LOCAL_ErrorMessage = NULL; LOCAL_ErrorMessage = NULL;
p = e; p = e;

View File

@ -1098,7 +1098,7 @@ bool set_clause_info(yamop *codeptr, PredEntry *pp) {
LOCAL_ActiveError->prologPredArity = pp->ArityOfPE; LOCAL_ActiveError->prologPredArity = pp->ArityOfPE;
} }
LOCAL_ActiveError->prologPredModule = LOCAL_ActiveError->prologPredModule =
(pp->ModuleOfPred ? pp->ModuleOfPred : TermProlog); (pp->ModuleOfPred ? pp->ModuleOfPred : "prolog");
LOCAL_ActiveError->prologPredFile = pp->src.OwnerFile; LOCAL_ActiveError->prologPredFile = pp->src.OwnerFile;
if (codeptr->opc == UNDEF_OPCODE) { if (codeptr->opc == UNDEF_OPCODE) {
LOCAL_ActiveError->prologPredFirstLine = 0; LOCAL_ActiveError->prologPredFirstLine = 0;

View File

@ -147,14 +147,14 @@ static int dump_runtime_variables(void) {
return 1; return 1;
} }
YAP_file_type_t Yap_InitDefaults(YAP_init_args *iap, char *saved_state, X_API YAP_file_type_t Yap_InitDefaults(void *x, char *saved_state,
int argc, char *argv[]) { int argc, char *argv[]) {
YAP_init_args *iap = x;
memset(iap, 0, sizeof(YAP_init_args)); memset(iap, 0, sizeof(YAP_init_args));
#if __ANDROID__ #if __ANDROID__
iap->boot_file_type = YAP_BOOT_PL; iap->boot_file_type = YAP_BOOT_PL;
iap->SavedState = malloc(strlen(saved_state)+1); iap->SavedState = NULL;
strcpy(iap->SavedState, saved_state); iap->assetManager = NULL;
iap->assetManager = true;
#else #else
iap->boot_file_type = YAP_QLY; iap->boot_file_type = YAP_QLY;
iap->SavedState = saved_state; iap->SavedState = saved_state;
@ -498,7 +498,7 @@ X_API YAP_file_type_t YAP_parse_yap_arguments(int argc, char *argv[],
} else if (!strncmp("-home=", p, strlen("-home="))) { } else if (!strncmp("-home=", p, strlen("-home="))) {
GLOBAL_Home = p + strlen("-home="); GLOBAL_Home = p + strlen("-home=");
} else if (!strncmp("-cwd=", p, strlen("-cwd="))) { } else if (!strncmp("-cwd=", p, strlen("-cwd="))) {
if (!ChDir(p + strlen("-cwd=")) ) { if (!Yap_ChDir(p + strlen("-cwd=")) ) {
fprintf(stderr, " [ YAP unrecoverable error in setting cwd: %s ]\n", fprintf(stderr, " [ YAP unrecoverable error in setting cwd: %s ]\n",
strerror(errno)); strerror(errno));
} }

View File

@ -88,7 +88,9 @@ protected:
PredEntry *ap; PredEntry *ap;
/// auxiliary routine to find a predicate in the current module. /// auxiliary routine to find a predicate in the current module.
PredEntry *getPred(YAPTerm &t, Term *&outp);
/// auxiliary routine to find a predicate in the current module.
PredEntry *getPred(YAPTerm &t, CELL *& outp);
PredEntry *asPred() { return ap; }; PredEntry *asPred() { return ap; };
@ -122,7 +124,7 @@ protected:
/// ///
/// It is just a call to getPred /// It is just a call to getPred
inline YAPPredicate(Term t) { inline YAPPredicate(Term t) {
CELL *v = NULL; CELL *v = nullptr;
YAPTerm tt = YAPTerm(t); YAPTerm tt = YAPTerm(t);
ap = getPred(tt, v); ap = getPred(tt, v);
} }
@ -131,7 +133,7 @@ protected:
/// ///
/// It is just a call to getPred /// It is just a call to getPred
inline YAPPredicate(YAPTerm t) { inline YAPPredicate(YAPTerm t) {
Term *v = nullptr; CELL *v = nullptr;
ap = getPred(t, v); ap = getPred(t, v);
} }
@ -275,13 +277,13 @@ public:
*/ */
class X_API YAPFLIP : public YAPPredicate { class X_API YAPFLIP : public YAPPredicate {
public: public:
YAPFLIP(CPredicate call, YAPAtom name, uintptr_t arity, YAPFLIP(YAP_UserCPred call, YAPAtom name, YAP_Arity arity,
YAPModule module = YAPModule(), CPredicate retry = 0, YAPModule module = YAPModule(), YAP_UserCPred retry = 0,
CPredicate cut = 0, size_t extra = 0, bool test = false) YAP_UserCPred cut = 0, YAP_Arity extra = 0, bool test = false)
: YAPPredicate(name, arity, module) { : YAPPredicate(name, arity, module) {
if (retry) { if (retry) {
Yap_InitCPredBackCut(name.getName(), arity, extra, call, retry, cut, YAP_UserBackCutCPredicate(name.getName(), call, retry, cut, arity, extra
UserCPredFlag); );
} else { } else {
if (test) { if (test) {
YAP_UserCPredicate(name.getName(), call, arity); YAP_UserCPredicate(name.getName(), call, arity);

View File

@ -19,7 +19,7 @@ X_API char *Yap_TermToString(Term t, encoding_t encodingp,
X_API void YAP_UserCPredicateWithArgs(const char *, YAP_UserCPred, arity_t, X_API void YAP_UserCPredicateWithArgs(const char *, YAP_UserCPred, arity_t,
YAP_Term); YAP_Term);
X_API void YAP_UserBackCPredicate(const char *, YAP_UserCPred, YAP_UserCPred, X_API void YAP_UserBackCPredicate(const char *, YAP_UserCPred, YAP_UserCPred,
arity_t, arity_t); YAP_Arity, YAP_Arity);
#if YAP_PYTHON #if YAP_PYTHON
X_API bool do_init_python(void); X_API bool do_init_python(void);
@ -944,7 +944,7 @@ void YAPEngine::doInit(YAP_file_type_t BootMode)
#if YAP_PYTHON #if YAP_PYTHON
do_init_python(); do_init_python();
#endif #endif
YAP_PredEntryPtr p = YAP_AtomToPred( YAP_LookupAtom("initialize_prolog") ); YAPPredicate p = YAPPredicate( YAPAtomTerm("initialize_prolog") );
YAPQuery initq = YAPQuery(YAPPredicate(p), nullptr); YAPQuery initq = YAPQuery(YAPPredicate(p), nullptr);
if (initq.next()) if (initq.next())
{ {
@ -992,7 +992,7 @@ YAPEngine::YAPEngine(int argc, char *argv[],
} }
/// auxiliary routine to find a predicate in the current module. /// auxiliary routine to find a predicate in the current module.
PredEntry *YAPPredicate::getPred(YAPTerm &tt, Term *&outp) PredEntry *YAPPredicate::getPred(YAPTerm &tt, CELL * &outp)
{ {
CACHE_REGS CACHE_REGS
Term m = Yap_CurrentModule(), t = tt.term(); Term m = Yap_CurrentModule(), t = tt.term();
@ -1110,7 +1110,7 @@ YAPEngine::YAPEngine(int argc, char *argv[],
if (LOCAL_ActiveError->prologPredLine) if (LOCAL_ActiveError->prologPredLine)
{ {
s += "\n"; s += "\n";
s += LOCAL_ActiveError->prologPredFile->StrOfAE; s += LOCAL_ActiveError->prologPredFile;
s += ":"; s += ":";
sprintf(buf, "%ld", (long int)LOCAL_ActiveError->prologPredLine); sprintf(buf, "%ld", (long int)LOCAL_ActiveError->prologPredLine);
s += buf; // std::to_string(LOCAL_ActiveError->prologPredLine) ; s += buf; // std::to_string(LOCAL_ActiveError->prologPredLine) ;
@ -1118,7 +1118,7 @@ YAPEngine::YAPEngine(int argc, char *argv[],
s += ":0 "; s += ":0 ";
s += LOCAL_ActiveError->prologPredModule; s += LOCAL_ActiveError->prologPredModule;
s += ":"; s += ":";
s += (LOCAL_ActiveError->prologPredName)->StrOfAE; s += (LOCAL_ActiveError->prologPredName);
s += "/"; s += "/";
sprintf(buf, "%ld", (long int)LOCAL_ActiveError->prologPredArity); sprintf(buf, "%ld", (long int)LOCAL_ActiveError->prologPredArity);
s += // std::to_string(LOCAL_ActiveError->prologPredArity); s += // std::to_string(LOCAL_ActiveError->prologPredArity);
@ -1126,13 +1126,13 @@ YAPEngine::YAPEngine(int argc, char *argv[],
} }
s += " error "; s += " error ";
if (LOCAL_ActiveError->classAsText != nullptr) if (LOCAL_ActiveError->classAsText != nullptr)
s += LOCAL_ActiveError->classAsText->StrOfAE; s += LOCAL_ActiveError->classAsText;
s += "."; s += ".";
s += LOCAL_ActiveError->errorAsText->StrOfAE; s += LOCAL_ActiveError->errorAsText;
s += ".\n"; s += ".\n";
if (LOCAL_ActiveError->errorTerm) if (LOCAL_ActiveError->errorTerm)
{ {
Term t = LOCAL_ActiveError->errorTerm->Entry; Term t = Yap_PopTermFromDB(LOCAL_ActiveError->errorTerm);
if (t) if (t)
{ {
s += "error term is: "; s += "error term is: ";

View File

@ -85,8 +85,16 @@ X_API extern void YAP_UserCPredicate(const char *, YAP_UserCPred,
X_API extern void YAP_UserCPredicateWithArgs(const char *, YAP_UserCPred, X_API extern void YAP_UserCPredicateWithArgs(const char *, YAP_UserCPred,
YAP_Arity, YAP_Term); YAP_Arity, YAP_Term);
X_API extern void UserBackCPredicate(const char *name, int *init(), int *cont(), X_API extern void YAP_UserBackCPredicate(const char *name,
int arity, int extra); YAP_UserCPred init,
YAP_UserCPred cont,
YAP_Arity arity, YAP_Arity extra);
X_API extern void YAP_UserBackCutCPredicate(const char *name,
YAP_UserCPred init,
YAP_UserCPred cont,
YAP_UserCPred cut,
YAP_Arity arity, YAP_Arity extra);
X_API extern YAP_Term YAP_ReadBuffer(const char *s, YAP_Term *tp); X_API extern YAP_Term YAP_ReadBuffer(const char *s, YAP_Term *tp);

View File

@ -185,16 +185,14 @@ public:
virtual void run(char *s) {} virtual void run(char *s) {}
}; };
void YAP_init_args::YAP_init_args()
{
Yap_InitDefaults(this, NULL, 0, NULL);
} ;
/// @brief Setup all arguments to a new engine /// @brief Setup all arguments to a new engine
struct X_API YAPEngineArgs: YAP_init_args { struct X_API YAPEngineArgs: YAP_init_args {
public: public:
YAPEngineArgs():yap_boot_params() { YAPEngineArgs():yap_boot_params() {
char s[32];
strcpy(s, "startup.yss" );
Yap_InitDefaults(this,s,0,nullptr);
#if YAP_PYTHON #if YAP_PYTHON
Embedded = true; Embedded = true;
python_in_python = Py_IsInitialized(); python_in_python = Py_IsInitialized();

View File

@ -96,7 +96,7 @@ typedef struct ExtraAtomEntryStruct {
#define USE_OFFSETS_IN_PROPS 0 #define USE_OFFSETS_IN_PROPS 0
#endif #endif
typedef SFLAGS PropFlags; typedef u_short PropFlags;
/* basic property entry structure */ /* basic property entry structure */
typedef struct PropEntryStruct { typedef struct PropEntryStruct {

View File

@ -17,9 +17,6 @@
#include "YapTermConfig.h" #include "YapTermConfig.h"
#include "config.h" #include "config.h"
typedef void *Functor;
typedef void *Atom;
#endif #endif
#if HAVE_STDINT_H #if HAVE_STDINT_H
@ -29,6 +26,22 @@ typedef void *Atom;
#include <inttypes.h> #include <inttypes.h>
#endif #endif
/* truth-values */
/* stdbool defines the booleam type, bool,
and the constants false and true */
#if HAVE_STDBOOL_H
#include <stdbool.h>
#else
#ifndef true
typedef int _Bool;
#define bool _Bool;
#define false 0
#define true 1
#endif
#endif /* HAVE_STDBOOL_H */
#define ALIGN_BY_TYPE(X, TYPE) \ #define ALIGN_BY_TYPE(X, TYPE) \
(((CELL)(X) + (sizeof(TYPE) - 1)) & ~(sizeof(TYPE) - 1)) (((CELL)(X) + (sizeof(TYPE) - 1)) & ~(sizeof(TYPE) - 1))
@ -45,59 +58,98 @@ typedef void *Atom;
#if defined(PRIdPTR) #if defined(PRIdPTR)
typedef intptr_t Int; typedef intptr_t YAP_Int;
typedef uintptr_t UInt; typedef uintptr_t YAP_UInt;
#elif defined(_WIN64) #elif defined(_WIN64)
typedef int64_t Int; typedef int64_t YAP_Int;
typedef uint64_t UInt; typedef uint64_t YAP_UInt;
#elif defined(_WIN32) #elif defined(_WIN32)
typedef int32_t Int; typedef int32_t YAP_Int;
typedef uint32_t UInt; typedef uint32_t YAP_UInt;
#elif SIZEOF_LONG_INT == SIZEOF_INT_P #elif SIZEOF_LONG_INT == SIZEOF_INT_P
typedef long int Int; typedef long int YAP_Int;
typedef unsigned long int UInt; typedef unsigned long int YAP_UInt;
#elif SIZEOF_INT == SIZEOF_INT_P #elif SIZEOF_INT == SIZEOF_INT_P
typedef int Int; typedef int YAP_Int;
typedef unsigned int UInt; typedef unsigned int YAP_UInt;
#else #else
#error Yap require integer types of the same size as a pointer #error Yap require integer types of the same size as a pointer
#endif #endif
/* */ typedef short int Short; /* */ typedef short int YAP_Short;
/* */ typedef unsigned short int UShort; /* */ typedef unsigned short int YAP_UShort;
typedef UInt CELL; typedef YAP_UInt YAP_CELL;
typedef YAP_UInt YAP_Term;
/* Type definitions */
#ifndef TRUE
#define TRUE true
#endif
#ifndef FALSE
#endif
typedef bool YAP_Bool;
#define FALSE false
typedef YAP_Int YAP_handle_t;
typedef double YAP_Float;
typedef void *YAP_Atom;
typedef void *YAP_Functor;
#ifdef YAP_H
typedef YAP_Int Int;
typedef YAP_UInt UInt;
typedef YAP_Short Short;
typedef YAP_UShort UShort;
typedef uint16_t BITS16; typedef uint16_t BITS16;
typedef int16_t SBITS16; typedef int16_t SBITS16;
typedef uint32_t BITS32; typedef uint32_t BITS32;
typedef YAP_CELL CELL;
typedef YAP_Term Term;
#define WordSize sizeof(BITS16) #define WordSize sizeof(BITS16)
#define CellSize sizeof(CELL) #define CellSize sizeof(CELL)
#define SmallSize sizeof(SMALLUNSGN) #define SmallSize sizeof(SMALLUNSGN)
typedef YAP_Int Int;
typedef YAP_Float Float;
typedef YAP_handle_t yhandle_t;
#endif
#include "YapError.h"
#include "../os/encoding.h"
typedef encoding_t YAP_encoding_t;
#include "YapFormat.h" #include "YapFormat.h"
/************************************************************************************************* /*************************************************************************************************
type casting macros type casting macros
*************************************************************************************************/ *************************************************************************************************/
typedef UInt Term;
typedef Int yhandle_t;
typedef double Float;
#if SIZEOF_INT < SIZEOF_INT_P #if SIZEOF_INT < SIZEOF_INT_P
#define SHORT_INTS 1 #define SHORT_INTS 1
#else #else

View File

@ -1306,10 +1306,10 @@ void Yap_UpdateTimestamps(PredEntry *);
void Yap_ErDBE(DBRef); void Yap_ErDBE(DBRef);
DBTerm *Yap_StoreTermInDB(Term, int); DBTerm *Yap_StoreTermInDB(Term, int);
DBTerm *Yap_StoreTermInDBPlusExtraSpace(Term, UInt, UInt *); DBTerm *Yap_StoreTermInDBPlusExtraSpace(Term, UInt, UInt *);
Term Yap_FetchTermFromDB(DBTerm *); Term Yap_FetchTermFromDB(void *);
Term Yap_FetchClauseTermFromDB(DBTerm *); Term Yap_FetchClauseTermFromDB(void *);
Term Yap_PopTermFromDB(DBTerm *); Term Yap_PopTermFromDB(void *);
void Yap_ReleaseTermFromDB(DBTerm *); void Yap_ReleaseTermFromDB(void *);
/* init.c */ /* init.c */
Atom Yap_GetOp(OpEntry *, int *, int); Atom Yap_GetOp(OpEntry *, int *, int);
@ -1598,8 +1598,8 @@ bool Yap_PutException(Term t);
INLINE_ONLY inline EXTERN bool Yap_HasException(void) { INLINE_ONLY inline EXTERN bool Yap_HasException(void) {
return LOCAL_BallTerm != NULL; return LOCAL_BallTerm != NULL;
} }
INLINE_ONLY inline EXTERN DBTerm *Yap_RefToException(void) { INLINE_ONLY inline EXTERN void *Yap_RefToException(void) {
DBTerm *dbt = LOCAL_BallTerm; void *dbt = LOCAL_BallTerm;
LOCAL_BallTerm = NULL; LOCAL_BallTerm = NULL;
return dbt; return dbt;
} }

View File

@ -80,7 +80,7 @@ typedef struct vfs {
const char *suffix; const char *suffix;
bool (*chDir)(struct vfs *me, const char *s); bool (*chDir)(struct vfs *me, const char *s);
/** operations */ /** operations */
void *(*open)(int sno, const char *fname, const char *io_mode); /// open an object void *(*open)(struct vfs *,int sno, const char *fname, const char *io_mode); /// open an object
/// in this space, usual w,r,a,b flags plus B (store in a buffer) /// in this space, usual w,r,a,b flags plus B (store in a buffer)
bool (*close)(int sno); /// close the object bool (*close)(int sno); /// close the object
int (*get_char)(int sno); /// get an octet to the stream int (*get_char)(int sno); /// get an octet to the stream
@ -92,7 +92,7 @@ typedef struct vfs {
const char *(*nextdir)(void *d); /// walk to the next entry in a directory object const char *(*nextdir)(void *d); /// walk to the next entry in a directory object
bool (*closedir)(void *d); bool (*closedir)(void *d);
; /// close access a directory object ; /// close access a directory object
bool (*stat)(const char *s, bool (*stat)(struct vfs *,const char *s,
vfs_stat *); /// obtain size, age, permissions of a file. vfs_stat *); /// obtain size, age, permissions of a file.
bool (*isdir)(struct vfs *,const char *s); /// verify whether is directory. bool (*isdir)(struct vfs *,const char *s); /// verify whether is directory.
bool (*exists)(struct vfs *, const char *s); /// verify whether a file exists. bool (*exists)(struct vfs *, const char *s); /// verify whether a file exists.

View File

@ -50,23 +50,10 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#ifdef YAP_H
/* The YAP main types */ /* The YAP main types */
#include "YapTerm.h" #include "YapTerm.h"
/**
This term can never be constructed as a valid term, so it is
used as a "BAD" term
*/
#define TermZERO ((Term)0)
#else
#include "YapConfig.h"
#endif /* YAP_H */
#if HAVE_STDINT_H #if HAVE_STDINT_H
#include <stdint.h> #include <stdint.h>
#endif #endif
@ -74,21 +61,6 @@
#include <inttypes.h> #include <inttypes.h>
#endif #endif
/* truth-values */
/* stdbool defines the booleam type, bool,
and the constants false and true */
#if HAVE_STDBOOL_H
#include <stdbool.h>
#else
#ifndef true
typedef int _Bool;
#define bool _Bool;
#define false 0
#define true 1
#endif
#endif /* HAVE_STDBOOL_H */
/** /**
FALSE and TRUE are the pre-standard versions, FALSE and TRUE are the pre-standard versions,
still widely used. still widely used.
@ -103,79 +75,26 @@ typedef int _Bool;
typedef bool YAP_Bool; typedef bool YAP_Bool;
#endif #endif
#ifdef YAP_H
/* if Yap.h is available, just reexport */ /**
This term can never be constructed as a valid term, so it is
used as a "BAD" term
*/
#define TermZERO ((Term)0)
#define YAP_CELL CELL
#define YAP_Term Term #include "YapConfig.h"
#define YAP_Arity arity_t
#define YAP_Module Term typedef void *YAP_PredEntryPtr;
#define YAP_Functor Functor
#define YAP_Atom Atom
#define YAP_Int Int
#define YAP_UInt UInt
#define YAP_Float Float
#define YAP_handle_t yhandle_t
#define YAP_PredEntryPtr struct pred_entry *
#define YAP_UserCPred CPredicate
#define YAP_agc_hook Agc_hook
#define YAP_encoding_t encoding_t
#else
/* Type definitions */
#if defined(PRIdPTR)
typedef uintptr_t YAP_UInt;
typedef intptr_t YAP_Int;
#elif _WIN64
typedef int64_t YAP_Int;
typedef uint64_t YAP_UInt;
#elif _WIN32
typedef int32_t YAP_Int;
typedef uint32_t YAP_UInt;
#else
typedef long int YAP_Int;
typedef unsigned long int YAP_UInt;
#endif
typedef YAP_UInt YAP_CELL;
typedef YAP_CELL YAP_Term;
typedef size_t YAP_Arity; typedef size_t YAP_Arity;
typedef YAP_Term YAP_Module; typedef YAP_Term YAP_Module;
typedef struct FunctorEntry *YAP_Functor;
typedef struct AtomEntry *YAP_Atom;
typedef double YAP_Float;
#ifndef TRUE
#define TRUE 1
#endif
#ifndef FALSE
#define FALSE 0
#endif
typedef YAP_Int YAP_handle_t; typedef YAP_Int YAP_handle_t;
typedef struct YAP_pred_entry *YAP_PredEntryPtr; typedef void *YAP_PredEntryPtr;
typedef YAP_Bool (*YAP_UserCPred)(void); typedef YAP_Bool (*YAP_UserCPred)(void);
@ -187,8 +106,6 @@ typedef int (*YAP_agc_hook)(void *_Atom);
typedef encoding_t YAP_encoding_t; typedef encoding_t YAP_encoding_t;
#endif
#if __ANDROID__ #if __ANDROID__
#include <android/asset_manager.h> #include <android/asset_manager.h>
#include <android/native_activity.h> #include <android/native_activity.h>
@ -257,6 +174,10 @@ typedef enum {
#define YAP_RECONSULT_MODE 1 #define YAP_RECONSULT_MODE 1
#define YAP_BOOT_MODE 2 #define YAP_BOOT_MODE 2
X_API YAP_file_type_t Yap_InitDefaults(void *init_args, char saved_state[],
int Argc, char *Argv[]);
typedef struct yap_boot_params { typedef struct yap_boot_params {
//> boot type as suggested by the user //> boot type as suggested by the user
YAP_file_type_t boot_file_type; YAP_file_type_t boot_file_type;
@ -351,16 +272,8 @@ typedef struct yap_boot_params {
int ErrorNo; int ErrorNo;
//> errorstring //> errorstring
char *ErrorCause; char *ErrorCause;
#ifdef __cplusplus
void YAP_init_args();
#endif
} YAP_init_args; } YAP_init_args;
#ifdef YAP_H
YAP_file_type_t Yap_InitDefaults(YAP_init_args *init_args, char saved_state[],
int Argc, char **Argv);
#endif
/* this should be opaque to the user */ /* this should be opaque to the user */
typedef struct { typedef struct {
unsigned long b; //> choice-point at entry unsigned long b; //> choice-point at entry

View File

@ -176,48 +176,48 @@ INLINE_ONLY extern inline Term Yap_ensure_atom__(const char *fu, const char *fi,
/// go back t /// go back t
} yap_error_stage_t; } yap_error_stage_t;
/// a Prolo goal that caused a bug /// a Prolog goal that caused a bug
typedef struct error_prolog_source { typedef struct error_prolog_source {
YAP_Int prologPredCl; intptr_t prologPredCl;
YAP_UInt prologPredLine; uintptr_t prologPredLine;
YAP_UInt prologPredFirstLine; uintptr_t prologPredFirstLine;
YAP_UInt prologPredLastLine; uintptr_t prologPredLastLine;
YAP_Atom prologPredName; const char * prologPredName;
YAP_UInt prologPredArity; uintptr_t prologPredArity;
YAP_Term prologPredModule; const char * prologPredModule;
YAP_Atom prologPredFile; const char * prologPredFile;
struct DB_TERM *errorGoal; void *errorGoal;
struct error_prolog_source *errorParent; struct error_prolog_source *errorParent;
} error_prolog_source_t; } error_prolog_source_t;
/// all we need to know about an error/throw /// all we need to know about an error/throw
typedef struct yap_error_descriptor { typedef struct error_descriptor {
enum yap_error_status status; enum yap_error_status status;
yap_error_class_number errorClass; yap_error_class_number errorClass;
YAP_Atom errorAsText; const char * errorAsText;
YAP_Atom classAsText; const char * classAsText;
yap_error_number errorNo; yap_error_number errorNo;
YAP_Int errorLine; intptr_t errorLine;
const char *errorFunction; const char *errorFunction;
const char *errorFile; const char *errorFile;
// struct error_prolog_source *errorSource; // struct error_prolog_source *errorSource;
YAP_Int prologPredCl; intptr_t prologPredCl;
YAP_UInt prologPredLine; uintptr_t prologPredLine;
YAP_UInt prologPredFirstLine; uintptr_t prologPredFirstLine;
YAP_UInt prologPredLastLine; uintptr_t prologPredLastLine;
YAP_Atom prologPredName; const char * prologPredName;
YAP_UInt prologPredArity; uintptr_t prologPredArity;
YAP_Term prologPredModule; const char * prologPredModule;
YAP_Atom prologPredFile; const char * prologPredFile;
YAP_UInt prologParserLine; uintptr_t prologParserLine;
YAP_UInt prologParserFirstLine; uintptr_t prologParserFirstLine;
YAP_UInt prologParserLastLine; uintptr_t prologParserLastLine;
YAP_Atom prologParserName; const char * prologParserName;
YAP_Atom prologParserFile; const char * prologParserFile;
YAP_Bool prologConsulting; bool prologConsulting;
struct DB_TERM *errorTerm; void *errorTerm;
YAP_Term rawErrorTerm, rawExtraErrorTerm; uintptr_t rawErrorTerm, rawExtraErrorTerm;
char *errorMsg; char *errorMsg;
size_t errorMsgLen; size_t errorMsgLen;
struct yap_error_descriptor *top_error; struct yap_error_descriptor *top_error;

View File

@ -268,18 +268,19 @@ extern X_API void YAP_UserCPredicateWithArgs(const char *, YAP_UserCPred,
extern X_API void YAP_UserBackCPredicate(const char *, YAP_UserCPred, extern X_API void YAP_UserBackCPredicate(const char *, YAP_UserCPred,
YAP_UserCPred, YAP_Arity, YAP_Arity); YAP_UserCPred, YAP_Arity, YAP_Arity);
/* YAP_Int YAP_ListLength(YAP_Term t) */
extern X_API YAP_Int YAP_ListLength(YAP_Term);
extern X_API size_t YAP_UTF8_TextLength(YAP_Term t);
/* void UserBackCPredicate(char *name, int *init(), int *cont(), int *cut(), /* void UserBackCPredicate(char *name, int *init(), int *cont(), int *cut(),
int int
arity, int extra) */ arity, int extra) */
extern X_API void YAP_UserBackCutCPredicate(const char *, YAP_UserCPred, extern X_API void YAP_UserBackCutCPredicate(const char *name, YAP_UserCPred,
YAP_UserCPred, YAP_UserCPred, YAP_UserCPred, YAP_UserCPred,
YAP_Arity, YAP_Arity); YAP_Arity, YAP_Arity);
/* YAP_Int YAP_ListLength(YAP_Term t) */
extern X_API YAP_Int YAP_ListLength(YAP_Term);
extern X_API size_t YAP_UTF8_TextLength(YAP_Term t);
/* void CallProlog(YAP_Term t) */ /* void CallProlog(YAP_Term t) */
extern X_API YAP_Int YAP_CallProlog(YAP_Term t); extern X_API YAP_Int YAP_CallProlog(YAP_Term t);
@ -399,7 +400,7 @@ extern X_API YAP_Term YAP_ReadFromStream(int s);
/// read a Prolog clause from a Prolog opened stream $s$. Similar to /// read a Prolog clause from a Prolog opened stream $s$. Similar to
/// YAP_ReadFromStream() but takes /// default options from read_clause/3. /// YAP_ReadFromStream() but takes /// default options from read_clause/3.
extern X_API YAP_Term YAP_ReadFromStream(int s); extern X_API YAP_Term YAP_ReadClauseFromStream(int s);
extern X_API void YAP_Write(YAP_Term t, FILE *s, int); extern X_API void YAP_Write(YAP_Term t, FILE *s, int);

View File

@ -136,9 +136,9 @@ FILE *open_memstream(char **buf, size_t *len);
#endif #endif
#if __ANDROID__ #if __ANDROID__
//extern FILE * fmemopen(void *buf, size_t size, const char *mode);
#undef HAVE_FMEMOPEN #define HAVE_FMEMOPEN 1
#undef HAVE_OPEN_MEMSTREAM #define HAVE_OPEN_MEMSTREAM 1
#endif #endif
#if HAVE_FMEMOPEN #if HAVE_FMEMOPEN

View File

@ -7,7 +7,10 @@ set (YAPOS_SOURCES
console.c console.c
files.c files.c
fmem.c fmem.c
fmemopen.c # fmemopen.c
#android/fmemopen.c
# android/fopencookie.c
# android/open_memstream.c
format.c format.c
iopreds.c iopreds.c
mem.c mem.c

View File

@ -35,81 +35,70 @@ static char SccsId[] = "%W% %G%";
#if __ANDROID__ #if __ANDROID__
#include <android/asset_manager.h> #include <android/asset_manager.h>
#include <android/native_activity.h> #include <android/native_activity.h>
AAssetManager *Yap_assetManager; AAssetManager *Yap_assetManager;
jboolean Java_pt_up_yap_app_YAPDroid_setAssetManager(JNIEnv* env, jclass clazz, jobject assetManager) jboolean
{ Java_pt_up_yap_app_YAPDroid_setAssetManager(JNIEnv *env, jclass clazz, jobject assetManager) {
Yap_assetManager = AAssetManager_fromJava(env, assetManager); Yap_assetManager = AAssetManager_fromJava(env, assetManager);
return true; return true;
} }
static void * static void *
open_asset__( int sno, const char *fname, const char *io_mode) open_asset__(VFS_t *me, int sno, const char *fname, const char *io_mode) {
{
int mode; int mode;
const void *buf; const void *buf;
VFS_t *me = GLOBAL_Stream[sno].vfs;
if (strstr(fname,"/assets") == fname) {
// we're in
if ( Yap_assetManager == NULL) {
PlIOError(PERMISSION_ERROR_OPEN_SOURCE_SINK, TermNil,
"asset manager",
fname);
return NULL;
}
if (strchr(io_mode, 'w') || strchr(io_mode, 'a')) {
PlIOError(PERMISSION_ERROR_OPEN_SOURCE_SINK, TermNil,
"%s: no writing but flags are %s",
fname, io_mode);
return NULL;
}
if (strchr(io_mode, 'B')) if (strchr(io_mode, 'B'))
mode = AASSET_MODE_BUFFER; mode = AASSET_MODE_BUFFER;
else { else {
mode = AASSET_MODE_UNKNOWN; mode = AASSET_MODE_UNKNOWN;
} }
fname += strlen(me->prefix) + 1;
AAsset *a = AAssetManager_open(Yap_assetManager, fname, mode); AAsset *a = AAssetManager_open(Yap_assetManager, fname, mode);
if (!a)
return NULL;
// try not to use it as an asset // try not to use it as an asset
off64_t sz = AAsset_getLength64(a), sz0 = 0; off64_t sz = AAsset_getLength64(a), sz0 = 0;
int fd; int fd;
StreamDesc *st = GLOBAL_Stream + sno; StreamDesc *st = GLOBAL_Stream + sno;
if ((fd = AAsset_openFileDescriptor64(a, &sz0, &sz)) >= 0) { if ((buf = AAsset_getBuffer(a))) {
// can use it as red-only file
st->file = fdopen(fd, "r");
st->vfs = me;
st->vfs_handle = a;
return a;
} else if ((buf = AAsset_getBuffer(a))) {
// copy to memory // copy to memory
bool rc = Yap_set_stream_to_buf(st, buf, sz); bool rc = Yap_set_stream_to_buf(st, buf, sz);
if (rc) AAsset_close(a); if (rc) AAsset_close(a);
st->vfs = NULL;
st->vfs_handle = NULL;
st->status = InMemory_Stream_f|Seekable_Stream_f|Input_Stream_f;
return st; return st;
} } else if ((fd = AAsset_openFileDescriptor64(a, &sz0, &sz)) >= 0) {
// can use it as read-only file
st->file = fdopen(fd, "r");
st->vfs = NULL;
st->vfs_handle = NULL;
st->status = Seekable_Stream_f|Input_Stream_f;
return st;
} else {
// should be done, but if not // should be done, but if not
GLOBAL_Stream[sno].vfs_handle = a; GLOBAL_Stream[sno].vfs_handle = a;
st->vfs = me; st->vfs = me;
st->status = Input_Stream_f;
return a; return a;
} }
return NULL;
} }
static bool static bool
close_asset(int sno) close_asset(int sno) {
{
AAsset_close(GLOBAL_Stream[sno].vfs_handle); AAsset_close(GLOBAL_Stream[sno].vfs_handle);
return true; return true;
} }
static int64_t seek64(int sno, int64_t offset, int whence) static int64_t seek64(int sno, int64_t offset, int whence) {
{
return AAsset_seek64(GLOBAL_Stream[sno].vfs_handle, offset, whence); return AAsset_seek64(GLOBAL_Stream[sno].vfs_handle, offset, whence);
} }
static int getc_asset(int sno) static int getc_asset(int sno) {
{
int ch; int ch;
if (AAsset_read(GLOBAL_Stream[sno].vfs_handle, &ch, 1)) if (AAsset_read(GLOBAL_Stream[sno].vfs_handle, &ch, 1))
return ch; return ch;
@ -117,26 +106,24 @@ static int getc_asset(int sno)
} }
static void *opendir_a( VFS_t *me, const char *dirName) static void *opendir_a(VFS_t *me, const char *dirName) {
{ dirName += strlen(me->prefix) + 1;
return (void *) AAssetManager_openDir(Yap_assetManager, dirName); return (void *) AAssetManager_openDir(Yap_assetManager, dirName);
} }
static const char *readdir_a(void *dirHandle) static const char *readdir_a(void *dirHandle) {
{
return AAssetDir_getNextFileName((AAssetDir *) dirHandle); return AAssetDir_getNextFileName((AAssetDir *) dirHandle);
} }
static bool closedir_a(void *dirHandle) static bool closedir_a(void *dirHandle) {
{
AAssetDir_close((AAssetDir *) dirHandle); AAssetDir_close((AAssetDir *) dirHandle);
return true; return true;
} }
static bool stat_a(VFS_t *me, const char *fname, vfs_stat *out) static bool stat_a(VFS_t *me, const char *fname, vfs_stat *out) {
{
struct stat bf; struct stat bf;
fname += strlen(me->prefix) + 1;
if (stat("/assets", &bf)) { if (stat("/assets", &bf)) {
out->st_dev = bf.st_dev; out->st_dev = bf.st_dev;
@ -145,7 +132,8 @@ static bool stat_a(VFS_t *me, const char *fname, vfs_stat *out)
memcpy(&out->st_atimespec, (const void *) &out->st_atimespec, sizeof(struct timespec)); memcpy(&out->st_atimespec, (const void *) &out->st_atimespec, sizeof(struct timespec));
memcpy(&out->st_mtimespec, (const void *) &out->st_mtimespec, sizeof(struct timespec)); memcpy(&out->st_mtimespec, (const void *) &out->st_mtimespec, sizeof(struct timespec));
memcpy(&out->st_ctimespec, (const void *) &out->st_ctimespec, sizeof(struct timespec)); memcpy(&out->st_ctimespec, (const void *) &out->st_ctimespec, sizeof(struct timespec));
memcpy(&out->st_birthtimespec, (const void *)&out->st_birthtimespec, sizeof(struct timespec)); memcpy(&out->st_birthtimespec, (const void *) &out->st_birthtimespec,
sizeof(struct timespec));
} }
AAsset *a = AAssetManager_open(Yap_assetManager, fname, AASSET_MODE_UNKNOWN); AAsset *a = AAssetManager_open(Yap_assetManager, fname, AASSET_MODE_UNKNOWN);
// try not to use it as an asset // try not to use it as an asset
@ -156,45 +144,46 @@ static bool stat_a(VFS_t *me, const char *fname, vfs_stat *out)
} }
static static
bool is_dir_a( VFS_t *me,const char *dirName) bool is_dir_a(VFS_t *me, const char *dirName) {
{
bool rc; bool rc;
dirName += strlen(me->prefix) + 1;
// try not to use it as an asset // try not to use it as an asset
AAssetDir *d = AAssetManager_openDir(Yap_assetManager, dirName); AAssetDir *d = AAssetManager_openDir(Yap_assetManager, dirName);
if (d == NULL) if (d == NULL)
return false; return false;
rc = (AAssetDir_getNextFileName(d) != NULL); rc = (AAssetDir_getNextFileName(d) != NULL);
__android_log_print(ANDROID_LOG_INFO, "YAPDroid", "isdir %s <%p>", dirName, d);
AAssetDir_close(d); AAssetDir_close(d);
return rc; return rc;
} }
static static
VFS_t *exists_a(VFS_t *me, const char *dirName) bool exists_a(VFS_t *me, const char *dirName) {
{ dirName += strlen(me->prefix) + 1;
// try not to use it as an asset // try not to use it as an asset
AAsset *d = AAssetManager_open(Yap_assetManager, dirName, AASSET_MODE_UNKNOWN); AAsset *d = AAssetManager_open(Yap_assetManager, dirName, AASSET_MODE_UNKNOWN);
__android_log_print(ANDROID_LOG_INFO, "YAPDroid", "exists %s <%p>", dirName, d);
if (d == NULL) if (d == NULL)
return NULL; return false;
AAsset_close(d); AAsset_close(d);
return me; return true;
} }
static bool set_cwd(VFS_t *me, const char *dirName) { static bool set_cwd(VFS_t *me, const char *dirName) {
chdir("/assets"); chdir("/assets");
if (me->virtual_cwd) if (!me->virtual_cwd)
free((void*)(me->virtual_cwd)); me->virtual_cwd = malloc(YAP_FILENAME_MAX + 1);
me->virtual_cwd = malloc( sizeof(dirName) + 1 ); strcpy(me->virtual_cwd, dirName);
return me!= NULL; return true;
} }
#endif #endif
VFS_t * VFS_t *
Yap_InitAssetManager( void ) Yap_InitAssetManager(void) {
{
#if __ANDROID__ #if __ANDROID__
VFS_t *me; VFS_t *me;
@ -202,7 +191,8 @@ Yap_InitAssetManager( void )
/* init standard VFS */ /* init standard VFS */
me = (VFS_t *) Yap_AllocCodeSpace(sizeof(struct vfs)); me = (VFS_t *) Yap_AllocCodeSpace(sizeof(struct vfs));
me->name = "/assets"; me->name = "/assets";
me->vflags = VFS_CAN_EXEC|VFS_CAN_SEEK|VFS_HAS_PREFIX; /// the main flags describing the operation of the Fs. me->vflags = VFS_CAN_EXEC | VFS_CAN_SEEK |
VFS_HAS_PREFIX; /// the main flags describing the operation of the Fs.
me->prefix = "/assets"; me->prefix = "/assets";
/** operations */ /** operations */
me->open = open_asset__; /// open an object in this space me->open = open_asset__; /// open an object in this space
@ -220,6 +210,7 @@ Yap_InitAssetManager( void )
me->enc = ENC_ISO_UTF8; /// how the file is encoded. me->enc = ENC_ISO_UTF8; /// how the file is encoded.
me->parsers = NULL; /// a set of parsers that can read the stream and generate a term me->parsers = NULL; /// a set of parsers that can read the stream and generate a term
me->writers = NULL; me->writers = NULL;
me->virtual_cwd = NULL;
LOCK(BGL); LOCK(BGL);
me->next = GLOBAL_VFS; me->next = GLOBAL_VFS;
GLOBAL_VFS = me; GLOBAL_VFS = me;

View File

@ -20,6 +20,11 @@
#define ENCODING_H 1 #define ENCODING_H 1
#include "YapError.h" #include "YapError.h"
#if HAVE_STRING_H
#include <string.h>
#endif
typedef enum { typedef enum {
ENC_OCTET = 0, /// binary files ENC_OCTET = 0, /// binary files

View File

@ -114,16 +114,18 @@ bool fill_pads(int sno, int sno0, int total, format_info *fg USES_REGS)
return true; return true;
} }
bool Yap_set_stream_to_buf(StreamDesc *st, const char *buf, encoding_t enc, bool Yap_set_stream_to_buf(StreamDesc *st, const char *buf,
size_t nchars) { size_t nchars USES_REGS) {
FILE *f; FILE *f;
// like any file stream. // like any file stream.
st->file = f = fmemopen((void *)buf, nchars, "r"); st->file = f = fmemopen(buf, nchars, "r");
st->status = Input_Stream_f | InMemory_Stream_f | Seekable_Stream_f; st->status = Input_Stream_f | Seekable_Stream_f | InMemory_Stream_f;
st->vfs = NULL; st->vfs = NULL;
st->encoding = enc; st->encoding = LOCAL_encoding;
Yap_DefaultStreamOps(st); Yap_DefaultStreamOps(st);
st->linecount = 0;
st->linepos = st->charcount = 0;
return true; return true;
} }
@ -198,9 +200,7 @@ int Yap_open_buf_write_stream(encoding_t enc, memBufSource src) {
// setbuf(st->file, NULL); // setbuf(st->file, NULL);
st->status |= Seekable_Stream_f; st->status |= Seekable_Stream_f;
#else #else
st->file = fmemopen((void *)buf, nchars, "w"); st->file = fmemopen((void *)st->nbuf, st->nsize, "w");
st->nsize = nchars;
st->nbuf = buf;
if (!st->nbuf) { if (!st->nbuf) {
return -1; return -1;
} }

View File

@ -1,10 +1,12 @@
* Copyright (C) 2007 Eric Blake /* Copyright (C) 2007 Eric Blake
* Permission to use, copy, modify, and distribute this software * Permission to use, copy, modify, and distribute this software
* is freely granted, provided that this notice is preserved. * is freely granted, provided that this notice is preserved.
* *
* Modifications for Android written Jul 2009 by Alan Viverette * Modifications for Android written Jul 2009 by Alan Viverette
*/ */
#if __ANDROID__
/* /*
FUNCTION FUNCTION
<fmemopen>>---open a stream around a fixed-length string <fmemopen>>---open a stream around a fixed-length string
@ -69,7 +71,7 @@ Supporting OS subroutines required: <<sbrk>>.
#include <errno.h> #include <errno.h>
#include <string.h> #include <string.h>
#include <malloc.h> #include <malloc.h>
#include "stdioext.h" //#include "stdioext.h"
/* Describe details of an open memstream. */ /* Describe details of an open memstream. */
typedef struct fmemcookie { typedef struct fmemcookie {
@ -172,13 +174,13 @@ fmemseek(void *cookie, fpos_t pos, int whence)
} }
else else
{ {
if (c->writeonly &amp;&amp; c->pos < c->eof) if (c->writeonly && c->pos < c->eof)
{ {
c->buf[c->pos] = c->saved; c->buf[c->pos] = c->saved;
c->saved = '\0'; c->saved = '\0';
} }
c->pos = offset; c->pos = offset;
if (c->writeonly &amp;&amp; c->pos < c->eof) if (c->writeonly && c->pos < c->eof)
{ {
c->saved = c->buf[c->pos]; c->saved = c->buf[c->pos];
c->buf[c->pos] = '\0'; c->buf[c->pos] = '\0';
@ -206,9 +208,9 @@ fmemopen(void *buf, size_t size, const char *mode)
int flags; int flags;
int dummy; int dummy;
if ((flags = __sflags (mode, &amp;dummy)) == 0) if ((flags = __sflags (mode, &dummy)) == 0)
return NULL; return NULL;
if (!size || !(buf || flags &amp; __SAPP)) if (!size || !(buf || flags & __SAPP))
{ {
return NULL; return NULL;
} }
@ -225,7 +227,7 @@ fmemopen(void *buf, size_t size, const char *mode)
c->max = size; c->max = size;
/* 9 modes to worry about. */ /* 9 modes to worry about. */
/* w/a, buf or no buf: Guarantee a NUL after any file writes. */ /* w/a, buf or no buf: Guarantee a NUL after any file writes. */
c->writeonly = (flags &amp; __SWR) != 0; c->writeonly = (flags & __SWR) != 0;
c->saved = '\0'; c->saved = '\0';
if (!buf) if (!buf)
{ {
@ -233,7 +235,7 @@ fmemopen(void *buf, size_t size, const char *mode)
c->buf = (char *) (c + 1); c->buf = (char *) (c + 1);
*(char *) buf = '\0'; *(char *) buf = '\0';
c->pos = c->eof = 0; c->pos = c->eof = 0;
c->append = (flags &amp; __SAPP) != 0; c->append = (flags & __SAPP) != 0;
} }
else else
{ {
@ -244,7 +246,7 @@ fmemopen(void *buf, size_t size, const char *mode)
/* a/a+ and buf: position and size at first NUL. */ /* a/a+ and buf: position and size at first NUL. */
buf = memchr (c->buf, '\0', size); buf = memchr (c->buf, '\0', size);
c->eof = c->pos = buf ? (char *) buf - c->buf : size; c->eof = c->pos = buf ? (char *) buf - c->buf : size;
if (!buf &amp;&amp; c->writeonly) if (!buf && c->writeonly)
/* a: guarantee a NUL within size even if no writes. */ /* a: guarantee a NUL within size even if no writes. */
c->buf[size - 1] = '\0'; c->buf[size - 1] = '\0';
c->append = 1; c->append = 1;
@ -267,10 +269,12 @@ fmemopen(void *buf, size_t size, const char *mode)
fp->_file = -1; fp->_file = -1;
fp->_flags = flags; fp->_flags = flags;
fp->_cookie = c; fp->_cookie = c;
fp->_read = flags &amp; (__SRD | __SRW) ? fmemread : NULL; fp->_read = flags & (__SRD | __SRW) ? fmemread : NULL;
fp->_write = flags &amp; (__SWR | __SRW) ? fmemwrite : NULL; fp->_write = flags & (__SWR | __SRW) ? fmemwrite : NULL;
fp->_seek = fmemseek; fp->_seek = fmemseek;
fp->_close = fmemclose; fp->_close = fmemclose;
return fp; return fp;
} }
#endif

View File

@ -39,42 +39,57 @@ static char SccsId[] = "%W% %G%";
#include "Yatom.h" #include "Yatom.h"
#include "yapio.h" #include "yapio.h"
#include <stdlib.h> #include <stdlib.h>
#if HAVE_UNISTD_H #if HAVE_UNISTD_H
#include <unistd.h> #include <unistd.h>
#endif #endif
#if HAVE_STDARG_H #if HAVE_STDARG_H
#include <stdarg.h> #include <stdarg.h>
#endif #endif
#if HAVE_CTYPE_H #if HAVE_CTYPE_H
#include <ctype.h>
#endif #endif
#if HAVE_WCTYPE_H #if HAVE_WCTYPE_H
#include <wctype.h>
#endif #endif
#if HAVE_SYS_TIME_H #if HAVE_SYS_TIME_H
#include <sys/time.h> #include <sys/time.h>
#endif #endif
#if HAVE_SYS_TYPES_H #if HAVE_SYS_TYPES_H
#include <sys/types.h> #include <sys/types.h>
#endif #endif
#ifdef HAVE_SYS_STAT_H #ifdef HAVE_SYS_STAT_H
#include <sys/stat.h> #include <sys/stat.h>
#endif #endif
#if HAVE_SYS_SELECT_H && !_MSC_VER && !defined(__MINGW32__) #if HAVE_SYS_SELECT_H && !_MSC_VER && !defined(__MINGW32__)
#include <sys/select.h> #include <sys/select.h>
#endif #endif
#ifdef HAVE_UNISTD_H #ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif #endif
#if HAVE_STRING_H #if HAVE_STRING_H
#include <string.h> #include <string.h>
#endif #endif
#if HAVE_SIGNAL_H #if HAVE_SIGNAL_H
#include <signal.h> #include <signal.h>
#endif #endif
#if HAVE_FCNTL_H #if HAVE_FCNTL_H
/* for O_BINARY and O_TEXT in WIN32 */ /* for O_BINARY and O_TEXT in WIN32 */
#include <fcntl.h> #include <fcntl.h>
#endif #endif
#ifdef _WIN32 #ifdef _WIN32
#if HAVE_IO_H #if HAVE_IO_H
@ -298,7 +313,7 @@ static void InitStdStream(int sno, SMALLUNSGN flags, FILE *file, VFS_t *vfsp) {
INIT_LOCK(s->streamlock); INIT_LOCK(s->streamlock);
if (vfsp != NULL) { if (vfsp != NULL) {
s->u.private_data = s->u.private_data =
vfsp->open(sno, vfsp->name, (sno == StdInStream ? "read" : "write")); vfsp->open(vfsp, sno, vfsp->name, (sno == StdInStream ? "read" : "write"));
if (s->u.private_data == NULL) { if (s->u.private_data == NULL) {
(PlIOError(EXISTENCE_ERROR_SOURCE_SINK, MkIntTerm(sno), "%s", (PlIOError(EXISTENCE_ERROR_SOURCE_SINK, MkIntTerm(sno), "%s",
vfsp->name)); vfsp->name));
@ -1123,8 +1138,7 @@ static bool open_header(int sno, Atom open_mode) {
// skip header // skip header
int ch; int ch;
while ((ch = Yap_peek(sno)) == '#') { while ((ch = Yap_peek(sno)) == '#') {
while ((ch = GLOBAL_Stream[sno].stream_wgetc(sno)) != 10 && ch != -1) while ((ch = GLOBAL_Stream[sno].stream_wgetc(sno)) != 10 && ch != -1);
;
} }
} }
return true; return true;
@ -1147,7 +1161,9 @@ static bool open_header(int sno, Atom open_mode) {
PAR("wait", booleanFlag, OPEN_WAIT), PAR(NULL, ok, OPEN_END) PAR("wait", booleanFlag, OPEN_WAIT), PAR(NULL, ok, OPEN_END)
#define PAR(x, y, z) z #define PAR(x, y, z) z
typedef enum open_enum_choices { OPEN_DEFS() } open_choices_t; typedef enum open_enum_choices {
OPEN_DEFS()
} open_choices_t;
#undef PAR #undef PAR
@ -1162,8 +1178,6 @@ do_open(Term file_name, Term t2,
Term tlist USES_REGS) { /* '$open'(+File,+Mode,?Stream,-ReturnCode) */ Term tlist USES_REGS) { /* '$open'(+File,+Mode,?Stream,-ReturnCode) */
Atom open_mode; Atom open_mode;
int sno; int sno;
SMALLUNSGN s;
char io_mode[8];
StreamDesc *st; StreamDesc *st;
bool avoid_bom = false, needs_bom = false; bool avoid_bom = false, needs_bom = false;
const char *fname; const char *fname;
@ -1173,6 +1187,7 @@ do_open(Term file_name, Term t2,
const char *s_encoding; const char *s_encoding;
encoding_t encoding; encoding_t encoding;
Term tenc; Term tenc;
char io_mode[8];
// original file name // original file name
if (IsVarTerm(file_name)) { if (IsVarTerm(file_name)) {
Yap_Error(INSTANTIATION_ERROR, file_name, "open/3"); Yap_Error(INSTANTIATION_ERROR, file_name, "open/3");
@ -1203,20 +1218,6 @@ do_open(Term file_name, Term t2,
} else { } else {
open_mode = AtomOfTerm(t2); open_mode = AtomOfTerm(t2);
} }
// read, write, append
if (open_mode == AtomRead) {
strncpy(io_mode, "rb", 8);
s = Input_Stream_f;
} else if (open_mode == AtomWrite) {
strncpy(io_mode, "w", 8);
s = Output_Stream_f;
} else if (open_mode == AtomAppend) {
strncpy(io_mode, "a", 8);
s = Append_Stream_f | Output_Stream_f;
} else {
Yap_Error(DOMAIN_ERROR_IO_MODE, t2, "open/3");
return (FALSE);
}
/* get options */ /* get options */
xarg *args = Yap_ArgListToVector(tlist, open_defs, OPEN_END); xarg *args = Yap_ArgListToVector(tlist, open_defs, OPEN_END);
if (args == NULL) { if (args == NULL) {
@ -1235,7 +1236,7 @@ do_open(Term file_name, Term t2,
} }
st = &GLOBAL_Stream[sno]; st = &GLOBAL_Stream[sno];
st->user_name = file_name; st->user_name = file_name;
flags = s; flags = 0;
// user requested encoding? // user requested encoding?
if (args[OPEN_ALIAS].used) { if (args[OPEN_ALIAS].used) {
Atom al = AtomOfTerm(args[OPEN_ALIAS].tvalue); Atom al = AtomOfTerm(args[OPEN_ALIAS].tvalue);
@ -1308,28 +1309,18 @@ do_open(Term file_name, Term t2,
if (st - GLOBAL_Stream < 3) { if (st - GLOBAL_Stream < 3) {
flags |= RepError_Prolog_f; flags |= RepError_Prolog_f;
} }
struct vfs *vfsp = NULL; if (open_mode == AtomRead) {
if ((vfsp = vfs_owner(fname)) != NULL) { strncpy(io_mode, "rb", 8);
st->u.private_data = vfsp->open(sno, fname, io_mode); } else if (open_mode == AtomWrite) {
fd = NULL; strncpy(io_mode, "w", 8);
if (st->u.private_data == NULL) } else if (open_mode == AtomAppend) {
return (PlIOError(EXISTENCE_ERROR_SOURCE_SINK, file_name, "%s", fname)); strncpy(io_mode, "a", 8);
st->vfs = vfsp;
} else if ((fd = fopen(fname, io_mode)) == NULL ||
(!(flags & Binary_Stream_f) && binary_file(fname))) {
strncpy(LOCAL_FileNameBuf, fname, MAXPATHLEN);
if (fname != fbuf)
freeBuffer((void *)fname);
fname = LOCAL_FileNameBuf;
UNLOCK(st->streamlock);
free(args);
if (errno == ENOENT && !strchr(io_mode, 'r')) {
return (PlIOError(EXISTENCE_ERROR_SOURCE_SINK, file_name, "%s: %s", fname,
strerror(errno)));
} else { } else {
return (PlIOError(PERMISSION_ERROR_OPEN_SOURCE_SINK, file_name, "%s: %s", Yap_Error(DOMAIN_ERROR_IO_MODE, MkAtomTerm(open_mode), "open/3");
fname, strerror(errno))); return -2;
} }
if (Yap_OpenStream(RepAtom(AtomOfTerm(file_name))->StrOfAE, io_mode) < 0) {
return false;
} }
#if MAC #if MAC
if (open_mode == AtomWrite) { if (open_mode == AtomWrite) {
@ -1338,11 +1329,8 @@ do_open(Term file_name, Term t2,
#endif #endif
// __android_log_print(ANDROID_LOG_INFO, "YAPDroid", "open %s", fname); // __android_log_print(ANDROID_LOG_INFO, "YAPDroid", "open %s", fname);
flags &= ~(Free_Stream_f); flags &= ~(Free_Stream_f);
if (!Yap_initStream(sno, fd, fname, file_name, encoding, flags, open_mode, Yap_DefaultStreamOps(st);
vfsp)) if (needs_bom && !write_bom(sno, st)) {
return false;
if (open_mode == AtomWrite) {
if (needs_bom && !write_bom(sno, st))
return false; return false;
} else if (open_mode == AtomRead && !avoid_bom) { } else if (open_mode == AtomRead && !avoid_bom) {
check_bom(sno, st); // can change encoding check_bom(sno, st); // can change encoding
@ -1352,7 +1340,6 @@ do_open(Term file_name, Term t2,
st->encoding = enc_id(s_encoding, st->encoding); st->encoding = enc_id(s_encoding, st->encoding);
else else
st->encoding = encoding; st->encoding = encoding;
Yap_DefaultStreamOps(st);
if (script) if (script)
open_header(sno, open_mode); open_header(sno, open_mode);
if (fname != fbuf) if (fname != fbuf)
@ -1528,45 +1515,57 @@ int Yap_OpenStream(const char *fname, const char * io_mode) {
struct vfs *vfsp; struct vfs *vfsp;
FILE *fd; FILE *fd;
int flags; int flags;
SMALLUNSGN s;
sno = GetFreeStreamD(); sno = GetFreeStreamD();
if (sno < 0) if (sno < 0) {
return (PlIOError(RESOURCE_ERROR_MAX_STREAMS, MkAtomTerm(Yap_LookupAtom(fname)), PlIOError(RESOURCE_ERROR_MAX_STREAMS, MkAtomTerm(Yap_LookupAtom(fname)),
"new stream not available for opening")); "new stream not available for opening");
return -1;
}
st = GLOBAL_Stream + sno; st = GLOBAL_Stream + sno;
vfsp = NULL; // read, write, append
st->file = NULL;
if ((vfsp = vfs_owner(fname)) != NULL ) { if ((vfsp = vfs_owner(fname)) != NULL ) {
st->u.private_data = vfsp->open(sno, fname, io_mode); if (!vfsp->open(vfsp, sno, fname, io_mode)) {
UNLOCK(st->streamlock); UNLOCK(st->streamlock);
fd = NULL; PlIOError(EXISTENCE_ERROR_SOURCE_SINK,
if (st->u.private_data == NULL) MkAtomTerm(Yap_LookupAtom(fname)), "%s", fname);
return (PlIOError(EXISTENCE_ERROR_SOURCE_SINK, MkAtomTerm(Yap_LookupAtom(fname)), "%s", fname)); return -1;
st->vfs = vfsp; }
} else if ((fd = fopen(fname, io_mode)) == NULL || } else if ((fd = fopen(fname, io_mode)) == NULL ||
(!strchr(io_mode, 'b') && binary_file(fname))) { (!strchr(io_mode, 'b') && binary_file(fname))) {
UNLOCK(st->streamlock); UNLOCK(st->streamlock);
if (errno == ENOENT && !strchr(io_mode, 'r')) { if (errno == ENOENT && !strchr(io_mode, 'r')) {
return PlIOError(EXISTENCE_ERROR_SOURCE_SINK, MkAtomTerm(Yap_LookupAtom(fname)), "%s: %s", fname, PlIOError(EXISTENCE_ERROR_SOURCE_SINK, MkAtomTerm(Yap_LookupAtom(fname)), "%s: %s",
fname,
strerror(errno)); strerror(errno));
} else { } else {
return PlIOError(PERMISSION_ERROR_OPEN_SOURCE_SINK, MkAtomTerm(Yap_LookupAtom(fname)), "%s: %s", PlIOError(PERMISSION_ERROR_OPEN_SOURCE_SINK, MkAtomTerm(Yap_LookupAtom(fname)),
"%s: %s",
fname, strerror(errno)); fname, strerror(errno));
} }
return -1;
} }
if (strchr(io_mode, 'r')) { flags = st->status;
if (strchr(io_mode, 'w')) {
if (strchr(io_mode, 'a')) { if (strchr(io_mode, 'a')) {
at = AtomAppend; at = AtomAppend;
flags = Append_Stream_f | Output_Stream_f; flags |= Append_Stream_f | Output_Stream_f;
} else { } else {
at = AtomWrite; at = AtomWrite;
flags = Output_Stream_f; flags |= Output_Stream_f;
} }
} else { } else {
at = AtomRead; at = AtomRead;
flags = Input_Stream_f; flags |= Input_Stream_f;
} }
Yap_initStream(sno, fd, fname, fname, LOCAL_encoding, flags, at, NULL); Atom name = Yap_LookupAtom(fname);
return sno; Yap_initStream(sno, st->file, name, MkAtomTerm( name ), LOCAL_encoding, st->status, at, NULL);
__android_log_print(ANDROID_LOG_INFO, "YAPDroid",
"exists %s <%d>", fname, sno);
return sno
;
} }
int Yap_FileStream(FILE *fd, char *name, Term file_name, int flags) { int Yap_FileStream(FILE *fd, char *name, Term file_name, int flags) {
@ -1589,14 +1588,13 @@ int Yap_FileStream(FILE *fd, char *name, Term file_name, int flags) {
return sno; return sno;
} }
int FileStream(FILE* fd, char *name, Term file_name, int flags ) int FileStream(FILE *fd, char *name, Term file_name, int flags) {
{
int sno; int sno;
Atom at; Atom at;
sno = GetFreeStreamD(); sno = GetFreeStreamD();
if (sno < 0) if (sno < 0)
return (PlIOError(RESOURCE_ERROR_MAX_STREAMS, name, return (PlIOError(RESOURCE_ERROR_MAX_STREAMS, MkAtomTerm(Yap_LookupAtom(name)),
"new stream not available for opening")); "new stream not available for opening"));
if (flags & Output_Stream_f) { if (flags & Output_Stream_f) {
if (flags & Append_Stream_f) if (flags & Append_Stream_f)
@ -1771,7 +1769,9 @@ static Int close1 /** @pred close(+ _S_) is iso
#define PAR(x, y, z) z #define PAR(x, y, z) z
typedef enum close_enum_choices { CLOSE_DEFS() } close_choices_t; typedef enum close_enum_choices {
CLOSE_DEFS()
} close_choices_t;
#undef PAR #undef PAR
@ -1830,6 +1830,7 @@ Term read_line(int sno) {
return (MkPairTerm(MkIntTerm(ch), tail)); return (MkPairTerm(MkIntTerm(ch), tail));
} }
#define ABSOLUTE_FILE_NAME_DEFS() \ #define ABSOLUTE_FILE_NAME_DEFS() \
PAR("access", isatom, ABSOLUTE_FILE_NAME_ACCESS) \ PAR("access", isatom, ABSOLUTE_FILE_NAME_ACCESS) \
, PAR("expand", booleanFlag, ABSOLUTE_FILE_NAME_EXPAND), \ , PAR("expand", booleanFlag, ABSOLUTE_FILE_NAME_EXPAND), \

View File

@ -80,7 +80,7 @@ Int Yap_CloseSocket(int, socket_info, socket_domain);
#endif /* USE_SOCKET */ #endif /* USE_SOCKET */
extern bool Yap_clearInput(int sno); extern bool Yap_clearInput(int sno);
extern Term Yap_read_term(int inp_stream, Term opts, bool clauatse); extern Term Yap_read_term(int inp_stream, Term opts, bool clause);
extern Term Yap_Parse(UInt prio, encoding_t enc, Term cmod); extern Term Yap_Parse(UInt prio, encoding_t enc, Term cmod);
extern void init_read_data(ReadData _PL_rd, struct stream_desc *s); extern void init_read_data(ReadData _PL_rd, struct stream_desc *s);
@ -216,7 +216,7 @@ INLINE_ONLY inline EXTERN void count_output_char(int ch, StreamDesc *s) {
inline static Term StreamName(int i) { return (GLOBAL_Stream[i].user_name); } inline static Term StreamName(int i) { return (GLOBAL_Stream[i].user_name); }
inline static Atom StreamFullName(int i) { return (GLOBAL_Stream[i].name); } inline static Atom StreamFullName(int i) { return (Atom)(GLOBAL_Stream[i].name); }
inline static void console_count_output_char(int ch, StreamDesc *s) { inline static void console_count_output_char(int ch, StreamDesc *s) {
CACHE_REGS CACHE_REGS

View File

@ -829,13 +829,13 @@ static parser_state_t initParser(Term opts, FEnv *fe, REnv *re, int inp_stream,
return YAP_SCANNING; return YAP_SCANNING;
} }
static parser_state_t scan(REnv *re, FEnv *fe, int inp_stream) { static parser_state_t scan(REnv *re, FEnv *fe, int sno) {
CACHE_REGS CACHE_REGS
/* preserve value of H after scanning: otherwise we may lose strings /* preserve value of H after scanning: otherwise we may lose strings
and floats */ and floats */
LOCAL_tokptr = LOCAL_toktide = LOCAL_tokptr = LOCAL_toktide =
Yap_tokenizer(GLOBAL_Stream + inp_stream, false, &fe->tpos); Yap_tokenizer(GLOBAL_Stream + sno, false, &fe->tpos);
#if DEBUG #if DEBUG
if (GLOBAL_Option[2]) { if (GLOBAL_Option[2]) {
TokEntry *t = LOCAL_tokptr; TokEntry *t = LOCAL_tokptr;
@ -857,7 +857,7 @@ static parser_state_t scan(REnv *re, FEnv *fe, int inp_stream) {
LOCAL_Error_TYPE = SYNTAX_ERROR; LOCAL_Error_TYPE = SYNTAX_ERROR;
return YAP_PARSING_ERROR; return YAP_PARSING_ERROR;
} }
return scanEOF(fe, inp_stream); return scanEOF(fe, sno);
} }
static parser_state_t scanError(REnv *re, FEnv *fe, int inp_stream) { static parser_state_t scanError(REnv *re, FEnv *fe, int inp_stream) {
@ -956,7 +956,7 @@ static parser_state_t parse(REnv *re, FEnv *fe, int inp_stream) {
* *
* *
*/ */
Term Yap_read_term(int inp_stream, Term opts, bool clause) { Term Yap_read_term(int sno, Term opts, bool clause) {
FEnv fe; FEnv fe;
REnv re; REnv re;
#if EMACS #if EMACS
@ -968,23 +968,23 @@ Term Yap_read_term(int inp_stream, Term opts, bool clause) {
while (true) { while (true) {
switch (state) { switch (state) {
case YAP_START_PARSING: case YAP_START_PARSING:
state = initParser(opts, &fe, &re, inp_stream, clause); state = initParser(opts, &fe, &re, sno, clause);
if (state == YAP_PARSING_FINISHED) { if (state == YAP_PARSING_FINISHED) {
pop_text_stack(lvl); pop_text_stack(lvl);
return 0; return 0;
} }
break; break;
case YAP_SCANNING: case YAP_SCANNING:
state = scan(&re, &fe, inp_stream); state = scan(&re, &fe, sno);
break; break;
case YAP_SCANNING_ERROR: case YAP_SCANNING_ERROR:
state = scanError(&re, &fe, inp_stream); state = scanError(&re, &fe, sno);
break; break;
case YAP_PARSING: case YAP_PARSING:
state = parse(&re, &fe, inp_stream); state = parse(&re, &fe, sno);
break; break;
case YAP_PARSING_ERROR: case YAP_PARSING_ERROR:
state = parseError(&re, &fe, inp_stream); state = parseError(&re, &fe, sno);
break; break;
case YAP_PARSING_FINISHED: { case YAP_PARSING_FINISHED: {
CACHE_REGS CACHE_REGS
@ -1021,17 +1021,17 @@ static Int
static Int read_term( static Int read_term(
USES_REGS1) { /* '$read2'(+Flag,?Term,?Module,?Vars,-Pos,-Err,+Stream) */ USES_REGS1) { /* '$read2'(+Flag,?Term,?Module,?Vars,-Pos,-Err,+Stream) */
int inp_stream; int sno;
Term out; Term out;
/* needs to change LOCAL_output_stream for write */ /* needs to change LOCAL_output_stream for write */
inp_stream = Yap_CheckTextStream(ARG1, Input_Stream_f, "read/3"); sno = Yap_CheckTextStream(ARG1, Input_Stream_f, "read/3");
if (inp_stream == -1) { if (sno == -1) {
return (FALSE); return (FALSE);
} }
out = Yap_read_term(inp_stream, add_output(ARG2, ARG3), false); out = Yap_read_term(sno, add_output(ARG2, ARG3), false);
UNLOCK(GLOBAL_Stream[inp_stream].streamlock); UNLOCK(GLOBAL_Stream[sno].streamlock);
return out != 0L; return out != 0L;
} }
@ -1060,7 +1060,7 @@ static const param_t read_clause_defs[] = {READ_CLAUSE_DEFS()};
#undef PAR #undef PAR
static xarg *setClauseReadEnv(Term opts, FEnv *fe, struct renv *re, static xarg *setClauseReadEnv(Term opts, FEnv *fe, struct renv *re,
int inp_stream) { int sno) {
CACHE_REGS CACHE_REGS
xarg *args = Yap_ArgListToVector(opts, read_clause_defs, READ_CLAUSE_END); xarg *args = Yap_ArgListToVector(opts, read_clause_defs, READ_CLAUSE_END);
@ -1082,7 +1082,7 @@ static xarg *setClauseReadEnv(Term opts, FEnv *fe, struct renv *re,
fe->cmod = PROLOG_MODULE; fe->cmod = PROLOG_MODULE;
} }
re->bq = getBackQuotesFlag(); re->bq = getBackQuotesFlag();
fe->enc = GLOBAL_Stream[inp_stream].encoding; fe->enc = GLOBAL_Stream[sno].encoding;
fe->sp = 0; fe->sp = 0;
fe->qq = 0; fe->qq = 0;
if (args[READ_CLAUSE_OUTPUT].used) { if (args[READ_CLAUSE_OUTPUT].used) {
@ -1118,12 +1118,12 @@ static xarg *setClauseReadEnv(Term opts, FEnv *fe, struct renv *re,
fe->vp = 0; fe->vp = 0;
} }
fe->ce = Yap_CharacterEscapes(fe->cmod); fe->ce = Yap_CharacterEscapes(fe->cmod);
re->seekable = (GLOBAL_Stream[inp_stream].status & Seekable_Stream_f) != 0; re->seekable = (GLOBAL_Stream[sno].status & Seekable_Stream_f) != 0;
if (re->seekable) { if (re->seekable) {
#if HAVE_FGETPOS #if HAVE_FGETPOS
fgetpos(GLOBAL_Stream[inp_stream].file, &re->rpos); fgetpos(GLOBAL_Stream[sno].file, &re->rpos);
#else #else
re->cpos = GLOBAL_Stream[inp_stream].charcount; re->cpos = GLOBAL_Stream[sno].charcount;
#endif #endif
} }
re->prio = LOCAL_default_priority; re->prio = LOCAL_default_priority;
@ -1166,15 +1166,15 @@ static Int read_clause2(USES_REGS1) {
*/ */
static Int read_clause( static Int read_clause(
USES_REGS1) { /* '$read2'(+Flag,?Term,?Module,?Vars,-Pos,-Err,+Stream) */ USES_REGS1) { /* '$read2'(+Flag,?Term,?Module,?Vars,-Pos,-Err,+Stream) */
int inp_stream; int sno;
Term out; Term out;
/* needs to change LOCAL_output_stream for write */ /* needs to change LOCAL_output_stream for write */
inp_stream = Yap_CheckTextStream(ARG1, Input_Stream_f, "read/3"); sno = Yap_CheckTextStream(ARG1, Input_Stream_f, "read/3");
if (inp_stream < 0) if (sno < 0)
return false; return false;
out = Yap_read_term(inp_stream, add_output(ARG2, ARG3), true); out = Yap_read_term(sno, add_output(ARG2, ARG3), true);
UNLOCK(GLOBAL_Stream[inp_stream].streamlock); UNLOCK(GLOBAL_Stream[sno].streamlock);
return out != 0; return out != 0;
} }
@ -1191,20 +1191,20 @@ static Int read_clause(
*/ */
#if 0 #if 0
static Int start_mega(USES_REGS1) { static Int start_mega(USES_REGS1) {
int inp_stream; int sno;
Term out; Term out;
Term t3 = Deref(ARG3); Term t3 = Deref(ARG3);
yhandle_t h = Yap_InitSlot(ARG2); yhandle_t h = Yap_InitSlot(ARG2);
TokENtry *tok; TokENtry *tok;
arity_t srity = 0; arity_t srity = 0;
/* needs to change LOCAL_output_stream for write */ /* needs to change LOCAL_output_stream for write */
inp_stream = Yap_CheckTextStream(ARG1, Input_Stream_f, "read_exo/3"); sno = Yap_CheckTextStream(ARG1, Input_Stream_f, "read_exo/3");
if (inp_stream < 0) if (sno < 0)
return false; return false;
/* preserve value of H after scanning: otherwise we may lose strings /* preserve value of H after scanning: otherwise we may lose strings
and floats */ and floats */
LOCAL_tokptr = LOCAL_toktide = LOCAL_tokptr = LOCAL_toktide =
x Yap_tokenizer(GLOBAL_Stream + inp_stream, false, &tpos); x Yap_tokenizer(GLOBAL_Stream + sno, false, &tpos);
if (tokptr->Tok == Name_tok && (next = tokptr->TokNext) != NULL && if (tokptr->Tok == Name_tok && (next = tokptr->TokNext) != NULL &&
next->Tok == Ponctuation_tok && next->TokInfo == TermOpenBracket) { next->Tok == Ponctuation_tok && next->TokInfo == TermOpenBracket) {
bool start = true; bool start = true;
@ -1253,16 +1253,16 @@ static Int source_location(USES_REGS1) {
*/ */
static Int read2( static Int read2(
USES_REGS1) { /* '$read2'(+Flag,?Term,?Module,?Vars,-Pos,-Err,+Stream) */ USES_REGS1) { /* '$read2'(+Flag,?Term,?Module,?Vars,-Pos,-Err,+Stream) */
int inp_stream; int sno;
Int out; Int out;
/* needs to change LOCAL_output_stream for write */ /* needs to change LOCAL_output_stream for write */
inp_stream = Yap_CheckTextStream(ARG1, Input_Stream_f, "read/3"); sno = Yap_CheckTextStream(ARG1, Input_Stream_f, "read/3");
if (inp_stream == -1) { if (sno == -1) {
return (FALSE); return (FALSE);
} }
out = Yap_read_term(inp_stream, add_output(ARG2, TermNil), false); out = Yap_read_term(sno, add_output(ARG2, TermNil), false);
UNLOCK(GLOBAL_Stream[inp_stream].streamlock); UNLOCK(GLOBAL_Stream[sno].streamlock);
return out; return out;
} }

View File

@ -52,7 +52,6 @@ static Int p_putenv(USES_REGS1);
static Term do_glob(const char *spec, bool ok_to); static Term do_glob(const char *spec, bool ok_to);
#ifdef MACYAP #ifdef MACYAP
static int chdir(char *);
/* #define signal skel_signal */ /* #define signal skel_signal */
#endif /* MACYAP */ #endif /* MACYAP */
static const char *expandVars(const char *spec, char *u); static const char *expandVars(const char *spec, char *u);
@ -104,7 +103,7 @@ static bool is_directory(const char *FileName) {
bool Yap_Exists(const char *f) { bool Yap_Exists(const char *f) {
VFS_t *vfs; VFS_t *vfs;
if ((vfs = vfs_owner(f))) { if ((vfs = vfs_owner(f))) {
return vfs->exists(vfs,f) != NULL; return vfs->exists(vfs,f);
} }
#if _WIN32 #if _WIN32
if (_access(f, 0) == 0) if (_access(f, 0) == 0)
@ -346,7 +345,7 @@ static char *PrologPath(const char *Y, char *X) { return (char *)Y; }
#define HAVE_REALPATH 1 #define HAVE_REALPATH 1
#endif #endif
bool ChDir(const char *path) { bool Yap_ChDir(const char *path) {
bool rc = false; bool rc = false;
char qp[FILENAME_MAX + 1]; char qp[FILENAME_MAX + 1];
const char *qpath = Yap_AbsoluteFile(path, qp, true); const char *qpath = Yap_AbsoluteFile(path, qp, true);
@ -1137,6 +1136,14 @@ static int volume_header(char *file) {
int Yap_volume_header(char *file) { return volume_header(file); } int Yap_volume_header(char *file) { return volume_header(file); }
const char *Yap_getcwd(const char *cwd, size_t cwdlen) { const char *Yap_getcwd(const char *cwd, size_t cwdlen) {
VFS_t *me = GLOBAL_VFS;
while(me) {
if (me->virtual_cwd) {
strcpy(cwd, me->virtual_cwd);
return cwd;
}
me = me->next;
}
#if _WIN32 || defined(__MINGW32__) #if _WIN32 || defined(__MINGW32__)
if (GetCurrentDirectory(cwdlen, (char *)cwd) == 0) { if (GetCurrentDirectory(cwdlen, (char *)cwd) == 0) {
Yap_WinError("GetCurrentDirectory failed"); Yap_WinError("GetCurrentDirectory failed");
@ -1166,7 +1173,7 @@ static Int working_directory(USES_REGS1) {
} }
if (t2 == TermEmptyAtom || t2 == TermDot) if (t2 == TermEmptyAtom || t2 == TermDot)
return true; return true;
return ChDir(RepAtom(AtomOfTerm(t2))->StrOfAE); return Yap_ChDir(RepAtom(AtomOfTerm(t2))->StrOfAE);
} }
/** Yap_findFile(): tries to locate a file, no expansion should be performed/ /** Yap_findFile(): tries to locate a file, no expansion should be performed/
@ -1188,11 +1195,21 @@ const char *Yap_findFile(const char *isource, const char *idef,
YAP_file_type_t ftype, bool expand_root, bool in_lib) { YAP_file_type_t ftype, bool expand_root, bool in_lib) {
char *save_buffer = NULL; char *save_buffer = NULL;
const char *root = iroot, *source = isource; char *root = iroot, *source = isource;
int rc = FAIL_RESTORE; int rc = FAIL_RESTORE;
int try int try = 0;
= 0;
bool abspath = false; bool abspath = false;
int lvl = push_text_stack();
root = Malloc(YAP_FILENAME_MAX+1);
source= Malloc(YAP_FILENAME_MAX+1);
if (iroot && iroot[0])
strcpy(root, iroot);
else
root[0] = 0;
if (isource && isource[0])
strcpy(source, isource);
else
source[0] = 0;
//__android_log_print(ANDROID_LOG_ERROR, "YAPDroid " __FUNCTION__, //__android_log_print(ANDROID_LOG_ERROR, "YAPDroid " __FUNCTION__,
// "try=%d %s %s", try, isource, iroot) ; } // "try=%d %s %s", try, isource, iroot) ; }
while (rc == FAIL_RESTORE) { while (rc == FAIL_RESTORE) {
@ -1201,21 +1218,28 @@ const char *Yap_findFile(const char *isource, const char *idef,
// { CACHE_REGS // { CACHE_REGS
switch (try ++) { switch (try ++) {
case 0: // path or file name is given; case 0: // path or file name is given;
root = iroot; if (!source[0] && idef && idef[0]) {
if (idef || isource) { strcpy(source, idef);
source = (isource ? isource : idef);
} }
if (source) { if (source[0]) {
abspath = Yap_IsAbsolutePath(source); abspath = Yap_IsAbsolutePath(source);
} }
if (!abspath && !root && ftype == YAP_BOOT_PL) { if (!abspath && !root[0] && ftype == YAP_BOOT_PL) {
root = YAP_PL_SRCDIR; root = YAP_PL_SRCDIR;
} }
break; break;
case 1: // library directory is given in command line case 1: // library directory is given in command line
if (in_lib && ftype == YAP_SAVED_STATE) { if (in_lib && ftype == YAP_SAVED_STATE) {
root = iroot; if (iroot && iroot[0])
source = (isource ? isource : idef); strcpy(root, iroot);
else
root[0] = 0;
if (isource && isource[0])
strcpy(source, isource);
else if (idef && idef[0])
strcpy(source, idef);
else
source[0] = 0;
} else { } else {
done = true; done = true;
} }
@ -1223,35 +1247,45 @@ const char *Yap_findFile(const char *isource, const char *idef,
case 2: // use environment variable YAPLIBDIR case 2: // use environment variable YAPLIBDIR
#if HAVE_GETENV #if HAVE_GETENV
if (in_lib) { if (in_lib) {
const char *eroot;
if (ftype == YAP_SAVED_STATE || ftype == YAP_OBJ) { if (ftype == YAP_SAVED_STATE || ftype == YAP_OBJ) {
root = getenv("YAPLIBDIR"); eroot = getenv("YAPLIBDIR");
} else if (ftype == YAP_BOOT_PL) { } else if (ftype == YAP_BOOT_PL) {
root = getenv("YAPSHAREDIR" eroot = getenv("YAPSHAREDIR"
"/pl"); "/pl");
if (root == NULL) { if (eroot == NULL) {
continue; continue;
} else { } else {
save_buffer = getFileNameBuffer(); strncpy(root, eroot, YAP_FILENAME_MAX);
strncpy(save_buffer, root, YAP_FILENAME_MAX); strncat(root, "/pl", YAP_FILENAME_MAX);
strncat(save_buffer, "/pl", YAP_FILENAME_MAX);
} }
} }
source = (isource ? isource : idef); if (isource && isource[0])
strcpy(source, isource);
else if (idef && idef[0])
strcpy(source, idef);
else
source[0] = 0;
} else } else
#endif #endif
done = true; done = true;
break; break;
case 3: // use compilation variable YAPLIBDIR case 3: // use compilation variable YAPLIBDIR
if (in_lib) { if (in_lib) {
source = (isource ? isource : idef); if (isource && isource[0])
strcpy(source, isource);
else if (idef && idef[0])
strcpy(source, idef);
else
source[0] = 0;
if (ftype == YAP_PL) { if (ftype == YAP_PL) {
root = YAP_SHAREDIR; strcpy(root,YAP_SHAREDIR);
} else if (ftype == YAP_BOOT_PL) { } else if (ftype == YAP_BOOT_PL) {
root = malloc(YAP_FILENAME_MAX+1);
strcpy(root, YAP_SHAREDIR); strcpy(root, YAP_SHAREDIR);
strcat(root,"/pl"); strcat(root,"/pl");
} else { } else {
root = YAP_LIBDIR; strcpy(root,YAP_LIBDIR);
} }
} else } else
done = true; done = true;
@ -1260,9 +1294,9 @@ const char *Yap_findFile(const char *isource, const char *idef,
case 4: // WIN stuff: registry case 4: // WIN stuff: registry
#if __WINDOWS #if __WINDOWS
if (in_lib) { if (in_lib) {
source = (ftype == YAP_PL || ftype == YAP_QLY ? "library" : "startup"); const char *key = (ftype == YAP_PL || ftype == YAP_QLY ? "library" : "startup");
source = Yap_RegistryGetString(source); strcpy( source, Yap_RegistryGetString(source) );
root = NULL; root[0] = 0;
} else } else
#endif #endif
done = true; done = true;
@ -1280,42 +1314,45 @@ const char *Yap_findFile(const char *isource, const char *idef,
if (pt) { if (pt) {
if (ftype == YAP_BOOT_PL) { if (ftype == YAP_BOOT_PL) {
#if __ANDROID__ #if __ANDROID__
root = "../../../files/Yap/pl"; strcpy(root, "../../../files/Yap/pl");
#else #else
root = "../../share/Yap/pl"; root = "../../share/Yap/pl";
#endif #endif
} else { } else {
root = (ftype == YAP_SAVED_STATE || ftype == YAP_OBJ strcpy(root, (ftype == YAP_SAVED_STATE || ftype == YAP_OBJ
? "../../lib/Yap" ? "../../lib/Yap"
: "../../share/Yap"); : "../../share/Yap"));
} }
if (root == iroot) { if (strcmp(root, iroot)==0) {
done = true; done = true;
continue; continue;
} }
if (!save_buffer) if (!save_buffer)
save_buffer = getFileNameBuffer(); save_buffer = Malloc(YAP_FILENAME_MAX+1);
if (Yap_findFile(source, NULL, root, save_buffer, access, ftype, if (Yap_findFile(source, NULL, root, save_buffer, access, ftype,
expand_root, in_lib)) expand_root, in_lib))
root = save_buffer; strcpy(root, save_buffer);
else else
done = true; done = true;
} else { } else {
done = true; done = true;
} }
source = (isource ? isource : idef); if (isource && isource[0])
strcpy(source, isource);
else if (idef && idef[0])
strcpy(source, idef);
else
source[0] = 0;
} }
break; break;
case 6: // default, try current directory case 6: // default, try current directory
if (!isource && ftype == YAP_SAVED_STATE) if (!isource && ftype == YAP_SAVED_STATE)
source = idef; strcpy(source, idef);
root = NULL; root[0] = 0;
break; break;
default: default:
if (save_buffer) pop_text_stack(lvl);
freeFileNameBuffer(save_buffer); return NULL;
return false;
} }
if (done) if (done)
@ -1324,12 +1361,11 @@ const char *Yap_findFile(const char *isource, const char *idef,
// "root= %s %s ", root, source) ; } // "root= %s %s ", root, source) ; }
const char *work = PlExpandVars(source, root, result); const char *work = PlExpandVars(source, root, result);
if (save_buffer)
freeFileNameBuffer(save_buffer);
// expand names in case you have // expand names in case you have
// to add a prefix // to add a prefix
if (!access || Yap_Exists(work)) { if (!access || Yap_Exists(work)) {
work = pop_output_text_stack(lvl,work);
return work; // done return work; // done
} else if (abspath) } else if (abspath)
return NULL; return NULL;

View File

@ -162,6 +162,7 @@ extern uint64_t Yap_StartOfWTimes;
extern bool Yap_HandleSIGINT(void); extern bool Yap_HandleSIGINT(void);
extern bool Yap_set_stream_to_buf(StreamDesc *st, const char *buf, size_t nchars USES_REGS); extern bool Yap_set_stream_to_buf(StreamDesc *st, const char *bufi,
size_t nchars USES_REGS);
#endif #endif

View File

@ -294,8 +294,8 @@ const char* jniStrError(int errnum, char* buf, size_t buflen) {
// char *strerror_r(int errnum, char *buf, size_t n); // char *strerror_r(int errnum, char *buf, size_t n);
return strerror_r(errnum, buf, buflen); return strerror_r(errnum, buf, buflen);
#else #else
int rc = strerror_r(errnum, buf, buflen); char *rc = strerror_r(errnum, buf, buflen);
if (rc != 0) { if (rc != NULL) {
// (POSIX only guarantees a value other than 0. The safest // (POSIX only guarantees a value other than 0. The safest
// way to implement this function is to use C++ and overload on the // way to implement this function is to use C++ and overload on the
// type of strerror_r to accurately distinguish GNU from POSIX.) // type of strerror_r to accurately distinguish GNU from POSIX.)