From 697f7c650c29bf7c341bc340d27df809b1ec1bf4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADtor=20Santos=20Costa?= Date: Fri, 22 Apr 2011 15:29:41 +0100 Subject: [PATCH] I/O fixes. --- C/iopreds.c | 26 ++++--------------------- C/write.c | 39 ++++++++++++++++++++++++++++++++++--- packages/PLStream/pl-read.c | 6 ++++-- packages/PLStream/pl-yap.c | 20 +++++++++++++++++++ 4 files changed, 64 insertions(+), 27 deletions(-) diff --git a/C/iopreds.c b/C/iopreds.c index a04ef602b..8baceb7e4 100644 --- a/C/iopreds.c +++ b/C/iopreds.c @@ -90,7 +90,6 @@ STATIC_PROTO (Int p_read, ( USES_REGS1 )); STATIC_PROTO (Int p_startline, ( USES_REGS1 )); STATIC_PROTO (Int p_change_type_of_char, ( USES_REGS1 )); STATIC_PROTO (Int p_type_of_char, ( USES_REGS1 )); -STATIC_PROTO (Term StreamPosition, (IOSTREAM *)); extern Atom Yap_FileName(IOSTREAM *s); @@ -559,9 +558,9 @@ static Int int emacs_cares = FALSE; #endif Term tmod = Deref(ARG3), OCurrentModule = CurrentModule, tpos; - extern void Yap_setCurrentSourceLocation(IOSTREAM *s); + extern void Yap_setCurrentSourceLocation(IOSTREAM **s); - Yap_setCurrentSourceLocation(inp_stream); + Yap_setCurrentSourceLocation(&inp_stream); if (IsVarTerm(tmod)) { tmod = CurrentModule; } else if (!IsAtomTerm(tmod)) { @@ -569,7 +568,7 @@ static Int return FALSE; } Yap_Error_TYPE = YAP_NO_ERROR; - tpos = StreamPosition(inp_stream); + tpos = Yap_StreamPosition(inp_stream); if (!Yap_unify(tpos,ARG5)) { /* do this early so that we do not have to protect it in case of stack expansion */ return FALSE; @@ -587,7 +586,7 @@ static Int while (TRUE) { old_H = H; Yap_eot_before_eof = FALSE; - tpos = StreamPosition(inp_stream); + tpos = Yap_StreamPosition(inp_stream); tokstart = Yap_tokptr = Yap_toktide = Yap_tokenizer(inp_stream, &tpos); if (Yap_Error_TYPE != YAP_NO_ERROR && seekable) { H = old_H; @@ -759,23 +758,6 @@ p_read2 ( USES_REGS1 ) } -static Term -StreamPosition(IOSTREAM *st) -{ - Term t[4]; - t[0] = MkIntegerTerm(st->posbuf.charno); - t[1] = MkIntegerTerm(st->posbuf.lineno); - t[2] = MkIntegerTerm(st->posbuf.linepos); - t[3] = MkIntegerTerm(st->posbuf.byteno); - return Yap_MkApplTerm(FunctorStreamPos,4,t); -} - -Term -Yap_StreamPosition(IOSTREAM *st) -{ - return StreamPosition(st); -} - #if HAVE_SELECT && FALSE /* stream_select(+Streams,+TimeOut,-Result) */ static Int diff --git a/C/write.c b/C/write.c index dae203e2a..62137bdb2 100755 --- a/C/write.c +++ b/C/write.c @@ -309,13 +309,14 @@ legalAtom(unsigned char *s) /* Is this a legal atom ? */ return (s[1] == ']' && !s[2]); } else if (ch == '{') { return (s[1] == '}' && !s[2]); -// else if (ch == '/') -// return (*++s != '*'); } else if (Yap_chtype[ch] == SL) { return (!s[1]); } else if ((ch == ',' || ch == '.') && !s[1]) { return FALSE; } else { + if (ch == '/') { + if (s[1] == '*') return FALSE; + } while (ch) { if (Yap_chtype[ch] != SY) { return FALSE; @@ -451,7 +452,7 @@ putAtom(Atom atom, int Quote_illegal, wrf writewch) /* writes an atom */ if (lastw == atom_or_symbol && atom_or_symbol != separator /* solo */) wrputc(' ', writewch); lastw = atom_or_symbol; - if (!legalAtom(s) && Quote_illegal) { + if (Quote_illegal && !legalAtom(s)) { wrputc('\'', writewch); while (*s) { wchar_t ch = *s++; @@ -738,6 +739,38 @@ writeTerm(Term t, int p, int depth, int rinfixarg, struct write_globs *wglb, str } else if (IsAtomTerm(t)) { putAtom(AtomOfTerm(t), wglb->Quote_illegal, wglb->writewch); } else if (IsPairTerm(t)) { + if (wglb->Ignore_ops) { + Int sl = 0; + + wrputs("'.'(",wglb->writewch); + lastw = separator; + if (wglb->keep_terms) { + /* garbage collection may be called */ + sl = Yap_InitSlot(t); + } + writeTerm(HeadOfTerm(t), 999, depth + 1, FALSE, wglb, &nrwt); + restore_from_write(&nrwt, wglb); + if (wglb->keep_terms) { + /* garbage collection may be called */ + t = Yap_GetFromSlot(sl); + Yap_RecoverSlots(1); + } + wrputs(",",wglb->writewch); + if (wglb->keep_terms) { + /* garbage collection may be called */ + sl = Yap_InitSlot(t); + } + writeTerm(TailOfTerm(t), 999, depth + 1, FALSE, wglb, &nrwt); + restore_from_write(&nrwt, wglb); + if (wglb->keep_terms) { + /* garbage collection may be called */ + t = Yap_GetFromSlot(sl); + Yap_RecoverSlots(1); + } + wrputc(')', wglb->writewch); + lastw = separator; + return; + } if (wglb->Use_portray) { Term targs[1]; struct DB_TERM *old_EX = NULL; diff --git a/packages/PLStream/pl-read.c b/packages/PLStream/pl-read.c index 4f2654f9f..705b4c53f 100644 --- a/packages/PLStream/pl-read.c +++ b/packages/PLStream/pl-read.c @@ -344,10 +344,12 @@ setCurrentSourceLocation(IOSTREAM *s ARG_LD) #if __YAP_PROLOG__ void -Yap_setCurrentSourceLocation(IOSTREAM *s) +Yap_setCurrentSourceLocation(IOSTREAM **s) { GET_LD - setCurrentSourceLocation(s PASS_LD); + if (!*s) + *s = Suser_input; + setCurrentSourceLocation(*s PASS_LD); } #endif diff --git a/packages/PLStream/pl-yap.c b/packages/PLStream/pl-yap.c index 940ddb862..60dfa6bdc 100755 --- a/packages/PLStream/pl-yap.c +++ b/packages/PLStream/pl-yap.c @@ -1096,6 +1096,26 @@ pl_readline(term_t flag) } +static Term +StreamPosition(IOSTREAM *st) +{ + Term t[4]; + if (!st) + st = Suser_input; + t[0] = MkIntegerTerm(st->posbuf.charno); + t[1] = MkIntegerTerm(st->posbuf.lineno); + t[2] = MkIntegerTerm(st->posbuf.linepos); + t[3] = MkIntegerTerm(st->posbuf.byteno); + return Yap_MkApplTerm(FunctorStreamPos,4,t); +} + +Term +Yap_StreamPosition(IOSTREAM *st) +{ + return StreamPosition(st); +} + + #if THREADS #define COUNT_MUTEX_INITIALIZER(name) \