Merge 192.168.1.103:github/YAPDroid/app/src/yap

This commit is contained in:
Vitor Santos Costa 2017-12-01 10:56:19 +00:00
commit b645f60405
42 changed files with 3079 additions and 2992 deletions

View File

@ -209,8 +209,8 @@ bool YAP_get_blob(Term t, void **blob, size_t *len, blob_type_t **type) {
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 (len)

View File

@ -33,7 +33,6 @@
#include <unistd.h>
#endif
#include <stdlib.h>
#include <string.h>
#if HAVE_STDARG_H
@ -62,7 +61,6 @@
#endif /* YAPOR */
#include "cut_c.h"
#include "threads.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 Term YAP_MkAtomTerm(Atom n) {
X_API Term YAP_MkAtomTerm(YAP_Atom n) {
Term t;
t = MkAtomTerm(n);
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;
int32_t v;
while (*s) {
@ -448,14 +446,14 @@ X_API bool YAP_IsWideAtom(Atom a) {
return false;
}
X_API const char *YAP_AtomName(Atom a) {
X_API const char *YAP_AtomName(YAP_Atom a) {
const char *o;
o = AtomName(a);
return (o);
}
X_API const wchar_t *YAP_WideAtomName(Atom a) {
X_API const wchar_t *YAP_WideAtomName(YAP_Atom a) {
int32_t v;
const unsigned char *s = RepAtom(a)->UStrOfAE;
size_t n = strlen_utf8(s);
@ -470,7 +468,7 @@ X_API const wchar_t *YAP_WideAtomName(Atom a) {
return dest;
}
X_API Atom YAP_LookupAtom(const char *c) {
X_API YAP_Atom YAP_LookupAtom(const char *c) {
CACHE_REGS
Atom a;
@ -488,7 +486,7 @@ X_API Atom YAP_LookupAtom(const char *c) {
return NULL;
}
X_API Atom YAP_LookupWideAtom(const wchar_t *c) {
X_API YAP_Atom YAP_LookupWideAtom(const wchar_t *c) {
CACHE_REGS
Atom a;
@ -506,7 +504,7 @@ X_API Atom YAP_LookupWideAtom(const wchar_t *c) {
return NULL;
}
X_API Atom YAP_FullLookupAtom(const char *c) {
X_API YAP_Atom YAP_FullLookupAtom(const char *c) {
CACHE_REGS
Atom at;
@ -524,7 +522,7 @@ X_API Atom YAP_FullLookupAtom(const char *c) {
return NULL;
}
X_API size_t YAP_AtomNameLength(Atom at) {
X_API size_t YAP_AtomNameLength(YAP_Atom at) {
if (IsBlob(at)) {
return RepAtom(at)->rep.blob->length;
}
@ -648,7 +646,7 @@ X_API Int YAP_SkipList(Term *l, Term **tailp) {
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
Term t;
BACKUP_H();
@ -662,7 +660,7 @@ X_API Term YAP_MkApplTerm(Functor f, UInt arity, Term args[]) {
return t;
}
X_API Term YAP_MkNewApplTerm(Functor f, UInt arity) {
X_API Term YAP_MkNewApplTerm(YAP_Functor f, UInt arity) {
CACHE_REGS
Term t;
BACKUP_H();
@ -676,7 +674,7 @@ X_API Term YAP_MkNewApplTerm(Functor f, UInt arity) {
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)); }
@ -688,11 +686,11 @@ X_API Term *YAP_ArgsOfTerm(Term t) {
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) {
CACHE_REGS
@ -1703,21 +1701,21 @@ X_API void YAP_Error(int myerrno, Term t, const char *buf, ...) {
Yap_Error(myerrno, t, tmpbuf);
}
X_API PredEntry *YAP_FunctorToPred(Functor func) {
X_API YAP_PredEntryPtr YAP_FunctorToPred(YAP_Functor func) {
CACHE_REGS
return RepPredProp(PredPropByFunc(func, CurrentModule));
}
X_API PredEntry *YAP_AtomToPred(Atom at) {
X_API YAP_PredEntryPtr YAP_AtomToPred(YAP_Atom at) {
CACHE_REGS
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));
}
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));
}
@ -1729,8 +1727,9 @@ static int run_emulator(USES_REGS1) {
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
PredEntry *pe = ape;
bool out;
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];
if (blob_tag < USER_BLOB_START ||
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;
}
YAP_opaque_tag_t blob_info = blob_tag;
@ -2139,11 +2139,11 @@ X_API int YAP_InitConsult(int mode, const char *fname, char *full,
Yap_CloseStream(sno);
pop_text_stack(lvl);
sno = -1;
return sno;
}
GLOBAL_Stream[sno].name = Yap_LookupAtom(fl);
GLOBAL_Stream[sno].user_name = MkAtomTerm(Yap_LookupAtom(fname));
GLOBAL_Stream[sno].encoding = LOCAL_encoding;
pop_text_stack(lvl);
RECOVER_MACHINE_REGS();
UNLOCK(GLOBAL_Stream[sno].streamlock);
return sno;
@ -2151,8 +2151,7 @@ X_API int YAP_InitConsult(int mode, const char *fname, char *full,
/// given a stream descriptor or stream alias (see open/3),
/// return YAP's internal handle.
X_API void *YAP_GetStreamFromId(int no)
{
X_API void *YAP_GetStreamFromId(int no) {
return GLOBAL_Stream + no;
}
@ -2195,18 +2194,17 @@ X_API Term YAP_ReadFromStream(int sno) {
Term o;
BACKUP_MACHINE_REGS();
o = Yap_read_term(sno, TermNil, 1);
o = Yap_read_term(sno, TermNil, false);
RECOVER_MACHINE_REGS();
return o;
}
X_API Term YAP_ReadClauseFromStream(int sno) {
Term o;
BACKUP_MACHINE_REGS();
o = Yap_read_term(sno, TermNil, -1);
Term t = Yap_read_term(sno, TermNil, true);
RECOVER_MACHINE_REGS();
return o;
return t;
}
X_API void YAP_Write(Term t, FILE *f, int flags) {
@ -2253,7 +2251,8 @@ X_API char *YAP_WriteBuffer(Term t, char *buf, size_t sze, int flags) {
} else {
return pop_output_text_stack(l, out.val.c);
}
}}
}
}
/// write a a term to n user-provided buffer: make sure not tp
@ -2263,7 +2262,7 @@ X_API int YAP_WriteDynamicBuffer(YAP_Term t, char *buf, size_t sze,
char *b;
BACKUP_MACHINE_REGS();
b = Yap_TermToString(t, enc, flags);
b = Yap_TermToBuffer(t, enc, flags);
strncpy(buf, b, sze);
buf[sze] = 0;
RECOVER_MACHINE_REGS();
@ -2307,7 +2306,7 @@ static int yap_lineno = 0;
/* do initial boot by consulting the file boot.yap */
static void do_bootfile(const char *b_file USES_REGS) {
Term t;
int bootfile, osno;
int boot_stream, osno;
Functor functor_query = Yap_MkFunctor(Yap_LookupAtom("?-"), 1);
Functor functor_command1 = Yap_MkFunctor(Yap_LookupAtom(":-"), 1);
@ -2315,9 +2314,9 @@ static void do_bootfile(const char *b_file USES_REGS) {
char *full = malloc(YAP_FILENAME_MAX + 1);
full[0] = '\0';
/* the consult mode does not matter here, really */
bootfile = YAP_InitConsult(YAP_BOOT_MODE, b_file, full, &osno);
if (bootfile < 0) {
fprintf(stderr, "[ FATAL ERROR: could not open bootfile %s ]\n",
boot_stream = YAP_InitConsult(YAP_BOOT_MODE, b_file, full, &osno);
if (boot_stream < 0) {
fprintf(stderr, "[ FATAL ERROR: could not open boot_stream %s ]\n",
b_file);
exit(1);
}
@ -2326,19 +2325,19 @@ free(full);
CACHE_REGS
YAP_Reset(YAP_FULL_RESET);
Yap_StartSlots();
t = YAP_ReadClauseFromStream(bootfile);
t = YAP_ReadClauseFromStream(boot_stream);
// Yap_DebugPlWriteln(t);
if (t == 0) {
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);
} else if (YAP_IsVarTerm(t) || t == TermNil) {
fprintf(stderr, "[ line %d: term cannot be compiled ]", yap_lineno);
} else if (YAP_IsPairTerm(t)) {
fprintf(stderr, "[ SYSTEM ERROR: consult not allowed in boot file ]\n");
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 ||
FunctorOfTerm(t) == functor_command1)) {
YAP_RunGoalOnce(ArgOfTerm(1, t));
@ -2361,7 +2360,7 @@ free(full);
}
} while (t != TermEof);
YAP_EndConsult(bootfile, &osno);
YAP_EndConsult(boot_stream, &osno);
#if DEBUG
if (Yap_output_msg)
fprintf(stderr, "Boot loaded\n");
@ -2425,12 +2424,15 @@ free(full);
CELL Trail = 0, Stack = 0, Heap = 0, Atts = 0;
char *boot_file;
Int rc;
const char *yroot;
boot_file = calloc(YAP_FILENAME_MAX + 1, 1);
/* ignore repeated calls to YAP_Init */
char *yroot;
if (YAP_initialized)
return YAP_FOUND_BOOT_ERROR;
if (!LOCAL_TextBuffer)
LOCAL_TextBuffer = Yap_InitTextAllocator();
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_page_size = Yap_InitPageSize(); /* init memory page size, required by
later functions */
@ -2446,25 +2448,28 @@ free(full);
GLOBAL_argc = yap_init->Argc;
if (0 && ((YAP_QLY && yap_init->SavedState) ||
(YAP_BOOT_PL && (yap_init->YapPrologBootFile)))) {
yroot = ".";
strcpy(yroot, ".");
} else {
yroot = BootFilePath;
strcpy(yroot, BootFile);
}
}
char *tmp = 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 (yap_init->assetManager)
Yap_InitAssetManager();
#endif
#if USE_DL_MALLOC
if (yap_init->SavedState == NULL)
yap_init->SavedState = YAP_STARTUP;
#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);
#endif
if (yap_init->SavedState == NULL) {
@ -2473,13 +2478,19 @@ LOCAL_TextBuffer = Yap_InitTextAllocator();
if (restore_result == YAP_BOOT_PL) {
#if USE_DL_MALLOC
if (yap_init->YapPrologBootFile == NULL)
if (yap_init->YapPrologBootFile == NULL ||
yap_init->YapPrologBootFile[0] == 0)
yap_init->YapPrologBootFile = BootFile;
strcpy(boot_file, BootFile);
}
#else
const char *s = Yap_findFile(yap_init->YapPrologBootFile, BootFile, yroot, boot_file,
strcpy(boot_file, BootFile);
const char *s = Yap_findFile(yap_init->YapPrologBootFile,
BootFile, yroot, boot_file,
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
}
@ -2504,7 +2515,8 @@ LOCAL_TextBuffer = Yap_InitTextAllocator();
}
#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->DelayedReleaseLoad);
//
@ -2521,15 +2533,19 @@ LOCAL_TextBuffer = Yap_InitTextAllocator();
restore will print out messages ....
*/
setBooleanGlobalPrologFlag(HALT_AFTER_CONSULT_FLAG,
yap_init->HaltAfterConsult);
yap_init
->HaltAfterConsult);
}
/* tell the system who should cope with interrupts */
Yap_ExecutionMode = yap_init->ExecutionMode;
if (do_bootstrap) {
restore_result |= YAP_BOOT_PL;
restore_result |=
YAP_BOOT_PL;
} else { // try always to boot from the saved state.
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)) {
restore_result = YAP_BOOT_PL;
} else {
@ -2543,7 +2559,9 @@ LOCAL_TextBuffer = Yap_InitTextAllocator();
}
GLOBAL_FAST_BOOT_FLAG = yap_init->FastBoot;
#if defined(YAPOR) || defined(TABLING)
Yap_init_root_frames();
#endif /* YAPOR || TABLING */
#ifdef YAPOR
Yap_init_yapor_workers();
@ -2590,56 +2608,72 @@ LOCAL_TextBuffer = Yap_InitTextAllocator();
}
if (yap_init->YapPrologRCFile) {
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
has been overwritten ....
*/
setBooleanGlobalPrologFlag(HALT_AFTER_CONSULT_FLAG,
yap_init->HaltAfterConsult);
yap_init
->HaltAfterConsult);
}
if (yap_init->YapPrologTopLevelGoal) {
Yap_PutValue(AtomTopLevelGoal,
MkAtomTerm(Yap_LookupAtom(yap_init->YapPrologTopLevelGoal)));
MkAtomTerm(Yap_LookupAtom(yap_init->YapPrologTopLevelGoal))
);
}
if (yap_init->YapPrologGoal) {
Yap_PutValue(AtomInitGoal,
MkAtomTerm(Yap_LookupAtom(yap_init->YapPrologGoal)));
MkAtomTerm(Yap_LookupAtom(yap_init->YapPrologGoal))
);
}
if (yap_init->YapPrologAddPath) {
Yap_PutValue(AtomExtendFileSearchPath,
MkAtomTerm(Yap_LookupAtom(yap_init->YapPrologAddPath)));
MkAtomTerm(Yap_LookupAtom(yap_init->YapPrologAddPath))
);
}
if (yap_init->YapShareDir) {
setAtomicGlobalPrologFlag(PROLOG_LIBRARY_DIRECTORY_FLAG,
MkAtomTerm(Yap_LookupAtom(yap_init->YapShareDir)));
MkAtomTerm(Yap_LookupAtom(yap_init->YapShareDir))
);
}
if (yap_init->YapLibDir) {
setAtomicGlobalPrologFlag(PROLOG_FOREIGN_DIRECTORY_FLAG,
MkAtomTerm(Yap_LookupAtom(yap_init->YapLibDir)));
MkAtomTerm(Yap_LookupAtom(yap_init->YapLibDir))
);
}
if (yap_init->QuietMode) {
setVerbosity(TermSilent);
}
if (restore_result == YAP_QLY) {
setAtomicGlobalPrologFlag(RESOURCE_DATABASE_FLAG,
MkAtomTerm(Yap_LookupAtom(yap_init->SavedState)));
LOCAL_PrologMode &= ~BootMode;
MkAtomTerm(Yap_LookupAtom(yap_init->SavedState))
);
LOCAL_PrologMode &=
~BootMode;
CurrentModule = LOCAL_SourceModule = USER_MODULE;
setBooleanGlobalPrologFlag(SAVED_PROGRAM_FLAG, true);
setBooleanGlobalPrologFlag(SAVED_PROGRAM_FLAG,
true);
rc = YAP_QLY;
} else {
if (boot_file[0] == '\0')
strcpy(boot_file, BootFile);
strcpy(boot_file, BootFile
);
do_bootfile(boot_file PASS_REGS);
setAtomicGlobalPrologFlag(
RESOURCE_DATABASE_FLAG,
MkAtomTerm(Yap_LookupAtom(boot_file)));
setBooleanGlobalPrologFlag(SAVED_PROGRAM_FLAG, false);
MkAtomTerm(Yap_LookupAtom(boot_file))
);
setBooleanGlobalPrologFlag(SAVED_PROGRAM_FLAG,
false);
}
start_modules();
YAP_initialized = true;
return YAP_BOOT_PL;
return
YAP_BOOT_PL;
}
#if (DefTrailSpace < MinTrailSpace)
@ -2674,9 +2708,9 @@ LOCAL_TextBuffer = Yap_InitTextAllocator();
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) {
return Yap_compare_terms(t1, t2);
@ -2740,7 +2774,7 @@ LOCAL_TextBuffer = Yap_InitTextAllocator();
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) {
return ((void *) RepPredProp(PredPropByAtom(a, m)));
} else {
@ -2749,7 +2783,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;
if (pd->ArityOfPE) {
*arity = pd->ArityOfPE;
@ -2764,42 +2798,35 @@ LOCAL_TextBuffer = Yap_InitTextAllocator();
*m = TermProlog;
}
X_API void YAP_UserCPredicate(const char *name, CPredicate def, arity_t arity) {
Yap_InitCPred(name, arity, def, UserCPredFlag);
X_API void YAP_UserCPredicate(const char *name, YAP_UserCPred def, YAP_Arity arity) {
Yap_InitCPred(name, arity, (CPredicate)def, UserCPredFlag);
}
X_API void YAP_UserBackCPredicate_(const char *name, CPredicate init,
CPredicate cont, arity_t arity,
X_API void YAP_UserBackCPredicate_(const char *name, YAP_UserCPred init,
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) {
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,
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,
X_API void YAP_UserCPredicateWithArgs(const char *a, YAP_UserCPred f,
arity_t arity, Term mod) {
CACHE_REGS
PredEntry *pe;
Term cm = CurrentModule;
CurrentModule = mod;
YAP_UserCPredicate(a, f, arity);
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;
Yap_InitCPred(a, arity, (CPredicate)f, UserCPredFlag|CArgsPredFlag );
CurrentModule = cm;
}
@ -2815,7 +2842,7 @@ LOCAL_TextBuffer = Yap_InitTextAllocator();
return omod;
}
X_API Term YAP_CreateModule(Atom at) {
X_API Term YAP_CreateModule(YAP_Atom at) {
Term t;
WRITE_LOCK(RepAtom(at)->ARWLock);
t = Yap_Module(MkAtomTerm(at));
@ -2871,13 +2898,13 @@ LOCAL_TextBuffer = Yap_InitTextAllocator();
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) {
Agc_hook old = GLOBAL_AGCHook;
GLOBAL_AGCHook = hook;
X_API YAP_agc_hook YAP_AGCRegisterHook(YAP_agc_hook hook) {
YAP_agc_hook old = (YAP_agc_hook)GLOBAL_AGCHook;
GLOBAL_AGCHook = (Agc_hook)hook;
return old;
}
@ -3202,11 +3229,12 @@ LOCAL_TextBuffer = Yap_InitTextAllocator();
X_API Term YAP_ModuleUser(void) { return MkAtomTerm(AtomUser); }
/* 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;
}
X_API int YAP_MaxOpPriority(Atom at, Term module) {
X_API int YAP_MaxOpPriority(YAP_Atom at, Term module) {
AtomEntry *ae = RepAtom(at);
OpEntry *info;
WRITE_LOCK(ae->ARWLock);
@ -3224,7 +3252,7 @@ LOCAL_TextBuffer = Yap_InitTextAllocator();
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);
OpEntry *info;
int n;
@ -3445,7 +3473,7 @@ LOCAL_TextBuffer = Yap_InitTextAllocator();
size_t AtomTranslations, MaxAtomTranslations;
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);
if (te != NIL)
return te->Translation;
@ -3468,10 +3496,10 @@ LOCAL_TextBuffer = Yap_InitTextAllocator();
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) {
Atom At = NameOfFunctor(f);
X_API Int YAP_FunctorToInt(YAP_Functor f) {
YAP_Atom At = NameOfFunctor(f);
arity_t arity = ArityOfFunctor(f);
TranslationEntry *te = Yap_GetTranslationProp(At, arity);
if (te != NIL)
@ -3498,12 +3526,12 @@ LOCAL_TextBuffer = Yap_InitTextAllocator();
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 PredEntry *YAP_TopGoal(void) {
YAP_Functor f = Yap_MkFunctor(Yap_LookupAtom("yap_query"), 3);
X_API YAP_PredEntryPtr YAP_TopGoal(void) {
Functor f = Yap_MkFunctor(Yap_LookupAtom("yap_query"), 3);
Term tmod = MkAtomTerm(Yap_LookupAtom("yapi"));
PredEntry *p = RepPredProp(Yap_GetPredPropByFunc(f, tmod));
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);
}
Term Yap_FetchTermFromDB(DBTerm *ref) {
Term Yap_FetchTermFromDB(void *ref) {
CACHE_REGS
return GetDBTerm(ref, FALSE PASS_REGS);
}
Term Yap_FetchClauseTermFromDB(DBTerm *ref) {
Term Yap_FetchClauseTermFromDB(void *ref) {
CACHE_REGS
return GetDBTerm(ref, TRUE PASS_REGS);
}
Term Yap_PopTermFromDB(DBTerm *ref) {
Term Yap_PopTermFromDB(void *ref) {
CACHE_REGS
Term t = GetDBTerm(ref, FALSE PASS_REGS);
@ -5304,7 +5304,7 @@ static void ReleaseTermFromDB(DBTerm *ref USES_REGS) {
FreeDBSpace((char *)ref);
}
void Yap_ReleaseTermFromDB(DBTerm *ref) {
void Yap_ReleaseTermFromDB(void *ref) {
CACHE_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);
}
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->classAsText =
Yap_LookupAtom(Yap_errorClassName(LOCAL_ActiveError->errorClass));
Yap_errorClassName(LOCAL_ActiveError->errorClass);
LOCAL_ActiveError->errorLine = lineno;
LOCAL_ActiveError->errorFunction = function;
LOCAL_ActiveError->errorFile = file;

View File

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

View File

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

View File

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

View File

@ -487,7 +487,7 @@ unsigned char *Yap_readText(seq_tv_t *inp USES_REGS) {
#endif
if (inp->type & YAP_STRING_TERM) {
// Yap_DebugPlWriteln(inp->val.t);
char *s = (char *)Yap_TermToString(inp->val.t, ENC_ISO_UTF8, 0);
char *s = (char *) Yap_TermToBuffer(inp->val.t, ENC_ISO_UTF8, 0);
return inp->val.uc = (unsigned char *)s;
}
if (inp->type & YAP_STRING_CHARS) {

View File

@ -501,7 +501,7 @@ unsigned char *Yap_readText(seq_tv_t *inp, size_t *lengp) {
#endif
if (inp->type & YAP_STRING_TERM) {
// Yap_DebugPlWriteln(inp->val.t);
char *s = (char *)Yap_TermToString(inp->val.t, lengp, ENC_ISO_UTF8, 0);
char *s = (char *) Yap_TermToBuffer(inp->val.t, lengp, ENC_ISO_UTF8, 0);
return inp->val.uc = (unsigned char *)s;
}
if (inp->type & YAP_STRING_CHARS) {

View File

@ -87,7 +87,7 @@ static char *send_tracer_message(char *start, char *name, arity_t arity,
continue;
}
}
const char *sn = Yap_TermToString(args[i], LOCAL_encoding,
const char *sn = Yap_TermToBuffer(args[i], LOCAL_encoding,
Quote_illegal_f | Handle_vars_f);
size_t sz;
if (sn == NULL) {

View File

@ -1255,7 +1255,7 @@ static void wrputref(CODEADDR ref, int Quote_illegal,
Yap_CloseSlots(sls);
}
char *Yap_TermToString(Term t, encoding_t enc, int flags) {
char *Yap_TermToBuffer(Term t, encoding_t enc, int flags) {
CACHE_REGS
int sno = Yap_open_buf_write_stream(enc, flags);
const char *sf;

View File

@ -147,14 +147,14 @@ static int dump_runtime_variables(void) {
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[]) {
YAP_init_args *iap = x;
memset(iap, 0, sizeof(YAP_init_args));
#if __ANDROID__
iap->boot_file_type = YAP_BOOT_PL;
iap->SavedState = malloc(strlen(saved_state)+1);
strcpy(iap->SavedState, saved_state);
iap->assetManager = true;
iap->SavedState = NULL;
iap->assetManager = NULL;
#else
iap->boot_file_type = YAP_QLY;
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="))) {
GLOBAL_Home = p + strlen("-home=");
} 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",
strerror(errno));
}

View File

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

View File

@ -12,14 +12,14 @@ extern "C" {
#include "YapInterface.h"
#include "blobs.h"
X_API char *Yap_TermToString(Term t, encoding_t encodingp,
X_API char *Yap_TermToBuffer(Term t, encoding_t encodingp,
int flags);
X_API void YAP_UserCPredicate(const char *, YAP_UserCPred, arity_t arity);
X_API void YAP_UserCPredicateWithArgs(const char *, YAP_UserCPred, arity_t,
YAP_Term);
X_API void YAP_UserBackCPredicate(const char *, YAP_UserCPred, YAP_UserCPred,
arity_t, arity_t);
YAP_Arity, YAP_Arity);
#if YAP_PYTHON
X_API bool do_init_python(void);
@ -934,17 +934,11 @@ void YAPEngine::doInit(YAP_file_type_t BootMode)
}
/* Begin preprocessor code */
/* live */
__android_log_print(ANDROID_LOG_INFO, "YAPDroid", "initialize_prolog");
#if __ANDROID__
Yap_AndroidBufp = (char *)malloc(Yap_AndroidMax = 4096);
Yap_AndroidBufp[0] = '\0';
Yap_AndroidSz = 0;
#endif
//yerror = YAPError();
#if YAP_PYTHON
do_init_python();
#endif
YAP_PredEntryPtr p = YAP_AtomToPred( YAP_LookupAtom("initialize_prolog") );
YAPPredicate p = YAPPredicate( YAPAtomTerm("initialize_prolog") );
YAPQuery initq = YAPQuery(YAPPredicate(p), nullptr);
if (initq.next())
{
@ -992,7 +986,7 @@ YAPEngine::YAPEngine(int argc, char *argv[],
}
/// 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
Term m = Yap_CurrentModule(), t = tt.term();
@ -1015,9 +1009,11 @@ YAPEngine::YAPEngine(int argc, char *argv[],
else if (IsPairTerm(t))
{
Term ts[2];
ts[0] = t;
ts[1] = m;
t = Yap_MkApplTerm(FunctorCsult, 2, ts);
Functor FunctorConsult = Yap_MkFunctor(Yap_LookupAtom("consult"), 1);
ts[1] = t;
ts[0] = m;
t = Yap_MkApplTerm(FunctorModule, 2, ts);
t = Yap_MkApplTerm(FunctorConsult, 1, &t);
tt.put(t);
outp = RepAppl(t) + 1;
}
@ -1110,7 +1106,7 @@ YAPEngine::YAPEngine(int argc, char *argv[],
if (LOCAL_ActiveError->prologPredLine)
{
s += "\n";
s += LOCAL_ActiveError->prologPredFile->StrOfAE;
s += LOCAL_ActiveError->prologPredFile;
s += ":";
sprintf(buf, "%ld", (long int)LOCAL_ActiveError->prologPredLine);
s += buf; // std::to_string(LOCAL_ActiveError->prologPredLine) ;
@ -1118,7 +1114,7 @@ YAPEngine::YAPEngine(int argc, char *argv[],
s += ":0 ";
s += LOCAL_ActiveError->prologPredModule;
s += ":";
s += (LOCAL_ActiveError->prologPredName)->StrOfAE;
s += (LOCAL_ActiveError->prologPredName);
s += "/";
sprintf(buf, "%ld", (long int)LOCAL_ActiveError->prologPredArity);
s += // std::to_string(LOCAL_ActiveError->prologPredArity);
@ -1126,13 +1122,13 @@ YAPEngine::YAPEngine(int argc, char *argv[],
}
s += " error ";
if (LOCAL_ActiveError->classAsText != nullptr)
s += LOCAL_ActiveError->classAsText->StrOfAE;
s += LOCAL_ActiveError->classAsText;
s += ".";
s += LOCAL_ActiveError->errorAsText->StrOfAE;
s += LOCAL_ActiveError->errorAsText;
s += ".\n";
if (LOCAL_ActiveError->errorTerm)
{
Term t = LOCAL_ActiveError->errorTerm->Entry;
Term t = Yap_PopTermFromDB(LOCAL_ActiveError->errorTerm);
if (t)
{
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,
YAP_Arity, YAP_Term);
X_API extern void UserBackCPredicate(const char *name, int *init(), int *cont(),
int arity, int extra);
X_API extern void YAP_UserBackCPredicate(const char *name,
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);

View File

@ -185,16 +185,14 @@ public:
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
struct X_API YAPEngineArgs: YAP_init_args {
public:
YAPEngineArgs():yap_boot_params() {
char s[32];
strcpy(s, "startup.yss" );
Yap_InitDefaults(this,s,0,nullptr);
#if YAP_PYTHON
Embedded = true;
python_in_python = Py_IsInitialized();

View File

@ -241,7 +241,7 @@ public:
char *os;
BACKUP_MACHINE_REGS();
if (!(os = Yap_TermToString(Yap_GetFromSlot(t), enc, Handle_vars_f))) {
if (!(os = Yap_TermToBuffer(Yap_GetFromSlot(t), enc, Handle_vars_f))) {
RECOVER_MACHINE_REGS();
return 0;
}

View File

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

View File

@ -17,9 +17,6 @@
#include "YapTermConfig.h"
#include "config.h"
typedef void *Functor;
typedef void *Atom;
#endif
#if HAVE_STDINT_H
@ -29,6 +26,22 @@ typedef void *Atom;
#include <inttypes.h>
#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) \
(((CELL)(X) + (sizeof(TYPE) - 1)) & ~(sizeof(TYPE) - 1))
@ -45,59 +58,98 @@ typedef void *Atom;
#if defined(PRIdPTR)
typedef intptr_t Int;
typedef uintptr_t UInt;
typedef intptr_t YAP_Int;
typedef uintptr_t YAP_UInt;
#elif defined(_WIN64)
typedef int64_t Int;
typedef uint64_t UInt;
typedef int64_t YAP_Int;
typedef uint64_t YAP_UInt;
#elif defined(_WIN32)
typedef int32_t Int;
typedef uint32_t UInt;
typedef int32_t YAP_Int;
typedef uint32_t YAP_UInt;
#elif SIZEOF_LONG_INT == SIZEOF_INT_P
typedef long int Int;
typedef unsigned long int UInt;
typedef long int YAP_Int;
typedef unsigned long int YAP_UInt;
#elif SIZEOF_INT == SIZEOF_INT_P
typedef int Int;
typedef unsigned int UInt;
typedef int YAP_Int;
typedef unsigned int YAP_UInt;
#else
#error Yap require integer types of the same size as a pointer
#endif
/* */ typedef short int Short;
/* */ typedef unsigned short int UShort;
/* */ typedef short int YAP_Short;
/* */ 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 int16_t SBITS16;
typedef uint32_t BITS32;
typedef YAP_CELL CELL;
typedef YAP_Term Term;
#define WordSize sizeof(BITS16)
#define CellSize sizeof(CELL)
#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"
/*************************************************************************************************
type casting macros
*************************************************************************************************/
typedef UInt Term;
typedef Int yhandle_t;
typedef double Float;
#if SIZEOF_INT < SIZEOF_INT_P
#define SHORT_INTS 1
#else

View File

@ -416,7 +416,7 @@ extern AAssetManager *Yap_assetManager;
extern void *Yap_openAssetFile(const char *path);
extern bool Yap_isAsset(const char *path);
#endif
extern const char *Yap_getcwd(const char *, size_t);
extern const char *Yap_getcwd( char *, size_t);
extern void Yap_cputime_interval(Int *, Int *);
extern void Yap_systime_interval(Int *, Int *);
extern void Yap_InitSysbits(int wid);

View File

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

View File

@ -80,7 +80,7 @@ typedef struct vfs {
const char *suffix;
bool (*chDir)(struct vfs *me, const char *s);
/** 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)
bool (*close)(int sno); /// close the object
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
bool (*closedir)(void *d);
; /// 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.
bool (*isdir)(struct vfs *,const char *s); /// verify whether is directory.
bool (*exists)(struct vfs *, const char *s); /// verify whether a file exists.

View File

@ -50,23 +50,10 @@
#include <stdio.h>
#include <stdlib.h>
#ifdef YAP_H
/* The YAP main types */
#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
#include <stdint.h>
#endif
@ -74,21 +61,6 @@
#include <inttypes.h>
#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,
still widely used.
@ -103,79 +75,26 @@ typedef int _Bool;
typedef bool YAP_Bool;
#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
#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 void *YAP_PredEntryPtr;
typedef size_t YAP_Arity;
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 struct YAP_pred_entry *YAP_PredEntryPtr;
typedef void *YAP_PredEntryPtr;
typedef YAP_Bool (*YAP_UserCPred)(void);
@ -187,8 +106,6 @@ typedef int (*YAP_agc_hook)(void *_Atom);
typedef encoding_t YAP_encoding_t;
#endif
#if __ANDROID__
#include <android/asset_manager.h>
#include <android/native_activity.h>
@ -257,6 +174,10 @@ typedef enum {
#define YAP_RECONSULT_MODE 1
#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 {
//> boot type as suggested by the user
YAP_file_type_t boot_file_type;
@ -351,16 +272,8 @@ typedef struct yap_boot_params {
int ErrorNo;
//> errorstring
char *ErrorCause;
#ifdef __cplusplus
void YAP_init_args();
#endif
} 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 */
typedef struct {
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
} yap_error_stage_t;
/// a Prolo goal that caused a bug
/// a Prolog goal that caused a bug
typedef struct error_prolog_source {
YAP_Int prologPredCl;
YAP_UInt prologPredLine;
YAP_UInt prologPredFirstLine;
YAP_UInt prologPredLastLine;
YAP_Atom prologPredName;
YAP_UInt prologPredArity;
YAP_Term prologPredModule;
YAP_Atom prologPredFile;
struct DB_TERM *errorGoal;
intptr_t prologPredCl;
uintptr_t prologPredLine;
uintptr_t prologPredFirstLine;
uintptr_t prologPredLastLine;
const char * prologPredName;
uintptr_t prologPredArity;
const char * prologPredModule;
const char * prologPredFile;
void *errorGoal;
struct error_prolog_source *errorParent;
} error_prolog_source_t;
/// all we need to know about an error/throw
typedef struct yap_error_descriptor {
typedef struct error_descriptor {
enum yap_error_status status;
yap_error_class_number errorClass;
YAP_Atom errorAsText;
YAP_Atom classAsText;
const char * errorAsText;
const char * classAsText;
yap_error_number errorNo;
YAP_Int errorLine;
intptr_t errorLine;
const char *errorFunction;
const char *errorFile;
// struct error_prolog_source *errorSource;
YAP_Int prologPredCl;
YAP_UInt prologPredLine;
YAP_UInt prologPredFirstLine;
YAP_UInt prologPredLastLine;
YAP_Atom prologPredName;
YAP_UInt prologPredArity;
YAP_Term prologPredModule;
YAP_Atom prologPredFile;
YAP_UInt prologParserLine;
YAP_UInt prologParserFirstLine;
YAP_UInt prologParserLastLine;
YAP_Atom prologParserName;
YAP_Atom prologParserFile;
YAP_Bool prologConsulting;
struct DB_TERM *errorTerm;
YAP_Term rawErrorTerm, rawExtraErrorTerm;
intptr_t prologPredCl;
uintptr_t prologPredLine;
uintptr_t prologPredFirstLine;
uintptr_t prologPredLastLine;
const char * prologPredName;
uintptr_t prologPredArity;
const char * prologPredModule;
const char * prologPredFile;
uintptr_t prologParserLine;
uintptr_t prologParserFirstLine;
uintptr_t prologParserLastLine;
const char * prologParserName;
const char * prologParserFile;
bool prologConsulting;
void *errorTerm;
uintptr_t rawErrorTerm, rawExtraErrorTerm;
char *errorMsg;
size_t errorMsgLen;
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,
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(),
int
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_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) */
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
/// 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);

View File

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

View File

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

View File

@ -35,81 +35,70 @@ static char SccsId[] = "%W% %G%";
#if __ANDROID__
#include <android/asset_manager.h>
#include <android/native_activity.h>
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);
return true;
}
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;
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'))
mode = AASSET_MODE_BUFFER;
else {
mode = AASSET_MODE_UNKNOWN;
}
fname += strlen(me->prefix) + 1;
AAsset *a = AAssetManager_open(Yap_assetManager, fname, mode);
if (!a)
return NULL;
// try not to use it as an asset
off64_t sz = AAsset_getLength64(a), sz0 = 0;
int fd;
StreamDesc *st = GLOBAL_Stream + sno;
if ((fd = AAsset_openFileDescriptor64(a, &sz0, &sz)) >= 0) {
// 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))) {
if ((buf = AAsset_getBuffer(a))) {
// copy to memory
bool rc = Yap_set_stream_to_buf(st, buf, sz);
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;
}
} 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
GLOBAL_Stream[sno].vfs_handle = a;
st->vfs = me;
st->status = Input_Stream_f;
return a;
}
return NULL;
}
static bool
close_asset(int sno)
{
close_asset(int sno) {
AAsset_close(GLOBAL_Stream[sno].vfs_handle);
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);
}
static int getc_asset(int sno)
{
static int getc_asset(int sno) {
int ch;
if (AAsset_read(GLOBAL_Stream[sno].vfs_handle, &ch, 1))
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);
}
static const char *readdir_a(void *dirHandle)
{
static const char *readdir_a(void *dirHandle) {
return AAssetDir_getNextFileName((AAssetDir *) dirHandle);
}
static bool closedir_a(void *dirHandle)
{
static bool closedir_a(void *dirHandle) {
AAssetDir_close((AAssetDir *) dirHandle);
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;
fname += strlen(me->prefix) + 1;
if (stat("/assets", &bf)) {
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_mtimespec, (const void *) &out->st_mtimespec, 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);
// try not to use it as an asset
@ -156,45 +144,49 @@ static bool stat_a(VFS_t *me, const char *fname, vfs_stat *out)
}
static
bool is_dir_a( VFS_t *me,const char *dirName)
{
bool is_dir_a(VFS_t *me, const char *dirName) {
bool rc;
dirName += strlen(me->prefix) + 1;
// try not to use it as an asset
AAssetDir *d = AAssetManager_openDir(Yap_assetManager, dirName);
if (d == NULL)
return false;
rc = (AAssetDir_getNextFileName(d) != NULL);
__android_log_print(ANDROID_LOG_INFO, "YAPDroid", "isdir %s <%p>", dirName, d);
AAssetDir_close(d);
return rc;
}
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
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)
return NULL;
return false;
AAsset_close(d);
return me;
return true;
}
extern char virtual_cwd[YAP_FILENAME_MAX + 1];
static bool set_cwd(VFS_t *me, const char *dirName) {
chdir("/assets");
if (me->virtual_cwd)
free((void*)(me->virtual_cwd));
me->virtual_cwd = malloc( sizeof(dirName) + 1 );
return me!= NULL;
strcpy(virtual_cwd, dirName);
__android_log_print(ANDROID_LOG_INFO, "YAPDroid",
"chdir %s", virtual_cwd);
Yap_do_low_level_trace=1;
return true;
}
#endif
VFS_t *
Yap_InitAssetManager( void )
{
Yap_InitAssetManager(void) {
#if __ANDROID__
VFS_t *me;
@ -202,7 +194,8 @@ Yap_InitAssetManager( void )
/* init standard VFS */
me = (VFS_t *) Yap_AllocCodeSpace(sizeof(struct vfs));
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";
/** operations */
me->open = open_asset__; /// open an object in this space

View File

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

View File

@ -460,7 +460,7 @@ static Int exists_directory(USES_REGS1) {
#if HAVE_STAT
struct SYSTEM_STAT ss;
file_name = RepAtom(AtomOfTerm(tname))->StrOfAE;
file_name = Yap_VF(RepAtom(AtomOfTerm(tname))->StrOfAE);
if (SYSTEM_STAT(file_name, &ss) != 0) {
/* ignore errors while checking a file */
return false;
@ -483,7 +483,7 @@ static Int is_absolute_file_name(USES_REGS1) { /* file_base_name(Stream,N) */
int l = push_text_stack();
const char *buf = Yap_TextTermToText(t PASS_REGS);
if (buf) {
rc = Yap_IsAbsolutePath(buf);
rc = Yap_IsAbsolutePath(buf, true);
} else {
at = AtomOfTerm(t);
#if _WIN32

View File

@ -114,16 +114,18 @@ bool fill_pads(int sno, int sno0, int total, format_info *fg USES_REGS)
return true;
}
bool Yap_set_stream_to_buf(StreamDesc *st, const char *buf, encoding_t enc,
size_t nchars) {
bool Yap_set_stream_to_buf(StreamDesc *st, const char *buf,
size_t nchars USES_REGS) {
FILE *f;
// like any file stream.
st->file = f = fmemopen((void *)buf, nchars, "r");
st->status = Input_Stream_f | InMemory_Stream_f | Seekable_Stream_f;
st->file = f = fmemopen((char *)buf, nchars, "r");
st->status = Input_Stream_f | Seekable_Stream_f | InMemory_Stream_f;
st->vfs = NULL;
st->encoding = enc;
st->encoding = LOCAL_encoding;
Yap_DefaultStreamOps(st);
st->linecount = 0;
st->linepos = st->charcount = 0;
return true;
}
@ -198,9 +200,7 @@ int Yap_open_buf_write_stream(encoding_t enc, memBufSource src) {
// setbuf(st->file, NULL);
st->status |= Seekable_Stream_f;
#else
st->file = fmemopen((void *)buf, nchars, "w");
st->nsize = nchars;
st->nbuf = buf;
st->file = fmemopen((void *)st->nbuf, st->nsize, "w");
if (!st->nbuf) {
return -1;
}
@ -292,17 +292,15 @@ void Yap_MemOps(StreamDesc *st) {
st->stream_getc = PlGetc;
}
static int sssno;
bool Yap_CloseMemoryStream(int sno) {
sssno++;
// if (sssno > 1720) Yap_do_low_level_trace=1;
if ((GLOBAL_Stream[sno].status & Output_Stream_f)) {
if ((GLOBAL_Stream[sno].status & Output_Stream_f) && GLOBAL_Stream[sno].file) {
fflush(GLOBAL_Stream[sno].file);
fclose(GLOBAL_Stream[sno].file);
if (GLOBAL_Stream[sno].status & FreeOnClose_Stream_f)
free(GLOBAL_Stream[sno].nbuf);
} else {
if (GLOBAL_Stream[sno].file)
fclose(GLOBAL_Stream[sno].file);
if (GLOBAL_Stream[sno].status & FreeOnClose_Stream_f)
free(GLOBAL_Stream[sno].nbuf);

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

View File

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

View File

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

View File

@ -959,7 +959,8 @@ static void CloseStream(int sno) {
CACHE_REGS
fflush(NULL);
if (!(GLOBAL_Stream[sno].status &
if (GLOBAL_Stream[sno].file &&
!(GLOBAL_Stream[sno].status &
(Null_Stream_f | Socket_Stream_f | InMemory_Stream_f | Pipe_Stream_f)))
fclose(GLOBAL_Stream[sno].file);
#if HAVE_SOCKET

View File

@ -34,13 +34,6 @@ static void FileError(yap_error_number type, Term where, const char *format,
}
}
/// Allocate a temporary buffer
static char *getFileNameBuffer(void) {
return Yap_AllocAtomSpace(YAP_FILENAME_MAX);
}
static void freeFileNameBuffer(char *s) { Yap_FreeCodeSpace(s); }
static Int p_sh(USES_REGS1);
static Int p_shell(USES_REGS1);
@ -52,7 +45,6 @@ static Int p_putenv(USES_REGS1);
static Term do_glob(const char *spec, bool ok_to);
#ifdef MACYAP
static int chdir(char *);
/* #define signal skel_signal */
#endif /* MACYAP */
static const char *expandVars(const char *spec, char *u);
@ -103,8 +95,9 @@ static bool is_directory(const char *FileName) {
bool Yap_Exists(const char *f) {
VFS_t *vfs;
f = Yap_VFAlloc(f);
if ((vfs = vfs_owner(f))) {
return vfs->exists(vfs,f) != NULL;
return vfs->exists(vfs,f);
}
#if _WIN32
if (_access(f, 0) == 0)
@ -114,8 +107,9 @@ bool Yap_Exists(const char *f) {
}
return false;
#elif HAVE_ACCESS
if (access(f, F_OK) == 0)
if (access(f, F_OK) == 0) {
return true;
}
if (errno == EINVAL) {
Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil, "bad flags to access");
}
@ -147,10 +141,13 @@ int Yap_dir_separator(int ch) { return dir_separator(ch); }
char *libdir = NULL;
#endif
bool Yap_IsAbsolutePath(const char *p0) {
bool Yap_IsAbsolutePath(const char *p0, bool expand) {
// verify first if expansion is needed: ~/ or $HOME/
const char *p = expandVars(p0, LOCAL_FileNameBuf);
const char *p = p0;
bool nrc;
if (expand) {
p = expandVars(p0, LOCAL_FileNameBuf);
}
#if _WIN32 || __MINGW32__
nrc = !PathIsRelative(p);
#else
@ -266,7 +263,7 @@ static const char *PlExpandVars(const char *source, const char *root,
pop_text_stack(lvl);
return NULL;
}
if (root && !Yap_IsAbsolutePath(source)) {
if (root && !Yap_IsAbsolutePath(source, false)) {
strncpy(result, root, YAP_FILENAME_MAX);
if (root[strlen(root) - 1] != '/')
strncat(result, "/", YAP_FILENAME_MAX);
@ -346,7 +343,9 @@ static char *PrologPath(const char *Y, char *X) { return (char *)Y; }
#define HAVE_REALPATH 1
#endif
bool ChDir(const char *path) {
char virtual_cwd[YAP_FILENAME_MAX + 1];
bool Yap_ChDir(const char *path) {
bool rc = false;
char qp[FILENAME_MAX + 1];
const char *qpath = Yap_AbsoluteFile(path, qp, true);
@ -370,14 +369,15 @@ static char *PrologPath(const char *Y, char *X) { return (char *)Y; }
return rc;
}
static const char *myrealpath(const char *path, char *out) {
if (!out)
out = LOCAL_FileNameBuf;
int lvl = push_text_stack();
#if _WIN32
DWORD retval = 0;
// notice that the file does not need to exist
retval = GetFullPathName(path, YAP_FILENAME_MAX, out, NULL);
pop_text_stack(lvl);
if (retval == 0) {
Yap_WinError("Generating a full path name for a file");
return NULL;
@ -388,12 +388,13 @@ static const char *myrealpath(const char *path, char *out) {
char *rc = realpath(path, NULL);
if (rc) {
pop_text_stack(lvl);
return rc;
}
// rc = NULL;
if (errno == ENOENT || errno == EACCES) {
char base[YAP_FILENAME_MAX + 1];
strncpy(base, path, YAP_FILENAME_MAX - 1);
char *base= Malloc(YAP_FILENAME_MAX + 1);
strncpy(base, path, YAP_FILENAME_MAX );
rc = realpath(dirname(base), out);
if (rc) {
@ -417,6 +418,7 @@ static const char *myrealpath(const char *path, char *out) {
}
#endif
strcat(rc, b);
rc = pop_output_text_stack(lvl, rc);
return rc;
}
}
@ -424,6 +426,7 @@ static const char *myrealpath(const char *path, char *out) {
#endif
out = malloc(strlen(path) + 1);
strcpy(out, path);
pop_text_stack(lvl);
return out;
}
@ -929,7 +932,7 @@ static Int make_directory(USES_REGS1) {
#if defined(__MINGW32__) || _MSC_VER
if (_mkdir(fd) == -1) {
#else
if (mkdir(fd, 0777) == -1) {
if (mkdir(Yap_VFAlloc(fd), 0777) == -1) {
#endif
/* return an error number */
return false; // errno?
@ -938,7 +941,7 @@ static Int make_directory(USES_REGS1) {
}
static Int p_rmdir(USES_REGS1) {
const char *fd = AtomName(AtomOfTerm(ARG1));
const char *fd = Yap_VFAlloc(AtomName(AtomOfTerm(ARG1)));
#if defined(__MINGW32__) || _MSC_VER
if (_rmdir(fd) == -1) {
#else
@ -1136,7 +1139,14 @@ static int volume_header(char *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(char *cwd, size_t cwdlen) {
if (virtual_cwd[0]) {
if (!cwd) {
cwd = malloc(cwdlen+1);
}
strcpy( cwd, virtual_cwd);
return cwd;
}
#if _WIN32 || defined(__MINGW32__)
if (GetCurrentDirectory(cwdlen, (char *)cwd) == 0) {
Yap_WinError("GetCurrentDirectory failed");
@ -1166,7 +1176,7 @@ static Int working_directory(USES_REGS1) {
}
if (t2 == TermEmptyAtom || t2 == TermDot)
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/
@ -1188,11 +1198,21 @@ const char *Yap_findFile(const char *isource, const char *idef,
YAP_file_type_t ftype, bool expand_root, bool in_lib) {
char *save_buffer = NULL;
const char *root = iroot, *source = isource;
char *root, *source;
int rc = FAIL_RESTORE;
int try
= 0;
int try = 0;
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__,
// "try=%d %s %s", try, isource, iroot) ; }
while (rc == FAIL_RESTORE) {
@ -1201,21 +1221,28 @@ const char *Yap_findFile(const char *isource, const char *idef,
// { CACHE_REGS
switch (try ++) {
case 0: // path or file name is given;
root = iroot;
if (idef || isource) {
source = (isource ? isource : idef);
if (!source[0] && idef && idef[0]) {
strcpy(source, idef);
}
if (source) {
abspath = Yap_IsAbsolutePath(source);
if (source[0]) {
abspath = Yap_IsAbsolutePath(source, expand_root);
}
if (!abspath && !root && ftype == YAP_BOOT_PL) {
root = YAP_PL_SRCDIR;
if (!abspath && !root[0] && ftype == YAP_BOOT_PL) {
strcpy(root, YAP_PL_SRCDIR);
}
break;
case 1: // library directory is given in command line
if (in_lib && ftype == YAP_SAVED_STATE) {
root = iroot;
source = (isource ? isource : idef);
if (iroot && iroot[0])
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 {
done = true;
}
@ -1223,35 +1250,44 @@ const char *Yap_findFile(const char *isource, const char *idef,
case 2: // use environment variable YAPLIBDIR
#if HAVE_GETENV
if (in_lib) {
const char *eroot;
if (ftype == YAP_SAVED_STATE || ftype == YAP_OBJ) {
root = getenv("YAPLIBDIR");
eroot = getenv("YAPLIBDIR");
} else if (ftype == YAP_BOOT_PL) {
root = getenv("YAPSHAREDIR"
"/pl");
if (root == NULL) {
eroot = getenv("YAPSHAREDIR");
if (eroot == NULL) {
continue;
} else {
save_buffer = getFileNameBuffer();
strncpy(save_buffer, root, YAP_FILENAME_MAX);
strncat(save_buffer, "/pl", YAP_FILENAME_MAX);
strncpy(root, eroot, YAP_FILENAME_MAX);
strncat(root, "/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
#endif
done = true;
break;
case 3: // use compilation variable YAPLIBDIR
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) {
root = YAP_SHAREDIR;
strcpy(root,YAP_SHAREDIR);
} else if (ftype == YAP_BOOT_PL) {
root = malloc(YAP_FILENAME_MAX+1);
strcpy(root, YAP_SHAREDIR);
strcat(root,"/pl");
} else {
root = YAP_LIBDIR;
strcpy(root,YAP_LIBDIR);
}
} else
done = true;
@ -1260,9 +1296,9 @@ const char *Yap_findFile(const char *isource, const char *idef,
case 4: // WIN stuff: registry
#if __WINDOWS
if (in_lib) {
source = (ftype == YAP_PL || ftype == YAP_QLY ? "library" : "startup");
source = Yap_RegistryGetString(source);
root = NULL;
const char *key = (ftype == YAP_PL || ftype == YAP_QLY ? "library" : "startup");
strcpy( source, Yap_RegistryGetString(source) );
root[0] = 0;
} else
#endif
done = true;
@ -1280,42 +1316,48 @@ const char *Yap_findFile(const char *isource, const char *idef,
if (pt) {
if (ftype == YAP_BOOT_PL) {
#if __ANDROID__
root = "../../../files/Yap/pl";
strcpy(root, "../../../files/Yap/pl");
#else
root = "../../share/Yap/pl";
#endif
} else {
root = (ftype == YAP_SAVED_STATE || ftype == YAP_OBJ
strcpy(root, (ftype == YAP_SAVED_STATE || ftype == YAP_OBJ
? "../../lib/Yap"
: "../../share/Yap");
: "../../share/Yap"));
}
if (root == iroot) {
if (strcmp(root, iroot) == 0) {
done = true;
continue;
}
if (!save_buffer)
save_buffer = getFileNameBuffer();
if (!save_buffer) {
save_buffer = Malloc(YAP_FILENAME_MAX + 1);
save_buffer[0] = 0;
}
if (Yap_findFile(source, NULL, root, save_buffer, access, ftype,
expand_root, in_lib))
root = save_buffer;
strcpy(root, save_buffer);
else
done = true;
} else {
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;
case 6: // default, try current directory
if (!isource && ftype == YAP_SAVED_STATE)
source = idef;
root = NULL;
strcpy(source, idef);
root[0] = 0;
break;
default:
if (save_buffer)
freeFileNameBuffer(save_buffer);
return false;
pop_text_stack(lvl);
return NULL;
}
if (done)
@ -1324,16 +1366,19 @@ const char *Yap_findFile(const char *isource, const char *idef,
// "root= %s %s ", root, source) ; }
const char *work = PlExpandVars(source, root, result);
if (save_buffer)
freeFileNameBuffer(save_buffer);
// expand names in case you have
// to add a prefix
if (!access || Yap_Exists(work)) {
work = pop_output_text_stack(lvl,work);
return work; // done
} else if (abspath)
} else if (abspath) {
pop_text_stack(lvl);
return NULL;
}
}
pop_text_stack(lvl);
return NULL;
}
@ -1422,7 +1467,7 @@ static Int p_sh(USES_REGS1) { /* sh */
shell = (char *)getenv("SHELL");
if (shell == NULL)
shell = "/bin/sh";
if (system(shell) < 0) {
if (system(Yap_VFAlloc(shell)) < 0) {
#if HAVE_STRERROR
Yap_Error(SYSTEM_ERROR_OPERATING_SYSTEM, TermNil, "%s in sh/0",
strerror(errno));
@ -1628,10 +1673,12 @@ static Int p_mv(USES_REGS1) { /* rename(+OldName,+NewName) */
} else if (!IsAtomTerm(t2)) {
Yap_Error(TYPE_ERROR_ATOM, t2, "second argument to rename/2 not atom");
} else {
oldname = (RepAtom(AtomOfTerm(t1)))->StrOfAE;
newname = (RepAtom(AtomOfTerm(t2)))->StrOfAE;
oldname = Yap_VFAlloc((RepAtom(AtomOfTerm(t1)))->StrOfAE);
newname = Yap_VFAlloc((RepAtom(AtomOfTerm(t2)))->StrOfAE);
if ((r = link(oldname, newname)) == 0 && (r = unlink(oldname)) != 0)
unlink(newname);
free(oldname);
free(newname);
if (r != 0) {
#if HAVE_STRERROR
Yap_Error(SYSTEM_ERROR_OPERATING_SYSTEM, t2, "%s in rename(%s,%s)",

View File

@ -678,7 +678,7 @@ static Int term_to_string(USES_REGS1) {
Term t2 = Deref(ARG2), rc = false, t1 = Deref(ARG1);
const char *s;
if (IsVarTerm(t2)) {
s = Yap_TermToString(ARG1,LOCAL_encoding,
s = Yap_TermToBuffer(ARG1, LOCAL_encoding,
Quote_illegal_f | Handle_vars_f);
if (!s || !MkStringTerm(s)) {
Yap_Error(RESOURCE_ERROR_HEAP, t1,
@ -699,7 +699,7 @@ static Int term_to_atom(USES_REGS1) {
Term t2 = Deref(ARG2), ctl, rc = false;
Atom at;
if (IsVarTerm(t2)) {
const char *s = Yap_TermToString(Deref(ARG1), LOCAL_encoding,
const char *s = Yap_TermToBuffer(Deref(ARG1), LOCAL_encoding,
Quote_illegal_f | Handle_vars_f);
if (!s || !(at = Yap_UTF8ToAtom((const unsigned char *)s))) {
Yap_Error(RESOURCE_ERROR_HEAP, t2,

View File

@ -88,7 +88,7 @@ extern int Yap_GetCharForSIGINT(void);
extern Int Yap_StreamToFileNo(Term);
extern int Yap_OpenStream(const char*, const char*);
extern int Yap_FileStream(FILE*, char *, Term, int);
extern char *Yap_TermToString(Term t, encoding_t encoding, int flags);
extern char *Yap_TermToBuffer(Term t, encoding_t encoding, int flags);
extern char *Yap_HandleToString(yhandle_t l, size_t sz, size_t *length,
encoding_t *encoding, int flags);
extern int Yap_GetFreeStreamD(void);
@ -103,7 +103,7 @@ extern int Yap_growheap_in_parser(tr_fr_ptr *, TokEntry **, VarEntry **);
extern int Yap_growstack_in_parser(tr_fr_ptr *, TokEntry **, VarEntry **);
extern int Yap_growtrail_in_parser(tr_fr_ptr *, TokEntry **, VarEntry **);
extern bool Yap_IsAbsolutePath(const char *p);
extern bool Yap_IsAbsolutePath(const char *p, bool);
extern Atom Yap_TemporaryFile(const char *prefix, int *fd);
extern const char *Yap_AbsoluteFile(const char *spec, char *obuf, bool expand);
@ -156,12 +156,46 @@ INLINE_ONLY inline EXTERN Term MkCharTerm(Int c) {
return MkAtomTerm(Yap_ULookupAtom(cs));
}
INLINE_ONLY inline EXTERN char *Yap_VF(const char *path){
char out[YAP_FILENAME_MAX+1], *p = (char *)path;
extern char virtual_cwd[];
if ( virtual_cwd[0] == 0 || Yap_IsAbsolutePath(path, false)) {
return p;
}
strcpy(out, virtual_cwd);
strcat(out, "/" );
strcat(out, p);
strcpy(p, out);
return p;
}
INLINE_ONLY inline EXTERN char *Yap_VFAlloc(const char *path){
char *out;
extern char virtual_cwd[];
out = (char *)malloc(YAP_FILENAME_MAX+1);
if ( virtual_cwd[0] == 0 || !Yap_IsAbsolutePath(path, false)) {
return (char *)path;
}
strcpy(out, virtual_cwd);
strcat(out, "/" );
strcat(out, path);
return out;
}
/// UT when yap started
extern uint64_t Yap_StartOfWTimes;
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

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);
return strerror_r(errnum, buf, buflen);
#else
int rc = strerror_r(errnum, buf, buflen);
if (rc != 0) {
char *rc = strerror_r(errnum, buf, buflen);
if (rc != NULL) {
// (POSIX only guarantees a value other than 0. The safest
// way to implement this function is to use C++ and overload on the
// type of strerror_r to accurately distinguish GNU from POSIX.)

View File

@ -5,6 +5,7 @@
#include "streamer.h"
static AndroidStreamer * streamerInstance = 0;
void setStreamer(AndroidStreamer* streamer) {
@ -21,24 +22,29 @@ extern "C" {
#include <Yap.h>
#include <YapStreams.h>
#include <VFS.h>
#include <yapio.h>
#include <iopreds.h>
VFS_t andstream;
extern void Java_pt_up_yap_lib_streamerJNI_swig_1module_1init__(void);
static VFS_t andstream;
void Java_pt_up_yap_lib_streamerJNI_swig_1module_1init__(void) {
streamerInstance = 0;
} ;
static std::string buff0;
int n;
static void *
and_open(const char *name, const char *io_mode) {
if (!strcmp(name,"//android/user_output") ||
!strcmp(name,"//android/user_error"))
and_open(struct vfs *me, int sno, const char *name, const char *io_mode) {
// we assume object is already open, so there is no need to open it.
GLOBAL_Stream[sno].vfs_handle = streamerInstance;
GLOBAL_Stream[sno].vfs = me;
GLOBAL_Stream[sno].status = Append_Stream_f | Output_Stream_f;
buff0.clear();
return streamerInstance;
else
return NULL;
}
}
static bool
and_close(int sno) {
return true;
@ -46,10 +52,13 @@ and_close(int sno) {
static int
and_put(int sno, int ch) {
buff0[n++] = ch;
if (ch=='\n' || n == 128) { buff0[n] = '\0'; streamerInstance->display(buff0); n = 0; }
buff0 += ch;
if (ch=='\n' || buff0.length() == 128) { //buff0+= '\0';
streamerInstance->display(buff0);
}
return ch;
}
static int
and_get(int sno) {
return EOF;
@ -62,9 +71,10 @@ static int64_t and_seek(int sno, int64_t where, int how) {
static void
and_flush(int sno) {
buff0[n] = '\0';
streamerInstance->display(buff0); n = 0;
}
buff0 += '\0';
streamerInstance->display(buff0);
//
// Created by vsc on 11-07-2017.
@ -73,13 +83,11 @@ streamerInstance->display(buff0); n = 0;
}
void
AndroidStreamer::bind()
{
AndroidStreamer::bind() {
buff0 = *new std::string[256];
n = 0;
andstream.name = "//android/user_error";
andstream.name = "/android/user_error";
andstream.vflags = VFS_CAN_WRITE | VFS_HAS_PREFIX;
andstream.prefix = "//android/";
andstream.prefix = "/android";
andstream.suffix = NULL;
andstream.open = and_open;
andstream.close = and_close;
@ -89,8 +97,6 @@ AndroidStreamer::bind()
andstream.seek = and_seek;
andstream.next = GLOBAL_VFS;
GLOBAL_VFS = &andstream;
// NULL;
Yap_InitStdStream(StdOutStream, Output_Stream_f, NULL, &andstream);
Yap_InitStdStream(StdErrStream, Output_Stream_f, NULL, &andstream);
Yap_InitStdStream(StdOutStream, Output_Stream_f | Append_Stream_f, NULL, &andstream);
Yap_InitStdStream(StdErrStream, Output_Stream_f | Append_Stream_f, NULL, &andstream);
}

View File

@ -2,6 +2,7 @@
%module(directors="1") streamer
%{
#include "streamer.h"
%}
%include "std_string.i"
@ -10,7 +11,3 @@
%feature("director") AndroidStreamer;
%include "streamer.h"
%init %{
%}