This commit is contained in:
Vitor Santos Costa 2018-02-14 10:37:07 +00:00
parent 311e1d697d
commit 76d0d54a34
4 changed files with 146 additions and 141 deletions

View File

@ -20,8 +20,8 @@
#include "YapHeap.h"
#include "YapInterface.h"
#include "YapStreams.h"
#include "iopreds.h"
#include "config.h"
#include "iopreds.h"
#if HAVE_UNISTD_H
@ -168,22 +168,22 @@ static void consult(const char *b_file USES_REGS) {
Functor functor_compile2 = Yap_MkFunctor(Yap_LookupAtom("c_compile"), 1);
/* consult boot.pl */
int lvl = push_text_stack();
int lvl = push_text_stack();
char *full = Malloc(YAP_FILENAME_MAX + 1);
full[0] = '\0';
/* the consult mode does not matter here, really */
if ((osno = Yap_CheckAlias(AtomLoopStream)) < 0)
osno = 0;
if ((osno = Yap_CheckAlias(AtomLoopStream)) < 0)
osno = 0;
c_stream = YAP_InitConsult(YAP_BOOT_MODE, b_file, full, &oactive);
if (c_stream < 0) {
pop_text_stack(lvl);
pop_text_stack(lvl);
fprintf(stderr, "[ FATAL ERROR: could not open stream %s ]\n", b_file);
exit(1);
}
if (!Yap_AddAlias(AtomLoopStream, c_stream)) {
pop_text_stack(lvl);
return;
}
if (!Yap_AddAlias(AtomLoopStream, c_stream)) {
pop_text_stack(lvl);
return;
}
do {
CACHE_REGS
@ -215,8 +215,8 @@ static void consult(const char *b_file USES_REGS) {
}
} while (t != TermEof);
BACKUP_MACHINE_REGS();
YAP_EndConsult(c_stream, &osno, full);
pop_text_stack(lvl);
YAP_EndConsult(c_stream, &osno, full);
pop_text_stack(lvl);
}
/** @brief A simple language for detecting where YAP stuff can be found
@ -247,7 +247,9 @@ typedef struct config {
#if __ANDROID__
const char *gd_root[] = {"@RootDir", "/assets"};
const char *gd_lib[] = {"@LibDir", "[lib]", "(root)/lib/" "x86"};
const char *gd_lib[] = {"@LibDir", "[lib]",
"(root)/lib/"
"x86"};
const char *gd_share[] = {"@ShareDir", "(root)"};
const char *gd_include[] = {"@IncludeDir", "[include]", "(root)/include"};
const char *gd_dll[] = {"@DLLDir", "(lib)"};
@ -256,7 +258,7 @@ const char *gd_commons[] = {"@CommonsDir", "(share)/PrologCommons"};
const char *gd_ss[] = {"(dll)"};
const char *gd_oss[] = {"."};
const char *gd_bootpldir[] = {"@BootPlDir", "@PrologBootFile/..", "(pl)/pl"};
const char *gd_bootpl[] = {"(bootpldir)" };
const char *gd_bootpl[] = {"(bootpldir)"};
#else
const char *gd_root[] = {"@RootDir", "[root]", "(execdir)/.."};
const char *gd_lib[] = {"@LibDir", "[lib]", "(root)/lib"};
@ -294,7 +296,7 @@ char *location(YAP_init_args *iap, const char *inp) {
if (inp == NULL || inp[0] == '\0') {
return NULL;
}
char * out = Malloc(FILENAME_MAX+1);
char *out = Malloc(FILENAME_MAX + 1);
out[0] = '\0';
if (inp[0] == '(') {
if (strstr(inp + 1, "root") == inp + 1 && Yap_ROOTDIR &&
@ -457,22 +459,21 @@ static const char *find_directory(YAP_init_args *iap, const char *paths[],
char *out = Malloc(YAP_FILENAME_MAX + 1);
const char *inp;
if (filename) {
strcpy(out, filename);
if (Yap_IsAbsolutePath(out, true)) {
out = pop_output_text_stack(lvl, out);
return out;
if (Yap_IsAbsolutePath(filename, true)) {
return pop_output_text_stack(lvl, filename);
}
}
int i = 0;
while ((inp = paths[i++]) != NULL) {
char *o = location(iap, inp);
if (filename && o) {
strcat(o, "/");
strcat(o, filename);
if (o =(const char *) Yap_AbsoluteFile(o, false)) {
o = pop_output_text_stack(lvl, o);
return o;
if (filename && o) {
strcat(o, "/");
strcat(o, filename);
if ((o = Yap_AbsoluteFile(o, false))) {
return pop_output_text_stack(lvl, o);
}
} else if (o && Yap_isDirectory(o)) {
return pop_output_text_stack(lvl, o);
}
}
pop_text_stack(lvl);
@ -490,17 +491,21 @@ static void Yap_set_locations(YAP_init_args *iap) {
Yap_DLLDIR = find_directory(iap, template->dll, NULL);
Yap_PLDIR = find_directory(iap, template->pl, NULL);
Yap_BOOTPLDIR = find_directory(iap, template->bootpldir, NULL);
if (iap->PrologBootFile == NULL)
iap->PrologBootFile = "boot.yap";
Yap_BOOTFILE = find_directory(iap, template->bootpldir,iap->PrologBootFile );
if (iap->PrologBootFile == NULL)
iap->PrologBootFile = "boot.yap";
Yap_BOOTFILE = find_directory(iap, template->bootpldir, iap->PrologBootFile);
Yap_COMMONSDIR = find_directory(iap, template->commons, NULL);
if (iap->SavedState == NULL)
iap->SavedState = "startup.yss";
if (iap->SavedState == NULL) {
if (iap->OutputSavedState)
iap->SavedState = iap->OutputSavedState;
else
iap->SavedState = "startup.yss";
}
Yap_STARTUP = find_directory(iap, template->ss, iap->SavedState);
if (iap->OutputSavedState == NULL)
iap->OutputSavedState = "startup.yss";
Yap_OUTPUT_STARTUP = find_directory(iap, template->ss, iap->OutputSavedState);
if (Yap_ROOTDIR)
if (Yap_ROOTDIR)
setAtomicGlobalPrologFlag(HOME_FLAG,
MkAtomTerm(Yap_LookupAtom(Yap_ROOTDIR)));
if (Yap_PLDIR)

View File

@ -165,7 +165,7 @@ static const char *PlExpandVars(const char *source, const char *root) {
CACHE_REGS
int lvl = push_text_stack();
const char *src = source;
char * result = Malloc(YAP_FILENAME_MAX + 1);
char *result = Malloc(YAP_FILENAME_MAX + 1);
if (strlen(source) >= YAP_FILENAME_MAX) {
Yap_Error(SYSTEM_ERROR_OPERATING_SYSTEM, TermNil,
@ -344,13 +344,13 @@ char virtual_cwd[YAP_FILENAME_MAX + 1];
bool Yap_ChDir(const char *path) {
bool rc = false;
int lvl = push_text_stack();
int lvl = push_text_stack();
VFS_t *v;
if ((v = vfs_owner(path))) {
rc = v->chdir(v, path);
pop_text_stack(lvl);
return rc;
rc = v->chdir(v, path);
pop_text_stack(lvl);
return rc;
}
const char *qpath = Yap_AbsoluteFile(path, true);
#if _WIN32
@ -362,94 +362,90 @@ bool Yap_ChDir(const char *path) {
#else
rc = (chdir(qpath) == 0);
#endif
pop_text_stack(lvl);
pop_text_stack(lvl);
return rc;
}
static char * close_path(char *b0,char *o0, char *o ){
static char *close_path(char *b0, char *o0, char *o) {
if (b0[0] == '\0') {
if (b0[0] == '\0') {
return o;
} else if (!strcmp(b0, "..")) {
while (o-- > o0) {
if (dir_separator(*o)) {
} else if (!strcmp(b0, "..")) {
while (o-- > o0) {
if (dir_separator(*o)) {
break;
}
}
}
} else if (strcmp(b0, ".") != 0) {
} else if (strcmp(b0, ".") != 0) {
*o++ = '/';
strcpy(o, b0);
o += strlen(b0);
}
return o;
}
return o;
}
static char * clean_path(const char *path)
{
const char *p, *p0;
int lvl = push_text_stack();
static char *clean_path(const char *path) {
const char *p, *p0;
int lvl = push_text_stack();
__android_log_print(ANDROID_LOG_INFO, "YAPDroid ", " looking at %s", path) ;
char *o0 = Malloc(FILENAME_MAX+1),*o = o0;
int ch;
char *b0 = Malloc(FILENAME_MAX+1), *b = b0;
p = p0 = path;
while((ch = *p++)) {
if (dir_separator(ch)) {
if (b==b0) {
o = o0;
} else {
b[0] = '\0';
o = close_path(b0, o0, o);
b = b0;
}
} else {
*b++ = ch;
}
__android_log_print(ANDROID_LOG_INFO, "YAPDroid ", " looking at %s", path);
char *o0 = Malloc(FILENAME_MAX + 1), *o = o0;
int ch;
char *b0 = Malloc(FILENAME_MAX + 1), *b = b0;
p = p0 = path;
while ((ch = *p++)) {
if (dir_separator(ch)) {
if (b == b0) {
o = o0;
} else {
b[0] = '\0';
o = close_path(b0, o0, o);
b = b0;
}
} else {
*b++ = ch;
}
if (!dir_separator(p[-1])) {
b[0] = '\0';
o = close_path(b0, o0, o);
}
if (o == o0)
*o++ = '/';
*o = '\0';
__android_log_print(ANDROID_LOG_INFO, "YAPDroid ", " %s at %s, %p-%p", p0, o0, o, o0) ;
return pop_output_text_stack(lvl,o0);
}
if (!dir_separator(p[-1])) {
b[0] = '\0';
o = close_path(b0, o0, o);
}
if (o == o0)
*o++ = '/';
*o = '\0';
__android_log_print(ANDROID_LOG_INFO, "YAPDroid ", " %s at %s, %p-%p", p0, o0,
o, o0);
return pop_output_text_stack(lvl, o0);
}
static const char *myrealpath(const char *path USES_REGS) {
int lvl = push_text_stack();
VFS_t *v;
char *out, *o;
if (Yap_IsAbsolutePath(path,true)) {
o = clean_path(path);
if ((v = vfs_owner(o))
) {
return pop_output_text_stack(lvl, o);
char *out, *o;
if (Yap_IsAbsolutePath(path, true)) {
o = clean_path(path);
return pop_output_text_stack(lvl, o);
}
} else {
out = Malloc(FILENAME_MAX+1);
} else {
out = Malloc(FILENAME_MAX + 1);
Yap_getcwd(out, FILENAME_MAX);
strcat(out, "/");
strcat(out, path);
o = clean_path(out);
if ((v = vfs_owner(o))) {
return pop_output_text_stack(lvl, o);
}
}
if ((v = vfs_owner(o))) {
return pop_output_text_stack(lvl, o);
}
}
#if _WIN32
DWORD retval = 0;
// notice that the file does not need to exist
retval = GetFullPathName(path, YAP_FILENAME_MAX, o, NULL);
if (retval == 0) {
pop_text_stack(lvl);
Yap_WinError("Generating a full path name for a file");
pop_text_stack(lvl);
Yap_WinError("Generating a full path name for a file");
return NULL;
}
return pop_output_text_stack(lvl, o);
@ -458,25 +454,30 @@ char *out, *o;
char *rc = realpath(path, o);
if (rc) {
rc = pop_output_text_stack(lvl, rc);
return pop_output_text_stack(lvl, rc);
}
// rc = NULL;
if (errno == ENOENT || errno == EACCES) {
char *base = Malloc(FILENAME_MAX + 1);
strncpy(base, path, FILENAME_MAX);
char *p = base+strlen(base);
while (p>base && !dir_separator(*--p));
if (p == base) p[1] = '\0';
else p[0] = '\0';
char *tmp = Malloc(FILENAME_MAX + 1);
char *p = base + strlen(base);
while (p > base && !dir_separator(*--p))
;
if (p == base)
p[1] = '\0';
else
p[0] = '\0';
char *tmp = Malloc(FILENAME_MAX + 1);
rc = realpath(base, tmp);
if (rc) {
// base may have been destroyed
char *b = base+strlen(base);
while (b>base && !dir_separator(*--b));
if (b[0] && !dir_separator(b[0])) b++;
size_t e = strlen(rc);
char *b = base + strlen(base);
while (b > base && !dir_separator(*--b))
;
if (b[0] && !dir_separator(b[0]))
b++;
size_t e = strlen(rc);
size_t bs = strlen(b);
if (rc != out && rc != base) {
@ -536,7 +537,7 @@ const char *Yap_AbsoluteFile(const char *spec, bool ok) {
const char *rc;
const char *spec1;
const char *spec2;
int lvl = push_text_stack();
int lvl = push_text_stack();
/// spec gothe original spec;
/// rc0 may be an outout buffer
@ -565,7 +566,7 @@ const char *Yap_AbsoluteFile(const char *spec, bool ok) {
spec2 = spec1;
}
rc = myrealpath(spec2 PASS_REGS);
return pop_output_text_stack(lvl,rc);
return pop_output_text_stack(lvl, rc);
}
static Term
@ -754,15 +755,15 @@ static Int real_path(USES_REGS1) {
}
cmd = rc;
#endif
int lvl = push_text_stack();
int lvl = push_text_stack();
rc0 = myrealpath(cmd PASS_REGS);
if (!rc0) {
PlIOError(SYSTEM_ERROR_OPERATING_SYSTEM, ARG1, NULL);
}
bool out = Yap_unify(MkAtomTerm(Yap_LookupAtom(rc0)), ARG2);
pop_output_text_stack(lvl,rc0);
pop_output_text_stack(lvl, rc0);
return out;
return out;
}
#define EXPAND_FILENAME_DEFS() \
@ -1163,8 +1164,8 @@ static Int true_file_name(USES_REGS1) {
Term t = Deref(ARG1);
const char *s;
if (IsVarTerm(t)) {
Yap_Error(INSTANTIATION_ERROR, t, "argument to true_file_name unbound");
if (IsVarTerm(t)) {
Yap_Error(INSTANTIATION_ERROR, t, "argument to true_file_name unbound");
return FALSE;
}
if (IsAtomTerm(t)) {
@ -1172,14 +1173,14 @@ static Int true_file_name(USES_REGS1) {
} else if (IsStringTerm(t)) {
s = StringOfTerm(t);
} else {
Yap_Error(TYPE_ERROR_ATOM, t, "argument to true_file_name");
Yap_Error(TYPE_ERROR_ATOM, t, "argument to true_file_name");
return FALSE;
}
int l = push_text_stack();
int l = push_text_stack();
if (!(s = Yap_AbsoluteFile(s, true)))
return false;
bool rc = Yap_unify(ARG2, MkAtomTerm(Yap_LookupAtom(s)));
pop_text_stack(l);
pop_text_stack(l);
return rc;
}
@ -1207,30 +1208,29 @@ static Int p_expand_file_name(USES_REGS1) {
}
static Int true_file_name3(USES_REGS1) {
Term t = Deref(ARG1), t2 = Deref(ARG2);
Term t = Deref(ARG1), t2 = Deref(ARG2);
if (IsVarTerm(t)) {
Yap_Error(INSTANTIATION_ERROR, t, "argument to true_file_name unbound");
return FALSE;
}
if (IsVarTerm(t)) {
Yap_Error(INSTANTIATION_ERROR, t, "argument to true_file_name unbound");
return FALSE;
}
if (!IsAtomTerm(t)) {
Yap_Error(TYPE_ERROR_ATOM, t, "argument to true_file_name");
return FALSE;
}
if (!IsVarTerm(t2)) {
if (!IsAtomTerm(t)) {
Yap_Error(TYPE_ERROR_ATOM, t, "argument to true_file_name");
return FALSE;
Yap_Error(TYPE_ERROR_ATOM, t2, "argument to true_file_name");
return FALSE;
}
if (!IsVarTerm(t2)) {
if (!IsAtomTerm(t)) {
Yap_Error(TYPE_ERROR_ATOM, t2, "argument to true_file_name");
return FALSE;
}
// root = RepAtom(AtomOfTerm(t2))->StrOfAE;
}
int lvl = push_text_stack();
const char *tmp = Yap_AbsoluteFile(RepAtom(AtomOfTerm(t))->StrOfAE, true);
Atom at = NULL;
bool rc = (tmp != NULL &&
(at = Yap_LookupAtom(tmp)) != NULL);
pop_text_stack(lvl);
return rc && Yap_unify(ARG3, MkAtomTerm(at));
// root = RepAtom(AtomOfTerm(t2))->StrOfAE;
}
int lvl = push_text_stack();
const char *tmp = Yap_AbsoluteFile(RepAtom(AtomOfTerm(t))->StrOfAE, true);
Atom at = NULL;
bool rc = (tmp != NULL && (at = Yap_LookupAtom(tmp)) != NULL);
pop_text_stack(lvl);
return rc && Yap_unify(ARG3, MkAtomTerm(at));
}
/* Executes $SHELL under Prolog */

View File

@ -19,13 +19,13 @@
* @file directives.yap
* @author VITOR SANTOS COSTA <vsc@VITORs-MBP-2.lan>
* @date Thu Oct 19 11:47:38 2017
*
*
* @brief Control File Loading
%
% @defgroup Directives
@ @ingroup consult
*
*
*
*
*/
@ -130,14 +130,14 @@
'$discontiguous'(D,M).
/** @pred initialization
Execute the goals defined by initialization/1. Only the first answer is
considered.
*/
'$exec_directive'(M:A, Status, _M, VL, Pos) :-
'$exec_directives'(A, Status, M, VL, Pos).
'$exec_directives'(A, Status, M, VL, Pos).
'$exec_directive'(initialization(D), _, M, _, _) :-
'$initialization'(M:D).
'$exec_directive'(initialization(D,OPT), _, M, _, _) :-
@ -193,7 +193,7 @@ considered.
'$exec_directive'(use_module(F, Is), _, M, _, _) :-
use_module(M:F, Is).
'$exec_directive'(use_module(Mod,F,Is), _, _, _, _) :-
'$use_module'(Mod,F,Is).
use_module(Mod,F,Is).
'$exec_directive'(block(BlockSpec), _, _, _, _) :-
'$block'(BlockSpec).
'$exec_directive'(wait(BlockSpec), _, _, _, _) :-

View File

@ -82,10 +82,10 @@ Saves an image of the current state of the YAP database in file
trying goal _G_.
**/
qsave_program(File) :-
'$save_program_status'([], qsave_program(File)),
open(File, write, S, [type(binary)]),
'$save_program_status'([], qsave_program(File)),
open(File, write, S, [type(binary)]),
'$qsave_program'(S),
close(S).
close(S).
/** @pred qsave_program(+ _F_, Opts)