IO patches

simplify error handling
use get and inject
use wide support in OS
be stricter in checkin streams and arguments
This commit is contained in:
Vítor Santos Costa 2015-10-08 02:23:45 +01:00
parent b788dc131d
commit b3cc23ce64
30 changed files with 1224 additions and 1381 deletions

View File

@ -427,7 +427,7 @@ atom_chars( USES_REGS1 )
if (LOCAL_Error_TYPE && Yap_HandleError( "atom_chars/2" )) { if (LOCAL_Error_TYPE && Yap_HandleError( "atom_chars/2" )) {
goto restart_aux; goto restart_aux;
} }
return FALSE; return false;
} }
static Int static Int

View File

@ -1143,7 +1143,7 @@ setInitialValue( bool bootstrap, flag_func f, const char *s,flag_term *tarr )
Term t0; Term t0;
if (bootstrap) { return false; } if (bootstrap) { return false; }
CACHE_REGS CACHE_REGS
t0 = Yap_StringToTerm(s, strlen(s)+1, LOCAL_encoding, 1200, NULL); t0 = Yap_StringToTerm(s, strlen(s)+1, &LOCAL_encoding, 1200, NULL);
if (!t0) if (!t0)
return false; return false;
if (IsAtomTerm(t0) || IsIntTerm(t0)) { if (IsAtomTerm(t0) || IsIntTerm(t0)) {

1582
C/stack.c

File diff suppressed because it is too large Load Diff

View File

@ -22,12 +22,6 @@
#include "yapio.h" #include "yapio.h"
#include "YapText.h" #include "YapText.h"
#if defined(__BIG_ENDIAN__)
#define ENC_WCHAR ENC_ISO_UTF32_BE
#else
#define ENC_WCHAR ENC_ISO_UTF32_LE
#endif
#include <string.h> #include <string.h>
#include <wchar.h> #include <wchar.h>
@ -39,9 +33,9 @@ min_size(size_t i, size_t j) {
#define wcsnlen(S, N) min_size(N, wcslen(S)) #define wcsnlen(S, N) min_size(N, wcslen(S))
#endif #endif
static inline unsigned char *get_char(unsigned char *p, int *c) { *c = *p; return p+1; } static inline unsigned char *getChar(unsigned char *p, int *c) { *c = *p; return p+1; }
static inline wchar_t *get_wchar(wchar_t *p, int *c) { *c = *p; return p+1; } static inline wchar_t *getWchar(wchar_t *p, int *c) { *c = *p; return p+1; }
#ifndef NAN #ifndef NAN
#define NAN (0.0/0.0) #define NAN (0.0/0.0)
@ -366,19 +360,31 @@ Yap_readText( void *buf, seq_tv_t *inp, encoding_t *enc, int *minimal, size_t *l
bool wide; bool wide;
/* we know what the term is */ /* we know what the term is */
if (inp->type & YAP_STRING_STRING && !IsVarTerm(inp->val.t) && IsStringTerm(inp->val.t)) { const char *s; if ( !(inp->type & (YAP_STRING_CHARS|YAP_STRING_WCHARS)))
s = (char *)StringOfTerm( inp->val.t ); {
if ( s == NULL ) { if (IsVarTerm(inp->val.t) && !(inp->type & YAP_STRING_TERM)) {
return 0L; LOCAL_Error_TYPE = INSTANTIATION_ERROR;
} else if (!IsAtomTerm(inp->val.t) && inp->type == YAP_STRING_ATOM) {
LOCAL_Error_TYPE = TYPE_ERROR_ATOM;
} else if (!IsStringTerm(inp->val.t) && inp->type == YAP_STRING_STRING) {
LOCAL_Error_TYPE = TYPE_ERROR_STRING;
} else if (!IsNumTerm(inp->val.t) && (inp->type & ( YAP_STRING_INT|YAP_STRING_FLOAT| YAP_STRING_BIG)) == inp->type) {
LOCAL_Error_TYPE = TYPE_ERROR_NUMBER;
} else if (!IsAtomicTerm(inp->val.t) && !(inp->type & YAP_STRING_TERM)) {
LOCAL_Error_TYPE = TYPE_ERROR_ATOMIC;
}
LOCAL_Error_Term = inp->val.t;
} }
// this is a term, extract the UTF8 representation // this is a term, extract the UTF8 representation
if ( IsStringTerm(inp->val.t)) {
*enc = ENC_ISO_UTF8; *enc = ENC_ISO_UTF8;
*minimal = FALSE; *minimal = FALSE;
if (lengp) if (lengp)
*lengp = strlen(s); *lengp = strlen(s);
return (void *)s; return (void *)s;
} }
if (inp->type & YAP_STRING_ATOM && !IsVarTerm(inp->val.t) && IsAtomTerm(inp->val.t)) { if ( IsAtomTerm(inp->val.t)) {
// this is a term, extract to a buffer, and representation is wide // this is a term, extract to a buffer, and representation is wide
*minimal = TRUE; *minimal = TRUE;
Atom at = AtomOfTerm(inp->val.t); Atom at = AtomOfTerm(inp->val.t);
@ -394,13 +400,13 @@ Yap_readText( void *buf, seq_tv_t *inp, encoding_t *enc, int *minimal, size_t *l
return s; return s;
} }
} }
if (inp->type & YAP_STRING_CODES && !IsVarTerm(inp->val.t) && (s = Yap_ListOfCodesToBuffer( buf, inp->val.t, inp, &wide, lengp PASS_REGS))) { if (inp->type & YAP_STRING_CODES && (s = Yap_ListOfCodesToBuffer( buf, inp->val.t, inp, &wide, lengp 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
*minimal = TRUE; *minimal = TRUE;
*enc = ( wide ? ENC_WCHAR : ENC_ISO_LATIN1 ); *enc = ( wide ? ENC_WCHAR : ENC_ISO_LATIN1 );
return s; return s;
} }
if (inp->type & YAP_STRING_ATOMS && !IsVarTerm(inp->val.t) && (s = Yap_ListOfAtomsToBuffer( buf, inp->val.t, inp, &wide, lengp PASS_REGS))) { if (inp->type & YAP_STRING_ATOMS && (s = Yap_ListOfAtomsToBuffer( buf, inp->val.t, inp, &wide, lengp 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
*minimal = TRUE; *minimal = TRUE;
s = Yap_ListOfAtomsToBuffer( buf, inp->val.t, inp, &wide, lengp PASS_REGS); s = Yap_ListOfAtomsToBuffer( buf, inp->val.t, inp, &wide, lengp PASS_REGS);
@ -449,7 +455,8 @@ Yap_readText( void *buf, seq_tv_t *inp, encoding_t *enc, int *minimal, size_t *l
if (buf) s = buf; if (buf) s = buf;
else s = Yap_PreAllocCodeSpace(); else s = Yap_PreAllocCodeSpace();
size_t sz = LOCAL_MAX_SIZE-1; size_t sz = LOCAL_MAX_SIZE-1;
o = Yap_TermToString(inp->val.t, s, sz, lengp, ENC_ISO_UTF8, 0); encoding_t enc = ENC_ISO_UTF8;
o = Yap_TermToString(inp->val.t, s, sz, lengp, &enc, 0);
return s; return s;
} }
if (inp->type & YAP_STRING_CHARS) { if (inp->type & YAP_STRING_CHARS) {
@ -513,7 +520,7 @@ write_strings( void *s0, seq_tv_t *out, encoding_t enc, int minimal, size_t leng
LOCAL_TERM_ERROR( t, 2*(lim-s) ); LOCAL_TERM_ERROR( t, 2*(lim-s) );
buf = buf_from_tstring(HR); buf = buf_from_tstring(HR);
while (cp < lim) { while (cp < lim) {
cp = get_char(cp, &chr); cp = getChar(cp, &chr);
buf += put_utf8(buf, chr); buf += put_utf8(buf, chr);
} }
if (max >= min) *buf++ = '\0'; if (max >= min) *buf++ = '\0';
@ -535,7 +542,7 @@ write_strings( void *s0, seq_tv_t *out, encoding_t enc, int minimal, size_t leng
buf = buf_from_tstring(HR); buf = buf_from_tstring(HR);
while (wp < lim) { while (wp < lim) {
utf8proc_int32_t chr; utf8proc_int32_t chr;
wp = get_wchar(wp, &chr); wp = getWchar(wp, &chr);
buf += put_utf8(buf, chr); buf += put_utf8(buf, chr);
} }
if (max >= min) *buf++ = '\0'; if (max >= min) *buf++ = '\0';
@ -599,7 +606,7 @@ write_atoms( void *s0, seq_tv_t *out, encoding_t enc, int minimal, size_t leng U
LOCAL_TERM_ERROR( t, 2*(lim-s) ); LOCAL_TERM_ERROR( t, 2*(lim-s) );
while (cp < lim) { while (cp < lim) {
utf8proc_int32_t chr; utf8proc_int32_t chr;
cp = get_char(cp, &chr); cp = getChar(cp, &chr);
if (chr == '\0') break; if (chr == '\0') break;
w[0] = chr; w[0] = chr;
HR[0] = MkAtomTerm(Yap_LookupAtom(w)); HR[0] = MkAtomTerm(Yap_LookupAtom(w));
@ -619,7 +626,7 @@ write_atoms( void *s0, seq_tv_t *out, encoding_t enc, int minimal, size_t leng U
LOCAL_TERM_ERROR( t, 2*(lim-s) ); LOCAL_TERM_ERROR( t, 2*(lim-s) );
while (*cp && cp < lim) { while (*cp && cp < lim) {
utf8proc_int32_t chr; utf8proc_int32_t chr;
cp = get_wchar(cp, &chr); cp = getWchar(cp, &chr);
if (chr == '\0') break; if (chr == '\0') break;
w[0] = chr; w[0] = chr;
HR[0] = MkAtomTerm(Yap_LookupMaybeWideAtom(w)); HR[0] = MkAtomTerm(Yap_LookupMaybeWideAtom(w));
@ -679,7 +686,7 @@ write_codes( void *s0, seq_tv_t *out, encoding_t enc, int minimal, size_t leng U
LOCAL_TERM_ERROR( t, 2*(lim-s) ); LOCAL_TERM_ERROR( t, 2*(lim-s) );
while (cp < lim) { while (cp < lim) {
utf8proc_int32_t chr; utf8proc_int32_t chr;
cp = get_char(cp, &chr); cp = getChar(cp, &chr);
HR[0] = MkIntTerm(chr); HR[0] = MkIntTerm(chr);
HR[1] = AbsPair(HR+2); HR[1] = AbsPair(HR+2);
HR += 2; HR += 2;
@ -695,7 +702,7 @@ write_codes( void *s0, seq_tv_t *out, encoding_t enc, int minimal, size_t leng U
LOCAL_TERM_ERROR( t, 2*(lim-s) ); LOCAL_TERM_ERROR( t, 2*(lim-s) );
while (cp < lim) { while (cp < lim) {
utf8proc_int32_t chr; utf8proc_int32_t chr;
cp = get_wchar(cp, &chr); cp = getWchar(cp, &chr);
HR[0] = MkIntTerm(chr); HR[0] = MkIntTerm(chr);
HR[1] = AbsPair(HR+2); HR[1] = AbsPair(HR+2);
HR += 2; HR += 2;
@ -1049,13 +1056,13 @@ write_length( void *s0, seq_tv_t *out, encoding_t enc, int minimal, size_t leng
static Term static Term
write_number( void *s0, seq_tv_t *out, encoding_t enc, int minimal, int size USES_REGS) write_number( void *s0, seq_tv_t *out, encoding_t enc, int minimal, int size USES_REGS)
{ {
return Yap_StringToNumberTerm(s0, enc); return Yap_StringToNumberTerm(s0, &enc);
} }
static Term static Term
string_to_term( void *s0, seq_tv_t *out, encoding_t enc, int minimal, size_t leng USES_REGS) string_to_term( void *s0, seq_tv_t *out, encoding_t enc, int minimal, size_t leng USES_REGS)
{ {
return Yap_StringToTerm(s0, strlen(s0)+1, enc, 1200, NULL); return Yap_StringToTerm(s0, strlen(s0)+1, &enc, 1200, NULL);
} }

View File

@ -388,7 +388,7 @@ int Yap_FormatFloat(Float f, char **s, size_t sz) {
int sno; int sno;
char *so; char *so;
sno = Yap_open_buf_write_stream(*s, sz, GLOBAL_Stream[LOCAL_c_output_stream].encoding, 0); sno = Yap_open_buf_write_stream(*s, sz, &GLOBAL_Stream[LOCAL_c_output_stream].encoding, 0);
if (sno < 0) if (sno < 0)
return FALSE; return FALSE;
wglb.stream = GLOBAL_Stream+sno; wglb.stream = GLOBAL_Stream+sno;
@ -1251,7 +1251,7 @@ void Yap_plwrite(Term t, StreamDesc *mywrite, int max_depth, int flags, int prio
char * char *
Yap_TermToString(Term t, char *s, size_t sz, size_t *length, encoding_t encp, int flags) Yap_TermToString(Term t, char *s, size_t sz, size_t *length, encoding_t *encp, int flags)
{ {
CACHE_REGS CACHE_REGS
int sno = Yap_open_buf_write_stream(s, sz, encp, flags); int sno = Yap_open_buf_write_stream(s, sz, encp, flags);
@ -1259,10 +1259,11 @@ Yap_TermToString(Term t, char *s, size_t sz, size_t *length, encoding_t encp, i
if (sno < 0) if (sno < 0)
return NULL; return NULL;
LOCK(GLOBAL_Stream[sno].streamlock);
LOCAL_c_output_stream = sno; LOCAL_c_output_stream = sno;
if (encp) if (encp)
GLOBAL_Stream[sno].encoding = encp; GLOBAL_Stream[sno].encoding = *encp;
else
GLOBAL_Stream[sno].encoding = LOCAL_encoding;
Yap_plwrite (t, GLOBAL_Stream+sno, 0, flags, 1200); Yap_plwrite (t, GLOBAL_Stream+sno, 0, flags, 1200);
s = Yap_MemExportStreamPtr( sno ); s = Yap_MemExportStreamPtr( sno );
Yap_CloseStream( sno ); Yap_CloseStream( sno );

View File

@ -384,7 +384,7 @@ Yap_AtomSWIToString(Term t0 USES_REGS)
seq_tv_t inp, out; seq_tv_t inp, out;
inp.val.t = t0; inp.val.t = t0;
inp.type = YAP_STRING_ATOM|YAP_STRING_STRING|YAP_STRING_INT|YAP_STRING_FLOAT|YAP_STRING_BIG|YAP_STRING_ATOMS_CODES|YAP_STRING_TERM; inp.type = YAP_STRING_ATOM|YAP_STRING_STRING|YAP_STRING_INT|YAP_STRING_FLOAT|YAP_STRING_BIG|YAP_STRING_ATOMS_CODES;
out.type = YAP_STRING_STRING; out.type = YAP_STRING_STRING;
if (!Yap_CVT_Text(&inp, &out PASS_REGS)) if (!Yap_CVT_Text(&inp, &out PASS_REGS))

View File

@ -102,8 +102,6 @@
#define DBLOAD_MODULE Yap_heap_regs->dbload_module #define DBLOAD_MODULE Yap_heap_regs->dbload_module
#define RANGE_MODULE Yap_heap_regs->range_module #define RANGE_MODULE Yap_heap_regs->range_module
#define HIDDEN_PREDICATES Yap_heap_regs->hidden_predicates
#define CurrentModules Yap_heap_regs->current_modules #define CurrentModules Yap_heap_regs->current_modules
@ -111,6 +109,8 @@
#define HIDDEN_PREDICATES Yap_heap_regs->hidden_predicates
#define GLOBAL_Flags Yap_heap_regs->GLOBAL_Flags_ #define GLOBAL_Flags Yap_heap_regs->GLOBAL_Flags_

View File

@ -102,8 +102,6 @@
Term dbload_module; Term dbload_module;
Term range_module; Term range_module;
Prop hidden_predicates;
struct mod_entry *current_modules; struct mod_entry *current_modules;
@ -111,6 +109,8 @@
Prop hidden_predicates;
union flagTerm* GLOBAL_Flags_; union flagTerm* GLOBAL_Flags_;

View File

@ -102,8 +102,6 @@
DBLOAD_MODULE = MkAtomTerm(AtomDBLoad); DBLOAD_MODULE = MkAtomTerm(AtomDBLoad);
RANGE_MODULE = MkAtomTerm(AtomRange); RANGE_MODULE = MkAtomTerm(AtomRange);
HIDDEN_PREDICATES = NULL;
CurrentModules = NULL; CurrentModules = NULL;
@ -111,6 +109,8 @@
Yap_InitModules(); Yap_InitModules();
HIDDEN_PREDICATES = NULL;
Yap_InitPlIO(); Yap_InitPlIO();
GLOBAL_Flags = 0; GLOBAL_Flags = 0;

View File

@ -21,7 +21,7 @@ static void InitWorker(int wid) {
REMOTE_FormatInfo(wid) = NULL; REMOTE_FormatInfo(wid) = NULL;
REMOTE_AtPrompt(wid) = AtomNil; REMOTE_AtPrompt(wid) = AtomNil;
REMOTE_encoding(wid) = Yap_InitialEncoding(); REMOTE_encoding(wid) = Yap_DefaultEncoding();
REMOTE_quasi_quotations(wid) = false; REMOTE_quasi_quotations(wid) = false;
REMOTE_default_priority(wid) = 1200; REMOTE_default_priority(wid) = 1200;
REMOTE_eot_before_eof(wid) = false; REMOTE_eot_before_eof(wid) = false;

View File

@ -102,8 +102,6 @@
DBLOAD_MODULE = AtomTermAdjust(DBLOAD_MODULE); DBLOAD_MODULE = AtomTermAdjust(DBLOAD_MODULE);
RANGE_MODULE = AtomTermAdjust(RANGE_MODULE); RANGE_MODULE = AtomTermAdjust(RANGE_MODULE);
RestoreHiddenPredicates();
CurrentModules = ModEntryPtrAdjust(CurrentModules); CurrentModules = ModEntryPtrAdjust(CurrentModules);
@ -111,6 +109,8 @@
RestoreHiddenPredicates();

View File

@ -96,6 +96,8 @@ typedef bool YAP_Bool;
#define YAP_agc_hook Agc_hook #define YAP_agc_hook Agc_hook
#define YAP_encoding_t encoding_t
#else #else
/* Type definitions */ /* Type definitions */
@ -146,6 +148,10 @@ typedef int (*YAP_agc_hook)(void *_Atom);
#include "YapError.h" #include "YapError.h"
#include <../os/encoding.h>
typedef encoding_t YAP_encoding_t;
#endif #endif
typedef struct YAP_thread_attr_struct { typedef struct YAP_thread_attr_struct {
@ -368,21 +374,6 @@ typedef enum stream_f {
typedef uint64_t stream_flags_t; typedef uint64_t stream_flags_t;
/********* encoding ***********************/
typedef enum
{ PL_ENC_UNKNOWN = 0, /* invalid/unknown */
PL_ENC_OCTET, /* raw 8 bit input */
PL_ENC_ASCII, /* US-ASCII (0..127) */
PL_ENC_ISO_LATIN_1, /* ISO Latin-1 (0..256) */
PL_ENC_ANSI, /* default (multibyte) codepage */
PL_ENC_UTF8,
PL_ENC_UNICODE_BE, /* big endian unicode file */
PL_ENC_UNICODE_LE, /* little endian unicode file */
PL_ENC_WCHAR /* pl_wchar_t */
} PL_IOENC;
/********* YAP C-Flags ***********************/ /********* YAP C-Flags ***********************/
typedef enum typedef enum

View File

@ -46,6 +46,7 @@ BEGIN_ERRORS()
E(DOMAIN_ERROR_STREAM_POSITION, DOMAIN_ERROR, "stream_position") E(DOMAIN_ERROR_STREAM_POSITION, DOMAIN_ERROR, "stream_position")
E(DOMAIN_ERROR_TIMEOUT_SPEC, DOMAIN_ERROR, "timeout_spec") E(DOMAIN_ERROR_TIMEOUT_SPEC, DOMAIN_ERROR, "timeout_spec")
E(DOMAIN_ERROR_SYNTAX_ERROR_HANDLER, DOMAIN_ERROR, "syntax_error_handler") E(DOMAIN_ERROR_SYNTAX_ERROR_HANDLER, DOMAIN_ERROR, "syntax_error_handler")
E(DOMAIN_ERROR_WRITE_OPTION, DOMAIN_ERROR, "write_option")
E(EVALUATION_ERROR_FLOAT_OVERFLOW, EVALUATION_ERROR, "float_overflow") E(EVALUATION_ERROR_FLOAT_OVERFLOW, EVALUATION_ERROR, "float_overflow")
E(EVALUATION_ERROR_FLOAT_UNDERFLOW, EVALUATION_ERROR, "float_underflow") E(EVALUATION_ERROR_FLOAT_UNDERFLOW, EVALUATION_ERROR, "float_underflow")

View File

@ -1868,7 +1868,7 @@ extern X_API void YAP_Error(int myerrno, YAP_Term t, const char *buf, ...);
extern X_API int YAP_WriteBuffer(YAP_Term,char *,size_t,int); extern X_API int YAP_WriteBuffer(YAP_Term,char *,size_t,int);
extern X_API char* YAP_WriteDynamicBuffer(YAP_Term t,char *buf,size_t sze, size_t *lengthp, int *encp, int flags); extern X_API char* YAP_WriteDynamicBuffer(YAP_Term t,char *buf,size_t sze, size_t *lengthp, YAP_encoding_t *encp, int flags);
/* void YAP_Term(YAP_Term) */ /* void YAP_Term(YAP_Term) */
extern X_API YAP_Term YAP_CopyTerm(YAP_Term); extern X_API YAP_Term YAP_CopyTerm(YAP_Term);

View File

@ -1530,7 +1530,7 @@ atom_to_term(term_t atom, term_t term, term_t bindings)
} }
Term Term
Yap_StringToTerm(const char *s, size_t len, term_t bindings) Yap_StringToTerm(const char *s, size_t *lenp, term_t bindings)
{ GET_LD; { GET_LD;
read_data rd; read_data rd;
int rval; int rval;

View File

@ -105,9 +105,6 @@ Term swi_module SWI_MODULE MkAT AtomSwi
Term dbload_module DBLOAD_MODULE MkAT AtomDBLoad Term dbload_module DBLOAD_MODULE MkAT AtomDBLoad
Term range_module RANGE_MODULE MkAT AtomRange Term range_module RANGE_MODULE MkAT AtomRange
// hidden predicates
Prop hidden_predicates HIDDEN_PREDICATES =NULL RestoreHiddenPredicates()
// //
// Module list // Module list
// //
@ -116,6 +113,9 @@ struct mod_entry *current_modules CurrentModules =NULL ModEntryPtrAdjust
// don't actually want to define a field // don't actually want to define a field
void void void Yap_InitModules() void void void void Yap_InitModules() void
// hidden predicates
Prop hidden_predicates HIDDEN_PREDICATES =NULL RestoreHiddenPredicates()
// make sure we have the streams set at this point. // make sure we have the streams set at this point.
// don't actually want to define a field // don't actually want to define a field
void void void Yap_InitPlIO() void void void void Yap_InitPlIO() void

View File

@ -25,7 +25,7 @@ struct format_status* FormatInfo =NULL
Atom AtPrompt =AtomNil Atom AtPrompt =AtomNil
char Prompt[MAX_PROMPT+1] void char Prompt[MAX_PROMPT+1] void
encoding_t encoding =Yap_InitialEncoding() encoding_t encoding =Yap_DefaultEncoding()
bool quasi_quotations =false bool quasi_quotations =false
UInt default_priority =1200 UInt default_priority =1200

View File

@ -71,8 +71,10 @@ INLINE_ONLY inline EXTERN Term
MkCharTerm (Int c) MkCharTerm (Int c)
{ {
wchar_t cs[2]; wchar_t cs[2];
if (c < 0)
return MkAtomTerm(AtomEof);
cs[0] = c; cs[0] = c;
cs[0] = '\0'; cs[1] = '\0';
return MkAtomTerm(Yap_LookupMaybeWideAtom(cs)); return MkAtomTerm(Yap_LookupMaybeWideAtom(cs));
} }
@ -94,22 +96,75 @@ CharOfAtom (Atom at)
} }
} }
static int
plUnGetc( int sno, int ch )
{
return ungetc(ch, GLOBAL_Stream[sno].file);
}
static Int dopeek( int sno )
{
Int ocharcount, olinecount, olinepos;
StreamDesc *s;
Int ch;
s = GLOBAL_Stream+sno;
ocharcount = s->charcount;
olinecount = s->linecount;
olinepos = s->linepos;
ch = get_wchar(sno);
s->charcount = ocharcount;
s->linecount = olinecount;
s->linepos = olinepos;
/* buffer the character */
if (s->encoding == LOCAL_encoding)) {
ungetwc( ch, s-> file );
} else {
/* do the ungetc as if a write .. */
int (*f)(int, int) = s->stream_putc;
s->stream_putc = plUnGetc;
put_wchar( ch, sno );
s->stream_putc = f;
}
return ch;
}
static Int dopeek_byte( int sno )
{
Int ocharcount, olinecount, olinepos;
StreamDesc *s;
Int ch;
s = GLOBAL_Stream+sno;
ocharcount = s->charcount;
olinecount = s->linecount;
olinepos = s->linepos;
ch = GLOBAL_Stream[sno].stream_getc(sno);
s->charcount = ocharcount;
s->linecount = olinecount;
s->linepos = olinepos;
/* buffer the character */
ungetc(ch, s->file);
return ch;
}
static Int static Int
at_end_of_stream ( USES_REGS1 ) at_end_of_stream ( USES_REGS1 )
{ /* at_end_of_stream */ { /* at_end_of_stream */
/* the next character is a EOF */ /* the next character is a EOF */
int sno = Yap_CheckStream (ARG1, Input_Stream_f, "past_eof/1"); int sno = Yap_CheckStream (ARG1, Input_Stream_f, NULL);
Int out; Int out;
if (sno < 0) if (sno < 0)
return (FALSE); return (FALSE);
if (GLOBAL_Stream[sno].stream_getc == PlUnGetc) {
UNLOCK(GLOBAL_Stream[sno].streamlock);
return FALSE;
}
out = GLOBAL_Stream[sno].status & Eof_Stream_f; out = GLOBAL_Stream[sno].status & Eof_Stream_f;
if (!out) {
if (GLOBAL_Stream[sno].status & Binary_Stream_f) {
out = ( dopeek_byte(sno) < 0 );
} else {
out = ( dopeek(sno) < 0 );
}
}
UNLOCK(GLOBAL_Stream[sno].streamlock); UNLOCK(GLOBAL_Stream[sno].streamlock);
return out; return out;
} }
@ -118,15 +173,13 @@ static Int
at_end_of_stream_0 ( USES_REGS1 ) at_end_of_stream_0 ( USES_REGS1 )
{ /* at_end_of_stream */ { /* at_end_of_stream */
/* the next character is a EOF */ /* the next character is a EOF */
int sno = LOCAL_c_input_stream;
Int out; Int out;
LOCK(GLOBAL_Stream[sno].streamlock); int sno = LOCAL_c_input_stream;
if (GLOBAL_Stream[sno].stream_getc == PlUnGetc) {
UNLOCK(GLOBAL_Stream[sno].streamlock);
return FALSE;
}
out = GLOBAL_Stream[sno].status & Eof_Stream_f; out = GLOBAL_Stream[sno].status & Eof_Stream_f;
if (!out) {
out = ( dopeek(sno) < 0 );
}
UNLOCK(GLOBAL_Stream[sno].streamlock); UNLOCK(GLOBAL_Stream[sno].streamlock);
return out; return out;
} }
@ -152,18 +205,13 @@ yap_fflush( sno)
static Int static Int
get ( USES_REGS1 ) get ( USES_REGS1 )
{ /* '$get'(Stream,-N) */ { /* '$get'(Stream,-N) */
int sno = Yap_CheckStream (ARG1, Input_Stream_f, "get/2"); int sno = Yap_CheckTextStream (ARG1, Input_Stream_f, "get/2");
int ch; int ch;
Int status; Int status;
if (sno < 0) if (sno < 0)
return FALSE; return FALSE;
status = GLOBAL_Stream[sno].status; status = GLOBAL_Stream[sno].status;
if (status & Binary_Stream_f) {
UNLOCK(GLOBAL_Stream[sno].streamlock);
Yap_Error(PERMISSION_ERROR_INPUT_BINARY_STREAM, ARG1, "get/2");
return FALSE;
}
while ((ch = GLOBAL_Stream[sno].stream_wgetc(sno)) <= 32 && ch >= 0) while ((ch = GLOBAL_Stream[sno].stream_wgetc(sno)) <= 32 && ch >= 0)
; ;
UNLOCK(GLOBAL_Stream[sno].streamlock); UNLOCK(GLOBAL_Stream[sno].streamlock);
@ -173,18 +221,13 @@ get ( USES_REGS1 )
static Int static Int
get_char ( USES_REGS1 ) get_char ( USES_REGS1 )
{ /* '$get'(Stream,-N) */ { /* '$get'(Stream,-N) */
int sno = Yap_CheckStream (ARG1, Input_Stream_f, "get/2"); int sno = Yap_CheckTextStream (ARG1, Input_Stream_f, "get/2");
int ch; int ch;
Int status; Int status;
if (sno < 0) if (sno < 0)
return FALSE; return FALSE;
status = GLOBAL_Stream[sno].status; status = GLOBAL_Stream[sno].status;
if (status & Binary_Stream_f) {
UNLOCK(GLOBAL_Stream[sno].streamlock);
Yap_Error(PERMISSION_ERROR_INPUT_BINARY_STREAM, ARG1, "get/2");
return FALSE;
}
ch = GLOBAL_Stream[sno].stream_wgetc(sno); ch = GLOBAL_Stream[sno].stream_wgetc(sno);
UNLOCK(GLOBAL_Stream[sno].streamlock); UNLOCK(GLOBAL_Stream[sno].streamlock);
return (Yap_unify_constant (ARG2, MkCharTerm (ch))); return (Yap_unify_constant (ARG2, MkCharTerm (ch)));
@ -193,18 +236,13 @@ get_char ( USES_REGS1 )
static Int static Int
get_code ( USES_REGS1 ) get_code ( USES_REGS1 )
{ /* get0(Stream,-N) */ { /* get0(Stream,-N) */
int sno = Yap_CheckStream (ARG1, Input_Stream_f, "get0/2"); int sno = Yap_CheckTextStream (ARG1, Input_Stream_f, "get0/2");
Int status; Int status;
Int out; Int out;
if (sno < 0) if (sno < 0)
return(FALSE); return(FALSE);
status = GLOBAL_Stream[sno].status; status = GLOBAL_Stream[sno].status;
if (status & Binary_Stream_f) {
UNLOCK(GLOBAL_Stream[sno].streamlock);
Yap_Error(PERMISSION_ERROR_INPUT_BINARY_STREAM, ARG1, "get0/2");
return FALSE;
}
out = GLOBAL_Stream[sno].stream_wgetc(sno); out = GLOBAL_Stream[sno].stream_wgetc(sno);
UNLOCK(GLOBAL_Stream[sno].streamlock); UNLOCK(GLOBAL_Stream[sno].streamlock);
return (Yap_unify_constant (ARG2, MkIntegerTerm (out)) ); return (Yap_unify_constant (ARG2, MkIntegerTerm (out)) );
@ -220,11 +258,6 @@ get_1 ( USES_REGS1 )
LOCK(GLOBAL_Stream[sno].streamlock); LOCK(GLOBAL_Stream[sno].streamlock);
status = GLOBAL_Stream[sno].status; status = GLOBAL_Stream[sno].status;
if (status & Binary_Stream_f) {
UNLOCK(GLOBAL_Stream[sno].streamlock);
Yap_Error(PERMISSION_ERROR_INPUT_BINARY_STREAM, ARG1, "get_code/1");
return FALSE;
}
while ((ch = GLOBAL_Stream[sno].stream_wgetc(sno)) <= 32 && ch >= 0) while ((ch = GLOBAL_Stream[sno].stream_wgetc(sno)) <= 32 && ch >= 0)
; ;
UNLOCK(GLOBAL_Stream[sno].streamlock); UNLOCK(GLOBAL_Stream[sno].streamlock);
@ -240,11 +273,6 @@ getcode_1 ( USES_REGS1 )
status = GLOBAL_Stream[sno].status; status = GLOBAL_Stream[sno].status;
LOCK(GLOBAL_Stream[sno].streamlock); LOCK(GLOBAL_Stream[sno].streamlock);
if (status & Binary_Stream_f) {
UNLOCK(GLOBAL_Stream[sno].streamlock);
Yap_Error(PERMISSION_ERROR_INPUT_BINARY_STREAM, ARG1, "get0/2");
return FALSE;
}
out = GLOBAL_Stream[sno].stream_wgetc(sno); out = GLOBAL_Stream[sno].stream_wgetc(sno);
UNLOCK(GLOBAL_Stream[sno].streamlock); UNLOCK(GLOBAL_Stream[sno].streamlock);
return (Yap_unify_constant (ARG1, MkIntegerTerm (out)) ); return (Yap_unify_constant (ARG1, MkIntegerTerm (out)) );
@ -259,11 +287,6 @@ getchar_1 ( USES_REGS1 )
LOCK(GLOBAL_Stream[sno].streamlock); LOCK(GLOBAL_Stream[sno].streamlock);
status = GLOBAL_Stream[sno].status; status = GLOBAL_Stream[sno].status;
if (status & Binary_Stream_f) {
UNLOCK(GLOBAL_Stream[sno].streamlock);
Yap_Error(PERMISSION_ERROR_INPUT_BINARY_STREAM, ARG1, "get0/2");
return FALSE;
}
out = GLOBAL_Stream[sno].stream_wgetc(sno); out = GLOBAL_Stream[sno].stream_wgetc(sno);
UNLOCK(GLOBAL_Stream[sno].streamlock); UNLOCK(GLOBAL_Stream[sno].streamlock);
return (Yap_unify_constant (ARG1, MkCharTerm (out)) ); return (Yap_unify_constant (ARG1, MkCharTerm (out)) );
@ -273,7 +296,7 @@ getchar_1 ( USES_REGS1 )
static Int static Int
get0_line_codes ( USES_REGS1 ) get0_line_codes ( USES_REGS1 )
{ /* '$get0'(Stream,-N) */ { /* '$get0'(Stream,-N) */
int sno = Yap_CheckStream (ARG1, Input_Stream_f, "get0/2"); int sno = Yap_CheckTextStream (ARG1, Input_Stream_f, "get0/2");
Int status; Int status;
Term out; Term out;
Int ch = '\0'; Int ch = '\0';
@ -281,18 +304,8 @@ get0_line_codes ( USES_REGS1 )
if (sno < 0) if (sno < 0)
return(FALSE); return(FALSE);
if (GLOBAL_Stream[sno].stream_getc == PlUnGetc) { rewind = FALSE;
ch = PlUnGetc(sno);
rewind = TRUE;
} else {
rewind = FALSE;
}
status = GLOBAL_Stream[sno].status; status = GLOBAL_Stream[sno].status;
if (status & Binary_Stream_f) {
UNLOCK(GLOBAL_Stream[sno].streamlock);
Yap_Error(PERMISSION_ERROR_INPUT_BINARY_STREAM, ARG1, "get0/2");
return FALSE;
}
out = read_line(sno); out = read_line(sno);
UNLOCK(GLOBAL_Stream[sno].streamlock); UNLOCK(GLOBAL_Stream[sno].streamlock);
if (rewind) if (rewind)
@ -311,8 +324,9 @@ get_byte ( USES_REGS1 )
if (sno < 0) if (sno < 0)
return(FALSE); return(FALSE);
status = GLOBAL_Stream[sno].status; status = GLOBAL_Stream[sno].status;
if (!(status & Binary_Stream_f) && if (!(status & Binary_Stream_f)
strictISOFlag()) { //&& strictISOFlag()
) {
UNLOCK(GLOBAL_Stream[sno].streamlock); UNLOCK(GLOBAL_Stream[sno].streamlock);
Yap_Error(PERMISSION_ERROR_INPUT_TEXT_STREAM, ARG1, "get_byte/2"); Yap_Error(PERMISSION_ERROR_INPUT_TEXT_STREAM, ARG1, "get_byte/2");
return(FALSE); return(FALSE);
@ -331,8 +345,9 @@ get_byte_1 ( USES_REGS1 )
LOCK(GLOBAL_Stream[sno].streamlock); LOCK(GLOBAL_Stream[sno].streamlock);
status = GLOBAL_Stream[sno].status; status = GLOBAL_Stream[sno].status;
if (!(status & Binary_Stream_f) && if (!(status & Binary_Stream_f)
strictISOFlag()) { // &&strictISOFlag()
) {
UNLOCK(GLOBAL_Stream[sno].streamlock); UNLOCK(GLOBAL_Stream[sno].streamlock);
Yap_Error(PERMISSION_ERROR_INPUT_TEXT_STREAM, ARG1, "get_byte/1"); Yap_Error(PERMISSION_ERROR_INPUT_TEXT_STREAM, ARG1, "get_byte/1");
return(FALSE); return(FALSE);
@ -359,11 +374,6 @@ put_code_1 ( USES_REGS1 )
return FALSE; return FALSE;
} }
LOCK(GLOBAL_Stream[sno].streamlock); LOCK(GLOBAL_Stream[sno].streamlock);
if (GLOBAL_Stream[sno].status & Binary_Stream_f) {
UNLOCK(GLOBAL_Stream[sno].streamlock);
Yap_Error(PERMISSION_ERROR_OUTPUT_BINARY_STREAM, ARG1, "put/2");
return(FALSE);
}
GLOBAL_Stream[sno].stream_wputc (sno, (int) IntegerOfTerm (Deref (ARG2))); GLOBAL_Stream[sno].stream_wputc (sno, (int) IntegerOfTerm (Deref (ARG2)));
/* /*
* if (!(GLOBAL_Stream[sno].status & Null_Stream_f)) * if (!(GLOBAL_Stream[sno].status & Null_Stream_f))
@ -390,7 +400,7 @@ put_code ( USES_REGS1 )
Yap_Error(DOMAIN_ERROR_OUT_OF_RANGE, t2, "put_code/1"); Yap_Error(DOMAIN_ERROR_OUT_OF_RANGE, t2, "put_code/1");
return FALSE; return FALSE;
} }
sno = Yap_CheckStream (ARG1, Output_Stream_f, "put/2"); sno = Yap_CheckTextStream (ARG1, Output_Stream_f, "put/2");
if (sno < 0) if (sno < 0)
return (FALSE); return (FALSE);
if (GLOBAL_Stream[sno].status & Binary_Stream_f) { if (GLOBAL_Stream[sno].status & Binary_Stream_f) {
@ -457,7 +467,7 @@ put_char ( USES_REGS1 )
Yap_Error(DOMAIN_ERROR_OUT_OF_RANGE, t2, "put_char/1"); Yap_Error(DOMAIN_ERROR_OUT_OF_RANGE, t2, "put_char/1");
return FALSE; return FALSE;
} }
sno = Yap_CheckStream (ARG1, Output_Stream_f, "put/2"); sno = Yap_CheckTextStream (ARG1, Output_Stream_f, "put/2");
if (sno < 0) if (sno < 0)
return (FALSE); return (FALSE);
if (GLOBAL_Stream[sno].status & Binary_Stream_f) { if (GLOBAL_Stream[sno].status & Binary_Stream_f) {
@ -524,7 +534,7 @@ tab ( USES_REGS1 )
Yap_Error(DOMAIN_ERROR_OUT_OF_RANGE, t2, "tab/1"); Yap_Error(DOMAIN_ERROR_OUT_OF_RANGE, t2, "tab/1");
return FALSE; return FALSE;
} }
sno = Yap_CheckStream (ARG1, Output_Stream_f, "nl/1"); sno = Yap_CheckTextStream (ARG1, Output_Stream_f, "nl/1");
if (sno < 0) if (sno < 0)
return (FALSE); return (FALSE);
@ -566,7 +576,7 @@ nl_1 ( USES_REGS1 )
static Int static Int
nl ( USES_REGS1 ) nl ( USES_REGS1 )
{ /* nl(Stream) */ { /* nl(Stream) */
int sno = Yap_CheckStream (ARG1, Output_Stream_f, "nl/1"); int sno = Yap_CheckTextStream (ARG1, Output_Stream_f, "nl/1");
if (sno < 0) if (sno < 0)
return (FALSE); return (FALSE);
if (GLOBAL_Stream[sno].status & Binary_Stream_f) { if (GLOBAL_Stream[sno].status & Binary_Stream_f) {
@ -589,10 +599,10 @@ put_byte ( USES_REGS1 )
Term t2; Term t2;
Int ch; Int ch;
if (IsVarTerm(t2 = Deref(ARG2))) { if (IsVarTerm(t2 = Deref(ARG2))) {
Yap_Error(INSTANTIATION_ERROR, t2, "put_code/1"); Yap_Error(INSTANTIATION_ERROR, t2, "put_code/2");
return FALSE; return FALSE;
} else if (!IsIntegerTerm (t2)) { } else if (!IsIntegerTerm (t2)) {
Yap_Error(TYPE_ERROR_INTEGER, t2, "put_code/1"); Yap_Error(TYPE_ERROR_BYTE, t2, "put_code/2");
return FALSE; return FALSE;
} else if ((ch = IntegerOfTerm (t2)) < -1) { } else if ((ch = IntegerOfTerm (t2)) < -1) {
Yap_Error(DOMAIN_ERROR_OUT_OF_RANGE, t2, "put_code/1"); Yap_Error(DOMAIN_ERROR_OUT_OF_RANGE, t2, "put_code/1");
@ -601,11 +611,12 @@ put_byte ( USES_REGS1 )
int sno = Yap_CheckStream (ARG1, Output_Stream_f, "put/2"); int sno = Yap_CheckStream (ARG1, Output_Stream_f, "put/2");
if (sno < 0) if (sno < 0)
return (FALSE); return (FALSE);
if (!(GLOBAL_Stream[sno].status & Binary_Stream_f) && if (!(GLOBAL_Stream[sno].status & Binary_Stream_f)
strictISOFlag()) { // && strictISOFlag()
) {
UNLOCK(GLOBAL_Stream[sno].streamlock); UNLOCK(GLOBAL_Stream[sno].streamlock);
Yap_Error(PERMISSION_ERROR_OUTPUT_TEXT_STREAM, ARG1, "get0/2"); Yap_Error(PERMISSION_ERROR_OUTPUT_TEXT_STREAM, ARG1, NULL);
return(FALSE); return false;
} }
GLOBAL_Stream[sno].stream_putc(sno, ch); GLOBAL_Stream[sno].stream_putc(sno, ch);
/* /*
@ -626,15 +637,16 @@ put_byte_1 ( USES_REGS1 )
Yap_Error(INSTANTIATION_ERROR, t2, "put_code/1"); Yap_Error(INSTANTIATION_ERROR, t2, "put_code/1");
return FALSE; return FALSE;
} else if (!IsIntegerTerm (t2)) { } else if (!IsIntegerTerm (t2)) {
Yap_Error(TYPE_ERROR_INTEGER, t2, "put_code/1"); Yap_Error(TYPE_ERROR_BYTE, t2, "put_code/1");
return FALSE; return FALSE;
} else if ((ch = IntegerOfTerm (t2)) < -1) { } else if ((ch = IntegerOfTerm (t2)) < -1) {
Yap_Error(DOMAIN_ERROR_OUT_OF_RANGE, t2, "put_code/1"); Yap_Error(DOMAIN_ERROR_OUT_OF_RANGE, t2, "put_code/1");
return FALSE; return FALSE;
} }
LOCK(GLOBAL_Stream[sno].streamlock); LOCK(GLOBAL_Stream[sno].streamlock);
if (!(GLOBAL_Stream[sno].status & Binary_Stream_f) && if (!(GLOBAL_Stream[sno].status & Binary_Stream_f)
strictISOFlag()) { //&& strictISOFlag()
) {
UNLOCK(GLOBAL_Stream[sno].streamlock); UNLOCK(GLOBAL_Stream[sno].streamlock);
Yap_Error(PERMISSION_ERROR_OUTPUT_TEXT_STREAM, ARG1, "get0/2"); Yap_Error(PERMISSION_ERROR_OUTPUT_TEXT_STREAM, ARG1, "get0/2");
return(FALSE); return(FALSE);
@ -663,7 +675,7 @@ skip_1 ( USES_REGS1 )
Yap_Error(DOMAIN_ERROR_OUT_OF_RANGE, t2, "skip/2"); Yap_Error(DOMAIN_ERROR_OUT_OF_RANGE, t2, "skip/2");
return FALSE; return FALSE;
} }
sno = Yap_CheckStream (ARG1, Input_Stream_f, "skip/2"); sno = Yap_CheckTextStream (ARG1, Input_Stream_f, "skip/2");
if (sno < 0) if (sno < 0)
return (FALSE); return (FALSE);
while ((ch = GLOBAL_Stream[sno].stream_wgetc(sno)) != n && ch != -1) while ((ch = GLOBAL_Stream[sno].stream_wgetc(sno)) != n && ch != -1)
@ -750,30 +762,6 @@ flush_all_streams ( USES_REGS1 )
return TRUE; return TRUE;
} }
static Int dopeek( int sno )
{
Int ocharcount, olinecount, olinepos;
StreamDesc *s;
Int ch;
s = GLOBAL_Stream+sno;
ocharcount = s->charcount;
olinecount = s->linecount;
olinepos = s->linepos;
ch = get_wchar(sno);
s->charcount = ocharcount;
s->linecount = olinecount;
s->linepos = olinepos;
/* buffer the character */
s->och = ch;
/* mark a special function to recover this character */
Yap_DefaultStreamOps( s );
s->stream_getc = PlUnGetc;
return ch;
}
/** @pred peek(+ _S_, - _C_) is deprecated /** @pred peek(+ _S_, - _C_) is deprecated
@ -797,7 +785,7 @@ static Int
peek_code ( USES_REGS1 ) peek_code ( USES_REGS1 )
{ /* at_end_of_stream */ { /* at_end_of_stream */
/* the next character is a EOF */ /* the next character is a EOF */
int sno = Yap_CheckStream (ARG1, Input_Stream_f, "peek/2"); int sno = Yap_CheckTextStream (ARG1, Input_Stream_f, "peek/2");
Int ch; Int ch;
if (sno < 0) if (sno < 0)
@ -807,9 +795,11 @@ peek_code ( USES_REGS1 )
PlIOError(PERMISSION_ERROR_INPUT_BINARY_STREAM, ARG1, "peek_code/2"); PlIOError(PERMISSION_ERROR_INPUT_BINARY_STREAM, ARG1, "peek_code/2");
return FALSE; return FALSE;
} }
if ((ch = dopeek( sno )) < 0) if ((ch = dopeek( sno )) < 0) {
return false;
UNLOCK(GLOBAL_Stream[sno].streamlock); UNLOCK(GLOBAL_Stream[sno].streamlock);
return false;
}
UNLOCK(GLOBAL_Stream[sno].streamlock);
return(Yap_unify_constant(ARG2,MkIntTerm(ch))); return(Yap_unify_constant(ARG2,MkIntTerm(ch)));
} }
@ -854,19 +844,21 @@ static Int
peek_byte ( USES_REGS1 ) peek_byte ( USES_REGS1 )
{ /* at_end_of_stream */ { /* at_end_of_stream */
/* the next character is a EOF */ /* the next character is a EOF */
int sno = Yap_CheckStream (ARG1, Input_Stream_f, "peek/2"); int sno = Yap_CheckStream (ARG1, Input_Stream_f, "peek_byte/2");
Int ch; Int ch;
if (sno < 0) if (sno < 0)
return(FALSE); return(FALSE);
if (!(GLOBAL_Stream[sno].status & Binary_Stream_f)) { if (!(GLOBAL_Stream[sno].status & Binary_Stream_f)) {
UNLOCK(GLOBAL_Stream[sno].streamlock); UNLOCK(GLOBAL_Stream[sno].streamlock);
Yap_Error(PERMISSION_ERROR_INPUT_TEXT_STREAM, ARG1, "peek_byte/2"); Yap_Error(PERMISSION_ERROR_INPUT_STREAM, ARG1, "peek_byte/2");
return(FALSE); return(FALSE);
} }
if ((ch = dopeek( sno )) < 0) if ((ch = dopeek_byte( sno )) < 0) {
return false;
UNLOCK(GLOBAL_Stream[sno].streamlock); UNLOCK(GLOBAL_Stream[sno].streamlock);
return false;
}
UNLOCK(GLOBAL_Stream[sno].streamlock);
return(Yap_unify_constant(ARG2,MkIntTerm(ch))); return(Yap_unify_constant(ARG2,MkIntTerm(ch)));
} }
@ -893,7 +885,7 @@ peek_byte_1 ( USES_REGS1 )
Yap_Error(PERMISSION_ERROR_INPUT_TEXT_STREAM, ARG1, "peek_byte/2"); Yap_Error(PERMISSION_ERROR_INPUT_TEXT_STREAM, ARG1, "peek_byte/2");
return(FALSE); return(FALSE);
} }
if ((ch = dopeek( sno )) < 0) { if ((ch = dopeek_byte( sno )) < 0) {
UNLOCK(GLOBAL_Stream[sno].streamlock); UNLOCK(GLOBAL_Stream[sno].streamlock);
return false; return false;
} }
@ -913,7 +905,7 @@ static Int
peek_char ( USES_REGS1 ) peek_char ( USES_REGS1 )
{ {
/* the next character is a EOF */ /* the next character is a EOF */
int sno = Yap_CheckStream (ARG1, Input_Stream_f, "peek/2"); int sno = Yap_CheckTextStream (ARG1, Input_Stream_f, "peek/2");
wchar_t wsinp[2]; wchar_t wsinp[2];
Int ch; Int ch;
@ -945,7 +937,7 @@ static Int
peek_char_1 ( USES_REGS1 ) peek_char_1 ( USES_REGS1 )
{ {
/* the next character is a EOF */ /* the next character is a EOF */
int sno = LOCAL_c_input_stream; int sno = LOCAL_c_input_stream;
wchar_t wsinp[2]; wchar_t wsinp[2];
Int ch; Int ch;

View File

@ -1,4 +1,3 @@
/************************************************************************* /*************************************************************************
* * * *
* YAP Prolog * * YAP Prolog *
@ -54,14 +53,18 @@ static char SccsId[] = "%W% %G%";
static Int p_change_type_of_char(USES_REGS1); static Int p_change_type_of_char(USES_REGS1);
static Int p_type_of_char(USES_REGS1); static Int p_type_of_char(USES_REGS1);
Term Yap_StringToNumberTerm(char *s, encoding_t enc) { Term Yap_StringToNumberTerm(char *s, encoding_t *encp) {
CACHE_REGS
int sno; int sno;
Term t; Term t;
sno = Yap_open_buf_read_stream(s, strlen(s), enc, MEM_BUF_USER); sno = Yap_open_buf_read_stream(s, strlen(s), encp, MEM_BUF_USER);
if (sno < 0) if (sno < 0)
return FALSE; return FALSE;
GLOBAL_Stream[sno].encoding = enc; if (encp)
GLOBAL_Stream[sno].encoding = *encp;
else
GLOBAL_Stream[sno].encoding = LOCAL_encoding;
UNLOCK(GLOBAL_Stream[sno].streamlock); UNLOCK(GLOBAL_Stream[sno].streamlock);
while (*s && isblank(*s++)) while (*s && isblank(*s++))
; ;
@ -162,25 +165,6 @@ void Yap_SetDefaultEncoding(encoding_t new_encoding) {
LOCAL_encoding = new_encoding; LOCAL_encoding = new_encoding;
} }
encoding_t Yap_InitialEncoding(void) {
char *s = getenv("LANG");
size_t sz;
/* if we don't have a LANG then just use ISO_LATIN1 */
if (s == NULL)
s = getenv("LC_CTYPE");
if (s == NULL)
return ENC_ISO_UTF8;
sz = strlen(s);
if (sz >= 5) {
if (s[sz - 5] == 'U' && s[sz - 4] == 'T' && s[sz - 3] == 'F' &&
s[sz - 2] == '-' && s[sz - 1] == '8') {
return ENC_ISO_UTF8;
}
}
return ENC_ISO_UTF8;
}
static Int get_default_encoding(USES_REGS1) { static Int get_default_encoding(USES_REGS1) {
Term out = MkIntegerTerm(Yap_DefaultEncoding()); Term out = MkIntegerTerm(Yap_DefaultEncoding());
return Yap_unify(ARG1, out); return Yap_unify(ARG1, out);

View File

@ -19,9 +19,6 @@
#define ENCODING_H 1 #define ENCODING_H 1
#include "Yap.h"
typedef enum { typedef enum {
ENC_OCTET = 0, /// binary files ENC_OCTET = 0, /// binary files
ENC_ISO_LATIN1 = 1, /// US+West Europe ENC_ISO_LATIN1 = 1, /// US+West Europe
@ -33,12 +30,56 @@ typedef enum {
ENC_ISO_UTF32_BE = 64, /// nobody ENC_ISO_UTF32_BE = 64, /// nobody
ENC_ISO_UTF32_LE = 128, /// yes, nobody ENC_ISO_UTF32_LE = 128, /// yes, nobody
} encoding_t; } encoding_t;
/// read the initial encoding from the Operating System's environment;
encoding_t Yap_InitialEncoding( void ); #if defined(__BIG_ENDIAN__)
#define ENC_WCHAR ENC_ISO_UTF32_BE
#else
#define ENC_WCHAR ENC_ISO_UTF32_LE
#endif
#ifdef YAP_H
/// read the current environment, as set by the user or as Initial /// read the current environment, as set by the user or as Initial
encoding_t Yap_DefaultEncoding( void ); encoding_t Yap_DefaultEncoding( void );
void Yap_SetDefaultEncoding(encoding_t new_encoding); void Yap_SetDefaultEncoding(encoding_t new_encoding);
#if HAVE_XLOCALE_H
typedef enum {
SEQ_ENC_OCTET, /// binary files
SEQ_ENC_ISO_LATIN1, /// US+West Europe
SEQ_ENC_ISO_ASCII , /// US only
SEQ_ENC_ISO_ANSI , /// Who cares
SEQ_ENC_ISO_UTF8 , /// Most everyone nowadays
SEQ_ENC_UTF16_BE, /// People who made a mistake
SEQ_ENC_UTF16_LE, /// People who made the same mistake
v\ SEQ_ENC_ISO_UTF32_BE, /// nobody
SEQ_ENC_ISO_UTF32_LE /// yes, nobody
} seq_encoding_t;
/// convert from unary to binary representation.
static inline seq_encoding_t seq_encoding(encoding_t inp) {
#if HAVE__BUILTIN_FFSLL
return __builtin_ffsll(inp);
#elif HAVE_FFSLL
return ffsll(inp);
#else
unsigned int out;
// supports max 16 different encodings.
if (inp==0)
return 0L;
// if (inp & ((CELL)0xffffL << 16)) {inp >>= 16; out += 16;}
if (inp & ((CELL)0xffL << 8)) {inp >>= 8; out += 8;}
if (inp & ((CELL)0xfL << 4)) {inp >>= 4; out += 4;}
if (inp & ((CELL)0x3L << 2)) {inp >>= 2; out += 2;}
if (inp & ((CELL)0x1 << 1)) out++;
#endif
return out;
}
extern xlocale enc_locales[SEQ_ENC_ISO_UTF32_LE+1];
#endif
static inline const char *enc_name(encoding_t enc) static inline const char *enc_name(encoding_t enc)
{ {
switch(enc) switch(enc)
@ -59,23 +100,24 @@ static inline
encoding_t enc_id(char *s) encoding_t enc_id(char *s)
{ {
{ {
if (!strcmp(s, "octet")) return ENC_OCTET; if (!strcmp(s, "iso_utf8")) return ENC_ISO_UTF8;
if (!strcmp(s, "utf16_be")) return ENC_UTF16_BE;
if (!strcmp(s, "utf16_le")) return ENC_UTF16_LE;
if (!strcmp(s, "octet")) return ENC_OCTET;
if (!strcmp(s, "iso_latin_1")) return ENC_ISO_LATIN1; if (!strcmp(s, "iso_latin_1")) return ENC_ISO_LATIN1;
if (!strcmp(s, "iso_ascii")) return ENC_ISO_ASCII; if (!strcmp(s, "iso_ascii")) return ENC_ISO_ASCII;
if (!strcmp(s, "iso_ansi")) return ENC_ISO_ANSI; if (!strcmp(s, "iso_ansi")) return ENC_ISO_ANSI;
if (!strcmp(s, "iso_utf8")) return ENC_ISO_UTF8;
if (!strcmp(s, "utf16_be")) return ENC_UTF16_BE;
if (!strcmp(s, "utf16_le")) return ENC_UTF16_LE;
if (!strcmp(s, "utf32_be")) return ENC_ISO_UTF32_BE; if (!strcmp(s, "utf32_be")) return ENC_ISO_UTF32_BE;
if (!strcmp(s, "utf32_le")) return ENC_ISO_UTF32_LE; if (!strcmp(s, "utf32_le")) return ENC_ISO_UTF32_LE;
if (!strcmp(s, "default")) return Yap_DefaultEncoding(); if (!strcmp(s, "default")) return Yap_DefaultEncoding();
else { else {
Yap_Error(DOMAIN_ERROR_OUT_OF_RANGE, 0, "bad encoding %s", s); Yap_Error(DOMAIN_ERROR_OUT_OF_RANGE, 0, "bad encoding %s", s);
return ENC_OCTET; return ENC_OCTET;
} }
} }
} }
#endif
#endif #endif

View File

@ -73,7 +73,7 @@ static char SccsId[] = "%W% %G%";
#endif #endif
#ifdef _WIN32 #ifdef _WIN32
#if HAVE_IO_H #if HAVE_IO_H
/* Windows */ /* Windows */
#include <io.h> #include <io.h>
#endif #endif
#endif #endif

View File

@ -829,7 +829,7 @@ doformat(volatile Term otail, volatile Term oargs, int sno USES_REGS)
finfo.pad_entries[finfo.padders].pad = (char *)bufp; finfo.pad_entries[finfo.padders].pad = (char *)bufp;
bufp = NULL; bufp = NULL;
sz = 0; sz = 0;
nsno = Yap_open_buf_write_stream((char *)bufp, sz, GLOBAL_Stream[sno].encoding, 0); nsno = Yap_open_buf_write_stream((char *)bufp, sz, &GLOBAL_Stream[sno].encoding, 0);
if (osno) { if (osno) {
GLOBAL_Stream[nsno].linepos = GLOBAL_Stream[sno].linepos; GLOBAL_Stream[nsno].linepos = GLOBAL_Stream[sno].linepos;
GLOBAL_Stream[nsno].linecount = GLOBAL_Stream[sno].linecount; GLOBAL_Stream[nsno].linecount = GLOBAL_Stream[sno].linecount;

View File

@ -158,7 +158,6 @@ Yap_DefaultStreamOps( StreamDesc * st)
{ {
st->stream_wputc = put_wchar; st->stream_wputc = put_wchar;
st->stream_wgetc = get_wchar; st->stream_wgetc = get_wchar;
st->stream_gets = DefaultGets;
if (GLOBAL_CharConversionTable != NULL) if (GLOBAL_CharConversionTable != NULL)
st->stream_wgetc_for_read = ISOWGetc; st->stream_wgetc_for_read = ISOWGetc;
else else
@ -363,7 +362,7 @@ Int
PlIOError__ (const char *file, const char *function, int lineno, yap_error_number type, Term culprit, ...) PlIOError__ (const char *file, const char *function, int lineno, yap_error_number type, Term culprit, ...)
{ {
if (trueLocalPrologFlag(FILEERRORS_FLAG) == MkIntTerm(1) || if (trueLocalPrologFlag(FILEERRORS_FLAG) == TermTrue||
type == RESOURCE_ERROR_MAX_STREAMS /* do not catch resource errors */) { type == RESOURCE_ERROR_MAX_STREAMS /* do not catch resource errors */) {
va_list args; va_list args;
const char *format; const char *format;
@ -469,9 +468,15 @@ Yap_DebugPuts( FILE *s, const char *sch)
{ {
if (Yap_Option['l' - 96]) if (Yap_Option['l' - 96])
(void) fputs(sch, Yap_logfile); (void) fputs(sch, Yap_logfile);
return (fputs(sch, s)); return fputs( sch, s);
} }
void Yap_DebugErrorPuts(const char *s)
{
Yap_DebugPuts (stderr, s);
}
void void
Yap_DebugPlWrite(Term t) Yap_DebugPlWrite(Term t)
{ {
@ -485,12 +490,6 @@ Yap_DebugErrorPutc(int c)
Yap_DebugPutc (stderr, c); Yap_DebugPutc (stderr, c);
} }
void
Yap_DebugErrorPuts(const char *s)
{
Yap_DebugPuts (stderr, s);
}
void Yap_DebugWriteIndicator( PredEntry *ap ) void Yap_DebugWriteIndicator( PredEntry *ap )
{ {
CACHE_REGS CACHE_REGS
@ -616,6 +615,23 @@ ResetEOF(StreamDesc *s) {
} }
/* handle reading from a stream after having found an EOF */ /* handle reading from a stream after having found an EOF */
static int
EOFWGetc(int sno)
{
register StreamDesc *s = &GLOBAL_Stream[sno];
if (s->status & Push_Eof_Stream_f) {
/* ok, we have pushed an EOF, send it away */
s->status &= ~Push_Eof_Stream_f;
return EOF;
}
if (ResetEOF(s)) {
s->stream_wgetc = get_wchar;
return(s->stream_wgetc(sno));
}
return EOF;
}
static int static int
EOFGetc(int sno) EOFGetc(int sno)
{ {
@ -627,6 +643,7 @@ EOFGetc(int sno)
return EOF; return EOF;
} }
if (ResetEOF(s)) { if (ResetEOF(s)) {
s->stream_getc = PlGetc;
return(s->stream_getc(sno)); return(s->stream_getc(sno));
} }
return EOF; return EOF;
@ -638,7 +655,6 @@ console_post_process_eof(StreamDesc *s)
{ {
CACHE_REGS CACHE_REGS
s->stream_getc = EOFGetc; s->stream_getc = EOFGetc;
Yap_DefaultStreamOps( s );
LOCAL_newline = FALSE; LOCAL_newline = FALSE;
return EOFCHAR; return EOFCHAR;
} }
@ -665,10 +681,18 @@ post_process_eof(StreamDesc *s)
{ {
s->status |= Eof_Stream_f; s->status |= Eof_Stream_f;
s->stream_getc = EOFGetc; s->stream_getc = EOFGetc;
Yap_DefaultStreamOps( s );
return EOFCHAR; return EOFCHAR;
} }
int
post_process_weof(StreamDesc *s)
{
s->status |= Eof_Stream_f;
s->stream_wgetc = EOFWGetc;
return EOFCHAR;
}
/* standard routine, it should read from anything pointed by a FILE *. /* standard routine, it should read from anything pointed by a FILE *.
It could be made more efficient by doing our own buffering and avoiding It could be made more efficient by doing our own buffering and avoiding
post_process_read_char, something to think about */ post_process_read_char, something to think about */
@ -741,168 +765,6 @@ GetUTF8 (int sno)
return c; return c;
} }
/* reads a character from a buffer and does the rest */
int
PlUnGetc (int sno)
{
register StreamDesc *s = &GLOBAL_Stream[sno];
Int ch;
if (s->stream_getc != PlUnGetc)
return(s->stream_getc(sno));
ch = s->och;
if (s->status & InMemory_Stream_f) {
Yap_MemOps( s );
s->stream_wputc = put_wchar;
} else if (s->status & Socket_Stream_f) {
Yap_SocketOps( s );
s->stream_wputc = put_wchar;
} else if (s->status & Promptable_Stream_f) {
Yap_ConsoleOps( s );
s->stream_wputc = put_wchar;
} else {
s->stream_getc = PlGetc;
s->stream_gets = PlGetsFunc();
}
return(ch);
}
/* give back 0376+ch */
static int
PlUnGetc376 (int sno)
{
register StreamDesc *s = &GLOBAL_Stream[sno];
Int ch;
if (s->stream_getc != PlUnGetc376)
return(s->stream_getc(sno));
s->stream_getc = PlUnGetc;
ch = s->och;
s->och = 0xFE;
return ch;
}
/* give back 0376+ch */
static int
PlUnGetc00 (int sno)
{
register StreamDesc *s = &GLOBAL_Stream[sno];
Int ch;
if (s->stream_getc != PlUnGetc00)
return(s->stream_getc(sno));
s->stream_getc = PlUnGetc;
ch = s->och;
s->och = 0x00;
return ch;
}
/* give back 0377+ch */
static int
PlUnGetc377 (int sno)
{
register StreamDesc *s = &GLOBAL_Stream[sno];
Int ch;
if (s->stream_getc != PlUnGetc377)
return(s->stream_getc(sno));
s->stream_getc = PlUnGetc;
ch = s->och;
s->och = 0xFF;
return ch;
}
/* give back 0357+ch */
static int
PlUnGetc357 (int sno)
{
register StreamDesc *s = &GLOBAL_Stream[sno];
Int ch;
if (s->stream_getc != PlUnGetc357)
return(s->stream_getc(sno));
s->stream_getc = PlUnGetc;
ch = s->och;
s->och = 0xEF;
return ch;
}
/* give back 0357+0273+ch */
static int
PlUnGetc357273 (int sno)
{
register StreamDesc *s = &GLOBAL_Stream[sno];
Int ch;
if (s->stream_getc != PlUnGetc357273)
return(s->stream_getc(sno));
s->stream_getc = PlUnGetc357;
ch = s->och;
s->och = 0xBB;
return ch;
}
/* give back 000+000+ch */
static int
PlUnGetc0000 (int sno)
{
register StreamDesc *s = &GLOBAL_Stream[sno];
Int ch;
if (s->stream_getc != PlUnGetc0000)
return(s->stream_getc(sno));
s->stream_getc = PlUnGetc00;
ch = s->och;
s->och = 0x00;
return ch;
}
/* give back 000+000+ch */
static int
PlUnGetc0000fe (int sno)
{
register StreamDesc *s = &GLOBAL_Stream[sno];
Int ch;
if (s->stream_getc != PlUnGetc0000fe)
return(s->stream_getc(sno));
s->stream_getc = PlUnGetc0000;
ch = s->och;
s->och = 0xfe;
return ch;
}
/* give back 0377+0376+ch */
static int
PlUnGetc377376 (int sno)
{
register StreamDesc *s = &GLOBAL_Stream[sno];
Int ch;
if (s->stream_getc != PlUnGetc377376)
return(s->stream_getc(sno));
s->stream_getc = PlUnGetc377;
ch = s->och;
s->och = 0xFE;
return ch;
}
/* give back 0377+0376+000+ch */
static int
PlUnGetc37737600 (int sno)
{
register StreamDesc *s = &GLOBAL_Stream[sno];
Int ch;
if (s->stream_getc != PlUnGetc37737600)
return(s->stream_getc(sno));
s->stream_getc = PlUnGetc377376;
ch = s->och;
s->och = 0x00;
return ch;
}
static int static int
utf8_nof(char ch) utf8_nof(char ch)
{ {
@ -917,20 +779,21 @@ utf8_nof(char ch)
return 5; return 5;
} }
int static int
get_wchar(int sno) get_wchar__(int sno)
{ {
int ch; int ch;
wchar_t wch; wchar_t wch;
int how_many = 0; int how_many = 0;
StreamDesc *s = GLOBAL_Stream+sno;
while (TRUE) { while (TRUE) {
ch = GLOBAL_Stream[sno].stream_getc(sno); ch = getc(GLOBAL_Stream[sno].file);
if (ch == -1) { if (ch == -1) {
if (how_many) { if (how_many) {
/* error */ /* error */
} }
return EOF; return post_process_weof(s);
} }
switch (GLOBAL_Stream[sno].encoding) { switch (GLOBAL_Stream[sno].encoding) {
case ENC_OCTET: case ENC_OCTET:
@ -981,12 +844,7 @@ get_wchar(int sno)
/* error */ /* error */
/* try to recover character, assume this is our first character */ /* try to recover character, assume this is our first character */
wchar_t och = GLOBAL_Stream[sno].och; wchar_t och = GLOBAL_Stream[sno].och;
return och;
GLOBAL_Stream[sno].och = ch;
GLOBAL_Stream[sno].stream_getc = PlUnGetc;
GLOBAL_Stream[sno].stream_wgetc = get_wchar;
GLOBAL_Stream[sno].stream_gets = DefaultGets;
return och;
} }
if (!how_many) { if (!how_many) {
return wch; return wch;
@ -1033,6 +891,12 @@ get_wchar(int sno)
return EOF; return EOF;
} }
int
get_wchar(int sno)
{
return post_process_read_char( get_wchar__( sno ), GLOBAL_Stream+sno );
}
#ifndef MB_LEN_MAX #ifndef MB_LEN_MAX
#define MB_LEN_MAX 6 #define MB_LEN_MAX 6
#endif #endif
@ -1075,8 +939,7 @@ handle_write_encoding_error(int sno, wchar_t ch)
int int
put_wchar(int sno, wchar_t ch) put_wchar(int sno, wchar_t ch)
{ {
/* pass the bucck if we can */
/* pass the bug if we can */
switch (GLOBAL_Stream[sno].encoding) { switch (GLOBAL_Stream[sno].encoding) {
case ENC_OCTET: case ENC_OCTET:
return GLOBAL_Stream[sno].stream_putc(sno, ch); return GLOBAL_Stream[sno].stream_putc(sno, ch);
@ -1284,42 +1147,31 @@ write_bom(int sno, StreamDesc *st)
static void static void
check_bom(int sno, StreamDesc *st) check_bom(int sno, StreamDesc *st)
{ {
int ch1, ch2, ch3, ch4;
int ch; ch1 = st->stream_getc(sno);
switch(ch1) {
ch = st->stream_getc(sno);
if (ch == EOFCHAR) {
st->och = ch;
st->stream_getc = PlUnGetc;
st->stream_wgetc = get_wchar;
st->stream_gets = DefaultGets;
return;
}
switch(ch) {
case 0x00: case 0x00:
{ {
ch = st->stream_getc(sno); ch2 = st->stream_getc(sno);
if (ch == EOFCHAR || ch != 0x00) { if ( ch2 != 0x00) {
st->och = ch; ungetc( ch1, st->file );
st->stream_getc = PlUnGetc00; ungetc( ch2, st->file );
st->stream_wgetc = get_wchar;
st->stream_gets = DefaultGets;
return; return;
} else { } else {
ch = st->stream_getc(sno); ch3 = st->stream_getc(sno);
if (ch == EOFCHAR || ch != 0xFE) { if (ch3 == EOFCHAR || ch3 != 0xFE) {
st->och = ch; ungetc( ch1, st->file );
st->stream_getc = PlUnGetc0000; ungetc( ch2, st->file );
st->stream_wgetc = get_wchar; ungetc( ch3, st->file );
st->stream_gets = DefaultGets;
return; return;
} else { } else {
ch = st->stream_getc(sno); ch4 = st->stream_getc(sno);
if (ch == EOFCHAR || ch != 0xFF) { if (ch4 == EOFCHAR || ch3 != 0xFF) {
st->och = ch; ungetc( ch1, st->file );
st->stream_getc = PlUnGetc0000fe; ungetc( ch2, st->file );
st->stream_wgetc = get_wchar; ungetc( ch3, st->file );
st->stream_gets = DefaultGets; ungetc( ch4, st->file );
return; return;
} else { } else {
st->status |= HAS_BOM_f; st->status |= HAS_BOM_f;
@ -1331,12 +1183,10 @@ check_bom(int sno, StreamDesc *st)
} }
case 0xFE: case 0xFE:
{ {
ch = st->stream_getc(sno); ch2 = st->stream_getc(sno);
if (ch != 0xFF) { if (ch2 != 0xFF) {
st->och = ch; ungetc( ch1, st->file );
st->stream_getc = PlUnGetc376; ungetc( ch2, st->file );
st->stream_wgetc = get_wchar;
st->stream_gets = DefaultGets;
return; return;
} else { } else {
st->status |= HAS_BOM_f; st->status |= HAS_BOM_f;
@ -1346,27 +1196,26 @@ check_bom(int sno, StreamDesc *st)
} }
case 0xFF: case 0xFF:
{ {
ch = st->stream_getc(sno); ch2 = st->stream_getc(sno);
if (ch != 0xFE) { if (ch2 != 0xFE) {
st->och = ch; ungetc( ch1, st->file );
st->stream_getc = PlUnGetc377; ungetc( ch2, st->file );
st->stream_wgetc = get_wchar;
st->stream_gets = DefaultGets;
return; return;
} else { } else {
ch = st->stream_getc(sno); ch3 = st->stream_getc(sno);
if (ch == EOFCHAR || ch != 0x00) { if ( ch3 != 0x00) {
st->och = ch; ungetc( ch1, st->file );
st->stream_getc = PlUnGetc377376; ungetc( ch2, st->file );
st->stream_wgetc = get_wchar; ungetc( ch3, st->file );
st->stream_gets = DefaultGets; return;
} else { } else {
ch = st->stream_getc(sno); ch4 = st->stream_getc(sno);
if (ch == EOFCHAR || ch != 0x00) { if (ch4 != 0x00) {
st->och = ch; ungetc( ch1, st->file );
st->stream_getc = PlUnGetc37737600; ungetc( ch2, st->file );
st->stream_wgetc = get_wchar; ungetc( ch3, st->file );
st->stream_gets = DefaultGets; ungetc( ch4, st->file );
return;
} else { } else {
st->status |= HAS_BOM_f; st->status |= HAS_BOM_f;
st->encoding = ENC_ISO_UTF32_LE; st->encoding = ENC_ISO_UTF32_LE;
@ -1379,20 +1228,17 @@ check_bom(int sno, StreamDesc *st)
} }
} }
case 0xEF: case 0xEF:
ch = st->stream_getc(sno); ch2 = st->stream_getc(sno);
if (ch != 0xBB) { if (ch2 != 0xBB) {
st->och = ch; ungetc( ch1, st->file );
st->stream_getc = PlUnGetc357; ungetc( ch2, st->file );
st->stream_wgetc = get_wchar; return;
st->stream_gets = DefaultGets;
return;
} else { } else {
ch = st->stream_getc(sno); ch3 = st->stream_getc(sno);
if (ch != 0xBF) { if (ch3 != 0xBF) {
st->och = ch; ungetc( ch1, st->file );
st->stream_getc = PlUnGetc357273; ungetc( ch2, st->file );
st->stream_wgetc = get_wchar; ungetc( ch3, st->file );
st->stream_gets = DefaultGets;
return; return;
} else { } else {
st->status |= HAS_BOM_f; st->status |= HAS_BOM_f;
@ -1400,12 +1246,8 @@ check_bom(int sno, StreamDesc *st)
return; return;
} }
} }
default: default:
st->och = ch; ungetc( ch1, st->file );
st->stream_getc = PlUnGetc;
st->stream_wgetc = get_wchar;
st->stream_gets = DefaultGets;
return;
} }
} }
@ -1548,8 +1390,11 @@ do_open ( Term file_name, Term t2, Term tlist USES_REGS )
} }
/* get options */ /* get options */
xarg *args = Yap_ArgListToVector ( tlist, open_defs, OPEN_END ); xarg *args = Yap_ArgListToVector ( tlist, open_defs, OPEN_END );
if (args == NULL) if (args == NULL) {
if (LOCAL_Error_TYPE)
Yap_Error(LOCAL_Error_TYPE, LOCAL_Error_Term, "option handling in open/3");
return FALSE; return FALSE;
}
/* done */ /* done */
sno = GetFreeStreamD(); sno = GetFreeStreamD();
if (sno < 0) if (sno < 0)
@ -1579,7 +1424,7 @@ do_open ( Term file_name, Term t2, Term tlist USES_REGS )
fname = Yap_AbsoluteFile( fname, LOCAL_FileNameBuf); fname = Yap_AbsoluteFile( fname, LOCAL_FileNameBuf);
} else { } else {
if (!strncpy(LOCAL_FileNameBuf, fname, YAP_FILENAME_MAX)) if (!strncpy(LOCAL_FileNameBuf, fname, YAP_FILENAME_MAX))
return (PlIOError (SYSTEM_ERROR_INTERNAL,file_name,"file name is too long in open/3")); return PlIOError (SYSTEM_ERROR_INTERNAL,file_name,"file name is too long in open/3");
} }
} else if (trueGlobalPrologFlag(OPEN_EXPANDS_FILENAME_FLAG)) { } else if (trueGlobalPrologFlag(OPEN_EXPANDS_FILENAME_FLAG)) {
fname = Yap_AbsoluteFile( fname, LOCAL_FileNameBuf); fname = Yap_AbsoluteFile( fname, LOCAL_FileNameBuf);
@ -1651,6 +1496,7 @@ do_open ( Term file_name, Term t2, Term tlist USES_REGS )
Yap_SetTextFile (RepAtom (AtomOfTerm (file_name))->StrOfAE); Yap_SetTextFile (RepAtom (AtomOfTerm (file_name))->StrOfAE);
} }
#endif #endif
flags &= ~(Free_Stream_f);
if (!initStream( sno, fd, fname, file_name, encoding, flags, open_mode )) if (!initStream( sno, fd, fname, file_name, encoding, flags, open_mode ))
return false; return false;
if (open_mode == AtomWrite ) { if (open_mode == AtomWrite ) {
@ -1670,7 +1516,6 @@ do_open ( Term file_name, Term t2, Term tlist USES_REGS )
} }
flags &= ~(Free_Stream_f);
UNLOCK(st->streamlock); UNLOCK(st->streamlock);
{ {
Term t = Yap_MkStream (sno); Term t = Yap_MkStream (sno);
@ -1730,7 +1575,7 @@ p_open_null_stream (USES_REGS1)
st->stream_putc = NullPutc; st->stream_putc = NullPutc;
st->stream_wputc = put_wchar; st->stream_wputc = put_wchar;
st->stream_getc = PlGetc; st->stream_getc = PlGetc;
st->stream_gets = PlGetsFunc(); st->stream_gets = PlGets;
st->stream_wgetc = get_wchar; st->stream_wgetc = get_wchar;
st->stream_wgetc_for_read = get_wchar; st->stream_wgetc_for_read = get_wchar;
if (st->encoding == ENC_ISO_UTF8) if (st->encoding == ENC_ISO_UTF8)
@ -1808,28 +1653,43 @@ CheckStream (Term arg, int kind, const char *msg)
} }
if (GLOBAL_Stream[sno].status & Free_Stream_f) if (GLOBAL_Stream[sno].status & Free_Stream_f)
{ {
PlIOError(EXISTENCE_ERROR_STREAM, arg, msg); Yap_Error(EXISTENCE_ERROR_STREAM, arg, msg);
return (-1); return (-1);
} }
LOCK(GLOBAL_Stream[sno].streamlock); LOCK(GLOBAL_Stream[sno].streamlock);
if ((GLOBAL_Stream[sno].status & kind) == 0) if (( kind & Input_Stream_f) && !(GLOBAL_Stream[sno].status & Input_Stream_f))
{
UNLOCK(GLOBAL_Stream[sno].streamlock);
PlIOError(PERMISSION_ERROR_INPUT_STREAM, arg, msg);
}
if ((kind & (Append_Stream_f|Output_Stream_f)) && ! (GLOBAL_Stream[sno].status & Output_Stream_f))
{ {
UNLOCK(GLOBAL_Stream[sno].streamlock); UNLOCK(GLOBAL_Stream[sno].streamlock);
if (kind & Input_Stream_f) PlIOError(PERMISSION_ERROR_OUTPUT_STREAM, arg, msg);
PlIOError(PERMISSION_ERROR_INPUT_STREAM, arg, msg);
else
PlIOError(PERMISSION_ERROR_OUTPUT_STREAM, arg, msg);
return (-1);
} }
return (sno); return (sno);
} }
int int
Yap_CheckStream (Term arg, int kind, const char *msg) Yap_CheckStream (Term arg, int kind, const char *msg)
{ {
return CheckStream(arg, kind, (char *)msg); return CheckStream(arg, kind, (char *)msg);
} }
int
Yap_CheckTextStream (Term arg, int kind, const char *msg)
{
int sno;
if ((sno = CheckStream(arg, kind, (char *)msg)) < 0)
return -1;
if ((GLOBAL_Stream[sno].status & Binary_Stream_f)) {
UNLOCK(GLOBAL_Stream[sno].streamlock);
return -1;
}
return sno;
}
static Int static Int

View File

@ -52,6 +52,7 @@ typedef enum{ /* we accept two domains for the moment, IPV6 may follow */
extern Term Yap_InitSocketStream(int, socket_info, socket_domain); extern Term Yap_InitSocketStream(int, socket_info, socket_domain);
extern int Yap_CheckStream(Term, int, const char *); extern int Yap_CheckStream(Term, int, const char *);
extern int Yap_CheckTextStream(Term, int, const char *);
extern int Yap_CheckSocketStream(Term, const char *); extern int Yap_CheckSocketStream(Term, const char *);
extern socket_domain Yap_GetSocketDomain(int); extern socket_domain Yap_GetSocketDomain(int);
extern socket_info Yap_GetSocketStatus(int); extern socket_info Yap_GetSocketStatus(int);
@ -305,13 +306,13 @@ int console_post_process_read_char( int, StreamDesc *);
int console_post_process_eof( StreamDesc *); int console_post_process_eof( StreamDesc *);
int post_process_read_char( int, StreamDesc *); int post_process_read_char( int, StreamDesc *);
int post_process_eof( StreamDesc *); int post_process_eof( StreamDesc *);
int post_process_weof( StreamDesc *);
bool is_same_tty(FILE *f1, FILE *f2); bool is_same_tty(FILE *f1, FILE *f2);
int ISOWGetc (int sno); int ISOWGetc (int sno);
int GetUTF8 (int sno); int GetUTF8 (int sno);
Term read_line(int sno); Term read_line(int sno);
int PlUnGetc( int);
int PlGets (int sno, UInt size, char *buf); int PlGets (int sno, UInt size, char *buf);
GetsFunc PlGetsFunc(void); GetsFunc PlGetsFunc(void);
int PlGetc (int sno); int PlGetc (int sno);
@ -419,9 +420,6 @@ StreamPosition(int sno)
Term sargs[5]; Term sargs[5];
Int cpos; Int cpos;
cpos = GLOBAL_Stream[sno].charcount; cpos = GLOBAL_Stream[sno].charcount;
if (GLOBAL_Stream[sno].stream_getc == PlUnGetc) {
cpos--;
}
sargs[0] = MkIntegerTerm (LOCAL_StartCharCount = cpos); sargs[0] = MkIntegerTerm (LOCAL_StartCharCount = cpos);
sargs[1] = MkIntegerTerm (LOCAL_StartLineCount = GLOBAL_Stream[sno].linecount); sargs[1] = MkIntegerTerm (LOCAL_StartLineCount = GLOBAL_Stream[sno].linecount);
sargs[2] = MkIntegerTerm (LOCAL_StartLinePos = GLOBAL_Stream[sno].linepos); sargs[2] = MkIntegerTerm (LOCAL_StartLinePos = GLOBAL_Stream[sno].linepos);

View File

@ -153,8 +153,9 @@ MemPutc(int sno, int ch)
int int
Yap_open_buf_read_stream(const char *nbuf, size_t nchars, encoding_t enc, memBufSource src) Yap_open_buf_read_stream(const char *nbuf, size_t nchars, encoding_t *encp, memBufSource src)
{ {
CACHE_REGS
int sno; int sno;
StreamDesc *st; StreamDesc *st;
@ -180,7 +181,10 @@ MemPutc(int sno, int ch)
st->linepos = 0; st->linepos = 0;
st->charcount = 0; st->charcount = 0;
st->linecount = 1; st->linecount = 1;
st->encoding = enc; if (encp)
st->encoding = *encp;
else
st->encoding = LOCAL_encoding;
UNLOCK(st->streamlock); UNLOCK(st->streamlock);
return sno; return sno;
} }
@ -227,14 +231,15 @@ open_mem_read_stream (USES_REGS1) /* $open_mem_read_stream(+List,-Stream) */
ti = TailOfTerm(ti); ti = TailOfTerm(ti);
} }
nbuf[nchars] = '\0'; nbuf[nchars] = '\0';
sno = Yap_open_buf_read_stream(nbuf, nchars, LOCAL_encoding, MEM_BUF_CODE); sno = Yap_open_buf_read_stream(nbuf, nchars, &LOCAL_encoding, MEM_BUF_CODE);
t = Yap_MkStream (sno); t = Yap_MkStream (sno);
return (Yap_unify (ARG2, t)); return (Yap_unify (ARG2, t));
} }
int int
Yap_open_buf_write_stream(char *buf, size_t nchars, encoding_t enc, memBufSource sr) Yap_open_buf_write_stream(char *buf, size_t nchars, encoding_t *encp, memBufSource sr)
{ {
CACHE_REGS
int sno; int sno;
StreamDesc *st; StreamDesc *st;
@ -258,7 +263,10 @@ Yap_open_buf_write_stream(char *buf, size_t nchars, encoding_t enc, memBufSourc
st->linepos = 0; st->linepos = 0;
st->charcount = 0; st->charcount = 0;
st->linecount = 1; st->linecount = 1;
st->encoding = enc; if (encp)
st->encoding = *encp;
else
st->encoding = LOCAL_encoding;
Yap_DefaultStreamOps( st ); Yap_DefaultStreamOps( st );
#if MAY_WRITE #if MAY_WRITE
st->file = open_memstream(&st->nbuf, &st->nsize); st->file = open_memstream(&st->nbuf, &st->nsize);
@ -287,7 +295,7 @@ Yap_OpenBufWriteStream( USES_REGS1 )
return -1; return -1;
} }
} }
return Yap_open_buf_write_stream(nbuf, sz, GLOBAL_Stream[LOCAL_c_output_stream].encoding, 0); return Yap_open_buf_write_stream(nbuf, sz, &GLOBAL_Stream[LOCAL_c_output_stream].encoding, 0);
} }
static Int static Int

View File

@ -432,10 +432,6 @@ static xarg *setReadEnv(Term opts, FEnv *fe, struct renv *re, int inp_stream) {
} }
re->seekable = (GLOBAL_Stream[inp_stream].status & Seekable_Stream_f) != 0; re->seekable = (GLOBAL_Stream[inp_stream].status & Seekable_Stream_f) != 0;
if (re->seekable) { if (re->seekable) {
if (GLOBAL_Stream[inp_stream].stream_getc == PlUnGetc) {
re->had_ungetc = TRUE;
re->ungetc_oldc = GLOBAL_Stream[inp_stream].och;
}
#if HAVE_FGETPOS #if HAVE_FGETPOS
fgetpos(GLOBAL_Stream[inp_stream].file, &re->rpos); fgetpos(GLOBAL_Stream[inp_stream].file, &re->rpos);
#else #else
@ -676,10 +672,6 @@ static parser_state_t scanError(REnv *re, FEnv *fe, int inp_stream) {
} }
} }
// go back to the start // go back to the start
if (re->had_ungetc) {
GLOBAL_Stream[inp_stream].stream_getc = PlUnGetc;
GLOBAL_Stream[inp_stream].och = re->ungetc_oldc;
}
if (re->seekable) { if (re->seekable) {
if (GLOBAL_Stream[inp_stream].status & InMemory_Stream_f) { if (GLOBAL_Stream[inp_stream].status & InMemory_Stream_f) {
GLOBAL_Stream[inp_stream].u.mem_string.pos = re->cpos; GLOBAL_Stream[inp_stream].u.mem_string.pos = re->cpos;
@ -808,7 +800,7 @@ static Int read_term(
/* needs to change LOCAL_output_stream for write */ /* needs to change LOCAL_output_stream for write */
yhandle_t h = Yap_InitSlot(ARG2); yhandle_t h = Yap_InitSlot(ARG2);
inp_stream = Yap_CheckStream(ARG1, Input_Stream_f, "read/3"); inp_stream = Yap_CheckTextStream(ARG1, Input_Stream_f, "read/3");
if (inp_stream == -1) { if (inp_stream == -1) {
return (FALSE); return (FALSE);
} }
@ -876,10 +868,6 @@ static xarg *setClauseReadEnv(Term opts, FEnv *fe, struct renv *re,
fe->ce = Yap_CharacterEscapes(CurrentModule); fe->ce = Yap_CharacterEscapes(CurrentModule);
re->seekable = (GLOBAL_Stream[inp_stream].status & Seekable_Stream_f) != 0; re->seekable = (GLOBAL_Stream[inp_stream].status & Seekable_Stream_f) != 0;
if (re->seekable) { if (re->seekable) {
if (GLOBAL_Stream[inp_stream].stream_getc == PlUnGetc) {
re->had_ungetc = TRUE;
re->ungetc_oldc = GLOBAL_Stream[inp_stream].och;
}
#if HAVE_FGETPOS #if HAVE_FGETPOS
fgetpos(GLOBAL_Stream[inp_stream].file, &re->rpos); fgetpos(GLOBAL_Stream[inp_stream].file, &re->rpos);
#else #else
@ -995,7 +983,7 @@ static Int read_clause(
Term t3 = Deref(ARG3); Term t3 = Deref(ARG3);
yhandle_t h = Yap_InitSlot(ARG2); yhandle_t h = Yap_InitSlot(ARG2);
/* needs to change LOCAL_output_stream for write */ /* needs to change LOCAL_output_stream for write */
inp_stream = Yap_CheckStream(ARG1, Input_Stream_f, "read/3"); inp_stream = Yap_CheckTextStream(ARG1, Input_Stream_f, "read/3");
out = Yap_read_term(inp_stream, t3, -3); out = Yap_read_term(inp_stream, t3, -3);
UNLOCK(GLOBAL_Stream[inp_stream].streamlock); UNLOCK(GLOBAL_Stream[inp_stream].streamlock);
return out && Yap_unify(Yap_GetFromSlot(h), out); return out && Yap_unify(Yap_GetFromSlot(h), out);
@ -1036,7 +1024,7 @@ static Int read2(
Int out; Int out;
/* needs to change LOCAL_output_stream for write */ /* needs to change LOCAL_output_stream for write */
inp_stream = Yap_CheckStream(ARG1, Input_Stream_f, "read/3"); inp_stream = Yap_CheckTextStream(ARG1, Input_Stream_f, "read/3");
if (inp_stream == -1) { if (inp_stream == -1) {
return (FALSE); return (FALSE);
} }
@ -1129,7 +1117,7 @@ static Int style_checker(USES_REGS1) {
return TRUE; return TRUE;
} }
Term Yap_StringToTerm(const char *s, size_t len, encoding_t enc, int prio, Term Yap_StringToTerm(const char *s, size_t len, encoding_t *encp, int prio,
Term *bindings) { Term *bindings) {
CACHE_REGS CACHE_REGS
Term bvar = MkVarTerm(), ctl; Term bvar = MkVarTerm(), ctl;
@ -1144,7 +1132,7 @@ Term Yap_StringToTerm(const char *s, size_t len, encoding_t enc, int prio,
} }
Term rval; Term rval;
int stream = Yap_open_buf_read_stream(s, len, enc, MEM_BUF_USER); int stream = Yap_open_buf_read_stream(s, len, encp, MEM_BUF_USER);
rval = Yap_read_term(stream, ctl, 3); rval = Yap_read_term(stream, ctl, 3);
Yap_CloseStream(stream); Yap_CloseStream(stream);
@ -1162,12 +1150,14 @@ Term Yap_ReadFromAtom(Atom a, Term opts) {
if (IsWideAtom(a)) { if (IsWideAtom(a)) {
wchar_t *ws = a->WStrOfAE; wchar_t *ws = a->WStrOfAE;
size_t len = wcslen(ws); size_t len = wcslen(ws);
sno = Yap_open_buf_read_stream((char *)ws, len, ENC_ISO_ANSI, MEM_BUF_USER); encoding_t enc = ENC_ISO_ANSI;
sno = Yap_open_buf_read_stream((char *)ws, len, &enc, MEM_BUF_USER);
} else { } else {
char *s = a->StrOfAE; char *s = a->StrOfAE;
size_t len = strlen(s); size_t len = strlen(s);
encoding_t enc = ENC_ISO_LATIN1;
sno = sno =
Yap_open_buf_read_stream((char *)s, len, ENC_ISO_LATIN1, MEM_BUF_USER); Yap_open_buf_read_stream((char *)s, len, &enc, MEM_BUF_USER);
} }
rval = Yap_read_term(sno, opts, 3); rval = Yap_read_term(sno, opts, 3);
@ -1178,7 +1168,8 @@ Term Yap_ReadFromAtom(Atom a, Term opts) {
static Term readFromBuffer(const char *s, Term opts) { static Term readFromBuffer(const char *s, Term opts) {
Term rval; Term rval;
int sno; int sno;
sno = Yap_open_buf_read_stream((char *)s, strlen_utf8((unsigned char *)s), ENC_ISO_UTF8, encoding_t enc = ENC_ISO_UTF8;
sno = Yap_open_buf_read_stream((char *)s, strlen_utf8((unsigned char *)s), &enc,
MEM_BUF_USER); MEM_BUF_USER);
rval = Yap_read_term(sno, opts, 3); rval = Yap_read_term(sno, opts, 3);
@ -1231,7 +1222,7 @@ term_to_string(USES_REGS1) {
const char * s; const char * s;
if (IsVarTerm(t2)) { if (IsVarTerm(t2)) {
size_t length; size_t length;
s = Yap_TermToString(ARG1, NULL, 0, &length, 0, Quote_illegal_f|Handle_vars_f); s = Yap_TermToString(ARG1, NULL, 0, &length, NULL, Quote_illegal_f|Handle_vars_f);
if (!s || ! if (!s || !
MkStringTerm(s)) { MkStringTerm(s)) {
Yap_Error(RESOURCE_ERROR_HEAP,t1,"Could not get memory from the operating system"); Yap_Error(RESOURCE_ERROR_HEAP,t1,"Could not get memory from the operating system");
@ -1265,7 +1256,7 @@ term_to_atom(USES_REGS1) {
if (IsVarTerm(t1)) { if (IsVarTerm(t1)) {
size_t length; size_t length;
char * s = char * s =
Yap_TermToString(t1, NULL, 0, &length, 0, Quote_illegal_f|Handle_vars_f); Yap_TermToString(t1, NULL, 0, &length, NULL, Quote_illegal_f|Handle_vars_f);
if (!s || !(at = Yap_LookupAtom(s))) { if (!s || !(at = Yap_LookupAtom(s))) {
Yap_Error(RESOURCE_ERROR_HEAP,t1,"Could not get memory from the operating system"); Yap_Error(RESOURCE_ERROR_HEAP,t1,"Could not get memory from the operating system");
return false; return false;
@ -1340,7 +1331,8 @@ static Int read_term_from_string(USES_REGS1) {
len = strlen_utf8(s); len = strlen_utf8(s);
} }
char *ss = (char *)s; char *ss = (char *)s;
int sno = Yap_open_buf_read_stream(ss, len, ENC_ISO_UTF8, MEM_BUF_USER); encoding_t enc = ENC_ISO_UTF8;
int sno = Yap_open_buf_read_stream(ss, len, &enc, MEM_BUF_USER);
rc = readFromBuffer(ss, Deref(ARG3)); rc = readFromBuffer(ss, Deref(ARG3));
Yap_CloseStream(sno); Yap_CloseStream(sno);
if (!rc) if (!rc)
@ -1378,7 +1370,8 @@ static Int read_term_from_atomic(USES_REGS1) {
len = strlen_utf8(( unsigned char *)s); len = strlen_utf8(( unsigned char *)s);
} }
char *ss = (char *)s; char *ss = (char *)s;
int sno = Yap_open_buf_read_stream(ss, len, ENC_ISO_UTF8, MEM_BUF_USER); encoding_t enc = ENC_ISO_UTF8;
int sno = Yap_open_buf_read_stream(ss, len, &enc, MEM_BUF_USER);
rc = readFromBuffer(ss, Deref(ARG3)); rc = readFromBuffer(ss, Deref(ARG3));
Yap_CloseStream(sno); Yap_CloseStream(sno);
if (!rc) if (!rc)

View File

@ -83,7 +83,7 @@ rl_to_codes(Term TEnd, int do_as_binary, int arity USES_REGS)
end = Deref(XREGS[arity]); end = Deref(XREGS[arity]);
if (GLOBAL_Stream[sno].encoding == ENC_ISO_UTF8) if (GLOBAL_Stream[sno].encoding == ENC_ISO_UTF8)
return Yap_unify(ARG2, Yap_UTF8ToDiffListOfCodes((const char *)TR, end PASS_REGS)) ; return Yap_unify(ARG2, Yap_UTF8ToDiffListOfCodes((const char *)TR, end PASS_REGS)) ;
else if (GLOBAL_Stream[sno].encoding == PL_ENC_WCHAR) else if (GLOBAL_Stream[sno].encoding == ENC_WCHAR)
return Yap_unify(ARG2, Yap_WCharsToDiffListOfCodes((const wchar_t *)TR, end PASS_REGS)) ; return Yap_unify(ARG2, Yap_WCharsToDiffListOfCodes((const wchar_t *)TR, end PASS_REGS)) ;
return Yap_unify(ARG2, Yap_CharsToDiffListOfCodes((const char *)TR, end, ENC_ISO_LATIN1 PASS_REGS)) ; return Yap_unify(ARG2, Yap_CharsToDiffListOfCodes((const char *)TR, end, ENC_ISO_LATIN1 PASS_REGS)) ;
} }

View File

@ -137,9 +137,9 @@ int GetFreeStreamD(void) {
UNLOCK(GLOBAL_StreamDescLock); UNLOCK(GLOBAL_StreamDescLock);
return -1; return -1;
} }
GLOBAL_Stream[sno].encoding = LOCAL_encoding;
LOCK(GLOBAL_Stream[sno].streamlock); LOCK(GLOBAL_Stream[sno].streamlock);
UNLOCK(GLOBAL_StreamDescLock); UNLOCK(GLOBAL_StreamDescLock);
GLOBAL_Stream[sno].encoding = LOCAL_encoding;
return sno; return sno;
} }
@ -588,6 +588,7 @@ static bool do_stream_property(int sno,
} }
} }
} }
UNLOCK(GLOBAL_Stream[sno].streamlock);
return rc; return rc;
} }
@ -657,7 +658,8 @@ static Int stream_property(USES_REGS1) { /* Init current_stream */
i = Yap_CheckStream(t1, Input_Stream_f | Output_Stream_f | Append_Stream_f, i = Yap_CheckStream(t1, Input_Stream_f | Output_Stream_f | Append_Stream_f,
"current_stream/3"); "current_stream/3");
if (i < 0) { if (i < 0) {
cut_fail(); UNLOCK(GLOBAL_Stream[i].streamlock);
cut_fail();
} }
args = Yap_ArgListToVector(Deref(ARG2), stream_property_defs, args = Yap_ArgListToVector(Deref(ARG2), stream_property_defs,
STREAM_PROPERTY_END); STREAM_PROPERTY_END);

View File

@ -310,8 +310,13 @@ write_term2 ( USES_REGS1 )
/* notice: we must have ASP well set when using portray, otherwise /* notice: we must have ASP well set when using portray, otherwise
we cannot make recursive Prolog calls */ we cannot make recursive Prolog calls */
xarg * args = Yap_ArgListToVector ( ARG2, write_defs, WRITE_END ); xarg * args = Yap_ArgListToVector ( ARG2, write_defs, WRITE_END );
if (args == NULL) if (args == NULL) {
if (LOCAL_Error_TYPE == DOMAIN_ERROR_OUT_OF_RANGE)
LOCAL_Error_TYPE = DOMAIN_ERROR_WRITE_OPTION;
if (LOCAL_Error_TYPE)
Yap_Error(LOCAL_Error_TYPE, LOCAL_Error_Term, NULL);
return false; return false;
}
yhandle_t mySlots = Yap_StartSlots(); yhandle_t mySlots = Yap_StartSlots();
int output_stream = LOCAL_c_output_stream; int output_stream = LOCAL_c_output_stream;
if (output_stream == -1) output_stream = 1; if (output_stream == -1) output_stream = 1;
@ -335,11 +340,18 @@ write_term3 ( USES_REGS1 )
/* notice: we must have ASP well set when using portray, otherwise /* notice: we must have ASP well set when using portray, otherwise
we cannot make recursive Prolog calls */ we cannot make recursive Prolog calls */
yhandle_t mySlots = Yap_StartSlots();
xarg *args = Yap_ArgListToVector ( ARG3, write_defs, WRITE_END ); xarg *args = Yap_ArgListToVector ( ARG3, write_defs, WRITE_END );
if (args == NULL) if (args == NULL) {
if (LOCAL_Error_TYPE == DOMAIN_ERROR_OUT_OF_RANGE)
LOCAL_Error_TYPE = DOMAIN_ERROR_WRITE_OPTION;
if (LOCAL_Error_TYPE)
Yap_Error(LOCAL_Error_TYPE, LOCAL_Error_Term, NULL);
return false; return false;
int output_stream = Yap_CheckStream (ARG1, Output_Stream_f, "write/2"); }
int output_stream = Yap_CheckTextStream (ARG1, Output_Stream_f, "write/2");
if (output_stream < 0 )
return false;
yhandle_t mySlots = Yap_StartSlots();
write_term( output_stream, ARG2, args PASS_REGS); write_term( output_stream, ARG2, args PASS_REGS);
UNLOCK(GLOBAL_Stream[output_stream].streamlock); UNLOCK(GLOBAL_Stream[output_stream].streamlock);
Yap_CloseSlots( mySlots ); Yap_CloseSlots( mySlots );
@ -360,14 +372,23 @@ write2 ( USES_REGS1 )
we cannot make recursive Prolog calls */ we cannot make recursive Prolog calls */
xarg *args = Yap_ArgListToVector ( TermNil, write_defs, WRITE_END ); xarg *args;
if (args == NULL) yhandle_t mySlots;
int output_stream = Yap_CheckTextStream (ARG1, Output_Stream_f, "write/2");
if (output_stream < 0 )
return false; return false;
yhandle_t mySlots = Yap_StartSlots(); args = Yap_ArgListToVector ( TermNil, write_defs, WRITE_END );
int output_stream = Yap_CheckStream (ARG1, Output_Stream_f, "write/2"); if (args == NULL) {
if (LOCAL_Error_TYPE == DOMAIN_ERROR_OUT_OF_RANGE)
LOCAL_Error_TYPE = DOMAIN_ERROR_WRITE_OPTION;
if (LOCAL_Error_TYPE)
Yap_Error(LOCAL_Error_TYPE, LOCAL_Error_Term, NULL);
return false;
}
mySlots = Yap_StartSlots();
args[WRITE_NUMBERVARS].used = true; args[WRITE_NUMBERVARS].used = true;
args[WRITE_NUMBERVARS].tvalue = TermTrue; args[WRITE_NUMBERVARS].tvalue = TermTrue;
write_term( output_stream, ARG2, args PASS_REGS); write_term( output_stream, ARG2, args PASS_REGS);
UNLOCK(GLOBAL_Stream[output_stream].streamlock); UNLOCK(GLOBAL_Stream[output_stream].streamlock);
Yap_CloseSlots( mySlots ); Yap_CloseSlots( mySlots );
if (EX != 0L) { if (EX != 0L) {
@ -385,13 +406,16 @@ write1 ( USES_REGS1 )
/* notice: we must have ASP well set when using portray, otherwise /* notice: we must have ASP well set when using portray, otherwise
we cannot make recursive Prolog calls */ we cannot make recursive Prolog calls */
int output_stream = LOCAL_c_output_stream; int output_stream = LOCAL_c_output_stream;
if (output_stream == -1) output_stream = 1; if (output_stream == -1) output_stream = 1;
xarg * args = Yap_ArgListToVector ( TermNil, write_defs, WRITE_END ); xarg * args = Yap_ArgListToVector ( TermNil, write_defs, WRITE_END );
if (args == NULL) if (args == NULL) {
if (LOCAL_Error_TYPE)
Yap_Error(LOCAL_Error_TYPE, LOCAL_Error_Term, NULL);
return false; return false;
}
yhandle_t mySlots = Yap_StartSlots(); yhandle_t mySlots = Yap_StartSlots();
args[WRITE_NUMBERVARS].used = true; args[WRITE_NUMBERVARS].used = true;
args[WRITE_NUMBERVARS].tvalue = TermTrue; args[WRITE_NUMBERVARS].tvalue = TermTrue;
LOCK(GLOBAL_Stream[output_stream].streamlock); LOCK(GLOBAL_Stream[output_stream].streamlock);
write_term( output_stream, ARG1, args PASS_REGS); write_term( output_stream, ARG1, args PASS_REGS);
@ -415,8 +439,11 @@ write_canonical1 ( USES_REGS1 )
int output_stream = LOCAL_c_output_stream; int output_stream = LOCAL_c_output_stream;
if (output_stream == -1) output_stream = 1; if (output_stream == -1) output_stream = 1;
xarg * args = Yap_ArgListToVector ( TermNil, write_defs, WRITE_END ); xarg * args = Yap_ArgListToVector ( TermNil, write_defs, WRITE_END );
if (args == NULL) if (args == NULL) {
if (LOCAL_Error_TYPE)
Yap_Error(LOCAL_Error_TYPE, LOCAL_Error_Term, NULL);
return false; return false;
}
yhandle_t mySlots = Yap_StartSlots(); yhandle_t mySlots = Yap_StartSlots();
args[WRITE_IGNORE_OPS].used = true; args[WRITE_IGNORE_OPS].used = true;
args[WRITE_IGNORE_OPS].tvalue = TermTrue; args[WRITE_IGNORE_OPS].tvalue = TermTrue;
@ -442,10 +469,15 @@ write_canonical ( USES_REGS1 )
/* notice: we must have ASP well set when using portray, otherwise /* notice: we must have ASP well set when using portray, otherwise
we cannot make recursive Prolog calls */ we cannot make recursive Prolog calls */
xarg * args = Yap_ArgListToVector ( TermNil, write_defs, WRITE_END ); xarg * args = Yap_ArgListToVector ( TermNil, write_defs, WRITE_END );
if (args == NULL) if (args == NULL) {
if (LOCAL_Error_TYPE)
Yap_Error(LOCAL_Error_TYPE, LOCAL_Error_Term, NULL);
return false;
}
int output_stream = Yap_CheckTextStream (ARG1, Output_Stream_f, "write/2");
if (output_stream < 0 )
return false; return false;
yhandle_t mySlots = Yap_StartSlots(); yhandle_t mySlots = Yap_StartSlots();
int output_stream = Yap_CheckStream (ARG1, Output_Stream_f, "write/2");
args[WRITE_IGNORE_OPS].used = true; args[WRITE_IGNORE_OPS].used = true;
args[WRITE_IGNORE_OPS].tvalue = TermTrue; args[WRITE_IGNORE_OPS].tvalue = TermTrue;
args[WRITE_QUOTED].used = true; args[WRITE_QUOTED].used = true;
@ -469,8 +501,11 @@ writeq1 ( USES_REGS1 )
/* notice: we must have ASP well set when using portray, otherwise /* notice: we must have ASP well set when using portray, otherwise
we cannot make recursive Prolog calls */ we cannot make recursive Prolog calls */
xarg *args = Yap_ArgListToVector ( TermNil, write_defs, WRITE_END ); xarg *args = Yap_ArgListToVector ( TermNil, write_defs, WRITE_END );
if (args == NULL) if (args == NULL) {
if (LOCAL_Error_TYPE)
Yap_Error(LOCAL_Error_TYPE, LOCAL_Error_Term, NULL);
return false; return false;
}
yhandle_t mySlots = Yap_StartSlots(); yhandle_t mySlots = Yap_StartSlots();
int output_stream = LOCAL_c_output_stream; int output_stream = LOCAL_c_output_stream;
if (output_stream == -1) output_stream = 1; if (output_stream == -1) output_stream = 1;
@ -499,10 +534,15 @@ writeq ( USES_REGS1 )
/* notice: we must have ASP well set when using portray, otherwise /* notice: we must have ASP well set when using portray, otherwise
we cannot make recursive Prolog calls */ we cannot make recursive Prolog calls */
xarg *args = Yap_ArgListToVector ( TermNil, write_defs, WRITE_END ); xarg *args = Yap_ArgListToVector ( TermNil, write_defs, WRITE_END );
if (args == NULL) if (args == NULL) {
if (LOCAL_Error_TYPE)
Yap_Error(LOCAL_Error_TYPE, LOCAL_Error_Term, NULL);
return false;
}
int output_stream = Yap_CheckTextStream (ARG1, Output_Stream_f, "write/2");
if (output_stream < 0 )
return false; return false;
yhandle_t mySlots = Yap_StartSlots(); yhandle_t mySlots = Yap_StartSlots();
int output_stream = Yap_CheckStream (ARG1, Output_Stream_f, "write/2");
args[WRITE_NUMBERVARS].used = true; args[WRITE_NUMBERVARS].used = true;
args[WRITE_NUMBERVARS].tvalue = TermTrue; args[WRITE_NUMBERVARS].tvalue = TermTrue;
args[WRITE_QUOTED].used = true; args[WRITE_QUOTED].used = true;
@ -527,8 +567,11 @@ print1 ( USES_REGS1 )
/* notice: we must have ASP well set when using portray, otherwise /* notice: we must have ASP well set when using portray, otherwise
we cannot make recursive Prolog calls */ we cannot make recursive Prolog calls */
xarg *args = Yap_ArgListToVector ( TermNil, write_defs, WRITE_END ); xarg *args = Yap_ArgListToVector ( TermNil, write_defs, WRITE_END );
if (args == NULL) if (args == NULL) {
if (LOCAL_Error_TYPE)
Yap_Error(LOCAL_Error_TYPE, LOCAL_Error_Term, NULL);
return false; return false;
}
yhandle_t mySlots = Yap_StartSlots(); yhandle_t mySlots = Yap_StartSlots();
int output_stream = LOCAL_c_output_stream; int output_stream = LOCAL_c_output_stream;
if (output_stream == -1) output_stream = 1; if (output_stream == -1) output_stream = 1;
@ -557,9 +600,14 @@ print ( USES_REGS1 )
/* notice: we must have ASP well set when using portray, otherwise /* notice: we must have ASP well set when using portray, otherwise
we cannot make recursive Prolog calls */ we cannot make recursive Prolog calls */
xarg *args = Yap_ArgListToVector ( TermNil, write_defs, WRITE_END ); xarg *args = Yap_ArgListToVector ( TermNil, write_defs, WRITE_END );
if (args == NULL) if (args == NULL) {
if (LOCAL_Error_TYPE)
Yap_Error(LOCAL_Error_TYPE, LOCAL_Error_Term, NULL);
return false;
}
int output_stream = Yap_CheckTextStream (ARG1, Output_Stream_f, "write/2");
if (output_stream < 0 )
return false; return false;
int output_stream = Yap_CheckStream (ARG1, Output_Stream_f, "write/2");
yhandle_t mySlots = Yap_StartSlots(); yhandle_t mySlots = Yap_StartSlots();
args[WRITE_PORTRAY].used = true; args[WRITE_PORTRAY].used = true;
args[WRITE_PORTRAY].tvalue = TermTrue; args[WRITE_PORTRAY].tvalue = TermTrue;
@ -586,8 +634,11 @@ writeln1 ( USES_REGS1 )
int output_stream = LOCAL_c_output_stream; int output_stream = LOCAL_c_output_stream;
if (output_stream == -1) output_stream = 1; if (output_stream == -1) output_stream = 1;
xarg *args = Yap_ArgListToVector ( TermNil, write_defs, WRITE_END ); xarg *args = Yap_ArgListToVector ( TermNil, write_defs, WRITE_END );
if (args == NULL) if (args == NULL) {
if (LOCAL_Error_TYPE)
Yap_Error(LOCAL_Error_TYPE, LOCAL_Error_Term, NULL);
return false; return false;
}
yhandle_t mySlots = Yap_StartSlots(); yhandle_t mySlots = Yap_StartSlots();
args[WRITE_NL].used = true; args[WRITE_NL].used = true;
args[WRITE_NL].tvalue = TermTrue; args[WRITE_NL].tvalue = TermTrue;
@ -614,9 +665,12 @@ writeln ( USES_REGS1 )
/* notice: we must have ASP well set when using portray, otherwise /* notice: we must have ASP well set when using portray, otherwise
we cannot make recursive Prolog calls */ we cannot make recursive Prolog calls */
xarg *args = Yap_ArgListToVector ( TermNil, write_defs, WRITE_END ); xarg *args = Yap_ArgListToVector ( TermNil, write_defs, WRITE_END );
if (args == NULL ) if (args == NULL) {
if (LOCAL_Error_TYPE)
Yap_Error(LOCAL_Error_TYPE, LOCAL_Error_Term, NULL);
return false; return false;
int output_stream = Yap_CheckStream (ARG1, Output_Stream_f, "writeln/2"); }
int output_stream = Yap_CheckTextStream (ARG1, Output_Stream_f, "writeln/2");
if (output_stream < 0) if (output_stream < 0)
return false; return false;
yhandle_t mySlots = Yap_StartSlots(); yhandle_t mySlots = Yap_StartSlots();

View File

@ -98,8 +98,8 @@ int Yap_PlFGetchar(void);
int Yap_GetCharForSIGINT(void); int Yap_GetCharForSIGINT(void);
Int Yap_StreamToFileNo(Term); Int Yap_StreamToFileNo(Term);
int Yap_OpenStream(FILE *,char *,Term,int); int Yap_OpenStream(FILE *,char *,Term,int);
char *Yap_TermToString(Term t, char *s, size_t sz, size_t *length, encoding_t encoding, int flags); char *Yap_TermToString(Term t, char *s, size_t sz, size_t *length, encoding_t *encoding, int flags);
char *Yap_HandleToString(yhandle_t l, size_t sz, size_t *length, encoding_t encoding, int flags); char *Yap_HandleToString(yhandle_t l, size_t sz, size_t *length, encoding_t *encoding, int flags);
int Yap_GetFreeStreamD(void); int Yap_GetFreeStreamD(void);
int Yap_GetFreeStreamDForReading(void); int Yap_GetFreeStreamDForReading(void);
@ -143,11 +143,11 @@ typedef enum mem_buf_source {
char * Yap_MemStreamBuf( int sno ); char * Yap_MemStreamBuf( int sno );
extern Term Yap_StringToTerm(const char *s, size_t len, encoding_t enc, int prio, Term *bindings_p); extern Term Yap_StringToTerm(const char *s, size_t len, encoding_t* encp, int prio, Term *bindings_p);
extern Term Yap_StringToNumberTerm(char *s, encoding_t encp); extern Term Yap_StringToNumberTerm(char *s, encoding_t *encp);
int Yap_FormatFloat(Float f, char **s, size_t sz); int Yap_FormatFloat(Float f, char **s, size_t sz);
int Yap_open_buf_read_stream(const char *nbuf, size_t nchars, encoding_t enc, memBufSource src); int Yap_open_buf_read_stream(const char *nbuf, size_t nchars, encoding_t *encp, memBufSource src);
int Yap_open_buf_write_stream( char *nbuf, size_t nchars, encoding_t enc, memBufSource src); int Yap_open_buf_write_stream( char *nbuf, size_t nchars, encoding_t *encp, memBufSource src);
Term Yap_ReadFromAtom(Atom a, Term opts); Term Yap_ReadFromAtom(Atom a, Term opts);
FILE *Yap_GetInputStream(Term t, const char *m); FILE *Yap_GetInputStream(Term t, const char *m);
FILE *Yap_GetOutputStream(Term t,const char *m); FILE *Yap_GetOutputStream(Term t,const char *m);