fix bad handle recovery properly.

This commit is contained in:
Vitor Santos Costa 2014-05-15 01:11:26 +01:00
parent 7c8e2bbd16
commit 0aa91d7c00
2 changed files with 662 additions and 956 deletions

294
C/write.c
View File

@ -874,7 +874,6 @@ writeTerm(Term t, int p, int depth, int rinfixarg, struct write_globs *wglb, str
/* context priority */
{
<<<<<<< HEAD
CACHE_REGS
struct rewind_term nrwt;
nrwt.parent = rwt;
@ -937,108 +936,6 @@ writeTerm(Term t, int p, int depth, int rinfixarg, struct write_globs *wglb, str
Atom atom;
int op, lp, rp;
if (IsExtensionFunctor(functor)) {
switch((CELL)functor) {
case (CELL)FunctorDouble:
wrputf(FloatOfTerm(t),wglb);
return;
case (CELL)FunctorString:
write_string(StringOfTerm(t),wglb);
return;
case (CELL)FunctorAttVar:
write_var(RepAppl(t)+1, wglb, &nrwt);
return;
case (CELL)FunctorDBRef:
wrputref(RefOfTerm(t), wglb->Quote_illegal, wglb);
return;
case (CELL)FunctorLongInt:
wrputn(LongIntOfTerm(t),wglb);
return;
/* case (CELL)FunctorBigInt: */
default:
writebig(t, p, depth, rinfixarg, wglb, rwt);
return;
}
}
Arity = ArityOfFunctor(functor);
atom = NameOfFunctor(functor);
#ifdef SFUNC
if (Arity == SFArity) {
int argno = 1;
CELL *p = ArgsOfSFTerm(t);
putAtom(atom, wglb->Quote_illegal, wglb);
wropen_bracket(wglb, FALSE);
lastw = separator;
while (*p) {
Int sl = 0;
while (argno < *p) {
wrputc('_', wglb->stream), wrputc(',', wglb->stream);
++argno;
=======
CACHE_REGS
struct rewind_term nrwt;
nrwt.parent = rwt;
nrwt.u_sd.s.ptr = 0;
if (wglb->MaxDepth != 0 && depth > wglb->MaxDepth) {
putAtom(Atom3Dots, wglb->Quote_illegal, wglb);
return;
>>>>>>> 596768a56b69ad4170d5ca0f18dcc7a0f55316e9
}
if (EX)
return;
t = Deref(t);
if (IsVarTerm(t)) {
write_var((CELL *)t, wglb, &nrwt);
} else if (IsIntTerm(t)) {
wrputn((Int) IntOfTerm(t),wglb);
} else if (IsAtomTerm(t)) {
putAtom(AtomOfTerm(t), wglb->Quote_illegal, wglb);
} else if (IsPairTerm(t)) {
if (wglb->Ignore_ops) {
wrputs("'.'(",wglb->stream);
lastw = separator;
writeTerm(from_pointer(RepPair(t), &nrwt, wglb), 999, depth + 1, FALSE, wglb, &nrwt);
t = AbsPair(restore_from_write(&nrwt, wglb));
wrputs(",",wglb->stream);
writeTerm(from_pointer(RepPair(t)+1, &nrwt, wglb), 999, depth + 1, FALSE, wglb, &nrwt);
restore_from_write(&nrwt, wglb);
wrclose_bracket(wglb, TRUE);
return;
}
if (wglb->Use_portray) {
Term targs[1];
struct DB_TERM *old_EX = NULL;
Int sl = 0;
targs[0] = t;
Yap_PutValue(AtomPortray, MkAtomTerm(AtomNil));
if (EX) old_EX = EX;
sl = Yap_InitSlot(t PASS_REGS);
Yap_execute_goal(Yap_MkApplTerm(FunctorPortray, 1, targs), 0, 1);
t = Yap_GetFromSlot(sl PASS_REGS);
Yap_RecoverSlots(1 PASS_REGS);
if (old_EX != NULL) EX = old_EX;
if (Yap_GetValue(AtomPortray) == MkAtomTerm(AtomTrue))
return;
}
if (yap_flags[WRITE_QUOTED_STRING_FLAG] && IsCodesTerm(t)) {
putString(t, wglb);
} else {
wrputc('[', wglb->stream);
lastw = separator;
/* we assume t was already saved in the stack */
write_list(t, 0, depth, wglb, rwt);
wrputc(']', wglb->stream);
lastw = separator;
}
} else { /* compound term */
Functor functor = FunctorOfTerm(t);
int Arity;
Atom atom;
int op, lp, rp;
if (IsExtensionFunctor(functor)) {
switch((CELL)functor) {
case (CELL)FunctorDouble:
@ -1091,7 +988,6 @@ writeTerm(Term t, int p, int depth, int rinfixarg, struct write_globs *wglb, str
return;
}
#endif
<<<<<<< HEAD
if (wglb->Use_portray) {
Term targs[1];
struct DB_TERM *old_EX = NULL;
@ -1200,195 +1096,6 @@ writeTerm(Term t, int p, int depth, int rinfixarg, struct write_globs *wglb, str
!IsVarTerm(tright) && IsAtomTerm(tright) &&
Yap_IsOp(AtomOfTerm(tright));
if (op > p) {
/* avoid stuff such as \+ (a,b) being written as \+(a,b) */
wropen_bracket(wglb, TRUE);
lastw = separator;
}
if (bracket_left) {
wropen_bracket(wglb, TRUE);
}
writeTerm(from_pointer(RepAppl(t)+1, &nrwt, wglb), lp, depth + 1, rinfixarg, wglb, &nrwt);
t = AbsAppl(restore_from_write(&nrwt, wglb)-1);
if (bracket_left) {
wrclose_bracket(wglb, TRUE);
}
/* avoid quoting commas and bars */
if (!strcmp(RepAtom(atom)->StrOfAE,",")) {
wrputc(',', wglb->stream);
lastw = separator;
} else if (!strcmp(RepAtom(atom)->StrOfAE,"|")) {
wrputc('|', wglb->stream);
lastw = separator;
} else
putAtom(atom, wglb->Quote_illegal, wglb);
if (bracket_right) {
wropen_bracket(wglb, TRUE);
}
writeTerm(from_pointer(RepAppl(t)+2, &nrwt, wglb), rp, depth + 1, TRUE, wglb, &nrwt);
restore_from_write(&nrwt, wglb);
if (bracket_right) {
wrclose_bracket(wglb, TRUE);
}
if (op > p) {
wrclose_bracket(wglb, TRUE);
}
} else if (wglb->Handle_vars && functor == LOCAL_FunctorVar) {
Term ti = ArgOfTerm(1, t);
if (lastw == alphanum) {
wrputc(' ', wglb->stream);
}
if (!IsVarTerm(ti) && (IsIntTerm(ti) || IsCodesTerm(ti) || IsAtomTerm(ti))) {
if (IsIntTerm(ti)) {
Int k = IntOfTerm(ti);
if (k == -1) {
wrputc('_', wglb->stream);
lastw = alphanum;
return;
} else {
wrputc((k % 26) + 'A', wglb->stream);
if (k >= 26) {
/* make sure we don't get confused about our context */
lastw = separator;
wrputn( k / 26 ,wglb);
} else
lastw = alphanum;
}
} else if (IsAtomTerm(ti)) {
putAtom(AtomOfTerm(ti), FALSE, wglb);
} else {
putUnquotedString(ti, wglb);
}
} else {
wrputs("'$VAR'(",wglb->stream);
lastw = separator;
writeTerm(from_pointer(RepAppl(t)+1, &nrwt, wglb), 999, depth + 1, FALSE, wglb, &nrwt);
restore_from_write(&nrwt, wglb);
wrclose_bracket(wglb, TRUE);
}
} else if (!wglb->Ignore_ops && functor == FunctorBraces) {
wrputc('{', wglb->stream);
lastw = separator;
writeTerm(from_pointer(RepAppl(t)+1, &nrwt, wglb), 1200, depth + 1, FALSE, wglb, &nrwt);
restore_from_write(&nrwt, wglb);
wrputc('}', wglb->stream);
lastw = separator;
} else if (atom == AtomArray) {
wrputc('{', wglb->stream);
lastw = separator;
for (op = 1; op <= Arity; ++op) {
if (op == wglb->MaxArgs) {
wrputs("...", wglb->stream);
break;
=======
if (wglb->Use_portray) {
Term targs[1];
struct DB_TERM *old_EX = NULL;
Int sl = 0;
targs[0] = t;
Yap_PutValue(AtomPortray, MkAtomTerm(AtomNil));
if (EX) old_EX = EX;
sl = Yap_InitSlot(t PASS_REGS);
Yap_execute_goal(Yap_MkApplTerm(FunctorPortray, 1, targs),0, 1);
t = Yap_GetFromSlot(sl PASS_REGS);
Yap_RecoverSlots(1 PASS_REGS);
if (old_EX) EX = old_EX;
if (Yap_GetValue(AtomPortray) == MkAtomTerm(AtomTrue) || EX)
return;
}
if (!wglb->Ignore_ops &&
Arity == 1 &&
Yap_IsPrefixOp(atom, &op, &rp)
) {
Term tright = ArgOfTerm(1, t);
int bracket_right =
!IsVarTerm(tright) && IsAtomTerm(tright) &&
Yap_IsOp(AtomOfTerm(tright));
if (op > p) {
wropen_bracket(wglb, TRUE);
}
putAtom(atom, wglb->Quote_illegal, wglb);
if (bracket_right) {
/* avoid stuff such as \+ (a,b) being written as \+(a,b) */
wropen_bracket(wglb, TRUE);
} else if (atom == AtomMinus) {
last_minus = TRUE;
}
writeTerm(from_pointer(RepAppl(t)+1, &nrwt, wglb), rp, depth + 1, TRUE, wglb, &nrwt);
restore_from_write(&nrwt, wglb);
if (bracket_right) {
wrclose_bracket(wglb, TRUE);
}
if (op > p) {
wrclose_bracket(wglb, TRUE);
}
} else if (!wglb->Ignore_ops &&
( Arity == 1 || ((atom == AtomEmptyBrackets || atom == AtomEmptyCurlyBrackets || atom == AtomEmptySquareBrackets) && !IsVarTerm(ArgOfTerm(1, t)))) &&
Yap_IsPosfixOp(atom, &op, &lp)) {
Term tleft = ArgOfTerm(1, t);
int bracket_left, offset;
if (Arity != 1) {
tleft = ArgOfTerm(1, t);
offset = 2;
} else {
tleft = ArgOfTerm(1, t);
offset = 1;
}
bracket_left =
!IsVarTerm(tleft) &&
IsAtomTerm(tleft) &&
Yap_IsOp(AtomOfTerm(tleft));
if (op > p) {
/* avoid stuff such as \+ (a,b) being written as \+(a,b) */
wropen_bracket(wglb, TRUE);
}
if (bracket_left) {
wropen_bracket(wglb, TRUE);
}
writeTerm(from_pointer(RepAppl(t)+offset, &nrwt, wglb), lp, depth + 1, rinfixarg, wglb, &nrwt);
restore_from_write(&nrwt, wglb);
if (bracket_left) {
wrclose_bracket(wglb, TRUE);
}
if (Arity > 1) {
if (atom == AtomEmptyBrackets) {
wrputc('(', wglb->stream);
} else if (atom == AtomEmptySquareBrackets) {
wrputc('[', wglb->stream);
} else if (atom == AtomEmptyCurlyBrackets) {
wrputc('{', wglb->stream);
}
lastw = separator;
write_list(ArgOfTerm(1,t), 0, depth, wglb, rwt);
if (atom == AtomEmptyBrackets) {
wrputc(')', wglb->stream);
} else if (atom == AtomEmptySquareBrackets) {
wrputc(']', wglb->stream);
} else if (atom == AtomEmptyCurlyBrackets) {
wrputc('}', wglb->stream);
}
lastw = separator;
} else {
putAtom(atom, wglb->Quote_illegal, wglb);
}
if (op > p) {
wrclose_bracket(wglb, TRUE);
}
} else if (!wglb->Ignore_ops &&
Arity == 2 && Yap_IsInfixOp(atom, &op, &lp,
&rp) ) {
Term tleft = ArgOfTerm(1, t);
Term tright = ArgOfTerm(2, t);
int bracket_left =
!IsVarTerm(tleft) && IsAtomTerm(tleft) &&
Yap_IsOp(AtomOfTerm(tleft));
int bracket_right =
!IsVarTerm(tright) && IsAtomTerm(tright) &&
Yap_IsOp(AtomOfTerm(tright));
if (op > p) {
/* avoid stuff such as \+ (a,b) being written as \+(a,b) */
wropen_bracket(wglb, TRUE);
@ -1499,7 +1206,6 @@ writeTerm(Term t, int p, int depth, int rinfixarg, struct write_globs *wglb, str
}
wrclose_bracket(wglb, TRUE);
}
>>>>>>> 596768a56b69ad4170d5ca0f18dcc7a0f55316e9
}
}

View File

@ -146,7 +146,7 @@ Yap_RecoverSlots(int n, Int topSlot USES_REGS)
if (ASP+1 != LCL0+topSlot)
return FALSE;
ASP += n;
ASP[old_slots+(n-old_slots+1)] = ASP[0] = MkIntTerm(old_slots-n);
ASP[old_slots+(n-1)] = ASP[0] = MkIntTerm(old_slots-n);
return TRUE;
}