memory mgmt

This commit is contained in:
Vitor Santos Costa 2017-09-23 02:17:55 +01:00
parent 70f4f7adcf
commit a40fbe420f
24 changed files with 4238 additions and 4265 deletions

3
.gitignore vendored
View File

@ -12,6 +12,7 @@
*.dll *.dll
docs/yap.info* docs/yap.info*
.build .build
build
tags tags
TGSautom4te.cache TGSautom4te.cache
cscope.* cscope.*
@ -24,7 +25,6 @@ tmtags*
.Rhistory .Rhistory
.zedstate .zedstate
config.h config.h
Yap.h
YapConfig.h YapConfig.h
YapTermConfig.h YapTermConfig.h
.graffiti .graffiti
@ -42,7 +42,6 @@ groups
.cproject .cproject
.dir-locals.el .dir-locals.el
.DS_store .DS_store
.kateproject
.project .project
.pydevproject .pydevproject
.Rhistory .Rhistory

View File

@ -268,12 +268,11 @@ static Int char_code(USES_REGS1) {
Atom at = AtomOfTerm(t0); Atom at = AtomOfTerm(t0);
Term tf; Term tf;
unsigned char *c = RepAtom(at)->UStrOfAE; unsigned char *c = RepAtom(at)->UStrOfAE;
int32_t v; int32_t v = IntegerOfTerm(ARG1);
c += get_utf8(c, 1, &v);
if (c[0] != '\0') { get_utf8(c, -1, &v);
Yap_Error(TYPE_ERROR_CHARACTER, t0, "char_code/2"); if (!v)
return FALSE; return false;
}
tf = MkIntTerm(v); tf = MkIntTerm(v);
return Yap_unify(ARG2, tf); return Yap_unify(ARG2, tf);
} }

View File

@ -2239,10 +2239,9 @@ X_API int YAP_WriteDynamicBuffer(YAP_Term t, char *buf, size_t sze,
char *b; char *b;
BACKUP_MACHINE_REGS(); BACKUP_MACHINE_REGS();
b = Yap_TermToString(t, lengthp, enc, flags); b = Yap_TermToString(t, enc, flags);
if (*lengthp >= sze)
*lengthp = sze;
strncpy(buf, b, sze); strncpy(buf, b, sze);
buf[sze] = 0;
RECOVER_MACHINE_REGS(); RECOVER_MACHINE_REGS();
return true; return true;
} }

View File

@ -53,6 +53,7 @@ static ModEntry *initMod(AtomEntry *toname, AtomEntry *ae) {
n->NextME = CurrentModules; n->NextME = CurrentModules;
CurrentModules = n; CurrentModules = n;
n->AtomOfME = ae; n->AtomOfME = ae;
n->NextOfPE =NULL;
n->OwnerFile = Yap_ConsultingFile(PASS_REGS1); n->OwnerFile = Yap_ConsultingFile(PASS_REGS1);
AddPropToAtom(ae, (PropEntry *)n); AddPropToAtom(ae, (PropEntry *)n);
Yap_setModuleFlags(n, parent); Yap_setModuleFlags(n, parent);
@ -385,24 +386,19 @@ static Int strip_module(USES_REGS1) {
} }
static Int yap_strip_clause(USES_REGS1) { static Int yap_strip_clause(USES_REGS1) {
Functor f;
Term t1 = Deref(ARG1), tmod = LOCAL_SourceModule; Term t1 = Deref(ARG1), tmod = LOCAL_SourceModule;
if (tmod == PROLOG_MODULE) { if (tmod == PROLOG_MODULE) {
tmod = TermProlog; tmod = TermProlog;
} }
t1 = Yap_StripModule(t1, &tmod); t1 = Yap_StripModule(t1, &tmod);
if (IsVarTerm(t1)) { if (IsVarTerm(t1) || IsVarTerm(tmod)) {
Yap_Error(INSTANTIATION_ERROR, t1, "trying to obtain module"); Yap_Error(INSTANTIATION_ERROR, t1, "trying to obtain module");
return false; return false;
} else if (IsVarTerm(tmod)) { } else if (IsApplTerm(t1)) {
Yap_Error(INSTANTIATION_ERROR, tmod, "trying to obtain module"); Functor f = FunctorOfTerm(t1);
return false; if (IsExtensionFunctor(f)) {
} else if (IsIntTerm(t1) || (IsApplTerm(t1) && IsExtensionFunctor((f = FunctorOfTerm(t1))))) {
Yap_Error(TYPE_ERROR_CALLABLE, t1, "trying to obtain module"); Yap_Error(TYPE_ERROR_CALLABLE, t1, "trying to obtain module");
return false; return false;
} else if (!IsAtomTerm(tmod)) {
Yap_Error(TYPE_ERROR_ATOM, tmod, "trying to obtain module");
return false;
} }
if (f == FunctorAssert || f == FunctorDoubleArrow) { if (f == FunctorAssert || f == FunctorDoubleArrow) {
Term thmod = tmod; Term thmod = tmod;
@ -422,6 +418,11 @@ static Int yap_strip_clause(USES_REGS1) {
return false; return false;
} }
} }
} else if (IsIntTerm(t1) || IsIntTerm(tmod) ) {
Yap_Error(TYPE_ERROR_CALLABLE, t1, "trying to obtain module");
return false;
}
return Yap_unify(ARG3, t1) && Yap_unify(ARG2, tmod); return Yap_unify(ARG3, t1) && Yap_unify(ARG2, tmod);
} }

View File

@ -404,10 +404,10 @@ writing, writing a BOM can be requested using the option
*/ */
#include "Yap.h" #include "Yap.h"
#include "YapEval.h"
#include "YapHeap.h" #include "YapHeap.h"
#include "Yatom.h" #include "Yatom.h"
#include "alloc.h" #include "alloc.h"
#include "YapEval.h"
#include "yapio.h" #include "yapio.h"
/* stuff we want to use in standard YAP code */ /* stuff we want to use in standard YAP code */
#include "YapText.h" #include "YapText.h"
@ -1104,7 +1104,7 @@ Term Yap_scan_num(StreamDesc *inp, bool error_on) {
CACHE_REGS CACHE_REGS
Term out; Term out;
int sign = 1; int sign = 1;
int ch, cherr; int ch, cherr = 0;
char *ptr; char *ptr;
void *old_tr = TR; void *old_tr = TR;
@ -1604,7 +1604,6 @@ TokEntry *Yap_tokenizer(struct stream_desc *inp_stream, bool store_comments,
if (!(t->TokInfo)) { if (!(t->TokInfo)) {
return CodeSpaceError(t, p, l); return CodeSpaceError(t, p, l);
} }
Free(TokImage);
t->Tok = Ord(kind = String_tok); t->Tok = Ord(kind = String_tok);
} else if (quote == '`') { } else if (quote == '`') {
t->TokInfo = Yap_CharsToTBQ((char *)TokImage, CurrentModule, t->TokInfo = Yap_CharsToTBQ((char *)TokImage, CurrentModule,
@ -1612,7 +1611,6 @@ TokEntry *Yap_tokenizer(struct stream_desc *inp_stream, bool store_comments,
if (!(t->TokInfo)) { if (!(t->TokInfo)) {
return CodeSpaceError(t, p, l); return CodeSpaceError(t, p, l);
} }
Free(TokImage);
t->Tok = Ord(kind = String_tok); t->Tok = Ord(kind = String_tok);
} else { } else {
t->TokInfo = MkAtomTerm(Yap_ULookupAtom(TokImage)); t->TokInfo = MkAtomTerm(Yap_ULookupAtom(TokImage));

View File

@ -948,9 +948,10 @@ static Int current_predicate(USES_REGS1) {
static OpEntry *NextOp(Prop pp USES_REGS) { static OpEntry *NextOp(Prop pp USES_REGS) {
while (!EndOfPAEntr(pp) && pp->KindOfPE != OpProperty && while (!EndOfPAEntr(pp) &&
(RepOpProp(pp)->OpModule != PROLOG_MODULE pp->KindOfPE != OpProperty &&
|| RepOpProp(pp)->OpModule != CurrentModule) ) (RepOpProp(pp)->OpModule != PROLOG_MODULE || RepOpProp(pp)->OpModule != CurrentModule)
)
pp = pp->NextOfPE; pp = pp->NextOfPE;
return RepOpProp(pp); return RepOpProp(pp);
} }

350
C/text.c
View File

@ -51,10 +51,7 @@ typedef struct TextBuffer_manager {
int lvl; int lvl;
} text_buffer_t; } text_buffer_t;
int push_text_stack(USES_REGS1) { int push_text_stack(USES_REGS1) { return LOCAL_TextBuffer->lvl++; }
return LOCAL_TextBuffer->lvl++;
}
int pop_text_stack(int i) { int pop_text_stack(int i) {
int lvl = LOCAL_TextBuffer->lvl; int lvl = LOCAL_TextBuffer->lvl;
@ -73,43 +70,9 @@ int pop_text_stack(int i) {
return lvl; return lvl;
} }
void *protected_pop_text_stack(int i, void *protected, bool tmp,
size_t sz USES_REGS) {
void *out = protected;
int lvl = LOCAL_TextBuffer->lvl;
while (lvl > i) {
struct mblock *p = LOCAL_TextBuffer->first[lvl];
while (p) {
struct mblock *np = p->next;
if (p + 1 == protected) {
if (tmp)
out = LOCAL_FileNameBuf;
else
out = p;
memcpy(out, protected, sz);
} else {
free(p);
}
p = np;
}
LOCAL_TextBuffer->first[lvl] = NULL;
LOCAL_TextBuffer->last[lvl] = NULL;
lvl--;
}
LOCAL_TextBuffer->lvl = lvl;
return out;
}
// void pop_text_stack(int i) { LOCAL_TextBuffer->lvl = i; } // void pop_text_stack(int i) { LOCAL_TextBuffer->lvl = i; }
void insert_block(struct mblock *o) {
void *Malloc(size_t sz USES_REGS) { int lvl = o->lvl;
int lvl = LOCAL_TextBuffer->lvl;
if (sz == 0)
sz = 1024;
sz = ALIGN_BY_TYPE(sz + sizeof(struct mblock), CELL);
struct mblock *o = malloc(sz);
if (!o)
return NULL;
o->prev = LOCAL_TextBuffer->last[lvl]; o->prev = LOCAL_TextBuffer->last[lvl];
if (o->prev) { if (o->prev) {
o->prev->next = o; o->prev->next = o;
@ -120,8 +83,36 @@ void *Malloc(size_t sz USES_REGS) {
LOCAL_TextBuffer->first[lvl] = LOCAL_TextBuffer->last[lvl] = o; LOCAL_TextBuffer->first[lvl] = LOCAL_TextBuffer->last[lvl] = o;
} }
o->next = NULL; o->next = NULL;
}
void release_block(struct mblock *o) {
if (o->prev)
o->prev->next = o->next;
if (o->next)
o->next->prev = o->prev;
int lvl = o->lvl;
if (LOCAL_TextBuffer->first[lvl] == o) {
if (LOCAL_TextBuffer->last[lvl] == o) {
LOCAL_TextBuffer->first[lvl] = LOCAL_TextBuffer->last[lvl] = NULL;
}
LOCAL_TextBuffer->first[lvl] = o->next;
} else if (LOCAL_TextBuffer->last[lvl] == o) {
LOCAL_TextBuffer->last[lvl] = o->prev;
}
}
void *Malloc(size_t sz USES_REGS) {
int lvl = LOCAL_TextBuffer->lvl;
if (sz == 0)
sz = 1024;
sz = ALIGN_BY_TYPE(sz + sizeof(struct mblock), CELL);
struct mblock *o = malloc(sz);
if (!o)
return NULL;
o->sz = sz; o->sz = sz;
o->lvl = lvl; o->lvl = lvl;
o->prev = o->next = 0;
insert_block(o);
return o + 1; return o + 1;
} }
@ -141,26 +132,28 @@ void *Realloc(void *pt, size_t sz USES_REGS) {
if (LOCAL_TextBuffer->last[lvl] == old) { if (LOCAL_TextBuffer->last[lvl] == old) {
LOCAL_TextBuffer->last[lvl] = o; LOCAL_TextBuffer->last[lvl] = o;
} }
o->sz = sz;
return o + 1; return o + 1;
} }
void *export_block(int i, void *protected USES_REGS) {
struct mblock *o = ((struct mblock *)protected) - 1;
release_block(o);
if (i >= 0) {
o->lvl = i;
insert_block(o);
return protected;
} else {
memcpy(o, protected, o->sz);
return o;
}
}
void Free(void *pt USES_REGS) { void Free(void *pt USES_REGS) {
struct mblock *o = pt; struct mblock *o = pt;
o--; o--;
if (o->prev) release_block(o);
o->prev->next = o->next;
if (o->next)
o->next->prev = o->prev;
int lvl = o->lvl;
if (LOCAL_TextBuffer->first[lvl] == o) {
if (LOCAL_TextBuffer->last[lvl] == o) {
LOCAL_TextBuffer->first[lvl] = LOCAL_TextBuffer->last[lvl] = NULL;
}
LOCAL_TextBuffer->first[lvl] = o->next;
} else if (LOCAL_TextBuffer->last[lvl] == o) {
LOCAL_TextBuffer->last[lvl] = o->prev;
}
free(o); free(o);
} }
@ -262,7 +255,6 @@ static Int SkipListCodes(unsigned char **bufp, Term *l, Term **tailp,
l = RepPair(*l) + 1; l = RepPair(*l) + 1;
do_derefa(v, l, derefa2_unk, derefa2_nonvar); do_derefa(v, l, derefa2_unk, derefa2_nonvar);
} }
} }
if (IsVarTerm(*l)) { if (IsVarTerm(*l)) {
@ -279,13 +271,12 @@ static Int SkipListCodes(unsigned char **bufp, Term *l, Term **tailp,
return length; return length;
} }
static unsigned char *latin2utf8(seq_tv_t *inp, size_t *lengp) { static unsigned char *latin2utf8(seq_tv_t *inp) {
unsigned char *b0 = inp->val.uc; unsigned char *b0 = inp->val.uc;
size_t sz = *lengp = strlen(inp->val.c); size_t sz = strlen(inp->val.c);
sz *= 2; sz *= 2;
int ch; int ch;
unsigned char *buf = Malloc(sz + 1), *pt = buf; unsigned char *buf = Malloc(sz + 1), *pt = buf;
*lengp = strlen(inp->val.c);
if (!buf) if (!buf)
return NULL; return NULL;
while ((ch = *b0++)) { while ((ch = *b0++)) {
@ -298,9 +289,8 @@ static unsigned char *latin2utf8(seq_tv_t *inp, size_t *lengp) {
return buf; return buf;
} }
static unsigned char *wchar2utf8(seq_tv_t *inp, size_t *lengp) { static unsigned char *wchar2utf8(seq_tv_t *inp) {
*lengp = wcslen(inp->val.w); size_t sz = wcslen(inp->val.w) * 4;
size_t sz = *lengp * 4;
wchar_t *b0 = inp->val.w; wchar_t *b0 = inp->val.w;
unsigned char *buf = Malloc(sz + 1), *pt = buf; unsigned char *buf = Malloc(sz + 1), *pt = buf;
int ch; int ch;
@ -315,43 +305,40 @@ static unsigned char *wchar2utf8(seq_tv_t *inp, size_t *lengp) {
static void *slice(size_t min, size_t max, const unsigned char *buf USES_REGS); static void *slice(size_t min, size_t max, const unsigned char *buf USES_REGS);
static unsigned char *to_buffer(unsigned char *buf, Term t, seq_tv_t *inp, static unsigned char *to_buffer(unsigned char *buf, Term t, seq_tv_t *inp,
bool *widep, Int *atoms, bool *widep, Int *atoms USES_REGS) {
size_t *lenp USES_REGS) {
CELL *r = NULL; CELL *r = NULL;
Int n; Int n;
if (!buf) {
inp->max = *lenp;
}
unsigned char *bufc = buf; unsigned char *bufc = buf;
if (bufc == NULL) {
bufc = malloc(1024);
}
n = SkipListCodes(&bufc, &t, &r, atoms, widep, inp PASS_REGS); n = SkipListCodes(&bufc, &t, &r, atoms, widep, inp PASS_REGS);
if (n < 0) { if (n < 0) {
LOCAL_Error_TYPE = -n; LOCAL_Error_TYPE = -n;
return NULL; return NULL;
} }
*lenp = n;
return bufc; return bufc;
} }
static unsigned char *Yap_ListOfCodesToBuffer(unsigned char *buf, Term t, static unsigned char *Yap_ListOfCodesToBuffer(unsigned char *buf, Term t,
seq_tv_t *inp, bool *widep, seq_tv_t *inp,
size_t *lenp USES_REGS) { bool *widep USES_REGS) {
Int atoms = 1; // we only want lists of atoms Int atoms = 1; // we only want lists of atoms
return to_buffer(buf, t, inp, widep, &atoms, lenp PASS_REGS); return to_buffer(buf, t, inp, widep, &atoms PASS_REGS);
} }
static unsigned char *Yap_ListOfAtomsToBuffer(unsigned char *buf, Term t, static unsigned char *Yap_ListOfAtomsToBuffer(unsigned char *buf, Term t,
seq_tv_t *inp, bool *widep, seq_tv_t *inp,
size_t *lenp USES_REGS) { bool *widep USES_REGS) {
Int atoms = 2; // we only want lists of integer codes Int atoms = 2; // we only want lists of integer codes
return to_buffer(buf, t, inp, widep, &atoms, lenp PASS_REGS); return to_buffer(buf, t, inp, widep, &atoms PASS_REGS);
} }
static unsigned char *Yap_ListToBuffer(unsigned char *buf, Term t, static unsigned char *Yap_ListToBuffer(unsigned char *buf, Term t,
seq_tv_t *inp, bool *widep, seq_tv_t *inp, bool *widep USES_REGS) {
size_t *lenp USES_REGS) {
Int atoms = 0; // we accept both types of lists. Int atoms = 0; // we accept both types of lists.
return to_buffer(buf, t, inp, widep, &atoms, lenp PASS_REGS); return to_buffer(buf, t, inp, widep, &atoms PASS_REGS);
} }
#if USE_GEN_TYPE_ERROR #if USE_GEN_TYPE_ERROR
@ -381,7 +368,7 @@ static yap_error_number gen_type_error(int flags) {
// static int cnt; // static int cnt;
unsigned char *Yap_readText(seq_tv_t *inp, size_t *lengp) { unsigned char *Yap_readText(seq_tv_t *inp USES_REGS) {
unsigned char *s0 = NULL; unsigned char *s0 = NULL;
bool wide; bool wide;
@ -401,8 +388,8 @@ unsigned char *Yap_readText(seq_tv_t *inp, size_t *lengp) {
} else if (!IsPairOrNilTerm(inp->val.t) && !IsStringTerm(inp->val.t) && } else if (!IsPairOrNilTerm(inp->val.t) && !IsStringTerm(inp->val.t) &&
inp->type == (YAP_STRING_ATOMS_CODES | YAP_STRING_STRING)) { inp->type == (YAP_STRING_ATOMS_CODES | YAP_STRING_STRING)) {
LOCAL_Error_TYPE = TYPE_ERROR_LIST; LOCAL_Error_TYPE = TYPE_ERROR_LIST;
} else if (!IsPairOrNilTerm(inp->val.t) && !IsStringTerm(inp->val.t) } else if (!IsPairOrNilTerm(inp->val.t) && !IsStringTerm(inp->val.t) &&
&& !IsAtomTerm(inp->val.t) && !(inp->type & YAP_STRING_DATUM)) { !IsAtomTerm(inp->val.t) && !(inp->type & YAP_STRING_DATUM)) {
LOCAL_Error_TYPE = TYPE_ERROR_TEXT; LOCAL_Error_TYPE = TYPE_ERROR_TEXT;
} }
} }
@ -414,27 +401,25 @@ unsigned char *Yap_readText(seq_tv_t *inp, size_t *lengp) {
// this is a term, extract to a buffer, and representation is wide // this is a term, extract to a buffer, and representation is wide
// Yap_DebugPlWriteln(inp->val.t); // Yap_DebugPlWriteln(inp->val.t);
Atom at = AtomOfTerm(inp->val.t); Atom at = AtomOfTerm(inp->val.t);
size_t sz = strlen(at->StrOfAE);
if (lengp)
*lengp = sz;
if (inp->type & YAP_STRING_WITH_BUFFER) if (inp->type & YAP_STRING_WITH_BUFFER)
return at->UStrOfAE; return at->UStrOfAE;
size_t sz = strlen(at->StrOfAE);
inp->type |= YAP_STRING_IN_TMP; inp->type |= YAP_STRING_IN_TMP;
char *o = Malloc(sz + 1); char *o = Malloc(sz + 1);
strcpy(o, at->StrOfAE); strcpy(o, at->StrOfAE);
o = export_block(0, o);
return (unsigned char *)o; return (unsigned char *)o;
} }
if (IsStringTerm(inp->val.t) && inp->type & YAP_STRING_STRING) { if (IsStringTerm(inp->val.t) && inp->type & YAP_STRING_STRING) {
// this is a term, extract to a buffer, and representation is wide // this is a term, extract to a buffer, and representation is wide
// Yap_DebugPlWriteln(inp->val.t); // Yap_DebugPlWriteln(inp->val.t);
const char *s = StringOfTerm(inp->val.t); const char *s = StringOfTerm(inp->val.t);
size_t sz = strlen(s);
if (lengp)
*lengp = sz;
if (inp->type & YAP_STRING_WITH_BUFFER) if (inp->type & YAP_STRING_WITH_BUFFER)
return (unsigned char *)UStringOfTerm(inp->val.t); return (unsigned char *)UStringOfTerm(inp->val.t);
inp->type |= YAP_STRING_IN_TMP; inp->type |= YAP_STRING_IN_TMP;
size_t sz = strlen(s);
char *o = Malloc(sz + 1); char *o = Malloc(sz + 1);
o = export_block(0, o);
strcpy(o, s); strcpy(o, s);
return (unsigned char *)o; return (unsigned char *)o;
} }
@ -442,30 +427,28 @@ unsigned char *Yap_readText(seq_tv_t *inp, size_t *lengp) {
(YAP_STRING_CODES | YAP_STRING_ATOMS)) && (YAP_STRING_CODES | YAP_STRING_ATOMS)) &&
IsPairOrNilTerm(inp->val.t)) { IsPairOrNilTerm(inp->val.t)) {
// Yap_DebugPlWriteln(inp->val.t); // Yap_DebugPlWriteln(inp->val.t);
return Yap_ListToBuffer(s0, inp->val.t, inp, &wide, lengp PASS_REGS); return Yap_ListToBuffer(s0, inp->val.t, inp, &wide PASS_REGS);
// this is a term, extract to a sfer, and representation is wide // this is a term, extract to a sfer, and representation is wide
} }
if (inp->type & YAP_STRING_CODES && IsPairOrNilTerm(inp->val.t)) { if (inp->type & YAP_STRING_CODES && IsPairOrNilTerm(inp->val.t)) {
// Yap_DebugPlWriteln(inp->val.t); // Yap_DebugPlWriteln(inp->val.t);
return Yap_ListOfCodesToBuffer(s0, inp->val.t, inp, &wide, lengp PASS_REGS); return Yap_ListOfCodesToBuffer(s0, inp->val.t, inp, &wide PASS_REGS);
// this is a term, extract to a sfer, and representation is wide // this is a term, extract to a sfer, and representation is wide
} }
if (inp->type & YAP_STRING_ATOMS && IsPairOrNilTerm(inp->val.t)) { if (inp->type & YAP_STRING_ATOMS && IsPairOrNilTerm(inp->val.t)) {
// Yap_DebugPlWriteln(inp->val.t); // Yap_DebugPlWriteln(inp->val.t);
return Yap_ListOfAtomsToBuffer(s0, inp->val.t, inp, &wide, lengp PASS_REGS); return Yap_ListOfAtomsToBuffer(s0, inp->val.t, inp, &wide PASS_REGS);
// this is a term, extract to a buffer, and representation is wide // this is a term, extract to a buffer, and representation is wide
} }
if (inp->type & YAP_STRING_INT && IsIntegerTerm(inp->val.t)) { if (inp->type & YAP_STRING_INT && IsIntegerTerm(inp->val.t)) {
// ASCII, so both LATIN1 and UTF-8 // ASCII, so both LATIN1 and UTF-8
// Yap_DebugPlWriteln(inp->val.t); // Yap_DebugPlWriteln(inp->val.t);
char *s; char *s;
s = Malloc(0); s = Malloc(2 * MaxTmp(PASS_REGS1));
if (snprintf(s, MaxTmp(PASS_REGS1) - 1, Int_FORMAT, if (snprintf(s, MaxTmp(PASS_REGS1) - 1, Int_FORMAT,
IntegerOfTerm(inp->val.t)) < 0) { IntegerOfTerm(inp->val.t)) < 0) {
AUX_ERROR(inp->val.t, 2 * MaxTmp(PASS_REGS1), s, char); AUX_ERROR(inp->val.t, 2 * MaxTmp(PASS_REGS1), s, char);
} }
if (lengp)
*lengp = strlen(s);
return (unsigned char *)s; return (unsigned char *)s;
} }
if (inp->type & YAP_STRING_FLOAT && IsFloatTerm(inp->val.t)) { if (inp->type & YAP_STRING_FLOAT && IsFloatTerm(inp->val.t)) {
@ -474,51 +457,42 @@ unsigned char *Yap_readText(seq_tv_t *inp, size_t *lengp) {
if (!Yap_FormatFloat(FloatOfTerm(inp->val.t), &s, 1024)) { if (!Yap_FormatFloat(FloatOfTerm(inp->val.t), &s, 1024)) {
return NULL; return NULL;
} }
if (lengp)
*lengp = strlen(s);
return (unsigned char *)s; return (unsigned char *)s;
} }
#if USE_GMP #if USE_GMP
if (inp->type & YAP_STRING_BIG && IsBigIntTerm(inp->val.t)) { if (inp->type & YAP_STRING_BIG && IsBigIntTerm(inp->val.t)) {
// Yap_DebugPlWriteln(inp->val.t); // Yap_DebugPlWriteln(inp->val.t);
char *s; char *s;
s = Malloc(0); s = Malloc(MaxTmp());
if (!Yap_mpz_to_string(Yap_BigIntOfTerm(inp->val.t), s, MaxTmp() - 1, 10)) { if (!Yap_mpz_to_string(Yap_BigIntOfTerm(inp->val.t), s, MaxTmp() - 1, 10)) {
AUX_ERROR(inp->val.t, MaxTmp(PASS_REGS1), s, char); AUX_ERROR(inp->val.t, MaxTmp(PASS_REGS1), s, char);
} }
if (lengp)
*lengp = strlen(s);
return inp->val.uc = (unsigned char *)s; return inp->val.uc = (unsigned char *)s;
} }
#endif #endif
if (inp->type & YAP_STRING_TERM) { if (inp->type & YAP_STRING_TERM) {
// Yap_DebugPlWriteln(inp->val.t); // Yap_DebugPlWriteln(inp->val.t);
char *s = (char *) Yap_TermToString(inp->val.t, lengp, ENC_ISO_UTF8, 0); char *s = (char *)Yap_TermToString(inp->val.t, ENC_ISO_UTF8, 0);
return inp->val.uc = (unsigned char *)s; return inp->val.uc = (unsigned char *)s;
} }
if (inp->type & YAP_STRING_CHARS) { if (inp->type & YAP_STRING_CHARS) {
if (inp->enc == ENC_ISO_LATIN1) { if (inp->enc == ENC_ISO_LATIN1) {
return latin2utf8(inp, lengp); return latin2utf8(inp);
} else if (inp->enc == ENC_ISO_ASCII) { } else if (inp->enc == ENC_ISO_ASCII) {
if (lengp)
*lengp = strlen(inp->val.c);
return inp->val.uc; return inp->val.uc;
} else { // if (inp->enc == ENC_ISO_UTF8) { } else { // if (inp->enc == ENC_ISO_UTF8) {
if (lengp)
*lengp = strlen(inp->val.c);
return inp->val.uc; return inp->val.uc;
} }
} }
if (inp->type & YAP_STRING_WCHARS) { if (inp->type & YAP_STRING_WCHARS) {
// printf("%S\n",inp->val.w); // printf("%S\n",inp->val.w);
return wchar2utf8(inp, lengp); return wchar2utf8(inp);
} }
return NULL; return NULL;
} }
static Term write_strings(unsigned char *s0, seq_tv_t *out, static Term write_strings(unsigned char *s0, seq_tv_t *out USES_REGS) {
size_t leng USES_REGS) { size_t min = 0, max = strlen((char *)s0);
size_t min = 0, max = leng;
if (out->type & (YAP_STRING_NCHARS | YAP_STRING_TRUNC)) { if (out->type & (YAP_STRING_NCHARS | YAP_STRING_TRUNC)) {
if (out->type & YAP_STRING_NCHARS) if (out->type & YAP_STRING_NCHARS)
@ -547,12 +521,13 @@ static Term write_strings(unsigned char *s0, seq_tv_t *out,
return out->val.t; return out->val.t;
} }
static Term write_atoms(void *s0, seq_tv_t *out, size_t leng USES_REGS) { static Term write_atoms(void *s0, seq_tv_t *out USES_REGS) {
Term t = AbsPair(HR); Term t = AbsPair(HR);
char *s1 = (char *)s0;
size_t sz = 0; size_t sz = 0;
size_t max = leng; size_t max = strlen(s1);
if (leng == 0) { if (s1[0] == '\0') {
out->val.t = t; out->val.t = TermNil;
return TermNil; return TermNil;
} }
if (out->type & (YAP_STRING_NCHARS | YAP_STRING_TRUNC)) { if (out->type & (YAP_STRING_NCHARS | YAP_STRING_TRUNC)) {
@ -598,23 +573,23 @@ static Term write_atoms(void *s0, seq_tv_t *out, size_t leng USES_REGS) {
return (t); return (t);
} }
static Term write_codes(void *s0, seq_tv_t *out, size_t leng USES_REGS) { static Term write_codes(void *s0, seq_tv_t *out USES_REGS) {
Term t = AbsPair(HR); Term t;
size_t sz = 0; size_t sz = strlen(s0);
size_t max = leng; if (sz == 0) {
if (leng == 0) { if (out->type & YAP_STRING_DIFF) {
out->val.t = t; out->val.t = Globalize(out->dif PASS_REGS);
return TermNil; } else {
out->val.t = TermNil;
} }
if (out->type & (YAP_STRING_NCHARS | YAP_STRING_TRUNC)) { return out->val.t;
if (out->type & YAP_STRING_TRUNC && out->max < max)
max = out->max;
} }
unsigned char *s = s0, *lim = s + sz;
unsigned char *s = s0, *lim = s + strlen((char *) s);
unsigned char *cp = s; unsigned char *cp = s;
t = AbsPair(HR);
LOCAL_TERM_ERROR(t, 2 * (lim - s)); LOCAL_TERM_ERROR(t, 2 * (lim - s));
t = AbsPair(HR);
while (*cp) { while (*cp) {
utf8proc_int32_t chr; utf8proc_int32_t chr;
CELL *cl; CELL *cl;
@ -625,28 +600,20 @@ static Term write_codes(void *s0, seq_tv_t *out, size_t leng USES_REGS) {
HR += 2; HR += 2;
cl[0] = MkIntegerTerm(chr); cl[0] = MkIntegerTerm(chr);
cl[1] = AbsPair(HR); cl[1] = AbsPair(HR);
sz++;
if (sz == max)
break;
} }
if (out->type & YAP_STRING_DIFF) { if (sz == 0) {
if (sz == 0)
t = out->dif;
else
HR[-1] = Globalize(out->dif PASS_REGS); HR[-1] = Globalize(out->dif PASS_REGS);
} else { } else {
if (sz == 0)
t = TermNil;
else
HR[-1] = TermNil; HR[-1] = TermNil;
} }
out->val.t = t; out->val.t = t;
return (t); return (t);
} }
static Atom write_atom(void *s0, seq_tv_t *out, size_t leng USES_REGS) { static Atom write_atom(void *s0, seq_tv_t *out USES_REGS) {
unsigned char *s = s0; unsigned char *s = s0;
int32_t ch; int32_t ch;
size_t leng = strlen(s0);
if (leng == 0) { if (leng == 0) {
return Yap_LookupAtom(""); return Yap_LookupAtom("");
} }
@ -660,7 +627,8 @@ static Atom write_atom(void *s0, seq_tv_t *out, size_t leng USES_REGS) {
} }
} }
size_t write_buffer(unsigned char *s0, seq_tv_t *out, size_t leng USES_REGS) { size_t write_buffer(unsigned char *s0, seq_tv_t *out USES_REGS) {
size_t leng = strlen((char *)s0);
size_t min = 0, max = leng, room_end; size_t min = 0, max = leng, room_end;
if (out->enc == ENC_ISO_UTF8) { if (out->enc == ENC_ISO_UTF8) {
room_end = strlen((char *)s0) + 1; room_end = strlen((char *)s0) + 1;
@ -728,12 +696,11 @@ size_t write_buffer(unsigned char *s0, seq_tv_t *out, size_t leng USES_REGS) {
return room_end; return room_end;
} }
static size_t write_length(const unsigned char *s0, seq_tv_t *out, static size_t write_length(const unsigned char *s0, seq_tv_t *out USES_REGS) {
size_t leng USES_REGS) { return strlen_utf8(s0);
return leng;
} }
static Term write_number(unsigned char *s, seq_tv_t *out, int size, static Term write_number(unsigned char *s, seq_tv_t *out,
bool error_on USES_REGS) { bool error_on USES_REGS) {
Term t; Term t;
yap_error_number erro = LOCAL_Error_TYPE; yap_error_number erro = LOCAL_Error_TYPE;
@ -744,28 +711,28 @@ static Term write_number(unsigned char *s, seq_tv_t *out, int size,
return t; return t;
} }
static Term string_to_term(void *s, seq_tv_t *out, size_t leng USES_REGS) { static Term string_to_term(void *s, seq_tv_t *out USES_REGS) {
Term o; Term o;
yap_error_number erro = LOCAL_Error_TYPE; yap_error_number erro = LOCAL_Error_TYPE;
o = out->val.t = Yap_BufferToTerm(s, strlen(s) + 1, TermNil); o = out->val.t = Yap_BufferToTerm(s, TermNil);
LOCAL_Error_TYPE = erro; LOCAL_Error_TYPE = erro;
return o; return o;
} }
bool write_Text(unsigned char *inp, seq_tv_t *out, size_t leng USES_REGS) { bool write_Text(unsigned char *inp, seq_tv_t *out USES_REGS) {
/* we know what the term is */ /* we know what the term is */
if (out->type == 0) { if (out->type == 0) {
return true; return true;
} }
if (out->type & YAP_STRING_DATUM) { if (out->type & YAP_STRING_DATUM) {
if ((out->val.t = string_to_term(inp, out, leng PASS_REGS)) != 0L) if ((out->val.t = string_to_term(inp, out PASS_REGS)) != 0L)
return out->val.t != 0; return out->val.t != 0;
} }
if (out->type & (YAP_STRING_INT | YAP_STRING_FLOAT | YAP_STRING_BIG)) { if (out->type & (YAP_STRING_INT | YAP_STRING_FLOAT | YAP_STRING_BIG)) {
if ((out->val.t = write_number( if ((out->val.t = write_number(
inp, out, leng, !(out->type & YAP_STRING_ATOM) PASS_REGS)) != 0L) { inp, out, !(out->type & YAP_STRING_ATOM) PASS_REGS)) != 0L) {
// Yap_DebugPlWriteln(out->val.t); // Yap_DebugPlWriteln(out->val.t);
return true; return true;
@ -775,7 +742,7 @@ bool write_Text(unsigned char *inp, seq_tv_t *out, size_t leng USES_REGS) {
return false; return false;
} }
if (out->type & (YAP_STRING_ATOM)) { if (out->type & (YAP_STRING_ATOM)) {
if ((out->val.a = write_atom(inp, out, leng PASS_REGS)) != NIL) { if ((out->val.a = write_atom(inp, out PASS_REGS)) != NIL) {
Atom at = out->val.a; Atom at = out->val.a;
if (at && (out->type & YAP_STRING_OUTPUT_TERM)) if (at && (out->type & YAP_STRING_OUTPUT_TERM))
out->val.t = MkAtomTerm(at); out->val.t = MkAtomTerm(at);
@ -786,42 +753,40 @@ bool write_Text(unsigned char *inp, seq_tv_t *out, size_t leng USES_REGS) {
switch (out->type & YAP_TYPE_MASK) { switch (out->type & YAP_TYPE_MASK) {
case YAP_STRING_CHARS: { case YAP_STRING_CHARS: {
size_t room = write_buffer(inp, out, leng PASS_REGS); size_t room = write_buffer(inp, out PASS_REGS);
// printf("%s\n", out->val.c); // printf("%s\n", out->val.c);
return ((Int)room > 0); return ((Int)room > 0);
} }
case YAP_STRING_WCHARS: { case YAP_STRING_WCHARS: {
size_t room = write_buffer(inp, out, leng PASS_REGS); size_t room = write_buffer(inp, out PASS_REGS);
// printf("%S\n", out->val.w); // printf("%S\n", out->val.w);
return ((Int)room > 0); return ((Int)room > 0);
} }
case YAP_STRING_STRING: case YAP_STRING_STRING:
out->val.t = write_strings(inp, out, leng PASS_REGS); out->val.t = write_strings(inp, out PASS_REGS);
// Yap_DebugPlWriteln(out->val.t); // Yap_DebugPlWriteln(out->val.t);
return out->val.t != 0; return out->val.t != 0;
case YAP_STRING_ATOMS: case YAP_STRING_ATOMS:
out->val.t = write_atoms(inp, out, leng PASS_REGS); out->val.t = write_atoms(inp, out PASS_REGS);
// Yap_DebugPlWriteln(out->val.t); // Yap_DebugPlWriteln(out->val.t);
return out->val.t != 0; return out->val.t != 0;
case YAP_STRING_CODES: case YAP_STRING_CODES:
out->val.t = write_codes(inp, out, leng PASS_REGS); out->val.t = write_codes(inp, out PASS_REGS);
// Yap_DebugPlWriteln(out->val.t); // Yap_DebugPlWriteln(out->val.t);
return out->val.t != 0; return out->val.t != 0;
case YAP_STRING_LENGTH: case YAP_STRING_LENGTH:
out->val.l = write_length(inp, out, leng PASS_REGS); out->val.l = write_length(inp, out PASS_REGS);
// printf("s\n",out->val.l); // printf("s\n",out->val.l);
return out->val.l != (size_t)(-1); return out->val.l != (size_t)(-1);
case YAP_STRING_ATOM: case YAP_STRING_ATOM:
out->val.a = write_atom(inp, out, leng PASS_REGS); out->val.a = write_atom(inp, out PASS_REGS);
// Yap_DebugPlWriteln(out->val.t); // Yap_DebugPlWriteln(out->val.t);
return out->val.a != NULL; return out->val.a != NULL;
case YAP_STRING_INT | YAP_STRING_FLOAT | YAP_STRING_BIG: case YAP_STRING_INT | YAP_STRING_FLOAT | YAP_STRING_BIG:
out->val.t = write_number(inp, out, leng, true PASS_REGS); out->val.t = write_number(inp, out, true PASS_REGS);
// Yap_DebugPlWriteln(out->val.t); // Yap_DebugPlWriteln(out->val.t);
return out->val.t != 0; return out->val.t != 0;
default: { default: { return true; }
return true;
}
} }
return false; return false;
} }
@ -855,8 +820,6 @@ static size_t downcase(void *s0, seq_tv_t *out USES_REGS) {
bool Yap_CVT_Text(seq_tv_t *inp, seq_tv_t *out USES_REGS) { bool Yap_CVT_Text(seq_tv_t *inp, seq_tv_t *out USES_REGS) {
unsigned char *buf; unsigned char *buf;
bool rc; bool rc;
size_t leng;
/* /*
f//printfmark(stderr, "[ %d ", n++) ; f//printfmark(stderr, "[ %d ", n++) ;
if (inp->type & (YAP_STRING_TERM|YAP_STRING_ATOM|YAP_STRING_ATOMS_CODES if (inp->type & (YAP_STRING_TERM|YAP_STRING_ATOM|YAP_STRING_ATOMS_CODES
@ -867,10 +830,13 @@ bool Yap_CVT_Text(seq_tv_t *inp, seq_tv_t *out USES_REGS) {
else fprintf(stderr,"s %s\n", inp->val.c); else fprintf(stderr,"s %s\n", inp->val.c);
*/ */
// cnt++; // cnt++;
buf = Yap_readText(inp, &leng PASS_REGS); int l = push_text_stack();
buf = Yap_readText(inp PASS_REGS);
if (!buf) { if (!buf) {
pop_text_stack(l);
return 0L; return 0L;
} }
size_t leng = strlen_utf8(buf);
if (out->type & (YAP_STRING_NCHARS | YAP_STRING_TRUNC)) { if (out->type & (YAP_STRING_NCHARS | YAP_STRING_TRUNC)) {
if (out->max < leng) { if (out->max < leng) {
const unsigned char *ptr = skip_utf8(buf, out->max); const unsigned char *ptr = skip_utf8(buf, out->max);
@ -881,23 +847,25 @@ bool Yap_CVT_Text(seq_tv_t *inp, seq_tv_t *out USES_REGS) {
leng = diff; leng = diff;
} }
// else if (out->type & YAP_STRING_NCHARS && // else if (out->type & YAP_STRING_NCHARS &&
// const unsigned char *ptr = skip_utf8(buf, leng) // const unsigned char *ptr = skip_utf8(buf)
} }
if (out->type & (YAP_STRING_UPCASE | YAP_STRING_DOWNCASE)) { if (out->type & (YAP_STRING_UPCASE | YAP_STRING_DOWNCASE)) {
if (out->type & YAP_STRING_UPCASE) { if (out->type & YAP_STRING_UPCASE) {
if (!upcase(buf, out)) { if (!upcase(buf, out)) {
pop_text_stack(l);
return false; return false;
} }
} }
if (out->type & YAP_STRING_DOWNCASE) { if (out->type & YAP_STRING_DOWNCASE) {
if (!downcase(buf, out)) { if (!downcase(buf, out)) {
pop_text_stack(l);
return false; return false;
} }
} }
} }
rc = write_Text(buf, out, leng PASS_REGS); rc = write_Text(buf, out PASS_REGS);
/* fprintf(stderr, " -> "); /* fprintf(stderr, " -> ");
if (!rc) fprintf(stderr, "NULL"); if (!rc) fprintf(stderr, "NULL");
else if (out->type & else if (out->type &
@ -910,10 +878,10 @@ bool Yap_CVT_Text(seq_tv_t *inp, seq_tv_t *out USES_REGS) {
else else
fprintf(stderr, "%s", out->val.c); fprintf(stderr, "%s", out->val.c);
fprintf(stderr, "\n]\n"); */ fprintf(stderr, "\n]\n"); */
pop_text_stack(l);
return rc; return rc;
} }
static unsigned char *concat(int n, void *sv[] USES_REGS) { static unsigned char *concat(int n, void *sv[] USES_REGS) {
void *buf; void *buf;
unsigned char *buf0; unsigned char *buf0;
@ -921,16 +889,21 @@ static unsigned char *concat(int n, void *sv[] USES_REGS) {
int i; int i;
for (i = 0; i < n; i++) { for (i = 0; i < n; i++) {
room += strlen((char *) sv[i]); char *s = sv[i];
if (s[0])
room += strlen(s);
} }
buf = Malloc(room + 1); buf = Malloc(room + 1);
buf0 = buf; buf0 = buf;
for (i = 0; i < n; i++) { for (i = 0; i < n; i++) {
char *s = sv[i];
if (!s[0])
continue;
#if _WIN32 || defined(__ANDROID__) #if _WIN32 || defined(__ANDROID__)
strcpy(buf, sv[i]); strcpy(buf, s);
buf = (char *)buf + strlen(buf); buf = (char *)buf + strlen(buf);
#else #else
buf = stpcpy(buf, sv[i]); buf = stpcpy(buf, s);
#endif #endif
} }
return buf0; return buf0;
@ -955,25 +928,32 @@ static void *slice(size_t min, size_t max, const unsigned char *buf USES_REGS) {
bool Yap_Concat_Text(int tot, seq_tv_t inp[], seq_tv_t *out USES_REGS) { bool Yap_Concat_Text(int tot, seq_tv_t inp[], seq_tv_t *out USES_REGS) {
void **bufv; void **bufv;
unsigned char *buf; unsigned char *buf;
int i; int i, j;
size_t leng;
bufv = Malloc(tot * sizeof(unsigned char *)); bufv = Malloc(tot * sizeof(unsigned char *));
if (!bufv) { if (!bufv) {
return NULL; return NULL;
} }
for (i = 0; i < tot; i++) { for (i = 0, j = 0; i < tot; i++) {
inp[i].type |= YAP_STRING_WITH_BUFFER; inp[j].type |= YAP_STRING_WITH_BUFFER;
unsigned char *nbuf = Yap_readText(inp + i, &leng PASS_REGS); unsigned char *nbuf = Yap_readText(inp + i PASS_REGS);
if (!nbuf) { if (!nbuf) {
return NULL; return NULL;
} }
bufv[i] = nbuf; if (!nbuf[0])
continue;
bufv[j++] = nbuf;
} }
if (j == 0) {
buf = malloc(8);
memset(buf, 0, 4);
} else if (j == 1) {
buf = bufv[0];
} else {
buf = concat(tot, bufv PASS_REGS); buf = concat(tot, bufv PASS_REGS);
bool rc = write_Text(buf, out, strlen_utf8(buf) PASS_REGS); }
bool rc = write_Text(buf, out PASS_REGS);
return rc; return rc;
} }
@ -985,10 +965,11 @@ bool Yap_Splice_Text(int n, size_t cuts[], seq_tv_t *inp,
size_t b_l, u_l; size_t b_l, u_l;
inp->type |= YAP_STRING_IN_TMP; inp->type |= YAP_STRING_IN_TMP;
buf = Yap_readText(inp, &b_l PASS_REGS); buf = Yap_readText(inp PASS_REGS);
if (!buf) { if (!buf) {
return false; return false;
} }
b_l = strlen((char *)buf);
u_l = strlen_utf8(buf); u_l = strlen_utf8(buf);
if (!cuts) { if (!cuts) {
if (n == 2) { if (n == 2) {
@ -996,28 +977,31 @@ bool Yap_Splice_Text(int n, size_t cuts[], seq_tv_t *inp,
unsigned char *buf0, *buf1; unsigned char *buf0, *buf1;
if (outv[0].val.t) { if (outv[0].val.t) {
buf0 = Yap_readText(outv, &b_l0 PASS_REGS); buf0 = Yap_readText(outv PASS_REGS);
if (!buf0) { if (!buf0) {
return false; return false;
} }
b_l0 = strlen((const char *)buf0);
if (bcmp(buf, buf0, b_l0) != 0) { if (bcmp(buf, buf0, b_l0) != 0) {
return false; return false;
} }
u_l0 = strlen_utf8(buf0); u_l0 = strlen_utf8(buf0);
u_l1 = u_l - u_l0; u_l1 = u_l - u_l0;
b_l1 = b_l - b_l0;
buf1 = slice(u_l0, u_l, buf PASS_REGS); buf1 = slice(u_l0, u_l, buf PASS_REGS);
b_l1 = strlen((const char *)buf1); b_l1 = strlen((const char *)buf1);
bool rc = write_Text(buf1, outv + 1, b_l1 PASS_REGS); bool rc = write_Text(buf1, outv + 1 PASS_REGS);
if (!rc) { if (!rc) {
return false; return false;
} }
return rc; return rc;
} else /* if (outv[1].val.t) */ { } else /* if (outv[1].val.t) */ {
buf1 = Yap_readText(outv + 1, &b_l1 PASS_REGS); buf1 = Yap_readText(outv + 1 PASS_REGS);
if (!buf1) { if (!buf1) {
return false; return false;
} }
b_l1 = strlen((char *)buf1);
u_l1 = strlen_utf8(buf1); u_l1 = strlen_utf8(buf1);
b_l0 = b_l - b_l1; b_l0 = b_l - b_l1;
u_l0 = u_l - u_l1; u_l0 = u_l - u_l1;
@ -1026,7 +1010,7 @@ bool Yap_Splice_Text(int n, size_t cuts[], seq_tv_t *inp,
return false; return false;
} }
buf0 = slice(0, u_l0, buf PASS_REGS); buf0 = slice(0, u_l0, buf PASS_REGS);
bool rc = write_Text(buf0, outv, b_l0 PASS_REGS); bool rc = write_Text(buf0, outv PASS_REGS);
return rc; return rc;
} }
} }
@ -1040,7 +1024,7 @@ bool Yap_Splice_Text(int n, size_t cuts[], seq_tv_t *inp,
if (i > 0 && cuts[i] == 0) if (i > 0 && cuts[i] == 0)
break; break;
void *bufi = slice(next, cuts[i], buf PASS_REGS); void *bufi = slice(next, cuts[i], buf PASS_REGS);
if (!write_Text(bufi, outv + i, strlen(bufi) PASS_REGS)) { if (!write_Text(bufi, outv + i PASS_REGS)) {
return false; return false;
} }
} }
@ -1063,8 +1047,7 @@ bool Yap_Splice_Text(int n, size_t cuts[], seq_tv_t *inp,
* @return the buffer, or NULL in case of failure. If so, Yap_Error may be * @return the buffer, or NULL in case of failure. If so, Yap_Error may be
called. called.
*/ */
const char *Yap_TextTermToText(Term t, char *buf, size_t len, encoding_t enc) { const char *Yap_TextTermToText(Term t, char *buf, encoding_t enc USES_REGS) {
CACHE_REGS
seq_tv_t inp, out; seq_tv_t inp, out;
inp.val.t = t; inp.val.t = t;
if (IsAtomTerm(t) && t != TermNil) { if (IsAtomTerm(t) && t != TermNil) {
@ -1127,8 +1110,7 @@ const char *Yap_PredIndicatorToUTF8String(PredEntry *ap) {
if (ap->ModuleOfPred == IDB_MODULE) { if (ap->ModuleOfPred == IDB_MODULE) {
if (ap->PredFlags & NumberDBPredFlag) { if (ap->PredFlags & NumberDBPredFlag) {
Int key = ap->src.IndxId; Int key = ap->src.IndxId;
snprintf(s, smax - s, "%" snprintf(s, smax - s, "%" PRIdPTR, key);
PRIdPTR, key);
return LOCAL_FileNameBuf; return LOCAL_FileNameBuf;
} else if (ap->PredFlags & AtomDBPredFlag) { } else if (ap->PredFlags & AtomDBPredFlag) {
at = (Atom)(ap->FunctorOfPred); at = (Atom)(ap->FunctorOfPred);
@ -1151,8 +1133,7 @@ const char *Yap_PredIndicatorToUTF8String(PredEntry *ap) {
return NULL; return NULL;
} }
s += strlen(s); s += strlen(s);
snprintf(s, smax - s, "/%" snprintf(s, smax - s, "/%" PRIdPTR, arity);
PRIdPTR, arity);
return s0; return s0;
} }
@ -1165,8 +1146,7 @@ const char *Yap_PredIndicatorToUTF8String(PredEntry *ap) {
* *
* @return the term * @return the term
*/ */
Term Yap_MkTextTerm(const char *s, encoding_t enc, Term tguide) { Term Yap_MkTextTerm(const char *s, encoding_t enc, Term tguide USES_REGS) {
CACHE_REGS
if (IsAtomTerm(tguide)) if (IsAtomTerm(tguide))
return MkAtomTerm(Yap_LookupAtom(s)); return MkAtomTerm(Yap_LookupAtom(s));
if (IsStringTerm(tguide)) if (IsStringTerm(tguide))

View File

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

View File

@ -4585,6 +4585,9 @@ renumbervar(Term t, Int id USES_REGS)
ts[1] = MkIntegerTerm(id); ts[1] = MkIntegerTerm(id);
} }
extern int vsc;
int vsc;
static Int numbervars_in_complex_term(register CELL *pt0, register CELL *pt0_end, Int numbv, int singles USES_REGS) static Int numbervars_in_complex_term(register CELL *pt0, register CELL *pt0_end, Int numbv, int singles USES_REGS)
{ {
@ -4604,6 +4607,7 @@ static Int numbervars_in_complex_term(register CELL *pt0, register CELL *pt0_end
deref_head(d0, vars_in_term_unk); deref_head(d0, vars_in_term_unk);
vars_in_term_nvar: vars_in_term_nvar:
{ {
vsc++;
if (IsPairTerm(d0)) { if (IsPairTerm(d0)) {
if (to_visit + 1024 >= (CELL **)AuxSp) { if (to_visit + 1024 >= (CELL **)AuxSp) {
goto aux_overflow; goto aux_overflow;

View File

@ -390,7 +390,7 @@ int Yap_FormatFloat(Float f, char **s, size_t sz) {
wglb.stream = GLOBAL_Stream + sno; wglb.stream = GLOBAL_Stream + sno;
wrputf(f, &wglb); wrputf(f, &wglb);
so = Yap_MemExportStreamPtr(sno); so = Yap_MemExportStreamPtr(sno);
*s = Malloc( strlen(so) )+1; *s = Malloc( strlen(so)+1 );
strcpy(*s, so ); strcpy(*s, so );
Yap_CloseStream(sno); Yap_CloseStream(sno);
return true; return true;
@ -1255,7 +1255,7 @@ void Yap_plwrite(Term t, StreamDesc *mywrite, int max_depth, int flags,
Yap_CloseSlots(sls); Yap_CloseSlots(sls);
} }
char *Yap_TermToString(Term t, size_t *lengthp, encoding_t enc, int flags) { char *Yap_TermToString(Term t, encoding_t enc, int flags) {
CACHE_REGS CACHE_REGS
int sno = Yap_open_buf_write_stream(enc, flags); int sno = Yap_open_buf_write_stream(enc, flags);
const char *sf; const char *sf;

View File

@ -12,7 +12,7 @@ extern "C" {
#include "YapInterface.h" #include "YapInterface.h"
#include "blobs.h" #include "blobs.h"
X_API char *Yap_TermToString(Term t, size_t *length, encoding_t encodingp, X_API char *Yap_TermToString(Term t, encoding_t encodingp,
int flags); int flags);
X_API void YAP_UserCPredicate(const char *, YAP_UserCPred, arity_t arity); X_API void YAP_UserCPredicate(const char *, YAP_UserCPred, arity_t arity);

View File

@ -226,18 +226,17 @@ public:
/// return a string with a textual representation of the term /// return a string with a textual representation of the term
virtual const char *text() { virtual const char *text() {
CACHE_REGS CACHE_REGS
size_t length = 0;
encoding_t enc = LOCAL_encoding; encoding_t enc = LOCAL_encoding;
char *os; char *os;
BACKUP_MACHINE_REGS(); BACKUP_MACHINE_REGS();
if (!(os = Yap_TermToString(Yap_GetFromSlot(t), &length, enc, if (!(os = Yap_TermToString(Yap_GetFromSlot(t), enc,
Handle_vars_f))) { Handle_vars_f))) {
RECOVER_MACHINE_REGS(); RECOVER_MACHINE_REGS();
return 0; return 0;
} }
RECOVER_MACHINE_REGS(); RECOVER_MACHINE_REGS();
length = strlen(os) + 1; size_t length = strlen(os);
char *sm = (char *)malloc(length + 1); char *sm = (char *)malloc(length + 1);
strcpy(sm, os); strcpy(sm, os);
return sm; return sm;

View File

@ -47,7 +47,6 @@
cut_succeed(); \ cut_succeed(); \
} }
/// allocate a temporary text block /// allocate a temporary text block
/// ///
extern void *Malloc(size_t sz USES_REGS); extern void *Malloc(size_t sz USES_REGS);
@ -56,8 +55,7 @@ extern void Free(void *buf USES_REGS);
extern int push_text_stack(USES_REGS1); extern int push_text_stack(USES_REGS1);
extern int pop_text_stack(int lvl USES_REGS); extern int pop_text_stack(int lvl USES_REGS);
extern void *protected_pop_text_stack(int lvl, void *safe, bool tmp, extern void *export_block(int lvl, void *exp USES_REGS);
size_t sz USES_REGS);
#ifndef Yap_Min #ifndef Yap_Min
#define Yap_Min(x, y) (x < y ? x : y) #define Yap_Min(x, y) (x < y ? x : y)
@ -187,8 +185,10 @@ INLINE_ONLY inline EXTERN utf8proc_ssize_t get_utf8(const utf8proc_uint8_t *ptr,
utf8proc_int32_t *valp) { utf8proc_int32_t *valp) {
utf8proc_ssize_t rc = utf8proc_iterate(ptr, n, valp); utf8proc_ssize_t rc = utf8proc_iterate(ptr, n, valp);
if (rc < 0) { if (rc < 0) {
if (rc == UTF8PROC_ERROR_INVALIDUTF8) Yap_Warning("get_utf8 found bad UTF-8 char %s, skipping...", ptr); if (rc == UTF8PROC_ERROR_INVALIDUTF8)
else Yap_Warning("UTF-8 error %d, skipping...", *ptr); Yap_Warning("get_utf8 found bad UTF-8 char %s, skipping...", ptr);
else
Yap_Warning("UTF-8 error %d, skipping...", *ptr);
rc = 1; rc = 1;
} }
return rc; return rc;
@ -200,7 +200,8 @@ INLINE_ONLY inline EXTERN utf8proc_ssize_t put_utf8(utf8proc_uint8_t *ptr,
INLINE_ONLY inline EXTERN utf8proc_ssize_t put_utf8(utf8proc_uint8_t *ptr, INLINE_ONLY inline EXTERN utf8proc_ssize_t put_utf8(utf8proc_uint8_t *ptr,
utf8proc_int32_t val) { utf8proc_int32_t val) {
utf8proc_ssize_t rc = utf8proc_encode_char(val, ptr); utf8proc_ssize_t rc = utf8proc_encode_char(val, ptr);
if (rc == 0) Yap_Warning("UTF-8 error at %s", ptr); if (rc == 0)
Yap_Warning("UTF-8 error at %s", ptr);
return rc; return rc;
} }
@ -213,8 +214,11 @@ inline static const utf8proc_uint8_t *skip_utf8(const utf8proc_uint8_t *pt,
if (b == 0) if (b == 0)
return pt; return pt;
if (l < 0) { if (l < 0) {
if (l == UTF8PROC_ERROR_INVALIDUTF8) Yap_Warning("skip_utf8: found bad UTF-8 in char %s[%d], skipping...", pt, i); if (l == UTF8PROC_ERROR_INVALIDUTF8)
else Yap_Warning("UTF-8 error %d at %s[%d], skipping...", l,pt,i); Yap_Warning("skip_utf8: found bad UTF-8 in char %s[%d], skipping...",
pt, i);
else
Yap_Warning("UTF-8 error %d at %s[%d], skipping...", l, pt, i);
l = 1; l = 1;
} }
pt += l; pt += l;
@ -237,8 +241,10 @@ inline static utf8proc_ssize_t strlen_utf8(const utf8proc_uint8_t *pt) {
pt += l; pt += l;
rc++; rc++;
} else { } else {
if (l == UTF8PROC_ERROR_INVALIDUTF8) Yap_Warning("found bad UTF-8 char %d, skipping %s...", *pt, pt); if (l == UTF8PROC_ERROR_INVALIDUTF8)
else Yap_Warning("UTF-8 error %d, skipping...", l); Yap_Warning("found bad UTF-8 char %d, skipping %s...", *pt, pt);
else
Yap_Warning("UTF-8 error %d, skipping...", l);
pt++; pt++;
} }
} }
@ -451,9 +457,8 @@ static inline seq_type_t mod_to_bqtype(Term mod USES_REGS) {
// the routines // the routines
extern unsigned char *Yap_readText(seq_tv_t *inp, size_t *lengp USES_REGS); extern unsigned char *Yap_readText(seq_tv_t *inp USES_REGS);
extern bool write_Text(unsigned char *inp, seq_tv_t *out, extern bool write_Text(unsigned char *inp, seq_tv_t *out USES_REGS);
size_t leng USES_REGS);
extern bool Yap_CVT_Text(seq_tv_t *inp, seq_tv_t *out USES_REGS); extern bool Yap_CVT_Text(seq_tv_t *inp, seq_tv_t *out USES_REGS);
extern bool Yap_Concat_Text(int n, seq_tv_t inp[], seq_tv_t *out USES_REGS); extern bool Yap_Concat_Text(int n, seq_tv_t inp[], seq_tv_t *out USES_REGS);
extern bool Yap_Splice_Text(int n, size_t cuts[], seq_tv_t *inp, extern bool Yap_Splice_Text(int n, size_t cuts[], seq_tv_t *inp,
@ -1376,6 +1381,7 @@ static inline size_t Yap_AtomToLength(Term t0 USES_REGS) {
out.type = out.type =
YAP_STRING_INT | YAP_STRING_FLOAT | YAP_STRING_BIG | YAP_STRING_TERM; YAP_STRING_INT | YAP_STRING_FLOAT | YAP_STRING_BIG | YAP_STRING_TERM;
out.val.uc = NULL; out.val.uc = NULL;
out.enc = ENC_ISO_UTF8;
if (!Yap_CVT_Text(&inp, &out PASS_REGS)) if (!Yap_CVT_Text(&inp, &out PASS_REGS))
return 0L; return 0L;
return out.val.t; return out.val.t;
@ -1629,5 +1635,6 @@ static inline size_t Yap_AtomToLength(Term t0 USES_REGS) {
#endif // ≈YAP_TEXT_H #endif // ≈YAP_TEXT_H
const char *Yap_TextTermToText(Term t, char *buf, size_t len, encoding_t enc); const char *Yap_TextTermToText(Term t, char *bufwrite_Text,
encoding_t e USES_REGS);
Term Yap_MkTextTerm(const char *s, encoding_t e, Term tguide); Term Yap_MkTextTerm(const char *s, encoding_t e, Term tguide);

View File

@ -227,9 +227,7 @@ X_API int PL_get_nchars(term_t l, size_t *lengthp, char **s, unsigned flags) {
pop_text_stack(lvl); pop_text_stack(lvl);
return false; return false;
} }
out.val.c = protected_pop_text_stack(lvl, out.val.c, out.val.c = export_block(-1, out.val.c PASS_REGS);
flags & (BUF_RING | BUF_DISCARDABLE),
strlen(out.val.c) + 1 PASS_REGS);
*s = out.val.c; *s = out.val.c;
return true; return true;
} }
@ -1786,7 +1784,8 @@ int PL_unify_termv(term_t l, va_list ap) {
Term t = Yap_MkNewApplTerm(ff, arity); Term t = Yap_MkNewApplTerm(ff, arity);
if (nels) { if (nels) {
if (depth == MAX_DEPTH) { if (depth == MAX_DEPTH) {
fprintf(stderr, "ERROR: very deep term in PL_unify_term, change " fprintf(stderr,
"ERROR: very deep term in PL_unify_term, change "
"MAX_DEPTH from %d\n", "MAX_DEPTH from %d\n",
MAX_DEPTH); MAX_DEPTH);
return FALSE; return FALSE;
@ -2525,7 +2524,8 @@ X_API void PL_register_foreign_in_module(const char *module, const char *name,
#ifdef DEBUG #ifdef DEBUG
if (flags & (PL_FA_CREF)) { if (flags & (PL_FA_CREF)) {
fprintf(stderr, "PL_register_foreign_in_module called with non-implemented " fprintf(stderr,
"PL_register_foreign_in_module called with non-implemented "
"flag %x when creating predicate %s:%s/%d\n", "flag %x when creating predicate %s:%s/%d\n",
flags, module, name, arity); flags, module, name, arity);
} }

View File

@ -42,7 +42,7 @@ bool Yap_GetFileName(Term t, char *buf, size_t len, encoding_t enc) {
t = ArgOfTerm(2, t); t = ArgOfTerm(2, t);
len -= (szl + 1); len -= (szl + 1);
} }
return Yap_TextTermToText(t, buf, len, enc); return Yap_TextTermToText(t, buf, enc);
} }
static Int file_name_extension(USES_REGS1) { static Int file_name_extension(USES_REGS1) {
@ -78,8 +78,7 @@ static Int file_name_extension(USES_REGS1) {
Yap_unify(t2, t); Yap_unify(t2, t);
} else { } else {
f2 = ss + (strlen(ss) + 1); f2 = ss + (strlen(ss) + 1);
if (!Yap_TextTermToText(t2, f2, YAP_FILENAME_MAX - 1 - (f2 - f), if (!Yap_TextTermToText(t2, f2, ENC_ISO_UTF8))
ENC_ISO_UTF8))
return false; return false;
#if __APPLE__ || _WIN32 #if __APPLE__ || _WIN32
Yap_OverwriteUTF8BufferToLowCase(f2); Yap_OverwriteUTF8BufferToLowCase(f2);
@ -113,12 +112,11 @@ static Int file_name_extension(USES_REGS1) {
return true; return true;
} else { } else {
char *f2; char *f2;
if (!Yap_TextTermToText(t1, f, YAP_FILENAME_MAX - 2, ENC_ISO_UTF8)) { if (!Yap_TextTermToText(t1, f, ENC_ISO_UTF8)) {
return false; return false;
} }
f2 = f + strlen(f); f2 = f + strlen(f);
if (!Yap_TextTermToText(t2, f2, YAP_FILENAME_MAX - 2 - (f2 - f), if (!Yap_TextTermToText(t2, f2, ENC_ISO_UTF8)) {
ENC_ISO_UTF8)) {
return false; return false;
} }
if (f2[0] != '.') { if (f2[0] != '.') {
@ -460,7 +458,7 @@ static Int is_absolute_file_name(USES_REGS1) { /* file_base_name(Stream,N) */
Yap_Error(INSTANTIATION_ERROR, t, "file_base_name/2"); Yap_Error(INSTANTIATION_ERROR, t, "file_base_name/2");
return false; return false;
} }
const char *buf = Yap_TextTermToText(t, NULL, 0, LOCAL_encoding); const char *buf = Yap_TextTermToText(t, NULL, LOCAL_encoding);
if (buf) { if (buf) {
rc = Yap_IsAbsolutePath(buf); rc = Yap_IsAbsolutePath(buf);
} else { } else {

View File

@ -23,13 +23,11 @@ static char SccsId[] = "%W% %G%";
* *
*/ */
#include "sysbits.h"
#include "format.h" #include "format.h"
#include "sysbits.h"
#if HAVE_FMEMOPEN #if HAVE_FMEMOPEN
int format_synch(int sno, int sno0, format_info *fg) { int format_synch(int sno, int sno0, format_info *fg) {
const char *s; const char *s;
int n; int n;
@ -115,8 +113,8 @@ static char SccsId[] = "%W% %G%";
return true; return true;
} }
bool Yap_set_stream_to_buf(StreamDesc *st, const char *buf, encoding_t enc,
bool Yap_set_stream_to_buf(StreamDesc *st, const char *buf, encoding_t enc, size_t nchars) { size_t nchars) {
FILE *f; FILE *f;
// like any file stream. // like any file stream.
@ -139,8 +137,7 @@ int Yap_open_buf_read_stream(const char *buf, size_t nchars, enc
sno = GetFreeStreamD(); sno = GetFreeStreamD();
if (sno < 0) if (sno < 0)
return (PlIOError(RESOURCE_ERROR_MAX_STREAMS, return (PlIOError(RESOURCE_ERROR_MAX_STREAMS, TermNil,
TermNil,
"new stream not available for open_mem_read_stream/1")); "new stream not available for open_mem_read_stream/1"));
st = GLOBAL_Stream + sno; st = GLOBAL_Stream + sno;
if (encp) if (encp)
@ -165,7 +162,7 @@ open_mem_read_stream(USES_REGS1) /* $open_mem_read_stream(+List,-Stream) */
const char *buf; const char *buf;
ti = Deref(ARG1); ti = Deref(ARG1);
buf = Yap_TextTermToText(ti, NULL, 0, LOCAL_encoding); buf = Yap_TextTermToText(ti, NULL, LOCAL_encoding);
if (!buf) { if (!buf) {
return false; return false;
} }
@ -290,7 +287,6 @@ void Yap_MemOps(StreamDesc *st) {
st->stream_putc = FilePutc; st->stream_putc = FilePutc;
st->stream_getc = PlGetc; st->stream_getc = PlGetc;
} }
static int sssno; static int sssno;

View File

@ -1,8 +1,10 @@
#define FORMAT_MAX_SIZE 1024 #define FORMAT_MAX_SIZE 1024
#include <Yap.h>
typedef struct { typedef struct {
Int filler; intptr_t filler;
/* character to dump */ /* character to dump */
int phys; int phys;
/* position in buffer */ /* position in buffer */

View File

@ -294,7 +294,7 @@ static void InitStdStream(int sno, SMALLUNSGN flags, FILE *file, VFS_t *vfsp) {
s->status = flags; s->status = flags;
s->linepos = 0; s->linepos = 0;
s->linecount = 1; s->linecount = 1;
s->charcount = 0.; s->charcount = 0;
s->vfs = vfsp; s->vfs = vfsp;
s->encoding = ENC_ISO_UTF8; s->encoding = ENC_ISO_UTF8;
INIT_LOCK(s->streamlock); INIT_LOCK(s->streamlock);

View File

@ -286,7 +286,7 @@ form:
+ symbols, including `(`, `)`, `,`, `;` + symbols, including `(`, `)`, `,`, `;`
*/ */
static Int scan_to_list(USES_ARGS1) { static Int scan_to_list(USES_REGS1) {
int inp_stream; int inp_stream;
Term tpos, tout; Term tpos, tout;
@ -1350,11 +1350,11 @@ static Int style_checker(USES_REGS1) {
return TRUE; return TRUE;
} }
Term Yap_BufferToTerm(const unsigned char *s, size_t len, Term opts) { Term Yap_BufferToTerm(const unsigned char *s, Term opts) {
Term rval; Term rval;
int sno; int sno;
encoding_t l = ENC_ISO_UTF8; encoding_t l = ENC_ISO_UTF8;
sno = Yap_open_buf_read_stream((char *)s, len, &l, MEM_BUF_USER); sno = Yap_open_buf_read_stream((char *)s, strlen((const char*)s), &l, MEM_BUF_USER);
rval = Yap_read_term(sno, opts, false); rval = Yap_read_term(sno, opts, false);
Yap_CloseStream(sno); Yap_CloseStream(sno);
@ -1374,7 +1374,7 @@ X_API Term Yap_BufferToTermWithPrioBindings(const unsigned char *s, size_t len,
if (prio != 1200) { if (prio != 1200) {
ctl = add_priority(bindings, ctl); ctl = add_priority(bindings, ctl);
} }
return Yap_BufferToTerm(s, len, ctl); return Yap_BufferToTerm(s, ctl);
} }
/** /**
@ -1397,7 +1397,6 @@ static Int read_term_from_atom(USES_REGS1) {
Term t1 = Deref(ARG1); Term t1 = Deref(ARG1);
Atom at; Atom at;
const unsigned char *s; const unsigned char *s;
size_t len;
if (IsVarTerm(t1)) { if (IsVarTerm(t1)) {
Yap_Error(INSTANTIATION_ERROR, t1, "style_check/1"); Yap_Error(INSTANTIATION_ERROR, t1, "style_check/1");
@ -1408,11 +1407,10 @@ static Int read_term_from_atom(USES_REGS1) {
} else { } else {
at = AtomOfTerm(t1); at = AtomOfTerm(t1);
s = at->UStrOfAE; s = at->UStrOfAE;
len = strlen_utf8(s);
} }
Term ctl = add_output(ARG2, ARG3); Term ctl = add_output(ARG2, ARG3);
return Yap_BufferToTerm(s, len, ctl); return Yap_BufferToTerm(s, ctl);
} }
/** /**
@ -1433,7 +1431,7 @@ static Int read_term_from_atom(USES_REGS1) {
static Int read_term_from_atomic(USES_REGS1) { static Int read_term_from_atomic(USES_REGS1) {
Term t1 = Deref(ARG1); Term t1 = Deref(ARG1);
const unsigned char *s; const unsigned char *s;
size_t len;
if (IsVarTerm(t1)) { if (IsVarTerm(t1)) {
Yap_Error(INSTANTIATION_ERROR, t1, "read_term_from_atomic/3"); Yap_Error(INSTANTIATION_ERROR, t1, "read_term_from_atomic/3");
return (FALSE); return (FALSE);
@ -1443,11 +1441,10 @@ static Int read_term_from_atomic(USES_REGS1) {
} else { } else {
Term t = Yap_AtomicToString(t1 PASS_REGS); Term t = Yap_AtomicToString(t1 PASS_REGS);
s = UStringOfTerm(t); s = UStringOfTerm(t);
len = strlen_utf8(s);
} }
Term ctl = add_output(ARG2, ARG3); Term ctl = add_output(ARG2, ARG3);
return Yap_BufferToTerm(s, len, ctl); return Yap_BufferToTerm(s, ctl);
} }
/** /**
@ -1488,7 +1485,6 @@ static Int read_term_from_string(USES_REGS1) {
static Int atomic_to_term(USES_REGS1) { static Int atomic_to_term(USES_REGS1) {
Term t1 = Deref(ARG1); Term t1 = Deref(ARG1);
size_t len;
if (IsVarTerm(t1)) { if (IsVarTerm(t1)) {
Yap_Error(INSTANTIATION_ERROR, t1, "read_term_from_string/3"); Yap_Error(INSTANTIATION_ERROR, t1, "read_term_from_string/3");
return (FALSE); return (FALSE);
@ -1498,15 +1494,13 @@ static Int atomic_to_term(USES_REGS1) {
} else { } else {
Term t = Yap_AtomicToString(t1 PASS_REGS); Term t = Yap_AtomicToString(t1 PASS_REGS);
const unsigned char *us = UStringOfTerm(t); const unsigned char *us = UStringOfTerm(t);
len = strlen_utf8(us); return Yap_BufferToTerm(us,
return Yap_BufferToTerm(us, len,
add_output(ARG2, add_names(ARG3, TermNil))); add_output(ARG2, add_names(ARG3, TermNil)));
} }
} }
static Int atom_to_term(USES_REGS1) { static Int atom_to_term(USES_REGS1) {
Term t1 = Deref(ARG1); Term t1 = Deref(ARG1);
size_t len;
if (IsVarTerm(t1)) { if (IsVarTerm(t1)) {
Yap_Error(INSTANTIATION_ERROR, t1, "read_term_from_string/3"); Yap_Error(INSTANTIATION_ERROR, t1, "read_term_from_string/3");
return (FALSE); return (FALSE);
@ -1516,15 +1510,14 @@ static Int atom_to_term(USES_REGS1) {
} else { } else {
Term t = Yap_AtomicToString(t1 PASS_REGS); Term t = Yap_AtomicToString(t1 PASS_REGS);
const unsigned char *us = UStringOfTerm(t); const unsigned char *us = UStringOfTerm(t);
len = strlen_utf8(us); return Yap_BufferToTerm(us,
return Yap_BufferToTerm(us, len,
add_output(ARG2, add_names(ARG3, TermNil))); add_output(ARG2, add_names(ARG3, TermNil)));
} }
} }
static Int string_to_term(USES_REGS1) { static Int string_to_term(USES_REGS1) {
Term t1 = Deref(ARG1); Term t1 = Deref(ARG1);
size_t len;
if (IsVarTerm(t1)) { if (IsVarTerm(t1)) {
Yap_Error(INSTANTIATION_ERROR, t1, "read_term_from_string/3"); Yap_Error(INSTANTIATION_ERROR, t1, "read_term_from_string/3");
return (FALSE); return (FALSE);
@ -1533,8 +1526,7 @@ static Int string_to_term(USES_REGS1) {
return (FALSE); return (FALSE);
} else { } else {
const unsigned char *us = UStringOfTerm(t1); const unsigned char *us = UStringOfTerm(t1);
len = strlen_utf8(us); return Yap_BufferToTerm(us,
return Yap_BufferToTerm(us, len,
add_output(ARG2, add_names(ARG3, TermNil))); add_output(ARG2, add_names(ARG3, TermNil)));
} }
} }

View File

@ -847,7 +847,7 @@ static Int absolute_file_system_path(USES_REGS1) {
const char *fp; const char *fp;
bool rc; bool rc;
char s[MAXPATHLEN + 1]; char s[MAXPATHLEN + 1];
const char *text = Yap_TextTermToText(t, s, MAXPATHLEN, LOCAL_encoding); const char *text = Yap_TextTermToText(t, s, LOCAL_encoding);
if (text == NULL) { if (text == NULL) {
return false; return false;
@ -1359,7 +1359,7 @@ static Int p_expand_file_name(USES_REGS1) {
Yap_Error(INSTANTIATION_ERROR, t, "argument to true_file_name unbound"); Yap_Error(INSTANTIATION_ERROR, t, "argument to true_file_name unbound");
return FALSE; return FALSE;
} }
text = Yap_TextTermToText(t, NULL, 0, LOCAL_encoding); text = Yap_TextTermToText(t, NULL, LOCAL_encoding);
if (!text) if (!text)
return false; return false;
if (!(text2 = PlExpandVars(text, NULL, NULL))) if (!(text2 = PlExpandVars(text, NULL, NULL)))
@ -1980,8 +1980,7 @@ pop_text_stack(l);
Atom at = Yap_NWCharsToAtom((wchar_t *)data, len PASS_REGS); Atom at = Yap_NWCharsToAtom((wchar_t *)data, len PASS_REGS);
pop_text_stack(l); pop_text_stack(l);
return Yap_unify(MkAtomTerm(at), ARG3); return Yap_unify(MkAtomTerm(at), ARG3);
case REG_DWORD: case REG_DWORD: {
{
DWORD *d = (DWORD *)data; DWORD *d = (DWORD *)data;
pop_text_stack(l); pop_text_stack(l);
return Yap_unify(MkIntegerTerm((Int)d[0]), ARG3); return Yap_unify(MkIntegerTerm((Int)d[0]), ARG3);

View File

@ -25,10 +25,10 @@ static char SccsId[] = "%W% %G%";
*/ */
#include "Yap.h" #include "Yap.h"
#include "YapEval.h"
#include "YapHeap.h" #include "YapHeap.h"
#include "YapText.h" #include "YapText.h"
#include "Yatom.h" #include "Yatom.h"
#include "YapEval.h"
#include "yapio.h" #include "yapio.h"
#include <stdlib.h> #include <stdlib.h>
#if HAVE_STDARG_H #if HAVE_STDARG_H
@ -678,8 +678,7 @@ static Int term_to_string(USES_REGS1) {
Term t2 = Deref(ARG2), rc = false, t1 = Deref(ARG1); Term t2 = Deref(ARG2), rc = false, t1 = Deref(ARG1);
const char *s; const char *s;
if (IsVarTerm(t2)) { if (IsVarTerm(t2)) {
size_t length; s = Yap_TermToString(ARG1,LOCAL_encoding,
s = Yap_TermToString(ARG1, &length, LOCAL_encoding,
Quote_illegal_f | Handle_vars_f); Quote_illegal_f | Handle_vars_f);
if (!s || !MkStringTerm(s)) { if (!s || !MkStringTerm(s)) {
Yap_Error(RESOURCE_ERROR_HEAP, t1, Yap_Error(RESOURCE_ERROR_HEAP, t1,
@ -700,8 +699,7 @@ static Int term_to_atom(USES_REGS1) {
Term t2 = Deref(ARG2), ctl, rc = false; Term t2 = Deref(ARG2), ctl, rc = false;
Atom at; Atom at;
if (IsVarTerm(t2)) { if (IsVarTerm(t2)) {
size_t length; const char *s = Yap_TermToString(Deref(ARG1), LOCAL_encoding,
const char *s = Yap_TermToString(Deref(ARG1), &length, LOCAL_encoding,
Quote_illegal_f | Handle_vars_f); Quote_illegal_f | Handle_vars_f);
if (!s || !(at = Yap_UTF8ToAtom((const unsigned char *)s))) { if (!s || !(at = Yap_UTF8ToAtom((const unsigned char *)s))) {
Yap_Error(RESOURCE_ERROR_HEAP, t2, Yap_Error(RESOURCE_ERROR_HEAP, t2,
@ -716,8 +714,7 @@ static Int term_to_atom(USES_REGS1) {
at = AtomOfTerm(t2); at = AtomOfTerm(t2);
} }
ctl = TermNil; ctl = TermNil;
return (rc = Yap_BufferToTerm(RepAtom(at)->UStrOfAE, return ((rc = Yap_BufferToTerm(RepAtom(at)->UStrOfAE, ctl))) &&
strlen(RepAtom(at)->StrOfAE), ctl)) &&
Yap_unify(rc, ARG1); Yap_unify(rc, ARG1);
} }

View File

@ -8,10 +8,9 @@
* * * *
************************************************************************** **************************************************************************
* * * *
* File: yapio.h * * File: yapio.h * Last
* Last rev: 22/1/03 * *rev: 22/1/03 * mods:
* mods: * ** comments: Input/Output information *
* comments: Input/Output information *
* * * *
*************************************************************************/ *************************************************************************/
@ -78,8 +77,7 @@ extern int Yap_PlFGetchar(void);
extern int Yap_GetCharForSIGINT(void); extern int Yap_GetCharForSIGINT(void);
extern Int Yap_StreamToFileNo(Term); extern Int Yap_StreamToFileNo(Term);
extern int Yap_OpenStream(FILE *, char *, Term, int); extern int Yap_OpenStream(FILE *, char *, Term, int);
extern char *Yap_TermToString(Term t, size_t *length, encoding_t encoding, extern char *Yap_TermToString(Term t, encoding_t encoding, int flags);
int flags);
extern char *Yap_HandleToString(yhandle_t l, size_t sz, size_t *length, extern char *Yap_HandleToString(yhandle_t l, size_t sz, size_t *length,
encoding_t *encoding, int flags); encoding_t *encoding, int flags);
extern int Yap_GetFreeStreamD(void); extern int Yap_GetFreeStreamD(void);
@ -110,10 +108,10 @@ extern Term Yap_StringToNumberTerm(const char *s, encoding_t *encp,
extern int Yap_FormatFloat(Float f, char **s, size_t sz); extern int Yap_FormatFloat(Float f, char **s, size_t sz);
extern int Yap_open_buf_read_stream(const char *buf, size_t nchars, extern int Yap_open_buf_read_stream(const char *buf, size_t nchars,
encoding_t *encp, memBufSource src); encoding_t *encp, memBufSource src);
extern bool Yap_set_stream_to_buf(struct stream_desc *st, const char *buf, encoding_t enc, extern bool Yap_set_stream_to_buf(struct stream_desc *st, const char *buf,
size_t nchars); encoding_t enc, size_t nchars);
extern int Yap_open_buf_write_stream(encoding_t enc, memBufSource src); extern int Yap_open_buf_write_stream(encoding_t enc, memBufSource src);
extern Term Yap_BufferToTerm(const unsigned char *s, size_t sz, Term opts); extern Term Yap_BufferToTerm(const unsigned char *s, Term opts);
extern X_API Term Yap_BufferToTermWithPrioBindings(const unsigned char *s, extern X_API Term Yap_BufferToTermWithPrioBindings(const unsigned char *s,
size_t sz, Term opts, size_t sz, Term opts,
int prio, Term bindings); int prio, Term bindings);

File diff suppressed because one or more lines are too long

View File

@ -97,25 +97,29 @@ if any(a.startswith(('bdist', 'build', 'install')) for a in sys.argv):
argv = make_yap_kernel_cmd(executable='python') argv = make_yap_kernel_cmd(executable='python')
dest = os.path.join(here, 'resources') dest = os.path.join(here, 'resources')
if os.path.exists(dest): try:
shutil.rmtree(dest) shutil.rmtree(dest)
os.makedirs( dest )
shutil.copy2('${CMAKE_SOURCE_DIR}/docs/icons/yap_32x32x32.png',dest)
shutil.copy2('${CMAKE_SOURCE_DIR}/docs/icons/yap_64x64x32.png',dest)
write_kernel_spec(dest, overrides={'argv': argv}) write_kernel_spec(dest, overrides={'argv': argv})
shutil.copy('${CMAKE_CURRENT_SOURCE_DIR}/kernel.js',dest) except:
shutil.copy('${CMAKE_SOURCE_DIR}/misc/editors/prolog.js',dest) pass
shutil.copy('${CMAKE_SOURCE_DIR}/docs/icons/yap_32x32x32.png',os.path.join(dest,'logo-32x32.png')) # shutil.copy('${CMAKE_CURRENT_SOURCE_DIR}/kernel.js',dest)
shutil.copy('${CMAKE_SOURCE_DIR}/docs/icons/yap_64x64x32.png',os.path.join(dest,'logo-64x64.png')) # shutil.copy('${CMAKE_SOURCE_DIR}/misc/editors/prolog.js',dest)
setup_args['data_files'] = [ setup_args['data_files'] = [
(pjoin('share', 'jupyter', 'kernels', KERNEL_NAME), glob(pjoin(dest, '*'))), (pjoin('share', 'jupyter', 'kernels', KERNEL_NAME), glob(pjoin(dest, '*'))),
] ]
mode_loc = pjoin( sysconfig.get_path('platlib'), 'notebook', 'static', 'components', 'codemirror', 'mode', 'prolog') mode_loc = pjoin( sysconfig.get_path('platlib'), 'notebook', 'static', 'components', 'codemirror', 'mode', 'prolog')
custom_loc = pjoin( sysconfig.get_path('platlib'), 'notebook', 'static', 'custom') custom_loc = pjoin( sysconfig.get_path('platlib'), 'notebook', 'static', 'custom')
try: try:
shutil.copy( pjoin( custom_loc, "custom.js") , pjoin( custom_loc, "custom.js.orig"))
shutil.copy( "${CMAKE_CURRENT_SOURCE_DIR}/custom.js" , pjoin( custom_loc, "custom.js"))
if not os.path.exists(mode_loc):
os.makedirs(mode_loc) os.makedirs(mode_loc)
shutil.copy( "${CMAKE_SOURCE_DIR}/misc/editors/prolog.js" , mode_loc) shutil.copy( "${CMAKE_SOURCE_DIR}/misc/editors/prolog.js" , mode_loc)
except: except:
pass pass
shutil.copy( pjoin( custom_loc, "custom.js") , pjoin( custom_loc, "custom.js.orig"))
shutil.copy( "${CMAKE_CURRENT_SOURCE_DIR}/custom.js" , pjoin( custom_loc, "custom.js"))
extras_require = setuptools_args['extras_require'] = { extras_require = setuptools_args['extras_require'] = {
'test:python_version=="2.7"': ['mock'], 'test:python_version=="2.7"': ['mock'],