This commit is contained in:
Vitor Santos Costa 2017-12-01 10:42:10 +00:00
parent 2a93f1da99
commit 876e42d629
16 changed files with 111 additions and 66 deletions

View File

@ -2144,7 +2144,6 @@ X_API int YAP_InitConsult(int mode, const char *fname, char *full,
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;
@ -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();
@ -2431,9 +2430,8 @@ X_API YAP_file_type_t YAP_Init(YAP_init_args *yap_init) {
if (!LOCAL_TextBuffer)
LOCAL_TextBuffer = Yap_InitTextAllocator();
int lvl = push_text_stack();
yroot = Malloc(YAP_FILENAME_MAX + 1);
boot_file = Malloc(YAP_FILENAME_MAX + 1);
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
@ -2674,7 +2672,6 @@ X_API YAP_file_type_t YAP_Init(YAP_init_args *yap_init) {
start_modules();
YAP_initialized = true;
pop_text_stack(lvl);
return
YAP_BOOT_PL;
}

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

@ -12,7 +12,7 @@ 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);

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

@ -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

@ -178,7 +178,7 @@ static bool set_cwd(VFS_t *me, const char *dirName) {
strcpy(virtual_cwd, dirName);
__android_log_print(ANDROID_LOG_INFO, "YAPDroid",
"chdir %s", virtual_cwd);
Yap_do_low_level_trace = true;
Yap_do_low_level_trace=1;
return true;
}

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

@ -119,7 +119,7 @@ bool Yap_set_stream_to_buf(StreamDesc *st, const char *buf,
FILE *f;
// like any file stream.
st->file = f = fmemopen(buf, nchars, "r");
st->file = f = fmemopen((char *)buf, nchars, "r");
st->status = Input_Stream_f | Seekable_Stream_f | InMemory_Stream_f;
st->vfs = NULL;
st->encoding = LOCAL_encoding;
@ -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

@ -1183,7 +1183,6 @@ do_open(Term file_name, Term t2,
const char *fname;
char fbuf[FILENAME_MAX];
stream_flags_t flags;
FILE *fd;
const char *s_encoding;
encoding_t encoding;
Term tenc;
@ -1515,7 +1514,6 @@ int Yap_OpenStream(const char *fname, const char *io_mode) {
struct vfs *vfsp;
FILE *fd;
int flags;
SMALLUNSGN s;
sno = GetFreeStreamD();
if (sno < 0) {
@ -1526,6 +1524,7 @@ int Yap_OpenStream(const char *fname, const char *io_mode) {
st = GLOBAL_Stream + sno;
// read, write, append
st->file = NULL;
fname = Yap_VF(fname);
if ((vfsp = vfs_owner(fname)) != NULL ) {
if (!vfsp->open(vfsp, sno, fname, io_mode)) {
UNLOCK(st->streamlock);

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

@ -95,6 +95,7 @@ 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);
}
@ -106,8 +107,9 @@ bool Yap_Exists(const char *f) {
}
return false;
#elif HAVE_ACCESS
if (access(f, F_OK) == 0)
return true;
if (access(f, F_OK) == 0) {
return true;
}
if (errno == EINVAL) {
Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil, "bad flags to access");
}
@ -139,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
@ -258,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);
@ -364,14 +369,15 @@ char virtual_cwd[YAP_FILENAME_MAX + 1];
return rc;
}
static const char *myrealpath(const char *path, char *out) {
if (!out)
out = LOCAL_FileNameBuf;
#if _WIN32
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;
@ -382,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) {
@ -411,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;
}
}
@ -418,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;
}
@ -923,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?
@ -932,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
@ -1130,7 +1139,7 @@ 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);
@ -1189,7 +1198,7 @@ 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;
char *root = iroot, *source = isource;
char *root, *source;
int rc = FAIL_RESTORE;
int try = 0;
bool abspath = false;
@ -1216,10 +1225,10 @@ const char *Yap_findFile(const char *isource, const char *idef,
strcpy(source, idef);
}
if (source[0]) {
abspath = Yap_IsAbsolutePath(source);
abspath = Yap_IsAbsolutePath(source, expand_root);
}
if (!abspath && !root[0] && ftype == YAP_BOOT_PL) {
root = YAP_PL_SRCDIR;
strcpy(root, YAP_PL_SRCDIR);
}
break;
case 1: // library directory is given in command line
@ -1246,8 +1255,7 @@ const char *Yap_findFile(const char *isource, const char *idef,
if (ftype == YAP_SAVED_STATE || ftype == YAP_OBJ) {
eroot = getenv("YAPLIBDIR");
} else if (ftype == YAP_BOOT_PL) {
eroot = getenv("YAPSHAREDIR"
"/pl");
eroot = getenv("YAPSHAREDIR");
if (eroot == NULL) {
continue;
} else {
@ -1306,24 +1314,27 @@ const char *Yap_findFile(const char *isource, const char *idef,
const char *pt = Yap_FindExecutable();
if (pt) {
if (ftype == YAP_BOOT_PL) {
if (ftype == YAP_BOOT_PL) {
#if __ANDROID__
strcpy(root, "../../../files/Yap/pl");
strcpy(root, "../../../files/Yap/pl");
#else
root = "../../share/Yap/pl";
root = "../../share/Yap/pl";
#endif
} else {
strcpy(root, (ftype == YAP_SAVED_STATE || ftype == YAP_OBJ
? "../../lib/Yap"
: "../../share/Yap"));
}
if (strcmp(root, iroot)==0) {
done = true;
continue;
}
if (!save_buffer)
save_buffer = Malloc(YAP_FILENAME_MAX+1);
if (Yap_findFile(source, NULL, root, save_buffer, access, ftype,
} else {
strcpy(root, (ftype == YAP_SAVED_STATE || ftype == YAP_OBJ
? "../../lib/Yap"
: "../../share/Yap"));
}
if (strcmp(root, iroot) == 0) {
done = true;
continue;
}
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))
strcpy(root, save_buffer);
else
@ -1361,9 +1372,13 @@ const char *Yap_findFile(const char *isource, const char *idef,
if (!access || Yap_Exists(work)) {
work = pop_output_text_stack(lvl,work);
return work; // done
} else if (abspath)
return NULL;
} else if (abspath) {
pop_text_stack(lvl);
return NULL;
}
}
pop_text_stack(lvl);
return NULL;
}
@ -1452,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));
@ -1658,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,6 +156,37 @@ 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;
@ -165,4 +196,6 @@ extern bool Yap_HandleSIGINT(void);
extern bool Yap_set_stream_to_buf(StreamDesc *st, const char *bufi,
size_t nchars USES_REGS);
#endif